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_RPM_config.h" 18 : 19 : #if AP_RPM_ENABLED 20 : 21 : #include <AP_Common/AP_Common.h> 22 : #include <AP_HAL/AP_HAL_Boards.h> 23 : #include <AP_Param/AP_Param.h> 24 : #include <AP_Math/AP_Math.h> 25 : #include "AP_RPM_Params.h" 26 : 27 : class AP_RPM_Backend; 28 : 29 : class AP_RPM 30 : { 31 : friend class AP_RPM_Backend; 32 : 33 : public: 34 : AP_RPM(); 35 : 36 : CLASS_NO_COPY(AP_RPM); /* Do not allow copies */ 37 : 38 : // RPM driver types 39 : enum RPM_Type { 40 : RPM_TYPE_NONE = 0, 41 : #if AP_RPM_PIN_ENABLED 42 : RPM_TYPE_PWM = 1, 43 : RPM_TYPE_PIN = 2, 44 : #endif 45 : #if AP_RPM_EFI_ENABLED 46 : RPM_TYPE_EFI = 3, 47 : #endif 48 : #if AP_RPM_HARMONICNOTCH_ENABLED 49 : RPM_TYPE_HNTCH = 4, 50 : #endif 51 : #if AP_RPM_ESC_TELEM_ENABLED 52 : RPM_TYPE_ESC_TELEM = 5, 53 : #endif 54 : #if AP_RPM_GENERATOR_ENABLED 55 : RPM_TYPE_GENERATOR = 6, 56 : #endif 57 : #if AP_RPM_DRONECAN_ENABLED 58 : RPM_TYPE_DRONECAN = 7, 59 : #endif 60 : #if AP_RPM_SIM_ENABLED 61 : RPM_TYPE_SITL = 10, 62 : #endif 63 : }; 64 : 65 : // The RPM_State structure is filled in by the backend driver 66 : struct RPM_State { 67 : uint8_t instance; // the instance number of this RPM 68 : float rate_rpm; // measured rate in revs per minute 69 : uint32_t last_reading_ms; // time of last reading 70 : float signal_quality; // synthetic quality metric 71 : }; 72 : 73 : // parameters for each instance 74 : AP_RPM_Params _params[RPM_MAX_INSTANCES]; 75 : 76 : static const struct AP_Param::GroupInfo var_info[]; 77 : 78 : // Return the number of rpm sensor instances 79 117 : uint8_t num_sensors(void) const { 80 117 : return num_instances; 81 : } 82 : 83 : // detect and initialise any available rpm sensors 84 : void init(void); 85 : 86 : // update state of all rpm sensors. Should be called from main loop 87 : void update(void); 88 : 89 : /* 90 : return RPM for a sensor. Return -1 if not healthy 91 : */ 92 : bool get_rpm(uint8_t instance, float &rpm_value) const; 93 : 94 : /* 95 : return signal quality for a sensor. 96 : */ 97 484590 : float get_signal_quality(uint8_t instance) const { 98 484590 : return state[instance].signal_quality; 99 : } 100 : 101 : bool healthy(uint8_t instance) const; 102 : 103 : bool enabled(uint8_t instance) const; 104 : 105 4934925 : static AP_RPM *get_singleton() { return _singleton; } 106 : 107 : // check settings are valid 108 : bool arming_checks(size_t buflen, char *buffer) const; 109 : 110 : #if AP_RPM_STREAM_ENABLED 111 : // Return the sensor id to use for streaming over DroneCAN, negative number disables 112 : int8_t get_dronecan_sensor_id(uint8_t instance) const; 113 : #endif 114 : 115 : private: 116 : static AP_RPM *_singleton; 117 : 118 : RPM_State state[RPM_MAX_INSTANCES]; 119 : AP_RPM_Backend *drivers[RPM_MAX_INSTANCES]; 120 : uint8_t num_instances; 121 : 122 : void Log_RPM() const; 123 : }; 124 : 125 : namespace AP { 126 : AP_RPM *rpm(); 127 : }; 128 : 129 : #endif // AP_RPM_ENABLED