AP_RangeFinder_LeddarOne.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #include "RangeFinder.h"
  3. #include "RangeFinder_Backend.h"
  4. // defines
  5. #define LEDDARONE_DEFAULT_ADDRESS 0x01
  6. #define LEDDARONE_MODOBUS_FUNCTION_CODE 0x04
  7. #define LEDDARONE_MODOBUS_FUNCTION_REGISTER_ADDRESS 20
  8. #define LEDDARONE_MODOBUS_FUNCTION_READ_NUMBER 10
  9. #define LEDDARONE_SERIAL_PORT_MAX 250
  10. #define LEDDARONE_READ_BUFFER_SIZE 25
  11. #define LEDDARONE_DETECTIONS_MAX 3
  12. #define LEDDARONE_DETECTION_DATA_NUMBER_INDEX 10
  13. #define LEDDARONE_DETECTION_DATA_INDEX_OFFSET 11
  14. #define LEDDARONE_DETECTION_DATA_OFFSET 4
  15. // LeddarOne status
  16. enum LeddarOne_Status {
  17. LEDDARONE_STATE_OK = 0,
  18. LEDDARONE_STATE_READING_BUFFER = 1,
  19. LEDDARONE_STATE_ERR_BAD_CRC = -1,
  20. LEDDARONE_STATE_ERR_NO_RESPONSES = -2,
  21. LEDDARONE_STATE_ERR_BAD_RESPONSE = -3,
  22. LEDDARONE_STATE_ERR_SHORT_RESPONSE = -4,
  23. LEDDARONE_STATE_ERR_SERIAL_PORT = -5,
  24. LEDDARONE_STATE_ERR_NUMBER_DETECTIONS = -6
  25. };
  26. // LeddarOne Modbus status
  27. enum LeddarOne_ModbusStatus {
  28. LEDDARONE_MODBUS_STATE_INIT = 0,
  29. LEDDARONE_MODBUS_STATE_PRE_SEND_REQUEST,
  30. LEDDARONE_MODBUS_STATE_SENT_REQUEST,
  31. LEDDARONE_MODBUS_STATE_AVAILABLE
  32. };
  33. class AP_RangeFinder_LeddarOne : public AP_RangeFinder_Backend
  34. {
  35. public:
  36. // constructor
  37. AP_RangeFinder_LeddarOne(RangeFinder::RangeFinder_State &_state,
  38. AP_RangeFinder_Params &_params,
  39. uint8_t serial_instance);
  40. // static detection function
  41. static bool detect(uint8_t serial_instance);
  42. // update state
  43. void update(void) override;
  44. protected:
  45. virtual MAV_DISTANCE_SENSOR _get_mav_distance_sensor_type() const override {
  46. return MAV_DISTANCE_SENSOR_LASER;
  47. }
  48. private:
  49. // get a reading
  50. bool get_reading(uint16_t &reading_cm);
  51. // CRC16
  52. bool CRC16(uint8_t *aBuffer, uint8_t aLength, bool aCheck);
  53. // parse a response message from ModBus
  54. LeddarOne_Status parse_response(uint8_t &number_detections);
  55. AP_HAL::UARTDriver *uart = nullptr;
  56. uint32_t last_sending_request_ms;
  57. uint32_t last_available_ms;
  58. uint16_t detections[LEDDARONE_DETECTIONS_MAX];
  59. uint32_t sum_distance;
  60. LeddarOne_ModbusStatus modbus_status = LEDDARONE_MODBUS_STATE_INIT;
  61. uint8_t read_buffer[LEDDARONE_READ_BUFFER_SIZE];
  62. uint32_t read_len;
  63. // Modbus send request buffer
  64. // read input register (function code 0x04)
  65. const uint8_t send_request_buffer[8] = {
  66. LEDDARONE_DEFAULT_ADDRESS,
  67. LEDDARONE_MODOBUS_FUNCTION_CODE,
  68. 0,
  69. LEDDARONE_MODOBUS_FUNCTION_REGISTER_ADDRESS, // 20: Address of first register to read
  70. 0,
  71. LEDDARONE_MODOBUS_FUNCTION_READ_NUMBER, // 10: The number of consecutive registers to read
  72. 0x30, // CRC Lo
  73. 0x09 // CRC Hi
  74. };
  75. };