ivdecision_brain.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #ifndef IVDECISION_BRAIN_H
  2. #define IVDECISION_BRAIN_H
  3. #include "ivdecision.h"
  4. #include "decition_type.h"
  5. #include "gps_type.h"
  6. #include "constants.h"
  7. #include <vector>
  8. #include <gnss_coordinate_convert.h>
  9. #include <adc_planner/frenet_planner.h>
  10. #include <adc_controller/pid_controller.h>
  11. #include <adc_adapter/ge3_adapter.h>
  12. #include <adc_adapter/qingyuan_adapter.h>
  13. #include <adc_adapter/vv7_adapter.h>
  14. #include <adc_adapter/zhongche_adapter.h>
  15. #include <adc_adapter/bus_adapter.h>
  16. #include <adc_adapter/hapo_adapter.h>
  17. #include <adc_adapter/zhongche_adapter.h>
  18. #include <adc_adapter/yuhesen_adapter.h>
  19. #include "common/perceptionoutput.h"
  20. #include "adc_tools/compute_00.h"
  21. #include "common/car_status.h"
  22. #include "adc_tools/gps_distance.h"
  23. #include "adc_tools/transfer.h"
  24. namespace iv {
  25. class ivdecision_brain : public ivdecision
  26. {
  27. public:
  28. enum VehState { normalRun, park, readyPark, stopObs, avoidObs, preAvoid, avoiding, backOri,preBack,
  29. waitAvoid ,shoushaTest,justRun, changingRoad, chaoche, chaocheBack, reverseCar,reversing, reverseCircle, reverseDirect,reverseArr,
  30. dRever,dRever0,dRever1,dRever2,dRever3,dRever4} mvehState;
  31. public:
  32. ivdecision_brain();
  33. public:
  34. virtual int getdecision(iv::brain::decition & xdecition,iv::brain::brainstate & xbs);
  35. private:
  36. // int getdecision_normal(iv::decition::Decition &xdecition,GPS_INS now_gps_ins,
  37. // const std::vector<GPSData> gpsMapLine,
  38. // iv::LidarGridPtr lidarGridPtr,
  39. // std::vector<iv::Perception::PerceptionOutput> lidar_per,
  40. // iv::TrafficLight trafficLight);
  41. private:
  42. std::vector<GPSData> mgpsMapLine;
  43. private:
  44. iv::decition::Decition getDecideFromGPS(GPS_INS now_gps_ins,const std::vector<GPSData> gpsMapLine,
  45. iv::LidarGridPtr lidarGridPtr,
  46. std::vector<iv::Perception::PerceptionOutput> lidar_per,
  47. iv::TrafficLight trafficLight);
  48. private:
  49. void updatev2x();
  50. void updateultra();
  51. void updateradar();
  52. void updategroupinfo();
  53. void updatechassistocarstatus();
  54. void loadxml();
  55. void adjuseGpsLidarPosition();
  56. private:
  57. int mnRunMode = 0;
  58. bool mbisFirstRun = true;
  59. int mnPathPoint;
  60. private:
  61. float xiuzhengCs=0;
  62. int PathPoint = -1;
  63. double minDis = iv::MaxValue;
  64. double maxAngle = iv::MinValue;
  65. //int lastGpsIndex = iv::MaxValue;
  66. int lastGpsIndex = 0;
  67. double controlSpeed = 0;
  68. double controlBrake = 0;
  69. double controlAng = 0;
  70. double Iv = 0;
  71. double lastU = 0;
  72. double lastVt = 0;
  73. double lastEv = 0;
  74. int gpsLineParkIndex = 0;
  75. int gpsMapParkIndex = 0;
  76. double lastDistance = MaxValue;
  77. double lastPreObsDistance = MaxValue;
  78. double obsDistance = -1;
  79. double obsSpeed=0;
  80. double obsDistanceAvoid = -1;
  81. double lastDistanceAvoid = -1;
  82. double lidarDistance = -1;
  83. double myesrDistance = -1;
  84. double lastLidarDis = -1;
  85. bool parkMode = false;
  86. bool readyParkMode = false;
  87. bool zhucheMode = false;
  88. bool readyZhucheMode = false;
  89. bool hasZhuched = false;
  90. double lastLidarDisAvoid = -1;
  91. double lidarDistanceAvoid = -1;
  92. int finishPointNum = -1;
  93. int zhuchePointNum = 0;
  94. ///kkcai, 2020-01-03
  95. bool isFirstRun = true;
  96. //////////////////////////////////////////////
  97. float minDecelerate=0;
  98. //bool startingFlag = true;//起步标志,在起步时需要进行frenet规划。
  99. double offset_real = 0;
  100. double realspeed;
  101. double dSpeed;
  102. double dSecSpeed;
  103. double secSpeed;
  104. double ac;
  105. iv::Point2D obsPoint;
  106. iv::Point2D obsPointAvoid;
  107. // iv::Point2D obsPoint(-1.0, -1.0);
  108. // iv::Point2D obsPointAvoid(-1, -1);
  109. int esrIndex = -1;
  110. int esrIndexAvoid = -1;
  111. double obsSpeedAvoid = 0;
  112. double esrDistance = -1;
  113. double esrDistanceAvoid = -1;
  114. int obsLostTime = 0;
  115. int obsLostTimeAvoid = 0;
  116. // double avoidTime = 0;
  117. double avoidX =0;
  118. float roadWidth = 3.5;
  119. int roadSum =10;
  120. int roadNow = 0;
  121. int roadOri =0;
  122. int roadPre = -1;
  123. int lastRoadOri = 0;
  124. int lightTimes = 0;
  125. int lastRoadNum=1;
  126. int stopCount = 0;
  127. int gpsMissCount = 0;
  128. bool rapidStop = false;
  129. bool hasTrumpeted = false;
  130. bool changeRoad=false;
  131. //实验手刹
  132. bool handFirst = true;
  133. double handTimeSpan = 0;
  134. double handStartTime = 0;
  135. double handLastTime = 0;
  136. bool handPark = false;
  137. long handParkTime=10000;
  138. //喇叭
  139. bool trumpetFirstCount=true;
  140. double trumpetTimeSpan = 0;
  141. double trumpetStartTime = 0;
  142. double trumpetLastTime = 0;
  143. //过渡
  144. bool transferFirstCount=true;
  145. double transferTimeSpan = 0;
  146. double transferStartTime = 0;
  147. double transferLastTime = 0;
  148. bool busMode = false;
  149. bool parkBesideRoad=false;
  150. bool chaocheMode = false;
  151. bool specialSignle = false;
  152. double offsetX = 0;
  153. double steerSpeed=9000;
  154. bool transferPieriod;
  155. bool transferPieriod2;
  156. double traceDevi;
  157. bool startingFlag = true; //起步标志,在起步时需要进行frenet规划。
  158. bool useFrenet = false; //标志是否启用frenet算法避障
  159. bool useOldAvoid = true; //标志是否用老方法避障
  160. // enum VehState { normalRun, park, readyPark, stopObs, avoidObs, preAvoid, avoiding, backOri,preBack,
  161. // waitAvoid ,shoushaTest,justRun, changingRoad, chaoche, chaocheBack, reverseCar,reversing, reverseCircle, reverseDirect,reverseArr,
  162. // dRever,dRever0,dRever1,dRever2,dRever3,dRever4} vehState;
  163. std::vector<iv::Point2D> gpsTraceAim; //记录变道后的目标路径,若车辆经frenet规划行驶至该路径上后,就关闭frenet规划
  164. std::vector<iv::Point2D> gpsTraceAvoid,gpsTraceNow,gpsTraceBack,gpsTracePre,gpsTraceOri,gpsTraceRear;
  165. std::vector<double> esrDisVector;
  166. std::vector<double> lidarDisVector;
  167. std::vector<double> obsDisVector;
  168. std::vector<double> obsSpeedVector;
  169. std::vector<int> obsLostTimeVector;
  170. std::vector<double> lastLidarDisVector;
  171. std::vector<double> lastDistanceVector;
  172. // std::vector<double> esrDisVector(roadSum, -1);
  173. // std::vector<double> lidarDisVector(roadSum, -1);
  174. // std::vector<double> obsDisVector(roadSum,-1);
  175. // std::vector<double> obsSpeedVector(roadSum, 0);
  176. // std::vector<int> obsLostTimeVector(roadSum, 0);
  177. // std::vector<double> lastLidarDisVector(roadSum, -1);
  178. // std::vector<double> lastDistanceVector(roadSum, -1);
  179. bool qiedianCount = false;
  180. private:
  181. // static double minDis;
  182. // static double maxAngle;
  183. // static int lastGpsIndex;
  184. // static double lidarDistance;
  185. // static double myesrDistance;
  186. // static double lastLidarDis;
  187. // static double lastLidarDisAvoid;
  188. // static double lastPreObsDistance;
  189. // static int gpsLineParkIndex;
  190. // static int gpsMapParkIndex;
  191. // static double lastDistance;
  192. // static float xiuzhengCs;
  193. // static double controlAng;
  194. // double laneAng;
  195. // static double controlSpeed;
  196. // static double controlBrake;
  197. // static bool parkMode;
  198. // static bool readyParkMode;
  199. // static bool zhucheMode;
  200. // static bool readyZhucheMode;
  201. // static bool hasZhuched;
  202. // static double Iv;
  203. // static double lastEv;
  204. // static double lastVt;
  205. // static double lastU;
  206. // static double obsDistance;
  207. // static double obsSpeed;
  208. // static double lidarDistanceAvoid;
  209. // static double obsDistanceAvoid;
  210. // static double lastDistanceAvoid;
  211. GPS_INS group_ori_gps;
  212. GPS_INS startAvoid_gps_ins;
  213. // static int finishPointNum;
  214. // static int zhuchePointNum;
  215. double avoidRunDistance=0;
  216. std::vector<iv::GPS_INS> startAvoidGpsInsVector;
  217. std::vector<double> avoidMinDistanceVector;
  218. std::vector<int> v2xTrafficVector;
  219. ///kkcai, 2020-01-03
  220. //bool isFirstRun = true;
  221. // static bool isFirstRun;
  222. /////////////////////////////////////
  223. // static bool startingFlag;//起步标志,在起步时需要进行frenet规划。
  224. // static float minDecelerate;
  225. double startTime = 0;
  226. double firstTime = 0;
  227. bool circleMode=false;
  228. int avoidTimes = 0;
  229. int backTimes = 0;
  230. int checkAvoidObsTimes = 0;
  231. int checkBackObsTimes = 0;
  232. bool hasCheckedAvoidLidar=false;
  233. bool hasCheckedBackLidar=false;
  234. bool personWaited = false;
  235. bool roadLightWaited = false;
  236. double decision_Angle = 0;
  237. double lastAngle=0;
  238. // iv::Point2D obsPoint;
  239. int checkAvoidTimes=0;
  240. int checkBackTimes=0;
  241. int chaocheCounts=0;
  242. // static int PathPoint;
  243. float lastGroupOffsetX=0.0;
  244. GPS_INS traffic_light_gps;
  245. int traffic_light_pathpoint;
  246. bool changingDangWei=false;
  247. iv::decition::BaseController *pid_Controller;
  248. iv::decition::BaseAdapter *ge3_Adapter;
  249. iv::decition::BaseAdapter *qingyuan_Adapter;
  250. iv::decition::BaseAdapter *vv7_Adapter;
  251. iv::decition::BaseAdapter *zhongche_Adapter;
  252. iv::decition::BaseAdapter *bus_Adapter;
  253. iv::decition::BaseAdapter *hapo_Adapter;
  254. iv::decition::BaseAdapter *yuhesen_Adapter;
  255. std::shared_ptr<iv::decition::BaseController> mpid_Controller;
  256. std::shared_ptr<iv::decition::BaseAdapter> mge3_Adapter;
  257. std::shared_ptr<iv::decition::BaseAdapter> mqingyuan_Adapter;
  258. std::shared_ptr<iv::decition::BaseAdapter> mvv7_Adapter;
  259. std::shared_ptr<iv::decition::BaseAdapter> mzhongche_Adapter;
  260. std::shared_ptr<iv::decition::BaseAdapter> mbus_Adapter;
  261. std::shared_ptr<iv::decition::BaseAdapter> mhapo_Adapter;
  262. std::shared_ptr<iv::decition::BaseAdapter> myuhesen_Adapter;
  263. iv::decition::FrenetPlanner *frenetPlanner;
  264. VehState vehState;
  265. // BasePlanner *laneChangePlanner;
  266. std::shared_ptr<iv::decition::FrenetPlanner> mfrenetPlanner;
  267. // std::shared_ptr<BasePlanner> mlaneChangePlanner;
  268. // Decition getDecideFromGPS(GPS_INS gpsIns, const std::vector<GPSData> navigation_data, iv::LidarGridPtr lidarGridPtr,
  269. // std::vector<iv::Perception::PerceptionOutput> lidar_per,iv::TrafficLight trafficLight);
  270. void initMethods();
  271. std::vector<Point2D> getGpsTrace(GPS_INS rp, const std::vector<GPSData> gpsMap, int lastIndex, bool circleMode);
  272. double getAngle(std::vector<Point2D> gpsTrace);
  273. double getAngle(std::vector<Point2D> gpsTrace, GPS_INS gpsIns,iv::decition::Decition decition);
  274. double getSpeed(std::vector<Point2D> gpsTrace);
  275. void getEsrObsDistance(std::vector<Point2D> gpsTrace, int roadNum);
  276. void getRearEsrObsDistance(std::vector<Point2D> gpsTrace, int roadNum);
  277. void getEsrObsDistanceAvoid();
  278. void getObsAvoid(iv::LidarGridPtr lidarGridPtr);
  279. void phaseSpeedDecition(iv::decition::Decition decition, double secSpeed, double obsDis, double obsSpeed, GPS_INS gpsIns );
  280. double limitAngle(double speed, double angle);
  281. double limitSpeed(double angle, double speed);
  282. bool checkAvoidEnable(double avoidX, iv::LidarGridPtr lidarGridPtr, int roadNum);
  283. bool checkReturnEnable(double avoidX, iv::LidarGridPtr lidarGridPtr, int roadNum);
  284. void computeObsOnRoad(iv::LidarGridPtr lidarGridPtr, std::vector<Point2D> gpsTrace, int roadNum,const std::vector<GPSData> gpsMapLine, std::vector<iv::Perception::PerceptionOutput> lidar_per);
  285. void getEsrObsDistanceByFrenet(const std::vector<Point2D>& gpsTrace, iv::decition::FrenetPoint car_now_frenet_point,iv::decition::FrenetPoint &esrObs_F_Point, const std::vector<iv::GPSData>& gpsMap,int pathpoint,GPS_INS nowGps);
  286. void computeRearObsOnRoad(iv::LidarGridPtr lidarGridPtr, std::vector<Point2D> gpsTrace, int roadNum,const std::vector<GPSData> gpsMapLine);
  287. void computeObsOnRoadByFrenet(iv::LidarGridPtr lidarGridPtr,const std::vector<Point2D>& gpsTrace, double & obs, const std::vector<iv::GPSData>& gpsMap,int pathpoint,GPS_INS nowGps);
  288. void predictObsOnRoad(std::vector<iv::Perception::PerceptionOutput> lidar_per,std::vector<Point2D> gpsTrace,double realSpeed);
  289. int chooseAvoidRoad(iv::LidarGridPtr lidarGridPtr, GPS_INS now_gps_ins,std::vector<GPSData> gpsMapLine,std::vector<iv::Perception::PerceptionOutput> lidar_per);
  290. int chooseBackRoad(iv::LidarGridPtr lidarGridPtr, GPS_INS now_gps_ins, std::vector<GPSData> gpsMapLine,std::vector<iv::Perception::PerceptionOutput> lidar_per);
  291. void getMapBeside(std::vector<iv::GPSData> navigation_data, iv::GPS_INS now_gps_ins);
  292. void handBrakePark(iv::decition::Decition decition, long duringTime, GPS_INS now_gps_ins);
  293. bool checkChaoCheBack(iv::LidarGridPtr);
  294. double trumpet();
  295. double transferP();
  296. bool nearStation;
  297. void updateGroupDate(GPS_INS now_gps_ins,float realspeed,float theta,float s);
  298. void init();
  299. std::vector<Point2D> getGpsTraceOffset(std::vector<Point2D> gpsTrace, double offset);
  300. std::vector<iv::Point2D> getGpsTraceBackExa(iv::GPS_INS now_gps_ins, const std::vector<GPSData> gpsMapLine, int pathpo);
  301. float ComputeTrafficLightSpeed(int traffic_light_color, int traffic_light_time, const std::vector<GPSData> gpsMapLine,int traffic_light_pathpoint,
  302. int pathpoint,float secSpeed,float dSpeed);
  303. void getV2XTrafficPositionVector( const std::vector<GPSData> gpsMapLine);
  304. float ComputeV2XTrafficLightSpeed(iv::TrafficLight TrafficLight, const std::vector<GPSData> gpsMapLine,std::vector<int> v2xTrafficVector,
  305. int pathpoint,float secSpeed,float dSpeed,bool circleMode);
  306. float getTrafficLightSpeed(int traffic_light_color, int traffic_light_time, float dis);
  307. bool computeTrafficPass(float trafficDis,int trafficColor,float trafficTime,float realSecSpeed,float dSecSpeed);
  308. float computeTrafficSpeedLimt(float trafficDis);
  309. float computeAvoidX(int roadAim,int roadOri,GPSData gps,float vehWidth);
  310. bool adjuseultra();
  311. iv::Station getNearestStation(iv::GPS_INS now_gps_ins,std::vector<Station> station_n,std::vector<GPSData> gpsMap);
  312. void transferGpsMode2( const std::vector<GPSData> gpsMapLine);
  313. GPS_INS aim_gps_ins;
  314. GPS_INS chaoche_start_gps;
  315. bool is_arrivaled=false;
  316. double chaocheObsDis = 0;
  317. double preChaocheDis = 0;
  318. double aimObsSpeed = 0;
  319. double followSpeed = 30;
  320. double chaocheSpeed = 50;
  321. int checkChaoCheBackCounts = 0;
  322. float lastTorque=0;
  323. float splimit=-1;
  324. float ObsTimeSpan=-1;
  325. double ObsTimeStart=-1;
  326. double ObsTimeEnd=-1;
  327. float ObsTimeWidth=1500;
  328. std::vector<iv::TracePoint> planTrace;
  329. bool roadNowStart=true;
  330. private:
  331. iv::decition::Decition decision_reverseCar(iv::GPS_INS now_gps_ins);
  332. iv::decition::Decition decision_reversing(iv::GPS_INS now_gps_ins);
  333. iv::decition::Decition decision_reverseCircle(iv::GPS_INS now_gps_ins);
  334. iv::decition::Decition decision_reverseDirect(iv::GPS_INS now_gps_ins);
  335. iv::decition::Decition decision_reverseArr(iv::GPS_INS now_gps_ins);
  336. iv::decition::Decition decision_dRever(iv::GPS_INS now_gps_ins);
  337. iv::decition::Decition decision_dRever0(iv::GPS_INS now_gps_ins);
  338. iv::decition::Decition decision_dRever1(iv::GPS_INS now_gps_ins);
  339. iv::decition::Decition decision_dRever2(iv::GPS_INS now_gps_ins);
  340. iv::decition::Decition decision_dRever3(iv::GPS_INS now_gps_ins);
  341. iv::decition::Decition decision_dRever4(iv::GPS_INS now_gps_ins);
  342. void decision_firstRun(GPS_INS now_gps_ins,
  343. const std::vector<GPSData> & gpsMapLine);
  344. int decision_ParkCalc(GPS_INS now_gps_ins,iv::decition::Decition & gps_decition);
  345. void decision_readyZhucheMode(GPS_INS now_gps_ins,const std::vector<GPSData> & gpsMapLine);
  346. void decision_readyParkMode(GPS_INS now_gps_ins,const std::vector<GPSData> & gpsMapLine);
  347. void decision_speedctrl(iv::decition::Decition & gps_decition,const std::vector<GPSData> & gpsMapLine);
  348. };
  349. }
  350. #endif // IVDECISION_BRAIN_H