vehicle_upload.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. #include "vehicle_upload.h"
  2. #include <QFile>
  3. #include <QString>
  4. #include <QStringList>
  5. #include <math.h>
  6. #include <iomanip>
  7. #include <thread>
  8. #include "modulecomm.h"
  9. #include "rawpic.pb.h"
  10. #include "chassis.pb.h"
  11. #include "platform_feedback.pb.h"
  12. #include "gpsimu.pb.h"
  13. #include "math/gnss_coordinate_convert.h" //for calculate totalPathLength
  14. extern std::string gstrserverip;
  15. extern std::string gstruploadPort;
  16. extern std::string gstruploadInterval;
  17. extern std::string gstrid;
  18. extern std::string gstrplateNumber;
  19. extern std::string gvehicleType;
  20. extern double gspeedWantAvg; // m/s
  21. extern double gwaitTime; // s
  22. extern char stryamlpath[256];
  23. extern uint8_t gShift_Status;//3 p 4 r 5 n 6 d
  24. extern uint8_t gCtrlMode_Status; //0 auto 1 remote 2 stop 3 platform
  25. extern uint8_t gNavagationSwitch;
  26. #ifndef GLOBAL_GPS_POINT
  27. #define GLOBAL_GPS_POINT
  28. struct gGPSPoint
  29. {
  30. double latitude = 0.0;
  31. double longitude = 0.0;
  32. double height = 0.0;
  33. };
  34. #endif
  35. extern QVector<gGPSPoint> gPOIPoints;
  36. using org::jeecg::defsDetails::grpc::Empty; ///< other message
  37. using org::jeecg::defsDetails::grpc::GPSPoint;
  38. using org::jeecg::defsDetails::grpc::MapPoint;
  39. using org::jeecg::defsDetails::grpc::ShiftStatus; ///< other enum
  40. using org::jeecg::defsDetails::grpc::CtrlMode;
  41. DataExchangeClient::DataExchangeClient(std::shared_ptr<Channel> channel)
  42. {
  43. stub_ = DataExchange::NewStub(channel);
  44. dec_yaml(stryamlpath);
  45. QObject::connect(this,&DataExchangeClient::destination_Recieved,this,&DataExchangeClient::destination_Recieved_Slot);
  46. QObject::connect(this,&DataExchangeClient::path_Updated,this,&DataExchangeClient::path_Updated_Slot);
  47. QObject::connect(&destinationRefreshedTimer,&QTimer::timeout,this,&DataExchangeClient::destination_Refreshed_Timeout);
  48. shmXodrRequest.mpa = iv::modulecomm::RegisterSend(shmXodrRequest.mstrmsgname,shmXodrRequest.mnBufferSize,shmXodrRequest.mnBufferCount);
  49. ModuleFun funupdate = std::bind(&DataExchangeClient::ListenFrontPicMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  50. shmPicFront.mpa = iv::modulecomm::RegisterRecvPlus(shmPicFront.mstrmsgname,funupdate);
  51. funupdate = std::bind(&DataExchangeClient::ListenRearPicMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  52. shmPicRear.mpa = iv::modulecomm::RegisterRecvPlus(shmPicRear.mstrmsgname,funupdate);
  53. funupdate = std::bind(&DataExchangeClient::ListenLeftPicMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  54. shmPicLeft.mpa = iv::modulecomm::RegisterRecvPlus(shmPicLeft.mstrmsgname,funupdate);
  55. funupdate = std::bind(&DataExchangeClient::ListenRightPicMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  56. shmPicRight.mpa = iv::modulecomm::RegisterRecvPlus(shmPicRight.mstrmsgname,funupdate);
  57. funupdate = std::bind(&DataExchangeClient::ListenChassisMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  58. shmChassis.mpa = iv::modulecomm::RegisterRecvPlus(shmChassis.mstrmsgname,funupdate);
  59. funupdate = std::bind(&DataExchangeClient::ListenGPSIMUMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  60. shmGPSIMU.mpa = iv::modulecomm::RegisterRecvPlus(shmGPSIMU.mstrmsgname,funupdate);
  61. funupdate = std::bind(&DataExchangeClient::ListenPlatformFeedbackMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  62. shmPlatformFeedback.mpa = iv::modulecomm::RegisterRecvPlus(shmPlatformFeedback.mstrmsgname,funupdate);
  63. funupdate = std::bind(&DataExchangeClient::ListenTraceMapMsg,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  64. shmTraceMap.mpa = iv::modulecomm::RegisterRecvPlus(shmTraceMap.mstrmsgname,funupdate);
  65. id = gstrid;
  66. plateNumber = gstrplateNumber;
  67. switch (std::atoi(gvehicleType.c_str())) {
  68. case 0:
  69. vehicleType = org::jeecg::defsDetails::grpc::VehicleClass::RUN_ERRANDS;
  70. break;
  71. case 1:
  72. vehicleType = org::jeecg::defsDetails::grpc::VehicleClass::GUIDE;
  73. break;
  74. case 2:
  75. vehicleType = org::jeecg::defsDetails::grpc::VehicleClass::CLEAN;
  76. break;
  77. default:
  78. vehicleType = org::jeecg::defsDetails::grpc::VehicleClass::RUN_ERRANDS;
  79. break;
  80. }
  81. }
  82. DataExchangeClient::~DataExchangeClient(void)
  83. {
  84. if(shmXodrRequest.mpa != nullptr)iv::modulecomm::Unregister(shmXodrRequest.mpa);
  85. if(shmPicFront.mpa != nullptr)iv::modulecomm::Unregister(shmPicFront.mpa);
  86. if(shmPicRear.mpa != nullptr)iv::modulecomm::Unregister(shmPicRear.mpa);
  87. if(shmPicLeft.mpa != nullptr)iv::modulecomm::Unregister(shmPicLeft.mpa);
  88. if(shmPicRight.mpa != nullptr)iv::modulecomm::Unregister(shmPicRight.mpa);
  89. if(shmChassis.mpa != nullptr)iv::modulecomm::Unregister(shmChassis.mpa);
  90. if(shmGPSIMU.mpa != nullptr)iv::modulecomm::Unregister(shmGPSIMU.mpa);
  91. if(shmPlatformFeedback.mpa != nullptr)iv::modulecomm::Unregister(shmPlatformFeedback.mpa);
  92. if(shmTraceMap.mpa != nullptr)iv::modulecomm::Unregister(shmTraceMap.mpa);
  93. requestInterruption();
  94. while(this->isFinished() == false);
  95. }
  96. void DataExchangeClient::dec_yaml(const char *stryamlpath)
  97. {
  98. YAML::Node config;
  99. try
  100. {
  101. config = YAML::LoadFile(stryamlpath);
  102. }
  103. catch(YAML::BadFile &e)
  104. {
  105. std::cout<<e.what()<<std::endl;
  106. std::cout<<"yaml file load fail."<<std::endl;
  107. return;
  108. }
  109. catch(YAML::ParserException &e)
  110. {
  111. std::cout<<e.what()<<std::endl;
  112. std::cout<<"yaml file is malformed."<<std::endl;
  113. return;
  114. }
  115. std::string strmsgname;
  116. if(config["pic_front"])
  117. {
  118. if(config["pic_front"]["msgname"]&&config["pic_front"]["buffersize"]&&config["pic_front"]["buffercount"])
  119. {
  120. strmsgname = config["pic_front"]["msgname"].as<std::string>();
  121. strncpy(shmPicFront.mstrmsgname,strmsgname.data(),255);
  122. shmPicFront.mnBufferSize = config["pic_front"]["buffersize"].as<int>();
  123. shmPicFront.mnBufferCount = config["pic_front"]["buffercount"].as<int>();
  124. std::cout << "pic_front:" << shmPicFront.mstrmsgname << "," << shmPicFront.mnBufferSize << "," << shmPicFront.mnBufferCount << std::endl;
  125. }
  126. }
  127. else
  128. {
  129. strmsgname = "picfront";
  130. strncpy(shmPicFront.mstrmsgname,strmsgname.data(),255);
  131. shmPicFront.mnBufferSize = 10000000;
  132. shmPicFront.mnBufferCount = 1;
  133. }
  134. if(config["pic_rear"])
  135. {
  136. if(config["pic_rear"]["msgname"]&&config["pic_rear"]["buffersize"]&&config["pic_rear"]["buffercount"])
  137. {
  138. strmsgname = config["pic_rear"]["msgname"].as<std::string>();
  139. strncpy(shmPicRear.mstrmsgname,strmsgname.data(),255);
  140. shmPicRear.mnBufferSize = config["pic_rear"]["buffersize"].as<int>();
  141. shmPicRear.mnBufferCount = config["pic_rear"]["buffercount"].as<int>();
  142. std::cout << "pic_rear:" << shmPicRear.mstrmsgname << "," << shmPicRear.mnBufferSize << "," << shmPicRear.mnBufferCount << std::endl;
  143. }
  144. }
  145. else
  146. {
  147. strmsgname = "picrear";
  148. strncpy(shmPicRear.mstrmsgname,strmsgname.data(),255);
  149. shmPicRear.mnBufferSize = 10000000;
  150. shmPicRear.mnBufferCount = 1;
  151. }
  152. if(config["pic_left"])
  153. {
  154. if(config["pic_left"]["msgname"]&&config["pic_left"]["buffersize"]&&config["pic_left"]["buffercount"])
  155. {
  156. strmsgname = config["pic_left"]["msgname"].as<std::string>();
  157. strncpy(shmPicLeft.mstrmsgname,strmsgname.data(),255);
  158. shmPicLeft.mnBufferSize = config["pic_left"]["buffersize"].as<int>();
  159. shmPicLeft.mnBufferCount = config["pic_left"]["buffercount"].as<int>();
  160. std::cout << "pic_left:" << shmPicLeft.mstrmsgname << "," << shmPicLeft.mnBufferSize << "," << shmPicLeft.mnBufferCount << std::endl;
  161. }
  162. }
  163. else
  164. {
  165. strmsgname = "picleft";
  166. strncpy(shmPicLeft.mstrmsgname,strmsgname.data(),255);
  167. shmPicLeft.mnBufferSize = 10000000;
  168. shmPicLeft.mnBufferCount = 1;
  169. }
  170. if(config["pic_right"])
  171. {
  172. if(config["pic_right"]["msgname"]&&config["pic_right"]["buffersize"]&&config["pic_right"]["buffercount"])
  173. {
  174. strmsgname = config["pic_right"]["msgname"].as<std::string>();
  175. strncpy(shmPicRight.mstrmsgname,strmsgname.data(),255);
  176. shmPicRight.mnBufferSize = config["pic_right"]["buffersize"].as<int>();
  177. shmPicRight.mnBufferCount = config["pic_right"]["buffercount"].as<int>();
  178. std::cout << "pic_right:" << shmPicRight.mstrmsgname << "," << shmPicRight.mnBufferSize << "," << shmPicRight.mnBufferCount << std::endl;
  179. }
  180. }
  181. else
  182. {
  183. strmsgname = "picright";
  184. strncpy(shmPicRight.mstrmsgname,strmsgname.data(),255);
  185. shmPicRight.mnBufferSize = 10000000;
  186. shmPicRight.mnBufferCount = 1;
  187. }
  188. if(config["chassis"])
  189. {
  190. if(config["chassis"]["msgname"]&&config["chassis"]["buffersize"]&&config["chassis"]["buffercount"])
  191. {
  192. strmsgname = config["chassis"]["msgname"].as<std::string>();
  193. strncpy(shmChassis.mstrmsgname,strmsgname.data(),255);
  194. shmChassis.mnBufferSize = config["chassis"]["buffersize"].as<int>();
  195. shmChassis.mnBufferCount = config["chassis"]["buffercount"].as<int>();
  196. std::cout << "chassis:" << shmChassis.mstrmsgname << "," << shmChassis.mnBufferSize << "," << shmChassis.mnBufferCount << std::endl;
  197. }
  198. }
  199. else
  200. {
  201. strmsgname = "chassis";
  202. strncpy(shmChassis.mstrmsgname,strmsgname.data(),255);
  203. shmChassis.mnBufferSize = 10000;
  204. shmChassis.mnBufferCount = 1;
  205. }
  206. if(config["platform_feedback"])
  207. {
  208. if(config["platform_feedback"]["msgname"]&&config["platform_feedback"]["buffersize"]&&config["platform_feedback"]["buffercount"])
  209. {
  210. strmsgname = config["platform_feedback"]["msgname"].as<std::string>();
  211. strncpy(shmPlatformFeedback.mstrmsgname,strmsgname.data(),255);
  212. shmPlatformFeedback.mnBufferSize = config["platform_feedback"]["buffersize"].as<int>();
  213. shmPlatformFeedback.mnBufferCount = config["platform_feedback"]["buffercount"].as<int>();
  214. std::cout << "platform_feedback:" << shmPlatformFeedback.mstrmsgname << "," << shmPlatformFeedback.mnBufferSize << "," << shmPlatformFeedback.mnBufferCount << std::endl;
  215. }
  216. }
  217. else
  218. {
  219. strmsgname = "platformFeedback";
  220. strncpy(shmPlatformFeedback.mstrmsgname,strmsgname.data(),255);
  221. shmPlatformFeedback.mnBufferSize = 10000;
  222. shmPlatformFeedback.mnBufferCount = 1;
  223. }
  224. if(config["GPS_IMU"])
  225. {
  226. if(config["GPS_IMU"]["msgname"]&&config["GPS_IMU"]["buffersize"]&&config["GPS_IMU"]["buffercount"])
  227. {
  228. strmsgname = config["GPS_IMU"]["msgname"].as<std::string>();
  229. strncpy(shmGPSIMU.mstrmsgname,strmsgname.data(),255);
  230. shmGPSIMU.mnBufferSize = config["GPS_IMU"]["buffersize"].as<int>();
  231. shmGPSIMU.mnBufferCount = config["GPS_IMU"]["buffercount"].as<int>();
  232. std::cout << "GPS_IMU:" << shmGPSIMU.mstrmsgname << "," << shmGPSIMU.mnBufferSize << "," << shmGPSIMU.mnBufferCount << std::endl;
  233. }
  234. }
  235. else
  236. {
  237. strmsgname = "hcp2_gpsimu";
  238. strncpy(shmGPSIMU.mstrmsgname,strmsgname.data(),255);
  239. shmGPSIMU.mnBufferSize = 10000;
  240. shmGPSIMU.mnBufferCount = 1;
  241. }
  242. if(config["trace_map"])
  243. {
  244. if(config["trace_map"]["msgname"]&&config["trace_map"]["buffersize"]&&config["trace_map"]["buffercount"])
  245. {
  246. strmsgname = config["trace_map"]["msgname"].as<std::string>();
  247. strncpy(shmTraceMap.mstrmsgname,strmsgname.data(),255);
  248. shmTraceMap.mnBufferSize = config["trace_map"]["buffersize"].as<int>();
  249. shmTraceMap.mnBufferCount = config["trace_map"]["buffercount"].as<int>();
  250. std::cout << "trace_map:" << shmTraceMap.mstrmsgname << "," << shmTraceMap.mnBufferSize << "," << shmTraceMap.mnBufferCount << std::endl;
  251. }
  252. }
  253. else
  254. {
  255. strmsgname = "simpletrace";
  256. strncpy(shmTraceMap.mstrmsgname,strmsgname.data(),255);
  257. shmTraceMap.mnBufferSize = 100000;
  258. shmTraceMap.mnBufferCount = 1;
  259. }
  260. if(config["xodr_request"])
  261. {
  262. if(config["xodr_request"]["msgname"]&&config["xodr_request"]["buffersize"]&&config["xodr_request"]["buffercount"])
  263. {
  264. strmsgname = config["xodr_request"]["msgname"].as<std::string>();
  265. strncpy(shmXodrRequest.mstrmsgname,strmsgname.data(),255);
  266. shmXodrRequest.mnBufferSize = config["xodr_request"]["buffersize"].as<int>();
  267. shmXodrRequest.mnBufferCount = config["xodr_request"]["buffercount"].as<int>();
  268. std::cout << "xodr_request:" << shmXodrRequest.mstrmsgname << "," << shmXodrRequest.mnBufferSize << "," << shmXodrRequest.mnBufferCount << std::endl;
  269. }
  270. }
  271. else
  272. {
  273. strmsgname = "xodrreq";
  274. strncpy(shmXodrRequest.mstrmsgname,strmsgname.data(),255);
  275. shmXodrRequest.mnBufferSize = 1000;
  276. shmXodrRequest.mnBufferCount = 1;
  277. }
  278. return;
  279. }
  280. void DataExchangeClient::ListenFrontPicMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) // need a lock
  281. {
  282. iv::vision::rawpic xdata;
  283. if(!xdata.ParseFromArray(strdata,nSize))
  284. {
  285. std::cout<<" ListenFrontPicMsg parese error."<<std::endl;
  286. return;
  287. }
  288. gMutex_ImageFront.lock();
  289. cameraImageFront.clear();
  290. if(statusFeedback == VehicleStatus::STATUS_REMOTE && gShift_Status == 6)
  291. cameraImageFront.append(xdata.picdata().data(),xdata.picdata().size());
  292. else if(statusFeedback == VehicleStatus::STATUS_AUTO || statusFeedback == VehicleStatus::STATUS_EMERGENCY_STOP)
  293. cameraImageFront.append(xdata.picdata().data(),xdata.picdata().size());
  294. gMutex_ImageFront.unlock();
  295. }
  296. void DataExchangeClient::ListenRearPicMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) // need a lock
  297. {
  298. iv::vision::rawpic xdata;
  299. if(!xdata.ParseFromArray(strdata,nSize))
  300. {
  301. std::cout<<" ListenRearPicMsg parese error."<<std::endl;
  302. return;
  303. }
  304. gMutex_ImageRear.lock();
  305. cameraImageRear.clear();
  306. if(statusFeedback == VehicleStatus::STATUS_REMOTE && gShift_Status == 4)
  307. cameraImageRear.append(xdata.picdata().data(),xdata.picdata().size());
  308. else if(statusFeedback == VehicleStatus::STATUS_AUTO || statusFeedback == VehicleStatus::STATUS_EMERGENCY_STOP)
  309. cameraImageRear.append(xdata.picdata().data(),xdata.picdata().size());
  310. gMutex_ImageRear.unlock();
  311. }
  312. void DataExchangeClient::ListenLeftPicMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) // need a lock
  313. {
  314. iv::vision::rawpic xdata;
  315. if(!xdata.ParseFromArray(strdata,nSize))
  316. {
  317. std::cout<<" ListenLeftPicMsg parese error."<<std::endl;
  318. return;
  319. }
  320. gMutex_ImageLeft.lock();
  321. cameraImageLeft.clear();
  322. if(statusFeedback == VehicleStatus::STATUS_REMOTE && gShift_Status == 5)
  323. cameraImageLeft.append(xdata.picdata().data(),xdata.picdata().size());
  324. else if(statusFeedback == VehicleStatus::STATUS_AUTO || statusFeedback == VehicleStatus::STATUS_EMERGENCY_STOP)
  325. cameraImageLeft.append(xdata.picdata().data(),xdata.picdata().size());
  326. gMutex_ImageLeft.unlock();
  327. }
  328. void DataExchangeClient::ListenRightPicMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) // need a lock
  329. {
  330. iv::vision::rawpic xdata;
  331. if(!xdata.ParseFromArray(strdata,nSize))
  332. {
  333. std::cout<<" ListenRightPicMsg parese error."<<std::endl;
  334. return;
  335. }
  336. gMutex_ImageRight.lock();
  337. cameraImageRight.clear();
  338. if(statusFeedback == VehicleStatus::STATUS_REMOTE && gShift_Status == 5)
  339. cameraImageRight.append(xdata.picdata().data(),xdata.picdata().size());
  340. else if(statusFeedback == VehicleStatus::STATUS_AUTO || statusFeedback == VehicleStatus::STATUS_EMERGENCY_STOP)
  341. cameraImageRight.append(xdata.picdata().data(),xdata.picdata().size());
  342. gMutex_ImageRight.unlock();
  343. }
  344. void DataExchangeClient::ListenChassisMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) // need a lock
  345. {
  346. iv::chassis xdata;
  347. if(!xdata.ParseFromArray(strdata,nSize))
  348. {
  349. std::cout<<" ListenChassisMsg parese error."<<std::endl;
  350. return;
  351. }
  352. gMutex_Chassis.lock();
  353. SOC = xdata.soc();
  354. gMutex_Chassis.unlock();
  355. // std::cout<<SOC<<std::endl;
  356. }
  357. void DataExchangeClient::ListenGPSIMUMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) // need a lock
  358. {
  359. iv::gps::gpsimu xdata;
  360. if(!xdata.ParseFromArray(strdata,nSize))
  361. {
  362. std::cout<<" ListenGPSIMUMsg parese error."<<std::endl;
  363. return;
  364. }
  365. // double GPS_Speed = sqrt(xdata.ve()*xdata.ve()+xdata.vd()*xdata.vd()+xdata.vn()*xdata.vn());
  366. // if (GPS_Speed < 0.01)GPS_Speed = 0.0;
  367. gMutex_GPSIMU.lock();
  368. GPSRTKStatus = xdata.rtk_state();
  369. positionFeedback.set_latitude(xdata.lat());
  370. positionFeedback.set_longitude(xdata.lon());
  371. positionFeedback.set_height(xdata.height());
  372. pitch = xdata.pitch();
  373. roll = xdata.roll();
  374. heading = xdata.heading();
  375. speed = xdata.speed();
  376. // std::cout<<"speed: "<<speed<<std::endl;
  377. if(speed < 0.01)speed = 0.0;
  378. gMutex_GPSIMU.unlock();
  379. // std::cout<<heading<<std::endl;
  380. }
  381. void DataExchangeClient::ListenPlatformFeedbackMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) // need a lock
  382. {
  383. iv::platformFeedback xdata;
  384. if(!xdata.ParseFromArray(strdata,nSize))
  385. {
  386. std::cout<<" ListenPlatformFeedbackMsg parese error."<<std::endl;
  387. return;
  388. }
  389. gMutex_PlatformFeedback.lock();
  390. switch (xdata.typefeedback()) {
  391. case iv::platformFeedback::ctrlType::platformFeedback_ctrlType_AUTO:
  392. statusFeedback = VehicleStatus::STATUS_AUTO;
  393. modeFeedback = CtrlMode::CMD_AUTO;
  394. break;
  395. case iv::platformFeedback::ctrlType::platformFeedback_ctrlType_REMOTE:
  396. if(gCtrlMode_Status == 1) //remote
  397. {
  398. statusFeedback = VehicleStatus::STATUS_REMOTE;
  399. modeFeedback = CtrlMode::CMD_REMOTE;
  400. }
  401. else if(gCtrlMode_Status == 3) //platform
  402. {
  403. statusFeedback = VehicleStatus::STATUS_REMOTE;
  404. modeFeedback = CtrlMode::CMD_CLOUD_PLATFORM;
  405. }
  406. break;
  407. case iv::platformFeedback::ctrlType::platformFeedback_ctrlType_STOP:
  408. statusFeedback = VehicleStatus::STATUS_EMERGENCY_STOP;
  409. modeFeedback = CtrlMode::CMD_EMERGENCY_STOP;
  410. break;
  411. default:
  412. break;
  413. }
  414. switch (xdata.shift()) {
  415. case 0: //0N 1D 2R 3P
  416. shiftFeedback = ShiftStatus::SHIFT_NEUTRAL; //3P 4R 5N 6D
  417. gShift_Status = 5;
  418. break;
  419. case 1: //0N 1D 2R 3P
  420. shiftFeedback = ShiftStatus::SHIFT_DRIVE;
  421. gShift_Status = 6;
  422. break;
  423. case 2: //0N 1D 2R 3P
  424. shiftFeedback = ShiftStatus::SHIFT_REVERSE;
  425. gShift_Status = 4;
  426. break;
  427. case 3: //0N 1D 2R 3P
  428. shiftFeedback = ShiftStatus::SHIFT_PARKING;
  429. gShift_Status = 3;
  430. break;
  431. default:
  432. break;
  433. }
  434. throttleFeedback = xdata.throttle();
  435. brakeFeedback = xdata.brake();
  436. steeringWheelAngleFeedback = (-1) * xdata.steeringwheelangle();
  437. gMutex_PlatformFeedback.unlock();
  438. // std::cout<<throttleFeedback<<","<<brakeFeedback<<","<<steeringWheelAngleFeedback<<","<<shiftFeedback<<","<<modeFeedback<<std::endl;
  439. }
  440. void DataExchangeClient::ListenTraceMapMsg(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  441. {
  442. // recive trance map
  443. int npoint = nSize/sizeof(iv::simpletrace);
  444. if(npoint < 1)
  445. {
  446. std::cout<<"simple trace is very small"<<std::endl;
  447. return;
  448. }
  449. std::vector<iv::simpletrace> simpletrace;
  450. simpletrace.clear();
  451. simpletrace.resize(npoint);
  452. memcpy(simpletrace.data(),strdata,npoint*sizeof(iv::simpletrace));
  453. org::jeecg::defsDetails::grpc::MapPoint onePoint;
  454. pathPoints.clear();
  455. for(int i = 0;i < npoint;i++)
  456. {
  457. onePoint.set_index(i);
  458. onePoint.mutable_mappoint()->set_latitude(simpletrace[i].gps_lat);
  459. onePoint.mutable_mappoint()->set_longitude(simpletrace[i].gps_lng);
  460. pathPoints.append(onePoint);
  461. // std::cout<<pathPoints.at(i).index()<<std::endl;
  462. }
  463. std::cout<<"get a tracemap"<<std::endl;
  464. totalPathLength = 0.0;
  465. double localPositionX = 0.0;
  466. double localPositionNextX = 0.0;
  467. double localPositionY = 0.0;
  468. double localPositionNextY = 0.0;
  469. for(int i = 0;i < npoint - 1;i++)
  470. {
  471. GaussProjCal(simpletrace[i].gps_lng,simpletrace[i].gps_lat,&localPositionX,&localPositionY);
  472. GaussProjCal(simpletrace[i+1].gps_lng,simpletrace[i+1].gps_lat,&localPositionNextX,&localPositionNextY);
  473. double deltaX = localPositionNextX - localPositionX;
  474. double deltaY = localPositionNextY - localPositionY;
  475. totalPathLength += sqrt(deltaX*deltaX + deltaY*deltaY);
  476. }
  477. std::cout<<"point number: "<<npoint<<",total length: "<<totalPathLength<<std::endl;
  478. arrivedTime = totalPathLength/gspeedWantAvg;
  479. waitTime = gwaitTime;
  480. if(destinationRefreshedTimer.isActive() == true)
  481. emit path_Updated();
  482. }
  483. std::string DataExchangeClient::uploadVehicleInfo(void)
  484. {
  485. // Data we are sending to the server.
  486. UplinkRequest request;
  487. request.set_id(id);
  488. request.set_timestamp(timeStamp);
  489. gMutex_Chassis.lock();
  490. request.set_soc(SOC);
  491. gMutex_Chassis.unlock();
  492. gMutex_PlatformFeedback.lock();
  493. request.set_statusfeedback(statusFeedback);//statusFeedback
  494. request.set_modefeedback(modeFeedback);//modeFeedback
  495. request.set_shiftfeedback(shiftFeedback);
  496. request.set_steeringwheelanglefeedback(steeringWheelAngleFeedback);
  497. request.set_throttlefeedback(throttleFeedback);
  498. request.set_brakefeedback(brakeFeedback);
  499. gMutex_PlatformFeedback.unlock();
  500. gMutex_GPSIMU.lock();
  501. request.set_mileage(mileage);
  502. request.set_speed(speed);
  503. request.set_gpsrtkstatus(GPSRTKStatus);
  504. request.mutable_positionfeedback()->CopyFrom(positionFeedback);
  505. request.set_pitch(pitch);
  506. request.set_roll(roll);
  507. request.set_heading(heading);
  508. gMutex_GPSIMU.unlock();
  509. // std::cout<<std::setprecision(12)<<"vehicle position: "<<positionFeedback.latitude()<<","<<positionFeedback.longitude()<<std::endl;
  510. // gMutex_ImageFront.lock();
  511. // request.set_cameraimagefront(cameraImageFront.data(),cameraImageFront.size());
  512. // gMutex_ImageFront.unlock();
  513. // gMutex_ImageRear.lock();
  514. // request.set_cameraimagerear(cameraImageRear.data(),cameraImageRear.size());
  515. // gMutex_ImageRear.unlock();
  516. // gMutex_ImageLeft.lock();
  517. // request.set_cameraimageleft(cameraImageLeft.data(),cameraImageLeft.size());
  518. // gMutex_ImageLeft.unlock();
  519. // gMutex_ImageRight.lock();
  520. // request.set_cameraimageright(cameraImageRight.data(),cameraImageRight.size());
  521. // gMutex_ImageRight.unlock();
  522. request.set_sensorstatusgpsimu(sensorStatusGPSIMU);
  523. request.set_sensorstatuslidar(sensorStatusLidar);
  524. request.set_sensorstatusradar(sensorStatusRadar);
  525. request.set_sensorstatuscamfront(sensorStatusCamFront);
  526. request.set_sensorstatuscamrear(sensorStatusCamRear);
  527. request.set_sensorstatuscamleft(sensorStatusCamLeft);
  528. request.set_sensorstatuscamright(sensorStatusCamRight);
  529. if(isArrived == 1)
  530. {
  531. if(gNavagationSwitch == 0)
  532. request.set_isarrived(0);
  533. if(gNavagationSwitch == 1)
  534. request.set_isarrived(isArrived);
  535. }
  536. else
  537. request.set_isarrived(isArrived);
  538. // std::cout<<"isarrived: "<<isArrived<<std::endl;
  539. request.set_platenumber(plateNumber);
  540. request.set_usestatusfeedback(useStatus);
  541. request.set_remainpathlength(remainPathLength);
  542. request.set_classfeedback(vehicleType);
  543. // Container for the data we expect from the server.
  544. ResponseMessage reply;
  545. // Context for the client. It could be used to convey extra information to
  546. // the server and/or tweak certain RPC behaviors.
  547. ClientContext context;
  548. gpr_timespec timespec;
  549. timespec.tv_sec = 2;
  550. timespec.tv_nsec = 0;
  551. timespec.clock_type = GPR_TIMESPAN;
  552. context.set_deadline(timespec);
  553. // The actual RPC.
  554. Status status = stub_ -> uploadVehicleInfo(&context, request, &reply);
  555. // Act upon its status.
  556. if (status.ok()) {
  557. destinationPosition.CopyFrom(reply.destinationposition());
  558. emit destination_Recieved();
  559. // std::cout<<std::setprecision(12)<<"lat:"<<reply.destinationposition().latitude()<<"lon:"<<reply.destinationposition().longitude()<<"height:"<<reply.destinationposition().height()<<std::endl;
  560. return "uploadVehicleInfo RPC successed";
  561. } else {
  562. std::cout << status.error_code() << ": " << status.error_message()
  563. << std::endl;
  564. if(status.error_code() == 4)
  565. {
  566. std::cout << "vehicleControl RPC connect timeout" << std::endl;
  567. }
  568. return "uploadVehicleInfo RPC failed";
  569. }
  570. }
  571. std::string DataExchangeClient::uploadPath(void)
  572. {
  573. // Data we are sending to the server.
  574. UploadPathRequest request;
  575. request.set_id(id);
  576. request.set_patrolpathid(patrolPathID);
  577. for(int i = 0;i < pathPoints.size();i++)
  578. {
  579. request.add_pathpoints();
  580. request.mutable_pathpoints(i)->operator =(pathPoints.at(i));
  581. }
  582. request.set_arrivedtime(arrivedTime);
  583. request.set_waittime(waitTime);
  584. request.set_totalpathlength(totalPathLength);
  585. // Container for the data we expect from the server.
  586. Empty reply;
  587. // Context for the client. It could be used to convey extra information to
  588. // the server and/or tweak certain RPC behaviors.
  589. ClientContext context;
  590. gpr_timespec timespec;
  591. timespec.tv_sec = 5;
  592. timespec.tv_nsec = 0;
  593. timespec.clock_type = GPR_TIMESPAN;
  594. context.set_deadline(timespec);
  595. // The actual RPC.
  596. Status status = stub_ -> uploadPath(&context,request,&reply);
  597. // Act upon its status.
  598. if (status.ok()) {
  599. if(reply.id() == gstrid)
  600. {
  601. std::cout<<"Path uploaded by car id:"<<reply.id()<<std::endl;
  602. emit uploadPath_Finished(patrolPathID);
  603. }
  604. return "uploadPath RPC successed";
  605. } else {
  606. std::cout << status.error_code() << ": " << status.error_message()
  607. << std::endl;
  608. if(status.error_code() == 4)
  609. {
  610. std::cout << "vehicleControl RPC connect timeout" << std::endl;
  611. }
  612. return "uploadPath RPC failed";
  613. }
  614. }
  615. void DataExchangeClient::updateData(uint64_t timeInterval_ms)
  616. {
  617. org::jeecg::defsDetails::grpc::GPSPoint tempPosition;
  618. timeStamp = QDateTime::currentMSecsSinceEpoch();
  619. // SOC = 87.5;
  620. // statusFeedback = VehicleStatus::STATUS_REMOTE;
  621. gMutex_GPSIMU.lock();
  622. mileage += (speed * timeInterval_ms/1000.0)/1000.0; // kilometer
  623. tempPosition.CopyFrom(positionFeedback);
  624. // std::cout<<std::setprecision(12)<<tempPosition.latitude()<<","<<tempPosition.longitude()<<std::endl;
  625. gMutex_GPSIMU.unlock();
  626. // speed = 0.0; // m/s
  627. // shiftFeedback = ShiftStatus::SHIFT_DRIVE;
  628. // steeringWheelAngleFeedback = 1.23; //+/-540 degree
  629. // throttleFeedback = 0.12;
  630. // brakeFeedback = 0.34;
  631. // GPSRTKStatus = 6; //GPS-RTK status 0-6 6 is best
  632. // positionFeedback.set_latitude(39.0666552);
  633. // positionFeedback.set_longitude(117.3540963);
  634. // positionFeedback.set_height(0.84);
  635. // pitch = 0.03;
  636. // roll = 0.02;
  637. // heading = 198.4;
  638. // QFile xFile;
  639. // xFile.setFileName("/home/nvidia/Pictures/123.jpg");
  640. // if(xFile.open(QIODevice::ReadOnly))
  641. // {
  642. // cameraImageFront = xFile.readAll();
  643. // }
  644. // xFile.close();
  645. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  646. // if(xFile.open(QIODevice::ReadOnly))
  647. // {
  648. // cameraImageRear = xFile.readAll();
  649. // }
  650. // xFile.close();
  651. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  652. // if(xFile.open(QIODevice::ReadOnly))
  653. // {
  654. // cameraImageLeft = xFile.readAll();
  655. // }
  656. // xFile.close();
  657. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  658. // if(xFile.open(QIODevice::ReadOnly))
  659. // {
  660. // cameraImageRight = xFile.readAll();
  661. // }
  662. // xFile.close();
  663. sensorStatusGPSIMU = false; //0 GPS-IMU ok 1 GPS-IMU error
  664. sensorStatusLidar = false;
  665. sensorStatusRadar = false;
  666. sensorStatusCamFront = false;
  667. sensorStatusCamRear = false;
  668. sensorStatusCamLeft = false;
  669. sensorStatusCamRight = false;
  670. useStatus = org::jeecg::defsDetails::grpc::UseStatus::ENABLING;
  671. if(useStatus == org::jeecg::defsDetails::grpc::UseStatus::DEACTIVATING)
  672. {
  673. destinationSaved.Clear();
  674. remainPathLength = 0.0;
  675. isArrived = 0;
  676. }
  677. remainPathLength -= (speed * timeInterval_ms/1000.0);
  678. if(remainPathLength > 0.0)std::cout<<"remainLength: "<<remainPathLength<<std::endl;
  679. if(remainPathLength <= 0.0)
  680. remainPathLength = 0.0;
  681. if(isArrived == 1)
  682. {
  683. double localPositionX = 0.0;
  684. double localPositionNextX = 0.0;
  685. double localPositionY = 0.0;
  686. double localPositionNextY = 0.0;
  687. GaussProjCal(tempPosition.longitude(),tempPosition.latitude(),&localPositionX,&localPositionY);
  688. GaussProjCal(destinationSaved.longitude(),destinationSaved.latitude(),&localPositionNextX,&localPositionNextY);
  689. double deltaX = localPositionNextX - localPositionX;
  690. double deltaY = localPositionNextY - localPositionY;
  691. double distance = sqrt(deltaX*deltaX + deltaY*deltaY);
  692. if(distance < 20.0)
  693. std::cout<<"distance: "<<distance<<std::endl;
  694. if(distance < 1.5)
  695. {
  696. isArrived = 2;
  697. remainPathLength = 0.0;
  698. destinationSaved.Clear();
  699. std::cout<<"destination has arrived"<<std::endl;
  700. }
  701. }
  702. // std::cout<<"is arrived: "<<isArrived<<std::endl;
  703. }
  704. void DataExchangeClient::updatePath(std::string pathID, QVector<MapPoint> points)
  705. {
  706. id = gstrid;
  707. patrolPathID = pathID;
  708. pathPoints.clear();
  709. for(int i = 0;i < points.size();i++)
  710. {
  711. pathPoints.append(points.value(i));
  712. // std::cout<<pathPoints.at(i).index()<<std::endl;
  713. }
  714. }
  715. void DataExchangeClient::run()
  716. {
  717. QTime xTime;
  718. xTime.start();
  719. QFile mileageFile;
  720. mileageFile.setFileName("./mileage.txt");
  721. int fileWriteTime = xTime.elapsed();
  722. if(mileageFile.exists())
  723. {
  724. if(mileageFile.open(QIODevice::ReadOnly | QIODevice::Text))
  725. {
  726. mileage = mileageFile.readAll().toDouble();
  727. std::cout<<"file open success. mileage is "<<mileage<<std::endl;
  728. }
  729. mileageFile.close();
  730. }
  731. else
  732. {
  733. if(mileageFile.open(QIODevice::WriteOnly | QIODevice::Text))
  734. {
  735. QString strMileage = QString::number(mileage,'f',4);
  736. mileageFile.write(strMileage.toUtf8());
  737. std::cout<<"file not exist. mileage has been cleared. "<<mileage<<std::endl;
  738. }
  739. mileageFile.flush();
  740. mileageFile.close();
  741. }
  742. int lastTime = xTime.elapsed();
  743. int realLastTime = xTime.elapsed();
  744. uint64_t interval = std::atoi(gstruploadInterval.c_str());
  745. while (!QThread::isInterruptionRequested())
  746. {
  747. if(abs(xTime.elapsed() - lastTime)>=interval)
  748. {
  749. uint64_t realInterval = abs(xTime.elapsed() - realLastTime);
  750. realLastTime = xTime.elapsed();
  751. updateData(realInterval);
  752. std::string reply = uploadVehicleInfo();
  753. // std::cout<< reply <<std::endl;
  754. // std::cout<<std::setprecision(12)<<destinationPosition.latitude()<<","<<destinationPosition.longitude()<<std::endl;
  755. lastTime = xTime.elapsed();
  756. }
  757. else
  758. {
  759. std::this_thread::sleep_for(std::chrono::microseconds(500));///< sleep 0.5ms
  760. }
  761. if(abs(xTime.elapsed() - fileWriteTime) >= 2500)
  762. {
  763. if(mileageFile.open(QIODevice::WriteOnly | QIODevice::Text))
  764. {
  765. QString strMileage = QString::number(mileage,'f',4);
  766. mileageFile.write(strMileage.toUtf8());
  767. }
  768. mileageFile.flush();
  769. mileageFile.close();
  770. fileWriteTime = xTime.elapsed();
  771. }
  772. else
  773. {
  774. std::this_thread::sleep_for(std::chrono::microseconds(500));///< sleep 0.5ms
  775. }
  776. }
  777. }
  778. void DataExchangeClient::patrolPOI_Recieved_Slot(std::string pathID)
  779. {
  780. isArrived = 0;
  781. remainPathLength = 0.0;
  782. destinationSaved.Clear();
  783. patrolPathID = pathID;
  784. xodrobj xodrDest;
  785. xodrDest.flon = gPOIPoints.at(1).longitude;
  786. xodrDest.flat = gPOIPoints.at(1).latitude;
  787. xodrDest.lane = 1;
  788. std::cout<<"destination: "<<std::setprecision(12)<<gPOIPoints.at(1).longitude<<","<<gPOIPoints.at(1).latitude<<std::endl;
  789. iv::modulecomm::ModuleSendMsg(shmXodrRequest.mpa,(char *)&xodrDest,sizeof(xodrobj)); ///< send request msg
  790. // std::cout<<"modulesend xodr request"<<std::endl;
  791. if(destinationRefreshedTimer.isActive() == false)destinationRefreshedTimer.start(10000);
  792. std::cout<<"patrol path calculating"<<std::endl;
  793. // QFile mapfile;
  794. // mapfile.setFileName("/home/nvidia/Documents/path2.txt");
  795. // QVector<org::jeecg::defsDetails::grpc::MapPoint> somePoints;
  796. // if(mapfile.open(QIODevice::ReadOnly | QIODevice::Text))
  797. // {
  798. // while(!mapfile.atEnd())
  799. // {
  800. // QByteArray line = mapfile.readLine();
  801. // QString map_str(line);
  802. // QStringList oneline = map_str.split(",");
  803. // org::jeecg::defsDetails::grpc::MapPoint onePoint;
  804. // onePoint.set_index(oneline.at(0).toInt());
  805. // onePoint.mutable_mappoint()->set_longitude(oneline.at(1).toDouble());
  806. // onePoint.mutable_mappoint()->set_latitude(oneline.at(2).toDouble());
  807. // onePoint.mutable_mappoint()->set_height(oneline.at(3).toDouble());
  808. // somePoints.append(onePoint);
  809. // }
  810. // }
  811. // updatePath(pathID,somePoints);
  812. // uploadPath();
  813. }
  814. void DataExchangeClient::destination_Recieved_Slot(void)
  815. {
  816. if(fabs(destinationPosition.latitude()) > 0.000001 \
  817. && fabs(destinationPosition.longitude()) > 0.000001)
  818. {
  819. }
  820. }
  821. void DataExchangeClient::path_Updated_Slot(void)
  822. {
  823. if(destinationRefreshedTimer.isActive() == true)
  824. destinationRefreshedTimer.stop();
  825. destinationSaved.set_latitude(pathPoints.at(pathPoints.size()-1).mappoint().latitude());
  826. destinationSaved.set_longitude(pathPoints.at(pathPoints.size()-1).mappoint().longitude());
  827. destinationSaved.set_height(0.0);
  828. remainPathLength = totalPathLength;
  829. isArrived = 1; ///< 0 no path 1 not arrived 2 arrived
  830. uploadPath();
  831. }
  832. void DataExchangeClient::destination_Refreshed_Timeout(void)
  833. {
  834. if(destinationRefreshedTimer.isActive() == true)
  835. destinationRefreshedTimer.stop();
  836. destinationSaved.Clear();
  837. remainPathLength = 0.0;
  838. isArrived = 0;
  839. emit uploadPath_Finished("");
  840. std::cout<<"path plan timeout."<<std::endl;
  841. }