vehicle_upload.cpp 31 KB

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