message_queue.cpp 51 KB

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