Files
ximeaAirborneSystem/Source_Files/udpserver.cpp

318 lines
11 KiB
C++
Raw Normal View History

#include "Header_Files/udpserver.h"
//using namespace sbgtc;
UdpServer::UdpServer()
{
m_udpSocket = new QUdpSocket(this);
m_udpSocket->bind(45454, QUdpSocket::ShareAddress);
connect(m_udpSocket, SIGNAL(readyRead()),this, SLOT(processPendingDatagrams()));
m_RecordSbgThread=new QThread();
m_sbgRecorder=new sbgtc::SbgRecorder();
m_sbgRecorder->moveToThread(m_RecordSbgThread);
m_RecordSbgThread->start();
m_RecordThread=new QThread();
m_imager=new XimeaImager();
m_imager->moveToThread(m_RecordThread);
m_RecordThread->start(QThread::HighestPriority);
m_CopyFileThread=new QThread();
m_copyFile=new FileOperation();
m_copyFile->moveToThread(m_CopyFileThread);
m_CopyFileThread->start();
//系统采集步骤1:打开sbg串口并采集数据打开光谱仪
connect(this, SIGNAL(systemStart()),m_sbgRecorder, SLOT(startRecordSbg()));
connect(this, SIGNAL(systemStart()),m_imager, SLOT(openImger()));
connect(this, SIGNAL(systemStop()),m_sbgRecorder, SLOT(closeSerialPort()));//
connect(this, SIGNAL(systemStop()),m_imager, SLOT(closeImger()));
//系统采集步骤2:开始采集高光谱影像
connect(this, SIGNAL(startRecordHyperspectralSignal()),m_sbgRecorder, SLOT(startRecordHyperspectral()));
connect(m_sbgRecorder, SIGNAL(sbgReady(double,QString)),m_imager, SLOT(startRecord(double,QString)));
connect(this, SIGNAL(recordXimeaOnlySignal(double,QString)),m_imager, SLOT(startRecord(double,QString)));
//系统采集步骤3:停止采集
connect(m_imager, SIGNAL(recordFinished()),this, SLOT(onRecordFinished()));
//系统采集步骤4:拷贝文件
connect(this, SIGNAL(startCopyFileSignal()),m_copyFile, SLOT(copyFile()));
connect(this, SIGNAL(startDeleteFileSignal()),m_copyFile, SLOT(deleteFile()));
//系统采集步骤5:进程间通讯
connect(m_sbgRecorder, SIGNAL(serialPortStatus(int)),this, SLOT(sendSerialPortStatus(int)));
connect(m_sbgRecorder, SIGNAL(sbgSolutionModeSignal(int)),this, SLOT(sendSbgSolutionModeState(int)));
connect(m_sbgRecorder, SIGNAL(sbgAccuracySignal(int,int)),this, SLOT(sendSbgAccuracyState(int,int)));
connect(m_imager, SIGNAL(ximeaImageStatus(int)),this, SLOT(sendXimeaImageStatus(int)));
connect(m_copyFile, SIGNAL(copyFileStatus(int)),this, SLOT(sendCopyFileStatus(int)));
//当软件不正常关闭并且重启后通知其他psdk程序
m_clientIpAddress=QHostAddress(QHostAddress::LocalHost);
sendSerialPortStatus(0);
sendXimeaImageStatus(0);
sendCopyFileStatus(0);
std::cout<<"UdpServer::UdpServer--------:System ready!"<<std::endl;
}
void UdpServer::processPendingDatagrams()
{
using namespace std;
while (m_udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(m_udpSocket->pendingDatagramSize());
m_udpSocket->readDatagram(datagram.data(), datagram.size(),&m_clientIpAddress, &m_clientPort);
printf("接收数据字节数: %d.\n",datagram.size());
QList<QByteArray> datagramList=datagram.split(',');
printf("有多少个list: %d.\n",datagramList.size());
QString instruction=datagramList[0].data();// QByteArray转QString方法1
switch (instruction.toInt())
{
case 1://启动系统: 打开sbg串口并采集数据打开光谱仪
{
std::cout<<"1代表启动系统!"<<std::endl;
system("sudo gpio write 10 1");
sleep(4);//睡眠4秒等待电脑打开usb电源以便给相机供电
emit systemStart();
break;
}
case 2://关闭系统:关闭相机和sbg串口,关闭软件
{
std::cout<<"2代表关闭系统!"<<std::endl;
if(m_sbgRecorder->getSbgState()>=1)
{
m_sbgRecorder->stopRecordSbg();
}
if(m_imager->getImagerState()>=101)
{
m_imager->stopRecord();
}
emit systemStop();
sleep(4);//睡眠4秒等待
system("sudo gpio write 10 0");
//QCoreApplication::quit();
break;
}
case 3://系统开始采集高光谱影像
{
//emit startRecordHyperspectralSignal();//真实的影像开始采集通过惯导中的信号(sbgReady)触发
m_sbgRecorder->startRecordHyperspectral();
// if(m_sbgRecorder->getSbgState()==2)//开始采集前还需要判断相机的状态??????????????????????????????????????????
// {
// }
// else if(m_sbgRecorder->getSbgState()==3)
// {
// std::cout<<"系统已经开始采集!"<<std::endl;
// }
break;
}
case 4://系统停止采集高光谱影像
{
std::cout<<"4代表系统停止采集高光谱影像!"<<std::endl;
if(m_imager->getImagerState()>=101 && m_imager->getImagerState()<=104)
{
m_imager->stopRecord();
}
break;
}
case 5://
{
if(m_imager->getImagerState()>=101 && m_imager->getImagerState()<=103)
{
std::cout<<"5代表设置帧率!"<<std::endl;
m_imager->setFramerate(datagramList[1].toFloat());
}
break;
}
case 6://
{
if(m_imager->getImagerState()>=101 && m_imager->getImagerState()<=103)
{
std::cout<<"6代表自动曝光!"<<std::endl;
m_imager->autoExposure();
}
break;
}
case 7:
{
float time = datagramList[1].toFloat();//ms
m_imager->wrapSetExposureTime(time*1000);
std::cout<<"7手动设置曝光时间为" << time <<std::endl;
break;
}
case 8:
{
2022-06-13 15:12:17 +08:00
;
}
case 9:
{
std::cout<<"9代表仅采集影像!"<<std::endl;
if(datagramList[1].toInt()==1)
{
QString xx = getFileNameBaseOnTime();
xx = xx + "testImage";
emit recordXimeaOnlySignal(1000.0,xx);
// emit recordXimeaOnlySignal(1000.0,"testImage");
}
else if(datagramList[1].toInt()==0)
{
m_imager->stopRecord();
}
break;
}
default:
std::cout<<">=9没有意义!"<<std::endl;
break;
}
}
}
double UdpServer::getTimeDifferenceBetweenSystemAndSbg(double secondSbg)
{
// time_t timer;//time_t就是long int 类型
// struct tm *tblock;
// timer = time(NULL);//返回秒数(精度为秒)从1970-1-1,00:00:00 可以当成整型输出或用于其它函数
// tblock = localtime(&timer);
// printf("Local time is: %s\n", asctime(tblock));
//https://blog.csdn.net/FUN6367/article/details/89787566
struct timespec systemTime;
clock_gettime(CLOCK_REALTIME,&systemTime);
tm systemTime_rili;
localtime_r(&systemTime.tv_sec, &systemTime_rili);
// std::cout<<"systemTime_rili--年: "<<systemTime_rili.tm_year+1900<<std::endl;
// std::cout<<"systemTime_rili--月: "<<systemTime_rili.tm_mon+1<<std::endl;
// std::cout<<"systemTime_rili--日: "<<systemTime_rili.tm_mday<<std::endl;
// std::cout<<"systemTime_rili--时: "<<systemTime_rili.tm_hour<<std::endl;
// std::cout<<"systemTime_rili--分: "<<systemTime_rili.tm_min<<std::endl;
// std::cout<<"systemTime_rili--秒: "<<systemTime_rili.tm_sec<<std::endl;
// printf("Local time is: %s\n", asctime(&systemTime_rili));
double secondSystem=(systemTime_rili.tm_mday-1)*24*60*60+systemTime_rili.tm_hour*60*60+systemTime_rili.tm_min*60+systemTime_rili.tm_sec;
double nanosecondSystem=secondSystem+static_cast<double>(systemTime.tv_nsec)/1000000000;
printf("UdpServer::getTimeDifferenceBetweenSystemAndSbg------%f\n", nanosecondSystem-secondSbg);
return nanosecondSystem-secondSbg;
}
void UdpServer::sender(int status)
{
QByteArray datagram2send;
datagram2send.operator =(QString::number(status).toStdString().c_str());
m_udpSocket->writeDatagram(datagram2send.data(),datagram2send.size(),m_clientIpAddress, m_clientPort+1);
}
void UdpServer::sendSerialPortStatus(int serialPortStatus)
{
// std::cout<<"UdpServer::sendSerialPortStatus---------------------:"<< serialPortStatus <<std::endl;
// std::cout<<"UdpServer::sendSerialPortStatus---------------------:"<< m_clientIpAddress.AnyIPv4 <<std::endl;
QByteArray datagram2send;
QString status = "sbg," + QString::number(serialPortStatus);
datagram2send.operator =(status.toStdString().c_str());
m_udpSocket->writeDatagram(datagram2send.data(),datagram2send.size(),m_clientIpAddress, 45455);
}
void UdpServer::sendSbgSolutionModeState(int SolutionMode)
{
// std::cout<<"UdpServer::sendSbgSolutionModeState---------------------:"<< SolutionMode <<std::endl;
QByteArray datagram2send;
QString status = "SolutionMode," + QString::number(SolutionMode);
datagram2send.operator =(status.toStdString().c_str());
m_udpSocket->writeDatagram(datagram2send.data(),datagram2send.size(),m_clientIpAddress, 45455);
}
void UdpServer::sendSbgAccuracyState(int Accuracy,int SatelliteCounter)
{
// std::cout<<"UdpServer::sendSbgAccuracyState---------------------:"<< Accuracy <<std::endl;
QByteArray datagram2send;
QString status = "Accuracy," + QString::number(Accuracy) + "," + QString::number(SatelliteCounter);
datagram2send.operator =(status.toStdString().c_str());
m_udpSocket->writeDatagram(datagram2send.data(),datagram2send.size(),m_clientIpAddress, 45455);
}
void UdpServer::sendXimeaImageStatus(int ximeaImageStatus)
{
// std::cout<<"UdpServer::sendXimeaImageStatus---------------------:"<< ximeaImageStatus <<std::endl;
QByteArray datagram2send;
QString status = "ximea," + QString::number(ximeaImageStatus);
datagram2send.operator =(status.toStdString().c_str());
m_udpSocket->writeDatagram(datagram2send.data(),datagram2send.size(),m_clientIpAddress, 45455);
}
void UdpServer::sendCopyFileStatus(int fileStatus)
{
// std::cout<<"UdpServer::sendCopyFileStatus---------------------:"<< fileStatus <<std::endl;
QByteArray datagram2send;
QString status = "file," + QString::number(fileStatus);
datagram2send.operator =(status.toStdString().c_str());
m_udpSocket->writeDatagram(datagram2send.data(),datagram2send.size(),m_clientIpAddress, 45455);
}
void UdpServer::onRecordFinished()
{
std::cout<<"UdpServer::onRecordFinished----------------:影像停止采集"<<std::endl;
}