添加故障码的上报

This commit is contained in:
root 2025-05-16 13:40:17 +08:00
parent ba6717c923
commit 32e17e29e4
3 changed files with 116 additions and 36 deletions

View File

@ -20,29 +20,13 @@ public:
// 添加错误码(自动去重)
void addErrorCode(int code)
{
auto result = errors.insert(code);
if (result.second)
{
std::cout << "Added error code: " << code << std::endl;
}
else
{
std::cout << "Error code " << code << " already exists.\n";
}
errors.insert(code);
}
// 删除错误码
void removeErrorCode(int code)
{
size_t erased = errors.erase(code);
if (erased > 0)
{
std::cout << "Removed error code: " << code << std::endl;
}
else
{
std::cout << "Error code " << code << " not found.\n";
}
errors.erase(code);
}
// 打印所有当前错误码
@ -75,6 +59,7 @@ private:
// 故障字典声明(在 .cpp 实现里定义)
extern std::unordered_map<int, FaultInfo> faultMap;
extern const uint8_t mcuErrorCode[25];
extern const uint8_t epsErrorCode[18];
extern ErrorCodeSet vehicle_error_code;
std::string generateFaultJson(int code, const std::string &vin, int64_t timestamp);

View File

@ -39,6 +39,8 @@ bool updateFaultLevel(int code, int newLevel)
const uint8_t mcuErrorCode[25] = {1, 2, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 30, 31, 32, 33, 34};
const uint8_t epsErrorCode[18] = {0x10, 0x14, 0x12, 0x21, 0x22, 0x23, 0x24, 0x25, 0x32, 0x33, 0x35, 0x37, 0x43, 0x46, 0x51, 0x55, 0x61, 0x62};
std::unordered_map<int, FaultInfo> faultMap = {
{1001, {"BMS", "BMS_TEMP_HIGH_ERR", "温度过高", 0}},
{1002, {"BMS", "BMS_TEMP_LOW_ERR", "温度过低", 0}},
@ -114,20 +116,36 @@ std::unordered_map<int, FaultInfo> faultMap = {
{1217, {"EPS", "EPS_MIN_GEAR_BREAK_ERR", "小齿轮角度传感器断开", 0}},
{1218, {"EPS", "EPS_MED_GEAR_BREAK_ERR", "中齿轮角度传感器断开", 0}},
{1301, {"MBC", "MBC_ZERO_CURR_DRIFT_ERR", "零点电流漂移", 0}},
{1302, {"MBC", "MBC_MS_COMM_ERR", "主从芯片通讯故障", 0}},
{1303, {"MBC", "MBC_OVER_CURR_ERR", "过流", 0}},
{1304, {"MBC", "MBC_PHASE_LOSS_ERR", "缺相(也有可能会由于大电流导致)", 0}},
{1305, {"MBC", "MBC_MOS_ERR", "MOS故障", 0}},
{1306, {"MBC", "MBC_LOCKED_ROTOR_ERR", "堵转", 0}},
{1307, {"MBC", "MBC_OVER_VOL_ERR", "过压", 0}},
{1308, {"MBC", "MBC_UNDER_VOL_ERR", "欠压", 0}},
{1301, {"MBC", "MBCM_ZERO_CURR_DRIFT_ERR", "零点电流漂移", 0}},
{1302, {"MBC", "MBCM_MS_COMM_ERR", "主从芯片通讯故障", 0}},
{1303, {"MBC", "MBCM_OVER_CURR_ERR", "过流", 0}},
{1304, {"MBC", "MBCM_PHASE_LOSS_ERR", "缺相(也有可能会由于大电流导致)", 0}},
{1305, {"MBC", "MBCM_MOS_ERR", "MOS故障", 0}},
{1306, {"MBC", "MBCM_LOCKED_ROTOR_ERR", "堵转", 0}},
{1307, {"MBC", "MBCM_OVER_VOL_ERR", "过压", 0}},
{1308, {"MBC", "MBCM_UNDER_VOL_ERR", "欠压", 0}},
{1309, {"MBC", "MBCS_ZERO_CURR_DRIFT_ERR", "零点电流漂移", 0}},
{1310, {"MBC", "MBCS_MS_COMM_ERR", "主从芯片通讯故障", 0}},
{1311, {"MBC", "MBCS_OVER_CURR_ERR", "过流", 0}},
{1312, {"MBC", "MBCS_PHASE_LOSS_ERR", "缺相(也有可能会由于大电流导致)", 0}},
{1313, {"MBC", "MBCS_MOS_ERR", "MOS故障", 0}},
{1314, {"MBC", "MBCS_LOCKED_ROTOR_ERR", "堵转", 0}},
{1315, {"MBC", "MBCS_OVER_VOL_ERR", "过压", 0}},
{1316, {"MBC", "MBCS_UNDER_VOL_ERR", "欠压", 0}},
{1401, {"EBC", "EBC_ZERO_CURR_DRIFT_ERR", "零点电流漂移", 0}},
{1402, {"EBC", "EBC_MS_COMM_ERR", "主从芯片通讯故障", 0}},
{1403, {"EBC", "EBC_OVER_CURR_ERR", "过流", 0}},
{1404, {"EBC", "EBC_PHASE_LOSS_ERR", "缺相(也有可能会由于大电流导致)", 0}},
{1405, {"EBC", "EBC_MOS_ERR", "MOS故障", 0}},
{1406, {"EBC", "EBC_LOCKED_ROTOR_ERR", "堵转", 0}},
{1407, {"EBC", "EBC_OVER_VOL_ERR", "过压", 0}},
{1408, {"EBC", "EBC_UNDER_VOL_ERR", "欠压", 0}}};
{1401, {"EBC", "EBCM_ZERO_CURR_DRIFT_ERR", "零点电流漂移", 0}},
{1402, {"EBC", "EBCM_MS_COMM_ERR", "主从芯片通讯故障", 0}},
{1403, {"EBC", "EBCM_OVER_CURR_ERR", "过流", 0}},
{1404, {"EBC", "EBCM_PHASE_LOSS_ERR", "缺相(也有可能会由于大电流导致)", 0}},
{1405, {"EBC", "EBCM_MOS_ERR", "MOS故障", 0}},
{1406, {"EBC", "EBCM_LOCKED_ROTOR_ERR", "堵转", 0}},
{1407, {"EBC", "EBCM_OVER_VOL_ERR", "过压", 0}},
{1408, {"EBC", "EBCM_UNDER_VOL_ERR", "欠压", 0}},
{1409, {"EBC", "EBCS_ZERO_CURR_DRIFT_ERR", "零点电流漂移", 0}},
{1410, {"EBC", "EBCS_MS_COMM_ERR", "主从芯片通讯故障", 0}},
{1411, {"EBC", "EBCS_OVER_CURR_ERR", "过流", 0}},
{1412, {"EBC", "EBCS_PHASE_LOSS_ERR", "缺相(也有可能会由于大电流导致)", 0}},
{1413, {"EBC", "EBCS_MOS_ERR", "MOS故障", 0}},
{1414, {"EBC", "EBCS_LOCKED_ROTOR_ERR", "堵转", 0}},
{1415, {"EBC", "EBCS_OVER_VOL_ERR", "过压", 0}},
{1416, {"EBC", "EBCS_UNDER_VOL_ERR", "欠压", 0}}};

