AP_RangeFinder_BLPing.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #pragma once
  2. #include "RangeFinder.h"
  3. #include "RangeFinder_Backend.h"
  4. class AP_RangeFinder_BLPing : public AP_RangeFinder_Backend
  5. {
  6. public:
  7. // constructor
  8. AP_RangeFinder_BLPing(RangeFinder::RangeFinder_State &_state,
  9. AP_RangeFinder_Params &_params,
  10. uint8_t serial_instance);
  11. // static detection function
  12. static bool detect(uint8_t serial_instance);
  13. // update state
  14. void update(void) override;
  15. protected:
  16. virtual MAV_DISTANCE_SENSOR _get_mav_distance_sensor_type() const override {
  17. return MAV_DISTANCE_SENSOR_ULTRASOUND;
  18. }
  19. private:
  20. void init_sensor();
  21. // send message to sensor
  22. void send_message(uint16_t msgid, const uint8_t *payload, uint16_t payload_len);
  23. // read a distance from the sensor
  24. bool get_reading(uint16_t &reading_cm);
  25. // process one byte received on serial port
  26. // returns true if a distance message has been successfully parsed
  27. // state is stored in msg structure
  28. bool parse_byte(uint8_t b);
  29. enum class ParseState {
  30. HEADER1 = 0,
  31. HEADER2,
  32. LEN_L,
  33. LEN_H,
  34. MSG_ID_L,
  35. MSG_ID_H,
  36. SRC_ID,
  37. DST_ID,
  38. PAYLOAD,
  39. CRC_L,
  40. CRC_H
  41. };
  42. AP_HAL::UARTDriver *uart;
  43. uint32_t last_init_ms; // system time that sensor was last initialised
  44. uint16_t distance_cm; // latest distance
  45. // structure holding latest message contents
  46. struct {
  47. ParseState state; // state of incoming message processing
  48. uint8_t payload[20]; // payload
  49. uint16_t payload_len; // latest message payload length
  50. uint16_t msgid; // latest message's message id
  51. uint16_t payload_recv; // number of message's payload bytes received so far
  52. uint16_t crc; // latest message's crc
  53. uint16_t crc_expected; // latest message's expected crc
  54. } msg;
  55. };