vehicle_upload.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include "vehicle_upload.h"
  2. #include <QFile>
  3. #include <QString>
  4. #include <QStringList>
  5. extern std::string gstrserverip;
  6. extern std::string gstruploadPort;
  7. extern std::string gstruploadInterval;
  8. extern std::string gstrid;
  9. extern std::string gstrplateNumber;
  10. extern uint8_t gShift_Status;//3 p 4 r 5 n 6 d
  11. namespace iv {
  12. struct msgunit
  13. {
  14. char mstrmsgname[256];
  15. int mnBufferSize = 10000;
  16. int mnBufferCount = 1;
  17. void * mpa;
  18. std::shared_ptr<char> mpstrmsgdata;
  19. int mndatasize = 0;
  20. bool mbRefresh = false;
  21. bool mbImportant = false;
  22. int mnkeeptime = 100;
  23. };
  24. }
  25. extern iv::msgunit shmPicFront;
  26. extern iv::msgunit shmPicRear;
  27. extern iv::msgunit shmPicLeft;
  28. extern iv::msgunit shmPicRight;
  29. using org::jeecg::defsDetails::grpc::Empty; ///< other message
  30. using org::jeecg::defsDetails::grpc::GPSPoint;
  31. using org::jeecg::defsDetails::grpc::MapPoint;
  32. using org::jeecg::defsDetails::grpc::ShiftStatus; ///< other enum
  33. using org::jeecg::defsDetails::grpc::CtrlMode;
  34. DataExchangeClient * gDataExchangeClient;
  35. void ListenFrontData(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  36. {
  37. gDataExchangeClient->ListenFrontPicMsg(strdata,nSize);
  38. }
  39. void ListenRearData(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  40. {
  41. gDataExchangeClient->ListenRearPicMsg(strdata,nSize);
  42. }
  43. void ListenLeftData(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  44. {
  45. gDataExchangeClient->ListenLeftPicMsg(strdata,nSize);
  46. }
  47. void ListenRightData(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  48. {
  49. gDataExchangeClient->ListenRightPicMsg(strdata,nSize);
  50. }
  51. DataExchangeClient::DataExchangeClient(std::shared_ptr<Channel> channel)
  52. {
  53. gDataExchangeClient = this;
  54. stub_ = DataExchange::NewStub(channel);
  55. shmPicFront.mpa = iv::modulecomm::RegisterRecv(shmPicFront.mstrmsgname,ListenFrontData);
  56. shmPicRear.mpa = iv::modulecomm::RegisterRecv(shmPicRear.mstrmsgname,ListenRearData);
  57. shmPicLeft.mpa = iv::modulecomm::RegisterRecv(shmPicLeft.mstrmsgname,ListenLeftData);
  58. shmPicRight.mpa = iv::modulecomm::RegisterRecv(shmPicRight.mstrmsgname,ListenRightData);
  59. }
  60. DataExchangeClient::~DataExchangeClient(void)
  61. {
  62. }
  63. void DataExchangeClient::ListenFrontPicMsg(const char * strdata,const unsigned int nSize) // need a lock
  64. {
  65. iv::vision::rawpic xdata;
  66. if(!xdata.ParseFromArray(strdata,nSize))
  67. {
  68. std::cout<<" MainWindow::UpdateSlider parese error."<<std::endl;
  69. return;
  70. }
  71. gMutex_ImageFront.lock();
  72. cameraImageFront.clear();
  73. if(gShift_Status == 6)cameraImageFront.append(xdata.picdata().data(),xdata.picdata().size());
  74. gMutex_ImageFront.unlock();
  75. }
  76. void DataExchangeClient::ListenRearPicMsg(const char * strdata,const unsigned int nSize) // need a lock
  77. {
  78. iv::vision::rawpic xdata;
  79. if(!xdata.ParseFromArray(strdata,nSize))
  80. {
  81. std::cout<<" MainWindow::UpdateSlider parese error."<<std::endl;
  82. return;
  83. }
  84. gMutex_ImageRear.lock();
  85. cameraImageRear.clear();
  86. if(gShift_Status == 4)cameraImageRear.append(xdata.picdata().data(),xdata.picdata().size());
  87. gMutex_ImageRear.unlock();
  88. }
  89. void DataExchangeClient::ListenLeftPicMsg(const char * strdata,const unsigned int nSize) // need a lock
  90. {
  91. iv::vision::rawpic xdata;
  92. if(!xdata.ParseFromArray(strdata,nSize))
  93. {
  94. std::cout<<" MainWindow::UpdateSlider parese error."<<std::endl;
  95. return;
  96. }
  97. gMutex_ImageLeft.lock();
  98. cameraImageLeft.clear();
  99. if(gShift_Status == 5)cameraImageLeft.append(xdata.picdata().data(),xdata.picdata().size());
  100. gMutex_ImageLeft.unlock();
  101. }
  102. void DataExchangeClient::ListenRightPicMsg(const char * strdata,const unsigned int nSize) // need a lock
  103. {
  104. iv::vision::rawpic xdata;
  105. if(!xdata.ParseFromArray(strdata,nSize))
  106. {
  107. std::cout<<" MainWindow::UpdateSlider parese error."<<std::endl;
  108. return;
  109. }
  110. gMutex_ImageRight.lock();
  111. cameraImageRight.clear();
  112. if(gShift_Status == 5)cameraImageRight.append(xdata.picdata().data(),xdata.picdata().size());
  113. gMutex_ImageRight.unlock();
  114. }
  115. std::string DataExchangeClient::uploadVehicleInfo(void)
  116. {
  117. // Data we are sending to the server.
  118. UplinkRequest request;
  119. request.set_id(id);
  120. request.set_timestamp(timeStamp);
  121. request.set_soc(SOC);
  122. request.set_statusfeedback(statusFeedback);
  123. request.set_mileage(mileage);
  124. request.set_speed(speed);
  125. request.set_shiftfeedback(shiftFeedback);
  126. request.set_steeringwheelanglefeedback(steeringWheelAngleFeedback);
  127. request.set_throttlefeedback(throttleFeedback);
  128. request.set_brakefeedback(brakeFeedback);
  129. request.set_gpsrtkstatus(GPSRTKStatus);
  130. request.mutable_positionfeedback()->CopyFrom(positionFeedback);
  131. request.set_pitch(pitch);
  132. request.set_roll(roll);
  133. request.set_heading(heading);
  134. gMutex_ImageFront.lock();
  135. request.set_cameraimagefront(cameraImageFront.data(),cameraImageFront.size());
  136. gMutex_ImageFront.unlock();
  137. gMutex_ImageRear.lock();
  138. request.set_cameraimagerear(cameraImageRear.data(),cameraImageRear.size());
  139. gMutex_ImageRear.unlock();
  140. gMutex_ImageLeft.lock();
  141. request.set_cameraimageleft(cameraImageLeft.data(),cameraImageLeft.size());
  142. gMutex_ImageLeft.unlock();
  143. gMutex_ImageRight.lock();
  144. request.set_cameraimageright(cameraImageRight.data(),cameraImageRight.size());
  145. gMutex_ImageRight.unlock();
  146. request.set_sensorstatusgpsimu(sensorStatusGPSIMU);
  147. request.set_sensorstatuslidar(sensorStatusLidar);
  148. request.set_sensorstatusradar(sensorStatusRadar);
  149. request.set_sensorstatuscamfront(sensorStatusCamFront);
  150. request.set_sensorstatuscamrear(sensorStatusCamRear);
  151. request.set_sensorstatuscamleft(sensorStatusCamLeft);
  152. request.set_sensorstatuscamright(sensorStatusCamRight);
  153. request.set_isarrived(isArrived);
  154. request.set_platenumber(plateNumber);
  155. request.set_modefeedback(modeFeedback);
  156. // Container for the data we expect from the server.
  157. ResponseMessage reply;
  158. // Context for the client. It could be used to convey extra information to
  159. // the server and/or tweak certain RPC behaviors.
  160. ClientContext context;
  161. gpr_timespec timespec;
  162. timespec.tv_sec = 2;
  163. timespec.tv_nsec = 0;
  164. timespec.clock_type = GPR_TIMESPAN;
  165. context.set_deadline(timespec);
  166. // The actual RPC.
  167. Status status = stub_ -> uploadVehicleInfo(&context, request, &reply);
  168. // Act upon its status.
  169. if (status.ok()) {
  170. destinationPosition.CopyFrom(reply.destinationposition());
  171. // std::cout<<"lat:"<<destinationPosition.latitude()<<"lon:"<<destinationPosition.longitude()<<"height:"<<destinationPosition.height()<<std::endl;
  172. return "uploadVehicleInfo RPC successed";
  173. } else {
  174. std::cout << status.error_code() << ": " << status.error_message()
  175. << std::endl;
  176. if(status.error_code() == 4)
  177. {
  178. std::cout << "vehicleControl RPC connect timeout" << std::endl;
  179. }
  180. return "uploadVehicleInfo RPC failed";
  181. }
  182. }
  183. std::string DataExchangeClient::uploadPath(void)
  184. {
  185. // Data we are sending to the server.
  186. UploadPathRequest request;
  187. request.set_id(id);
  188. request.set_patrolpathid(patrolPathID);
  189. for(int i = 0;i < pathPoints.size();i++)
  190. {
  191. request.add_pathpoints();
  192. request.mutable_pathpoints(i)->operator =(pathPoints.at(i));
  193. }
  194. // Container for the data we expect from the server.
  195. Empty reply;
  196. // Context for the client. It could be used to convey extra information to
  197. // the server and/or tweak certain RPC behaviors.
  198. ClientContext context;
  199. gpr_timespec timespec;
  200. timespec.tv_sec = 5;
  201. timespec.tv_nsec = 0;
  202. timespec.clock_type = GPR_TIMESPAN;
  203. context.set_deadline(timespec);
  204. // The actual RPC.
  205. Status status = stub_ -> uploadPath(&context,request,&reply);
  206. // Act upon its status.
  207. if (status.ok()) {
  208. if(reply.id() == gstrid)
  209. {
  210. std::cout<<"Path uploaded by car id:"<<reply.id()<<std::endl;
  211. emit uploadPath_Finished(patrolPathID);
  212. }
  213. return "uploadPath RPC successed";
  214. } else {
  215. std::cout << status.error_code() << ": " << status.error_message()
  216. << std::endl;
  217. if(status.error_code() == 4)
  218. {
  219. std::cout << "vehicleControl RPC connect timeout" << std::endl;
  220. }
  221. return "uploadPath RPC failed";
  222. }
  223. }
  224. void DataExchangeClient::updateData(void)
  225. {
  226. id = gstrid;
  227. timeStamp = QDateTime::currentMSecsSinceEpoch();
  228. SOC = 87.5;
  229. statusFeedback = VehicleStatus::STATUS_REMOTE;
  230. mileage = 123.45; // kilometer
  231. speed = 0.1; // m/s
  232. shiftFeedback = ShiftStatus::SHIFT_DRIVE;
  233. steeringWheelAngleFeedback = 1.23; //+/-540 degree
  234. throttleFeedback = 0.12;
  235. brakeFeedback = 0.34;
  236. GPSRTKStatus = 6; //GPS-RTK status 0-6 6 is best
  237. positionFeedback.set_latitude(39.0666552);
  238. positionFeedback.set_longitude(117.3540963);
  239. positionFeedback.set_height(0.84);
  240. pitch = 0.03;
  241. roll = 0.02;
  242. heading = 198.4;
  243. // QFile xFile;
  244. // xFile.setFileName("/home/nvidia/Pictures/123.jpg");
  245. // if(xFile.open(QIODevice::ReadOnly))
  246. // {
  247. // cameraImageFront = xFile.readAll();
  248. // }
  249. // xFile.close();
  250. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  251. // if(xFile.open(QIODevice::ReadOnly))
  252. // {
  253. // cameraImageRear = xFile.readAll();
  254. // }
  255. // xFile.close();
  256. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  257. // if(xFile.open(QIODevice::ReadOnly))
  258. // {
  259. // cameraImageLeft = xFile.readAll();
  260. // }
  261. // xFile.close();
  262. // xFile.setFileName("/home/samuel/Pictures/123.jpg");
  263. // if(xFile.open(QIODevice::ReadOnly))
  264. // {
  265. // cameraImageRight = xFile.readAll();
  266. // }
  267. // xFile.close();
  268. sensorStatusGPSIMU = false; //0 GPS-IMU ok 1 GPS-IMU error
  269. sensorStatusLidar = false;
  270. sensorStatusRadar = false;
  271. sensorStatusCamFront = false;
  272. sensorStatusCamRear = false;
  273. sensorStatusCamLeft = false;
  274. sensorStatusCamRight = false;
  275. isArrived = 0; //0 no destination 1 not arrived 2 arrived
  276. plateNumber = gstrplateNumber;
  277. modeFeedback = CtrlMode::CMD_REMOTE; //mode Feedback
  278. }
  279. void DataExchangeClient::updatePath(std::string pathID, QVector<MapPoint> points)
  280. {
  281. id = gstrid;
  282. patrolPathID = pathID;
  283. pathPoints.clear();
  284. for(int i = 0;i < points.size();i++)
  285. {
  286. pathPoints.append(points.value(i));
  287. // std::cout<<pathPoints.at(i).index()<<std::endl;
  288. }
  289. }
  290. void DataExchangeClient::run()
  291. {
  292. QTime xTime;
  293. xTime.start();
  294. uint64_t lastTime = xTime.elapsed();
  295. uint64_t interval = std::atoi(gstruploadInterval.c_str());
  296. while (true)
  297. {
  298. if((xTime.elapsed() - lastTime)>=interval)
  299. {
  300. updateData();
  301. std::string reply = uploadVehicleInfo();
  302. std::cout<< reply <<std::endl;
  303. lastTime = xTime.elapsed();
  304. }
  305. }
  306. }
  307. void DataExchangeClient::patrolPOI_Recieved_Slot(std::string pathID)
  308. {
  309. std::cout<<"patrol path calculating"<<std::endl;
  310. QFile mapfile("/home/samuel/Documents/path1.txt");
  311. QVector<org::jeecg::defsDetails::grpc::MapPoint> somePoints;
  312. if(mapfile.open(QIODevice::ReadOnly | QIODevice::Text))
  313. {
  314. while(!mapfile.atEnd())
  315. {
  316. QByteArray line = mapfile.readLine();
  317. QString map_str(line);
  318. QStringList oneline = map_str.split(",");
  319. org::jeecg::defsDetails::grpc::MapPoint onePoint;
  320. onePoint.set_index(oneline.at(0).toInt());
  321. onePoint.mutable_mappoint()->set_longitude(oneline.at(1).toDouble());
  322. onePoint.mutable_mappoint()->set_latitude(oneline.at(2).toDouble());
  323. onePoint.mutable_mappoint()->set_height(oneline.at(3).toDouble());
  324. somePoints.append(onePoint);
  325. }
  326. }
  327. updatePath(pathID,somePoints);
  328. uploadPath();
  329. }
  330. void DataExchangeClient::ctrlMode_Changed_Slot(org::jeecg::defsDetails::grpc::CtrlMode ctrlMode)
  331. {
  332. modeFeedback = ctrlMode;
  333. }