Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_BattMonitor/AP_BattMonitor_ESC.cpp
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/141516#include "AP_BattMonitor_config.h"1718#if AP_BATTERY_ESC_ENABLED1920#include "AP_BattMonitor_ESC.h"2122const AP_Param::GroupInfo AP_BattMonitor_ESC::var_info[] = {2324// @Param: ESC_MASK25// @DisplayName: ESC mask26// @Description: If 0 all connected ESCs will be used. If non-zero, only those selected in will be used.27// @Bitmask: 0: ESC 1, 1: ESC 2, 2: ESC 3, 3: ESC 4, 4: ESC 5, 5: ESC 6, 6: ESC 7, 7: ESC 8, 8: ESC 9, 9: ESC 10, 10: ESC 11, 11: ESC 12, 12: ESC 13, 13: ESC 14, 14: ESC 15, 15: ESC 16, 16: ESC 17, 17: ESC 18, 18: ESC 19, 19: ESC 20, 20: ESC 21, 21: ESC 22, 22: ESC 23, 23: ESC 24, 24: ESC 25, 25: ESC 26, 26: ESC 27, 27: ESC 28, 28: ESC 29, 29: ESC 30, 30: ESC 31, 31: ESC 3228// @User: Standard29AP_GROUPINFO("ESC_MASK", 36, AP_BattMonitor_ESC, _mask, 0),3031// CHECK/UPDATE INDEX TABLE IN AP_BattMonitor_Backend.cpp WHEN CHANGING OR ADDING PARAMETERS3233AP_GROUPEND34};3536// constructor. This incorporates initialisation as well.37AP_BattMonitor_ESC::AP_BattMonitor_ESC(AP_BattMonitor &mon,38AP_BattMonitor::BattMonitor_State &mon_state,39AP_BattMonitor_Params ¶ms):40AP_BattMonitor_Backend(mon, mon_state, params)41{42AP_Param::setup_object_defaults(this, var_info);43_state.var_info = var_info;44};4546void AP_BattMonitor_ESC::init(void)47{48}4950void AP_BattMonitor_ESC::read(void)51{52AP_ESC_Telem& telem = AP::esc_telem();5354uint8_t voltage_escs = 0; // number of ESCs with valid voltage55uint8_t temperature_escs = 0; // number of ESCs with valid temperature56float voltage_sum = 0;57float current_sum = 0;58float temperature_sum = 0;59float consumed_mah_sum = 0.0;60uint32_t highest_ms = 0;6162const bool all_enabled = _mask == 0;63for (uint8_t i=0; i<ESC_TELEM_MAX_ESCS; i++) {64if (!all_enabled && ((_mask & (1U<<i)) == 0)) {65// Only include ESCs set in mask66continue;67}6869int16_t temperature_cdeg;70float voltage;71float current;72float consumption_mah;7374if (telem.get_consumption_mah(i, consumption_mah)) {75// accumulate consumed_sum regardless of age, to cope with ESC76// dropping out77consumed_mah_sum += consumption_mah;78have_consumed_mah = true;79}8081if (telem.get_voltage(i, voltage)) {82voltage_sum += voltage;83voltage_escs++;84}8586if (telem.get_current(i, current)) {87current_sum += current;88have_current = true;89}9091if (telem.get_temperature(i, temperature_cdeg)) {92temperature_sum += float(temperature_cdeg) * 0.01f;93temperature_escs++;94}9596if (telem.get_last_telem_data_ms(i) > highest_ms) {97highest_ms = telem.get_last_telem_data_ms(i);98}99}100101if (voltage_escs > 0) {102_state.voltage = voltage_sum / voltage_escs;103_state.healthy = true;104} else {105_state.voltage = 0;106_state.healthy = false;107}108if (temperature_escs > 0) {109_state.temperature = temperature_sum / temperature_escs;110have_temperature = true;111} else {112_state.temperature = 0;113}114115_state.current_amps = current_sum;116_state.last_time_micros = highest_ms * 1000;117_state.temperature_time = highest_ms;118119const uint32_t now_us = AP_HAL::micros();120const uint32_t dt_us = now_us - last_read_us;121last_read_us = now_us;122123if (have_consumed_mah) {124// Report the cumulative consumed mah as reported by the ESCs125// delta_mah allows reset_remaining to function without being able to reset the values sent by the ESCs126_state.consumed_mah = delta_mah + consumed_mah_sum;127128} else if (have_current) {129// ESCs provide current but not consumed mah, integrate manually130update_consumed(_state, dt_us);131132}133}134135bool AP_BattMonitor_ESC::reset_remaining(float percentage)136{137delta_mah = 0.0f;138read();139const float current_mah = _state.consumed_mah;140if (AP_BattMonitor_Backend::reset_remaining(percentage)) {141delta_mah = _state.consumed_mah - current_mah;142return true;143}144145return false;146}147148#endif // AP_BATTERY_ESC_ENABLED149150151