AP_RangeFinder_Benewake.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include <AP_HAL/AP_HAL.h>
  14. #include "AP_RangeFinder_Benewake.h"
  15. #include <AP_SerialManager/AP_SerialManager.h>
  16. #include <ctype.h>
  17. #include <AP_HAL/utility/sparse-endian.h>
  18. extern const AP_HAL::HAL& hal;
  19. #define BENEWAKE_FRAME_HEADER 0x59
  20. #define BENEWAKE_FRAME_LENGTH 9
  21. #define BENEWAKE_DIST_MAX_CM 32768
  22. #define BENEWAKE_TFMINI_OUT_OF_RANGE_CM 1200
  23. #define BENEWAKE_TF02_OUT_OF_RANGE_CM 2200
  24. #define BENEWAKE_OUT_OF_RANGE_ADD_CM 100
  25. // format of serial packets received from benewake lidar
  26. //
  27. // Data Bit Definition Description
  28. // ------------------------------------------------
  29. // byte 0 Frame header 0x59
  30. // byte 1 Frame header 0x59
  31. // byte 2 DIST_L Distance (in cm) low 8 bits
  32. // byte 3 DIST_H Distance (in cm) high 8 bits
  33. // byte 4 STRENGTH_L Strength low 8 bits
  34. // byte 5 STRENGTH_H Strength high 8 bits
  35. // byte 6 (TF02) SIG Reliability in 8 levels, 7 & 8 means reliable
  36. // byte 6 (TFmini) Distance Mode 0x02 for short distance (mm), 0x07 for long distance (cm)
  37. // byte 7 (TF02 only) TIME Exposure time in two levels 0x03 and 0x06
  38. // byte 8 Checksum Checksum byte, sum of bytes 0 to bytes 7
  39. /*
  40. The constructor also initialises the rangefinder. Note that this
  41. constructor is not called until detect() returns true, so we
  42. already know that we should setup the rangefinder
  43. */
  44. AP_RangeFinder_Benewake::AP_RangeFinder_Benewake(RangeFinder::RangeFinder_State &_state,
  45. AP_RangeFinder_Params &_params,
  46. uint8_t serial_instance,
  47. benewake_model_type model) :
  48. AP_RangeFinder_Backend(_state, _params),
  49. model_type(model)
  50. {
  51. const AP_SerialManager &serial_manager = AP::serialmanager();
  52. uart = serial_manager.find_serial(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance);
  53. if (uart != nullptr) {
  54. uart->begin(serial_manager.find_baudrate(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance));
  55. }
  56. }
  57. /*
  58. detect if a Benewake rangefinder is connected. We'll detect by
  59. trying to take a reading on Serial. If we get a result the sensor is
  60. there.
  61. */
  62. bool AP_RangeFinder_Benewake::detect(uint8_t serial_instance)
  63. {
  64. return AP::serialmanager().find_serial(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance) != nullptr;
  65. }
  66. // distance returned in reading_cm, signal_ok is set to true if sensor reports a strong signal
  67. bool AP_RangeFinder_Benewake::get_reading(uint16_t &reading_cm)
  68. {
  69. if (uart == nullptr) {
  70. return false;
  71. }
  72. float sum_cm = 0;
  73. uint16_t count = 0;
  74. uint16_t count_out_of_range = 0;
  75. // read any available lines from the lidar
  76. int16_t nbytes = uart->available();
  77. while (nbytes-- > 0) {
  78. int16_t r = uart->read();
  79. if (r < 0) {
  80. continue;
  81. }
  82. uint8_t c = (uint8_t)r;
  83. // if buffer is empty and this byte is 0x59, add to buffer
  84. if (linebuf_len == 0) {
  85. if (c == BENEWAKE_FRAME_HEADER) {
  86. linebuf[linebuf_len++] = c;
  87. }
  88. } else if (linebuf_len == 1) {
  89. // if buffer has 1 element and this byte is 0x59, add it to buffer
  90. // if not clear the buffer
  91. if (c == BENEWAKE_FRAME_HEADER) {
  92. linebuf[linebuf_len++] = c;
  93. } else {
  94. linebuf_len = 0;
  95. }
  96. } else {
  97. // add character to buffer
  98. linebuf[linebuf_len++] = c;
  99. // if buffer now has 9 items try to decode it
  100. if (linebuf_len == BENEWAKE_FRAME_LENGTH) {
  101. // calculate checksum
  102. uint8_t checksum = 0;
  103. for (uint8_t i=0; i<BENEWAKE_FRAME_LENGTH-1; i++) {
  104. checksum += linebuf[i];
  105. }
  106. // if checksum matches extract contents
  107. if (checksum == linebuf[BENEWAKE_FRAME_LENGTH-1]) {
  108. // calculate distance
  109. uint16_t dist = ((uint16_t)linebuf[3] << 8) | linebuf[2];
  110. if (dist >= BENEWAKE_DIST_MAX_CM) {
  111. // this reading is out of range
  112. count_out_of_range++;
  113. } else if (model_type == BENEWAKE_TFmini) {
  114. // no signal byte from TFmini so add distance to sum
  115. sum_cm += dist;
  116. count++;
  117. } else {
  118. // TF02 provides signal reliability (good = 7 or 8)
  119. if (linebuf[6] >= 7) {
  120. // add distance to sum
  121. sum_cm += dist;
  122. count++;
  123. } else {
  124. // this reading is out of range
  125. count_out_of_range++;
  126. }
  127. }
  128. }
  129. // clear buffer
  130. linebuf_len = 0;
  131. }
  132. }
  133. }
  134. if (count > 0) {
  135. // return average distance of readings
  136. reading_cm = sum_cm / count;
  137. return true;
  138. }
  139. if (count_out_of_range > 0) {
  140. // if only out of range readings return larger of
  141. // driver defined maximum range for the model and user defined max range + 1m
  142. float model_dist_max_cm = (model_type == BENEWAKE_TFmini) ? BENEWAKE_TFMINI_OUT_OF_RANGE_CM : BENEWAKE_TF02_OUT_OF_RANGE_CM;
  143. reading_cm = MAX(model_dist_max_cm, max_distance_cm() + BENEWAKE_OUT_OF_RANGE_ADD_CM);
  144. return true;
  145. }
  146. // no readings so return false
  147. return false;
  148. }
  149. /*
  150. update the state of the sensor
  151. */
  152. void AP_RangeFinder_Benewake::update(void)
  153. {
  154. if (get_reading(state.distance_cm)) {
  155. // update range_valid state based on distance measured
  156. state.last_reading_ms = AP_HAL::millis();
  157. update_status();
  158. } else if (AP_HAL::millis() - state.last_reading_ms > 200) {
  159. set_status(RangeFinder::RangeFinder_NoData);
  160. }
  161. }