BMS查询改成远程帧

This commit is contained in:
lyq 2026-03-19 15:22:13 +08:00
parent 15b9046d8b
commit e6f549cc2a

View File

@ -1,30 +1,29 @@
#ifndef CAN_STRUCT_H
#define CAN_STRUCT_H
#include <cstdint>
#include <array>
#include <string.h>
#include <array>
#include <cstdint>
#include <iostream>
#include "can_driver.h"
#pragma pack(push, 1) // 禁止结构体补齐确保8字节紧密排列
#pragma pack(push, 1) // 禁止结构体补齐确保8字节紧密排列
struct can_BMS_query_0x100
{
// 报文ID与控制标志定义
static constexpr uint32_t CMD_ID = 0x100; // 标准帧ID
static constexpr bool EXT_FLAG = false; // 11位标准帧
static constexpr bool RTR_FLAG = false; // 普通数据帧(非远程帧)
static constexpr uint8_t DLC = 8; // 数据长度为8字节
static constexpr uint32_t CMD_ID = 0x100; // 标准帧ID
static constexpr bool EXT_FLAG = false; // 11位标准帧
static constexpr bool RTR_FLAG = true; // 普通数据帧(非远程帧)
static constexpr uint8_t DLC = 8; // 数据长度为8字节
// 数据区主机查询时通常发送全0
uint8_t data[8];
// 构造函数初始化数据为0
can_BMS_query_0x100()
{
memset(data, 0, sizeof(data));
}
can_BMS_query_0x100() { memset(data, 0, sizeof(data)); }
// 生成CAN帧
CANFrame toFrame() const
@ -42,19 +41,16 @@ struct can_BMS_query_0x100
struct can_BMS_query_0x101
{
// 报文ID与控制标志定义
static constexpr uint32_t CMD_ID = 0x101; // 标准帧ID
static constexpr bool EXT_FLAG = false; // 11位标准帧
static constexpr bool RTR_FLAG = false; // 普通数据帧(非远程帧)
static constexpr uint8_t DLC = 8; // 数据长度为8字节
static constexpr uint32_t CMD_ID = 0x101; // 标准帧ID
static constexpr bool EXT_FLAG = false; // 11位标准帧
static constexpr bool RTR_FLAG = true; // 普通数据帧(非远程帧)
static constexpr uint8_t DLC = 8; // 数据长度为8字节
// 数据区主机查询时通常发送全0
uint8_t data[8];
// 构造函数初始化数据为0
can_BMS_query_0x101()
{
memset(data, 0, sizeof(data));
}
can_BMS_query_0x101() { memset(data, 0, sizeof(data)); }
// 生成CAN帧
CANFrame toFrame() const
@ -85,10 +81,10 @@ union can_MCU_cmd_union
// Byte 3: 开关信号状态
struct
{
uint8_t cmd_status : 2; // BIT0-1: 命令状态(0空挡,1后退,2前进,3保留)
uint8_t brake : 1; // BIT2: 刹车开关(1=开)
uint8_t reserved_bit3 : 1; // BIT3: 保留
uint8_t reserved_bits47 : 4; // BIT4-7: 保留
uint8_t cmd_status : 2; // BIT0-1: 命令状态(0空挡,1后退,2前进,3保留)
uint8_t brake : 1; // BIT2: 刹车开关(1=开)
uint8_t reserved_bit3 : 1; // BIT3: 保留
uint8_t reserved_bits47 : 4; // BIT4-7: 保留
} switch_status;
// Byte 4-7: 保留
@ -105,14 +101,14 @@ union can_EPS_cmd_union
{
struct
{
uint8_t control_mode; // 控制方式
uint8_t reserve1; // 保留
uint8_t reserve2; // 保留
uint8_t angle_h; // 角度高位
uint8_t angle_l; // 角度低位
uint8_t center_cmd; // 角度对中指令
uint8_t angular_speed; // 角速度控制
uint8_t checksum; // 异或校验
uint8_t control_mode; // 控制方式
uint8_t reserve1; // 保留
uint8_t reserve2; // 保留
uint8_t angle_h; // 角度高位
uint8_t angle_l; // 角度低位
uint8_t center_cmd; // 角度对中指令
uint8_t angular_speed; // 角速度控制
uint8_t checksum; // 异或校验
} fields;
uint8_t bytes[8];
@ -134,36 +130,30 @@ struct can_MCU_cmd
{
memset(&data, 0, sizeof(data));
// 初始化默认值
data.fields.can_enabled = 0; // 默认不使能
data.fields.switch_status.cmd_status = 0; // 默认空挡
data.fields.switch_status.brake = 1; // 默认刹车
data.fields.can_enabled = 0; // 默认不使能
data.fields.switch_status.cmd_status = 0; // 默认空挡
data.fields.switch_status.brake = 1; // 默认刹车
}
// 设置使能状态
void setEnabled(bool en)
{
data.fields.can_enabled = en ? 1 : 0;
}
void setEnabled(bool en) { data.fields.can_enabled = en ? 1 : 0; }
// 设置档位 (0空挡,1后退,2前进,3保留)
void setGear(uint8_t gear)
{
data.fields.switch_status.cmd_status = gear & 0x03; // 只保留低2位
data.fields.switch_status.cmd_status = gear & 0x03; // 只保留低2位
}
// 设置刹车状态
void setBrake(bool brake_on)
{
data.fields.switch_status.brake = brake_on ? 1 : 0;
}
void setBrake(bool brake_on) { data.fields.switch_status.brake = brake_on ? 1 : 0; }
// 设置转速 (0-6000rpm)
void setRPM(uint16_t rpm)
{
// 限制转速范围0-6000
uint16_t clamped_rpm = std::clamp(rpm, static_cast<uint16_t>(0), static_cast<uint16_t>(6000));
data.fields.speed_low = clamped_rpm & 0xFF; // 低字节
data.fields.speed_high = (clamped_rpm >> 8) & 0xFF; // 高字节
data.fields.speed_low = clamped_rpm & 0xFF; // 低字节
data.fields.speed_high = (clamped_rpm >> 8) & 0xFF; // 高字节
}
CANFrame toFrame() const
@ -208,10 +198,7 @@ struct can_EPS_cmd
data.fields.angle_l = raw & 0xFF;
}
void setCenterCmd(uint8_t cmd)
{
data.fields.center_cmd = cmd;
}
void setCenterCmd(uint8_t cmd) { data.fields.center_cmd = cmd; }
void setAngularSpeed(uint16_t angle_speed)
{
@ -285,52 +272,28 @@ struct can_VCU_motor1_cmd
int8_t data[8]{};
// 设置Byte0
void setByte0(int8_t direction)
{
data[0] = std::clamp(direction, static_cast<int8_t>(0), static_cast<int8_t>(2));
}
void setByte0(int8_t direction) { data[0] = std::clamp(direction, static_cast<int8_t>(0), static_cast<int8_t>(2)); }
// 设置Byte1
void setByte1(int8_t value)
{
data[1] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte1(int8_t value) { data[1] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte2
void setByte2(int8_t value)
{
data[2] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte2(int8_t value) { data[2] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte3
void setByte3(int8_t value)
{
data[3] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte3(int8_t value) { data[3] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte4
void setByte4(int8_t value)
{
data[4] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte4(int8_t value) { data[4] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte5
void setByte5(int8_t value)
{
data[5] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte5(int8_t value) { data[5] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte6
void setByte6(int8_t value)
{
data[6] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte6(int8_t value) { data[6] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte7
void setByte7(int8_t value)
{
data[7] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte7(int8_t value) { data[7] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
CANFrame toFrame() const
{
@ -354,52 +317,28 @@ struct can_VCU_motor2_cmd
int8_t data[8]{};
// 设置Byte0
void setByte0(int8_t value)
{
data[0] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte0(int8_t value) { data[0] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte1
void setByte1(int8_t value)
{
data[1] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte1(int8_t value) { data[1] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte2
void setByte2(int8_t value)
{
data[2] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte2(int8_t value) { data[2] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte3
void setByte3(int8_t value)
{
data[3] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte3(int8_t value) { data[3] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte4
void setByte4(int8_t value)
{
data[4] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte4(int8_t value) { data[4] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte5
void setByte5(int8_t value)
{
data[5] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte5(int8_t value) { data[5] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte6
void setByte6(int8_t value)
{
data[6] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte6(int8_t value) { data[6] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
// 设置Byte7
void setByte7(int8_t value)
{
data[7] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100));
}
void setByte7(int8_t value) { data[7] = std::clamp(value, static_cast<int8_t>(-100), static_cast<int8_t>(100)); }
CANFrame toFrame() const
{