ardupilot/libraries/AP_BattMonitor/AP_BattMonitor_BLHeliESC.cpp
Andrew Tridgell dc9ae42067 AP_BattMonitor: added ESC telemetry virtual battery
this aggregates all BLHeli ESCs into a single virtual battery
2018-06-19 09:36:03 +10:00

79 lines
2.2 KiB
C++

/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <AP_HAL/AP_HAL.h>
#include <AP_BLHeli/AP_BLHeli.h>
#ifdef HAVE_AP_BLHELI_SUPPORT
#include "AP_BattMonitor_BLHeliESC.h"
extern const AP_HAL::HAL &hal;
void AP_BattMonitor_BLHeliESC::init(void)
{
}
void AP_BattMonitor_BLHeliESC::read(void)
{
AP_BLHeli *blheli = AP_BLHeli::get_instance();
if (!blheli) {
return;
}
uint8_t num_escs = 0;
float voltage_sum=0;
float current_sum=0;
float consumed_sum=0;
float temperature_sum=0;
uint32_t now = AP_HAL::millis();
uint32_t highest_ms=0;
for (uint8_t i=0; i<8; i++) {
AP_BLHeli::telem_data td;
uint32_t timestamp_ms;
blheli->get_telem_data(i, td, timestamp_ms);
if (now - timestamp_ms > 1000) {
// don't use old data
continue;
}
num_escs++;
voltage_sum += td.voltage;
current_sum += td.current;
consumed_sum += td.consumption;
temperature_sum += td.temperature;
if (timestamp_ms > highest_ms) {
highest_ms = timestamp_ms;
}
}
if (num_escs > 0) {
_state.voltage = (voltage_sum / num_escs) * 0.01;
_state.temperature = temperature_sum / num_escs;
_state.healthy = true;
} else {
_state.voltage = 0;
_state.temperature = 0;
_state.healthy = false;
}
_state.current_amps = current_sum * 0.01;
_state.consumed_mah = consumed_sum;
_state.last_time_micros = highest_ms * 1000;
_state.temperature_time = highest_ms;
}
#endif // HAVE_AP_BLHELI_SUPPORT