radar_sensor.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <stdint.h>
  2. #include <chrono>
  3. #include "../common/comm.h"
  4. #include "PCANBasic.h"
  5. #include "../common/iobuffer.h"
  6. #include "../common/sensor_socket.h"
  7. #include "radar_sensor.h"
  8. #include "message_queue.h"
  9. //#define make_int16(h,l) (((int16_t)h)<<8)|l
  10. CRadarSensor::CRadarSensor(CMessageQueue* q):_message(q)
  11. {
  12. _channelReady=false;
  13. }
  14. inline int16_t make_int16(int8_t h, int8_t l)
  15. {
  16. int16_t hi = (int16_t)(h & 0x00FF);
  17. int16_t low = (int16_t)(l & 0x00FF);
  18. return (hi << 8) | low;
  19. }
  20. void CRadarSensor::Notify(int8_t * buffer,int32_t size)
  21. {
  22. // std::vector<radar_node> _node;
  23. int32_t receivedCnt = size/CAN_MSG_LEN;
  24. cannet_frame* p = (cannet_frame*)buffer;
  25. auto tick=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  26. for(int32_t i=0;i<receivedCnt;i++)
  27. {
  28. int32_t id_normal=ntohl(p[i].canid);
  29. // assert(rid<5);
  30. int32_t message_id=(id_normal&0xF0F);
  31. switch (message_id)
  32. {
  33. case 0x70C:
  34. {
  35. //rid>>=8;
  36. // radar_info info;
  37. //radar_node n;
  38. int32_t rid=(id_normal&0x0F0)/0x10;
  39. rid-=1;
  40. bool bset=(mask&(0x1<<rid))!=0;
  41. uint32_t range=make_int16(p[i].data[2], p[i].data[3]);
  42. // _node[rid].range=(p[i].data[2]*0x100+p[i].data[3]);
  43. if(_node[rid].range>range||!bset)
  44. {
  45. _node[rid].range = range;
  46. mask|=(0x1<<rid);
  47. }
  48. // _info[rid].rcs=p[i].data[1]*0.5-50;
  49. _node[rid].radar_tick=tick;
  50. //std::cout<<_node[0].range<<" "<<_node[1].range<<" "<<_node[2].range<<" "<<_node[3].range<<" "<<_node[4].range<<" "<<_node[5].range<<" "<<_node[6].range<<" "<<std::endl;
  51. // float range =_info[rid]*0.01;
  52. // std::cout<<std::hex<<id_normal<<" "<<std::dec<<range<<" ";
  53. // _info[rid].azimuth=p[i].data[4]-90;
  54. // _info[rid].verl=(p[i].data[5]*0x100+p[i].data[6])*0.05-35;
  55. // _info[rid].snr=p[i].data[7]-127;
  56. };
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. /*
  63. bool bComplete=true;
  64. for(int32_t i=0;i<sizeof(_info)/sizeof(int32_t);i++)
  65. {
  66. if(_info[i]==-1)
  67. {
  68. bComplete=false;
  69. break;
  70. }
  71. }
  72. if(bComplete)
  73. */
  74. {
  75. // auto tick=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();;
  76. if(tick-_tick>200)
  77. if(tick-_tick>200/*&&((mask&0x1F)==0x1F)*/)
  78. {
  79. _message->WriteSensor(_node,5);
  80. mask=0;
  81. _tick=tick;
  82. }
  83. // memset(_info,-1,sizeof(_info));
  84. }
  85. //std::cout<<std::endl;
  86. /*
  87. for(int32_t i=0;i<5;i++)
  88. {
  89. _node[i].clear();
  90. }
  91. */
  92. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  93. }
  94. void CRadarSensor::Start()
  95. {
  96. _channelReady=false;
  97. memset(_node,0,sizeof(_node));
  98. _tick=std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();;
  99. /*
  100. CIOBuffer pBuffer;
  101. cannet_frame* frame=reinterpret_cast<cannet_frame *>(pBuffer.Buffer);
  102. memset(frame->data,0,sizeof(frame->data));
  103. frame->dlc=2;
  104. frame->canid=0x0000;
  105. frame->data[0]=0x01;
  106. frame->data[1]=0x00;
  107. _socket->Write(pBuffer.Buffer,sizeof(cannet_frame));
  108. */
  109. }
  110. void CRadarSensor::SetSensorSocket(SensorSocket<CRadarSensor>* can)
  111. {
  112. _socket=can;
  113. }
  114. void CRadarSensor::Stop()
  115. {
  116. _channelReady=false;
  117. }