优化气体传感器数据解析和消息传递

- 使用union解析CO2/H2O数据,解决大小端问题
- 添加LINECOMMEND/LINEDATASHOW消息类型区分
- 添加WorkingState/SavingDate原子变量
- 修正气体传感器部分逻辑
This commit is contained in:
xin
2026-03-04 10:22:51 +08:00
parent 82608bfa0a
commit e7cd1e93a6
5 changed files with 172 additions and 63 deletions

View File

@ -2,13 +2,18 @@
#include "unistd.h"
#include <cmath>
#include "hal_uart.h"
#define LINECOMMEND 0
#define LINEDATASHOW 1
std::atomic<int> WorkingState;
std::atomic<int> SavingDate;
CMainAcqThread::CMainAcqThread(QObject* parent /*= nullptr*/)
{
m_iFlagCaptureStatus = 0;
iFlagIsPathGenerated = false;
m_clsCapTimer.setTimerType(Qt::PreciseTimer);
WorkingState = 0;
SavingDate = 0;
#ifdef ZZ_FLAG_TEST
//connect(&m_clsCapTimer, &QTimer::timeout, this, &CMainAcqThread::OnTestTimer);
@ -52,15 +57,16 @@ int CMainAcqThread::StartUp()
m_ctrlVehicle.StartupPSDK_M300RTK();
QString qstrMessage = "Initializing sensors,Please wait...";
emit Signal_UpdateVehicleMessage(qstrMessage);
emit Signal_UpdateVehicleMessage(qstrMessage,LINECOMMEND);
int iRes = m_ctrlGasSensor.Initialize(m_struSensorPort.qstrGasSensorPort.toStdString());
if (iRes!=0)
{
qstrMessage.clear();
qstrMessage.append("Fatal Error!!! Gas sensor not connected.Please check.");
//qstrMessage = "Fatal Error!!! Gas sensor not connected.Please check.";
emit Signal_UpdateVehicleMessage(qstrMessage);
qstrMessage = "Fatal Error!!! Gas sensor not connected.Please check.";
emit Signal_UpdateVehicleMessage(qstrMessage,LINECOMMEND);
qDebug() << qstrMessage;
//return 1;
}
@ -69,28 +75,30 @@ int CMainAcqThread::StartUp()
if (iRes != 0)
{
qstrMessage.append("Fatal Error!!! Wind sensor not connected.Please check.");
//qstrMessage = "Fatal Error!!! Wind sensor not connected.Please check.";
emit Signal_UpdateVehicleMessage(qstrMessage);
qstrMessage = "Fatal Error!!! Wind sensor not connected.Please check.";
emit Signal_UpdateVehicleMessage(qstrMessage,LINECOMMEND);
qDebug() << qstrMessage;
//return 2;
}
qstrMessage.append("System ready.Waiting for signals");
//qstrMessage = "System ready.Waiting for signals";
emit Signal_UpdateVehicleMessage(qstrMessage);
// qstrMessage = "System ready.Waiting for signals";
// qstrMessage="123456789123456789123456789123456789123456789123456789123456789";
emit Signal_UpdateVehicleMessage(qstrMessage,LINECOMMEND);
return 0;
}
int CMainAcqThread::SetupMessagePipe()
{
connect(&m_ctrlVehicle, &VehicleController::Signal_StartCapture, this, &CMainAcqThread::Slot_StartCapture);
connect(&m_ctrlVehicle.m_clsWidget,&ZZ_Widget_M300RTK::SendCommand,this,&CMainAcqThread::GetCommand);
connect(&m_ctrlVehicle.m_clsWidget,&ZZ_Widget_M300RTK::SendCommand,this,&CMainAcqThread::GetCommand,Qt::QueuedConnection);
connect(this, &CMainAcqThread::SendCommand, &m_ctrlVehicle.m_clsWidget, &ZZ_Widget_M300RTK::BackCommand);
connect(&m_ctrlVehicle, &VehicleController::Signal_StopCapture, this, &CMainAcqThread::Slot_StopCapture);
connect(this, &CMainAcqThread::Signal_UpdateVehicleMessage, &m_ctrlVehicle, &VehicleController::Signal_UpdateVehicleMessage);
connect(&m_clsCapTimer, &QTimer::timeout, this, &CMainAcqThread::OnTimerCapture);
m_clsCapTimer.start(1000);
return 0;
}
@ -146,10 +154,21 @@ int CMainAcqThread::FormFixedWindData()
m_fTempFixedWindVecY = m_fTempFixedWindVecY - m_struM300RTKDataFrame.stVelocity.y;
m_fTempFixedWindVecZ = m_fTempFixedWindVecZ - m_struM300RTKDataFrame.stVelocity.z;
double dDotProduct = m_fTempFixedWindVecX; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
double dVectorLength = sqrt(m_fTempFixedWindVecX * m_fTempFixedWindVecX + m_fTempFixedWindVecY * m_fTempFixedWindVecY + m_fTempFixedWindVecZ * m_fTempFixedWindVecZ); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>
double dAngleInRadians = acos(dDotProduct / dVectorLength); // <20><><EFBFBD><EFBFBD>нǵĻ<C7B5><C4BB><EFBFBD>ֵ
double angleInDegrees = dAngleInRadians * (180.0 / M_PI); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
double dVectorLength = sqrt(m_fTempFixedWindVecX * m_fTempFixedWindVecX + m_fTempFixedWindVecY * m_fTempFixedWindVecY + m_fTempFixedWindVecZ * m_fTempFixedWindVecZ);
if (dVectorLength < 0.0001)
{
m_struUASDataFrame.fFixedWindDirection = 0;
m_struUASDataFrame.fFixedWindSpeed = 0;
return 0;
}
double dDotProduct = m_fTempFixedWindVecX;
double dCosAngle = dDotProduct / dVectorLength;
if (dCosAngle > 1.0) dCosAngle = 1.0;
if (dCosAngle < -1.0) dCosAngle = -1.0;
double dAngleInRadians = acos(dCosAngle);
double angleInDegrees = dAngleInRadians * (180.0 / M_PI);
m_struUASDataFrame.fFixedWindDirection = angleInDegrees;
m_struUASDataFrame.fFixedWindSpeed = dVectorLength;
@ -188,7 +207,7 @@ void CMainAcqThread::OnTestTimer()
int CMainAcqThread::GetData()
{
unsigned long ulCO2=0, ulH2O=0;
double ulCO2=0, ulH2O=0;
float fGasTemp=-1,fPP=-1,fPB=-1;
m_ctrlGasSensor.GetMeasuredData(ulCO2, ulH2O, fGasTemp, fPP, fPB);
m_struGSDataFrame.ulCO2 = ulCO2;
@ -207,10 +226,14 @@ int CMainAcqThread::GetData()
m_ctrlVehicle.GetOneDataFrame(m_struM300RTKDataFrame);//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
// #define ZZ_FLAG_TEST
int CMainAcqThread::OnTimerCapture()
{
if (m_iFlagCaptureStatus >1)
{
return 0;
}
#ifdef ZZ_FLAG_TEST
qDebug() << "OnTimerCapture entering time:" << QTime::currentTime();
#endif
@ -227,14 +250,23 @@ int CMainAcqThread::OnTimerCapture()
QString qstrMessage;
qstrMessage = QString("CO2: %1ppm, H20: %2ppm\r\nFixed Wind Speed: %3m/s\r\nFixed Wind Direction: %4deg").arg(m_struGSDataFrame.ulCO2).arg(m_struGSDataFrame.ulH2O)
.arg(m_struUASDataFrame.fFixedWindSpeed).arg(m_struUASDataFrame.fFixedWindDirection);
emit Signal_UpdateVehicleMessage(qstrMessage);
emit Signal_UpdateVehicleMessage("CO2:"+QString::number(m_struGSDataFrame.ulCO2,'f',1)+"\t H2O:"+QString::number(m_struGSDataFrame.ulH2O,'f',1),LINEDATASHOW);
emit Signal_UpdateVehicleMessage("WS:"+QString::number(m_struUASDataFrame.fFixedWindSpeed,'f',2)+"\t D:"+QString::number(m_struUASDataFrame.fFixedWindDirection,'f',2)+"",LINEDATASHOW+1);
// emit Signal_UpdateVehicleMessage("PP:"+QString::number(m_struGSDataFrame.fPP,'f',2)+"\t PB:"+QString::number(m_struGSDataFrame.fPB,'f',2)+"",LINEDATASHOW+2);
emit Signal_UpdateVehicleMessage("T:"+QString::number(m_struGSDataFrame.fTemp,'f',2)+"\t PB:"+QString::number(m_struGSDataFrame.fPB,'f',2),LINEDATASHOW+2);
// emit Signal_UpdateVehicleMessage(qstrMessage);
//////////////////////////////////////////////////////////////////////////
#ifdef ZZ_FLAG_TEST
qDebug() << m_struM300RTKDataFrame.stGPSPosition.x << m_struM300RTKDataFrame.stGPSPosition.y << m_struM300RTKDataFrame.stGPSPosition.z
<< m_struM300RTKDataFrame.stVelocity.x << m_struM300RTKDataFrame.stVelocity.y << m_struM300RTKDataFrame.stVelocity.z;
#endif
if (SavingDate==1)
{
m_ctrlData.WriteData(m_struM300RTKDataFrame, m_struGSDataFrame, m_struUASDataFrame);
}
m_ctrlData.WriteData(m_struM300RTKDataFrame, m_struGSDataFrame, m_struUASDataFrame);
#ifdef ZZ_FLAG_TEST
@ -258,12 +290,13 @@ int CMainAcqThread::Slot_StartCapture()
m_ctrlData.GenerateFile();
m_clsCapTimer.start(1000);
// m_clsCapTimer.start(1000);
#ifdef ZZ_FLAG_TEST
qDebug() << "CMainAcqThread Test Cap Started";
#endif // ZZ_FLAG_TEST
m_iFlagCaptureStatus = 1;
SavingDate = 1;
return 0;
}
@ -275,7 +308,8 @@ int CMainAcqThread::Slot_StopCapture()
iFlagIsPathGenerated = false;
m_clsCapTimer.stop();
SavingDate=0;
//m_clsCapTimer.stop();
m_ctrlData.CloseData();
#ifdef ZZ_FLAG_TEST
@ -283,7 +317,7 @@ int CMainAcqThread::Slot_StopCapture()
#endif // ZZ_FLAG_TEST
QString qstrMessage = QString("Capture Stopped.");
emit Signal_UpdateVehicleMessage(qstrMessage);
emit Signal_UpdateVehicleMessage(qstrMessage,LINECOMMEND);
return 0;
}
@ -296,20 +330,26 @@ void CMainAcqThread::GetCommand(QString Worker, QString Command) {
int waitetime=10;
qDebug()<<"GetCommand Worker:"<<Worker<<" Command:"<<Command;
if (Worker=="WDA") {
m_iFlagCaptureStatus=WADCALI;
QThread::msleep(1100);
//#分割字符comman
QStringList arc=Command.split("#");
if (arc[0]=="StartCalibrate") {
if (arc.length()<2) {
emit SendCommand("WDA","Finish");
m_iFlagCaptureStatus=NORMAL;
return;
}
m_ctrlWindSensor.CalibrateWDA(arc[1].toDouble());
emit SendCommand("WDA","Finish");
m_iFlagCaptureStatus=NORMAL;
}
}
else if (Worker == "GAS") {
m_iFlagCaptureStatus = GASGALI;
QStringList arc = Command.split("#");
qDebug()<<"arc is"<<arc;
if (arc[0] == "ZeroCalibrate") {
@ -349,7 +389,7 @@ void CMainAcqThread::GetCommand(QString Worker, QString Command) {
if (arc.size()<3) {
emit SendCommand("GAS", "Finish");
m_iFlagCaptureStatus=NORMAL;
return;
}
char cChannel=0;
@ -360,7 +400,7 @@ void CMainAcqThread::GetCommand(QString Worker, QString Command) {
}
else {
emit SendCommand("GAS", "Finish");
m_iFlagCaptureStatus=NORMAL;
return;
}
unsigned int uiPPM=arc[2].toUInt();
@ -376,6 +416,6 @@ void CMainAcqThread::GetCommand(QString Worker, QString Command) {
}
m_iFlagCaptureStatus=NORMAL;
}
}