| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- #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_hesai40line.h".h"
- #include "lidar_hesai40line_rawdata.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 (270*Lidar_Pi/180.0)
- std::thread * g_phesai40lineThread;
- std::thread * g_phesai40lineProcThread;
- 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];
- void * g_hesai40line_raw;
- void * g_lidar_pc;
- bool g_bhesai40line_run = false;
- bool g_bhesai40line_running = false;
- bool g_bhesai40line_Proc_running = false;
- int g_seq = 0;
- double groll_ang;
- class hesai40line_Buf
- {
- private:
- char * mstrdata;
- int mnSize; //Data SizeUse
- QMutex mMutex;
- int mIndex;
- public:
- hesai40line_Buf()
- {
- mstrdata = new char[BK32_DATA_BUFSIZE];
- mIndex = 0;
- mnSize = 0;
- }
- ~hesai40line_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_hesai40line hesai40line_Buf ReadData data nRead = "<<nRead<<" is small"<<std::endl;
- }
- memcpy(strdata,mstrdata,nRtn);
- mnSize = 0;
- if(pnIndex != 0)*pnIndex = mIndex;
- mMutex.unlock();
- return nRtn;
- }
- };
- hesai40line_Buf * g_hesai40line_Buf;
- char * g_RawData_Buf;
- int gnRawPos = 0;
- float gAngle_Old = 0;
- float gAngle_Total = 0;
- unsigned short gold = 0;
- int gnPac = 0;
- #include <QTime>
- QTime gTime;
- void processhesai40line_Data(QByteArray ba)
- {
- gnPac++;
- unsigned short * pAng;
- float fAng;
- char * pdata;
- pdata = ba.data();
- // qDebug("size is %d",ba.length());
- if(ba.length() == 1240)
- {
- pAng = (unsigned short *)(pdata+2);
- fAng = *pAng;fAng = fAng*0.01;
- 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_hesai40line_Buf->WriteData(g_RawData_Buf,gnRawPos);
- lidar_hesai40line_raw * p = new lidar_hesai40line_raw();
- p->mnLen = gnRawPos;
- memcpy(p->mstrdata,g_RawData_Buf,gnRawPos);
- // g_hesai40line_raw->writemsg((char *)p,sizeof(lidar_hesai40line_raw));
- delete p;
- memcpy(g_RawData_Buf,pdata,1240);
- gnRawPos = 1240;
- // std::cout<<"index = "<<gnPac<<" time ="<<gTime.elapsed()<<" a cycle"<<std::endl;
- gAngle_Total = 0;
- }
- else
- {
- if((gnRawPos+1240)<= BK32_DATA_BUFSIZE)
- {
- memcpy(g_RawData_Buf+gnRawPos,pdata,1240);
- gnRawPos= gnRawPos+1240;
- }
- else
- {
- std::cout<<"lidar_hesai40line processhesai40line_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,1240);
- gnRawPos = gnRawPos+1240;
- }
- }
- else
- {
- std::cout<<"lidar_hesai40line processhesai40line_Data receive data packet len is not 1240 "<<std::endl;
- }
- }
- void hesai40line_Func(int n)
- {
- gTime.start();
- std::cout<<"Enter hesai40line_Func."<<std::endl;
- QUdpSocket * udpSocket = new QUdpSocket( );
- udpSocket->bind(QHostAddress::Any, 8080);
- while(g_bhesai40line_run)
- {
- if(udpSocket->hasPendingDatagrams())
- {
- // std::cout<<"have data."<<std::endl;
- QNetworkDatagram datagram = udpSocket->receiveDatagram();
- processhesai40line_Data(datagram.data());
- datagram.clear();
- }
- else
- {
- // std::cout<<"running."<<std::endl;
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- }
- udpSocket->close();
- delete udpSocket;
- g_bhesai40line_running = false;
- std::cout<<"hesai40line_Func Exit."<<std::endl;
- }
- void process_obs(char * strdata,int nLen)
- {
- QDateTime dt = QDateTime::currentDateTime();
- pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
- new pcl::PointCloud<pcl::PointXYZI>());
- // point_cloud->header.stamp =
- // pcl_conversions::toPCL(sweep_data->header).stamp;
- 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 w = 0;
- float Ang = 0;
- float Range = 0;
- int bag = 0;
- int Group = 0;
- int pointi = 0;
- float wt = 0;
- int wt1 = 0;
- // int dx;
- // int dy;
- // float lidar_32_xdistance = 0.3; //32线激光雷达X轴补偿
- // float lidar_32_ydistance = -2.3; //32线激光雷达Y轴补偿
- float H_BETA[40] = {
- 0.005, 0.006, 0.006, 0.006,
- -2.479, -2.479, 2.491, -4.953,
- -2.479, 2.492, -4.953, -2.479,
- 2.492, -4.953, 0.007, 2.491,
- -4.953, 0.006, 4.961, -2.479,
- 0.006, 4.96, -2.478, 0.006,
- 4.958, -2.478, 2.488, 4.956,
- -2.477, 2.487, 2.485, 2.483,
- 0.004, 0.004, 0.003, 0.003,
- -2.466, -2.463, -2.46, -2.457};
- float V_theta[40] = {
- 6.96, 5.976, 4.988, 3.996,
- 2.999, 2.001, 1.667, 1.333,
- 1.001, 0.667, 0.333, 0,
- -0.334, -0.667, -1.001, -1.334,
- -1.667, -2.001, -2.331, -2.667,
- -3, -3.327, -3.663, -3.996,
- -4.321, -4.657, -4.986, -5.311,
- -5.647, -5.974, -6.957, -7.934,
- -8.908, -9.871, -10.826, -11.772,
- -12.705, -13.63, -14.543, -15.444};
- float T[32] = { 0, 3.125, 1.5625, 4.6875, 6.25, 9.375, 7.8125, 10.9375,
- 12.5, 15.625, 14.0625, 17.1875, 18.75, 21.875, 20.3125, 23.4375,
- 25, 28.125, 26.5625, 29.6875, 31.25, 34.375, 32.8125, 35.9375,
- 37.5, 40.625, 39.0625, 42.1875, 43.75, 46.875, 45.3125, 48.4375};
- int buf1len = nLen/1240;
- for (bag = 0; bag < buf1len; bag++)
- {
- for (Group = 0; Group < 6; Group++)
- {
- wt1 = ((pstr[bag*1240 +3 + Group * 204] *256) + pstr[bag*1240 + 2 + Group * 204]) ;
- wt = wt1/ 100.0;
- for (pointi = 0; pointi <= 39; pointi++)
- {
- // Ang = (0 - wt - w * T[pointi] - H_BETA[pointi]+213) / 180.0 * Lidar_Pi; //shuiping 213
- Ang = (0-wt - H_BETA[pointi]) / 180.0 * Lidar_Pi;
- Range = ((pstr[bag*1240 + Group * 204 + 6 + 5 * pointi] << 16) + (pstr[bag*1240 + Group * 204 + 5 + 5 * pointi] << 8) + pstr[bag*1240+Group * 204 + 4 + 5 * pointi]);
- unsigned short intensity =(pstr[bag*1240 + Group * 204 + 8 + 5 * pointi] << 8) + pstr[bag*1240 + Group * 204 + 7 + 5 * pointi];
- Range=Range* 2.0/1000.0;
- pcl::PointXYZI point;
- point.x = Range*cos(V_theta[pointi] / 180 * Lidar_Pi)*cos(Ang + groll_ang);
- point.y = Range*cos(V_theta[pointi] / 180 * Lidar_Pi)*sin(Ang + groll_ang);
- point.z = Range*sin(V_theta[pointi] / 180 * Lidar_Pi);
- point.intensity = intensity;
- 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;
- }
- void hesai40line_Proc_Func(int n)
- {
- std::cout<<"Enter hesai40line_Proc_Func"<<std::endl;
- char * strdata = new char[BK32_DATA_BUFSIZE];
- int nIndex;
- int nRead;
- while(g_bhesai40line_run)
- {
- if((nRead = g_hesai40line_Buf->ReadData(strdata,BK32_DATA_BUFSIZE,&nIndex))>0)
- {
- //process data.
- process_obs(strdata,nRead);
- }
- else
- {
- // std::cout<<"running."<<std::endl;
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- }
- g_bhesai40line_Proc_running = false;
- std::cout<<"Exit hesai40line_Proc_Func"<<std::endl;
- }
- int LIDAR_HESAI40LINESHARED_EXPORT StartLidar_hesai40line()
- {
- std::cout<<"Now Start hesai40line Listen."<<std::endl;
- g_RawData_Buf = new char[BK32_DATA_BUFSIZE];
- g_hesai40line_Buf = new hesai40line_Buf();
- g_bhesai40line_run = true;
- g_bhesai40line_running = true;
- g_bhesai40line_Proc_running = true;
- groll_ang = atof(gstr_rollang);
- std::cout<<"roll ang is "<<groll_ang;
- groll_ang = groll_ang * M_PI/180.0;
- g_hesai40line_raw = iv::modulecomm::RegisterSend("hesai40line_Raw",10*sizeof(lidar_hesai40line_raw),10);
- g_lidar_pc = iv::modulecomm::RegisterSend(gstr_memname,20000000,1);
- g_phesai40lineThread = new std::thread(hesai40line_Func,0);
- g_phesai40lineProcThread = new std::thread(hesai40line_Proc_Func,0);
- return 0;
- }
- void LIDAR_HESAI40LINESHARED_EXPORT StopLidar_hesai40line()
- {
- std::cout<<"Now Close hesai40line. "<<std::endl;
- g_bhesai40line_run = false;
- g_phesai40lineThread->join();
- g_phesai40lineProcThread->join();
- delete g_phesai40lineProcThread;
- delete g_phesai40lineThread;
- delete g_hesai40line_Buf;
- delete g_RawData_Buf;
- }
|