radar_sensor.cpp 3.8 KB

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