#include #include #include //#include #include #include #include //#include #include #include #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 = "< 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 "<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 = "<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."<pendingDatagramSize()); QHostAddress sender; quint16 senderPort; udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); // processTheDatagram(datagram); // std::cout<<"have data."<receiveDatagram(); processleishen16_Data(datagram); datagram.clear(); } else { // std::cout<<"running."< 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."<::Ptr point_cloud( new pcl::PointCloud()); 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."<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 = "<width<<" size = "<size()<ReadData(strdata,BK32_DATA_BUFSIZE,&nIndex))>0) { //process data. process_leishen16obs(strdata,nRead); } else { // std::cout<<"running."<join(); g_pleishen16ProcThread->join(); iv::modulecomm::Unregister(g_lidar_pc); delete g_pleishen16ProcThread; delete g_pleishen16Thread; delete g_leishen16_Buf; delete g_RawData_Buf; }