增加了udp的测试代码,增加了时间获取函数,完善了ccu发送指令的函数

This commit is contained in:
zjk
2025-02-18 16:57:11 +08:00
parent 2599050c1f
commit a179789b3f
12 changed files with 247 additions and 99 deletions
+1 -1
View File
@@ -3,7 +3,7 @@
ProcessConfig = pPowerManger
{
AppTick = 2
AppTick = 5
CommsTick = 4
}
+1 -1
View File
@@ -1,7 +1,7 @@
{
"HVolABusBreaker_unit16": 0,
"HVolBBusBreaker_unit16": 79,
"HVolBusBreaker_uint16": 32,
"HVolBusBreaker_uint16": 58,
"LVolBusBreaker_uint16": 159,
"faultCode_array":
[
+25 -25
View File
@@ -1,30 +1,30 @@
{
"backpressure1_fp32": 2.3670901456028489e+36,
"backpressure2_fp32": 4.5630481893809018e-41,
"ch4oConcent1_fp32": 4.2038953929744512e-45,
"ch4oConcent2_fp32": 0.0,
"backpressure1_fp32": 7.0,
"backpressure2_fp32": 0.0,
"ch4oConcent1_fp32": 35.0,
"ch4oConcent2_fp32": 58.0,
"ch4o_fp32": 0.0,
"excTemp_fp32": 4.5630481893809018e-41,
"faultCode1_uint16": 20381,
"faultCode2_uint16": 13180,
"faultCode3_uint16": 127,
"fcuCurIns_fp32": 5.7521569093081287e-30,
"excTemp_fp32": 100.0,
"faultCode1_uint16": 0,
"faultCode2_uint16": 0,
"faultCode3_uint16": 0,
"fcuCurIns_fp32": 0.0,
"fcuCurPower_fp32": 0.0,
"fcuCur_fp32": -4.4122612457458051e+32,
"fcuFaultLeve_uint8": 124,
"fcuOutIns_fp32": 4.5630481893809018e-41,
"fcuOutPower_fp32": 3.4750156610080462e+36,
"fcuState_uint8": 39,
"fcuVolIns_fp32": 4.3786375736699354e+36,
"fcuVolPower_fp32": 4.5630481893809018e-41,
"fcuVol_fp32": 0.0,
"flameDet_uint8": 88,
"h2Concent1_fp32": -644240900096.0,
"h2Concent2_fp32": 0.0,
"o2Concent1_fp32": 0.0,
"o2Concent2_fp32": 0.0,
"o2Level_fp32": 3.0722067531857289e-41,
"fcuCur_fp32": 81.0,
"fcuFaultLeve_uint8": 0,
"fcuOutIns_fp32": 28.0,
"fcuOutPower_fp32": 753.0,
"fcuState_uint8": 0,
"fcuVolIns_fp32": 0.0,
"fcuVolPower_fp32": 0.0,
"fcuVol_fp32": 341.0,
"flameDet_uint8": 1,
"h2Concent1_fp32": 4.0,
"h2Concent2_fp32": 69.0,
"o2Concent1_fp32": 54.0,
"o2Concent2_fp32": 91.0,
"o2Level_fp32": 0.0,
"o2_fp32": 0.0,
"powLimit_fp32": 4.5630481893809018e-41,
"powerGen_fp32": 9.8090892502737195e-45
"powLimit_fp32": 21.0,
"powerGen_fp32": 757.0
}
+27 -27
View File
@@ -1,38 +1,38 @@
{
"fcuCurToInsLimit1_uint8": 0,
"fcuCurToinsLimit2_uint8": 0,
"insBatCurElect_uint16": 127,
"insBatLimit_uint16": 13180,
"insBatSoc_uint8": 0,
"insBatTotalElect_uint16": 20377,
"insChargeState_uint8": 85,
"insBatCurElect_uint16": 0,
"insBatLimit_uint16": 6,
"insBatSoc_uint8": 5,
"insBatTotalElect_uint16": 529,
"insChargeState_uint8": 0,
"insCurSocHold1_uint8": 0,
"insCurSocHold2_uint8": 0,
"insCurSocHold3_uint8": 0,
"insCurrent_uint16": 0,
"insCurrent_uint16": 36,
"insLinkVol_uint16": 0,
"insNegRelayState_uint8": 208,
"insNegRes_uint16": 65235,
"insNegRelayState_uint8": 0,
"insNegRes_uint16": 0,
"insPackVol_uint16": 0,
"insPosRelayState_uint8": 0,
"insPosRes_uint16": 5632,
"insPosRelayState_uint8": 1,
"insPosRes_uint16": 0,
"insfaultCode_uint8": 0,
"powBatCurElect_uint16": 58368,
"powBatSoc_uint8": 208,
"powBatTotalElect_uint16": 13180,
"powChargeState_uint8": 48,
"powCurSocHold1_uint8": 208,
"powCurSocHold2_uint8": 97,
"powCurSocHold3_uint8": 39,
"powCurToInsLimit1_uint8": 124,
"powCurToinsLimit2_uint8": 51,
"powCur_uint16": 20538,
"powLinkVol_uint16": 127,
"powNegRelayState_uint8": 39,
"powNegRes_uint16": 127,
"powPosRelayState_uint8": 97,
"powPosRes_uint16": 13180,
"powerBatLimit_uint16": 127,
"powerPackVol_uint16": 18432,
"powBatCurElect_uint16": 0,
"powBatSoc_uint8": 64,
"powBatTotalElect_uint16": 70,
"powChargeState_uint8": 0,
"powCurSocHold1_uint8": 0,
"powCurSocHold2_uint8": 0,
"powCurSocHold3_uint8": 0,
"powCurToInsLimit1_uint8": 0,
"powCurToinsLimit2_uint8": 0,
"powCur_uint16": 30,
"powLinkVol_uint16": 0,
"powNegRelayState_uint8": 1,
"powNegRes_uint16": 0,
"powPosRelayState_uint8": 1,
"powPosRes_uint16": 0,
"powerBatLimit_uint16": 10,
"powerPackVol_uint16": 0,
"powfaultCode_uint8": 0
}
+56 -11
View File
@@ -37,10 +37,19 @@ PowerManger::PowerManger()
falutLevel = 0;
soc = 0;
sustainableTime = 0;
m_connected = false;
m_faultNum = 0;
m_ticker = 0;
faultCode.push_back(0);
msg_CcuStateFbMsg_Update_time = 0;
// msg_CcuSetParmFbMsg_Update_time = 0;
msg_disHighVolBusFbMsg_Update_time = 0;
msg_disHighAVolBusFbMsg_Update_time = 0;
msg_disHighBVolBusFbMsg_Update_time = 0;
msg_disLowBusFbMsg_Update_time = 0;
connectState = 0;
//初始化断路器状态
initTargetDisBreakerState();
@@ -115,22 +124,27 @@ bool PowerManger::OnConnectToServer()
//---------------------------------------------------------
// Procedure: Iterate()
// happens AppTick times per second
//固定为4Hz
bool PowerManger::Iterate()
{
AppCastingMOOSApp::Iterate();
//1. 更新当前状态
updatePmState();
updateTime();
updateConnectState();
//2. 运行状态机
if (FSM_RUN_IS_OK)
m_FsmThread.Start();
else
m_FsmThread.Stop();
//进行配电系统操作
//3. 进行操作 每秒执行一次
if (m_ticker++ % 5 == 0)
{
//a. 配电系统操作
disSysOption();
//3. 输出报告
//b. ccu操作
//c. 输出报告
buildPowerSysReport();
}
//4. 打印错误信息
if(!m_udpComm.sError.empty())
{
@@ -263,7 +277,6 @@ bool PowerManger::ListenLoopUDP()
cout<<MOOS::ConsoleColours::red()<< m_udpComm.sError[i] << MOOS::ConsoleColours::reset() << endl;
}
}
m_connected = true;
}
// updatePmState();
}
@@ -281,6 +294,31 @@ bool PowerManger::FsmLoop()
return true;
}
bool PowerManger::updateConnectState()
{
double seck = 0;
updateTime();
connectState = 0;
IsSkewed(msg_CcuStateFbMsg_Update_time,&seck);
connectState++;
// cout << "seck:" << seck << endl;
IsSkewed(msg_disHighVolBusFbMsg_Update_time,&seck);
connectState++;
// cout << "seck:" << seck << endl;
IsSkewed(msg_disHighAVolBusFbMsg_Update_time,&seck);
connectState++;
// cout << "seck:" << seck << endl;
IsSkewed(msg_disHighBVolBusFbMsg_Update_time,&seck);
connectState++;
// cout << "seck:" << seck << endl;
IsSkewed(msg_disLowBusFbMsg_Update_time,&seck);
connectState++;
// cout << "seck:" << seck << endl;
return true;
}
bool PowerManger::updatePmState()
{
//1. 更新下位机状态
@@ -290,6 +328,7 @@ bool PowerManger::updatePmState()
m_udpComm.popMsgFormQueue(time,m_CcuCurrentState);
m_pmCurrentState.ccustate = m_CcuCurrentState.data;
m_db->insertCcuData(m_CcuCurrentState.data); //写入数据库
msg_CcuStateFbMsg_Update_time = time;
}
if(!m_udpComm.m_qReceiveDisHighVolBusBuffer.empty())
{
@@ -297,6 +336,7 @@ bool PowerManger::updatePmState()
m_udpComm.popMsgFormQueue(time,m_disHighVolBusCurrentState);
m_pmCurrentState.bus1State = m_disHighVolBusCurrentState.data;
m_db->insertDisHighVolBusData(m_disHighVolBusCurrentState.data); //写入数据库
msg_disHighVolBusFbMsg_Update_time = time;
}
if(!m_udpComm.m_qReceiveDisHighAVolBusBuffer.empty())
{
@@ -304,6 +344,7 @@ bool PowerManger::updatePmState()
m_udpComm.popMsgFormQueue(time,m_disHighAVolBusCurrentState);
m_pmCurrentState.bus2State = m_disHighAVolBusCurrentState.data;
m_db->insertDisHighAVolBusData(m_disHighAVolBusCurrentState.data); //写入数据库
msg_disHighAVolBusFbMsg_Update_time = time;
}
if(!m_udpComm.m_qReceiveDisHighBVolBusBuffer.empty())
{
@@ -311,6 +352,7 @@ bool PowerManger::updatePmState()
m_udpComm.popMsgFormQueue(time,m_disHighBVolBusCurrentState);
m_pmCurrentState.bus3State = m_disHighBVolBusCurrentState.data;
m_db->insertDisHighBVolBusData(m_disHighBVolBusCurrentState.data); //写入数据库
msg_disHighBVolBusFbMsg_Update_time = time;
}
if(!m_udpComm.m_qReceiveDisLowBusBuffer.empty())
{
@@ -318,6 +360,7 @@ bool PowerManger::updatePmState()
m_udpComm.popMsgFormQueue(time,m_disLowBusState);
m_pmCurrentState.bus4State = m_disLowBusState.data;
m_db->insertDisLowBusData(m_disLowBusState.data); //写入数据库
msg_disLowBusFbMsg_Update_time = time;
}
return true;
}
@@ -396,6 +439,9 @@ void PowerManger::disSysOption()
//a. 高压母线的断路器
disHighVolBusCmd a;
a.fuelCellCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.fuelCellCircuitBreaker);
a.lithiumBatteryGroupInstrumentCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.lithiumBatteryGroupInstrumentCircuitBreaker);
a.dcDc5ModuleCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.dcDc5ModuleCircuitBreaker);
a.powerLithiumBatteryCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.powerLithiumBatteryCircuitBreaker);
a.propulsionMotorCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.propulsionMotorCircuitBreaker);
a.bowHighVoltageDistributionBoxCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.bowHighVoltageDistributionBoxCircuitBreaker);
a.tyzReservedCircuitBreaker = getBreakerOption(m_targetDisBreakerState.bus1Breaker.tyzReservedCircuitBreaker);
@@ -485,9 +531,9 @@ void PowerManger::printState(FSM_State st, string desp){
int PowerManger::handleInite_s1(FSM_State &fsm, void* pData)
{
PowerManger* pm = (PowerManger*)pData;
//1. 检查复合管控器连接 如果10S则进入故障状态
//1. 检查通信是否正常 如果超时则进入故障状态
double StartTime = pm->GetAppStartTime();
while (!pm->m_connected)
while (!pm->connectState == 5)
{
MOOSPause(1000);
pm->updateTime();
@@ -495,7 +541,7 @@ int PowerManger::handleInite_s1(FSM_State &fsm, void* pData)
if(dft > 10)
{
pm->m_pmState = pm->m_Fault_s99;
pm->m_faultNum = 1; // 通信超时
pm->m_faultNum = 1;
break;
}
string desp = "等待复合管控消息 : " + to_string(10-dft);
@@ -536,7 +582,6 @@ int PowerManger::handleStandby_s2(FSM_State &fsm, void* pData)
}
int PowerManger::handleFault_s99(FSM_State &fsm, void* pData)
{
// cout << fsm.name << endl;
@@ -561,7 +606,7 @@ void PowerManger::initTargetDisBreakerState()
m_targetDisBreakerState.bus1Breaker.fuelCellCircuitBreaker = 0;
m_targetDisBreakerState.bus1Breaker.powerLithiumBatteryCircuitBreaker = 1; //动力锂电池默认接入
m_targetDisBreakerState.bus1Breaker.propulsionMotorCircuitBreaker = 0;
m_targetDisBreakerState.bus1Breaker.lithiumBatteryGroupInstrumentCircuitBreaker = 0; //仪表锂电池默认接入
m_targetDisBreakerState.bus1Breaker.lithiumBatteryGroupInstrumentCircuitBreaker = 1; //仪表锂电池默认接入
m_targetDisBreakerState.bus1Breaker.dcDc5ModuleCircuitBreaker = 1; //DC-DC模块默认接入
m_targetDisBreakerState.bus1Breaker.bowHighVoltageDistributionBoxCircuitBreaker = 1; //艏部高压母线断路器默认接入
m_targetDisBreakerState.bus1Breaker.sternFTDevice45CircuitBreaker = 0; // sternFTDevice45CircuitBreaker默认接入
+30 -17
View File
@@ -65,18 +65,30 @@ class PowerManger : public AppCastingMOOSApp
bool buildReport();
void registerVariables();
protected: // app function
// bool _ListenCB();
bool updatePmState();
protected:
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 buildPowerSysReport();
bool updatePmState();
bool updateConnectState();
protected: // app variables
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:
//系统运行状态
unsigned char workCondition;
unsigned char currentMod;
@@ -85,9 +97,8 @@ class PowerManger : public AppCastingMOOSApp
vector<unsigned int> faultCode;
unsigned int soc;
unsigned int sustainableTime;
bool m_connected;
pmState m_pmCurrentState; //能源系统的当前状态
//配电系统状态
//断路器状态
disSysBreakerList m_currentDisBreakerState;
disSysBreakerList m_targetDisBreakerState;
@@ -98,11 +109,18 @@ class PowerManger : public AppCastingMOOSApp
msg_disHighBVolBusFbMsg m_disHighBVolBusCurrentState;
msg_disLowBusFbMsg m_disLowBusState;
//操作函数
//下位机操作函数
void disSysOption();
char getBreakerOption(char cmd);
void initTargetDisBreakerState();
void ccuOption();
//上位机操作函数
bool buildPowerSysReport();
private:
udpComm m_udpComm;
XPCUdpSocket *m_pCcuUdpComm;
@@ -113,16 +131,12 @@ class PowerManger : public AppCastingMOOSApp
unsigned int m_nSendBufferSizeKB;
double m_current_time;
unsigned int m_faultNum;
unsigned int m_ticker;
//数据库
SQLiteDB* m_db;
bool writeDataToDB();
// CMOOSThread m_SendThread;
private: //
bool inline saveStringToJsonFile(const string &strJson, const string &strFileName);
public: //状态实现
public:
//===========================状态机实现==============================================
void printState(FSM_State st, string desp);
bool FSM_RUN_IS_OK = true;
@@ -131,7 +145,6 @@ class PowerManger : public AppCastingMOOSApp
string m_pmEvent;
FSM m_fsm;
//系统状态定义
//初始化状态
FSM_State m_Inite_s1;
static int handleInite_s1(FSM_State &fsm, void* pData);
+6 -5
View File
@@ -40,11 +40,12 @@ typedef struct {
typedef struct {
unsigned short year;
unsigned short month;
unsigned short day;
unsigned short hour;
unsigned short minute;
unsigned short second;
unsigned char month;
unsigned char day;
unsigned char hour;
unsigned char minute;
unsigned char second;
unsigned char milliseconds; //每10毫秒
} m_date;
// 复合管控器操作指令
typedef struct {
+38 -3
View File
@@ -2,7 +2,10 @@
#include <iostream>
#include <memory>
udpComm::udpComm() {}
udpComm::udpComm()
{
}
udpComm::~udpComm() {}
@@ -153,7 +156,8 @@ bool udpComm::pushMsgToQueue(unsigned char *m)
switch (id)
{
case CCU_STATE_FB_ID: //CCU_STATE_FB 0x0051
{ msg_CcuStateFbMsg s;
{
msg_CcuStateFbMsg s;
if(getMsgFromBuff((char *)Buff,s))
{
if(m_qReceiveCcuStateBuffer.size()<QUENUE_SIZE)
@@ -169,7 +173,6 @@ bool udpComm::pushMsgToQueue(unsigned char *m)
}
else
{
//sError.push_back();
return false;
}
break;
@@ -480,6 +483,7 @@ bool udpComm::sendCcuColCmd(const ccuColCmd cmd)
msg->header.start1 = CCU_UDPMSG_START1;
msg->header.start2 = CCU_UDPMSG_START2;
msg->header.length = sizeof(ccuColCmd);
getDate(msg->date.year,msg->date.month,msg->date.day,msg->date.hour,msg->date.minute,msg->date.second,msg->date.milliseconds);
msg->cmd = cmd;
msgBuf = reinterpret_cast<unsigned char *>(msg);
cc = calculateChecksum(msgBuf,sizeof(msg_CcuColCmdMsg));
@@ -519,3 +523,34 @@ bool udpComm::sendCcuSetParmCmd(const ccuSetParmCmd cmd)
delete msg;
return true;
}
bool udpComm::getDate(unsigned short &year, unsigned char &month, unsigned char &day, unsigned char &hour, unsigned char &minute, unsigned char &second, unsigned char &milliseconds) {
// 获取当前时间
std::time_t now = std::time(nullptr);
if (now == -1) {
return false; // 获取时间失败
}
// 转换为本地时间
std::tm *localTime = std::localtime(&now);
if (!localTime) {
return false; // 转换失败
}
// 获取年、月、日、时、分、秒
year = localTime->tm_year + 1900; // tm_year 是从 1900 年开始的
month = localTime->tm_mon + 1; // tm_mon 是从 0 开始的,0 对应 1 月
day = localTime->tm_mday;
hour = localTime->tm_hour;
minute = localTime->tm_min;
second = localTime->tm_sec;
// 获取毫秒,使用 std::chrono
auto now_ms = std::chrono::system_clock::now();
auto duration = now_ms.time_since_epoch();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000; // 取毫秒部分
ms = ms/10;
milliseconds = static_cast<unsigned char>(ms);
return true;
}
+4 -5
View File
@@ -9,7 +9,8 @@
#include <string.h>
#include "MOOS/libMOOS/Utils/MOOSUtilityFunctions.h"
#include "MOOS/libMOOS/Comms/XPCUdpSocket.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <utility> // for std::pair
@@ -17,8 +18,6 @@ using namespace std;
#define QUENUE_SIZE 100
class udpComm {
private:
/* data */
public:
udpComm();
~udpComm();
@@ -26,7 +25,6 @@ public:
string ccuHost;
long int ccuPort;
bool pushMsgToQueue(unsigned char *m);
bool clearMsgQueue();
@@ -60,8 +58,9 @@ public:
Checksum calculateChecksum(const unsigned char* data, int size);
Checksum calculateChecksum(const unsigned char* data, int dataSize, int checkSize);
bool getDate(unsigned short &year, unsigned char &month, unsigned char &day, unsigned char &hour, unsigned char &minute, unsigned char &second, unsigned char &milliseconds);
std::vector<std::string> sError;
std::pair<int, std::string> getError();
// std::queue<msgWithTime> m_qReceiveCcuStateBuffer;
std::queue<std::pair<double,msg_CcuStateFbMsg>> m_qReceiveCcuStateBuffer;
+18
View File
@@ -17,6 +17,24 @@ find_package(Catch2 REQUIRED)
# 添加测试可执行文件
add_executable(SQLiteDBTest SQLiteDBTest.cpp)
# 查找 udpComm 项目库
find_library(UDPCOMM_LIB NAMES UDPComm PATHS /home/zjk/project/H100/H100PowerManger/lib)
if(NOT UDPCOMM_LIB)
message(FATAL_ERROR "udpComm library not found!")
endif()
# 添加 udpComm 测试可执行文件
add_executable(udpCommTest udpCommTest.cpp)
# 链接库文件(注意顺序:先项目库后系统库)
target_link_libraries(udpCommTest
${UDPCOMM_LIB} # 项目udpComm库
${MOOS_LIBRARIES}
Catch2::Catch2WithMain # 测试框架
)
# 链接库文件(注意顺序:先项目库后系统库)
target_link_libraries(SQLiteDBTest
${SQLiteDB_LIB} # 项目SQLiteDB库
+1 -1
View File
@@ -4,7 +4,7 @@ class msg_CcuStateFbMsg:
def __init__(self):
self.start1 = 0x20
self.start2 = 0x20
self.id = 0x0051
self.id = 0x0041
self.length = 0x0000
self.currentWorkCondition = 0x00
self.fcuState = 80
+37
View File
@@ -0,0 +1,37 @@
#include "../src/pPowerManger/udpComm.h"
#include "catch2/catch.hpp"
#include <iostream>
TEST_CASE("udpComm getDate function test", "[udpComm]") {
udpComm comm;
unsigned short year;
unsigned char month, day, hour, minute, second, milliseconds;
bool result = comm.getDate(year, month, day, hour, minute, second, milliseconds);
// 输出日期和时间信息
std::cout << "Year: " << year << std::endl;
std::cout << "Month: " << static_cast<int>(month) << std::endl;
std::cout << "Day: " << static_cast<int>(day) << std::endl;
std::cout << "Hour: " << static_cast<int>(hour) << std::endl;
std::cout << "Minute: " << static_cast<int>(minute) << std::endl;
std::cout << "Second: " << static_cast<int>(second) << std::endl;
std::cout << "Milliseconds: " << static_cast<int>(milliseconds) << std::endl;
REQUIRE(result == true);
REQUIRE(year >= 2023);
REQUIRE(year <= 2030);
REQUIRE(month >= 1);
REQUIRE(month <= 12);
REQUIRE(day >= 1);
REQUIRE(day <= 31);
REQUIRE(hour >= 0);
REQUIRE(hour <= 23);
REQUIRE(minute >= 0);
REQUIRE(minute <= 59);
REQUIRE(second >= 0);
REQUIRE(second <= 59);
REQUIRE(milliseconds >= 0);
REQUIRE(milliseconds <= 999);
}