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 : 16 : /* 17 : control of internal combustion engines (starter, ignition and choke) 18 : */ 19 : #pragma once 20 : 21 : #include "AP_ICEngine_config.h" 22 : 23 : #if AP_ICENGINE_ENABLED 24 : 25 : #include <AP_Param/AP_Param.h> 26 : #include <Filter/LowPassFilter.h> 27 : #include <AP_RPM/AP_RPM_config.h> 28 : #include <AP_HAL/I2CDevice.h> 29 : #include <AP_Relay/AP_Relay_config.h> 30 : #include <RC_Channel/RC_Channel.h> 31 : 32 : class AP_ICEngine { 33 : public: 34 : // constructor 35 : AP_ICEngine(); 36 : 37 : static const struct AP_Param::GroupInfo var_info[]; 38 : 39 : // One time init call 40 : void init(); 41 : 42 : // update engine state. Should be called at 10Hz or more 43 : void update(void); 44 : 45 : // check for throttle override 46 : bool throttle_override(float &percent, const float base_throttle); 47 : 48 : enum ICE_State { 49 : ICE_DISABLED = -1, 50 : ICE_OFF=0, 51 : ICE_START_HEIGHT_DELAY=1, 52 : ICE_START_DELAY=2, 53 : ICE_STARTING=3, 54 : ICE_RUNNING=4 55 : }; 56 : 57 : // get current engine control state 58 7095078 : ICE_State get_state(void) const { return !enable?ICE_DISABLED:state; } 59 : 60 : // handle DO_ENGINE_CONTROL messages via MAVLink or mission 61 : bool engine_control(float start_control, float cold_start, float height_delay, uint32_t flags); 62 : 63 : // do we have throttle while disarmed enabled? 64 : bool allow_throttle_while_disarmed(void) const; 65 : 66 : // Handle incoming aux function trigger 67 : void do_aux_function(const RC_Channel::AuxFuncTrigger &trigger); 68 : 69 : #if AP_RELAY_ENABLED 70 : // Needed for param conversion from relay numbers to functions 71 : bool get_legacy_ignition_relay_index(int8_t &num); 72 : #endif 73 : 74 7338760 : int8_t get_min_throttle_pct() const { return min_throttle_pct; } 75 : 76 2103 : static AP_ICEngine *get_singleton() { return _singleton; } 77 : 78 : private: 79 : static AP_ICEngine *_singleton; 80 : 81 : void set_ignition(bool on); 82 : void set_starter(bool on); 83 : 84 : enum ICE_State state; 85 : 86 : // Minimum throttle for idle (from idle_percent or idle governor) 87 : int8_t min_throttle_pct; 88 : 89 : #if AP_RPM_ENABLED 90 : // update min throttle for idle governor 91 : void update_idle_governor(int8_t &min_throttle); 92 : 93 : // filter for RPM value 94 : LowPassFilterConstDtFloat _rpm_filter; 95 : float filtered_rpm_value; 96 : #endif 97 : 98 : // enable library 99 : AP_Int8 enable; 100 : 101 : // min pwm on start channel for engine stop 102 : AP_Int16 start_chan_min_pwm; 103 : 104 : #if AP_RPM_ENABLED 105 : // which RPM instance to use 106 : AP_Int8 rpm_instance; 107 : #endif 108 : 109 : // time to run starter for (seconds) 110 : AP_Float starter_time; 111 : 112 : // delay between start attempts (seconds) 113 : AP_Float starter_delay; 114 : 115 : // max crank retry 116 : AP_Int8 max_crank_retry; 117 : int8_t crank_retry_ct; 118 : 119 : #if AP_RPM_ENABLED 120 : // RPM above which engine is considered to be running 121 : AP_Int32 rpm_threshold; 122 : #endif 123 : 124 : // time when we started the starter 125 : uint32_t starter_start_time_ms; 126 : 127 : // time when we last ran the starter 128 : uint32_t starter_last_run_ms; 129 : 130 : // time when we last had an uncommanded engine stop 131 : uint32_t last_uncommanded_stop_ms; 132 : 133 : // throttle percentage for engine start 134 : AP_Int8 start_percent; 135 : 136 : // throttle percentage for engine idle 137 : AP_Int8 idle_percent; 138 : 139 : #if AP_RPM_ENABLED 140 : // Idle Controller RPM setpoint 141 : AP_Int16 idle_rpm; 142 : 143 : // Idle Controller RPM deadband 144 : AP_Int16 idle_db; 145 : 146 : // Idle Controller Slew Rate 147 : AP_Float idle_slew; 148 : #endif 149 : 150 : // height when we enter ICE_START_HEIGHT_DELAY 151 : float initial_height; 152 : 153 : // height change required to start engine 154 : float height_required; 155 : 156 : // we are waiting for valid height data 157 : bool height_pending; 158 : 159 : bool allow_single_start_while_disarmed; 160 : 161 : // idle governor 162 : float idle_governor_integrator; 163 : 164 : enum class Options : uint16_t { 165 : DISABLE_IGNITION_RC_FAILSAFE = (1U << 0), 166 : DISABLE_REDLINE_GOVERNOR = (1U << 1), 167 : THROTTLE_WHILE_DISARMED = (1U << 2), 168 : NO_RUNNING_WHILE_DISARMED = (1U << 3), 169 : CRANK_DIR_REVERSE = (1U << 4), 170 : }; 171 : AP_Int16 options; 172 : 173 8623120 : bool option_set(Options option) const { 174 8623120 : return (options & uint16_t(option)) != 0; 175 : } 176 : 177 : // Last aux function value 178 : RC_Channel::AuxSwitchPos aux_pos = RC_Channel::AuxSwitchPos::MIDDLE; 179 : 180 : #if AP_RPM_ENABLED 181 : // redline rpm 182 : AP_Int32 redline_rpm; 183 : struct { 184 : bool flag; 185 : float governor_integrator; 186 : float throttle_percentage; 187 : } redline; 188 : #endif 189 : 190 : // Param conversion function and flag 191 : void param_conversion(); 192 : AP_Int8 param_format_version; 193 : }; 194 : 195 : 196 : namespace AP { 197 : AP_ICEngine *ice(); 198 : }; 199 : 200 : #endif // AP_ICENGINE_ENABLED