View File

@ -12,8 +12,6 @@ GeneralMsg info_report; // 常规消息上报
// 解析can报文做消息上报
void Msg_Handler(const mc::msg::CanFrame::SharedPtr msg)
{
info_report.timestamp = getCurrentTimestampMs();
switch (msg->id)
{
case 0x1821E5F1:
@ -98,6 +96,84 @@ void Msg_Handler(const mc::msg::CanFrame::SharedPtr msg)
}
break;
}
case 0x401:
{
// 清空所有 EPS 错误码
for (int code = 1201; code <= 1218; ++code)
{
vehicle_error_code.removeErrorCode(code);
}
// 添加当前报文中的错误码最多2个
uint8_t data_bytes[2] = {msg->data[3], msg->data[7]};
for (uint8_t byte : data_bytes)
{
for (int i = 0; i < 18; ++i)
{
if (byte == epsErrorCode[i])
{
vehicle_error_code.addErrorCode(1201 + i);
break; // 每个 byte 最多匹配一个错误码
}
}
}
break;
}
case 0x0CFF26EF:
{
uint8_t byte = msg->data[7]; // 主芯片
for (int i = 0; i < 8; ++i)
{
uint8_t bit_flag = (byte >> (7 - i)) & 0x01;
if (bit_flag)
{
vehicle_error_code.addErrorCode(1401 + i);
break; // 每个 byte 最多匹配一个错误码
}
}
byte = msg->data[5];
for (int i = 0; i < 8; ++i)
{
uint8_t bit_flag = (byte >> (7 - i)) & 0x01;
if (bit_flag)
{
vehicle_error_code.addErrorCode(1409 + i);
break; // 每个 byte 最多匹配一个错误码
}
}
break;
}
case 0x0CFF36EF:
{
uint8_t byte = msg->data[7]; // 主芯片
for (int i = 0; i < 8; ++i)
{
uint8_t bit_flag = (byte >> (7 - i)) & 0x01;
if (bit_flag)
{
vehicle_error_code.addErrorCode(1301 + i);
break; // 每个 byte 最多匹配一个错误码
}
}
byte = msg->data[5];
for (int i = 0; i < 8; ++i)
{
uint8_t bit_flag = (byte >> (7 - i)) & 0x01;
if (bit_flag)
{
vehicle_error_code.addErrorCode(1309 + i);
break; // 每个 byte 最多匹配一个错误码
}
}
break;
}
}
}
@ -151,6 +227,7 @@ private:
void info_timer_callback()
{
info_report.timestamp = getCurrentTimestampMs();
std::string json_msg = pack_general_info_to_json(info_report);
mqtt_client_.publish(info_topic_, json_msg);
}