vehicle_upload.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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. emit setFSMDestination(pathPoints.at(pathPoints.size()-1).mappoint().latitude(),pathPoints.at(pathPoints.size()-1).mappoint().longitude());
  464. std::cout<<"get a tracemap"<<std::endl;
  465. totalPathLength = 0.0;
  466. double localPositionX = 0.0;
  467. double localPositionNextX = 0.0;
  468. double localPositionY = 0.0;
  469. double localPositionNextY = 0.0;
  470. for(int i = 0;i < npoint - 1;i++)
  471. {
  472. GaussProjCal(simpletrace[i].gps_lng,simpletrace[i].gps_lat,&localPositionX,&localPositionY);
  473. GaussProjCal(simpletrace[i+1].gps_lng,simpletrace[i+1].gps_lat,&localPositionNextX,&localPositionNextY);
  474. double deltaX = localPositionNextX - localPositionX;
  475. double deltaY = localPositionNextY - localPositionY;
  476. totalPathLength += sqrt(deltaX*deltaX + deltaY*deltaY);
  477. }
  478. std::cout<<"point number: "<<npoint<<",total length: "<<totalPathLength<<std::endl;
  479. arrivedTime = totalPathLength/gspeedWantAvg;
  480. waitTime = gwaitTime;
  481. if(destinationRefreshedTimer.isActive() == true)
  482. emit path_Updated();
  483. }
  484. std::string DataExchangeClient::uploadVehicleInfo(void)
  485. {
  486. // Data we are sending to the server.
  487. UplinkRequest request;
  488. request.set_id(id);
  489. request.set_timestamp(timeStamp);
  490. gMutex_Chassis.lock();
  491. request.set_soc(SOC);
  492. gMutex_Chassis.unlock();
  493. gMutex_PlatformFeedback.lock();
  494. request.set_statusfeedback(statusFeedback);//statusFeedback
  495. request.set_modefeedback(modeFeedback);//modeFeedback
  496. request.set_shiftfeedback(shiftFeedback);
  497. request.set_steeringwheelanglefeedback(steeringWheelAngleFeedback);
  498. request.set_throttlefeedback(throttleFeedback);
  499. request.set_brakefeedback(brakeFeedback);
  500. gMutex_PlatformFeedback.unlock();
  501. gMutex_GPSIMU.lock();
  502. request.set_mileage(mileage);
  503. request.set_speed(speed);
  504. request.set_gpsrtkstatus(GPSRTKStatus);
  505. request.mutable_positionfeedback()->CopyFrom(positionFeedback);
  506. request.set_pitch(pitch);
  507. request.set_roll(roll);
  508. request.set_heading(heading);
  509. gMutex_GPSIMU.unlock();
  510. // std::cout<<std::setprecision(12)<<"vehicle position: "<<positionFeedback.latitude()<<","<<positionFeedback.longitude()<<std::endl;
  511. // gMutex_ImageFront.lock();
  512. // request.set_cameraimagefront(cameraImageFront.data(),cameraImageFront.size());
  513. // gMutex_ImageFront.unlock();
  514. // gMutex_ImageRear.lock();
  515. // request.set_cameraimagerear(cameraImageRear.data(),cameraImageRear.size());
  516. // gMutex_ImageRear.unlock();
  517. // gMutex_ImageLeft.lock();
  518. // request.set_cameraimageleft(cameraImageLeft.data(),cameraImageLeft.size());
  519. // gMutex_ImageLeft.unlock();
  520. // gMutex_ImageRight.lock();
  521. // request.set_cameraimageright(cameraImageRight.data(),cameraImageRight.size());
  522. // gMutex_ImageRight.unlock();
  523. request.set_sensorstatusgpsimu(sensorStatusGPSIMU);
  524. request.set_sensorstatuslidar(sensorStatusLidar);
  525. request.set_sensorstatusradar(sensorStatusRadar);
  526. request.set_sensorstatuscamfront(sensorStatusCamFront);
  527. request.set_sensorstatuscamrear(sensorStatusCamRear);
  528. request.set_sensorstatuscamleft(sensorStatusCamLeft);
  529. request.set_sensorstatuscamright(sensorStatusCamRight);
  530. if(isArrived == 1)
  531. {
  532. if(gNavagationSwitch == 0)
  533. request.set_isarrived(0);
  534. if(gNavagationSwitch == 1)
  535. request.set_isarrived(isArrived);
  536. }
  537. else
  538. request.set_isarrived(isArrived);
  539. // std::cout<<"isarrived: "<<isArrived<<std::endl;
  540. request.set_platenumber(plateNumber);
  541. request.set_usestatusfeedback(useStatus);
  542. request.set_remainpathlength(remainPathLength);
  543. request.set_classfeedback(vehicleType);
  544. // Container for the data we expect from the server.
  545. ResponseMessage reply;
  546. // Context for the client. It could be used to convey extra information to
  547. // the server and/or tweak certain RPC behaviors.
  548. ClientContext context;
  549. gpr_timespec timespec;
  550. timespec.tv_sec = 2;
  551. timespec.tv_nsec = 0;
  552. timespec.clock_type = GPR_TIMESPAN;
  553. context.set_deadline(timespec);
  554. // The actual RPC.
  555. Status status = stub_ -> uploadVehicleInfo(&context, request, &reply);
  556. // Act upon its status.
  557. if (status.ok()) {
  558. destinationPosition.CopyFrom(reply.destinationposition());
  559. emit destination_Recieved();
  560. // std::cout<<std::setprecision(12)<<"lat:"<<reply.destinationposition().latitude()<<"lon:"<<reply.destinationposition().longitude()<<"height:"<<reply.destinationposition().height()<<std::endl;
  561. return "uploadVehicleInfo RPC successed";
  562. } else {
  563. std::cout << status.error_code() << ": " << status.error_message()
  564. << std::endl;
  565. if(status.error_code() == 4)
  566. {
  567. std::cout << "vehicleControl RPC connect timeout" << std::endl;
  568. }
  569. return "uploadVehicleInfo RPC failed";
  570. }
  571. }
  572. std::string DataExchangeClient::uploadPath(void)
  573. {
  574. // Data we are sending to the server.
  575. UploadPathRequest request;
  576. request.set_id(id);
  577. request.set_patrolpathid(patrolPathID);
  578. for(int i = 0;i < pathPoints.size();i++)
  579. {
  580. request.add_pathpoints();
  581. request.mutable_pathpoints(i)->operator =(pathPoints.at(i));
  582. }
  583. request.set_arrivedtime(arrivedTime);
  584. request.set_waittime(waitTime);
  585. request.set_totalpathlength(totalPathLength);
  586. // Container for the data we expect from the server.
  587. Empty reply;
  588. // Context for the client. It could be used to convey extra information to
  589. // the server and/or tweak certain RPC behaviors.
  590. ClientContext context;
  591. gpr_timespec timespec;
  592. timespec.tv_sec = 5;
  593. timespec.tv_nsec = 0;
  594. timespec.clock_type = GPR_TIMESPAN;
  595. context.set_deadline(timespec);
  596. // The actual RPC.
  597. Status status = stub_ -> uploadPath(&context,request,&reply);
  598. // Act upon its status.
  599. if (status.ok()) {
  600. if(reply.id() == gstrid)
  601. {
  602. std::cout<<"Path uploaded by car id:"<<reply.id()<<std::endl;
  603. emit uploadPath_Finished(patrolPathID);
  604. }
  605. return "uploadPath RPC successed";
  606. } else {
  607. std::cout << status.error_code() << ": " << status.error_message()
  608. << std::endl;
  609. if(status.error_code() == 4)
  610. {
  611. std::cout << "vehicleControl RPC connect timeout" << std::endl;
  612. }
  613. return "uploadPath RPC failed";
  614. }
  615. }
  616. void DataExchangeClient::updateData(uint64_t timeInterval_ms)
  617. {
  618. org::jeecg::defsDetails::grpc::GPSPoint tempPosition;
  619. timeStamp = QDateTime::currentMSecsSinceEpoch();
  620. // SOC = 87.5;
  621. // statusFeedback = VehicleStatus::STATUS_REMOTE;
  622. gMutex_GPSIMU.lock();
  623. mileage += (speed * timeInterval_ms/1000.0)/1000.0; // kilometer
  624. tempPosition.CopyFrom(positionFeedback);
  625. // std::cout<<std::setprecision(12)<<tempPosition.latitude()<<","<<tempPosition.longitude()<<std::endl;
  626. gMutex_GPSIMU.unlock();
  627. // speed = 0.0; // m/s
  628. // shiftFeedback = ShiftStatus::SHIFT_DRIVE;
  629. // steeringWheelAngleFeedback = 1.23; //+/-540 degree
  630. // throttleFeedback = 0.12;
  631. // brakeFeedback = 0.34;
  632. // GPSRTKStatus = 6; //GPS-RTK status 0-6 6 is best
  633. // positionFeedback.set_latitude(39.0666552);
  634. // positionFeedback.set_longitude(117.3540963);
  635. // positionFeedback.set_height(0.84);
  636. // pitch = 0.03;
  637. // roll = 0.02;
  638. // heading = 198.4;
  639. // QFile xFile;
  640. // xFile.setFileName("/home/nvidia/Pictures/123.jpg");
  641. // if(xFile.open(QIODevice::ReadOnly))
  642. // {
  643. // cameraImageFront = xFile.readAll();
  644. // }
  645. // xFile.close();
  646. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  647. // if(xFile.open(QIODevice::ReadOnly))
  648. // {
  649. // cameraImageRear = xFile.readAll();
  650. // }
  651. // xFile.close();
  652. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  653. // if(xFile.open(QIODevice::ReadOnly))
  654. // {
  655. // cameraImageLeft = xFile.readAll();
  656. // }
  657. // xFile.close();
  658. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  659. // if(xFile.open(QIODevice::ReadOnly))
  660. // {
  661. // cameraImageRight = xFile.readAll();
  662. // }
  663. // xFile.close();
  664. sensorStatusGPSIMU = false; //0 GPS-IMU ok 1 GPS-IMU error
  665. sensorStatusLidar = false;
  666. sensorStatusRadar = false;
  667. sensorStatusCamFront = false;
  668. sensorStatusCamRear = false;
  669. sensorStatusCamLeft = false;
  670. sensorStatusCamRight = false;
  671. // useStatus = org::jeecg::defsDetails::grpc::UseStatus::ENABLING;
  672. if(useStatus == org::jeecg::defsDetails::grpc::UseStatus::DEACTIVATING)
  673. {
  674. destinationSaved.Clear();
  675. remainPathLength = 0.0;
  676. isArrived = 0;
  677. }
  678. remainPathLength -= (speed * timeInterval_ms/1000.0);
  679. if(remainPathLength > 0.0)std::cout<<"remainLength: "<<remainPathLength<<std::endl;
  680. if(remainPathLength <= 0.0)
  681. remainPathLength = 0.0;
  682. if(isArrived == 1)
  683. {
  684. double localPositionX = 0.0;
  685. double localPositionNextX = 0.0;
  686. double localPositionY = 0.0;
  687. double localPositionNextY = 0.0;
  688. GaussProjCal(tempPosition.longitude(),tempPosition.latitude(),&localPositionX,&localPositionY);
  689. GaussProjCal(destinationSaved.longitude(),destinationSaved.latitude(),&localPositionNextX,&localPositionNextY);
  690. double deltaX = localPositionNextX - localPositionX;
  691. double deltaY = localPositionNextY - localPositionY;
  692. double distance = sqrt(deltaX*deltaX + deltaY*deltaY);
  693. // if(distance < 20.0)
  694. // std::cout<<"distance: "<<distance<<std::endl;
  695. if(distance < 1.5)
  696. {
  697. isArrived = 2;
  698. remainPathLength = 0.0;
  699. destinationSaved.Clear();
  700. emit setMissionFinished();
  701. std::cout<<"destination has arrived"<<std::endl;
  702. }
  703. }
  704. // std::cout<<"is arrived: "<<isArrived<<std::endl;
  705. }
  706. void DataExchangeClient::updatePath(std::string pathID, QVector<MapPoint> points)
  707. {
  708. id = gstrid;
  709. patrolPathID = pathID;
  710. pathPoints.clear();
  711. for(int i = 0;i < points.size();i++)
  712. {
  713. pathPoints.append(points.value(i));
  714. // std::cout<<pathPoints.at(i).index()<<std::endl;
  715. }
  716. }
  717. void DataExchangeClient::run()
  718. {
  719. QTime xTime;
  720. xTime.start();
  721. QFile mileageFile;
  722. mileageFile.setFileName("./mileage.txt");
  723. int fileWriteTime = xTime.elapsed();
  724. if(mileageFile.exists())
  725. {
  726. if(mileageFile.open(QIODevice::ReadOnly | QIODevice::Text))
  727. {
  728. mileage = mileageFile.readAll().toDouble();
  729. std::cout<<"file open success. mileage is "<<mileage<<std::endl;
  730. }
  731. mileageFile.close();
  732. }
  733. else
  734. {
  735. if(mileageFile.open(QIODevice::WriteOnly | QIODevice::Text))
  736. {
  737. QString strMileage = QString::number(mileage,'f',4);
  738. mileageFile.write(strMileage.toUtf8());
  739. std::cout<<"file not exist. mileage has been cleared. "<<mileage<<std::endl;
  740. }
  741. mileageFile.flush();
  742. mileageFile.close();
  743. }
  744. int lastTime = xTime.elapsed();
  745. int realLastTime = xTime.elapsed();
  746. uint64_t interval = std::atoi(gstruploadInterval.c_str());
  747. while (!QThread::isInterruptionRequested())
  748. {
  749. if(abs(xTime.elapsed() - lastTime)>=interval)
  750. {
  751. uint64_t realInterval = abs(xTime.elapsed() - realLastTime);
  752. realLastTime = xTime.elapsed();
  753. updateData(realInterval);
  754. std::string reply = uploadVehicleInfo();
  755. // std::cout<< reply <<std::endl;
  756. // std::cout<<std::setprecision(12)<<destinationPosition.latitude()<<","<<destinationPosition.longitude()<<std::endl;
  757. lastTime = xTime.elapsed();
  758. }
  759. else
  760. {
  761. std::this_thread::sleep_for(std::chrono::microseconds(500));///< sleep 0.5ms
  762. }
  763. if(abs(xTime.elapsed() - fileWriteTime) >= 2500)
  764. {
  765. if(mileageFile.open(QIODevice::WriteOnly | QIODevice::Text))
  766. {
  767. QString strMileage = QString::number(mileage,'f',4);
  768. mileageFile.write(strMileage.toUtf8());
  769. }
  770. mileageFile.flush();
  771. mileageFile.close();
  772. fileWriteTime = xTime.elapsed();
  773. }
  774. else
  775. {
  776. std::this_thread::sleep_for(std::chrono::microseconds(500));///< sleep 0.5ms
  777. }
  778. }
  779. }
  780. void DataExchangeClient::patrolPOI_Recieved_Slot(std::string pathID)
  781. {
  782. isArrived = 0;
  783. remainPathLength = 0.0;
  784. destinationSaved.Clear();
  785. patrolPathID = pathID;
  786. xodrobj xodrDest;
  787. xodrDest.flon = gPOIPoints.at(1).longitude;
  788. xodrDest.flat = gPOIPoints.at(1).latitude;
  789. xodrDest.lane = 1;
  790. std::cout<<"destination: "<<std::setprecision(12)<<gPOIPoints.at(1).longitude<<","<<gPOIPoints.at(1).latitude<<std::endl;
  791. iv::modulecomm::ModuleSendMsg(shmXodrRequest.mpa,(char *)&xodrDest,sizeof(xodrobj)); ///< send request msg
  792. // std::cout<<"modulesend xodr request"<<std::endl;
  793. if(destinationRefreshedTimer.isActive() == false)destinationRefreshedTimer.start(10000);
  794. std::cout<<"patrol path calculating"<<std::endl;
  795. // QFile mapfile;
  796. // mapfile.setFileName("/home/nvidia/Documents/path2.txt");
  797. // QVector<org::jeecg::defsDetails::grpc::MapPoint> somePoints;
  798. // if(mapfile.open(QIODevice::ReadOnly | QIODevice::Text))
  799. // {
  800. // while(!mapfile.atEnd())
  801. // {
  802. // QByteArray line = mapfile.readLine();
  803. // QString map_str(line);
  804. // QStringList oneline = map_str.split(",");
  805. // org::jeecg::defsDetails::grpc::MapPoint onePoint;
  806. // onePoint.set_index(oneline.at(0).toInt());
  807. // onePoint.mutable_mappoint()->set_longitude(oneline.at(1).toDouble());
  808. // onePoint.mutable_mappoint()->set_latitude(oneline.at(2).toDouble());
  809. // onePoint.mutable_mappoint()->set_height(oneline.at(3).toDouble());
  810. // somePoints.append(onePoint);
  811. // }
  812. // }
  813. // updatePath(pathID,somePoints);
  814. // uploadPath();
  815. }
  816. void DataExchangeClient::destination_Recieved_Slot(void)
  817. {
  818. if(fabs(destinationPosition.latitude()) > 0.000001 \
  819. && fabs(destinationPosition.longitude()) > 0.000001)
  820. {
  821. }
  822. }
  823. void DataExchangeClient::path_Updated_Slot(void)
  824. {
  825. if(destinationRefreshedTimer.isActive() == true)
  826. destinationRefreshedTimer.stop();
  827. destinationSaved.set_latitude(pathPoints.at(pathPoints.size()-1).mappoint().latitude());
  828. destinationSaved.set_longitude(pathPoints.at(pathPoints.size()-1).mappoint().longitude());
  829. destinationSaved.set_height(0.0);
  830. remainPathLength = totalPathLength;
  831. isArrived = 1; ///< 0 no path 1 not arrived 2 arrived
  832. uploadPath();
  833. }
  834. void DataExchangeClient::destination_Refreshed_Timeout(void)
  835. {
  836. if(destinationRefreshedTimer.isActive() == true)
  837. destinationRefreshedTimer.stop();
  838. destinationSaved.Clear();
  839. emit setFSMDestination(destinationSaved.latitude(),destinationSaved.longitude());
  840. remainPathLength = 0.0;
  841. isArrived = 0;
  842. emit uploadPath_Finished("");
  843. std::cout<<"path plan timeout."<<std::endl;
  844. }
  845. void DataExchangeClient::useStatusChanged_Slot(bool status)
  846. {
  847. if(status)
  848. useStatus = org::jeecg::defsDetails::grpc::UseStatus::ENABLING;
  849. else
  850. useStatus = org::jeecg::defsDetails::grpc::UseStatus::DEACTIVATING;
  851. }