radar_sensor.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <stdint.h>
  2. #include <chrono>
  3. #include "../common/comm.h"
  4. #include "../common/iobuffer.h"
  5. #include "../common/sensor_socket.h"
  6. #include "radar_sensor.h"
  7. #include "message_queue.h"
  8. uint16_t make_uint16(uint8_t h,uint8_t l)
  9. {
  10. return (h * 256+ l);
  11. }
  12. uint16_t GetMinDate(uint8_t * data,int Num)
  13. {
  14. uint8_t Dst[4] = {0};
  15. for(int i = 0 ,j = 0; i < Num ; i++,j+=2)
  16. Dst[i] = make_uint16(*(data + j),*(data + j + 1));
  17. uint16_t loop, smallest;
  18. smallest = Dst[0];
  19. for(loop = 1; loop < Num; loop++)
  20. {
  21. if(smallest > Dst[loop])
  22. smallest = Dst[loop];
  23. }
  24. return smallest;
  25. }
  26. CRadarSensor::CRadarSensor(CMessageQueue* q):_message(q)
  27. {
  28. memset(&_message->_Radardata,0,sizeof(_message->_Radardata));
  29. _run = false;
  30. }
  31. uint16_t CRadarSensor::bcd2dec(const int8_t *data)
  32. {
  33. /*int32_t h=data[0]&0x00FF;
  34. int32_t l=data[1]&0x00FF;
  35. return h * 10 + l;*/
  36. unsigned char Dst[4];
  37. memset(Dst, 0, 4);
  38. uint16_t i = (((data[0] & 0x00FF) << 8) | (data[1] & 0x00FF));
  39. sprintf((char *)Dst,"%x", i);
  40. return strtol((const char *)Dst, NULL, 10);
  41. }
  42. void CRadarSensor::CRC_DYP(uint8_t *buf)
  43. {
  44. buf[4] = ((buf[0] + buf[1] + buf[2] + buf[3]) & 0x00ff);
  45. }
  46. void CRadarSensor::Notify(int8_t * buffer,int32_t size)
  47. {
  48. int i = 0;
  49. int8_t source[13];
  50. if(_run)
  51. {
  52. for(i = 0 ; i < size ; i += 13)
  53. {
  54. memset(source,0,13);
  55. memcpy(source,buffer + i,13);
  56. switch(source[2])
  57. {
  58. case 0x01:
  59. _message->_Radardata.r1 = GetMinDate((uint8_t *)source + 4,2);
  60. _message->_Radardata.r2 = GetMinDate((uint8_t *)source + 8,2);
  61. break;
  62. case 0x02:
  63. _message->_Radardata.r3 = GetMinDate((uint8_t *)source,4);
  64. break;
  65. case 0x03:
  66. _message->_Radardata.r4 = GetMinDate((uint8_t *)source,4);
  67. break;
  68. case 0x04:
  69. _message->_Radardata.r5 = GetMinDate((uint8_t *)source + 4,2);
  70. _message->_Radardata.r6 = GetMinDate((uint8_t *)source + 8,2);
  71. break;
  72. }
  73. }
  74. _count++;
  75. if (_count > 8)
  76. {
  77. _message->WriteRadarData(_message->_Radardata);
  78. _count = 0;
  79. }
  80. }
  81. }
  82. unsigned int CRadarSensor::CRC16(uint8_t *buf, int len)
  83. {
  84. unsigned int crc = 0xFFFF;
  85. for (int pos = 0; pos < len; pos++)
  86. {
  87. crc ^= (unsigned int)buf[pos]; // XOR byte into least sig. byte of crc
  88. for (int i = 8; i != 0; i--)
  89. { // Loop over each bit
  90. if ((crc & 0x0001) != 0)
  91. { // If the LSB is set
  92. crc >>= 1; // Shift right and XOR 0xA001
  93. crc ^= 0xA001;
  94. }
  95. else // Else LSB is not set
  96. crc >>= 1; // Just shift right
  97. }
  98. }
  99. return crc;
  100. }
  101. void CRadarSensor::Start()
  102. {
  103. if(!_run)
  104. {
  105. _thread = std::thread(&CRadarSensor::Run, this);
  106. _run = true;
  107. }
  108. }
  109. void CRadarSensor::Run()
  110. {
  111. CIOBuffer buffer[5];
  112. uint8_t Source[5] = {0x55,0xAA,0x01,0x01,0x00};
  113. for(int i = 0 ; i < 4 ; i++)
  114. {
  115. Source[2] = (i + 0x01);
  116. CRC_DYP(Source);
  117. buffer[i].Length = 5;
  118. memcpy(buffer[i].Buffer, Source, buffer[i].Length);
  119. }
  120. while (_run)
  121. {
  122. for(int i = 0 ; i < 4 ; i++)
  123. _socket->Write(&buffer[i]);
  124. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  125. }
  126. }
  127. void CRadarSensor::SetSensorSocket(SensorSocket<CRadarSensor>* can)
  128. {
  129. _socket = can;
  130. }
  131. void CRadarSensor::Stop()
  132. {
  133. if (_run)
  134. {
  135. _run = false;
  136. _thread.join();
  137. }
  138. }