|
@@ -0,0 +1,232 @@
|
|
|
+#include "computewaypoints.h"
|
|
|
+
|
|
|
+#include <limits>
|
|
|
+#include <memory>
|
|
|
+
|
|
|
+ComputeWayPoints::ComputeWayPoints()
|
|
|
+{
|
|
|
+ ModuleFun xFunGPSIMU = std::bind(&ComputeWayPoints::ListenGPSIMU,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
|
|
|
+ mpagpsimu = iv::modulecomm::RegisterRecvPlus("hcp2_gpsimu",xFunGPSIMU);
|
|
|
+
|
|
|
+ ModuleFun xFunNewTraceMap = std::bind(&ComputeWayPoints::ListenNewTraceMap,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
|
|
|
+ mpanewtrace = iv::modulecomm::RegisterRecvPlus("newtracemap",xFunNewTraceMap);
|
|
|
+
|
|
|
+ mpawaypoints = iv::modulecomm::RegisterSend("waypoints",1000000,1);
|
|
|
+
|
|
|
+ mpthread = new std::thread(&ComputeWayPoints::ThreadCompute,this);
|
|
|
+}
|
|
|
+
|
|
|
+ComputeWayPoints::~ComputeWayPoints()
|
|
|
+{
|
|
|
+ mbCompute = false;
|
|
|
+ mpthread->join();
|
|
|
+}
|
|
|
+
|
|
|
+void ComputeWayPoints::ThreadCompute()
|
|
|
+{
|
|
|
+ while(mbCompute)
|
|
|
+ {
|
|
|
+ if(mbHaveMap == false)
|
|
|
+ {
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(mbGPSIMUUpdate == false)
|
|
|
+ {
|
|
|
+ mMutexWait.lock();
|
|
|
+ mwc.wait(&mMutexWait,100);
|
|
|
+ mMutexWait.unlock();
|
|
|
+ }
|
|
|
+ if(mbGPSIMUUpdate)
|
|
|
+ {
|
|
|
+ iv::map::tracemap xtracemap;
|
|
|
+ int nrtn;
|
|
|
+ nrtn = FindWayPoints(xtracemap);
|
|
|
+ mbGPSIMUUpdate = false;
|
|
|
+ if(nrtn > 0)
|
|
|
+ {
|
|
|
+ int nbytesize = xtracemap.ByteSize();
|
|
|
+ std::shared_ptr<char> str_ptr = std::shared_ptr<char>(new char[nbytesize]);
|
|
|
+ if(xtracemap.SerializeToArray(str_ptr.get(),nbytesize))
|
|
|
+ {
|
|
|
+ iv::modulecomm::ModuleSendMsg(mpawaypoints,str_ptr.get(),nbytesize);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ std::cout<<" tracemap serialize fail."<<std::endl;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ std::cout<<" ThreadCompute Complete."<<std::endl;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void ComputeWayPoints::ListenGPSIMU(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
|
|
|
+{
|
|
|
+ (void )index;
|
|
|
+ (void)dt;
|
|
|
+ (void)strmemname;
|
|
|
+
|
|
|
+ iv::gps::gpsimu xgpsimu;
|
|
|
+ if(!xgpsimu.ParseFromArray(strdata,nSize))
|
|
|
+ {
|
|
|
+ std::cout<<" ListenGPSIMU Parse Fail. "<<std::endl;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ mMutexgpsimu.lock();
|
|
|
+ mgpsimu.CopyFrom(xgpsimu);
|
|
|
+ mbGPSIMUUpdate = true;
|
|
|
+ mMutexgpsimu.unlock();
|
|
|
+ mwc.wakeAll();
|
|
|
+}
|
|
|
+
|
|
|
+void ComputeWayPoints::ListenNewTraceMap(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
|
|
|
+{
|
|
|
+ (void )index;
|
|
|
+ (void)dt;
|
|
|
+ (void)strmemname;
|
|
|
+
|
|
|
+ const double fspeedlimt = 30.0/3.6;
|
|
|
+ const double fdacc = -0.5;
|
|
|
+
|
|
|
+ iv::map::tracemap xtracemap;
|
|
|
+ if(!xtracemap.ParseFromArray(strdata,nSize))
|
|
|
+ {
|
|
|
+ std::cout<<" ListenNewTraceMap Parse Fail. "<<std::endl;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ int nmapsize = xtracemap.point_size();
|
|
|
+ int i;
|
|
|
+ if(nmapsize<100)return;
|
|
|
+ iv::map::mappoint * plastpoint = xtracemap.mutable_point(nmapsize -1);
|
|
|
+ plastpoint->set_speed(0);
|
|
|
+ double fdis = 0;
|
|
|
+ for(i=(nmapsize-2);i>=0;i--)
|
|
|
+ {
|
|
|
+ iv::map::mappoint * ppoint = xtracemap.mutable_point(i);
|
|
|
+ fdis = fdis + sqrt(pow(ppoint->gps_x() - plastpoint->gps_x(),2)+pow(ppoint->gps_y() - plastpoint->gps_y(),2));
|
|
|
+ double fv = sqrt(fabs(2.0* fdacc * fdis ));
|
|
|
+ if(fv > fspeedlimt)fv = fspeedlimt;
|
|
|
+ ppoint->set_speed(fv);
|
|
|
+ plastpoint = ppoint;
|
|
|
+ // std::cout<<" speed : "<<fv<<std::endl;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ mMutexTraceMap.lock();
|
|
|
+ mtracemap.CopyFrom(xtracemap);
|
|
|
+ mbHaveMap = true;
|
|
|
+ mMutexTraceMap.unlock();
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+int ComputeWayPoints::FindWayPoints(iv::map::tracemap &xtracemap)
|
|
|
+{
|
|
|
+ qint64 ncompstart = std::chrono::system_clock::now().time_since_epoch().count();
|
|
|
+ static int nnearoldindex = -1;
|
|
|
+ iv::gps::gpsimu xgpsimu;
|
|
|
+ mMutexgpsimu.lock();
|
|
|
+ xgpsimu.CopyFrom(mgpsimu);
|
|
|
+ mMutexgpsimu.unlock();
|
|
|
+ mMutexTraceMap.lock();
|
|
|
+ double fdis;
|
|
|
+ double fheaddiff;
|
|
|
+ int nnearindex = -1;
|
|
|
+ double fdismin = std::numeric_limits<double>::max();
|
|
|
+
|
|
|
+ double fnowx,fnowy;
|
|
|
+ GaussProjCal(xgpsimu.lon(),xgpsimu.lat(),&fnowx,&fnowy);
|
|
|
+
|
|
|
+ int i;
|
|
|
+ int ntracesize = mtracemap.point_size();
|
|
|
+
|
|
|
+ int nfrom = nnearoldindex - 100;
|
|
|
+ if(nfrom < 0)nfrom = 0;
|
|
|
+ int nto = nnearoldindex + 100;
|
|
|
+ if(nto > ntracesize)nto = ntracesize;
|
|
|
+
|
|
|
+ //First, Find in last near.
|
|
|
+ for(i=nfrom;i<nto;i++)
|
|
|
+ {
|
|
|
+ iv::map::mappoint * ppoint = mtracemap.mutable_point(i);
|
|
|
+
|
|
|
+ fdis = sqrt(pow(ppoint->gps_x() - fnowx,2)+pow(ppoint->gps_y() - fnowy,2));
|
|
|
+ fheaddiff = (xgpsimu.heading() - ppoint->ins_heading_angle())*M_PI/180.0;
|
|
|
+ while(fheaddiff<(-M_PI))fheaddiff = fheaddiff + 2.0*M_PI;
|
|
|
+ while(fheaddiff > M_PI)fheaddiff = fheaddiff - 2.0*M_PI;
|
|
|
+ //First Find,so use nearest.
|
|
|
+ if(fabs(fheaddiff) < M_PI/9.0)
|
|
|
+ {
|
|
|
+ if(fdis < fdismin)
|
|
|
+ {
|
|
|
+ fdismin = fdis;
|
|
|
+ nnearindex = i;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //if distance < 1.0, so find
|
|
|
+ if(fdismin<1.0)
|
|
|
+ {
|
|
|
+ std::cout<<"use near successfully."<<std::endl;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+ for(i=0;i<ntracesize;i++)
|
|
|
+ {
|
|
|
+ iv::map::mappoint * ppoint = mtracemap.mutable_point(i);
|
|
|
+
|
|
|
+ fdis = sqrt(pow(ppoint->gps_x() - fnowx,2)+pow(ppoint->gps_y() - fnowy,2));
|
|
|
+ fheaddiff = (xgpsimu.heading() - ppoint->ins_heading_angle())*M_PI/180.0;
|
|
|
+ while(fheaddiff<(-M_PI))fheaddiff = fheaddiff + 2.0*M_PI;
|
|
|
+ while(fheaddiff > M_PI)fheaddiff = fheaddiff - 2.0*M_PI;
|
|
|
+ if(fabs(fheaddiff) < M_PI/2.0)
|
|
|
+ {
|
|
|
+ if(fdis < fdismin)
|
|
|
+ {
|
|
|
+ fdismin = fdis;
|
|
|
+ nnearindex = i;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(nnearindex >= 0)
|
|
|
+ {
|
|
|
+ nnearoldindex = nnearindex;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// std::cout<<" fdismin : "<<fdismin<<std::endl;
|
|
|
+
|
|
|
+ if(fdismin > 10.0)
|
|
|
+ {
|
|
|
+ mMutexTraceMap.unlock();
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ int j = 0;
|
|
|
+ for(i=nnearindex;i<ntracesize;i++)
|
|
|
+ {
|
|
|
+ iv::map::mappoint * pnew = xtracemap.add_point();
|
|
|
+ pnew->CopyFrom(mtracemap.point(i));
|
|
|
+ j++;
|
|
|
+ if(j>1000)break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// std::cout<<" waypoint size : "<<xtracemap.point_size()<<std::endl;
|
|
|
+ mMutexTraceMap.unlock();
|
|
|
+
|
|
|
+ qint64 ncalctime = std::chrono::system_clock::now().time_since_epoch().count() - ncompstart;
|
|
|
+ double fcalc = ncalctime/(1000000.0);
|
|
|
+ (void)fcalc;
|
|
|
+ // std::cout<<" calc time : "<<fcalc<<" ms "<<std::endl;
|
|
|
+ return 1;
|
|
|
+
|
|
|
+
|
|
|
+}
|