增加了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
+58 -13
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();
//进行配电系统操作
disSysOption();
//3. 输出报告
buildPowerSysReport();
//3. 进行操作 每秒执行一次
if (m_ticker++ % 5 == 0)
{
//a. 配电系统操作
disSysOption();
//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默认接入
+31 -18
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));
@@ -517,5 +521,36 @@ bool udpComm::sendCcuSetParmCmd(const ccuSetParmCmd cmd)
std::cerr << e.what() << '\n';
}
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;