message_queue.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. #include <Eigen/Dense>
  2. #include <atomic>
  3. #include <string>
  4. #include "PCANBasic.h"
  5. #include "../common/comm.h"
  6. #include "api.h"
  7. #include "../common/iobuffer.h"
  8. #include "../common/sensor_socket.h"
  9. #include "../common/peer_connection.h"
  10. #include "../common/can_sensor.h"
  11. #include "VideoRenderer.h"
  12. #include <jsoncpp/json/json.h>
  13. #include "protocol.pb.h"
  14. #include "radar_sensor.h"
  15. #include "lidar_sensor.h"
  16. #include "encoder_sensor.h"
  17. #include "car_sensor.h"
  18. #include "message_queue.h"
  19. #include <fstream>
  20. #include <iostream>
  21. #include <cstring>
  22. #include <alloca.h>
  23. #include <sstream>
  24. #include <iomanip>
  25. #include <ctime>
  26. #include <chrono>
  27. #include <time.h>
  28. #include <sys/stat.h>
  29. #include <sys/types.h>
  30. #include <sys/time.h>
  31. #include "message.h"
  32. #include <sensors/sensors.h>
  33. #include <sensors/error.h>
  34. //#include "auto_sensor.h"
  35. CMessageQueue::CMessageQueue():Head(nullptr),Tail(nullptr)
  36. {
  37. _peerId=-1;
  38. _curTick=0;
  39. }
  40. CMessageQueue::~CMessageQueue()
  41. {
  42. _robot->Stop();
  43. }
  44. void renableUSB(const char *file);
  45. void CMessageQueue::Create()
  46. {
  47. Json::Value root;
  48. Json::Reader jsonReader;
  49. std::ifstream ifile("./local.json");
  50. std::string serverip;
  51. if(jsonReader.parse(ifile,root))
  52. {
  53. std::cout<<"enter config json"<<std::endl;
  54. //std::string _lidarip=root["lidar_ip"].asString();
  55. // int32_t _lidar_host=root["lidar_host"].asInt();
  56. // std::cout<<"lidar ip:"<<_lidarip<<std::endl;
  57. //_lidar=std::make_unique<SensorSocket<CLidarSensor>>(this,_lidarip,0,_lidar_host);
  58. // _lidar->Start();
  59. max_left_length=root["max_left_length"].asInt();
  60. max_right_length=root["max_right_length"].asInt();
  61. min_left_length=root["min_left_length"].asInt();
  62. min_right_length=root["min_right_length"].asInt();
  63. tipin_time=root["tipin_time"].asInt();
  64. tipout_time=root["tipout_time"].asInt();
  65. // _auto=std::make_unique<SensorSocket<CAutoSensor>>(this,auto_host,32121,32122);
  66. // _auto->Start();
  67. serverip=root["ip"].asString();
  68. _name=root["name"].asString();
  69. _car=static_cast<CarType>(root["car_type"].asInt());
  70. const Json::Value arrayObj=root["camerainfo"];
  71. int32_t count=arrayObj.size();
  72. for(int32_t i=0;i<count;i++)
  73. {
  74. LocalCameraInfo info;
  75. const Json::Value& value=arrayObj[i];
  76. info.index=value["index"].asInt();
  77. info.label=value["label"].asString();
  78. _cameraArray.push_back(info);
  79. }
  80. for(int i=0;i<_cameraArray.size();i++)
  81. {
  82. _peerArray.push_back({nullptr});
  83. // _windowArray.push_back({nullptr});
  84. }
  85. /*
  86. const Json::Value files=root["playlist"];
  87. count=files.size();
  88. for(int32_t i=0;i<count;i++)
  89. {
  90. const Json::Value& value=files[i];
  91. int32_t index=value["index"].asInt();
  92. std::string file=value["file"].asString();
  93. LoadRecord(static_cast<ROUT_LIST>(index),file.c_str());
  94. }
  95. */
  96. int32_t _radarport=root["radar_port"].asInt();
  97. int32_t _radarhost=root["radar_host"].asInt();
  98. std::string _radarip=root["radar_ip"].asString();
  99. std::cout<<"radar ip:"<<_radarip<<std::endl;
  100. _radar=std::make_unique<SensorSocket<CRadarSensor>>(this,_radarip,_radarport,_radarhost);
  101. _radar->Start();
  102. int32_t encodeport=root["encode_port"].asInt();
  103. int32_t encodehost=root["encode_host"].asInt();
  104. _encoder=std::make_unique<SensorSocket<CEncoderSensor>>(this,_radarip,encodeport,encodehost);
  105. _encoder->Start();
  106. int32_t auto_port=root["auto_port"].asInt();
  107. std::string auto_host=root["auto_host"].asString();
  108. _robot=std::make_unique<SensorSocket<CCarSensor>>(this,auto_host,auto_port,23001);
  109. _robot->Start();
  110. std::string usb=root["usb_3"].asString();
  111. renableUSB(usb.c_str());
  112. usb=root["usb_2"].asString();
  113. renableUSB(usb.c_str());
  114. _serial=root["serial"].asString();
  115. }
  116. else{
  117. std::string error=jsonReader.getFormattedErrorMessages();
  118. std::cout<<error<<std::endl;
  119. }
  120. _client=std::make_unique<SocketClient>(this);
  121. _client->Start(serverip.c_str());
  122. //_can=std::make_unique<SocketCan>(this);
  123. //_can->Start(_canip,_canport,_hostport);
  124. std::this_thread::yield();
  125. // OnNotifyReq(0);
  126. }
  127. /*
  128. void CMessageQueue::WriteCanMessage(std::unordered_map<int32_t, cannet_frame>& node,bool islidar)
  129. {
  130. if(!bDataChannelCreated) return;
  131. // std::lock_guard<std::mutex> l(_canLock);
  132. RemoNet::CCCanMesage Req;
  133. Req.set_islidar(islidar);
  134. for(auto& p:node)
  135. {
  136. int32_t lidar=p.second.canid;
  137. auto m=Req.add_message();
  138. m->set_head(p.second.dlc);
  139. m->set_canid(lidar);
  140. m->set_data(p.second.data,8);
  141. }
  142. MessageHead Head;
  143. CIOBuffer pBuffer;
  144. Head.Command = RemoNet::CC_CAN;
  145. Head.Length = Req.ByteSizeLong();
  146. Head.Serialize(pBuffer.Buffer);
  147. auto ptr = pBuffer.Buffer + MessageHead::Size();
  148. Req.SerializeToArray(ptr, Head.Length);
  149. pBuffer.Length = MessageHead::Size() + Head.Length;
  150. _peerArray[0]->SendData(&pBuffer);
  151. }
  152. bool CMessageQueue::IsCarId(int32_t value)
  153. {
  154. return std::find(_carArray.begin(),_carArray.end(),value)!=_carArray.end();
  155. }
  156. */
  157. void CMessageQueue::EnQueue(CIOBuffer* pBuffer)
  158. {
  159. bool bNullBuffer=false;
  160. std::unique_lock <std::mutex> lck(_mlock);
  161. if(Head==nullptr)
  162. {
  163. Head=Tail=pBuffer;
  164. bNullBuffer=true;
  165. }
  166. else{
  167. Tail->NextBuf=pBuffer;
  168. Tail=Tail->NextBuf;
  169. }
  170. pBuffer->NextBuf=nullptr;
  171. if(bNullBuffer)
  172. {
  173. // std::cout<<"notify "<<_count<<std::endl;
  174. _cv.notify_all();
  175. }
  176. }
  177. void CMessageQueue::Process()
  178. {
  179. CIOBuffer * ptr=nullptr;
  180. {
  181. std::unique_lock <std::mutex> lck(_mlock);
  182. /*
  183. while(Head==nullptr)
  184. {
  185. _cv.wait(lck);
  186. }
  187. */
  188. while(Head==nullptr&&_cv.wait_for(lck,std::chrono::seconds(1))==std::cv_status::timeout)
  189. {
  190. // CheckSignal();
  191. std::cout<<".";
  192. std::cout.flush();
  193. }
  194. }
  195. while(Head!=nullptr)
  196. {
  197. ptr=Head;
  198. Head=Head->NextBuf;
  199. if(ptr!=nullptr)
  200. {
  201. Message* message=reinterpret_cast<Message *>(ptr->Buffer);
  202. switch (message->cmd)
  203. {
  204. case MessageType::ReqVideo:
  205. OnNotifyReq((int32_t)message->param_l);
  206. break;
  207. case MessageType::RepVideo:
  208. OnNotifyRep((int32_t)message->param_l);
  209. break;
  210. case MessageType::Connected:
  211. OnNotifyConnected((bool)message->param_l);
  212. break;
  213. case MessageType::Leave:
  214. OnNotifyLeave();
  215. break;
  216. case MessageType::AsyncMessage:
  217. OnNotifyMessage();
  218. break;
  219. case MessageType::StopSensor:
  220. OnNotifyStopSensor();
  221. break;
  222. case MessageType::Ping:
  223. OnNotifyPing(message->param_l);
  224. break;
  225. }
  226. ptr->Release(__FILE__,__LINE__);
  227. }
  228. }
  229. }
  230. void CMessageQueue::OnNotifyConnected(bool bRet)
  231. {
  232. if(bRet)
  233. {
  234. _client->WriteAddRobot(_serial,_name,std::string(""),static_cast<int32_t>(EgoType::Car),_car);
  235. _updatethread.start(_client.get());
  236. }
  237. else
  238. {
  239. if(_updatethread.running())
  240. {
  241. _updatethread.stop();
  242. }
  243. if(_peerId!=-1)
  244. {
  245. for (size_t i = 0; i < _peerArray.size(); i++)
  246. {
  247. if(_peerArray[i]!=nullptr)
  248. {
  249. _peerArray[i]->Close();
  250. _peerArray[i].reset();
  251. }
  252. /* code */
  253. }
  254. // for (size_t i = 0; i < _windowArray.size(); i++)
  255. // {
  256. /* code */
  257. // if(_windowArray[i]!=nullptr)
  258. // _windowArray[i].reset();
  259. // }
  260. _peerId=-1;
  261. // StopCar();
  262. }
  263. }
  264. }
  265. void CMessageQueue::ChangeState(UserState state)
  266. {
  267. RemoNet::CSState Req;
  268. Req.set_state((RemoNet::UserState)state);
  269. Req.set_uid(_uid);
  270. MessageHead Head;
  271. CIOBuffer pBuffer;
  272. Head.Command = RemoNet::CS_State;
  273. Head.Length = Req.ByteSizeLong();
  274. Head.Serialize(pBuffer.Buffer);
  275. auto ptr = pBuffer.Buffer + MessageHead::Size();
  276. Req.SerializeToArray(ptr, Head.Length);
  277. pBuffer.Length = MessageHead::Size() + Head.Length;
  278. _client->Write(&pBuffer);
  279. }
  280. void CMessageQueue::OnNotifyReq(int32_t index)
  281. {
  282. // std::cout<<__FUNCTION__<<","<<__LINE__<<std::endl;
  283. if(_peerId==-1) return;
  284. if(index==0)
  285. {
  286. mrsWebrtcCreateFactory(true);
  287. // _radar->Start();
  288. // _encoder->Start();
  289. ChangeState(UserState::Remote);
  290. // _robot->Start();
  291. }
  292. _curTick=0;
  293. // PeerConnectionWrapper* peer = nullptr;
  294. // VideoRenderer* window = nullptr;
  295. // _windowArray[temp]=std::make_unique<VideoRenderer>();// .reset(new VideoRenderer());
  296. _peerArray[index]=std::make_unique<CPeerConnection>(static_cast<ChannelType>(index),_client.get());
  297. _client->WriteVideoRep(_peerId, RemoNet::VideoDesc::OK, index);
  298. }
  299. void CMessageQueue::WriteBuffer(CIOBuffer& buffer)
  300. {
  301. _client->Write(&buffer);
  302. }
  303. const radar_node* CMessageQueue::GetRadar()
  304. {
  305. return _radar->Get()->GetRadarInfo();
  306. }
  307. void CMessageQueue::OnNotifyRep(int32_t index)
  308. {
  309. // std::cout<<__FUNCTION__<<","<<__LINE__<<std::endl;
  310. // index-=_indexOffset;
  311. // PeerConnectionWrapper* peer = nullptr;
  312. // VideoRenderer* window = nullptr;
  313. //_windowArray[temp]=std::make_unique<VideoRenderer>();
  314. _peerArray[index]=std::make_unique<CPeerConnection>(static_cast<ChannelType>(index), _client.get());
  315. InitPeerConnection(_peerId,index);
  316. _peerArray[index]->CreateOffer();
  317. }
  318. void CMessageQueue::InitPeerConnection(int32_t peer,int32_t index)
  319. {
  320. //bool NeedData=index==RenderPosition::CAR_FRONT;
  321. _peerArray[index]->Initialize(peer,index);
  322. _peerArray[index]->AddDataChannel(true, false);
  323. // window.reset(new VideoRenderer());
  324. // window->SetRenderWindow(GetDlgItem(IDC_REMOTE), 1, 1);
  325. //_peerArray[index]->AddLocalArgb32VideoFrameReady(&VideoRenderer::FrameCallback, _windowArray[index].get());
  326. // if(_cameraArray[index].type==CaptureType::RTSP)
  327. // {
  328. _peerArray[index]->AddLocalVideoTrack(static_cast<RenderPosition>(index),_cameraArray[index].index);
  329. if((index+1)==RenderPosition::ALL)
  330. {
  331. void * front=_peerArray[RenderPosition::FRONT]->GetCurrentCtx();
  332. void * back=_peerArray[RenderPosition::BACK]->GetCurrentCtx();
  333. assert(front!=nullptr);
  334. assert(back!=nullptr);
  335. _peerArray[RenderPosition::FRONT]->SetOtherCtx(back);
  336. _peerArray[RenderPosition::BACK]->SetOtherCtx(front);
  337. void * left=_peerArray[RenderPosition::LEFT]->GetCurrentCtx();
  338. void * right=_peerArray[RenderPosition::RIGHT]->GetCurrentCtx();
  339. _peerArray[RenderPosition::LEFT]->SetOtherCtx(right);
  340. _peerArray[RenderPosition::RIGHT]->SetOtherCtx(left);
  341. void * leftAnchor=_peerArray[RenderPosition::LANCHOR]->GetCurrentCtx();
  342. void * rightAnchor=_peerArray[RenderPosition::RANCHOR]->GetCurrentCtx();
  343. while(leftAnchor==nullptr||rightAnchor==nullptr)
  344. {
  345. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  346. leftAnchor=_peerArray[RenderPosition::LANCHOR]->GetCurrentCtx();
  347. rightAnchor=_peerArray[RenderPosition::RANCHOR]->GetCurrentCtx();
  348. }
  349. {
  350. _peerArray[RenderPosition::LANCHOR]->SetOtherCtx(rightAnchor);
  351. _peerArray[RenderPosition::RANCHOR]->SetOtherCtx(leftAnchor);
  352. _camera_inited=true;
  353. }
  354. // _camera_inited=false;
  355. }
  356. // }
  357. // else
  358. // {
  359. // _peerArray[index]->AddLocalVideoTrack(_cameraArray[index].type, _cameraArray[index].solution, 30);
  360. // }
  361. if(index==RenderPosition::FRONT)
  362. _peerArray[index]->AddLocalAudioTrack();
  363. //[temp]->StartRender(true);
  364. }
  365. void CMessageQueue::OnAdd(int32_t id,bool bRet)
  366. {
  367. _uid=id;
  368. }
  369. void CMessageQueue::OnConnected(bool bRet)
  370. {
  371. CIOBuffer * pBuffer=CIOBuffer::Alloc(__FILE__,__LINE__);
  372. Message* message=reinterpret_cast<Message *>(pBuffer->Buffer);
  373. message->cmd=MessageType::Connected;
  374. message->param_l=bRet;
  375. EnQueue(pBuffer);
  376. }
  377. void CMessageQueue::OnVideoLeave(int32_t peer,EgoType type)
  378. {
  379. {
  380. CIOBuffer * pBuffer=CIOBuffer::Alloc(__FILE__,__LINE__);
  381. Message* message=reinterpret_cast<Message *>(pBuffer->Buffer);
  382. message->cmd=MessageType::StopSensor;
  383. EnQueue(pBuffer);
  384. }
  385. {
  386. CIOBuffer * pBuffer=CIOBuffer::Alloc(__FILE__,__LINE__);
  387. Message* message=reinterpret_cast<Message *>(pBuffer->Buffer);
  388. message->cmd=MessageType::Leave;
  389. EnQueue(pBuffer);
  390. }
  391. _curTick=0;
  392. }
  393. #ifdef WIN32
  394. void CMessageQueue::OnVideoRep(int32_t index,RemoNet::VideoDesc desc)
  395. {
  396. if (desc == RemoNet::VideoDesc::OK)
  397. {
  398. assert(_peerId!=-1);
  399. CIOBuffer * pBuffer=CIOBuffer::Alloc();
  400. Message* message=reinterpret_cast<Message *>(pBuffer->Buffer);
  401. message->cmd=MessageType::ReqVideo;
  402. message->param_l=index;
  403. EnQueue(pBuffer);
  404. }
  405. }
  406. #else
  407. void CMessageQueue::OnVideoReq(int32_t video,int32_t peer)
  408. {
  409. if(video==0&&_peerId!=-1)
  410. {
  411. std::cout<<__FUNCTION__<<","<<__LINE__<<std::endl;
  412. _client->WriteVideoRep(peer,RemoNet::VideoDesc::Reject,video);
  413. return;
  414. }
  415. _peerId=peer;
  416. CIOBuffer * pBuffer=CIOBuffer::Alloc(__FILE__,__LINE__);
  417. Message* message=reinterpret_cast<Message *>(pBuffer->Buffer);
  418. message->cmd=MessageType::ReqVideo;
  419. message->param_l=video;
  420. EnQueue(pBuffer);
  421. }
  422. void CMessageQueue::OnMoveBegin(WorkArea area, int32_t no)
  423. {
  424. int64_t tick= std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  425. if(tick-_robot->Get()->GetLastEncodeTime()>500)
  426. {
  427. RemoNet::MoveRet Req;
  428. Req.set_desc(RemoNet::MoveDesc::Move_Encode_Fail);
  429. Req.set_peer(_peerId);
  430. CIOBuffer pBuffer;
  431. MessageHead Head;
  432. Head.Command = RemoNet::CS_MoveRet;
  433. Head.Length = Req.ByteSizeLong();
  434. Head.Serialize(pBuffer.Buffer);
  435. auto ptr = pBuffer.Buffer + MessageHead::Size();
  436. Req.SerializeToArray(ptr, Head.Length);
  437. pBuffer.Length = MessageHead::Size() + Head.Length;
  438. _client->Write(&pBuffer);
  439. return;
  440. }
  441. CIOBuffer pBuffer;
  442. auto ptr=pBuffer.Buffer;
  443. MessageHead Head;
  444. Head.Command=RA::RA_Goal;
  445. Head.Length=sizeof(ARMoveGoal);
  446. Head.Serialize(pBuffer.Buffer);
  447. ptr=pBuffer.Buffer+sizeof(MessageHead);
  448. ARMoveGoal * node=(ARMoveGoal *)ptr;
  449. node->area=area;
  450. node->no=no;
  451. /*node->forward=true;
  452. if(area==WorkArea::Area_I)
  453. {
  454. if(no>10&&no<14)
  455. {
  456. node->forward=false;
  457. }
  458. }
  459. */
  460. Head.Length=sizeof(ARMoveGoal);
  461. Head.Serialize(pBuffer.Buffer);
  462. pBuffer.Length=MessageHead::Size()+sizeof(ARMoveGoal);
  463. _robot->Write(pBuffer.Buffer,pBuffer.Length);
  464. _robot->Get()->SetAutomous(true,3);
  465. ChangeState(UserState::Automotive);
  466. }
  467. #endif
  468. void CMessageQueue::OnNotifyLeave()
  469. {
  470. SwitchCamera(true);
  471. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  472. for (size_t i = 0; i < _peerArray.size(); i++)
  473. {
  474. if(_peerArray[i]!=nullptr)
  475. {
  476. _peerArray[i]->Close();
  477. _peerArray[i].reset();
  478. }
  479. /* code */
  480. }
  481. //for (size_t i = 0; i < _windowArray.size(); i++)
  482. // {
  483. /* code */
  484. // _windowArray[i].reset();
  485. // }
  486. _peerId=-1;
  487. _robot->Get()->SetChannelReady(false);
  488. //StopCar();
  489. ChangeState(UserState::Idle);
  490. }
  491. void CMessageQueue::SetAutomous(bool value,int32_t delay)
  492. {
  493. _robot->Get()->SetAutomous(value,delay);
  494. }
  495. void CMessageQueue::OnNotifyStopSensor()
  496. {
  497. // std::cout<<"radar stop"<<std::endl;
  498. //_radar->Stop();
  499. // std::cout<<"robot stop"<<std::endl;
  500. // _encoder->Stop();
  501. // _lidar->Stop();
  502. _camera_inited=false;
  503. RemoNet::StopAck Rep;
  504. CIOBuffer Buffer;
  505. MessageHead Head;
  506. Head.Command = RemoNet::CC_StopACK;
  507. Head.Length = Rep.ByteSizeLong();
  508. Head.Serialize(Buffer.Buffer);
  509. auto ptr = Buffer.Buffer + MessageHead::Size();
  510. Rep.SerializeToArray(ptr, Head.Length);
  511. Buffer.Length = Head.Length + MessageHead::Size();
  512. _peerArray[RenderPosition::FRONT]->SendData(Buffer);
  513. }
  514. void CMessageQueue::OnVideoOffer(int32_t index,const char* type, const char* sdp)
  515. {
  516. // std::cout<<__FUNCTION__<<","<<__LINE__<<std::endl;
  517. InitPeerConnection(_peerId,index);
  518. _peerArray[index]->SetRemoteDescription(type,sdp);
  519. _peerArray[index]->CreateAnswer();
  520. }
  521. void CMessageQueue::OnVideoAnswer(int32_t index, const char* type, const char* sdp)
  522. {
  523. // std::cout<<__FUNCTION__<<","<<__LINE__<<std::endl;
  524. _peerArray[index]->SetRemoteDescription(type,sdp);
  525. }
  526. void CMessageQueue::OnVideoCandidate(int32_t index,const char* candidate,
  527. int32_t sdp_mline_index,
  528. const char* sdp_mid)
  529. {
  530. _peerArray[index]->AddIceCandidate(candidate,sdp_mline_index,sdp_mid);
  531. }
  532. void CMessageQueue::OnMessageFrameNotify(ChannelType type,int16_t cmd,int16_t length,const void * data)
  533. {
  534. if(cmd==RemoNet::CC_Text)
  535. {
  536. RemoNet::TestTextReq Req;
  537. Req.ParseFromArray(data,length);
  538. std::cout<<Req.text()<<std::endl;
  539. CIOBuffer * pBuffer=CIOBuffer::Alloc(__FILE__,__LINE__);
  540. Message* message=reinterpret_cast<Message *>(pBuffer->Buffer);
  541. message->cmd=MessageType::AsyncMessage;
  542. EnQueue(pBuffer);
  543. }
  544. else if(cmd==RemoNet::CC_Ping)
  545. {
  546. _curTick=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  547. RemoNet::CCPing Req;
  548. Req.ParseFromArray(data,length);
  549. CIOBuffer * pBuffer=CIOBuffer::Alloc(__FILE__,__LINE__);
  550. Message* message=reinterpret_cast<Message *>(pBuffer->Buffer);
  551. message->cmd=MessageType::Ping;
  552. message->param_l=Req.tick();
  553. EnQueue(pBuffer);
  554. }
  555. else if(cmd==RemoNet::CC_SensorStop)
  556. {
  557. CIOBuffer * pBuffer=CIOBuffer::Alloc(__FILE__,__LINE__);
  558. Message* message=reinterpret_cast<Message *>(pBuffer->Buffer);
  559. message->cmd=MessageType::StopSensor;
  560. EnQueue(pBuffer);
  561. }
  562. else if(cmd==RemoNet::CC_CANMSG)
  563. {
  564. RemoNet::CCCanMsg Req;
  565. Req.ParseFromArray(data,length);
  566. cannet_frame* frames=(cannet_frame *)alloca(sizeof(cannet_frame)*Req.frams_size());
  567. for(int32_t i=0;i<Req.frams_size();i++)
  568. {
  569. auto& frame =Req.frams(i);
  570. frames[i].canid=frame.canid();
  571. frames[i].dlc=frame.dlc();
  572. memcpy(frames[i].data,frame.data().data(),frame.dlc());
  573. }
  574. _robot->Get()->OnMessage(frames,Req.frams_size());
  575. //std::cout << "1111" << std::endl;
  576. /*
  577. static auto tick=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  578. auto diff=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count()-tick;
  579. std::cout<<diff<<std::endl;
  580. tick=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  581. */
  582. }
  583. else if(cmd==RemoNet::CC_ASKDATACHANNEL)
  584. {
  585. switch (type)
  586. {
  587. case ChannelType::CHANNEL_CAR:
  588. {
  589. _robot->Get()->SetChannelReady(true);
  590. }
  591. break;
  592. case ChannelType::CHANNEL_ENCODE:
  593. _encoder->Get()->SetChannelReady(true);
  594. // _lidar->Get()->SetChannelReady(true);
  595. break;
  596. case ChannelType::CHANNEL_RADAR:
  597. _radar->Get()->SetChannelReady(true);
  598. break;
  599. }
  600. }
  601. /*
  602. else if(cmd==RemoNet::CC_Switch)
  603. {
  604. RemoNet::CCSwitch Req;
  605. Req.ParseFromArray(data,length);
  606. bool front=Req.front();
  607. _peerArray[RenderPosition::FRONT_BACK]->SwitchCapture(front);
  608. }
  609. */
  610. }
  611. /*
  612. void CMessageQueue::StopCar()
  613. {
  614. for(auto id:_emergencyArray)
  615. {
  616. CIOBuffer Buffer;
  617. cannet_frame* msg=(cannet_frame *)Buffer.Buffer;
  618. msg->canid=id;
  619. msg->dlc=8;
  620. memset(msg->data,0,sizeof(msg->data));
  621. Buffer.Length=sizeof(cannet_frame);
  622. _can->Write(&Buffer);
  623. }
  624. _can->SetStartWrite(false);
  625. }
  626. */
  627. void CMessageQueue::OnNotifyMessage()
  628. {
  629. RemoNet::TestTextReq Req;
  630. Req.set_text("ewqewqewqe");
  631. CIOBuffer Buffer;
  632. MessageHead Head;
  633. Head.Command = RemoNet::CC_Text;
  634. Head.Length = Req.ByteSizeLong();
  635. Head.Serialize(Buffer.Buffer);
  636. auto ptr = Buffer.Buffer + MessageHead::Size();
  637. Req.SerializeToArray(ptr, Head.Length);
  638. Buffer.Length = Head.Length + MessageHead::Size();
  639. _peerArray[RenderPosition::FRONT]->SendData(Buffer);
  640. }
  641. void CMessageQueue::SetSteerAngle(int16_t angle)
  642. {
  643. _steer_angle=angle;
  644. }
  645. void CMessageQueue::SetNdtPos(ARNdtPos* node)
  646. {
  647. std::lock_guard<std::mutex> l(_pose_lock);
  648. _curpos.x=node->x;
  649. _curpos.y=node->y;
  650. _curpos.z=node->z;
  651. _curpos.rw=node->rw;
  652. _curpos.rx=node->rx;
  653. _curpos.ry=node->ry;
  654. _curpos.rz=node->rz;
  655. }
  656. void CMessageQueue::TestPark()
  657. {
  658. _parking=ParkingState::Turn;
  659. std::lock_guard<std::mutex> l(_pose_lock);
  660. _initpos.X=_curpos.x;
  661. _initpos.Y=_curpos.y;
  662. }
  663. void CMessageQueue::OnTip(int16_t value)
  664. {
  665. _start_time=std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  666. _parking=value>0?ParkingState::TipIn:ParkingState::TipRise;
  667. }
  668. void CMessageQueue::OnGoalFinish(WorkArea area,int32_t no)
  669. {
  670. _workarea=area;
  671. _no=no;
  672. _parking=ParkingState::Turn;
  673. Eigen::AngleAxisd axis;
  674. axis=Eigen::Quaterniond(_curpos.rw,_curpos.rx,_curpos.ry,_curpos.rz);
  675. double cur_angle=axis.angle();
  676. auto v=axis.axis();
  677. if(v[2]==-1)
  678. {
  679. cur_angle=EIGEN_PI*2-cur_angle;
  680. }
  681. {
  682. std::lock_guard<std::mutex> l(_pose_lock);
  683. _initpos.X=_curpos.x;
  684. _initpos.Y=_curpos.y;
  685. }
  686. if(area==WorkArea::Area_I)
  687. {
  688. //nothing to do
  689. _metalArea=Metal_Area::Put_Wait;
  690. _parking=ParkingState::None;
  691. }
  692. else if(area==WorkArea::Area_J)
  693. {
  694. if(std::abs(cur_angle-1.5*EIGEN_PI)<EIGEN_PI/4)
  695. {
  696. _side=TurnSide::Left;
  697. // std::cout << "left------" << std::endl;
  698. }
  699. else{
  700. _side=TurnSide::Right;
  701. // std::cout << "right----" << std::endl;
  702. }
  703. if(_no == 0 || _no == 1 || _no == 12 || _no ==13)
  704. {
  705. _sq_length=430;
  706. _diff_degree=30;
  707. _angle_value=10;
  708. _turn_base=1;
  709. } else {
  710. _sq_length=102;
  711. _diff_degree=30;
  712. _angle_value=10;
  713. _turn_base=1000;
  714. }
  715. }
  716. else
  717. {
  718. _sq_length=150;
  719. _diff_degree=30;
  720. _angle_value=10;
  721. _turn_base=1000;
  722. if(cur_angle<EIGEN_PI/4||cur_angle>EIGEN_PI*7/4) //east
  723. {
  724. switch(area)
  725. {
  726. case WorkArea::Area_A:
  727. _side=TurnSide::Right;
  728. _sq_length=140;
  729. _diff_degree=30;
  730. _angle_value=10;
  731. _turn_base=1000;
  732. break;
  733. case WorkArea::Area_C:
  734. case WorkArea::Area_E:
  735. case WorkArea::Area_G:
  736. _side=TurnSide::Right;
  737. _sq_length=140;
  738. _diff_degree=30;
  739. _angle_value=10;
  740. _turn_base=1000;
  741. break;
  742. case WorkArea::Area_B:
  743. _side=TurnSide::Left;
  744. _sq_length=110;
  745. _diff_degree=30;
  746. _angle_value=10;
  747. _turn_base=1000;
  748. case WorkArea::Area_D:
  749. case WorkArea::Area_F:
  750. case WorkArea::Area_H:
  751. _side=TurnSide::Left;
  752. _sq_length=140;
  753. _diff_degree=30;
  754. _angle_value=10;
  755. _turn_base=1000;
  756. break;
  757. default:
  758. break;
  759. }
  760. }
  761. else
  762. {
  763. switch(area)
  764. {
  765. case WorkArea::Area_A:
  766. _side=TurnSide::Left;
  767. _sq_length=130;
  768. _diff_degree=30;
  769. _angle_value=10;
  770. _turn_base=1000;
  771. break;
  772. case WorkArea::Area_C:
  773. case WorkArea::Area_E:
  774. case WorkArea::Area_G:
  775. _side=TurnSide::Left;
  776. _sq_length=140;
  777. _diff_degree=30;
  778. _angle_value=10;
  779. _turn_base=1000;
  780. break;
  781. case WorkArea::Area_B:
  782. case WorkArea::Area_D:
  783. case WorkArea::Area_F:
  784. case WorkArea::Area_H:
  785. _side=TurnSide::Right;
  786. break;
  787. default:
  788. break;
  789. }
  790. }
  791. }
  792. // std::cout << __FILE__ << " _sq_length: " << _sq_length << std::endl;
  793. // _robot->Get()->StartParking(true);
  794. // _parking=ParkingState::Turn;
  795. }
  796. const int32_t MAX_DGREE= (int32_t)(((4096/360*45)));
  797. void CMessageQueue::GetParkCanMessage(TPCANMsg* msg)
  798. {
  799. switch (_parking)
  800. {
  801. case ParkingState::TipIn:
  802. OnTipIn(msg);
  803. break;
  804. case ParkingState::TipOut:
  805. OnTipOut(msg);
  806. break;
  807. case ParkingState::TipDown:
  808. OnTipDown(msg);
  809. break;
  810. case ParkingState::TipRise:
  811. OnTipRise(msg);
  812. break;
  813. case ParkingState::RiseDown:
  814. OnRiseDown(msg);
  815. break;
  816. case ParkingState::Turn:
  817. OnTurn(msg);
  818. break;
  819. case ParkingState::Reverse:
  820. OnReverse(msg);
  821. break;
  822. case ParkingState::Restore:
  823. OnRestore(msg);
  824. break;
  825. default:
  826. break;
  827. }
  828. }
  829. void CMessageQueue::OnReverse(TPCANMsg* msg)
  830. {
  831. std::lock_guard<std::mutex> l(_pose_lock);
  832. double dx=_curpos.x-_initpos.X;
  833. double dy=_curpos.y-_initpos.Y;
  834. if((dx*dx+dy*dy)>_sq_length)
  835. {
  836. msg[2].ID=0x286;
  837. msg[2].MSGTYPE=PCAN_MESSAGE_STANDARD;
  838. msg[2].DATA[0]=0;
  839. msg[2].DATA[1]=0x21;
  840. msg[3].ID=0x386;
  841. msg[3].DATA[0]=0;
  842. msg[3].DATA[1]=0;
  843. _parking=ParkingState::Restore;
  844. }
  845. else
  846. {
  847. msg[2].ID=0x286;
  848. msg[2].MSGTYPE=PCAN_MESSAGE_STANDARD;
  849. msg[2].DATA[0]=0x10;
  850. msg[2].DATA[1]=0x20;
  851. msg[3].ID=0x386;
  852. msg[3].DATA[0]=40;
  853. msg[3].DATA[1]=0;
  854. }
  855. }
  856. void CMessageQueue::OnRestore(TPCANMsg* msg)
  857. {
  858. if(std::abs(_steer_angle)>_angle_value)
  859. {
  860. int32_t speed=std::max(600,std::abs(_steer_angle)/MAX_DGREE*1000);
  861. msg[1].ID=0x586;
  862. msg[1].MSGTYPE=PCAN_MESSAGE_STANDARD;
  863. msg[1].DATA[0]=0xFF;
  864. msg[1].DATA[1]=0x00;
  865. msg[1].DATA[2]=hi_byte(speed);
  866. msg[1].DATA[3]=lo_byte(speed);
  867. msg[1].DATA[4]=(_side==TurnSide::Left? 0:1)|0x02;
  868. msg[1].DATA[5] = 0;
  869. msg[1].DATA[6] = 0;
  870. msg[1].DATA[7] = 0;
  871. }
  872. else
  873. {
  874. msg[1].ID=0;
  875. msg[1].MSGTYPE=PCAN_MESSAGE_STANDARD;
  876. msg[1].DATA[0]=0xFF;
  877. msg[1].DATA[1]=0x00;
  878. msg[1].DATA[2]=hi_byte(800);
  879. msg[1].DATA[3]=lo_byte(800);
  880. msg[1].DATA[4]=(_side==TurnSide::Left? 1:0)|0x02;
  881. msg[1].DATA[5] = 0;
  882. msg[1].DATA[6] = 0;
  883. msg[1].DATA[7] = 0;
  884. msg[3].ID=0x386;
  885. msg[3].DATA[0]=0;
  886. msg[3].DATA[1]=120;
  887. _parking=ParkingState::None;
  888. }
  889. }
  890. void CMessageQueue::OnTurn(TPCANMsg* msg)
  891. {
  892. msg[2].ID=0x286;
  893. msg[2].MSGTYPE=PCAN_MESSAGE_STANDARD;
  894. msg[2].DATA[0]=0;
  895. msg[2].DATA[1]=0x21;
  896. if(MAX_DGREE-std::abs(_steer_angle)>_diff_degree)
  897. {
  898. int32_t speed=std::max(600,(1-std::abs(_steer_angle)/MAX_DGREE)*_turn_base);
  899. if(_workarea == WorkArea::Area_J && (_no == 0 || _no == 1 || _no == 12 || _no == 13))
  900. {
  901. speed=230;//std::max(300,(1-std::abs(_steer_angle)/MAX_DGREE)*_turn_base);
  902. }
  903. // std::cout << __FILE__ << " speed: " << speed << std::endl;
  904. msg[1].ID=0x586;
  905. msg[1].MSGTYPE=PCAN_MESSAGE_STANDARD;
  906. msg[1].LEN=8;
  907. msg[1].DATA[0]=0xFF;
  908. msg[1].DATA[1]=0x00;
  909. msg[1].DATA[2]=hi_byte(speed);
  910. msg[1].DATA[3]=lo_byte(speed);
  911. msg[1].DATA[4]=(_side==TurnSide::Left? 1:0)|0x02;
  912. msg[1].DATA[5] = 0;
  913. msg[1].DATA[6] = 0;
  914. msg[1].DATA[7] = 0;
  915. msg[2].ID=0x286;
  916. msg[2].MSGTYPE=PCAN_MESSAGE_STANDARD;
  917. msg[2].DATA[0]=0x10;
  918. msg[2].DATA[1]=0x20;
  919. msg[3].ID=0x386;
  920. msg[3].DATA[0]=60;
  921. msg[3].DATA[1]=0;
  922. }
  923. else
  924. {
  925. msg[1].ID=0;
  926. msg[1].MSGTYPE=PCAN_MESSAGE_STANDARD;
  927. msg[1].LEN=8;
  928. msg[1].DATA[0]=0xFF;
  929. msg[1].DATA[1]=0x00;
  930. msg[1].DATA[2]=hi_byte(1000);
  931. msg[1].DATA[3]=lo_byte(1000);
  932. msg[1].DATA[4]=(_side==TurnSide::Left? 1:0)|0x02;
  933. msg[1].DATA[5] = 0;
  934. msg[1].DATA[6] = 0;
  935. msg[1].DATA[7] = 0;
  936. _parking=ParkingState::Reverse;
  937. }
  938. }
  939. /*
  940. void CMessageQueue::OnPlayrecord(TPCANMsg* msg)
  941. {
  942. auto array=_radar->Get()->GetRadarInfo();
  943. auto tick=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  944. if(_play_index<frames[_rout_index].size())
  945. {
  946. for(int32_t i=0;i<5;i++)
  947. {
  948. msg[i].ID=frames[_rout_index][_play_index+i].canid;
  949. msg[i].LEN=frames[_rout_index][_play_index+i].dlc;
  950. memcpy(msg[i].DATA,frames[_rout_index][_play_index+i].data,frames[_rout_index][_play_index+i].dlc);
  951. }
  952. _play_index+=5;
  953. }
  954. else{
  955. msg[3].ID=0x386;
  956. msg[3].DATA[0]=0;
  957. msg[3].DATA[1]=120;
  958. _parking=ParkingState::None;
  959. }
  960. }
  961. */
  962. void CMessageQueue::WriteSensor(radar_node* node,int32_t count)
  963. {
  964. MessageHead Head;
  965. CIOBuffer Buffer;
  966. RemoNet::CCSensor Req;
  967. for(int32_t i=0;i<count;i++)
  968. {
  969. auto n= Req.add_node();
  970. n->set_id(i);
  971. n->set_range(node[i].range);
  972. }
  973. int32_t a=std::abs(_left_length-min_left_length) *400;
  974. a/=std::abs(max_left_length-min_left_length);
  975. Req.set_left_encode(a);
  976. a=std::abs(_right_length-min_right_length)*400;
  977. a/=std::abs(max_right_length-min_right_length);
  978. Req.set_right_encode(a);
  979. Head.Command=RemoNet::CC_Radar;
  980. Head.Length=Req.ByteSizeLong();
  981. Head.Serialize(Buffer.Buffer);
  982. auto ptr = Buffer.Buffer + MessageHead::Size();
  983. Req.SerializeToArray(ptr, Head.Length);
  984. Buffer.Length = Head.Length + MessageHead::Size();
  985. WriteBuffer(ChannelType::CHANNEL_RADAR,Buffer);
  986. }
  987. static double get_value(const sensors_chip_name *name,
  988. const sensors_subfeature *sub)
  989. {
  990. double val;
  991. int err;
  992. err = sensors_get_value(name, sub->number, &val);
  993. if (err) {
  994. val = 0;
  995. }
  996. return val;
  997. }
  998. static int get_input_value(const sensors_chip_name *name,
  999. const sensors_subfeature *sub,
  1000. double *val)
  1001. {
  1002. int err;
  1003. err = sensors_get_value(name, sub->number, val);
  1004. return err;
  1005. }
  1006. void CMessageQueue::OnNotifyPing(int64_t value)
  1007. {
  1008. double temp=0.f;
  1009. auto err=sensors_init(NULL);
  1010. if(err==0)
  1011. {
  1012. const sensors_chip_name *chip;
  1013. int chip_nr=0;
  1014. int i=0;
  1015. const sensors_feature * feature=nullptr;
  1016. while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
  1017. while ((feature = sensors_get_features(chip, &i))) {
  1018. if (feature->type==SENSORS_FEATURE_TEMP) {
  1019. auto sf = sensors_get_subfeature(chip, feature,
  1020. SENSORS_SUBFEATURE_TEMP_FAULT);
  1021. if (sf && (temp=get_value( chip , sf))) {
  1022. break;
  1023. }
  1024. else
  1025. {
  1026. sf = sensors_get_subfeature(chip, feature,SENSORS_SUBFEATURE_TEMP_INPUT);
  1027. if (sf && get_input_value(chip, sf, &temp) == 0) {
  1028. }
  1029. else
  1030. {
  1031. temp=0;
  1032. }
  1033. }
  1034. break;
  1035. }
  1036. }
  1037. }
  1038. }
  1039. //std::cout<<temp<<std::endl;
  1040. sensors_cleanup();
  1041. RemoNet::CCPing Rep;
  1042. Rep.set_tick(value);
  1043. Rep.set_temp(temp);
  1044. CIOBuffer Buffer;
  1045. MessageHead Head;
  1046. Head.Command = RemoNet::CC_Ping;
  1047. Head.Length = Rep.ByteSizeLong();
  1048. Head.Serialize(Buffer.Buffer);
  1049. auto ptr = Buffer.Buffer + MessageHead::Size();
  1050. Rep.SerializeToArray(ptr, Head.Length);
  1051. Buffer.Length = Head.Length + MessageHead::Size();
  1052. if(_peerArray[RenderPosition::FRONT]!=nullptr)
  1053. _peerArray[RenderPosition::FRONT]->SendData(Buffer);
  1054. }
  1055. /*
  1056. void CMessageQueue::StartCar()
  1057. {
  1058. _can->SetStartWrite(true);
  1059. }
  1060. */
  1061. void CMessageQueue::CheckSignal()
  1062. {
  1063. if(_curTick==0) return;
  1064. long long tick=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  1065. if(tick-_curTick>2000)
  1066. {
  1067. //_curbrake-=20.f;
  1068. int16_t value=100;//std::max(_curbrake,(int16_t)-127);
  1069. TPCANMsg msg;
  1070. memset(msg.DATA,0,sizeof(msg.DATA));
  1071. msg.ID=0x386;
  1072. msg.LEN=8;
  1073. msg.DATA[0]=hi_byte(value);
  1074. msg.DATA[1]=lo_byte(value);
  1075. _robot->Get()->Write(msg);
  1076. bStopedCar=true;
  1077. }
  1078. }
  1079. void CMessageQueue::OnSwitchDriver()
  1080. {
  1081. ChangeState(UserState::Remote);
  1082. CIOBuffer pBuffer;
  1083. //auto ptr=pBuffer.Buffer;
  1084. MessageHead Head;
  1085. Head.Command=RA_Stop;
  1086. // ptr=pBuffer.Buffer+sizeof(MessageHead);
  1087. Head.Length=0;
  1088. Head.Serialize(pBuffer.Buffer);
  1089. pBuffer.Length=MessageHead::Size();
  1090. _robot->Write(pBuffer.Buffer,pBuffer.Length);
  1091. }
  1092. void CMessageQueue::WriteBuffer(ChannelType type,CIOBuffer& pBufer)
  1093. {
  1094. if( _peerArray[type]!=nullptr)
  1095. _peerArray[type]->SendData(pBufer);
  1096. }
  1097. void CMessageQueue::SetLeftAngle(int16_t angle)
  1098. {
  1099. _left_length=angle;
  1100. }
  1101. void CMessageQueue::SetRightAngle(int16_t angle)
  1102. {
  1103. _right_length=angle;
  1104. }
  1105. /*
  1106. void CMessageQueue::WriteRadarData(radar_node* data)
  1107. {
  1108. RemoNet::CCRadar Req;
  1109. for (size_t i = 0; i < 5; i++)
  1110. {
  1111. auto node=Req.add_array();
  1112. node->set_id(data[i].rid);
  1113. for(auto & n:data[i]._info)
  1114. {
  1115. auto info=node->add_info();
  1116. // info->set_azimuth(n.second.azimuth);
  1117. // info->set_count(0);
  1118. info->set_index(n.second.index);
  1119. info->set_range(n.second.range);
  1120. // info->set_snr(n.second.snr);
  1121. // info->set_verl(n.second.verl);
  1122. }
  1123. }
  1124. if( _peerArray[ChannelType::CHANNEL_RADAR]!=nullptr)
  1125. _peerArray[ChannelType::CHANNEL_RADAR]->SendData(Buffer);
  1126. }
  1127. */
  1128. /*
  1129. #ifdef LIDAR_SENSOR
  1130. void CMessageQueue::WriteLidarPoint(const PointCloudMsg<PointXYZI>& msg,ChannelType side)
  1131. {
  1132. RemoNet::LidarPoint pt;
  1133. pt.set_is_left(side==ChannelType::CHANNEL_LEFT_LIDAR);
  1134. pt.set_frame_id(msg.frame_id);
  1135. pt.set_height(msg.height);
  1136. pt.set_width(msg.width);
  1137. pt.set_is_dense(msg.is_dense);
  1138. pt.set_seq(msg.seq);
  1139. pt.set_timestamp(msg.timestamp);
  1140. for(int i=0;i<msg.point_cloud_ptr->size();i++)
  1141. {
  1142. pt.add_data((*msg.point_cloud_ptr)[i].x);
  1143. pt.add_data((*msg.point_cloud_ptr)[i].y);
  1144. pt.add_data((*msg.point_cloud_ptr)[i].z);
  1145. pt.add_data((*msg.point_cloud_ptr)[i].intensity);
  1146. }
  1147. MessageHead Head;
  1148. CIOBuffer Buffer;
  1149. Head.Command=RemoNet::CC_LIDARDATA;
  1150. Head.Length=pt.ByteSizeLong();
  1151. Head.Serialize(Buffer.Buffer);
  1152. auto ptr = Buffer.Buffer + MessageHead::Size();
  1153. pt.SerializeToArray(ptr, Head.Length);
  1154. Buffer.Length = Head.Length + MessageHead::Size();
  1155. if( _peerArray[side]!=nullptr)
  1156. _peerArray[side]->SendData(&Buffer);
  1157. }
  1158. #endif
  1159. */
  1160. void CMessageQueue::SwitchCamera(bool front)
  1161. {
  1162. if(!_camera_inited) return;
  1163. if(_peerArray[RenderPosition::FRONT]!=nullptr) _peerArray[RenderPosition::FRONT]->SwitchCapture(front);
  1164. if(_peerArray[RenderPosition::BACK]!=nullptr) _peerArray[RenderPosition::BACK]->SwitchCapture(front);
  1165. if(_peerArray[RenderPosition::LEFT]!=nullptr) _peerArray[RenderPosition::LEFT]->SwitchCapture(front);
  1166. if(_peerArray[RenderPosition::RIGHT]!=nullptr) _peerArray[RenderPosition::RIGHT]->SwitchCapture(front);
  1167. if(_peerArray[RenderPosition::LANCHOR]!=nullptr) _peerArray[RenderPosition::LANCHOR]->SwitchCapture(front);
  1168. if(_peerArray[RenderPosition::RANCHOR]!=nullptr) _peerArray[RenderPosition::RANCHOR]->SwitchCapture(front);
  1169. }
  1170. void CMessageQueue::StopAutomous()
  1171. {
  1172. SetAutomous(false,0);
  1173. CIOBuffer pBuffer;
  1174. //auto ptr=pBuffer.Buffer;
  1175. MessageHead Head;
  1176. Head.Command=RA_Stop;
  1177. // ptr=pBuffer.Buffer+sizeof(MessageHead);
  1178. Head.Length=0;
  1179. Head.Serialize(pBuffer.Buffer);
  1180. pBuffer.Length=MessageHead::Size();
  1181. _robot->Write(pBuffer.Buffer,pBuffer.Length);
  1182. }
  1183. int32_t CMessageQueue::GetPeer()
  1184. {
  1185. return _peerId;
  1186. }
  1187. void CMessageQueue::OnTipRise(TPCANMsg* msg)
  1188. {
  1189. msg[3].ID=0x386;
  1190. msg[3].DATA[0]=120;
  1191. msg[3].DATA[1]=120;
  1192. msg[4].ID=0x486;
  1193. memset(msg[4].DATA,0,sizeof(msg[4].DATA));
  1194. // memset(msg[4].DATA,0,sizeof(msg[4].DATA));
  1195. // std::cout<<_robot->Get()->GetRiseDownValue()<<","<<_left_length<<","<<_right_length<<std::endl;
  1196. auto tick= std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  1197. if(!_start_check)
  1198. {
  1199. if(tick-_start_time>3)
  1200. {
  1201. _start_check=true;
  1202. _start_time=tick;
  1203. }
  1204. }
  1205. else
  1206. {
  1207. if(tick-_start_time>1)
  1208. {
  1209. if(pre_left_length==_left_length || pre_right_length==_right_length)
  1210. {
  1211. msg[3].ID=0x386;
  1212. msg[3].DATA[0]=0;
  1213. msg[3].DATA[1]=0;
  1214. msg[4].DATA[4]=0;
  1215. msg[4].DATA[5]=0;
  1216. msg[4].DATA[2]=0;
  1217. msg[4].DATA[3]=0;
  1218. pre_left_length=-1;
  1219. pre_right_length=-1;
  1220. _parking=ParkingState::TipOut;
  1221. return;
  1222. }
  1223. else
  1224. {
  1225. _start_time=std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  1226. pre_left_length=_left_length;
  1227. pre_right_length=_right_length;
  1228. }
  1229. }
  1230. }
  1231. if(!_start_check)
  1232. {
  1233. BYTE a=lo_byte(_robot->Get()->GetTipValue());
  1234. BYTE b=hi_byte(_robot->Get()->GetTipValue());
  1235. msg[4].DATA[4]=b;
  1236. msg[4].DATA[5]=a;
  1237. msg[4].DATA[2]=b;//hi_byte(_robot->Get()->GetTipValue());
  1238. msg[4].DATA[3]=a;//lo_byte(_robot->Get()->GetTipValue());
  1239. }
  1240. else if((_left_length>max_left_length&&_left_length<min_left_length)||(_right_length<max_right_length&&_right_length>min_right_length))
  1241. {
  1242. BYTE a=lo_byte(_robot->Get()->GetTipValue());
  1243. BYTE b=hi_byte(_robot->Get()->GetTipValue());
  1244. msg[4].DATA[4]=b;
  1245. msg[4].DATA[5]=a;
  1246. msg[4].DATA[2]=b;//hi_byte(_robot->Get()->GetTipValue());
  1247. msg[4].DATA[3]=a;//lo_byte(_robot->Get()->GetTipValue());
  1248. }
  1249. /*
  1250. BYTE a=lo_byte(_robot->Get()->GetTipValue());
  1251. BYTE b=hi_byte(_robot->Get()->GetTipValue());
  1252. if(_left_length>max_left_length&&_left_length<min_left_length)
  1253. {
  1254. msg[4].DATA[4]=b;
  1255. msg[4].DATA[5]=a;
  1256. }
  1257. if(_right_length<max_right_length&&_right_length>min_right_length)
  1258. {
  1259. msg[4].DATA[2]=b;
  1260. msg[4].DATA[3]=a;
  1261. }*/
  1262. }
  1263. CarType CMessageQueue::GetCarType()
  1264. {
  1265. return _car;
  1266. }
  1267. void CMessageQueue::OnRiseDown(TPCANMsg* msg)
  1268. {
  1269. msg[3].ID=0x386;
  1270. msg[3].DATA[0]=120;
  1271. msg[3].DATA[1]=120;
  1272. msg[4].ID=0x486;
  1273. memset(msg[4].DATA,0,sizeof(msg[4].DATA));
  1274. // std::cout<<_robot->Get()->GetRiseDownValue()<<","<<_left_length<<","<<pre_left_length<<std::endl;
  1275. auto tick= std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  1276. if(!_start_check)
  1277. {
  1278. if(tick-_start_time>3)
  1279. {
  1280. _start_check=true;
  1281. _start_time=tick;
  1282. }
  1283. }
  1284. else{
  1285. if(tick-_start_time>1)
  1286. {
  1287. if(pre_left_length==_left_length && pre_right_length==_right_length)
  1288. {
  1289. msg[3].ID=0x386;
  1290. msg[3].DATA[0]=0;
  1291. msg[3].DATA[1]=0;
  1292. msg[4].DATA[4]=0;
  1293. msg[4].DATA[5]=0;
  1294. msg[4].DATA[2]=0;
  1295. msg[4].DATA[3]=0;
  1296. pre_left_length=-1;
  1297. pre_right_length=-1;
  1298. _parking=ParkingState::None;
  1299. return;
  1300. }
  1301. else
  1302. {
  1303. _start_time=std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  1304. pre_left_length=_left_length;
  1305. pre_right_length=_right_length;
  1306. }
  1307. }
  1308. }
  1309. if(!_start_check)
  1310. {
  1311. BYTE a=lo_byte(_robot->Get()->GetRiseDownValue());
  1312. BYTE b=hi_byte(_robot->Get()->GetRiseDownValue());
  1313. msg[4].DATA[4]=a;
  1314. msg[4].DATA[5]=b;
  1315. msg[4].DATA[2]=a;//hi_byte(_robot->Get()->GetRiseDownValue());
  1316. msg[4].DATA[3]=b;//lo_byte(_robot->Get()->GetRiseDownValue());
  1317. }
  1318. else if((_left_length>max_left_length&&_left_length<min_left_length)||(_right_length<max_right_length&&_right_length>min_right_length))
  1319. {
  1320. BYTE a=lo_byte(_robot->Get()->GetRiseDownValue());
  1321. BYTE b=hi_byte(_robot->Get()->GetRiseDownValue());
  1322. msg[4].DATA[4]=a;
  1323. msg[4].DATA[5]=b;
  1324. msg[4].DATA[2]=a;//hi_byte(_robot->Get()->GetRiseDownValue());
  1325. msg[4].DATA[3]=b;//lo_byte(_robot->Get()->GetRiseDownValue());
  1326. }
  1327. /*
  1328. if((_left_length<=max_left_length||_left_length>=min_left_length)&&(_right_length>=max_right_length||_right_length<=min_right_length))
  1329. {
  1330. msg[3].ID=0x386;
  1331. msg[3].DATA[0]=0;
  1332. msg[3].DATA[1]=0;
  1333. msg[4].DATA[4]=0;
  1334. msg[4].DATA[5]=0;
  1335. msg[4].DATA[2]=0;
  1336. msg[4].DATA[3]=0;
  1337. _parking=ParkingState::None;
  1338. // _robot->Get()->StartParking(false);
  1339. }
  1340. */
  1341. }
  1342. /*
  1343. void CMessageQueue::OnDown(TPCANMsg* msg)
  1344. {
  1345. msg[3].ID=0x386;
  1346. msg[3].DATA[0]=90;
  1347. msg[3].DATA[1]=90;
  1348. msg[4].ID=0x486;
  1349. memset(msg[4].DATA,0,sizeof(msg[4].DATA));
  1350. if()
  1351. {
  1352. msg[4].ID=0x486;
  1353. msg[4].LEN=8;
  1354. msg[4].DATA[4]=0x81;
  1355. msg[4].DATA[5]=0x00;
  1356. msg[3].ID=0x386;
  1357. msg[3].DATA[0]=90;
  1358. msg[3].DATA[1]=90;
  1359. }
  1360. else
  1361. {
  1362. msg[4].ID=0x486;
  1363. msg[4].LEN=8;
  1364. msg[4].DATA[4]=0;
  1365. msg[4].DATA[4]=0;
  1366. msg[3].ID=0x386;
  1367. msg[3].DATA[0]=0;
  1368. msg[3].DATA[1]=0;
  1369. _parking=ParkingState::None;
  1370. _robot->Get()->StartParking(false);
  1371. }
  1372. }
  1373. */
  1374. void CMessageQueue::OnTipOut(TPCANMsg* msg)
  1375. {
  1376. msg[3].ID=0x386;
  1377. msg[3].DATA[0]=120;
  1378. msg[3].DATA[1]=120;
  1379. msg[4].ID=0x486;
  1380. memset(msg[4].DATA,0,sizeof(msg[4].DATA));
  1381. BYTE a=lo_byte(_robot->Get()->GetTipValue());
  1382. BYTE b=hi_byte(_robot->Get()->GetTipValue());
  1383. auto tick=std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  1384. if(tick-_start_time<tipout_time)
  1385. {
  1386. msg[4].DATA[0]=b;
  1387. msg[4].DATA[1]=a;
  1388. }
  1389. else
  1390. {
  1391. msg[3].ID=0x386;
  1392. msg[3].DATA[0]=0;
  1393. msg[3].DATA[1]=0;
  1394. msg[4].DATA[0]=0;
  1395. msg[4].DATA[1]=0;
  1396. _parking=ParkingState::None;
  1397. // _robot->Get()->StartParking(false);
  1398. }
  1399. }
  1400. void CMessageQueue::OnTipIn(TPCANMsg* msg)
  1401. {
  1402. msg[3].ID=0x386;
  1403. msg[3].DATA[0]=120;
  1404. msg[3].DATA[1]=120;
  1405. msg[4].ID=0x486;
  1406. BYTE a=lo_byte(_robot->Get()->GetTipValue());
  1407. BYTE b=hi_byte(_robot->Get()->GetTipValue());
  1408. memset(msg[4].DATA,0,sizeof(msg[4].DATA));
  1409. auto tick=std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  1410. if(tick-_start_time<tipin_time)
  1411. {
  1412. msg[4].DATA[0]=b;
  1413. msg[4].DATA[1]=a;
  1414. }
  1415. else
  1416. {
  1417. msg[3].ID=0x386;
  1418. msg[3].DATA[0]=0;
  1419. msg[3].DATA[1]=0;
  1420. msg[4].DATA[0]=0;
  1421. msg[4].DATA[1]=0;
  1422. _parking=ParkingState::None;
  1423. }
  1424. }
  1425. void CMessageQueue::OnTipDown(TPCANMsg* msg)
  1426. {
  1427. msg[3].ID=0x386;
  1428. msg[3].DATA[0]=90;
  1429. msg[3].DATA[1]=90;
  1430. msg[4].ID=0x486;
  1431. BYTE a=lo_byte(_robot->Get()->GetTipValue());
  1432. BYTE b=hi_byte(_robot->Get()->GetTipValue());
  1433. memset(msg[4].DATA,0,sizeof(msg[4].DATA));
  1434. if(_left_length>min_left_length)
  1435. {
  1436. msg[4].ID=0x486;
  1437. msg[4].LEN=8;
  1438. msg[4].DATA[4]=b;
  1439. msg[4].DATA[5]=a;
  1440. msg[3].ID=0x386;
  1441. msg[3].DATA[0]=100;
  1442. msg[3].DATA[1]=100;
  1443. }
  1444. else
  1445. {
  1446. msg[4].ID=0x486;
  1447. msg[4].LEN=8;
  1448. msg[4].DATA[4]=0;
  1449. msg[4].DATA[4]=0;
  1450. msg[3].ID=0x386;
  1451. msg[3].DATA[0]=0;
  1452. msg[3].DATA[1]=0;
  1453. _parking=ParkingState::None;
  1454. // _robot->Get()->StartParking(false);
  1455. }
  1456. }
  1457. ParkingState CMessageQueue::GetParkingState()
  1458. {
  1459. return _parking;
  1460. }
  1461. void CMessageQueue::SetParkingState(ParkingState state)
  1462. {
  1463. _parking=state;
  1464. if(_parking!=ParkingState::None)
  1465. {
  1466. _start_time=std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  1467. _start_check=false;
  1468. }
  1469. }
  1470. extern std::string getpath();
  1471. /*
  1472. void CMessageQueue::BegRecord()
  1473. {
  1474. if(_start_record==true) return;
  1475. _start_record=true;
  1476. auto path=getpath();
  1477. auto now = std::chrono::system_clock::now();
  1478. auto in_time_t = std::chrono::system_clock::to_time_t(now);
  1479. std::stringstream ss;
  1480. ss<<path<<"/../track/";
  1481. std::cout<<ss.str().c_str()<<std::endl;
  1482. if(access(ss.str().c_str(), F_OK) == -1) {
  1483. mkdir(ss.str().c_str(),S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
  1484. }
  1485. ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d-%X.track");
  1486. _ofile.open(ss.str().c_str(),std::ios::binary|std::ios::out);
  1487. }
  1488. void CMessageQueue::EndRecord()
  1489. {
  1490. if(_start_record==true)
  1491. {
  1492. _start_record=false;
  1493. _ofile.close();
  1494. }
  1495. }
  1496. void CMessageQueue::WriteRecord(TPCANMsg * msg)
  1497. {
  1498. cannet_frame frame[5];
  1499. for(int32_t i=0;i<5;i++)
  1500. {
  1501. frame[i].canid=msg[i].ID;
  1502. frame[i].dlc=msg[i].LEN;
  1503. memcpy(frame[i].data,msg[i].DATA,frame[i].dlc);
  1504. }
  1505. _ofile.write((char *)frame,sizeof(frame));
  1506. }
  1507. void CMessageQueue::LoadRecord(ROUT_LIST dir,const char * file)
  1508. {
  1509. auto path=getpath();
  1510. std::stringstream ss;
  1511. ss<<path<<"/../track/"<<file;
  1512. std::ifstream ifile(ss.str().c_str(),std::ios::in|std::ios::binary);
  1513. if(ifile.is_open())
  1514. {
  1515. long l=ifile.tellg();
  1516. ifile.seekg(0,std::ios::end);
  1517. long m=ifile.tellg();
  1518. //frames[dir]=static_cast<cannet_frame *>(malloc(m-l));
  1519. ifile.seekg(0,std::ios::beg);
  1520. cannet_frame frame;
  1521. int32_t index=0;
  1522. while(ifile.read((char * )&frame,sizeof(frame)))
  1523. {
  1524. frames[dir].push_back(frame);
  1525. index++;
  1526. }
  1527. ifile.close();
  1528. // std::cout<<frames[dir].size()<<std::endl;
  1529. }
  1530. }
  1531. void CMessageQueue::PlayRecord(ROUT_LIST index)
  1532. {
  1533. if(_parking!=ParkingState::None) return;
  1534. _rout_index=index;
  1535. _play_index=0;
  1536. _parking=ParkingState::PlayRecord;
  1537. }
  1538. */