AP_RangeFinder_Lanbao.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /*
  14. driver for Lanbao PSK-CM8JL65-CC5 Lidar
  15. */
  16. #include <AP_HAL/AP_HAL.h>
  17. #include "AP_RangeFinder_Lanbao.h"
  18. #include <AP_SerialManager/AP_SerialManager.h>
  19. #include <AP_Math/crc.h>
  20. #include <stdio.h>
  21. extern const AP_HAL::HAL& hal;
  22. /*
  23. this sensor has no way of reporting "out of range", it will keep
  24. reporting distances at of around 7 to 8 meters even when pointed at
  25. the sky. For this reason we limit the max range to 6 meters as
  26. otherwise we may be giving false data
  27. */
  28. #define LANBAO_MAX_RANGE_CM 600
  29. /*
  30. The constructor also initialises the rangefinder. Note that this
  31. constructor is not called until detect() returns true, so we
  32. already know that we should setup the rangefinder
  33. */
  34. AP_RangeFinder_Lanbao::AP_RangeFinder_Lanbao(RangeFinder::RangeFinder_State &_state,
  35. AP_RangeFinder_Params &_params,
  36. uint8_t serial_instance) :
  37. AP_RangeFinder_Backend(_state, _params)
  38. {
  39. const AP_SerialManager &serial_manager = AP::serialmanager();
  40. uart = serial_manager.find_serial(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance);
  41. if (uart != nullptr) {
  42. // always 115200
  43. uart->begin(115200);
  44. }
  45. }
  46. /*
  47. detect if a rangefinder is connected. We'll detect by trying to
  48. take a reading on Serial. If we get a result the sensor is there.
  49. */
  50. bool AP_RangeFinder_Lanbao::detect(uint8_t serial_instance)
  51. {
  52. return AP::serialmanager().find_serial(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance) != nullptr;
  53. }
  54. // read - return last value measured by sensor
  55. bool AP_RangeFinder_Lanbao::get_reading(uint16_t &reading_cm)
  56. {
  57. if (uart == nullptr) {
  58. return false;
  59. }
  60. float sum_range = 0;
  61. uint32_t count = 0;
  62. // format is: [ 0xA5 | 0x5A | distance-MSB-mm | distance-LSB-mm | crc16 ]
  63. // read any available lines from the lidar
  64. int16_t nbytes = uart->available();
  65. while (nbytes-- > 0) {
  66. int16_t b = uart->read();
  67. if (b == -1) {
  68. break;
  69. }
  70. if (buf_len == 0 && b != 0xA5) {
  71. // discard
  72. continue;
  73. }
  74. if (buf_len == 1 && b != 0x5A) {
  75. // discard
  76. if (b == 0xA5) {
  77. buf[0] = b;
  78. } else {
  79. buf_len = 0;
  80. }
  81. continue;
  82. }
  83. buf[buf_len++] = b;
  84. if (buf_len == sizeof(buf)) {
  85. buf_len = 0;
  86. uint16_t crc = (buf[5]<<8) | buf[4];
  87. if (crc != calc_crc_modbus(buf, 4)) {
  88. // bad CRC, discard
  89. continue;
  90. }
  91. sum_range += float((buf[2]<<8) | buf[3]) * 0.001;
  92. count++;
  93. }
  94. }
  95. if (count > 0) {
  96. reading_cm = (sum_range / count) * 100;
  97. return reading_cm <= LANBAO_MAX_RANGE_CM?true:false;
  98. }
  99. return false;
  100. }
  101. /*
  102. update the state of the sensor
  103. */
  104. void AP_RangeFinder_Lanbao::update(void)
  105. {
  106. if (get_reading(state.distance_cm)) {
  107. // update range_valid state based on distance measured
  108. state.last_reading_ms = AP_HAL::millis();
  109. update_status();
  110. } else if (AP_HAL::millis() - state.last_reading_ms > 200) {
  111. set_status(RangeFinder::RangeFinder_NoData);
  112. }
  113. }