dps_routeplan.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "dps/inc/dps.h"
  2. #include "qmath.h"
  3. #include "bus/inc/bus_manager.h"
  4. #include <vector>
  5. void Dps::DistanceCalculate(double currentLongitude, double currentLatitude, double targetLongitude, double targetLatitude,DistanceInfo &returnDis)
  6. {
  7. DistanceInfo distance;
  8. distance.disx=qAbs(currentLongitude-targetLongitude)*qCos(targetLatitude/180*M_PI)*111700;
  9. distance.disy=qAbs(currentLatitude-targetLatitude)*111700;
  10. distance.dis=qSqrt(distance.disx*distance.disx+distance.disy*distance.disy);//当前点与临时点坐标差
  11. returnDis = distance;
  12. }
  13. double Dps::GetHead(double aLongitude,double aLatitude,double bLongitude,double bLatitude)
  14. {
  15. double dx;
  16. double dy;
  17. double angle;
  18. dx = (bLongitude - aLongitude) * 111700 * cos(aLatitude / 180 * M_PI);//当前点与最近点经度距离
  19. dy = (bLatitude - aLatitude) * 111700;//当前点与最近点纬度距离
  20. if(dx>-0.0001&&dx<0.0001)
  21. {
  22. if(dy>0)
  23. angle=0;
  24. else
  25. angle=180;
  26. }
  27. else
  28. {
  29. angle = qAtan((double)dy / dx) / M_PI * 180;//当前点与最近点两点连线与正北的夹角
  30. if (dx > 0) angle = 90 - angle;
  31. else angle = 270 - angle;
  32. }
  33. return angle;
  34. }
  35. void Dps::GetUsualPointKey(const QList<GPSInfo> &gpsList,double currentLongitude,double currentLatitude,double currentHead,double vehicleSpeed,const KeyPointInfo &keyPointLast,KeyPointInfo &retKeyPoint)
  36. {
  37. KeyPointInfo keyPoint;//关键点信息
  38. int gpsListsize;//传入路径list大小
  39. GPSInfo memoryGps;//临时GPS信息
  40. double memoryLongitude;//临时经度
  41. double memoryLatitude;//临时纬度
  42. double memoryHead;//临时航向角
  43. double disHead;//航向差
  44. int preNum;//检索路径点个数
  45. DistanceInfo dis;//两点间距
  46. double currentGear;//当前档位
  47. int desiredGear;//期望档位
  48. double minDistance = MINDISTANCE;//寻找keyPoint的最小值
  49. double rtkSpd;
  50. int begine;
  51. double preDis;
  52. keyPoint = keyPointLast;
  53. begine = keyPointLast.num;
  54. gpsListsize=gpsList.size();
  55. DistanceInfo pointdistance;
  56. DistanceCalculate(gpsList.at(9).iLongitude,gpsList.at(9).iLatitude,gpsList.at(10).iLongitude,gpsList.at(10).iLatitude,pointdistance);
  57. pointdistance.dis=qAbs(pointdistance.dis);
  58. //qDebug()<<"间距:"<<pointdistance.dis;
  59. preDis = vehicleSpeed*2*0.2;
  60. preNum = qRound(preDis/pointdistance.dis);
  61. if(preDis < 6)
  62. {
  63. preNum = qRound(6/pointdistance.dis);
  64. }
  65. if(preDis > 14)
  66. {
  67. preNum = qRound(14/pointdistance.dis);
  68. }
  69. m_datapool->GetValue(desiredGear,DataPool::DESIRESHIFT);
  70. m_datapool->GetValue(currentGear,DataPool::VEHSFTPSNG);
  71. QVariant ina;
  72. ina = m_datapool->GetValue(DataPool::RTKDATA,0);
  73. rtkSpd = ina.value<RtkData>().gSpd;
  74. if((desiredGear==2&&currentGear==4)||(desiredGear==4&&currentGear==2))
  75. {
  76. preDis = 20;
  77. preNum = qRound(preDis/pointdistance.dis);
  78. begine =keyPointLast.num+qRound(6/pointdistance.dis);
  79. }
  80. int jLimit;
  81. //qDebug()<<"preNum:"<<preNum;
  82. jLimit=qMin(keyPointLast.num+preNum,gpsListsize);
  83. bool pointFlag=false;
  84. for (int j = begine ; j<jLimit; j++)
  85. {
  86. memoryGps = gpsList.at(j);
  87. memoryLongitude = memoryGps.iLongitude;
  88. memoryLatitude = memoryGps.iLatitude;
  89. DistanceCalculate(currentLongitude,currentLatitude,memoryLongitude,memoryLatitude,dis);
  90. if((desiredGear==2&&currentGear==4)||(desiredGear==4&&currentGear==2))
  91. {
  92. memoryHead=GetHead(currentLongitude,currentLatitude,memoryLongitude,memoryLatitude);//当前点到计算点连线与正北方向夹角
  93. }
  94. else
  95. {
  96. memoryHead=memoryGps.iHead;
  97. }
  98. disHead=qAbs(currentHead-memoryHead);
  99. if(disHead>M_PI)
  100. disHead=disHead-2*M_PI;
  101. if(disHead<-M_PI)
  102. disHead=2*M_PI+disHead;
  103. if((desiredGear==2&&currentGear==4)||(desiredGear==4&&currentGear==2))
  104. {
  105. if((minDistance > dis.dis)&&((disHead>90)&&(disHead<270)))
  106. {
  107. minDistance = dis.dis;
  108. keyPoint.num=memoryGps.num;
  109. keyPoint.id=memoryGps.id;
  110. keyPoint.type=memoryGps.type;
  111. pointFlag = true;
  112. timeCount = 0;
  113. historygear=currentGear;
  114. }
  115. }
  116. else
  117. {
  118. if((minDistance > dis.dis)&&((disHead<90)||(disHead>270)))
  119. {
  120. minDistance = dis.dis;
  121. keyPoint.num=memoryGps.num;
  122. keyPoint.id=memoryGps.id;
  123. keyPoint.type=memoryGps.type;
  124. pointFlag = true;
  125. timeCount = 0;
  126. }
  127. }
  128. }
  129. if(!pointFlag)
  130. {
  131. if(timeCount == 0)
  132. {
  133. newPointtime.restart();
  134. }
  135. timeCount++;
  136. if((vehicleSpeed > 1||rtkSpd >1) && newPointtime.elapsed() > 1000)
  137. {
  138. GetGlobalPointKey(gpsList,currentLongitude,currentLatitude,currentHead,keyPointLast.num,keyPoint);
  139. timeCount = 0;
  140. }
  141. else
  142. {
  143. keyPoint = keyPointLast;
  144. }
  145. }
  146. retKeyPoint = keyPoint;
  147. }
  148. void Dps::LateralPlan()//横向规划:输出关键点、横向偏差、航向偏差
  149. {
  150. int ij;
  151. double tLng;//实时经度
  152. double tLat;//实时纬度
  153. double tHead;//实时航向角
  154. double currentHead;//实时航向角
  155. SpecialCurve specialCurve;
  156. double mCur;//临时曲率
  157. double mLngs;
  158. double mLats;
  159. PrePoint prePoint;
  160. int prepointcount;
  161. int prepointcount2;
  162. double prepointdis;
  163. double prepointdis2;
  164. double m_angle = 0;//航向偏差
  165. double m_anglex[5];
  166. double pointangle;//当前点与预瞄点连线夹角
  167. double pointangle2;//当前点与关键点连线夹角
  168. double near_angle;
  169. double vehicleSpeed;//当前车速
  170. double strAng;//当前方向盘转角-0811
  171. KeyPointInfo keyPointLast;//上次计算的关键点
  172. KeyPointInfo keyPointLastRef;//上次计算的关键点
  173. QList<GPSInfo> pGpsList;
  174. double curveatKeypoint; //关键点附近的曲率
  175. double curveatPreview; //预瞄点1附近的曲率
  176. double curveatPreview2; //预瞄点2附近的曲率
  177. double pointDisx = 0; //横向偏差
  178. QVariant ina;
  179. ina = m_datapool->GetValue(DataPool::RTKDATA,0);
  180. tLng = ina.value<RtkData>().gLng;
  181. tLat = ina.value<RtkData>().gLat;
  182. tHead = ina.value<RtkData>().gHead;
  183. currentHead = ina.value<RtkData>().gHead;
  184. QVariant inb;
  185. inb = m_datapool->GetValue(DataPool::VEHSPD,0);
  186. vehicleSpeed = inb.value<double>();
  187. QVariant inc;
  188. inc = m_datapool->GetValue(DataPool::KEYPOINTINFO,0);
  189. keyPointLast = inc.value<KeyPointInfo>();
  190. auto ind = m_datapool->GetValue(DataPool::TMS_GPS_PATH, 0);
  191. auto *gpsinfo = ind.value<void*>();
  192. QList<GPSInfo> *normPath = (QList<GPSInfo>*)gpsinfo;
  193. if(normPath)
  194. {
  195. pGpsList=*normPath;
  196. }
  197. QVariant ine;//取方向盘转角值
  198. ine = m_datapool->GetValue(DataPool::VEHSTRANG,0);
  199. strAng = ine.value<double>();
  200. GetUsualPointKey( pGpsList,tLng,tLat,tHead,vehicleSpeed,keyPointLast, dpsKeyPoint);//计算车辆位置的关键点
  201. QVariant outa;
  202. outa = QVariant::fromValue(dpsKeyPoint);
  203. m_datapool->SetValue(DataPool::KEYPOINTINFO,outa);
  204. pointangle2=GetHead(tLng,tLat, pGpsList.at(dpsKeyPoint.num).iLongitude, pGpsList.at(dpsKeyPoint.num).iLatitude);
  205. /*计算预瞄轨迹*/
  206. QList<UTMInfo> globalUTMList = m_datapool->GetValue(DataPool::UTMROUT, 0).value<QList<UTMInfo>>();
  207. double cur_X,cur_Y,cur_heading;
  208. m_gpsCoorTrans.LatLonToUTMXY(tLat, tLng, 50, cur_X, cur_Y);
  209. cur_heading=currentHead/180*M_PI;
  210. float path_length = 0.0,dx=0.0,dy=0.0;
  211. std::vector<UTMInfo> ref_traj;
  212. for (int i = dpsKeyPoint.num; i < globalUTMList.size()-1; i++) { //车辆坐标系下参考轨迹
  213. UTMInfo traj;
  214. double x_t,y_t,heading_t;
  215. x_t = globalUTMList.at(i).x - cur_X;
  216. y_t = globalUTMList.at(i).y - cur_Y;
  217. traj.y= x_t * sin(M_PI*3.0/2.0+cur_heading) + y_t * cos(M_PI*3.0/2.0+cur_heading);
  218. traj.x = x_t * cos(M_PI*3.0/2.0+cur_heading) - y_t * sin(M_PI*3.0/2.0+cur_heading);//rtk to rear para广德的车要减0.9
  219. traj.yaw = -(dpsGlobalPathList.at(i).iHead/180*M_PI - cur_heading);
  220. if(traj.yaw < -M_PI){
  221. traj.yaw = traj.yaw + 2*M_PI;
  222. }
  223. if(traj.yaw > M_PI){
  224. traj.yaw = traj.yaw - 2*M_PI;
  225. }
  226. traj.k=dpsGlobalPathList.at(i).iCurvature;
  227. ref_traj.push_back(traj);
  228. if(i > 0){
  229. dx = globalUTMList.at(i).x - globalUTMList.at(i-1).x ;
  230. dy = globalUTMList.at(i).y - globalUTMList.at(i-1).y ;
  231. path_length += sqrt(dx*dx+dy*dy);
  232. }
  233. if((path_length > vehicleSpeed/3.6*2)&&(path_length >5)){
  234. break;
  235. }
  236. }
  237. double dist = 0;
  238. std::vector<UTMInfo> mpc_ref_traj;
  239. if(mpc_ref_traj.size()>0)
  240. {
  241. mpc_ref_traj.clear();
  242. }
  243. mpc_ref_traj.push_back(ref_traj.at(0));
  244. for(int i = 1;i < ref_traj.size();i++){
  245. dx = ref_traj.at(i).x - ref_traj.at(i-1).x;
  246. dy = ref_traj.at(i).y - ref_traj.at(i-1).y;
  247. dist += sqrt(dx*dx + dy*dy);
  248. if(dist > (vehicleSpeed/3.6*0.02)){
  249. mpc_ref_traj.push_back(ref_traj.at(i-1));
  250. dist = dist - vehicleSpeed/3.6*0.02;
  251. }
  252. if(mpc_ref_traj.size() > 30){
  253. break;
  254. }
  255. }
  256. /*将横向偏差、航向偏差、关键点、预瞄点、关注点曲率信息放入datapool*/
  257. angleExpect.biaDistance=pointDisx;
  258. angleExpect.biaHead=m_angle;
  259. QVariant outb;
  260. outb = QVariant::fromValue(angleExpect);
  261. m_datapool->SetValue(DataPool::HORIZONTALDISIRED,outb);
  262. prePoint.prePointCount=prepointcount;
  263. prePoint.prePointDistance=prepointdis;
  264. QVariant outc;
  265. outc = QVariant::fromValue(prePoint);
  266. m_datapool->SetValue(DataPool::PREPOINT,outc);
  267. QVariant outg;
  268. outg = QVariant::fromValue(mpc_ref_traj);
  269. m_datapool->SetValue(DataPool::REFTRAIL,outg);
  270. }