Line data Source code
1 : #pragma once 2 : 3 : #include "AP_RangeFinder_config.h" 4 : 5 : #if AP_RANGEFINDER_LIGHTWARE_GRF_I2C_ENABLED 6 : 7 : #include "AP_RangeFinder.h" 8 : #include "AP_RangeFinder_Backend_I2C.h" 9 : #include "AP_RangeFinder_LightWare_GRF_Common.h" 10 : 11 : #define AP_RANGEFINDER_LIGHTWARE_GRF_I2C_DEFAULT_ADDR 0x66 12 : 13 : class AP_RangeFinder_LightWare_GRF_I2C : public AP_RangeFinder_Backend_I2C, 14 : public AP_RangeFinder_LightWare_GRF_Common 15 : { 16 : public: 17 3 : static AP_RangeFinder_Backend *detect(RangeFinder::RangeFinder_State &_state, 18 : AP_RangeFinder_Params &_params, 19 : class AP_HAL::I2CDevice &dev) 20 : { 21 3 : return configure(NEW_NOTHROW AP_RangeFinder_LightWare_GRF_I2C(_state, _params, dev)); 22 : } 23 : 24 : static const struct AP_Param::GroupInfo var_info[]; 25 : 26 : void update() override; 27 : 28 : protected: 29 336 : MAV_DISTANCE_SENSOR _get_mav_distance_sensor_type() const override 30 : { 31 336 : return MAV_DISTANCE_SENSOR_LASER; 32 : } 33 : 34 : private: 35 : AP_RangeFinder_LightWare_GRF_I2C(RangeFinder::RangeFinder_State &_state, 36 : AP_RangeFinder_Params &_params, 37 : AP_HAL::I2CDevice &_dev); 38 : 39 : // Usual config steps 40 : enum class ConfigStep : uint8_t { 41 : WRITE_UPDATE_RATE, 42 : VERIFY_UPDATE_RATE, 43 : WRITE_DISTANCE_OUTPUT, 44 : VERIFY_DISTANCE_OUTPUT, 45 : DONE, 46 : }; 47 : 48 : bool init() override; 49 : void timer(); 50 : 51 : // Write/read a register on the sensor. 52 : bool write_command(MessageID msgid, const uint8_t *payload, uint16_t payload_len); 53 : bool read_payload(MessageID msgid, uint8_t *payload_buf, uint16_t payload_len); 54 : 55 : // Apply one step of the configuration. 56 : void run_config_step(); 57 : 58 : // Distance readings collected by timer() between update() calls; update() 59 : // averages them, publishes to state.distance_m, and resets the counters. 60 : struct { 61 : float sum_m; 62 : uint16_t count; 63 : } _accum; 64 : 65 : // config runs from the timer callback so init() stays quick. 66 : ConfigStep _config_step; 67 : uint8_t _config_retry; 68 : }; 69 : 70 : #endif // AP_RANGEFINDER_LIGHTWARE_GRF_I2C_ENABLED