car_sensor.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. #include <stdint.h>
  2. #include <thread>
  3. #include <sstream>
  4. #include <iomanip>
  5. #include <ctime>
  6. #include <fstream>
  7. #include "PCANBasic.h"
  8. #include "../common/comm.h"
  9. #include "../common/iobuffer.h"
  10. #include "../common/can_sensor.h"
  11. #include "../common/sensor_socket.h"
  12. #include "protocol.pb.h"
  13. #include "car_sensor.h"
  14. #include "radar_sensor.h"
  15. #include "message_queue.h"
  16. #include <sys/stat.h>
  17. #include <sys/types.h>
  18. //#define make_int16(h,l) (((int16_t)h)<<8)|l
  19. #pragma pack(1)
  20. struct can_array
  21. {
  22. int32_t count;
  23. cannet_frame frames[3];
  24. };
  25. #pragma pack()
  26. inline int16_t make_int16(int8_t h, int8_t l)
  27. {
  28. int16_t hi = (int16_t)(h & 0x00FF);
  29. int16_t low = (int16_t)(l & 0x00FF);
  30. return (hi << 8) | low;
  31. }
  32. CCarSensor::CCarSensor(CMessageQueue * q):_message(q)
  33. {
  34. //memset(frames,0,sizeof(frames));
  35. }
  36. CCarSensor::~CCarSensor()
  37. {
  38. }
  39. bool CCarSensor::Start()
  40. {
  41. _range_count=0;
  42. _channelReady=false;
  43. memset(_msg,0,sizeof(_msg));
  44. _can=std::make_unique<CCanSensor>();
  45. _log=std::make_unique<CEventLog>();
  46. TPCANStatus b=_can->Initialize();
  47. if(b==PCAN_ERROR_OK)
  48. {
  49. _can_ready=true;
  50. _thread=std::thread(&CCarSensor::Run,this);
  51. _can_thread=std::thread(&CCarSensor::CanSendProc,this);
  52. }
  53. else
  54. {
  55. std::cout<<b<<std::endl;
  56. }
  57. _log->Start();
  58. memset(&_data,0,sizeof(_data));
  59. return b==PCAN_ERROR_OK;
  60. }
  61. void CCarSensor::Stop()
  62. {
  63. // _channelReady=false;
  64. _run=false;
  65. _can_run=false;
  66. _can_thread.join();
  67. _can_ready=false;
  68. _can->Uninitialize();
  69. _thread.join();
  70. }
  71. void CCarSensor::CanSendProc()
  72. {
  73. _can_run=true;
  74. static int32_t brake_value=0;
  75. while(_can_run)
  76. {
  77. volatile int64_t tick=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  78. // if(_brake<100)
  79. // {
  80. EventType type=EventType::NORMAL;
  81. if(_message->GetParkingState()!=ParkingState::None)
  82. {
  83. _message->GetParkCanMessage(_msg);
  84. }
  85. else{
  86. if(_automous==true)
  87. {
  88. if(_brake>100)
  89. {
  90. _msg[3].DATA[1]=_brake;
  91. }
  92. }
  93. if(tick-_last_can_uptime>500)
  94. {
  95. _msg[3].DATA[0]=0;
  96. brake_value=std::min(++brake_value,200);
  97. _msg[3].DATA[1]=brake_value>>1;
  98. _log->SetEvent(EventType::OFFLINE,_steer,0,brake_value>>1);
  99. }
  100. else if(_data.engine_rpm<500)
  101. {
  102. _msg[3].DATA[0]=0;
  103. brake_value=std::min(++brake_value,200);
  104. _msg[3].DATA[1]=brake_value>>1;
  105. _log->SetEvent(EventType::LOW_RPM,_steer,0,brake_value>>1);
  106. }
  107. else
  108. {
  109. _log->SetEvent(EventType::NORMAL,0,0,0);
  110. brake_value=0;
  111. }
  112. auto * node=_message->GetRadar();
  113. // std::cout<<"tick :"<<tick-node[0].radar_tick<<" range :"<<node[0].range<< std::endl;
  114. {
  115. if((tick-node[0].radar_tick>5000)|| node[0].range<100)
  116. {
  117. std::cout << "tick :"<<node[0].radar_tick<<" range :"<<node[0].range<< std::endl;
  118. _range_count++;
  119. }
  120. else
  121. {
  122. _range_count=0;
  123. }
  124. if(_range_count>=10)
  125. {
  126. _log->SetEvent(EventType::EMERGENCY,_steer,0,120);
  127. _msg[3].DATA[0]=0;
  128. _msg[3].DATA[1]=120;
  129. }
  130. }
  131. }
  132. // }
  133. // else{
  134. // _msg[3].DATA[0]=0;
  135. // _msg[3].DATA[1]=100;
  136. // }
  137. // if(tick-_last_can_uptime<500)
  138. {
  139. std::lock_guard<std::mutex> lg(_last_can_lock);
  140. for(int32_t i=0;i<sizeof(_msg)/sizeof(TPCANMsg);i++)
  141. {
  142. if(_msg[i].ID!=0)
  143. {
  144. Write(_msg[i]);
  145. // _can->Write(&_msg[i]);
  146. }
  147. }
  148. }
  149. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  150. }
  151. }
  152. void CCarSensor::Run()
  153. {
  154. _run=true;
  155. while(_run)
  156. {
  157. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  158. /* for(int32_t i=0;i<sizeof(frames)/sizeof(cannet_frame);i++)
  159. {
  160. frames[i].canid=0;
  161. }
  162. */
  163. TPCANMsg CANMsg;
  164. TPCANTimestamp CANTimeStamp;
  165. TPCANStatus ret=PCAN_ERROR_OK;
  166. do {
  167. if(_can_ready==true)
  168. {
  169. ret=_can->Read(&CANMsg,&CANTimeStamp);
  170. if(ret!=PCAN_ERROR_QRCVEMPTY)
  171. {
  172. Notify(CANMsg);
  173. }
  174. memset(&CANMsg,0,sizeof(CANMsg));
  175. memset(&CANTimeStamp,0,sizeof(CANTimeStamp));
  176. }
  177. else
  178. {
  179. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  180. }
  181. if(_run==false) break;
  182. }while(!(ret&PCAN_ERROR_QRCVEMPTY));
  183. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  184. if(!_automous)
  185. {
  186. /*
  187. RemoNet::CCCanMsg req;
  188. for(int32_t i=0;i<sizeof(frames)/sizeof(cannet_frame);i++)
  189. {
  190. if(frames[i].canid!=0)
  191. {
  192. auto frame=req.add_frams();
  193. frame->set_canid(frames[i].canid);
  194. frame->set_dlc(frames[i].dlc);
  195. frame->set_data(frames[i].data,frames[i].dlc);
  196. }
  197. }
  198. */
  199. // std::cout<<_data.engine_rpm<<","<<_data.brake_pressure<<","<<_data.gear<<","<<_data.gearbox_oil_pressure<<","<<_data.gearbox_oil_temp<<","<<_data.speed<<","<<_data.work_pressure<<std::endl;
  200. RemoNet::State req;
  201. req.set_brake_pressure(_data.brake_pressure);
  202. req.set_engine_rpm(_data.engine_rpm);
  203. req.set_engine_pressure(_data.engine_pressure);
  204. //req.set_gear(_data.gear);
  205. req.set_gearbox_oil_pressure(_data.gearbox_oil_pressure);
  206. req.set_gearbox_oil_temp(_data.gearbox_oil_temp);
  207. req.set_speed(_data.speed);
  208. req.set_work_pressure(_data.work_pressure);
  209. req.set_cold_water(_data.cold_water);
  210. req.set_steer_angle(_data.steer_angle);
  211. req.set_left_lock(_data.left_lock_status);
  212. req.set_right_lock(_data.right_lock_status);
  213. MessageHead Head;
  214. CIOBuffer pBuffer;
  215. Head.Command = RemoNet::CC_STATE;
  216. Head.Length = req.ByteSizeLong();
  217. Head.Serialize(pBuffer.Buffer);
  218. auto ptr = pBuffer.Buffer + MessageHead::Size();
  219. req.SerializeToArray(ptr, Head.Length);
  220. pBuffer.Length = MessageHead::Size() + Head.Length;
  221. if(_channelReady)
  222. _message->WriteBuffer(ChannelType::CHANNEL_CAR,pBuffer);
  223. }
  224. }
  225. }
  226. void CCarSensor::SetAutomous(bool value,int32_t delay)
  227. { if(delay==0)
  228. _automous=value;
  229. else
  230. {
  231. _delayseconds=delay;
  232. _delayset=value;
  233. _delay=std::thread(&CCarSensor::DelayFn,this);
  234. _delay.detach();
  235. }
  236. }
  237. void CCarSensor::DelayFn()
  238. {
  239. std::this_thread::sleep_for(std::chrono::seconds(_delayseconds));
  240. _automous=_delayset;
  241. }
  242. /*
  243. bool CCarSensor::DriverAnalog(int32_t steer,int32_t throttle,int32_t brake,bool onoff)
  244. {
  245. CIOBuffer pBuffer;
  246. cannet_frame* frame=reinterpret_cast<cannet_frame *>(pBuffer.Buffer);
  247. memset(frame->data,0,sizeof(frame->data));
  248. frame->canid=htonl(0X386);
  249. frame->dlc=8;
  250. frame->data[0]=throttle&0XFF;
  251. frame->data[1]=brake&0XFF;
  252. frame->data[2]=steer&0xFF;
  253. frame->data[6]=onoff?1:0;
  254. _socket->Write(&pBuffer);
  255. return true;
  256. }
  257. bool CCarSensor::EmbraceAnalog(int32_t tip,int32_t zoomL,int32_t zoomR,bool onoff)
  258. {
  259. CIOBuffer pBuffer;
  260. cannet_frame* frame=reinterpret_cast<cannet_frame *>(pBuffer.Buffer);
  261. memset(frame->data,0,sizeof(frame->data));
  262. frame->canid=htonl(0x486);
  263. frame->dlc=8;
  264. frame->data[0]=tip&0xFF;
  265. frame->data[2]=zoomL&0xFF;
  266. frame->data[4]=zoomR&0xFF;
  267. frame->data[6]=onoff?1:0;
  268. _socket->Write(&pBuffer);
  269. return true;
  270. }
  271. */
  272. void CCarSensor::SetSensorSocket(SensorSocket<CCarSensor> *s)
  273. {
  274. _socket=s;
  275. }
  276. void CCarSensor::SetChannelReady(bool ret)
  277. {
  278. _channelReady=ret;
  279. }
  280. inline int8_t hi_byte(int16_t value)
  281. {
  282. int8_t hi =(int8_t)((value & 0xFF00) >> 8);
  283. return hi;
  284. }
  285. inline int8_t lo_byte(int16_t value)
  286. {
  287. int8_t lo = (int8_t)(value & 0xff);
  288. return lo;
  289. }
  290. void CCarSensor::OnMessage(cannet_frame* frames,int32_t count)
  291. {
  292. std::lock_guard<std::mutex> lg(_last_can_lock);
  293. _last_can_uptime= std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  294. for(int32_t i=0;i<count;i++)
  295. {
  296. cannet_frame& frame=frames[i];
  297. // TPCANMsg msg;
  298. // msg.LEN=frame.dlc;
  299. // msg.MSGTYPE=PCAN_MESSAGE_STANDARD;
  300. int32_t canid=frame.canid;
  301. switch(canid)
  302. {
  303. /*case 0x701:
  304. if(frame.data[5]==0)
  305. {
  306. _automous=_finished=false;
  307. }
  308. else
  309. {
  310. _automous=frame.data[5]!=0&&!_finished;
  311. }
  312. break; */
  313. case 0x181:
  314. {
  315. if(frame.data[0]==0x08)
  316. {
  317. if(_message->GetParkingState()!=ParkingState::None)
  318. {
  319. _message->SetParkingState(ParkingState::None);
  320. }
  321. // if(_automous)
  322. {
  323. _automous=false;
  324. _message->StopAutomous();
  325. }
  326. }
  327. _msg[0].ID=0x186;
  328. _msg[0].LEN=frame.dlc;
  329. _msg[0].MSGTYPE=PCAN_MESSAGE_STANDARD;
  330. memcpy(_msg[0].DATA,frame.data,frame.dlc);
  331. _front_view= (_msg[0].DATA[1]&0x01)==1;
  332. _message->SwitchCamera(_front_view);
  333. if((_msg[0].DATA[2]==0x01||_msg[0].DATA[2]==0x02)&&std::abs(_rise_down_value)>100)
  334. {
  335. _message->SetParkingState(ParkingState::RiseDown);
  336. }
  337. /* std::cout<<std::hex<<"ID:"<<msg.ID<<std::endl;
  338. for(int i=0;i<msg.LEN;i++)
  339. {
  340. int16_t t=msg.DATA[i];
  341. std::cout<<std::hex<<t<<" ";
  342. }
  343. std::cout<<std::endl;
  344. */
  345. }
  346. break;
  347. case 0x185:
  348. if(_message->GetParkingState()!=ParkingState::None) return;
  349. _msg[1].ID=_automous?0:0x586;
  350. _msg[1].LEN=frame.dlc;
  351. _msg[1].MSGTYPE=PCAN_MESSAGE_STANDARD;
  352. _steer=make_int16(frame.data[2],frame.data[3]);
  353. if(_message->GetCarType()==CarType::KRESS)
  354. {
  355. int16_t value=_steer;
  356. value/=2;
  357. frame.data[2] = hi_byte(value);
  358. frame.data[3] = lo_byte(value);
  359. }
  360. /*
  361. if(_front_view==false)
  362. {
  363. frame.data[4]^=1;
  364. }
  365. */
  366. // for(int i=0;i<frame.dlc;i++)
  367. // {
  368. // int16_t t=frame.data[i];
  369. // std::cout<<std::hex<<t<<" ";
  370. // }
  371. // std::cout<<std::endl;
  372. // frame.data[0]=0xFF;
  373. memcpy(_msg[1].DATA,frame.data,frame.dlc);
  374. break;
  375. case 0x281:
  376. {
  377. if(_message->GetParkingState()!=ParkingState::None) return;
  378. _msg[2].ID=_automous?0:0x286;
  379. _msg[2].LEN=frame.dlc;
  380. _msg[2].MSGTYPE=PCAN_MESSAGE_STANDARD;
  381. /* if(_automous)
  382. {
  383. frame.data[0]=0x08;
  384. frame.data[1]=0;
  385. memcpy(msg.DATA,frame.data,frame.dlc);
  386. }
  387. else
  388. */
  389. {
  390. memcpy(_msg[2].DATA,frame.data,frame.dlc);
  391. }
  392. }
  393. break;
  394. case 0x381:
  395. _brake=frame.data[1];
  396. if(_message->GetParkingState()!=ParkingState::None) return;
  397. _msg[3].ID=_automous?0:0x386;
  398. _msg[3].LEN=frame.dlc;
  399. _msg[3].MSGTYPE=PCAN_MESSAGE_STANDARD;
  400. memcpy(_msg[3].DATA,frame.data,frame.dlc);
  401. if(_msg[3].DATA[6]==0x0F&&std::abs(_tip_value)>2000)//&&_data.left_lock_status!=0&&_data.right_lock_status!=0)
  402. {
  403. _message->SetParkingState(_tip_value>0?ParkingState::TipIn:ParkingState::TipRise);
  404. }
  405. break;
  406. case 0x481:
  407. if(_message->GetParkingState()!=ParkingState::None) return;
  408. _msg[4].ID=_automous?0:0x486;
  409. _msg[4].LEN=frame.dlc;
  410. _msg[4].MSGTYPE=PCAN_MESSAGE_STANDARD;
  411. _rise_down_value=make_int16(_msg[4].DATA[5],_msg[4].DATA[4]);
  412. if(_rise_down_value==0)
  413. _rise_down_value=make_int16(_msg[4].DATA[3],_msg[4].DATA[2]);
  414. // std::cout<<std::hex<<_rise_down_value<<std::endl;
  415. _tip_value=make_int16(_msg[4].DATA[0],_msg[4].DATA[1]);
  416. memcpy(_msg[4].DATA,frame.data,frame.dlc);
  417. break;
  418. }
  419. // if(msg.ID!=0)
  420. // _can->Write(&msg);
  421. }
  422. }
  423. void CCarSensor::Write(TPCANMsg& msg)
  424. {
  425. if(_can_ready==false)
  426. {
  427. auto ret= _can->Uninitialize();
  428. std::cout<<ret<<std::endl;
  429. ret= _can->Initialize();
  430. _can_ready=ret==PCAN_ERROR_OK;
  431. std::cout<<ret<<std::endl;
  432. return;
  433. }
  434. auto ret= _can->Write(&msg);
  435. if(ret!=PCAN_ERROR_OK)
  436. {
  437. std::cout<<ret<<std::endl;
  438. ret= _can->Uninitialize();
  439. std::cout<<ret<<std::endl;
  440. ret= _can->Initialize();
  441. _can_ready=ret==PCAN_ERROR_OK;
  442. std::cout<<ret<<std::endl;
  443. }
  444. }
  445. void CCarSensor::Notify(int8_t * data,int32_t size)
  446. {
  447. ////if(!_automous) return;
  448. MessageHead head;
  449. head.Deserialize(data);
  450. auto ptr=data+sizeof(MessageHead);
  451. switch (head.Command)
  452. {
  453. case AR::AR_Finish:
  454. {
  455. ARFinish * node=(ARFinish *)ptr;
  456. _message->OnGoalFinish((WorkArea)node->area,node->no);
  457. RemoNet::CSMoveEnd Req;
  458. Req.set_area(node->area);
  459. Req.set_no(node->no);
  460. Req.set_uid(0);
  461. CIOBuffer buffer;
  462. MessageHead Head;
  463. Head.Command=RemoNet::CS_MoveEnd;
  464. Head.Length=Req.ByteSize();
  465. Head.Serialize(buffer.Buffer);
  466. auto ptr=buffer.Buffer+MessageHead::Size();
  467. Req.SerializeToArray(ptr,Head.Length);
  468. buffer.Length=MessageHead::Size()+Head.Length;
  469. _message->WriteBuffer(buffer);
  470. _message->SetAutomous(false,0);
  471. }
  472. break;
  473. case AR::AR_NDTPos:
  474. {
  475. ARNdtPos* node=(ARNdtPos *)ptr;
  476. _message->SetNdtPos(node);
  477. }
  478. break;
  479. case AR::AR_CTRL:
  480. {
  481. if(_automous)
  482. {
  483. _last_can_uptime= std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  484. // int64_t tick= std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  485. if(_last_can_uptime-_last_encode_uptime>5000)
  486. {
  487. _message->StopAutomous();
  488. RemoNet::MoveRet Req;
  489. Req.set_desc(RemoNet::MoveDesc::Move_Encode_Fail);
  490. Req.set_peer(_message->GetPeer());
  491. CIOBuffer pBuffer;
  492. MessageHead Head;
  493. Head.Command = RemoNet::CS_MoveRet;
  494. Head.Length = Req.ByteSizeLong();
  495. Head.Serialize(pBuffer.Buffer);
  496. auto ptr = pBuffer.Buffer + MessageHead::Size();
  497. Req.SerializeToArray(ptr, Head.Length);
  498. pBuffer.Length = MessageHead::Size() + Head.Length;
  499. _message->WriteBuffer(pBuffer);
  500. _automous=false;
  501. return;
  502. }
  503. can_array* array=reinterpret_cast<can_array *>(ptr);
  504. for(auto i=0;i<array->count;i++)
  505. {
  506. // TPCANMsg msg;
  507. cannet_frame& frame=array->frames[i];
  508. if(frame.canid==0x386)
  509. {
  510. int16_t accel=frame.data[0];
  511. int16_t brake=frame.data[1];
  512. std::cout<<"accel :"<<accel<<" brake :"<<brake<< std::endl;
  513. auto * node=_message->GetRadar();
  514. // std::cout<<"tick :"<<tick-node[0].radar_tick<<" range :"<<node[0].range<< std::endl;
  515. if((_last_can_uptime-node[0].radar_tick>5000)|| node[0].range<250)
  516. {
  517. std::cout << "tick :"<<_last_can_uptime-node[0].radar_tick<<" range :"<<node[0].range<< std::endl;
  518. _range_count++;
  519. }
  520. else
  521. {
  522. _range_count=0;
  523. }
  524. if(_range_count>=100||_brake>100)
  525. {
  526. frame.data[0]=0;
  527. frame.data[1]=100;
  528. }
  529. _msg[3].ID=frame.canid;
  530. _msg[3].LEN=frame.dlc;
  531. memcpy(_msg[3].DATA,frame.data,frame.dlc);
  532. }
  533. else if (frame.canid==0x586) {
  534. _msg[1].ID=frame.canid;
  535. _msg[1].LEN=frame.dlc;
  536. _msg[1].MSGTYPE=PCAN_MESSAGE_STANDARD;
  537. memcpy(_msg[1].DATA,frame.data,frame.dlc);
  538. }
  539. else if(frame.canid==0x286)
  540. {
  541. _msg[2].ID=frame.canid;
  542. _msg[2].LEN=frame.dlc;
  543. _msg[2].MSGTYPE=PCAN_MESSAGE_STANDARD;
  544. memcpy(_msg[2].DATA,frame.data,frame.dlc);
  545. }
  546. }
  547. }
  548. }
  549. break;
  550. }
  551. }
  552. void CCarSensor::Notify(TPCANMsg& canmsg)
  553. {
  554. // int32_t receivedCnt = size/CAN_MSG_LEN;
  555. // cannet_frame* p = (cannet_frame*)buffer;
  556. // for(int i=0; i<receivedCnt; i++)
  557. //{
  558. // p->canid=htonl(p->canid);
  559. int32_t index=-1;
  560. int32_t canid=canmsg.ID;
  561. //std::cout<<std::hex<<canid<<std::endl;
  562. switch (canid)
  563. {
  564. case 0x1F0:
  565. index=0;
  566. break;
  567. case 0x1F1:
  568. _data.work_pressure=make_int16(canmsg.DATA[1],canmsg.DATA[0]);//工作系统压力
  569. _data.brake_pressure=make_int16(canmsg.DATA[5],canmsg.DATA[4]);//制动压力
  570. break;;
  571. case 0x1F2:
  572. index=3;
  573. break;
  574. case 0x1F3:
  575. _data.engine_rpm=make_int16(canmsg.DATA[1],canmsg.DATA[0]);
  576. _data.engine_pressure=canmsg.DATA[4]+canmsg.DATA[5]*256;
  577. _data.cold_water=make_int16(canmsg.DATA[7],canmsg.DATA[6]);
  578. break;
  579. case 0x1F4:
  580. _data.gearbox_oil_temp=make_int16(canmsg.DATA[1],canmsg.DATA[0]);
  581. _data.gearbox_oil_pressure=make_int16(canmsg.DATA[3],canmsg.DATA[2]);
  582. //_data.gear=make_int16(canmsg.DATA[5],canmsg.DATA[4]);
  583. break;
  584. case 0x1F5:
  585. index=6;
  586. break;
  587. case 0x1F6:
  588. index=7;
  589. break;
  590. case 0x283:
  591. _data.left_lock_status= (canmsg.DATA[0]&0x2)!=0;
  592. _data.right_lock_status=(canmsg.DATA[0]&0x4)!=0;
  593. break;
  594. case 0x2F2:
  595. index=8;
  596. break;
  597. case 0x2F3:
  598. index=9;
  599. break;
  600. case 0x2F4:
  601. index=10;
  602. break;
  603. case 0x3F0:
  604. index=11;
  605. break;
  606. case 0x3F1:
  607. index=12;
  608. break;
  609. case 0x3F2:
  610. index=13;
  611. break;
  612. case 0x3F3:
  613. index=14;
  614. break;
  615. case 0x3F4:
  616. index=15;
  617. break;
  618. case 0x4F0:
  619. index=16;
  620. break;
  621. case 0x4F1:
  622. index=17;
  623. break;
  624. case 0x4F2:
  625. index=18;
  626. break;
  627. case 0x4F3:
  628. _data.speed=make_int16(canmsg.DATA[4],canmsg.DATA[5]);
  629. break;
  630. case 0x4F4:
  631. index=20;
  632. break;
  633. case 0x189:
  634. {
  635. _last_encode_uptime=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  636. int32_t l0=canmsg.DATA[0]&0x000000ff;
  637. int32_t l1=canmsg.DATA[1]&0x000000ff;
  638. int32_t l2=canmsg.DATA[2]&0x000000ff;
  639. int32_t l3=canmsg.DATA[3]&0x000000ff;
  640. int16_t last_steer_angle= (l1<<8)|l0;
  641. // std::cout<<last_steer_angle<<std::endl;
  642. _data.steer_angle=last_steer_angle;
  643. _message->SetSteerAngle(last_steer_angle);
  644. // if(_automous) // 可以随时发
  645. {
  646. CIOBuffer pBuffer;
  647. MessageHead Head;
  648. Head.Command=RA::RA_Encode,
  649. Head.Length=sizeof(cannet_frame);
  650. Head.Serialize(pBuffer.Buffer);
  651. auto ptr=pBuffer.Buffer+sizeof(MessageHead);
  652. cannet_frame * data=reinterpret_cast<cannet_frame *>(ptr);
  653. data->canid=canmsg.ID;
  654. data->dlc=canmsg.LEN;
  655. memcpy(data->data,canmsg.DATA,canmsg.LEN);
  656. pBuffer.Length=Head.Length+sizeof(MessageHead);
  657. _socket->Write(pBuffer.Buffer, pBuffer.Length);
  658. }
  659. /*index=21;
  660. int32_t l0=canmsg.DATA[0]&0x000000ff;
  661. int32_t l1=canmsg.DATA[1]&0x000000ff;
  662. int32_t l2=canmsg.DATA[2]&0x000000ff;
  663. int32_t l3=canmsg.DATA [3]&0x000000ff;
  664. int32_t value= (l3<<24)|(l2<<16)|(l1<<8)|l0;
  665. */
  666. // std::cout<<value<<std::endl;
  667. }
  668. break;
  669. default:
  670. break;
  671. }
  672. /*
  673. if(index!=-1)
  674. {
  675. frames[index].canid=canid;
  676. frames[index].dlc=canmsg.LEN;
  677. memcpy(frames[index].data,canmsg.DATA,sizeof(canmsg.DATA));
  678. }
  679. */
  680. }
  681. int64_t CCarSensor::GetLastEncodeTime()
  682. {
  683. return _last_encode_uptime;
  684. }
  685. bool CCarSensor::IsFrontView()
  686. {
  687. return _front_view;
  688. }
  689. int16_t CCarSensor::GetRiseDownValue() const
  690. {
  691. return _rise_down_value;
  692. }
  693. int16_t CCarSensor::GetTipValue() const
  694. {
  695. return _tip_value;
  696. }