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_Scripting.cpp
Views: 1798
#include "AP_BattMonitor_config.h"12#if AP_BATTERY_SCRIPTING_ENABLED34#include "AP_BattMonitor_Scripting.h"56#define AP_BATT_MONITOR_SCRIPTING_TIMEOUT_US 500000078bool AP_BattMonitor_Scripting::capacity_remaining_pct(uint8_t &percentage) const9{10if (internal_state.capacity_remaining_pct != UINT8_MAX) {11percentage = internal_state.capacity_remaining_pct;12return true;13}14// Fall back to default implementation15return AP_BattMonitor_Backend::capacity_remaining_pct(percentage);16}1718bool AP_BattMonitor_Scripting::get_cycle_count(uint16_t &cycles) const19{20if (internal_state.cycle_count == UINT16_MAX) {21return false;22}23cycles = internal_state.cycle_count;24return true;25}2627// Called by frontend to update the state. Called at 10Hz28void AP_BattMonitor_Scripting::read()29{30WITH_SEMAPHORE(sem);3132// Check for timeout, to prevent a faulty script from appearing healthy33if (last_update_us == 0 || AP_HAL::micros() - last_update_us > AP_BATT_MONITOR_SCRIPTING_TIMEOUT_US) {34_state.healthy = false;35return;36}3738if (_state.last_time_micros == last_update_us) {39// No new data40return;41}4243/*44the script can fill in voltages up to 32 cells, for mavlink reporting45the extra cell voltages get distributed over the max of 14 for mavlink46*/47for (uint8_t i = 0; i < MIN(AP_BATT_MONITOR_CELLS_MAX,internal_state.cell_count); i++) {48_state.cell_voltages.cells[i] = internal_state.cell_voltages[i];49}50_state.voltage = internal_state.voltage;51if (!isnan(internal_state.current_amps)) {52_state.current_amps = internal_state.current_amps;53}54if (!isnan(internal_state.consumed_mah)) {55_state.consumed_mah = internal_state.consumed_mah;56}57// Overide integrated consumed energy with script value if it has been set58if (!isnan(internal_state.consumed_wh)) {59_state.consumed_wh = internal_state.consumed_wh;60}61if (!isnan(internal_state.temperature)) {62_state.temperature = internal_state.temperature;63}6465_state.healthy = internal_state.healthy;6667// Update the timestamp (has to be done after the consumed_mah calculation)68_state.last_time_micros = last_update_us;69}7071bool AP_BattMonitor_Scripting::handle_scripting(const BattMonitorScript_State &battmon_state)72{73WITH_SEMAPHORE(sem);74internal_state = battmon_state;75const uint32_t now_us = AP_HAL::micros();76uint32_t dt_us = now_us - last_update_us;77if (last_update_us != 0 && !isnan(internal_state.current_amps) && isnan(internal_state.consumed_mah)) {78AP_BattMonitor_Backend::update_consumed(_state, dt_us);79}80last_update_us = now_us;81return true;82}8384#endif // AP_BATTERY_SCRIPTING_ENABLED858687