123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- #include <thread>
- #include <iostream>
- #include <QUdpSocket>
- //#include <QNetworkDatagram>
- #include <iostream>
- #include <QMutex>
- #include <QDateTime>
- //#include <pcl/conversions.h>
- #include <pcl/point_cloud.h>
- #include <pcl/point_types.h>
- #include "modulecomm.h"
- #include "lidar_driver_leishen16.h"
- #include "lidar_leishen16_rawdata.h"
- #include "ivexit.h"
- #include "ivfault.h"
- #include "ivlog.h"
- #ifdef VV7_1
- int vv7;
- #endif
- #define Lidar_Pi 3.1415926535897932384626433832795
- #define Lidar32 (unsigned long)3405883584//192.168.1.203
- #define Lidar_roll_ang 90*Lidar_Pi/180.0
- static std::thread * g_pleishen16Thread;
- static std::thread * g_pleishen16ProcThread;
- //float gV_theta[16] = {-15,1,-13,3,-11,5,-9,7,-7,9,-5,11,-3,13,-1,15}; //16 Angles
- static float gV_theta[16] = {-15,1,-13,3,-11,5,-9,7,-7,9,-5,11,-3,13,-1,15}; //16 Angles
- static float gV_theta_cos[16],gV_theta_sin[16];
- //static void * g_leishen16_raw;
- static void * g_lidar_pc;
- static bool g_bleishen16_run = false;
- static bool g_bleishen16_running = false;
- static bool g_bleishen16_Proc_running = false;
- static int g_seq = 0;
- static unsigned short glidar_port=2368;
- extern char gstr_memname[256];
- extern char gstr_rollang[256];
- extern char gstr_inclinationang_yaxis[256]; //from y axis
- extern char gstr_inclinationang_xaxis[256]; //from x axis
- //char gstr_hostip[256];
- extern char gstr_port[256];
- extern char gstr_yaml[256];
- extern iv::Ivfault * gIvfault;
- extern iv::Ivlog * gIvlog;
- /**
- * @brief The leishen16_Buf class
- * Use for Lidar UDP DATA Save
- */
- class leishen16_Buf
- {
- private:
- char * mstrdata;
- int mnSize; //Data SizeUse
- QMutex mMutex;
- int mIndex;
- public:
- leishen16_Buf()
- {
- mstrdata = new char[BK32_DATA_BUFSIZE];
- mIndex = 0;
- mnSize = 0;
- }
- ~leishen16_Buf()
- {
- delete mstrdata;
- }
- void WriteData(const char * strdata,const int nSize)
- {
- mMutex.lock();
- memcpy(mstrdata,strdata,nSize);
- mnSize = nSize;
- mIndex++;
- mMutex.unlock();
- }
- int ReadData(char * strdata,const int nRead,int * pnIndex)
- {
- int nRtn = 0;
- if(mnSize <= 0)return 0;
- mMutex.lock();
- nRtn = mnSize;
- if(nRtn >nRead)
- {
- nRtn = nRead;
- std::cout<<"lidar_leishen16 leishen16_Buf ReadData data nRead = "<<nRead<<" is small"<<std::endl;
- }
- memcpy(strdata,mstrdata,nRtn);
- mnSize = 0;
- if(pnIndex != 0)*pnIndex = mIndex;
- mMutex.unlock();
- return nRtn;
- }
- };
- static leishen16_Buf * g_leishen16_Buf;
- static char * g_RawData_Buf; //Buffer UDP Data
- static int gnRawPos = 0;
- static float gAngle_Old = 0;
- static float gAngle_Total = 0;
- static unsigned short gold = 0;
- static int gnPac = 0;
- #include <QTime>
- static QTime gTime;
- /**
- * @brief processleishen16_Data
- * @param ba UDP Buffer
- * 1.UDP ByteArray Length is 1206.
- * 2.if Angle is More than 360. Tell Another thread process.
- */
- static void processleishen16_Data(QByteArray ba)
- {
- gnPac++;
- unsigned short * pAng;
- float fAng;
- char * pdata;
- pdata = ba.data();
- // std::cout<<"len is "<<ba.length()<<std::endl;
- if(ba.length() == 1206)
- {
- pAng = (unsigned short *)(pdata+2);
- fAng = *pAng;fAng = fAng*0.01;
- // std::cout<<"ang is "<<*pAng<<std::endl;
- if(fabs(fAng-gAngle_Old)>300)
- {
- gAngle_Total = gAngle_Total + fabs(fabs(fAng-gAngle_Old)-360);
- }
- else
- {
- gAngle_Total = gAngle_Total + fabs(fabs(fAng-gAngle_Old));
- }
- gAngle_Old = fAng;
- if(gAngle_Total > 360)
- {
- g_leishen16_Buf->WriteData(g_RawData_Buf,gnRawPos);
- lidar_leishen16_raw * p = new lidar_leishen16_raw();
- p->mnLen = gnRawPos;
- memcpy(p->mstrdata,g_RawData_Buf,gnRawPos);
- // iv::modulecomm::ModuleSendMsg(g_leishen16_raw,(char *)p,sizeof(lidar_leishen16_raw));
- delete p;
- memcpy(g_RawData_Buf,pdata,1206);
- gnRawPos = 1206;
- // std::cout<<"index = "<<gnPac<<" time ="<<gTime.elapsed()<<" a cycle"<<std::endl;
- gAngle_Total = 0;
- }
- else
- {
- if((gnRawPos+1206)<= BK32_DATA_BUFSIZE)
- {
- memcpy(g_RawData_Buf+gnRawPos,pdata,1206);
- gnRawPos= gnRawPos+1206;
- }
- else
- {
- std::cout<<"lidar_leishen16 processleishen16_Data data is very big gnRawPos = "<<gnRawPos<<std::endl;
- }
- }
- // std::cout<<*pAng<<std::endl;
- // gold = *pAng;
- if(gnRawPos == 0)
- {
- gAngle_Total = 0;
- gAngle_Old = *pAng;
- gAngle_Old = gAngle_Old*0.01;
- memcpy(g_RawData_Buf,pdata,1206);
- gnRawPos = gnRawPos+1206;
- }
- }
- else
- {
- std::cout<<"lidar_leishen16 processleishen16_Data receive data packet len is not 1206 "<<std::endl;
- }
- }
- /**
- * @brief leishen16_Func thread for receive udp socket
- * @param n
- */
- static void leishen16_Func(int n)
- {
- gTime.start();
- std::cout<<"Enter leishen16_Func."<<std::endl;
- QUdpSocket * udpSocket = new QUdpSocket( );
- udpSocket->bind(QHostAddress::Any, glidar_port);
- unsigned int ndatamisstime = 0;
- int nstate = 0;
- int nlaststate = 0;
- while(g_bleishen16_run)
- {
- if(udpSocket->hasPendingDatagrams())
- {
- ndatamisstime = 0;
- // std::cout<<"have data."<<std::endl;
- QByteArray datagram;
- datagram.resize(udpSocket->pendingDatagramSize());
- QHostAddress sender;
- quint16 senderPort;
- udpSocket->readDatagram(datagram.data(), datagram.size(),
- &sender, &senderPort);
- // processTheDatagram(datagram);
- // std::cout<<"have data."<<std::endl;
- // QNetworkDatagram datagram = udpSocket->receiveDatagram();
- processleishen16_Data(datagram);
- datagram.clear();
- }
- else
- {
- // std::cout<<"running."<<std::endl;
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- if(ndatamisstime < 1000000) ndatamisstime++;
- }
- if(ndatamisstime > 1000)
- {
- nstate = 1;
- }
- if(ndatamisstime > 60000)
- {
- nstate = 2;
- }
- if(ndatamisstime < 100)
- {
- nstate = 0;
- }
- if(nlaststate != nstate)
- {
- nlaststate = nstate;
- switch (nstate) {
- case 0:
- gIvfault->SetFaultState(0,0,"OK");
- gIvlog->info("received udp data.device is ok.");
- break;
- case 1:
- gIvfault->SetFaultState(1,1,"No data");
- gIvlog->warn("more than 1 second no data. warning.");
- break;
- case 2:
- gIvfault->SetFaultState(2,2,"No data,Please Check Device or setting.");
- gIvlog->error("more than 60 seconds no data. error. Please check device or setting.");
- break;
- default:
- break;
- }
- }
- }
- udpSocket->close();
- delete udpSocket;
- g_bleishen16_running = false;
- std::cout<<"leishen16_Func Exit."<<std::endl;
- }
- /**
- * @brief process_leishen16obs Make PointCloud And Share
- * @param strdata pointer to data
- * @param nLen data length
- */
- static void process_leishen16obs(char * strdata,int nLen)
- {
- double frollang = atof(gstr_rollang) *M_PI/180.0; //Roll Angle
- double finclinationang_xaxis = atof(gstr_inclinationang_xaxis)*M_PI/180.0; //Inclination from x axis
- double finclinationang_yaxis = atof(gstr_inclinationang_yaxis)*M_PI/180.0; //Inclination from y axis
- bool binclix = false;
- bool bincliy = false;
- if(finclinationang_xaxis != 0.0)binclix = true;
- if(finclinationang_yaxis != 0.0)bincliy = true;
- double cos_finclinationang_xaxis = cos(finclinationang_xaxis);
- double sin_finclinationang_xaxis = sin(finclinationang_xaxis);
- double cos_finclinationang_yaxis = cos(finclinationang_yaxis);
- double sin_finclinationang_yaxis = sin(finclinationang_yaxis);
- QDateTime dt = QDateTime::currentDateTime();
- pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
- new pcl::PointCloud<pcl::PointXYZI>());
- point_cloud->header.frame_id = "velodyne";
- point_cloud->height = 1;
- point_cloud->header.stamp = dt.currentMSecsSinceEpoch();
- point_cloud->width = 0;
- point_cloud->header.seq =g_seq;
- g_seq++;
- unsigned char * pstr = (unsigned char *)strdata;
- // std::cout<<"enter obs."<<std::endl;
- // float w = 0.0036;
- float Ang = 0;
- float Range = 0;
- int bag = 0;
- int Group = 0;
- int pointi = 0;
- float wt = 0;
- int wt1 = 0;
- int buf1len = nLen/1206;
- unsigned short * pAng;
- double ang1,ang2;
- pAng = (unsigned short *)(strdata+2+0*100);
- ang1 = *pAng;ang1 = ang1/100.0;
- pAng = (unsigned short *)(strdata+2+1*100);
- ang2 = *pAng;ang2 = ang2/100.0;
- double angdiff = ang2 - ang1;
- if(angdiff<0)
- angdiff = angdiff + 360.0;
- angdiff = angdiff/2.0;
- for (bag = 0; bag < buf1len; bag++)
- {
- for (Group = 0; Group <= 11; Group++)
- {
- wt1 = ((pstr[bag*1206 +3 + Group * 100] *256) + pstr[bag*1206 + 2 + Group * 100]) ;
- wt = wt1/ 100.0;
- // std::cout<<"wt1 is "<<wt1<<std::endl;
- for (pointi = 0; pointi <16; pointi++)
- {
- Ang = (0 - wt) / 180.0 * Lidar_Pi;
- Range = ((pstr[bag*1206 + Group * 100 + 5 + 3 * pointi] << 8) + pstr[bag*1206+Group * 100 + 4 + 3 * pointi]);
- unsigned char intensity = pstr[bag*1206 + Group * 100 + 6 + 3 * pointi];
- Range=Range* 0.0025;
- pcl::PointXYZI point;
- point.x = Range* gV_theta_cos[pointi]*cos(Ang + frollang);
- point.y = Range* gV_theta_cos[pointi] *sin(Ang + frollang);
- point.z = Range* gV_theta_sin[pointi];
- point.intensity = intensity;
- if(binclix)
- {
- double y,z;
- y = point.y;z = point.z;
- point.y = y*cos_finclinationang_xaxis +z*sin_finclinationang_xaxis;
- point.z = z*cos_finclinationang_xaxis - y*sin_finclinationang_xaxis;
- }
- if(bincliy)
- {
- double z,x;
- z = point.z;x = point.x;
- point.z = z*cos_finclinationang_yaxis + x*sin_finclinationang_yaxis;
- point.x = x*cos_finclinationang_yaxis - z*sin_finclinationang_yaxis;
- }
- point_cloud->points.push_back(point);
- ++point_cloud->width;
- }
- wt = wt + 0.2; //扫描帧频10Hz时的水平角分辨率是0.2°,
- for (pointi = 0; pointi < 16; pointi++)
- {
- Ang = (0 - wt) / 180.0 * Lidar_Pi;
- Range = ((pstr[bag*1206 + Group * 100 + 53 + 3 * pointi] << 8) + pstr[bag*1206+Group * 100 + 52 + 3 * pointi]);
- unsigned char intensity = pstr[bag*1206 + Group * 100 + 54 + 3 * pointi];
- Range=Range* 0.0025;
- pcl::PointXYZI point;
- point.x = Range* gV_theta_cos[pointi] *cos(Ang + frollang);
- point.y = Range* gV_theta_cos[pointi] *sin(Ang + frollang);
- point.z = Range* gV_theta_sin[pointi] ;
- point.intensity = intensity;
- if(binclix)
- {
- double y,z;
- y = point.y;z = point.z;
- point.y = y*cos_finclinationang_xaxis +z*sin_finclinationang_xaxis;
- point.z = z*cos_finclinationang_xaxis - y*sin_finclinationang_xaxis;
- }
- if(bincliy)
- {
- double z,x;
- z = point.z;x = point.x;
- point.z = z*cos_finclinationang_yaxis + x*sin_finclinationang_yaxis;
- point.x = x*cos_finclinationang_yaxis - z*sin_finclinationang_yaxis;
- }
- point_cloud->points.push_back(point);
- ++point_cloud->width;
- }
- }
- }
- char * strOut = new char[4+sizeof(point_cloud->header.frame_id.size()) + 4+8+point_cloud->width * sizeof(pcl::PointXYZI)];
- int * pHeadSize = (int *)strOut;
- *pHeadSize = 4 + point_cloud->header.frame_id.size()+4+8;
- memcpy(strOut+4,point_cloud->header.frame_id.c_str(),point_cloud->header.frame_id.size());
- pcl::uint32_t * pu32 = (pcl::uint32_t *)(strOut+4+sizeof(point_cloud->header.frame_id.size()));
- *pu32 = point_cloud->header.seq;
- memcpy(strOut+4+sizeof(point_cloud->header.frame_id.size()) + 4,&point_cloud->header.stamp,8);
- pcl::PointXYZI * p;
- p = (pcl::PointXYZI *)(strOut +4+sizeof(point_cloud->header.frame_id.size()) + 4+8 );
- memcpy(p,point_cloud->points.data(),point_cloud->width * sizeof(pcl::PointXYZI));
- iv::modulecomm::ModuleSendMsg(g_lidar_pc,strOut,4+sizeof(point_cloud->header.frame_id.size()) + 4+8+point_cloud->width * sizeof(pcl::PointXYZI));
- delete strOut;
- // std::cout<<"point cloud width = "<<point_cloud->width<<" size = "<<point_cloud->size()<<std::endl;
- }
- /**
- * @brief leishen16_Proc_Func
- * thread for process data to PointCloud
- * @param n
- */
- static void leishen16_Proc_Func(int n)
- {
- std::cout<<"Enter leishen16_Proc_Func"<<std::endl;
- char * strdata = new char[BK32_DATA_BUFSIZE];
- int nIndex;
- int nRead;
- while(g_bleishen16_run)
- {
- if((nRead = g_leishen16_Buf->ReadData(strdata,BK32_DATA_BUFSIZE,&nIndex))>0)
- {
- //process data.
- process_leishen16obs(strdata,nRead);
- }
- else
- {
- // std::cout<<"running."<<std::endl;
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- }
- g_bleishen16_Proc_running = false;
- std::cout<<"Exit leishen16_Proc_Func"<<std::endl;
- }
- void exitfunc()
- {
- StopLidar_leishen16();
- }
- /**
- * @brief StartLidar_leishen16x Start
- * @return
- */
- int LIDAR_DRIVER_LEISHEN16SHARED_EXPORT StartLidar_leishen16()
- {
- iv::ivexit::RegIVExitCall(exitfunc);
- std::cout<<"Now Start leishen16 Listen."<<std::endl;
- g_RawData_Buf = new char[BK32_DATA_BUFSIZE];
- int i;
- for(i=0;i<16;i++)
- {
- gV_theta[i] = gV_theta[i]*M_PI/180.0;
- gV_theta_cos[i] = cos(gV_theta[i]);
- gV_theta_sin[i] = sin(gV_theta[i]);
- }
- g_leishen16_Buf = new leishen16_Buf();
- g_bleishen16_run = true;
- g_bleishen16_running = true;
- g_bleishen16_Proc_running = true;
- glidar_port = atoi(gstr_port);
- // g_leishen16_raw = iv::modulecomm::RegisterSend(strmemnameraw,10*sizeof(lidar_leishen16_raw),10);
- g_lidar_pc = iv::modulecomm::RegisterSend(gstr_memname,20000000,1);
- g_pleishen16Thread = new std::thread(leishen16_Func,0);
- g_pleishen16ProcThread = new std::thread(leishen16_Proc_Func,0);
- return 0;
- }
- /**
- * @brief StopLidar_leishen16 Stop
- */
- void LIDAR_DRIVER_LEISHEN16SHARED_EXPORT StopLidar_leishen16()
- {
- std::cout<<"Now Close leishen16. "<<std::endl;
- g_bleishen16_run = false;
- g_pleishen16Thread->join();
- g_pleishen16ProcThread->join();
- iv::modulecomm::Unregister(g_lidar_pc);
- delete g_pleishen16ProcThread;
- delete g_pleishen16Thread;
- delete g_leishen16_Buf;
- delete g_RawData_Buf;
- }
|