car_sensor.cpp 24 KB

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