Line data Source code
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 : 7 : This program is distributed in the hope that it will be useful, 8 : but WITHOUT ANY WARRANTY; without even the implied warranty of 9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 : GNU General Public License for more details. 11 : 12 : You should have received a copy of the GNU General Public License 13 : along with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : #pragma once 16 : 17 : #include "AP_RangeFinder_config.h" 18 : 19 : #if AP_RANGEFINDER_ENABLED 20 : 21 : #include <AP_Common/AP_Common.h> 22 : #include <AP_HAL/AP_HAL_Boards.h> 23 : #include <AP_HAL/Semaphores.h> 24 : #include "AP_RangeFinder.h" 25 : 26 : class AP_RangeFinder_Backend 27 : { 28 : public: 29 : // constructor. This incorporates initialisation as well. 30 : AP_RangeFinder_Backend(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params); 31 : 32 : // we declare a virtual destructor so that RangeFinder drivers can 33 : // override with a custom destructor if need be 34 6 : virtual ~AP_RangeFinder_Backend(void) {} 35 : 36 : // update the state structure 37 : virtual void update() = 0; 38 177 : virtual void init_serial(uint8_t serial_instance) {}; 39 : 40 64 : virtual void handle_msg(const mavlink_message_t &msg) { return; } 41 : 42 : #if AP_SCRIPTING_ENABLED 43 : void get_state(RangeFinder::RangeFinder_State &state_arg); 44 : 45 : // Returns false if scripting backing hasn't been setup. 46 0 : virtual bool handle_script_msg(float dist_m) { return false; } // legacy interface 47 0 : virtual bool handle_script_msg(const RangeFinder::RangeFinder_State &state_arg) { return false; } 48 : #endif 49 : 50 : #if HAL_MSP_RANGEFINDER_ENABLED 51 0 : virtual void handle_msp(const MSP::msp_rangefinder_data_message_t &pkt) { return; } 52 : #endif 53 : 54 22255523 : enum Rotation orientation() const { return (Rotation)params.orientation.get(); } 55 5942694 : float distance() const { return state.distance_m; } 56 558115 : int8_t signal_quality_pct() const WARN_IF_UNUSED { return state.signal_quality_pct; } 57 4400 : uint16_t voltage_mv() const { return state.voltage_mv; } 58 5184060 : virtual float max_distance() const { return params.max_distance; } 59 392077 : virtual float min_distance() const { return params.min_distance; } 60 4512273 : float ground_clearance() const { return params.ground_clearance; } 61 : MAV_DISTANCE_SENSOR get_mav_distance_sensor_type() const; 62 : RangeFinder::Status status() const; 63 39300952 : RangeFinder::Type type() const { return (RangeFinder::Type)params.type.get(); } 64 : 65 : // true if sensor is returning data 66 : bool has_data() const; 67 : 68 : // returns count of consecutive good readings 69 : // note that this method returning zero does not mean that the device is unhealthy: 70 118867 : uint8_t range_valid_count() const { return state.range_valid_count; } 71 : 72 : // return a 3D vector defining the position offset of the sensor 73 : // in metres relative to the body frame origin 74 5175467 : const Vector3f &get_pos_offset() const { return params.pos_offset; } 75 : 76 : // return system time of last successful read from the sensor 77 13305 : uint32_t last_reading_ms() const { return state.last_reading_ms; } 78 : 79 : // get temperature reading in C. returns true on success and populates temp argument. 80 : // non-virtual: checks for an externally-supplied temperature first (e.g. from 81 : // AP_TemperatureSensor, TEMPx_SRC=Rangefinder), then falls back to the backend's 82 : // own reading via _get_temp(). 83 : bool get_temp(float &temp) const; 84 : 85 : // return the actual type of the rangefinder, as opposed to the 86 : // parameter value which may be changed at runtime. 87 5146 : RangeFinder::Type allocated_type() const { return _backend_type; } 88 : 89 : protected: 90 : 91 : // update status based on distance measurement 92 : void update_status(RangeFinder::RangeFinder_State &state_arg) const; 93 282081 : void update_status() { update_status(state); } 94 : 95 : // set status and update valid_count 96 : static void set_status(RangeFinder::RangeFinder_State &state_arg, RangeFinder::Status status); 97 9472 : void set_status(RangeFinder::Status status) { set_status(state, status); } 98 : 99 : RangeFinder::RangeFinder_State &state; 100 : AP_RangeFinder_Params ¶ms; 101 : 102 : // semaphore for access to shared frontend data 103 : HAL_Semaphore _sem; 104 : 105 : //Type Backend initialised with 106 : RangeFinder::Type _backend_type; 107 : 108 : virtual MAV_DISTANCE_SENSOR _get_mav_distance_sensor_type() const = 0; 109 : 110 : // backend-specific temperature reading. Override in backends that have their 111 : // own temperature source (e.g. NMEA depth sounders reporting MTW). 112 334110 : virtual bool _get_temp(float &temp) const { return false; } 113 : 114 : // return true if rangefinder should be powered down 115 : bool should_power_down() const; 116 : 117 : }; 118 : 119 : #endif // AP_RANGEFINDER_ENABLED