Author SHA1 Message Date
zjk a870071490 合并优化后的代码 2025-04-09 23:28:35 +08:00
zjk b653f17b03 完善待机工况及相关逻辑 2025-04-09 23:28:05 +08:00
zjk 785a47ff99 优化通信代码,修复状态机的bug 2025-04-09 15:11:31 +08:00
zjk bac5f5291e 优化了上位机的通信函数 2025-04-07 23:32:35 +08:00
zjk 35e5d5230e 修复了udp的错误发送数据就报错的故障,莫名其妙好的,真是奇怪 2025-04-07 22:16:56 +08:00
zjk b5a746c2ef 测试 2025-04-07 21:45:19 +08:00
zjk 7ef9ec5ac0 优化结构 2025-04-07 21:18:02 +08:00
zjk c47660fcb1 使用tinyFSM优化了状态机 2025-04-07 11:40:16 +08:00
zjk ebf1701335 使用tinyfsm优化代码 2025-04-06 23:51:03 +08:00
zjk 002df307d7 使用tinyfsm 2025-04-06 21:42:55 +08:00
zjk 8677811dac 使用tinyfsm 2025-04-06 21:27:37 +08:00
zjk c51eb7073a 使用tinyfsm 2025-04-06 20:16:37 +08:00
36 changed files with 3085 additions and 1430 deletions
+26
View File
@@ -0,0 +1,26 @@
@startuml PowerManagerFsm状态图
[*] --> InitState
state InitState {
[*] --> 等待复合管控消息
等待复合管控消息 --> 等待关键断路器闭合: 连接成功
等待复合管控消息 --> FaultState: 超时(10秒)
等待关键断路器闭合 --> StandbyState: 关键断路器闭合
等待关键断路器闭合 --> FaultState: 系统错误
}
state StandbyState {
[*] --> 待机
待机 --> FaultState: 检测到错误
待机 --> 处理电源命令: 收到命令
处理电源命令 --> 待机: 完成处理
}
state FaultState {
[*] --> 故障处理
}
InitState --> FaultState : FaultEvent(faultCode=1/2)
StandbyState --> FaultState : FaultEvent(faultCode)
InitState --> StandbyState : StandbyEvent
@enduml
+1 -4
View File
@@ -1,8 +1,5 @@
//------------------------------------------------
// pPowerManger config block
ProcessConfig = pPowerManger
{
AppTick = 5
AppTick = 4
CommsTick = 4
}
+3
View File
@@ -0,0 +1,3 @@
{
"value_array_uint16" :[2,1,3]
}
+1 -1
View File
@@ -6,7 +6,7 @@
"HVABbusBVol_uint16": 0,
"HVABmastSteGeaCur_uint16": 0,
"HVABopenWaterCur_uint16": 0,
"HVABxczCur_uint16": 0,
"HVABxczCur_uint16": 1,
"HVBBCCUCur_uint16": 0,
"HVBBFCSSecurSysCur_uint16": 0,
"HVBBbatInsCur_uint16": 0,
+3 -3
View File
@@ -1,8 +1,8 @@
{
"HVolABusBreaker_unit16": 0,
"HVolBBusBreaker_unit16": 0,
"HVolBusBreaker_uint16": 0,
"LVolBusBreaker_uint16": 0,
"HVolBBusBreaker_unit16": 79,
"HVolBusBreaker_uint16": 58,
"LVolBusBreaker_uint16": 159,
"faultCode_array":
[
0
+16 -16
View File
@@ -1,30 +1,30 @@
{
"backpressure1_fp32": 0.0,
"backpressure2_fp32": 0.0,
"ch4oConcent1_fp32": 0.0,
"ch4oConcent2_fp32": 0.0,
"backpressure1_fp32": 1.0,
"backpressure2_fp32": 2.0,
"ch4oConcent1_fp32": 29.0,
"ch4oConcent2_fp32": 96.0,
"ch4o_fp32": 0.0,
"excTemp_fp32": 0.0,
"excTemp_fp32": 23.0,
"faultCode1_uint16": 0,
"faultCode2_uint16": 0,
"faultCode3_uint16": 0,
"fcuCurIns_fp32": 0.0,
"fcuCurPower_fp32": 0.0,
"fcuCur_fp32": 0.0,
"fcuCur_fp32": 84.0,
"fcuFaultLeve_uint8": 0,
"fcuOutIns_fp32": 0.0,
"fcuOutPower_fp32": 0.0,
"fcuOutIns_fp32": 53.0,
"fcuOutPower_fp32": 100.0,
"fcuState_uint8": 0,
"fcuVolIns_fp32": 0.0,
"fcuVolPower_fp32": 0.0,
"fcuVol_fp32": 0.0,
"flameDet_uint8": 0,
"h2Concent1_fp32": 0.0,
"h2Concent2_fp32": 0.0,
"o2Concent1_fp32": 0.0,
"o2Concent2_fp32": 0.0,
"fcuVol_fp32": 377.0,
"flameDet_uint8": 1,
"h2Concent1_fp32": 89.0,
"h2Concent2_fp32": 62.0,
"o2Concent1_fp32": 74.0,
"o2Concent2_fp32": 21.0,
"o2Level_fp32": 0.0,
"o2_fp32": 0.0,
"powLimit_fp32": 0.0,
"powerGen_fp32": 0.0
"powLimit_fp32": 88.0,
"powerGen_fp32": 622.0
}
+11 -11
View File
@@ -2,37 +2,37 @@
"fcuCurToInsLimit1_uint8": 0,
"fcuCurToinsLimit2_uint8": 0,
"insBatCurElect_uint16": 0,
"insBatLimit_uint16": 0,
"insBatSoc_uint8": 0,
"insBatTotalElect_uint16": 0,
"insBatLimit_uint16": 41,
"insBatSoc_uint8": 42,
"insBatTotalElect_uint16": 617,
"insChargeState_uint8": 0,
"insCurSocHold1_uint8": 0,
"insCurSocHold2_uint8": 0,
"insCurSocHold3_uint8": 0,
"insCurrent_uint16": 0,
"insCurrent_uint16": 2,
"insLinkVol_uint16": 0,
"insNegRelayState_uint8": 0,
"insNegRes_uint16": 0,
"insPackVol_uint16": 0,
"insPosRelayState_uint8": 0,
"insPosRelayState_uint8": 1,
"insPosRes_uint16": 0,
"insfaultCode_uint8": 0,
"powBatCurElect_uint16": 0,
"powBatSoc_uint8": 0,
"powBatTotalElect_uint16": 0,
"powBatSoc_uint8": 31,
"powBatTotalElect_uint16": 763,
"powChargeState_uint8": 0,
"powCurSocHold1_uint8": 0,
"powCurSocHold2_uint8": 0,
"powCurSocHold3_uint8": 0,
"powCurToInsLimit1_uint8": 0,
"powCurToinsLimit2_uint8": 0,
"powCur_uint16": 0,
"powCur_uint16": 21,
"powLinkVol_uint16": 0,
"powNegRelayState_uint8": 0,
"powNegRelayState_uint8": 1,
"powNegRes_uint16": 0,
"powPosRelayState_uint8": 0,
"powPosRelayState_uint8": 1,
"powPosRes_uint16": 0,
"powerBatLimit_uint16": 0,
"powerBatLimit_uint16": 13,
"powerPackVol_uint16": 0,
"powfaultCode_uint8": 0
}
@@ -0,0 +1,52 @@
{
"protocol_name": "CCU参数设置反馈",
"message_id": "0x0040",
"length": 10,
"checksum": {
"start_offset": 0,
"end_offset": 6,
"algorithm": "sum"
},
"fields": [
{
"name": "start1",
"offset": 0,
"type": "uint8",
"value": "0x20",
"description": "起始字节1"
},
{
"name": "start2",
"offset": 1,
"type": "uint8",
"value": "0x20",
"description": "起始字节2"
},
{
"name": "id",
"offset": 2,
"type": "uint16",
"value": "0x0040",
"description": "消息ID"
},
{
"name": "length",
"offset": 4,
"type": "uint16",
"value": 10,
"description": "消息长度"
},
{
"name": "flag",
"offset": 6,
"type": "uint8",
"description": "设置标志位"
},
{
"name": "failvreCode",
"offset": 7,
"type": "uint8",
"description": "故障代码"
}
]
}
+637
View File
@@ -0,0 +1,637 @@
{
"message_name": "CCUStateFbMsg",
"protocol_id": "0x0041",
"endianness": "little",
"fields": [
{
"name": "start1",
"offset": 0,
"length": 1,
"type": "uint8",
"value": "0x20",
"description": "起始字节1"
},
{
"name": "start2",
"offset": 1,
"length": 1,
"type": "uint8",
"value": "0x20",
"description": "起始字节2"
},
{
"name": "id",
"offset": 2,
"length": 2,
"type": "uint16",
"value": "0x0041",
"description": "消息ID"
},
{
"name": "length",
"offset": 4,
"length": 2,
"type": "uint16",
"dynamic": true,
"description": "数据长度(自动计算)"
},
{
"name": "currentWorkCondition",
"offset": 6,
"length": 1,
"type": "uint8",
"enum": {"0x00":"待机","0x01":"运行","0x02":"故障"},
"description": "当前工作状态"
},
{
"name": "fcuState",
"offset": 7,
"length": 1,
"type": "uint8",
"unit": "%",
"description": "燃料电池状态"
},
{
"name": "fcuFaultLeve",
"offset": 8,
"length": 1,
"type": "uint8",
"description": "故障等级"
},
{
"name": "powerLimit",
"offset": 9,
"length": 4,
"type": "float32",
"unit": "kW",
"description": "功率限制值"
},
{
"name": "totalPowerGeneration",
"offset": 13,
"length": 4,
"type": "float32",
"unit": "kWh",
"description": "总发电量"
},
{
"name": "ch4o",
"offset": 17,
"length": 4,
"type": "float32",
"unit": "ppm",
"description": "甲醇浓度"
},
{
"name": "o2",
"offset": 21,
"length": 4,
"type": "float32",
"unit": "%",
"description": "氧气浓度"
},
{
"name": "fcuVoltage",
"offset": 25,
"length": 4,
"type": "float32",
"unit": "V",
"description": "燃料电池电压"
},
{
"name": "fcuCurrent",
"offset": 29,
"length": 4,
"type": "float32",
"unit": "A",
"description": "燃料电池电流"
},
{
"name": "o2Level",
"offset": 33,
"length": 4,
"type": "float32",
"unit": "%",
"description": "氧气含量"
},
{
"name": "placeholder1",
"offset": 37,
"length": 4,
"type": "float32",
"description": "预留字段1"
},
{
"name": "exchangerTemperature",
"offset": 41,
"length": 4,
"type": "float32",
"unit": "°C",
"description": "换热器温度"
},
{
"name": "fcuOutPower",
"offset": 45,
"length": 4,
"type": "float32",
"unit": "kW",
"description": "燃料电池输出功率"
},
{
"name": "fcuOutIns",
"offset": 49,
"length": 4,
"type": "float32",
"unit": "kΩ",
"description": "绝缘电阻"
},
{
"name": "fcuVoltageIns",
"offset": 53,
"length": 4,
"type": "float32",
"unit": "V",
"description": "绝缘监测电压"
},
{
"name": "fcuVoltagePower",
"offset": 57,
"length": 4,
"type": "float32",
"unit": "V",
"description": "功率电压"
},
{
"name": "fcuCurrentIns",
"offset": 61,
"length": 4,
"type": "float32",
"unit": "A",
"description": "绝缘监测电流"
},
{
"name": "fcuCurrentPower",
"offset": 65,
"length": 4,
"type": "float32",
"unit": "A",
"description": "功率电流"
},
{
"name": "h2Concentration1",
"offset": 69,
"length": 4,
"type": "float32",
"unit": "ppm",
"description": "氢气浓度1"
},
{
"name": "h2Concentration2",
"offset": 73,
"length": 4,
"type": "float32",
"unit": "ppm",
"description": "氢气浓度2"
},
{
"name": "o2Concentration1",
"offset": 77,
"length": 4,
"type": "float32",
"unit": "%",
"description": "氧气浓度1"
},
{
"name": "o2Concentration2",
"offset": 81,
"length": 4,
"type": "float32",
"unit": "%",
"description": "氧气浓度2"
},
{
"name": "ch4oConcentration1",
"offset": 85,
"length": 4,
"type": "float32",
"unit": "ppm",
"description": "甲醇浓度1"
},
{
"name": "ch4oConcentration2",
"offset": 89,
"length": 4,
"type": "float32",
"unit": "ppm",
"description": "甲醇浓度2"
},
{
"name": "backpressure1",
"offset": 93,
"length": 4,
"type": "float32",
"unit": "kPa",
"description": "背压1"
},
{
"name": "backpressure2",
"offset": 97,
"length": 4,
"type": "float32",
"unit": "kPa",
"description": "背压2"
},
{
"name": "flameDetection",
"offset": 101,
"length": 1,
"type": "uint8",
"description": "火焰检测状态"
},
{
"name": "fcuGZ_1L",
"offset": 102,
"length": 1,
"type": "uint8",
"description": "故障字1低字节"
},
{
"name": "fcuGZ_1H",
"offset": 103,
"length": 1,
"type": "uint8",
"description": "故障字1高字节"
},
{
"name": "fcuGZ_2L",
"offset": 104,
"length": 1,
"type": "uint8",
"description": "故障字2低字节"
},
{
"name": "fcuGZ_2H",
"offset": 105,
"length": 1,
"type": "uint8",
"description": "故障字2高字节"
},
{
"name": "fcuGZ_3L",
"offset": 106,
"length": 1,
"type": "uint8",
"description": "故障字3低字节"
},
{
"name": "fcuGZ_3H",
"offset": 107,
"length": 1,
"type": "uint8",
"description": "故障字3高字节"
},
{
"name": "insPosRelayState",
"offset": 108,
"length": 1,
"type": "uint8",
"description": "绝缘正极继电器状态"
},
{
"name": "insNegRelayState",
"offset": 109,
"length": 1,
"type": "uint8",
"description": "绝缘负极继电器状态"
},
{
"name": "powerPosRelayState",
"offset": 110,
"length": 1,
"type": "uint8",
"description": "功率正极继电器状态"
},
{
"name": "powerNegRelayState",
"offset": 111,
"length": 1,
"type": "uint8",
"description": "功率负极继电器状态"
},
{
"name": "insBatLimit",
"offset": 112,
"length": 2,
"type": "uint16",
"unit": "A",
"description": "绝缘电池电流限制"
},
{
"name": "powerBatLimit",
"offset": 114,
"length": 2,
"type": "uint16",
"unit": "A",
"description": "功率电池电流限制"
},
{
"name": "insBatSoc",
"offset": 116,
"length": 1,
"type": "uint8",
"unit": "%",
"description": "绝缘电池SOC"
},
{
"name": "powerBatSoc",
"offset": 117,
"length": 1,
"type": "uint8",
"unit": "%",
"description": "功率电池SOC"
},
{
"name": "insBatTotalElectricity",
"offset": 118,
"length": 2,
"type": "uint16",
"unit": "kWh",
"description": "绝缘电池总电量"
},
{
"name": "powerBatTotalElectricity",
"offset": 120,
"length": 2,
"type": "uint16",
"unit": "kWh",
"description": "功率电池总电量"
},
{
"name": "insBatCurrentElectricity",
"offset": 122,
"length": 2,
"type": "uint16",
"unit": "A",
"description": "绝缘电池当前电流"
},
{
"name": "powerBatCurrentElectricity",
"offset": 124,
"length": 2,
"type": "uint16",
"unit": "A",
"description": "功率电池当前电流"
},
{
"name": "insChargeState",
"offset": 126,
"length": 1,
"type": "uint8",
"description": "绝缘电池充电状态"
},
{
"name": "powerChargeState",
"offset": 127,
"length": 1,
"type": "uint8",
"description": "功率电池充电状态"
},
{
"name": "insLinkVoltage",
"offset": 128,
"length": 2,
"type": "uint16",
"unit": "V",
"description": "绝缘链路电压"
},
{
"name": "insPackVoltage",
"offset": 130,
"length": 2,
"type": "uint16",
"unit": "V",
"description": "绝缘电池组电压"
},
{
"name": "insCurrent",
"offset": 132,
"length": 2,
"type": "uint16",
"unit": "A",
"description": "绝缘电流"
},
{
"name": "insPosInsulationResistance",
"offset": 134,
"length": 2,
"type": "uint16",
"unit": "kΩ",
"description": "正极绝缘电阻"
},
{
"name": "insNegInsulationResistance",
"offset": 136,
"length": 2,
"type": "uint16",
"unit": "kΩ",
"description": "负极绝缘电阻"
},
{
"name": "powerLinkVoltage",
"offset": 138,
"length": 2,
"type": "uint16",
"unit": "V",
"description": "功率链路电压"
},
{
"name": "powerPackVoltage",
"offset": 140,
"length": 2,
"type": "uint16",
"unit": "V",
"description": "功率电池组电压"
},
{
"name": "powerCurrent",
"offset": 142,
"length": 2,
"type": "uint16",
"unit": "A",
"description": "功率电流"
},
{
"name": "powerPosInsulationResistance",
"offset": 144,
"length": 2,
"type": "uint16",
"unit": "kΩ",
"description": "功率正极绝缘电阻"
},
{
"name": "powerNegInsulationResistance",
"offset": 146,
"length": 2,
"type": "uint16",
"unit": "kΩ",
"description": "功率负极绝缘电阻"
},
{
"name": "insEmergencyStatusWord",
"offset": 148,
"length": 1,
"type": "uint8",
"description": "绝缘紧急状态字"
},
{
"name": "powerEmergencyStatusWord",
"offset": 149,
"length": 1,
"type": "uint8",
"description": "功率紧急状态字"
},
{
"name": "insAlarmIdentificationWord",
"offset": 150,
"length": 1,
"type": "uint8",
"description": "绝缘报警标识字"
},
{
"name": "powerAlarmIdentificationWord",
"offset": 151,
"length": 1,
"type": "uint8",
"description": "功率报警标识字"
},
{
"name": "insCurrentSocHold1",
"offset": 152,
"length": 1,
"type": "uint8",
"unit": "%",
"description": "绝缘电流SOC保持1"
},
{
"name": "insCurrentSocHold2",
"offset": 153,
"length": 1,
"type": "uint8",
"unit": "%",
"description": "绝缘电流SOC保持2"
},
{
"name": "insCurrentSocHold3",
"offset": 154,
"length": 1,
"type": "uint8",
"unit": "%",
"description": "绝缘电流SOC保持3"
},
{
"name": "fcuCurrentToInsLimit1",
"offset": 155,
"length": 1,
"type": "uint8",
"unit": "A",
"description": "FCU到绝缘限制1"
},
{
"name": "fcuCurrentToinsLimit2",
"offset": 156,
"length": 1,
"type": "uint8",
"unit": "A",
"description": "FCU到绝缘限制2"
},
{
"name": "powerCurrentSocHold1",
"offset": 157,
"length": 1,
"type": "uint8",
"unit": "%",
"description": "功率电流SOC保持1"
},
{
"name": "powerCurrentSocHold2",
"offset": 158,
"length": 1,
"type": "uint8",
"unit": "%",
"description": "功率电流SOC保持2"
},
{
"name": "powerCurrentSocHold3",
"offset": 159,
"length": 1,
"type": "uint8",
"unit": "%",
"description": "功率电流SOC保持3"
},
{
"name": "powerCurrentToInsLimit1",
"offset": 160,
"length": 1,
"type": "uint8",
"unit": "A",
"description": "功率到绝缘限制1"
},
{
"name": "powerCurrentToinsLimit2",
"offset": 161,
"length": 1,
"type": "uint8",
"unit": "A",
"description": "功率到绝缘限制2"
},
{
"name": "placeholder2",
"offset": 162,
"length": 4,
"type": "float32",
"description": "预留字段2"
},
{
"name": "placeholder3",
"offset": 166,
"length": 4,
"type": "float32",
"description": "预留字段3"
},
{
"name": "placeholder4",
"offset": 170,
"length": 4,
"type": "float32",
"description": "预留字段4"
},
{
"name": "placeholder5",
"offset": 174,
"length": 4,
"type": "float32",
"description": "预留字段5"
},
{
"name": "checkCode",
"offset": 178,
"length": 4,
"type": "uint32",
"description": "校验码(从start1到placeholder5的字节累加和)"
}
],
"metadata": {
"total_length": 182,
"data_section_start": 6,
"data_section_end": 178,
"checksum_range": "0-178",
"float_precision": "IEEE-754单精度",
"reserved_fields": [
"placeholder1",
"placeholder2",
"placeholder3",
"placeholder4",
"placeholder5"
]
}
}
@@ -0,0 +1,142 @@
{
"protocol_name": "高压A母线状态反馈",
"message_id": "0x0006",
"length": 10,
"checksum": {
"start_offset": 0,
"end_offset": 30,
"algorithm": "sum_low_byte"
},
"fields": [
{
"name": "start1",
"offset": 0,
"type": "uint8",
"value": "0x20",
"description": "起始字节1"
},
{
"name": "start2",
"offset": 1,
"type": "uint8",
"value": "0x20",
"description": "起始字节2"
},
{
"name": "id",
"offset": 2,
"type": "uint16",
"value": "0x0006",
"description": "消息ID"
},
{
"name": "length",
"offset": 4,
"type": "uint16",
"value": 10,
"description": "消息长度"
},
{
"name": "bowFTDevice123CircuitBreaker",
"offset": 6,
"type": "uint8",
"description": "艏部FT设备123断路器"
},
{
"name": "actuatorCircuitBreaker",
"offset": 7,
"type": "uint8",
"description": "执行机构断路器"
},
{
"name": "mastSteeringGearControlBoxCircuitBreaker",
"offset": 8,
"type": "uint8",
"description": "桅杆舵机控制箱断路器"
},
{
"name": "xczCircuitBreaker",
"offset": 9,
"type": "uint8",
"description": "XCZ断路器"
},
{
"name": "bowRudderControlBoxCircuitBreaker",
"offset": 10,
"type": "uint8",
"description": "艏舵控制箱断路器"
},
{
"name": "openWaterCoverStartCylinderCircuitBreaker",
"offset": 11,
"type": "uint8",
"description": "开盖启动气缸断路器"
},
{
"name": "instrumentPowerFailureSignal",
"offset": 12,
"type": "uint8",
"description": "仪表电源失效信号"
},
{
"name": "emergencyPowerFailureSignal",
"offset": 13,
"type": "uint8",
"description": "应急电源失效信号"
},
{
"name": "waterIngressionAlarm",
"offset": 14,
"type": "uint8",
"description": "进水报警"
},
{
"name": "busbarBVoltage",
"offset": 15,
"type": "uint16",
"description": "B排电压(V)"
},
{
"name": "busbarBCurrent",
"offset": 17,
"type": "uint16",
"description": "B排电流(A)"
},
{
"name": "bowFTDevice123Current",
"offset": 19,
"type": "uint16",
"description": "艏部FT设备123电流(A)"
},
{
"name": "actuatorCurrent",
"offset": 21,
"type": "uint16",
"description": "执行机构电流(A)"
},
{
"name": "mastSteeringGearControlBoxCurrent",
"offset": 23,
"type": "uint16",
"description": "桅杆舵机控制箱电流(A)"
},
{
"name": "xczCurrent",
"offset": 25,
"type": "uint16",
"description": "XCZ电流(A)"
},
{
"name": "bowRudderControlBoxCurrent",
"offset": 27,
"type": "uint16",
"description": "艏舵控制箱电流(A)"
},
{
"name": "openWaterCoverStartCylinderCurrent",
"offset": 29,
"type": "uint16",
"description": "开盖启动气缸电流(A)"
}
]
}
@@ -0,0 +1,154 @@
{
"protocol_name": "高压B母线状态反馈",
"message_id": "0x0007",
"length": 36,
"checksum": {
"start_offset": 0,
"end_offset": 34,
"algorithm": "sum_low_byte"
},
"fields": [
{
"name": "start1",
"offset": 0,
"type": "uint8",
"value": "0x20",
"description": "起始字节1"
},
{
"name": "start2",
"offset": 1,
"type": "uint8",
"value": "0x20",
"description": "起始字节2"
},
{
"name": "id",
"offset": 2,
"type": "uint16",
"value": "0x0007",
"description": "消息ID"
},
{
"name": "length",
"offset": 4,
"type": "uint16",
"value": 36,
"description": "消息长度(包含校验码)"
},
{
"name": "lithiumBatteryGroupInstrumentCircuitBreaker",
"offset": 6,
"type": "uint8",
"description": "锂电池组仪表断路器"
},
{
"name": "bowLowVoltageDistributionBoxCircuitBreaker",
"offset": 7,
"type": "uint8",
"description": "艏部低压配电箱断路器"
},
{
"name": "unit4InstrumentDC48VCircuitBreaker",
"offset": 8,
"type": "uint8",
"description": "单元4仪表DC48V断路器"
},
{
"name": "dcC1DCDistributionPanelCircuitBreaker",
"offset": 9,
"type": "uint8",
"description": "DC C1直流配电板断路器"
},
{
"name": "reservedCircuitBreaker1",
"offset": 10,
"type": "uint8",
"description": "备用断路器1"
},
{
"name": "reservedCircuitBreaker2",
"offset": 11,
"type": "uint8",
"description": "备用断路器2"
},
{
"name": "emergencyLithiumBatteryGroup2CircuitBreaker",
"offset": 12,
"type": "uint8",
"description": "应急锂电池组2断路器"
},
{
"name": "instrumentPowerFailureSignal",
"offset": 13,
"type": "uint8",
"description": "仪表电源故障信号"
},
{
"name": "emergencyPowerFailureSignal",
"offset": 14,
"type": "uint8",
"description": "应急电源故障信号"
},
{
"name": "instrumentBusbarVoltage",
"offset": 15,
"type": "uint16",
"description": "仪表母线电压(V)"
},
{
"name": "bowLowVoltageDistributionBoxCurrent",
"offset": 17,
"type": "uint16",
"description": "艏部低压配电箱电流(A)"
},
{
"name": "dcC1InstrumentDC48VCurrent",
"offset": 19,
"type": "uint16",
"description": "DC C1仪表DC48V电流(A)"
},
{
"name": "reservedCurrent1",
"offset": 21,
"type": "uint16",
"description": "备用电流1(A)"
},
{
"name": "emergencyLithiumBatteryGroup2Current",
"offset": 23,
"type": "uint16",
"description": "应急锂电池组2电流(A)"
},
{
"name": "lithiumBatteryGroupInstrumentCurrent",
"offset": 25,
"type": "uint16",
"description": "锂电池组仪表电流(A)"
},
{
"name": "unit4InstrumentDC48VCurrent",
"offset": 27,
"type": "uint16",
"description": "单元4仪表DC48V电流(A)"
},
{
"name": "reservedCurrent2",
"offset": 29,
"type": "uint16",
"description": "备用电流2(A)"
},
{
"name": "emergency2BusbarVoltage",
"offset": 31,
"type": "uint16",
"description": "应急2母线电压(V)"
},
{
"name": "compositeEnergyManagementSystemEmergencyDC48VCurrent",
"offset": 33,
"type": "uint16",
"description": "综合能源管理系统应急DC48V电流(A)"
}
]
}
@@ -0,0 +1,184 @@
{
"protocol_name": "高压母线状态反馈",
"message_id": "0x0005",
"length": 38,
"checksum": {
"start_offset": 0,
"end_offset": 37,
"algorithm": "sum_low_byte"
},
"fields": [
{
"name": "start1",
"offset": 0,
"type": "uint8",
"value": "0x20",
"description": "起始字节1"
},
{
"name": "start2",
"offset": 1,
"type": "uint8",
"value": "0x20",
"description": "起始字节2"
},
{
"name": "id",
"offset": 2,
"type": "uint16",
"value": "0x0005",
"description": "消息ID"
},
{
"name": "length",
"offset": 4,
"type": "uint16",
"value": 38,
"description": "消息长度"
},
{
"name": "DCB1",
"offset": 6,
"type": "uint8",
"description": "直流断路器1状态"
},
{
"name": "DCB2",
"offset": 7,
"type": "uint8",
"description": "直流断路器2状态"
},
{
"name": "ZK1",
"offset": 8,
"type": "uint8",
"description": "真空接触器1状态"
},
{
"name": "ZK2",
"offset": 9,
"type": "uint8",
"description": "真空接触器2状态"
},
{
"name": "ZK3",
"offset": 10,
"type": "uint8",
"description": "真空接触器3状态"
},
{
"name": "ZK4",
"offset": 11,
"type": "uint8",
"description": "真空接触器4状态"
},
{
"name": "ZK5",
"offset": 12,
"type": "uint8",
"description": "真空接触器5状态"
},
{
"name": "ZK6",
"offset": 13,
"type": "uint8",
"description": "真空接触器6状态"
},
{
"name": "ZK7",
"offset": 14,
"type": "uint8",
"description": "真空接触器7状态"
},
{
"name": "ZK8",
"offset": 15,
"type": "uint8",
"description": "真空接触器8状态"
},
{
"name": "ZK9",
"offset": 16,
"type": "uint8",
"description": "真空接触器9状态"
},
{
"name": "ZK10",
"offset": 17,
"type": "uint8",
"description": "真空接触器10状态"
},
{
"name": "busInsulationLow",
"offset": 18,
"type": "uint8",
"description": "母线绝缘低报警"
},
{
"name": "busAInsulationHigh",
"offset": 19,
"type": "uint8",
"description": "A母线绝缘高报警"
},
{
"name": "lostPower",
"offset": 20,
"type": "uint8",
"description": "失电信号"
},
{
"name": "lostEmerPower",
"offset": 21,
"type": "uint8",
"description": "应急失电信号"
},
{
"name": "powerBusVoltage",
"offset": 22,
"type": "uint16",
"description": "动力母线电压(V)"
},
{
"name": "dcDc5ModuleCurrent",
"offset": 24,
"type": "uint16",
"description": "DC/DC5模块电流(A)"
},
{
"name": "powerBusCurrent",
"offset": 26,
"type": "uint16",
"description": "动力母线电流(A)"
},
{
"name": "busbarAVoltage",
"offset": 28,
"type": "uint16",
"description": "A排电压(V)"
},
{
"name": "propulsionMotorControlBoxCurrent",
"offset": 30,
"type": "uint16",
"description": "推进电机控制箱电流(A)"
},
{
"name": "busbarACurrent",
"offset": 32,
"type": "uint16",
"description": "A排电流(A)"
},
{
"name": "lithiumBatteryGroupMeterCurrent",
"offset": 34,
"type": "uint16",
"description": "锂电池组表计电流(A)"
},
{
"name": "bowHighVoltageDistributionBoxCurrent",
"offset": 36,
"type": "uint16",
"description": "艏部高压配电箱电流(A)"
}
]
}
-5
View File
@@ -1,5 +0,0 @@
{
"powerCMD":0,
"modCMD":0,
"workCMD":0
}
+128
View File
@@ -0,0 +1,128 @@
# PowerManager系统架构优化设计方案
## 1. 总体架构
```mermaid
classDiagram
class PowerManagerCore {
+startup()
+shutdown()
+getSystemState()
}
class StateMachineService {
+triggerEvent()
+getCurrentState()
+addStateListener()
}
class CommunicationMediator {
+registerHandler()
+sendMessage()
+addMessageListener()
}
PowerManagerCore --> StateMachineService
PowerManagerCore --> CommunicationMediator
```
## 2. 状态机重构方案
### 2.1 状态设计模式
```cpp
class IPowerState {
public:
virtual void onEntry(PowerContext&) = 0;
virtual void handleEvent(PowerContext&, const Event&) = 0;
virtual void onExit(PowerContext&) = 0;
};
```
### 2.2 状态转换规则
```yaml
# states_config.yaml
transitions:
- from: STANDBY
event: START_CMD
to: OPERATING
- from: OPERATING
event: STOP_CMD
to: STANDBY
```
## 3. 通信中介者设计
### 3.1 核心架构
```mermaid
sequenceDiagram
participant Upper as 上位机
participant Mediator as 通信中介者
participant Lower as 下位机
Upper->>Mediator: 控制命令(JSON)
Mediator->>Lower: 设备指令(ProtocolBuffer)
Lower->>Mediator: 状态更新
Mediator->>Upper: 系统报告
```
### 3.2 关键实现
```cpp
class CommunicationMediator {
// 线程安全消息队列
moodycamel::ConcurrentQueue<Message> queue;
// 消息处理线程
void processMessages() {
while (running) {
Message msg;
if (queue.try_dequeue(msg)) {
handlers[msg.type]->handle(msg);
}
}
}
};
```
## 4. 数据库服务设计
### 4.1 日志策略模式
```cpp
class ILogStrategy {
public:
virtual bool log(const SystemLog& log) = 0;
};
class SQLiteLogStrategy : public ILogStrategy {
// SQLite实现...
};
class FileLogStrategy : public ILogStrategy {
// 文件系统实现...
};
```
## 5. 实施计划
### 5.1 阶段划分
```mermaid
gantt
title 重构实施甘特图
section 核心框架
接口定义 :2025-04-08, 3d
基础实现 :2025-04-11, 5d
section 模块重构
状态机 :2025-04-16, 5d
通信模块 :2025-04-21, 5d
section 测试验证
单元测试 :2025-04-26, 3d
集成测试 :2025-04-29, 5d
```
## 6. 性能指标
| 模块 | 当前QPS | 目标QPS | 优化手段 |
|----------------|---------|---------|-----------------------|
| 状态机处理 | 100 | 500 | 无锁状态转换 |
| 消息处理 | 200 | 1000 | 批量处理+线程池 |
| 数据库写入 | 50 | 300 | 异步写入+批量提交 |
+5
View File
@@ -0,0 +1,5 @@
ProcessConfig = pPowerManger
{
AppTick = 4
CommsTick = 4
}
+13 -10
View File
@@ -19,13 +19,15 @@ endif (${WIN32})
find_package(SQLite3 REQUIRED)
SET(SRC
PowerManagerFsm.cpp
PowerManger.cpp
PowerManger_Info.cpp
udpComm.cpp
fsm.cpp
main.cpp
SQLiteDB.cpp
driver.cpp
LowerCommManager.cpp
UpperCommManager.cpp
)
ADD_EXECUTABLE(pPowerManger ${SRC})
@@ -51,22 +53,23 @@ SET(SQLITEDB_SRC
)
SET(FSMLIB_SRC
fsm.cpp)
PowerManagerFsm.cpp)
SET(DRIVER_SRC
driver.cpp)
SET(POWERMANGE_SRC
PowerManger.cpp
PowerManger_Info.cpp
udpComm.cpp
fsm.cpp
SQLiteDB.cpp
driver.cpp)
# SET(POWERMANGE_SRC
# PowerManger.cpp
# PowerManger_Info.cpp
# udpComm.cpp
# SQLiteDB.cpp
# driver.cpp
# PowerManagerFsm.cpp)
ADD_LIBRARY(SQLiteDB STATIC ${SQLITEDB_SRC})
ADD_LIBRARY(UDPComm STATIC ${UDPCOMM_SRC})
ADD_LIBRARY(FSMLib STATIC ${FSMLIB_SRC})
ADD_LIBRARY(Driver STATIC ${DRIVER_SRC})
ADD_LIBRARY(PowerMange STATIC ${POWERMANGE_SRC})
# ADD_LIBRARY(PowerMange STATIC ${POWERMANGE_SRC})
+326
View File
@@ -0,0 +1,326 @@
#include "LowerCommManager.h"
#include "PowerManger.h"
#include "driver.h"
#include "udpComm.h"
bool _ListenCB(void * pParam)
{
LowerCommManager* pMe = (LowerCommManager* )pParam;
return pMe->listenThreadFunc();
}
LowerCommManager::LowerCommManager(PowerManger* pm, long myPort, long ccuPort, const std::string& cccHost){
m_pm = pm;
m_udpComm = new udpComm();
m_port = myPort;
m_udpComm->ccuHost = cccHost;
m_udpComm->ccuPort = ccuPort;
m_udpComm->udpScoket = new XPCUdpSocket(m_port);
std::cout << "PORT = " << m_port << std::endl;
std::cout << "CCUHOST = " << m_udpComm->ccuHost << std::endl;
std::cout << "CCUPORT = " << m_udpComm->ccuPort << std::endl;
}
LowerCommManager::~LowerCommManager(){
try {
stopListenThread();
if (m_udpComm) {
delete m_udpComm;
m_udpComm = nullptr;
}
} catch (...) {
cerr << "Error during cleanup" << endl;
}
}
bool LowerCommManager::initUdpComm(){
//设置UDP监听端口
if(m_udpComm->udpScoket)
{
try
{
m_udpComm->udpScoket->vBindSocket();
cout<<MOOS::ConsoleColours::green()<< "UDP set succeed!"<< MOOS::ConsoleColours::reset() << endl;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
cout<<MOOS::ConsoleColours::red()<< "UDP set Fild!"<< MOOS::ConsoleColours::reset() << endl;
return false;
}
}
else
{
cout<<MOOS::ConsoleColours::red()<< "UDP set Fild!"<< MOOS::ConsoleColours::reset() << endl;
return false;
}
return true;
}
void LowerCommManager::processPeriodicTasks() {
sendCcuCommand(m_pm->m_CcuColCmd);
sendDisSysCommand(m_pm->m_targetDisBreakerState);
m_udpComm->clearMsgQueue();
}
bool LowerCommManager::sendCcuCommand(const ccuColCmd cmd){
// m_pm->m_db->insertCcuSysCmd(cmd);
if(!m_udpComm->sendCcuColCmd(cmd))
cout << "send ccu command failed!" << endl;
return true;
}
bool LowerCommManager::sendDisSysCommand(const disSysBreakerList& cmd){
//根据目标状态进行断路器操作
//a. 高压母线的断路器
disHighVolBusCmd a;
a.fuelCellCircuitBreaker = getBreakerOption(cmd.bus1Breaker.fuelCellCircuitBreaker);
a.lithiumBatteryGroupInstrumentCircuitBreaker = getBreakerOption(cmd.bus1Breaker.lithiumBatteryGroupInstrumentCircuitBreaker);
a.dcDc5ModuleCircuitBreaker = getBreakerOption(cmd.bus1Breaker.dcDc5ModuleCircuitBreaker);
a.powerLithiumBatteryCircuitBreaker = getBreakerOption(cmd.bus1Breaker.powerLithiumBatteryCircuitBreaker);
a.propulsionMotorCircuitBreaker = getBreakerOption(cmd.bus1Breaker.propulsionMotorCircuitBreaker);
a.bowHighVoltageDistributionBoxCircuitBreaker = getBreakerOption(cmd.bus1Breaker.bowHighVoltageDistributionBoxCircuitBreaker);
a.tyzReservedCircuitBreaker = getBreakerOption(cmd.bus1Breaker.tyzReservedCircuitBreaker);
a.sternFTDevice45CircuitBreaker = getBreakerOption(cmd.bus1Breaker.sternFTDevice45CircuitBreaker);
a.sternRudderSwitch1CircuitBreaker = getBreakerOption(cmd.bus1Breaker.sternRudderSwitch1CircuitBreaker);
a.sternRudderSwitch2CircuitBreaker = getBreakerOption(cmd.bus1Breaker.sternRudderSwitch2CircuitBreaker);
a.reservedCircuitBreaker = getBreakerOption(cmd.bus1Breaker.reservedCircuitBreaker);
a.fbReservedCircuitBreaker = getBreakerOption(cmd.bus1Breaker.fbReservedCircuitBreaker);
m_udpComm->sendDisSysCmd(a);
//b. 高压汇流排A断路器
disHighAVolBusCmd b;
b.bowFTDevice123CircuitBreaker = getBreakerOption(cmd.bus2Breaker.bowFTDevice123CircuitBreaker);
b.actuatorCircuitBreaker = getBreakerOption(cmd.bus2Breaker.actuatorCircuitBreaker);
b.mastSteeringGearControlBoxCircuitBreaker = getBreakerOption(cmd.bus2Breaker.mastSteeringGearControlBoxCircuitBreaker);
b.xczCircuitBreaker = getBreakerOption(cmd.bus2Breaker.xczCircuitBreaker);
b.bowRudderControlBoxCircuitBreaker = getBreakerOption(cmd.bus2Breaker.bowRudderControlBoxCircuitBreaker);
b.openWaterCoverStartCylinderCircuitBreaker = getBreakerOption(cmd.bus2Breaker.openWaterCoverStartCylinderCircuitBreaker);
m_udpComm->sendDisSysCmd(b);
//c. 高压汇流排B断路器
disHighBVolBusCmd c;
c.lithiumBatteryGroupInstrumentCircuitBreaker = getBreakerOption(cmd.bus3Breaker.lithiumBatteryGroupInstrumentCircuitBreaker);
c.bowLowVoltageDistributionBoxCircuitBreaker = getBreakerOption(cmd.bus3Breaker.bowLowVoltageDistributionBoxCircuitBreaker);
c.unit4InstrumentDC48VCircuitBreaker = getBreakerOption(cmd.bus3Breaker.unit4InstrumentDC48VCircuitBreaker);
c.dcC1DCDistributionPanelCircuitBreaker = getBreakerOption(cmd.bus3Breaker.dcC1DCDistributionPanelCircuitBreaker);
c.reservedCircuitBreaker1 = getBreakerOption(cmd.bus3Breaker.reservedCircuitBreaker1);
c.reservedCircuitBreaker2 = getBreakerOption(cmd.bus3Breaker.reservedCircuitBreaker2);
c.emergencyLithiumBatteryGroup2CircuitBreaker = getBreakerOption(cmd.bus3Breaker.emergencyLithiumBatteryGroup2CircuitBreaker);
m_udpComm->sendDisSysCmd(c);
//d. 闭合低压汇流排断路器
disLowBusCmd d;
d.unit1CircuitBreaker = getBreakerOption(cmd.bus4Breaker.unit1CircuitBreaker);
d.unit2CircuitBreaker = getBreakerOption(cmd.bus4Breaker.unit2CircuitBreaker);
d.unit3CircuitBreaker = getBreakerOption(cmd.bus4Breaker.unit3CircuitBreaker);
d.unit5CircuitBreaker = getBreakerOption(cmd.bus4Breaker.unit5CircuitBreaker);
d.bowPZDeviceCircuitBreaker = getBreakerOption(cmd.bus4Breaker.bowPZDeviceCircuitBreaker);
d.reservedCircuitBreaker1 = getBreakerOption(cmd.bus4Breaker.reservedCircuitBreaker1);
d.reservedCircuitBreaker2 = getBreakerOption(cmd.bus4Breaker.reservedCircuitBreaker2);
d.emergencyLithiumBatteryGroup1CircuitBreaker = getBreakerOption(cmd.bus4Breaker.emergencyLithiumBatteryGroup1CircuitBreaker);
m_udpComm->sendDisSysCmd(d);
//将指令写入数据库
m_pm->m_db->insertDisSysCmd(a);
m_pm->m_db->insertDisSysCmd(b);
m_pm->m_db->insertDisSysCmd(c);
m_pm->m_db->insertDisSysCmd(d);
return true;
}
void LowerCommManager::clearMsgQueue(){
m_udpComm->clearMsgQueue();
}
bool LowerCommManager::updatePoweSystemStates(){
// std::cout << "updatePoweSystemStates" << std::endl;
if(!m_udpComm->m_qReceiveCcuStateBuffer.empty())
{
double time;
m_udpComm->popMsgFormQueue(time,m_CcuCurrentState);
m_pm->m_pmCurrentState.ccustate = m_CcuCurrentState.data;
// m_db->insertCcuData(m_CcuCurrentState.data); //写入数据库
msg_CcuStateFbMsg_Update_time = time;
}
if(!m_udpComm->m_qReceiveDisHighVolBusBuffer.empty())
{
double time;
m_udpComm->popMsgFormQueue(time,m_disHighVolBusCurrentState);
m_pm->m_pmCurrentState.bus1State = m_disHighVolBusCurrentState.data;
// m_db->insertDisHighVolBusData(m_disHighVolBusCurrentState.data); //写入数据库
msg_disHighVolBusFbMsg_Update_time = time;
disBus1Breaker* pdis1=new disBus1Breaker;
(m_pm->m_pmCurrentState.bus1State.fuelCellCircuitBreaker == BREAK_ON) ? pdis1 -> fuelCellCircuitBreaker = 1 : pdis1 -> fuelCellCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.powerLithiumBatteryCircuitBreaker == BREAK_ON) ? pdis1 -> powerLithiumBatteryCircuitBreaker = 1 : pdis1 -> powerLithiumBatteryCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.propulsionMotorCircuitBreaker == BREAK_ON) ? pdis1 -> propulsionMotorCircuitBreaker = 1 : pdis1 -> propulsionMotorCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.lithiumBatteryGroupInstrumentCircuitBreaker == BREAK_ON) ? pdis1 -> lithiumBatteryGroupInstrumentCircuitBreaker = 1 : pdis1 -> lithiumBatteryGroupInstrumentCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.dcDc5ModuleCircuitBreaker == BREAK_ON) ? pdis1 -> dcDc5ModuleCircuitBreaker = 1 : pdis1 -> dcDc5ModuleCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.bowHighVoltageDistributionBoxCircuitBreaker == BREAK_ON) ? pdis1 -> bowHighVoltageDistributionBoxCircuitBreaker = 1 : pdis1 -> bowHighVoltageDistributionBoxCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.sternFTDevice45CircuitBreaker == BREAK_ON) ? pdis1 -> sternFTDevice45CircuitBreaker = 1 : pdis1 -> sternFTDevice45CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.sternRudderSwitch1CircuitBreaker == BREAK_ON) ? pdis1 -> sternRudderSwitch1CircuitBreaker = 1 : pdis1 -> sternRudderSwitch1CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.sternRudderSwitch2CircuitBreaker == BREAK_ON) ? pdis1 -> sternRudderSwitch2CircuitBreaker = 1 : pdis1 -> sternRudderSwitch2CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.fbReservedCircuitBreaker == BREAK_ON) ? pdis1 -> fbReservedCircuitBreaker = 1 : pdis1 -> fbReservedCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.tyzReservedCircuitBreaker == BREAK_ON) ? pdis1 -> tyzReservedCircuitBreaker = 1 : pdis1 -> tyzReservedCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus1State.reservedCircuitBreaker == BREAK_ON) ? pdis1 -> reservedCircuitBreaker = 1 : pdis1 -> reservedCircuitBreaker = 0;
pdis1 -> paceholder = 0;
m_pm->m_currentDisBreakerState.bus1Breaker = *pdis1;
delete pdis1;
}
if(!m_udpComm->m_qReceiveDisHighAVolBusBuffer.empty())
{
double time;
m_udpComm->popMsgFormQueue(time,m_disHighAVolBusCurrentState);
m_pm->m_pmCurrentState.bus2State = m_disHighAVolBusCurrentState.data;
// m_db->insertDisHighAVolBusData(m_disHighAVolBusCurrentState.data); //写入数据库
msg_disHighAVolBusFbMsg_Update_time = time;
disBus2Breaker* pdis2=new disBus2Breaker;
(m_pm->m_pmCurrentState.bus2State.bowFTDevice123CircuitBreaker == BREAK_ON)? pdis2 -> bowFTDevice123CircuitBreaker = 1 : pdis2 -> bowFTDevice123CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus2State.actuatorCircuitBreaker == BREAK_ON)? pdis2 -> actuatorCircuitBreaker = 1 : pdis2 -> actuatorCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus2State.mastSteeringGearControlBoxCircuitBreaker == BREAK_ON)? pdis2 -> mastSteeringGearControlBoxCircuitBreaker = 1 : pdis2 -> mastSteeringGearControlBoxCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus2State.xczCircuitBreaker == BREAK_ON)? pdis2 -> xczCircuitBreaker = 1 : pdis2 -> xczCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus2State.bowRudderControlBoxCircuitBreaker == BREAK_ON)? pdis2 -> bowRudderControlBoxCircuitBreaker = 1 : pdis2 -> bowRudderControlBoxCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus2State.openWaterCoverStartCylinderCircuitBreaker == BREAK_ON)? pdis2 -> openWaterCoverStartCylinderCircuitBreaker = 1 : pdis2 -> openWaterCoverStartCylinderCircuitBreaker = 0;
pdis2 -> paceholder = 0;
m_pm->m_currentDisBreakerState.bus2Breaker = *pdis2;
delete pdis2;
}
if(!m_udpComm->m_qReceiveDisHighBVolBusBuffer.empty())
{
double time;
m_udpComm->popMsgFormQueue(time,m_disHighBVolBusCurrentState);
m_pm->m_pmCurrentState.bus3State = m_disHighBVolBusCurrentState.data;
// m_db->insertDisHighBVolBusData(m_disHighBVolBusCurrentState.data); //写入数据库
msg_disHighBVolBusFbMsg_Update_time = time;
disBus3Breaker* pdis3=new disBus3Breaker;
(m_pm->m_pmCurrentState.bus3State.lithiumBatteryGroupInstrumentCircuitBreaker == BREAK_ON)? pdis3 -> lithiumBatteryGroupInstrumentCircuitBreaker = 1 : pdis3 -> lithiumBatteryGroupInstrumentCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus3State.bowLowVoltageDistributionBoxCircuitBreaker == BREAK_ON)? pdis3 -> bowLowVoltageDistributionBoxCircuitBreaker = 1 : pdis3 -> bowLowVoltageDistributionBoxCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus3State.unit4InstrumentDC48VCircuitBreaker == BREAK_ON)? pdis3 -> unit4InstrumentDC48VCircuitBreaker = 1 : pdis3 -> unit4InstrumentDC48VCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus3State.dcC1DCDistributionPanelCircuitBreaker == BREAK_ON)? pdis3 -> dcC1DCDistributionPanelCircuitBreaker = 1 : pdis3 -> dcC1DCDistributionPanelCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus3State.reservedCircuitBreaker1 == BREAK_ON)? pdis3 -> reservedCircuitBreaker1 = 1 : pdis3 -> reservedCircuitBreaker1 = 0;
(m_pm->m_pmCurrentState.bus3State.reservedCircuitBreaker2 == BREAK_ON)? pdis3 -> reservedCircuitBreaker2 = 1 : pdis3 -> reservedCircuitBreaker2 = 0;
(m_pm->m_pmCurrentState.bus3State.emergencyLithiumBatteryGroup2CircuitBreaker == BREAK_ON)? pdis3 -> emergencyLithiumBatteryGroup2CircuitBreaker = 1 : pdis3 -> emergencyLithiumBatteryGroup2CircuitBreaker = 0;
pdis3->paceholder = 0;
m_pm->m_currentDisBreakerState.bus3Breaker = *pdis3;
delete pdis3;
}
if(!m_udpComm->m_qReceiveDisLowBusBuffer.empty())
{
double time;
m_udpComm->popMsgFormQueue(time,m_disLowBusState);
m_pm->m_pmCurrentState.bus4State = m_disLowBusState.data;
// m_db->insertDisLowBusData(m_disLowBusState.data); //写入数据库
msg_disLowBusFbMsg_Update_time = time;
disBus4Breaker* pdis4=new disBus4Breaker;
(m_pm->m_pmCurrentState.bus4State.unit1CircuitBreaker == BREAK_ON)? pdis4 -> unit1CircuitBreaker = 1 : pdis4 -> unit1CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus4State.unit2CircuitBreaker == BREAK_ON)? pdis4 -> unit2CircuitBreaker = 1 : pdis4 -> unit2CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus4State.unit3CircuitBreaker == BREAK_ON)? pdis4 -> unit3CircuitBreaker = 1 : pdis4 -> unit3CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus4State.unit5CircuitBreaker == BREAK_ON)? pdis4 -> unit5CircuitBreaker = 1 : pdis4 -> unit5CircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus4State.bowPZDeviceCircuitBreaker == BREAK_ON)? pdis4 -> bowPZDeviceCircuitBreaker = 1 : pdis4 -> bowPZDeviceCircuitBreaker = 0;
(m_pm->m_pmCurrentState.bus4State.reservedCircuitBreaker1 == BREAK_ON)? pdis4 -> reservedCircuitBreaker1 = 1 : pdis4 -> reservedCircuitBreaker1 = 0;
(m_pm->m_pmCurrentState.bus4State.reservedCircuitBreaker2 == BREAK_ON)? pdis4 -> reservedCircuitBreaker2 = 1 : pdis4 -> reservedCircuitBreaker2 = 0;
(m_pm->m_pmCurrentState.bus4State.emergencyLithiumBatteryGroup1CircuitBreaker == BREAK_ON)? pdis4 -> emergencyLithiumBatteryGroup1CircuitBreaker = 1 : pdis4 -> emergencyLithiumBatteryGroup1CircuitBreaker = 0;
pdis4 -> paceholder = 0;
m_pm->m_currentDisBreakerState.bus4Breaker = *pdis4;
delete pdis4;
}
return true;
}
bool LowerCommManager::listenThreadFunc()
{
cout<<MOOS::ConsoleColours::green()<< "LowerCommManager::listenThreadFunc" << MOOS::ConsoleColours::reset() << endl;
unsigned char Buff[1024];
while(!m_ListenThread.IsQuitRequested())
{
try
{
int nRead = m_udpComm->udpScoket->iRecieveMessage(Buff, sizeof(Buff));
//cout<<MOOS::ConsoleColours::yellow()<< "RECEVE DATA COUNT:" << nRead << MOOS::ConsoleColours::reset() << endl;
if(nRead > 0)
{
try {
if(!m_udpComm->pushMsgToQueue(Buff))
{
for(int i=0; i<m_udpComm->sError.size(); i++)
{
cout<<MOOS::ConsoleColours::red()<< m_udpComm->sError[i] << MOOS::ConsoleColours::reset() << endl;
}
}
//更新当前状态
if(!updatePoweSystemStates())
{
std::cerr << "Error updating power system states" << std::endl;
}
}
catch(const std::exception& e) {
cerr << "Error processing message: " << e.what() << endl;
}
}
// 打印通信错误信息
if(!m_udpComm->sError.empty())
{
for(int i=0; i<m_udpComm->sError.size(); i++)
{
cout<<MOOS::ConsoleColours::red() << m_udpComm->sError[i] << MOOS::ConsoleColours::reset() << endl;
}
m_udpComm->sError.clear();
}
}
catch(const XPCException& e)
{
cerr << "XPCException in listenThreadFunc" << endl;
// 短暂休眠后继续尝试
MOOSPause(100);
}
catch(const std::exception& e)
{
cerr << "Exception in listenThreadFunc: " << e.what() << endl;
MOOSPause(100);
}
catch(...)
{
cerr << "Unknown exception in listenThreadFunc" << endl;
MOOSPause(100);
}
}
return true;
}
void LowerCommManager::startListenThread()
{
if(!m_ListenThread.IsThreadRunning())
{
m_ListenThread.Initialise(_ListenCB,this);
m_ListenThread.Start();
std::cout << "LowerCommManager::startListenThread" << std::endl;
}
}
void LowerCommManager::stopListenThread()
{
if(m_ListenThread.IsThreadRunning())
{
m_ListenThread.Stop();
std::cout << "LowerCommManager::stopListenThread" << std::endl;
}
}
char LowerCommManager::getBreakerOption(char cmd)
{
if(cmd ==1)
return 0x55;
else if(cmd ==0)
return 0xAA;
else
return 0x00;
}
//TODO:根据心跳检查连接状态
bool LowerCommManager::checkConnection()
{
m_pm->connectState =5;
}
+82
View File
@@ -0,0 +1,82 @@
#ifndef LOWER_COMM_MANAGER_H
#define LOWER_COMM_MANAGER_H
#define UNIX
#include "MOOS/libMOOS/Comms/XPCUdpSocket.h"
#include "MOOS/libMOOS/Utils/MOOSThread.h"
#include "MOOS/libMOOS/Utils/MOOSLock.h"
#include "ccuUdpMsg.h"
#include "pmSysvariable.h"
#include <queue>
#include <map>
#include <functional>
#include <atomic>
#include <vector>
#include "udpComm.h"
class PowerManger; // 前向声明
class LowerCommManager {
public:
LowerCommManager(PowerManger* pm, long myPort, long ccuPort, const std::string& cccHost);
~LowerCommManager();
//processPeriodicTasks
void processPeriodicTasks();
//clearMsgQueue
void clearMsgQueue();
public:
// 初始化UDP通信
bool initUdpComm();
// TODO:实现连接检查功能
bool checkConnection();
// 更新设备状态
bool updatePoweSystemStates();
// 发送CCU控制命令
bool sendCcuCommand(const ccuColCmd cmd);
bool sendDisSysCommand(const disSysBreakerList& cmd);
// 获取通信状态
int getConnectionState() const { return m_connectState; }
// 启动/停止监听线程
void startListenThread();
void stopListenThread();
//转换函数
char getBreakerOption(char cmd);
// 配电和CCU的反馈状态
msg_CcuStateFbMsg m_CcuCurrentState;
msg_disHighVolBusFbMsg m_disHighVolBusCurrentState;
msg_disHighAVolBusFbMsg m_disHighAVolBusCurrentState;
msg_disHighBVolBusFbMsg m_disHighBVolBusCurrentState;
msg_disLowBusFbMsg m_disLowBusState;
// 配电和CCU的反馈状态时间戳
double msg_CcuStateFbMsg_Update_time;
// double msg_CcuSetParmFbMsg_Update_time;
double msg_disHighVolBusFbMsg_Update_time;
double msg_disHighAVolBusFbMsg_Update_time;
double msg_disHighBVolBusFbMsg_Update_time;
double msg_disLowBusFbMsg_Update_time;
public:
long m_port;
long m_ccuPort;
std::string m_ccuHost;
std::string m_host;
PowerManger* m_pm;
udpComm* m_udpComm;
CMOOSThread m_ListenThread;
int m_connectState;
// UDP监听线程函数
bool listenThreadFunc();
};
#endif // LOWER_COMM_MANAGER_H
+65
View File
@@ -0,0 +1,65 @@
#include "PowerManagerFsm.hpp"
#include "PowerManger.h"
#include <string>
#include "MOOS/libMOOS/Utils/MOOSUtilityFunctions.h"
// 定义静态成员
PowerManger* PowerManagerFsm::pm = nullptr;
// InitState实现
void InitState::entry() {
PowerManagerFsm::entry();
std::cout << "初始化配电指令" << std::endl;
pm->initControlCmd();
// PowerManagerFsm::pm->printState("InitState", "进入初始化状态");
transit<StandbyState>();
}
// StandbyState实现
void StandbyState::entry() {
PowerManagerFsm::entry();
pm->m_driver.getSTANDBYTable(&pm->m_subDisSysCmd);
pm->m_CcuColCmd.fcuCmd = FcuCommand::Stop;
pm->m_CcuColCmd.powerBatCmd = BatteryCommand::Stop;
}
void StandbyState::react(StandbyEvent const &) {
transit<StandbyState>();
}
void StandbyState::handlePowerCommands() {
switch (PowerManagerFsm::pm->m_msCmd.powerCMD) {
case 0: PowerManagerFsm::pm->m_pmEvent = "收到上位机控制指令:关机"; break;
case 1: handlePowerOn(); break;
}
}
void StandbyState::handlePowerOn() {
PowerManagerFsm::pm->m_pmEvent = "动力电上电";
PowerManagerFsm::pm->m_CcuColCmd.powerBatCmd = 0x10;
if(PowerManagerFsm::pm->m_pmCurrentState.ccustate.fcuState == 0x04)
PowerManagerFsm::pm->m_CcuColCmd.fcuCmd = 0x02;
}
// FaultState实现
void FaultState::entry() {
PowerManagerFsm::entry();
PowerManagerFsm::pm->printState("FaultState", "进入故障状态");
}
void FaultState::react(FaultEvent const & e) {
switch (e.faultCode) {
case 1:
PowerManagerFsm::pm->m_pmEvent = "复合管控连接超时";
PowerManagerFsm::pm->printState("FaultState", "通信故障");
break;
case 2:
PowerManagerFsm::pm->m_pmEvent = "能源系统故障";
PowerManagerFsm::pm->printState("FaultState", "能源故障");
break;
}
}
// 设置初始状态
FSM_INITIAL_STATE(PowerManagerFsm, InitState);
+106
View File
@@ -0,0 +1,106 @@
#ifndef POWER_MANAGER_FSM_HPP
#define POWER_MANAGER_FSM_HPP
#include "tinyfsm.hpp"
#include <iostream>
#include <chrono> // 新增头文件
#include <iomanip> // 新增头文件
#include <ctime> // 新增头文件,用于localtime函数
#include <cxxabi.h> // 新增头文件,用于demangle功能
#include "pmSysvariable.h"
#include "driver.h"
// 前向声明
class PowerManger;
enum class PowerSystemState {
INIT,
STANDBY,
FAULT,
};
//==================状态切换事件=============================
struct InitEvent : tinyfsm::Event { };
struct StandbyEvent : tinyfsm::Event { };
struct FaultEvent : tinyfsm::Event {
unsigned int faultCode;
FaultEvent(unsigned int code) : faultCode(code) {}
};
//==================状态流程事件============================
struct MasterCommandEvent : tinyfsm::Event {};
struct TaskStart : tinyfsm::Event { };
struct DeviceCmd : tinyfsm::Event { };
class PowerManagerFsm : public tinyfsm::Fsm<PowerManagerFsm> {
protected:
static PowerManger* pm;
static DriverTable m_currentDriver;
public:
static void init(PowerManger* powerManager) {
pm = powerManager;
}
// 响应事件默认实现
virtual void react(tinyfsm::Event const &) { std::cout << "[ Reseve Envt ] " << "No Idear" << std::endl; }
virtual void react(InitEvent const &) {};
virtual void react(StandbyEvent const &) {};
virtual void react(FaultEvent const &) {};
virtual void react(MasterCommandEvent const &) {};
virtual void react(TaskStart const &) {};
virtual void react(DeviceCmd const &) {};
// 状态机生命周期方法
virtual void entry() {
auto now = std::chrono::system_clock::now();
auto now_time = std::chrono::system_clock::to_time_t(now);
int status = 0;
char* demangled = abi::__cxa_demangle(typeid(*this).name(), nullptr, nullptr, &status);
std::string typeName = (status == 0) ? demangled : typeid(*this).name();
if (demangled) free(demangled);
std::cout << "\033[1;38;5;51m╭──────────────────────────────────────────────────────\n"
<< "│ \033[1;38;5;226m⮞⮞⮞ \033[0;38;5;51m[" << std::put_time(std::localtime(&now_time), "%Y-%m-%d %H:%M:%S")
<< "] \033[1;38;5;255m进入状态: \033[1;38;5;213m" << typeName << "\n"
<< "│ \033[0m" << std::endl;
}
virtual void exit() {
auto now = std::chrono::system_clock::now();
auto now_time = std::chrono::system_clock::to_time_t(now);
int status = 0;
char* demangled = abi::__cxa_demangle(typeid(*this).name(), nullptr, nullptr, &status);
std::string typeName = (status == 0) ? demangled : typeid(*this).name();
if (demangled) free(demangled);
std::cout << "\033[1;38;5;203m│ \n"
<< "│ \033[1;38;5;208m⮜⮜⮜ \033[0;38;5;203m[" << std::put_time(std::localtime(&now_time), "%Y-%m-%d %H:%M:%S")
<< "] \033[1;38;5;255m离开状态: \033[1;38;5;213m" << typeName << "\n"
<< "╰──────────────────────────────────────────────────────\033[0m" << std::endl;
}
};
class InitState : public PowerManagerFsm {
public:
void entry() override;
};
class StandbyState : public PowerManagerFsm {
public:
void entry() override;
void react(StandbyEvent const &) override;
// void react(MasterCommandEvent const &) override;
// void react(DeviceCmd const &) override;
private:
void handlePowerCommands();
void handlePowerOn();
};
class FaultState : public PowerManagerFsm {
public:
void entry() override;
void react(FaultEvent const &);
};
#endif // POWER_MANAGER_FSM_HPP
File diff suppressed because it is too large Load Diff
+56 -108
View File
@@ -23,18 +23,19 @@
#include <queue>
#include "MOOS/libMOOS/Thirdparty/AppCasting/AppCastingMOOSApp.h"
#include "MOOS/libMOOS/Utils/MOOSThread.h"
#include "MOOS/libMOOS/Comms/XPCUdpSocket.h"
// #include "MOOS/libMOOS/Comms/XPCUdpSocket.h"
#include "MOOS/libMOOS/Utils/MOOSUtilityFunctions.h"
#include "MOOS/libMOOS/Utils/ConsoleColours.h"
#include "ccuUdpMsg.h"
#include "json/json.h"
#include "udpComm.h"
#include "pmSysvariable.h"
#include "fsm.h"
#include "PowerManagerFsm.hpp"
#include "SQLiteDB.h"
#include "driver.h"
#include "LowerCommManager.h"
#include "UpperCommManager.h"
#define VAR_TO_STR(var) #var
#define IPORT 5000
#define CCUPORT 5001
@@ -42,29 +43,12 @@
#define MAX_UDP_PKT_SIZE 65535
#define SKEW_TOLERANCE 5
//能源管理反馈的消息
#define PM_TO_DB_STATE "uPower_pmState_st"
#define PM_TO_DB_CURRENTSTATE "uPower_currentState_st"
#define PM_TO_DB_DISSTATE "uPower_disSysState_st"
#define PM_TO_DB_LIBATSTATE "uPower_liBatState_st"
#define PM_TO_DB_FCSSTATE "uPower_fcsState_st"
#define PM_TO_DB_SUBDISCMD "uPower_secdistState_cmd"
#define PM_TO_DB_EQUIPMENTSTATE "uPower_equState_fb"
//控制消息
//TODO:实现上位机交互控制功能
#define DB_TO_PM_MSCMD "uPower_sysState_cmd"
#define DB_TO_PM_EQUIPMENTCMD "uPower_equState_cmd"
#define DB_TO_PM_SUBDISSTATE "uDevice_secdistState_st"
class PowerManger : public AppCastingMOOSApp
{
public:
PowerManger();
~PowerManger();
public:
bool ListenLoopUDP();
bool FsmLoop();
protected: // Standard MOOSApp functions to overload
@@ -75,40 +59,35 @@ class PowerManger : public AppCastingMOOSApp
bool buildReport();
void registerVariables();
protected:
public:
void updateTime(){m_current_time = MOOSTime();}
bool IsSkewed(double dfTimeNow, double * pdfSkew = NULL);
string getTypeName(const string typeName);
bool inline saveStringToJsonFile(const string &strJson, const string &strFileName);
//数据库
SQLiteDB* m_db;
bool writeDataToDB();
//通信状态变量及操作函数
XPCUdpSocket* setUdpScoket(long lPort);
bool updatePmState();
bool updateConnectState();
/************************************************************/
/* |<------------FEEDBACK--------<-- */
/* | | */
/* IN-->+-------[STATE CONTROL]---------+--->OUT */
/* */
/************************************************************/
public:
//===========================IN============================================
DriverTable m_MSDriverCmd;
MSControlCmd m_msCmd;
//===========================OUT===========================================
DriverTable m_subDisSysCmd;
ccuColCmd m_CcuColCmd;
disSysBreakerList m_targetDisBreakerState;
//===========================FEEDBACK======================================
pmState m_pmCurrentState; //能源系统的当前状态
disSysBreakerList m_currentDisBreakerState; //配电断路器状态
DriverTable m_currentDriverState;
DriverTable m_subDisSysState;
int connectState;
double msg_CcuStateFbMsg_Update_time;
// double msg_CcuSetParmFbMsg_Update_time;
double msg_disHighVolBusFbMsg_Update_time;
double msg_disHighAVolBusFbMsg_Update_time;
double msg_disHighBVolBusFbMsg_Update_time;
double msg_disLowBusFbMsg_Update_time;
protected:
//===========================交互状态变量==================================
Driver m_driver; //设备驱动
pmState m_pmCurrentState; //能源系统的当前状态
disSysBreakerList m_currentDisBreakerState; //配电断路器状态
disSysBreakerList m_targetDisBreakerState;
DriverTable m_currentDriverState;
DriverTable m_MSDriverCmd;
DriverTable m_subDisSysCmd;
DriverTable m_subDisSysState;
//===========================================================================
//系统运行状态
int m_depth;
int m_missionCode;
//===========================STATE=========================================
unsigned char workCondition;
unsigned char currentMod;
unsigned char powerState;
@@ -116,73 +95,42 @@ class PowerManger : public AppCastingMOOSApp
vector<unsigned int> faultCode;
unsigned int soc;
unsigned int sustainableTime;
// 配电和CCU的反馈状态
msg_CcuStateFbMsg m_CcuCurrentState;
msg_disHighVolBusFbMsg m_disHighVolBusCurrentState;
msg_disHighAVolBusFbMsg m_disHighAVolBusCurrentState;
msg_disHighBVolBusFbMsg m_disHighBVolBusCurrentState;
msg_disLowBusFbMsg m_disLowBusState;
//下位机操作函数
ccuColCmd m_CcuColCmd;
void disSysOption();
void ccuOption();
char getBreakerOption(char cmd);
void initControlCmd();
//上位机操作函数
MSControlCmd m_msCmd;
bool buildPowerSysReport();
bool getMsCmdFromJson(MSControlCmd &cmd,const string &strJson);
private:
udpComm m_udpComm;
XPCUdpSocket *m_pCcuUdpComm;
CMOOSThread m_ListenThread;
CMOOSThread m_FsmThread;
unsigned int m_faultNum;
Driver m_driver; //设备表处理
std::string m_pmEvent; // 状态机事件描述
//===========================CONFIG========================================
long m_lPort;
unsigned int m_nReceiveBufferSizeKB;
unsigned int m_nSendBufferSizeKB;
double m_current_time;
unsigned int m_faultNum;
double m_current_time;
unsigned int m_ticker;
int connectState;
public:
//===========================状态机实现==============================================
void printState(FSM_State st, string desp);
//=============================过程函数======================================
void initControlCmd();
private:
// CMOOSThread m_FsmThread;
LowerCommManager* m_lowerCommManager;
UpperCommManager* m_upperCommManager;
// // 状态机实例
// PowerManagerFsm m_fsm;
public:
//===========================状态机接口=======================================
void printState(const std::string& stateName, const std::string& desp);
bool FSM_RUN_IS_OK = true;
StateData m_stateData;
FSM_State m_pmState; //当前状态
string m_pmEvent;
FSM m_fsm;
//初始化状态
FSM_State m_Inite_s1;
static int handleInite_s1(FSM_State &fsm, void* pData);
//待机状态
FSM_State m_Standby_s2;
static int handleStandby_s2(FSM_State &fsm, void* pData);
//岸基备航
// FSM_State m_ShoreReady_s3;
//水中备航
//自动工况
//故障处理
FSM_State m_Fault_s99;
static int handleFault_s99(FSM_State &fsm, void* pData);
//======================================================================================
int checkError();
bool shutdown();
bool powerOn();
bool powerOff();
//==================================================================================
int checkError();
bool shutdown();
bool powerOn();
bool powerOff();
bool sendNotification(const std::string& key, const std::string& value) {
return Notify(key, value);
}
};
#endif
+586
View File
@@ -0,0 +1,586 @@
#include "UpperCommManager.h"
#include "PowerManger.h"
#include "driver.h"
UpperCommManager::UpperCommManager(PowerManger* pm) : m_pm(pm) {}
bool UpperCommManager::processUpperMsg(const string& key, const string& strJson) {
if(key == DB_TO_PM_MSCMD) {
m_pm->m_msCmd = getMasterControlCmd(key, strJson);
return true;
}
else if(key == DB_TO_PM_EQUIPMENTCMD) {
m_pm->m_MSDriverCmd = getEquipmentCmd(key, strJson);
return true;
}
else if(key == DB_TO_PM_SUBDISSTATE) {
m_pm->m_subDisSysState = getSubDisState(key, strJson);
return true;
}
else if(key == DB_TO_PM_DEEPTH) {
m_pm->m_depth = getDeepth(key, strJson);
return true;
}
std::cout << "Unknown key: " << key << std::endl;
return false;
}
const MSControlCmd UpperCommManager::getMasterControlCmd(const std::string& key, const std::string& msg)
{
MSControlCmd cmd;
if (parseControlCmdFromJson(cmd, msg)) {
return cmd;
}
else
{
cout << MOOS::ConsoleColours::Red << "parse control cmd error:" << msg
<< MOOS::ConsoleColours::reset << endl;
}
}
const DriverTable UpperCommManager::getEquipmentCmd(const std::string& key, const std::string& msg)
{
DriverTable cmd;
if (m_pm->m_driver.loadDriverTableFromJson(msg, cmd) < 0) {
cout << MOOS::ConsoleColours::Red << "get equipment cmd error:" << msg
<< MOOS::ConsoleColours::reset << endl;
}
return cmd;
}
const DriverTable UpperCommManager::getSubDisState(const std::string& key, const std::string& msg)
{
DriverTable state;
if (m_pm->m_driver.loadDriverTableFromJson(msg, state) < 0) {
cout << MOOS::ConsoleColours::Red << "get sub dis state error:" << msg
<< MOOS::ConsoleColours::reset << endl;
}
}
const int UpperCommManager::getDeepth(const std::string& key, const std::string& msg){
int deepth;
Json::Value root;
Json::CharReaderBuilder builder;
JSONCPP_STRING errs;
// 解析JSON字符串
std::istringstream stream(msg);
if (!Json::parseFromStream(builder, stream, &root, &errs)) {
std::cerr << "JSON解析失败: " << errs << std::endl;
return -1;
}
// 检查并读取深度数组
if (root.isMember("value_array_uint16") && root["value_array_uint16"].isArray()) {
const Json::Value& array = root["value_array_uint16"];
if (array.size() >= 3) {
// 计算三个深度值的平均值
deepth = (array[0].asInt() + array[1].asInt() + array[2].asInt()) / 3;
} else {
std::cerr << "深度数组元素不足3个" << std::endl;
return -1;
}
} else {
std::cerr << "JSON格式错误" << std::endl;
return -1;
}
return deepth;
}
const std::string UpperCommManager::getCurrentEquState(DriverTable& driver)
{
return m_pm->m_driver.saveDriverTableToJson(&driver);
}
const std::string UpperCommManager::getSubDisCmd(DriverTable& driver)
{
return m_pm->m_driver.saveDriverTableToJson(&driver);
}
void UpperCommManager::processPeriodicTasks() {
buildSystemReport();
std::string strJson;
strJson = getCurrentEquState(m_pm->m_currentDriverState);
m_pm->sendNotification(PM_TO_DB_EQUIPMENTSTATE, strJson);
strJson = getSubDisCmd(m_pm->m_subDisSysCmd);
m_pm->sendNotification(PM_TO_DB_SUBDISCMD, strJson);
}
bool UpperCommManager::parseControlCmdFromJson(MSControlCmd &cmd, const std::string &strJson)
{
Json::CharReaderBuilder builder;
Json::Value root;
std::string errs;
// 解析JSON字符串
std::istringstream s(strJson);
if (!Json::parseFromStream(builder, s, &root, &errs))
{
std::cerr << "JSON解析失败: " << errs << std::endl;
return false; // 解析失败
}
if (root.isMember("powerCMD") && root["powerCMD"].isUInt())
{
cmd.powerCMD = root["powerCMD"].asUInt();
}
else
{
std::cerr << "缺少或类型不匹配: powerCMD" << std::endl;
return false; // 缺少或类型不匹配
}
if (root.isMember("modCMD") && root["modCMD"].isUInt())
{
cmd.modCMD = root["modCMD"].asUInt();
}
else
{
std::cerr << "缺少或类型不匹配: modCMD" << std::endl;
return false; // 缺少或类型不匹配
}
if (root.isMember("workCMD") && root["workCMD"].isUInt())
{
cmd.workCMD = root["workCMD"].asUInt();
}
else
{
std::cerr << "缺少或类型不匹配: workCMD" << std::endl;
return false; // 缺少或类型不匹配
}
return true; // 成功
}
void UpperCommManager::buildSystemReport() {
string suffix;
Json::Value pms;
Json::Value fcs;
Json::Value lbs;
Json::Value dis;
// 构建能源系统报告
//1.构建系统状态报告
suffix = getTypeName(typeid(m_pm->workCondition).name());
pms[VAR_TO_STR(workCondition)+suffix] = m_pm->workCondition;
suffix = getTypeName(typeid(m_pm->currentMod).name());
pms[VAR_TO_STR(currentMod)+suffix] = m_pm->currentMod;
suffix = getTypeName(typeid(m_pm->powerState).name());
pms[VAR_TO_STR(powerState)+suffix] = m_pm->powerState;
suffix = getTypeName(typeid(m_pm->falutLevel).name());
pms[VAR_TO_STR(falutLevel)+suffix] = m_pm->falutLevel;
Json::Value faultCode_array;
for(auto val : m_pm->faultCode)
{
faultCode_array.append(val);
}
pms[VAR_TO_STR(faultCode_array)] = faultCode_array;
suffix = getTypeName(typeid(m_pm->soc).name());
pms[VAR_TO_STR(soc)+suffix] = m_pm->soc;
suffix = getTypeName(typeid(m_pm->sustainableTime).name());
pms[VAR_TO_STR(sustainableTime)+suffix] = m_pm->sustainableTime;
//2.构建燃料电池状态报告
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuState).name());
fcs[VAR_TO_STR(fcuState)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuFaultLeve).name());
fcs[VAR_TO_STR(fcuFaultLeve)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuFaultLeve;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerLimit).name());
fcs[VAR_TO_STR(powLimit)+suffix] = m_pm->m_pmCurrentState.ccustate.powerLimit;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.totalPowerGeneration).name());
fcs[VAR_TO_STR(powerGen)+suffix] = m_pm->m_pmCurrentState.ccustate.totalPowerGeneration;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.ch4o).name());
fcs[VAR_TO_STR(ch4o)+suffix] = m_pm->m_pmCurrentState.ccustate.ch4o;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.o2).name());
fcs[VAR_TO_STR(o2)+suffix] = m_pm->m_pmCurrentState.ccustate.o2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuVoltage).name());
fcs[VAR_TO_STR(fcuVol)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuCurrent).name());
fcs[VAR_TO_STR(fcuCur)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.o2Level).name());
fcs[VAR_TO_STR(o2Level)+suffix] = m_pm->m_pmCurrentState.ccustate.o2Level;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.exchangerTemperature).name());
fcs[VAR_TO_STR(excTemp)+suffix] = m_pm->m_pmCurrentState.ccustate.exchangerTemperature;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuOutPower).name());
fcs[VAR_TO_STR(fcuOutPower)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuOutPower;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuOutIns).name());
fcs[VAR_TO_STR(fcuOutIns)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuOutIns;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuVoltageIns).name());
fcs[VAR_TO_STR(fcuVolIns)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuVoltageIns;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuVoltagePower).name());
fcs[VAR_TO_STR(fcuVolPower)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuVoltagePower;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuCurrentIns).name());
fcs[VAR_TO_STR(fcuCurIns)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuCurrentIns;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuCurrentPower).name());
fcs[VAR_TO_STR(fcuCurPower)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuCurrentPower;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.h2Concentration1).name());
fcs[VAR_TO_STR(h2Concent1)+suffix] = m_pm->m_pmCurrentState.ccustate.h2Concentration1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.h2Concentration2).name());
fcs[VAR_TO_STR(h2Concent2)+suffix] = m_pm->m_pmCurrentState.ccustate.h2Concentration2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.o2Concentration1).name());
fcs[VAR_TO_STR(o2Concent1)+suffix] = m_pm->m_pmCurrentState.ccustate.o2Concentration1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.o2Concentration2).name());
fcs[VAR_TO_STR(o2Concent2)+suffix] = m_pm->m_pmCurrentState.ccustate.o2Concentration2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.ch4oConcentration1).name());
fcs[VAR_TO_STR(ch4oConcent1)+suffix] = m_pm->m_pmCurrentState.ccustate.ch4oConcentration1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.ch4oConcentration2).name());
fcs[VAR_TO_STR(ch4oConcent2)+suffix] = m_pm->m_pmCurrentState.ccustate.ch4oConcentration2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.backpressure1).name());
fcs[VAR_TO_STR(backpressure1)+suffix] = m_pm->m_pmCurrentState.ccustate.backpressure1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.backpressure2).name());
fcs[VAR_TO_STR(backpressure2)+suffix] = m_pm->m_pmCurrentState.ccustate.backpressure2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.flameDetection).name());
fcs[VAR_TO_STR(flameDet)+suffix] = m_pm->m_pmCurrentState.ccustate.flameDetection;
unsigned short faultCode1;
unsigned short faultCode2;
unsigned short faultCode3;
faultCode1 = (m_pm->m_pmCurrentState.ccustate.fcuGZ_1H << 8) | m_pm->m_pmCurrentState.ccustate.fcuGZ_1L;
faultCode2 = (m_pm->m_pmCurrentState.ccustate.fcuGZ_2H << 8) | m_pm->m_pmCurrentState.ccustate.fcuGZ_2L;
faultCode3 = (m_pm->m_pmCurrentState.ccustate.fcuGZ_3H << 8) | m_pm->m_pmCurrentState.ccustate.fcuGZ_3L;
suffix = getTypeName(typeid(faultCode1).name());
fcs[VAR_TO_STR(faultCode1)+suffix] = faultCode1;
suffix = getTypeName(typeid(faultCode2).name());
fcs[VAR_TO_STR(faultCode2)+suffix] = faultCode2;
suffix = getTypeName(typeid(faultCode3).name());
fcs[VAR_TO_STR(faultCode3)+suffix] = faultCode3;
//4.构建动力锂电池状态报告
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insPosRelayState).name());
lbs[VAR_TO_STR(insPosRelayState)+suffix] = m_pm->m_pmCurrentState.ccustate.insPosRelayState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insNegRelayState).name());
lbs[VAR_TO_STR(insNegRelayState)+suffix] = m_pm->m_pmCurrentState.ccustate.insNegRelayState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerPosRelayState).name());
lbs[VAR_TO_STR(powPosRelayState)+suffix] = m_pm->m_pmCurrentState.ccustate.powerPosRelayState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerNegRelayState).name());
lbs[VAR_TO_STR(powNegRelayState)+suffix] = m_pm->m_pmCurrentState.ccustate.powerNegRelayState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insBatLimit).name());
lbs[VAR_TO_STR(insBatLimit)+suffix] = m_pm->m_pmCurrentState.ccustate.insBatLimit;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerBatLimit).name());
lbs[VAR_TO_STR(powerBatLimit)+suffix] = m_pm->m_pmCurrentState.ccustate.powerBatLimit;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insBatSoc).name());
lbs[VAR_TO_STR(insBatSoc)+suffix] = m_pm->m_pmCurrentState.ccustate.insBatSoc;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerBatSoc).name());
lbs[VAR_TO_STR(powBatSoc)+suffix] = m_pm->m_pmCurrentState.ccustate.powerBatSoc;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insBatTotalElectricity).name());
lbs[VAR_TO_STR(insBatTotalElect)+suffix] = m_pm->m_pmCurrentState.ccustate.insBatTotalElectricity;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerBatTotalElectricity).name());
lbs[VAR_TO_STR(powBatTotalElect)+suffix] = m_pm->m_pmCurrentState.ccustate.powerBatTotalElectricity;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insBatCurrentElectricity).name());
lbs[VAR_TO_STR(insBatCurElect)+suffix] = m_pm->m_pmCurrentState.ccustate.insBatCurrentElectricity;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerBatCurrentElectricity).name());
lbs[VAR_TO_STR(powBatCurElect)+suffix] = m_pm->m_pmCurrentState.ccustate.powerBatCurrentElectricity;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insChargeState).name());
lbs[VAR_TO_STR(insChargeState)+suffix] = m_pm->m_pmCurrentState.ccustate.insChargeState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerChargeState).name());
lbs[VAR_TO_STR(powChargeState)+suffix] = m_pm->m_pmCurrentState.ccustate.powerChargeState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insLinkVoltage).name());
lbs[VAR_TO_STR(insLinkVol)+suffix] = m_pm->m_pmCurrentState.ccustate.insLinkVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insPackVoltage).name());
lbs[VAR_TO_STR(insPackVol)+suffix] = m_pm->m_pmCurrentState.ccustate.insPackVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insCurrent).name());
lbs[VAR_TO_STR(insCurrent)+suffix] = m_pm->m_pmCurrentState.ccustate.insCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insPosInsulationResistance).name());
lbs[VAR_TO_STR(insPosRes)+suffix] = m_pm->m_pmCurrentState.ccustate.insPosInsulationResistance;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insNegInsulationResistance).name());
lbs[VAR_TO_STR(insNegRes)+suffix] = m_pm->m_pmCurrentState.ccustate.insNegInsulationResistance;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerLinkVoltage).name());
lbs[VAR_TO_STR(powLinkVol)+suffix] = m_pm->m_pmCurrentState.ccustate.powerLinkVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerPackVoltage).name());
lbs[VAR_TO_STR(powerPackVol)+suffix] = m_pm->m_pmCurrentState.ccustate.powerPackVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerCurrent).name());
lbs[VAR_TO_STR(powCur)+suffix] = m_pm->m_pmCurrentState.ccustate.powerCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerPosInsulationResistance).name());
lbs[VAR_TO_STR(powPosRes)+suffix] = m_pm->m_pmCurrentState.ccustate.powerPosInsulationResistance;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerNegInsulationResistance).name());
lbs[VAR_TO_STR(powNegRes)+suffix] = m_pm->m_pmCurrentState.ccustate.powerNegInsulationResistance;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insCurrentSocHold1).name());
lbs[VAR_TO_STR(insCurSocHold1)+suffix] = m_pm->m_pmCurrentState.ccustate.insCurrentSocHold1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insCurrentSocHold2).name());
lbs[VAR_TO_STR(insCurSocHold2)+suffix] = m_pm->m_pmCurrentState.ccustate.insCurrentSocHold2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insCurrentSocHold3).name());
lbs[VAR_TO_STR(insCurSocHold3)+suffix] = m_pm->m_pmCurrentState.ccustate.insCurrentSocHold3;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuCurrentToInsLimit1).name());
lbs[VAR_TO_STR(fcuCurToInsLimit1)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuCurrentToInsLimit1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.fcuCurrentToinsLimit2).name());
lbs[VAR_TO_STR(fcuCurToinsLimit2)+suffix] = m_pm->m_pmCurrentState.ccustate.fcuCurrentToinsLimit2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerCurrentSocHold1).name());
lbs[VAR_TO_STR(powCurSocHold1)+suffix] = m_pm->m_pmCurrentState.ccustate.powerCurrentSocHold1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerCurrentSocHold2).name());
lbs[VAR_TO_STR(powCurSocHold2)+suffix] = m_pm->m_pmCurrentState.ccustate.powerCurrentSocHold2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerCurrentSocHold3).name());
lbs[VAR_TO_STR(powCurSocHold3)+suffix] = m_pm->m_pmCurrentState.ccustate.powerCurrentSocHold3;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerCurrentToInsLimit1).name());
lbs[VAR_TO_STR(powCurToInsLimit1)+suffix] = m_pm->m_pmCurrentState.ccustate.powerCurrentToInsLimit1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerCurrentToinsLimit2).name());
lbs[VAR_TO_STR(powCurToinsLimit2)+suffix] = m_pm->m_pmCurrentState.ccustate.powerCurrentToinsLimit2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.insAlarmIdentificationWord).name());
lbs[VAR_TO_STR(insfaultCode)+suffix] = m_pm->m_pmCurrentState.ccustate.insAlarmIdentificationWord;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.ccustate.powerAlarmIdentificationWord).name());
lbs[VAR_TO_STR(powfaultCode)+suffix] = m_pm->m_pmCurrentState.ccustate.powerAlarmIdentificationWord;
//5.构建配电系统状态报告
disBus1Breaker* pdis1=new disBus1Breaker;
disBus2Breaker* pdis2=new disBus2Breaker;
disBus3Breaker* pdis3=new disBus3Breaker;
disBus4Breaker* pdis4=new disBus4Breaker;
*pdis1 = m_pm->m_currentDisBreakerState.bus1Breaker;
*pdis2 = m_pm->m_currentDisBreakerState.bus2Breaker;
*pdis3 = m_pm->m_currentDisBreakerState.bus3Breaker;
*pdis4 = m_pm->m_currentDisBreakerState.bus4Breaker;
dis["HVolBusBreaker_uint16"] = *(unsigned short*)pdis1;
dis["HVolABusBreaker_unit16"] = *(unsigned short*)pdis2;
dis["HVolBBusBreaker_unit16"] = *(unsigned short*)pdis3;
dis["LVolBusBreaker_uint16"] = *(unsigned short*)pdis4;
//TODO: 构建配电系统故障码
Json::Value faultCodeDis_array;
for(auto val : m_pm->faultCode)
{
faultCodeDis_array.append(val);
}
dis[VAR_TO_STR(faultCode_array)] = faultCodeDis_array;
Json::Value currentBusState;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.powerBusVoltage).name());
currentBusState[VAR_TO_STR(HVBpowerBusVol)+suffix] = m_pm->m_pmCurrentState.bus1State.powerBusVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.dcDc5ModuleCurrent).name());
currentBusState[VAR_TO_STR(HVBdcDc5Cur)+suffix] = m_pm->m_pmCurrentState.bus1State.dcDc5ModuleCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.powerBusCurrent).name());
currentBusState[VAR_TO_STR(HVBpowerBusCur)+suffix] = m_pm->m_pmCurrentState.bus1State.powerBusCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.busbarAVoltage).name());
currentBusState[VAR_TO_STR(HVBbusAVol)+suffix] = m_pm->m_pmCurrentState.bus1State.busbarAVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.propulsionMotorControlBoxCurrent).name());
currentBusState[VAR_TO_STR(HVBmotorCur)+suffix] = m_pm->m_pmCurrentState.bus1State.propulsionMotorControlBoxCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.busbarACurrent).name());
currentBusState[VAR_TO_STR(HVBbusACur)+suffix] = m_pm->m_pmCurrentState.bus1State.busbarACurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.lithiumBatteryGroupMeterCurrent).name());
currentBusState[VAR_TO_STR(HVBbatGroupCur)+suffix] = m_pm->m_pmCurrentState.bus1State.lithiumBatteryGroupMeterCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.bowHighVoltageDistributionBoxCurrent).name());
currentBusState[VAR_TO_STR(HVBbowHVolDisBoxCur)+suffix] = m_pm->m_pmCurrentState.bus1State.bowHighVoltageDistributionBoxCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.sternFTDevice45Current).name());
currentBusState[VAR_TO_STR(HVBFTDev45Cur)+suffix] = m_pm->m_pmCurrentState.bus1State.sternFTDevice45Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.tyzReservedSwitchCurrent).name());
currentBusState[VAR_TO_STR(HVBtyzCur)+suffix] = m_pm->m_pmCurrentState.bus1State.tyzReservedSwitchCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.sternRudderSwitch1Current).name());
currentBusState[VAR_TO_STR(HVBsternRud1Cur)+suffix] = m_pm->m_pmCurrentState.bus1State.sternRudderSwitch1Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.reservedCurrent1).name());
currentBusState[VAR_TO_STR(HVBreservedCur1)+suffix] = m_pm->m_pmCurrentState.bus1State.reservedCurrent1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.sternRudderSwitch2Current).name());
currentBusState[VAR_TO_STR(HVBsternRud2Cur)+suffix] = m_pm->m_pmCurrentState.bus1State.sternRudderSwitch2Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.reservedCurrent2).name());
currentBusState[VAR_TO_STR(HVBreservedCur2)+suffix] = m_pm->m_pmCurrentState.bus1State.reservedCurrent2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.fbReservedSwitchCurrent).name());
currentBusState[VAR_TO_STR(HVBfbCur)+suffix] = m_pm->m_pmCurrentState.bus1State.fbReservedSwitchCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus1State.reservedCurrent3).name());
currentBusState[VAR_TO_STR(HVBreservedCur3)+suffix] = m_pm->m_pmCurrentState.bus1State.reservedCurrent3;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.busbarBVoltage).name());
currentBusState[VAR_TO_STR(HVABbusBVol)+suffix] = m_pm->m_pmCurrentState.bus2State.busbarBVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.busbarBCurrent).name());
currentBusState[VAR_TO_STR(HVABbusBCur)+suffix] = m_pm->m_pmCurrentState.bus2State.busbarBCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.bowFTDevice123Current).name());
currentBusState[VAR_TO_STR(HVABFTDev123Cur)+suffix] = m_pm->m_pmCurrentState.bus2State.bowFTDevice123Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.actuatorCurrent).name());
currentBusState[VAR_TO_STR(HVABactCur)+suffix] = m_pm->m_pmCurrentState.bus2State.actuatorCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.mastSteeringGearControlBoxCurrent).name());
currentBusState[VAR_TO_STR(HVABmastSteGeaCur)+suffix] = m_pm->m_pmCurrentState.bus2State.mastSteeringGearControlBoxCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.xczCurrent).name());
currentBusState[VAR_TO_STR(HVABxczCur)+suffix] = m_pm->m_pmCurrentState.bus2State.xczCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.bowRudderControlBoxCurrent).name());
currentBusState[VAR_TO_STR(HVABbowRudCur)+suffix] = m_pm->m_pmCurrentState.bus2State.bowRudderControlBoxCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus2State.openWaterCoverStartCylinderCurrent).name());
currentBusState[VAR_TO_STR(HVABopenWaterCur)+suffix] = m_pm->m_pmCurrentState.bus2State.openWaterCoverStartCylinderCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.instrumentBusbarVoltage).name());
currentBusState[VAR_TO_STR(HVBBinsBusVol)+suffix] = m_pm->m_pmCurrentState.bus3State.instrumentBusbarVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.bowLowVoltageDistributionBoxCurrent).name());
currentBusState[VAR_TO_STR(HVBBbowLowVolDisBoxCur)+suffix] = m_pm->m_pmCurrentState.bus3State.bowLowVoltageDistributionBoxCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.dcC1InstrumentDC48VCurrent).name());
currentBusState[VAR_TO_STR(HVBBdcC1InsCur)+suffix] = m_pm->m_pmCurrentState.bus3State.dcC1InstrumentDC48VCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.reservedCurrent1).name());
currentBusState[VAR_TO_STR(HVBBreservedCur1)+suffix] = m_pm->m_pmCurrentState.bus3State.reservedCurrent1;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.emergencyLithiumBatteryGroup2Current).name());
currentBusState[VAR_TO_STR(HVBBemerBatCur)+suffix] = m_pm->m_pmCurrentState.bus3State.emergencyLithiumBatteryGroup2Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.lithiumBatteryGroupInstrumentCurrent).name());
currentBusState[VAR_TO_STR(HVBBbatInsCur)+suffix] = m_pm->m_pmCurrentState.bus3State.lithiumBatteryGroupInstrumentCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.unit4InstrumentDC48VCurrent).name());
currentBusState[VAR_TO_STR(HVBBunit4Cur)+suffix] = m_pm->m_pmCurrentState.bus3State.unit4InstrumentDC48VCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.reservedCurrent2).name());
currentBusState[VAR_TO_STR(HVBBreservedCur2)+suffix] = m_pm->m_pmCurrentState.bus3State.reservedCurrent2;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.emergency2BusbarVoltage).name());
currentBusState[VAR_TO_STR(HVBBemer2BusVol)+suffix] = m_pm->m_pmCurrentState.bus3State.emergency2BusbarVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.compositeEnergyManagementSystemEmergencyDC48VCurrent).name());
currentBusState[VAR_TO_STR(HVBBCCUCur)+suffix] = m_pm->m_pmCurrentState.bus3State.compositeEnergyManagementSystemEmergencyDC48VCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.fuelCellSecuritySystemEmergencyDC48VCurrent).name());
currentBusState[VAR_TO_STR(HVBBFCSSecurSysCur)+suffix] = m_pm->m_pmCurrentState.bus3State.fuelCellSecuritySystemEmergencyDC48VCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus3State.plcControlPowerCurrent).name());
currentBusState[VAR_TO_STR(HVBBplcCur)+suffix] = m_pm->m_pmCurrentState.bus3State.plcControlPowerCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.instrumentBusbarVoltage).name());
currentBusState[VAR_TO_STR(LVBinsBusVol)+suffix] = m_pm->m_pmCurrentState.bus4State.instrumentBusbarVoltage;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.instrumentBusbarCurrent).name());
currentBusState[VAR_TO_STR(LVBBinsBusCur)+suffix] = m_pm->m_pmCurrentState.bus4State.instrumentBusbarCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.unit1Current).name());
currentBusState[VAR_TO_STR(LVBBunit1Cur)+suffix] = m_pm->m_pmCurrentState.bus4State.unit1Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.unit2Current).name());
currentBusState[VAR_TO_STR(LVBBunit2Cur)+suffix] = m_pm->m_pmCurrentState.bus4State.unit2Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.unit3Current).name());
currentBusState[VAR_TO_STR(LVBBunit3Cur)+suffix] = m_pm->m_pmCurrentState.bus4State.unit3Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.unit5Current).name());
currentBusState[VAR_TO_STR(LVBBunit5Cur)+suffix] = m_pm->m_pmCurrentState.bus4State.unit5Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.bowPZDeviceCurrent).name());
currentBusState[VAR_TO_STR(LVBBbowPZDevCur)+suffix] = m_pm->m_pmCurrentState.bus4State.bowPZDeviceCurrent;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.emergencyLithiumBatteryGroup1Current).name());
currentBusState[VAR_TO_STR(LVBBemerBatCur)+suffix] = m_pm->m_pmCurrentState.bus4State.emergencyLithiumBatteryGroup1Current;
suffix = getTypeName(typeid(m_pm->m_pmCurrentState.bus4State.emergency1BusbarVoltage).name());
currentBusState[VAR_TO_STR(LVBBemerBusVol)+suffix] = m_pm->m_pmCurrentState.bus4State.emergency1BusbarVoltage;
// dis["currentBusState"] = currentBusState;
// pms[VAR_TO_STR(fcs_obj)] = fcs;
// pms[VAR_TO_STR(lbs_obj)] = lbs;
// pms[VAR_TO_STR(dis_obj)] = dis;
m_pm->sendNotification(PM_TO_DB_STATE, pms.toStyledString());
m_pm->sendNotification(PM_TO_DB_FCSSTATE, fcs.toStyledString());
m_pm->sendNotification(PM_TO_DB_LIBATSTATE, lbs.toStyledString());
m_pm->sendNotification(PM_TO_DB_DISSTATE, dis.toStyledString());
m_pm->sendNotification(PM_TO_DB_CURRENTSTATE, currentBusState.toStyledString());
// 使用StreamWriterBuilder来设置保持插入顺序
Json::StreamWriterBuilder builder;
builder["indentation"] = " "; // 设置不缩进,保持紧凑格式
builder["enableYAMLCompatibility"] = true; // 保持插入顺序
// 将Json::Value对象转换为字符串
string pathFile = "/home/zjk/project/H100/H100PowerManger/data/msg/";
string jsonStr = Json::writeString(builder, pms);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_STATE+".json");
jsonStr = Json::writeString(builder, fcs);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_FCSSTATE+".json");
jsonStr = Json::writeString(builder, lbs);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_LIBATSTATE+".json");
jsonStr = Json::writeString(builder, dis);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_DISSTATE + ".json");
jsonStr = Json::writeString(builder, currentBusState);
saveStringToJsonFile(jsonStr, pathFile+PM_TO_DB_CURRENTSTATE + ".json");
// return true;
}
bool UpperCommManager::saveStringToJsonFile(const string &strJson, const string &filePath)
{
std::ofstream file(filePath, std::ios::trunc);
if (file.is_open()) {
file << strJson;
file.close();
// std::cout << "字符串已成功保存到文件中。" << std::endl;
return true;
} else {
std::cerr << "无法打开文件!" << std::endl;
return false;
}
}
string UpperCommManager::getTypeName(const string typeName)
{
// 映射类型名称到指定的字符串
if (typeName == "i" || typeName == "c") {
return "_int8";
} else if (typeName == "h" || typeName == "C") {
return "_uint8";
} else if (typeName == "s" || typeName == "S") {
return "_int16";
} else if (typeName == "t" || typeName == "T") {
return "_uint16";
} else if (typeName == "i" || typeName == "I") {
return "_int32";
} else if (typeName == "j" || typeName == "J") {
return "_uint32";
} else if (typeName == "x" || typeName == "X") {
return "_int64";
} else if (typeName == "f" || typeName == "F") {
return "_fp32";
} else if (typeName == "d" || typeName == "D") {
return "_fp64";
} else if (typeName == "b" || typeName == "B") {
return "_bool";
} else if (typeName == "NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE") {
return "_str";
} else if (typeName == "St6vector" || typeName == "St5deque" || typeName == "St4list" || typeName == "St4pair") {
return "_array";
} else {
// 默认情况
return "_obj";
}
}
+70
View File
@@ -0,0 +1,70 @@
#ifndef UPPER_COMM_MANAGER_H
#define UPPER_COMM_MANAGER_H
#include "MOOS/libMOOS/Thirdparty/AppCasting/AppCastingMOOSApp.h"
#include "pmSysvariable.h"
#include "driver.h"
#include "json/json.h"
#include <string>
#include <vector>
#include <map>
using namespace std;
#define VAR_TO_STR(var) #var
//能源管理反馈的消息
#define PM_TO_DB_STATE "uPower_pmState_st"
#define PM_TO_DB_CURRENTSTATE "uPower_currentState_st"
#define PM_TO_DB_DISSTATE "uPower_disSysState_st"
#define PM_TO_DB_LIBATSTATE "uPower_liBatState_st"
#define PM_TO_DB_FCSSTATE "uPower_fcsState_st"
#define PM_TO_DB_SUBDISCMD "uPower_secdistState_cmd"
#define PM_TO_DB_EQUIPMENTSTATE "uPower_equState_fb"
//控制消息
#define DB_TO_PM_MSCMD "uPower_sysState_cmd"
#define DB_TO_PM_EQUIPMENTCMD "uPower_equState_cmd"
#define DB_TO_PM_SUBDISSTATE "uDevice_secdistState_st"
//决策消息
#define DB_TO_PM_DEEPTH "uDevice_depth_st"
#define DB_TO_PM_MISSION "uPlan_taskStart_cmd"
class PowerManger; // 前向声明
class UpperCommManager {
public:
UpperCommManager(PowerManger* powerManager);
~UpperCommManager();
// 周期调用
void processPeriodicTasks();
// 处理上位机消息
bool processUpperMsg(const std::string& key, const std::string& msg);
// 生成系统报告
void buildSystemReport();
// 消息处理函数
const MSControlCmd getMasterControlCmd(const std::string& key, const std::string& msg);
const DriverTable getEquipmentCmd(const std::string& key, const std::string& msg);
const DriverTable getSubDisState(const std::string& key, const std::string& msg);
const int getDeepth(const std::string& key, const std::string& msg);
const std::string getCurrentEquState(DriverTable& driver);
const std::string getSubDisCmd(DriverTable& driver);
// 其他公共函数
string getTypeName(const string typeName);
bool saveStringToJsonFile(const string &strJson, const string &filePath);
private:
PowerManger* m_pm;
MSControlCmd m_msCmd;
Driver m_driver;
// 从JSON解析控制命令
bool parseControlCmdFromJson(MSControlCmd &cmd, const std::string &strJson);
};
#endif // UPPER_COMM_MANAGER_H
+62 -62
View File
@@ -59,72 +59,72 @@ int Driver::getDriverTable(workCondition condition, DriverTable* driverTable)
void Driver::getSTANDBYTable(DriverTable* driverTable)
{
//打开所有传感器
driverTable->depth1 = DRIVER_FULL_POWER_ON;
driverTable->depth2 = DRIVER_FULL_POWER_ON;
driverTable->depth3 = DRIVER_FULL_POWER_ON;
driverTable->overDeep1 = DRIVER_FULL_POWER_ON;
driverTable->overDeep2 = DRIVER_FULL_POWER_ON;
driverTable->ctd1 = DRIVER_FULL_POWER_ON;
driverTable->ctd2 = DRIVER_FULL_POWER_ON;
driverTable->soundVeloc = DRIVER_FULL_POWER_ON;
driverTable->commMast = DRIVER_FULL_POWER_ON;
driverTable->ins = DRIVER_FULL_POWER_ON;
driverTable->ntp = DRIVER_FULL_POWER_ON;
driverTable->inputWaterSensor1 = DRIVER_FULL_POWER_ON;
driverTable->inputWaterSensor2 = DRIVER_FULL_POWER_ON;
driverTable->inputWaterSensor3 = DRIVER_FULL_POWER_ON;
driverTable->depth1 = 1;
driverTable->depth2 = 1;
driverTable->depth3 = 1;
driverTable->overDeep1 = 1;
driverTable->overDeep2 = 1;
driverTable->ctd1 = 1;
driverTable->ctd2 = 1;
driverTable->soundVeloc = 1;
driverTable->commMast = 1;
driverTable->ins = 1;
driverTable->ntp = 1;
driverTable->inputWaterSensor1 = 1;
driverTable->inputWaterSensor2 = 1;
driverTable->inputWaterSensor3 = 1;
//关闭所有负载
driverTable->photoSensor = DRIVER_STANDBY;
driverTable->electSensor = DRIVER_STANDBY;
driverTable->launchControler = DRIVER_STANDBY;
driverTable->sideSonar = DRIVER_POWER_OFF;
driverTable->buoyage = DRIVER_POWER_OFF;
driverTable->photoSensor = 0;
driverTable->electSensor = 0;
driverTable->launchControler = 0;
driverTable->sideSonar = 0;
driverTable->buoyage = 0;
//关闭所有水声设备
driverTable->singleBeamSonar1 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar2 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar3 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar4 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar5 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar6 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar7 = DRIVER_POWER_OFF;
driverTable->singleBeamSonar8 = DRIVER_POWER_OFF;
driverTable->DVL = DRIVER_POWER_OFF;
driverTable->forwardSonar = DRIVER_POWER_OFF;
driverTable->lowFreqSonar = DRIVER_POWER_OFF;
driverTable->USBL = DRIVER_POWER_OFF;
driverTable->singleBeamSonar1 = 0;
driverTable->singleBeamSonar2 = 0;
driverTable->singleBeamSonar3 = 0;
driverTable->singleBeamSonar4 = 0;
driverTable->singleBeamSonar5 = 0;
driverTable->singleBeamSonar6 = 0;
driverTable->singleBeamSonar7 = 0;
driverTable->singleBeamSonar8 = 0;
driverTable->DVL = 0;
driverTable->forwardSonar = 0;
driverTable->lowFreqSonar = 0;
driverTable->USBL = 0;
//关闭所有执行机构
driverTable->bowCover1 = DRIVER_POWER_OFF;
driverTable->bowCover2 = DRIVER_POWER_OFF;
driverTable->bowCover3 = DRIVER_POWER_OFF;
driverTable->bowCover4 = DRIVER_POWER_OFF;
driverTable->bowCover5 = DRIVER_POWER_OFF;
driverTable->bowCover6 = DRIVER_POWER_OFF;
driverTable->bowRudderDeploy = DRIVER_POWER_OFF;
driverTable->bowRudderServo1 = DRIVER_POWER_OFF;
driverTable->bowRudderServo2 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet1 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet2 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet3 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet4 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet5 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet6 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet7 = DRIVER_POWER_OFF;
driverTable->bowWaterlnet8 = DRIVER_POWER_OFF;
driverTable->waterInject = DRIVER_POWER_OFF;
driverTable->mastLiftingServo = DRIVER_POWER_OFF;
driverTable->buoyancyAdjust1 = DRIVER_POWER_OFF;
driverTable->buoyancyAdjust2 = DRIVER_POWER_OFF;
driverTable->buoyancyAdjust3 = DRIVER_POWER_OFF;
driverTable->buoyancyAdjust4 = DRIVER_POWER_OFF;
driverTable->buoyancyAdjust5 = DRIVER_POWER_OFF;
driverTable->sternRudderServo1 = DRIVER_POWER_OFF;
driverTable->sternRudderServo2 = DRIVER_POWER_OFF;
driverTable->sternRudderServo3 = DRIVER_POWER_OFF;
driverTable->sternRudderServo4 = DRIVER_POWER_OFF;
driverTable->thruster = DRIVER_POWER_OFF;
driverTable->bowThrow = DRIVER_POWER_OFF;
driverTable->sternThrow = DRIVER_POWER_OFF;
driverTable->bowCover1 = 0;
driverTable->bowCover2 = 0;
driverTable->bowCover3 = 0;
driverTable->bowCover4 = 0;
driverTable->bowCover5 = 0;
driverTable->bowCover6 = 0;
driverTable->bowRudderDeploy = 0;
driverTable->bowRudderServo1 = 0;
driverTable->bowRudderServo2 = 0;
driverTable->bowWaterlnet1 = 0;
driverTable->bowWaterlnet2 = 0;
driverTable->bowWaterlnet3 = 0;
driverTable->bowWaterlnet4 = 0;
driverTable->bowWaterlnet5 = 0;
driverTable->bowWaterlnet6 = 0;
driverTable->bowWaterlnet7 = 0;
driverTable->bowWaterlnet8 = 0;
driverTable->waterInject = 0;
driverTable->mastLiftingServo = 0;
driverTable->buoyancyAdjust1 = 0;
driverTable->buoyancyAdjust2 = 0;
driverTable->buoyancyAdjust3 = 0;
driverTable->buoyancyAdjust4 = 0;
driverTable->buoyancyAdjust5 = 0;
driverTable->sternRudderServo1 = 0;
driverTable->sternRudderServo2 = 0;
driverTable->sternRudderServo3 = 0;
driverTable->sternRudderServo4 = 0;
driverTable->thruster = 0;
driverTable->bowThrow = 0;
driverTable->sternThrow = 0;
}
void Driver::getSHORE_BASED_READYable(DriverTable* driverTable)
-103
View File
@@ -1,103 +0,0 @@
#include "fsm.h"
#include <iostream>
FSM::FSM(/* args */)
{
idStateMap.clear();
handleMap.clear();
stateMap.clear();
};
FSM::~FSM()
{
};
int FSM::run(FSM_State &fsm,void* data)
{
FSM_Handle handle = handleMap[fsm.id];
return handle(fsm,data);
}
int FSM::RegisterHandleFunction(FSM_State* fsm, FSM_Handle handle)
{
if(fsm == NULL)
return -1;
if(handleMap.count(fsm->id) > 0)
return -1;
handleMap[fsm->id] = handle;
return 0;
}
int FSM::unRegisterHandleFunction(FSM_State* fsm)
{
if(fsm == NULL)
return -1;
if(handleMap.count(fsm->id) <= 0)
return -1;
handleMap.erase(fsm->id);
return 0;
}
int FSM::RegisterState(std::string name, unsigned int id,FSM_State &state)
{
if(stateMap.count(name) > 0)
return -1;
if(idStateMap.count(id) > 0)
return -1;
state.name = name;
state.id = id;
stateMap[name] = state;
idStateMap[id] = state;
return 0;
}
unsigned int FSM::getStateId(std::string name)
{
return stateMap[name].id;
}
FSM_State FSM::getState(std::string name)
{
return stateMap[name];
}
int FSM::RegisterEvent(FSM_State* fsm, std::string event, FSM_State *state)
{
if(fsm == NULL)
return -1;
fsm->event[event] = state;
return 0;
}
int FSM::unRegisterEvent(FSM_State* fsm, std::string event)
{
if(fsm == NULL)
return -1;
if(fsm->event.count(event) <= 0)
return -1;
fsm->event.erase(event);
return 0;
}
int FSM::run(FSM_State *fsm,void* data, std::string event)
{
if(fsm->event.count(event) <= 0)
{
std::cout << "no event" << std::endl;
return run(*(fsm),data);
}
else
{
int r = run(*(fsm),data);
// fsm = fsm->event[event];
*fsm = *fsm->event[event];
// std::cout << "event " << event <<std::endl;
// std::cout << "fsm" << fsm->name << std::endl;
return r;
}
}
// int FSM::run(FSM_State *fsm,void* data)
// {
// return run(*fsm,data);
// }
-40
View File
@@ -1,40 +0,0 @@
#ifndef FSM_H
#define FSM_H
#include <string>
#include <map>
typedef struct FSM_State
{
std::string name;
unsigned int id;
std::map<std::string,FSM_State*> event;
}FSM_State;
typedef int (*FSM_Handle)(FSM_State &fsm, void *data);
// typedef int (*FSM_EventHandle)(FSM_State &fsm,std::string event,void *data);
class FSM
{
public:
FSM();
~FSM();
std::map<unsigned int, FSM_Handle> handleMap;
std::map<std::string, FSM_State> stateMap;
std::map<unsigned int, FSM_State> idStateMap;
int run(FSM_State &fsm,void* data);
int run(FSM_State *fsm,void* data, std::string event);
//TODO: 事件注册机制实现
int RegisterEvent(FSM_State* fsm, std::string event, FSM_State *state);
int unRegisterEvent(FSM_State* fsm, std::string event);
int RegisterState(std::string name, unsigned int id,FSM_State &state);
FSM_State getState(std::string name);
unsigned int getStateId(std::string name);
int RegisterHandleFunction(FSM_State* fsm, FSM_Handle handle);
int unRegisterHandleFunction(FSM_State* fsm);
};
#endif
+49
View File
@@ -360,5 +360,54 @@ typedef struct pmState
unsigned int workCMD;
}MSControlCmd;
namespace FcuCommand {
enum Type : unsigned char {
SelfCheck = 0x01,
Start = 0x02,
Stop = 0x03,
Reset = 0x04,
EmergencyStop = 0x05,
Supply = 0x06,
WarmUp = 0x07,
Maintenance = 0x08
};
}
enum class FcuState : unsigned char {
//初始化
Init = 0x01,
//待机
Standby = 0x02,
//自检异常
SelfCheckError = 0x03,
//就绪
Ready = 0x04,
//启动
Start = 0x05,
//运行
Running = 0x06,
//停机
Stop = 0x07,
//故障
Fault = 0x08,
//补给
Supply = 0x09,
//预热
WarmUp = 0x0A,
//预热完成
WarmUpComplete = 0x0B,
//维护
Maintenance = 0x0C,
//维护完成
MaintenanceComplete = 0x0D
};
namespace BatteryCommand {
enum Type : unsigned char {
Start = 0x10,
Stop = 0x20
};
}
#endif
+251
View File
@@ -0,0 +1,251 @@
/*
* TinyFSM - Tiny Finite State Machine Processor
*
* Copyright (c) 2012-2022 Axel Burri
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/* ---------------------------------------------------------------------
* Version: 0.3.3
*
* API documentation: see "../doc/50-API.md"
*
* The official TinyFSM website is located at:
* https://digint.ch/tinyfsm/
*
* Author:
* Axel Burri <axel@tty0.ch>
* ---------------------------------------------------------------------
*/
#ifndef TINYFSM_HPP_INCLUDED
#define TINYFSM_HPP_INCLUDED
#ifndef TINYFSM_NOSTDLIB
#include <type_traits>
#endif
// #include <iostream>
// #define DBG(str) do { std::cerr << str << std::endl; } while( false )
// DBG("*** dbg_example *** " << __PRETTY_FUNCTION__);
namespace tinyfsm
{
// --------------------------------------------------------------------------
struct Event { };
// --------------------------------------------------------------------------
#ifdef TINYFSM_NOSTDLIB
// remove dependency on standard library (silent fail!).
// useful in conjunction with -nostdlib option, e.g. if your compiler
// does not provide a standard library.
// NOTE: this silently disables all static_assert() calls below!
template<typename F, typename S>
struct is_same_fsm { static constexpr bool value = true; };
#else
// check if both fsm and state class share same fsmtype
template<typename F, typename S>
struct is_same_fsm : std::is_same< typename F::fsmtype, typename S::fsmtype > { };
#endif
template<typename S>
struct _state_instance
{
using value_type = S;
using type = _state_instance<S>;
static S value;
};
template<typename S>
typename _state_instance<S>::value_type _state_instance<S>::value;
// --------------------------------------------------------------------------
template<typename F>
class Fsm
{
public:
using fsmtype = Fsm<F>;
using state_ptr_t = F *;
static state_ptr_t current_state_ptr;
// public, leaving ability to access state instance (e.g. on reset)
template<typename S>
static constexpr S & state(void) {
static_assert(is_same_fsm<F, S>::value, "accessing state of different state machine");
return _state_instance<S>::value;
}
template<typename S>
static constexpr bool is_in_state(void) {
static_assert(is_same_fsm<F, S>::value, "accessing state of different state machine");
return current_state_ptr == &_state_instance<S>::value;
}
/// state machine functions
public:
// explicitely specialized in FSM_INITIAL_STATE macro
static void set_initial_state();
static void reset() { };
static void enter() {
current_state_ptr->entry();
}
static void start() {
set_initial_state();
enter();
}
template<typename E>
static void dispatch(E const & event) {
current_state_ptr->react(event);
}
/// state transition functions
protected:
template<typename S>
void transit(void) {
static_assert(is_same_fsm<F, S>::value, "transit to different state machine");
current_state_ptr->exit();
current_state_ptr = &_state_instance<S>::value;
current_state_ptr->entry();
}
template<typename S, typename ActionFunction>
void transit(ActionFunction action_function) {
static_assert(is_same_fsm<F, S>::value, "transit to different state machine");
current_state_ptr->exit();
// NOTE: do not send events in action_function definisions.
action_function();
current_state_ptr = &_state_instance<S>::value;
current_state_ptr->entry();
}
template<typename S, typename ActionFunction, typename ConditionFunction>
void transit(ActionFunction action_function, ConditionFunction condition_function) {
if(condition_function()) {
transit<S>(action_function);
}
}
};
template<typename F>
typename Fsm<F>::state_ptr_t Fsm<F>::current_state_ptr;
// --------------------------------------------------------------------------
template<typename... FF>
struct FsmList;
template<> struct FsmList<> {
static void set_initial_state() { }
static void reset() { }
static void enter() { }
template<typename E>
static void dispatch(E const &) { }
};
template<typename F, typename... FF>
struct FsmList<F, FF...>
{
using fsmtype = Fsm<F>;
static void set_initial_state() {
fsmtype::set_initial_state();
FsmList<FF...>::set_initial_state();
}
static void reset() {
F::reset();
FsmList<FF...>::reset();
}
static void enter() {
fsmtype::enter();
FsmList<FF...>::enter();
}
static void start() {
set_initial_state();
enter();
}
template<typename E>
static void dispatch(E const & event) {
fsmtype::template dispatch<E>(event);
FsmList<FF...>::template dispatch<E>(event);
}
};
// --------------------------------------------------------------------------
template<typename... SS> struct StateList;
template<> struct StateList<> {
static void reset() { }
};
template<typename S, typename... SS>
struct StateList<S, SS...>
{
static void reset() {
_state_instance<S>::value = S();
StateList<SS...>::reset();
}
};
// --------------------------------------------------------------------------
template<typename F>
struct MooreMachine : tinyfsm::Fsm<F>
{
virtual void entry(void) { }; /* entry actions in some states */
void exit(void) { }; /* no exit actions */
};
template<typename F>
struct MealyMachine : tinyfsm::Fsm<F>
{
// input actions are modeled in react():
// - conditional dependent of event type or payload
// - transit<>(ActionFunction)
void entry(void) { }; /* no entry actions */
void exit(void) { }; /* no exit actions */
};
} /* namespace tinyfsm */
#define FSM_INITIAL_STATE(_FSM, _STATE) \
namespace tinyfsm { \
template<> void Fsm< _FSM >::set_initial_state(void) { \
current_state_ptr = &_state_instance< _STATE >::value; \
} \
}
#endif /* TINYFSM_HPP_INCLUDED */
+15 -2
View File
@@ -4,10 +4,17 @@
udpComm::udpComm()
{
udpScoket = nullptr;
ccuHost = "";
ccuPort = 0;
}
udpComm::~udpComm() {}
udpComm::~udpComm() {
if (udpScoket) {
delete udpScoket;
udpScoket = nullptr;
}
}
Checksum udpComm::calculateChecksum(const unsigned char* data, int size)
@@ -490,6 +497,12 @@ bool udpComm::sendCcuColCmd(const ccuColCmd cmd)
msg->checkCode = static_cast<cChecksum>(cc & 0xFF);
try
{
if(udpScoket == nullptr) {
throw std::runtime_error("UDP socket not initialized");
}
if(ccuHost.empty() || ccuPort <= 0 || ccuPort > 65535) {
throw std::runtime_error("Invalid CCU address: " + ccuHost + ":" + std::to_string(ccuPort));
}
sendMsg(msgBuf,sizeof(msg_CcuColCmdMsg));
}
catch(const std::exception& e)
+1
View File
@@ -50,6 +50,7 @@ public:
bool sendDisSysCmd(const disHighBVolBusCmd cmd);
bool sendDisSysCmd(const disLowBusCmd cmd);
bool sendMsg(const unsigned char* msg,unsigned int size){
// cout << "sendMsgTo" << ccuHost << ":" << ccuPort << endl;
udpScoket->iSendMessageTo((void*) msg, size, ccuPort, ccuHost);
return true;
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.