radar_can.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "radar_can.h"
  2. #include "message_queue.h"
  3. CCanRadarSensor::CCanRadarSensor(CMessageQueue* q) :_message(q)
  4. {
  5. _run = false;
  6. }
  7. void CCanRadarSensor::SetCanBusSensor(SensorCanBus<CCanRadarSensor>* can)
  8. {
  9. _canbus = can;
  10. }
  11. void CCanRadarSensor::Start()
  12. {
  13. if(!_run)
  14. {
  15. _run = true;
  16. _count = 0;
  17. }
  18. }
  19. void CCanRadarSensor::Run()
  20. {
  21. if (_run)
  22. {
  23. }
  24. }
  25. void CCanRadarSensor::Stop()
  26. {
  27. if (_run)
  28. {
  29. _run = false;
  30. _count = 0;
  31. }
  32. }
  33. uint16_t CCanRadarSensor::bcd2dec(uint8_t data0, uint8_t data1)
  34. {
  35. unsigned char Dst[4];
  36. memset(Dst, 0, 4);
  37. uint16_t i = (((data0 & 0x00FF) << 8) | (data1 & 0x00FF));
  38. sprintf((char *)Dst, "%x", i);
  39. return strtol((const char *)Dst, NULL, 10);
  40. }
  41. #include <chrono>
  42. void CCanRadarSensor::Notify(struct can_frame *date)
  43. {
  44. //auto start = std::chrono::system_clock::now();
  45. if(_run)
  46. {
  47. if (date -> can_id == 0x61b){
  48. int c_l_1 = date->data[1];
  49. int c_l_2 = date->data[2];
  50. c_l_1 = c_l_1 * 32;
  51. c_l_2 = c_l_2 >> 3;
  52. int32_t dis_long2 = ((c_l_1 + c_l_2) * 0.2f - 500.0f) * 1000; // 毫米 纵向
  53. _data.r0 = dis_long2;
  54. _count++;
  55. }
  56. else if (date -> can_id == 0x62b){
  57. int c_l_1 = date->data[1];
  58. int c_l_2 = date->data[2];
  59. c_l_1 = c_l_1 * 32;
  60. c_l_2 = c_l_2 >> 3;
  61. int32_t dis_long2 = ((c_l_1 + c_l_2) * 0.2f - 500.0f) * 1000; // 毫米 纵向
  62. _data.r1 = dis_long2;
  63. // _data.r2 = dis_long2;
  64. _count++;
  65. }
  66. else if (date -> can_id == 0x63b){
  67. int c_l_1 = date->data[1];
  68. int c_l_2 = date->data[2];
  69. c_l_1 = c_l_1 * 32;
  70. c_l_2 = c_l_2 >> 3;
  71. int32_t dis_long2 = ((c_l_1 + c_l_2) * 0.2f - 500.0f) * 1000; // 毫米 纵向
  72. _data.r2 = dis_long2;
  73. // _data.r4 = dis_long2;
  74. _count++;
  75. }
  76. else if (date -> can_id == 0x64b){
  77. int c_l_1 = date->data[1];
  78. int c_l_2 = date->data[2];
  79. c_l_1 = c_l_1 * 32;
  80. c_l_2 = c_l_2 >> 3;
  81. int32_t dis_long2 = ((c_l_1 + c_l_2) * 0.2f - 500.0f) * 1000; // 毫米 纵向
  82. _data.r3 = dis_long2;
  83. // _data.r6 = dis_long2;
  84. _count++;
  85. }
  86. else if (date-> can_id== 0x611)
  87. {
  88. _data.r4 = bcd2dec(date->data[0], date->data[1]);
  89. _data.r5 = bcd2dec(date->data[2], date->data[3]);
  90. _data.r6 = bcd2dec(date->data[4], date->data[5]);
  91. _data.r7 = bcd2dec(date->data[6], date->data[7]);
  92. _count += 4;
  93. }
  94. //std::cout << "radar :" << _data.r0 << " " << _data.r1 << " " << _data.r2 << " " << _data.r3 << " "
  95. //<< _data.r4 << " " << _data.r5 << " " << _data.r6 << " " << _data.r7 << " "<< std::endl;
  96. // _count++;
  97. if (_count > 8)
  98. {
  99. _message->WriteRadarData(_data);
  100. _count = 0;
  101. }
  102. //std::this_thread::sleep_for(std::chrono::milliseconds(20));
  103. // 执行一些操作
  104. //auto end = std::chrono::system_clock::now();
  105. //std::chrono::duration<double> elapsed = end - start;
  106. //std::cout << "Elapsed time: " << elapsed.count() << " seconds" << std::endl;
  107. }
  108. }