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 : battery model for electric aircraft 17 : */ 18 : 19 : #pragma once 20 : 21 : #include <Filter/LowPassFilter.h> 22 : 23 : namespace SITL { 24 : 25 : class Battery { 26 : public: 27 : // Note: capacity<=0 means **unlimited** capacity. 28 : void setup(float _capacity_Ah, float _resistance_ohm, float _max_voltage, float _ambient_temperature_degC); 29 : 30 : // Resets the battery state if the configuration (e.g. from SIM_BATT_* parameters) has changed. 31 : // Changing desired_resistance_ohm does not cause a state reset. 32 : // A negative desired_resistance value means to leave the existing resistance unchanged. 33 : void maybe_reset(float desired_voltage, float desired_capacity_Ah, float desired_resistance_ohm = -1.0f); 34 : 35 : // Call this periodically to "step" the battery forward in time 36 : void consume_energy(float attempted_current_amp, uint64_t now_us); 37 : 38 177027951 : float get_voltage(void) const { return voltage_filter.get(); } 39 37 : float get_capacity(void) const { return capacity_Ah; } 40 167668475 : float get_temperature_degC(void) const { return temperature_degC; } 41 354242567 : bool capacity_is_unlimited(void) const { return !(is_positive(capacity_Ah)); } // for readability 42 : 43 : private: 44 : float capacity_Ah; // set <= 0 for unlmited capacity 45 : float resistance_ohm; 46 : float max_voltage; 47 : float ambient_temperature_degC; 48 : float voltage_set; 49 : float remaining_Ah; // if capacity is unlimited, this is FLT_MAX 50 : uint64_t last_us; 51 : 52 : float temperature_degC = 0.0f; 53 : void update_temperature(float current_amp, float dt); 54 : 55 : // 10Hz filter for battery voltage 56 : LowPassFilterFloat voltage_filter{10}; 57 : 58 : float get_resting_voltage(void) const; 59 : float compute_remaining_Ah(float voltage) const; 60 : }; 61 : }