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_Generator.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*/1415#include "AP_BattMonitor_config.h"1617#if AP_BATTERY_GENERATOR_ENABLED1819#include <AP_Common/AP_Common.h>20#include <AP_Math/AP_Math.h>2122#include "AP_BattMonitor_Generator.h"2324/*25Fuel class26*/27// This is where we tell the battery monitor 'we have current' if we want to report a fuel level remaining28bool AP_BattMonitor_Generator_FuelLevel::has_current(void) const29{30// If the generator has fuel remaining we must also state that we have current31return has_consumed_energy();32}3334// This is where we tell the battery monitor 'we have consumed energy' if we want to report a fuel level remaining35bool AP_BattMonitor_Generator_FuelLevel::has_consumed_energy(void) const36{37// Get pointer to generator singleton38AP_Generator *generator = AP::generator();3940if (generator == nullptr) {41return false;42}4344// Use consumed_mAh in BattMonitor to display fuel remaining45return generator->has_fuel_remaining();46}4748void AP_BattMonitor_Generator_FuelLevel::init()49{50// Set params for users:51// Fuel level is only reported as a percentage52_params._pack_capacity.set(100);53// Fuel only reports a fixed 1v, don't want batt monitor failsafes on this instance54_params._low_voltage.set(0);55_params._critical_voltage.set(0);56}5758// Read the fuel level. Should be called at 10hz59void AP_BattMonitor_Generator_FuelLevel::read()60{61_state.healthy = false;6263// Get pointer to generator singleton64AP_Generator *generator = AP::generator();6566// Not healthy if we can't find a generator67if (generator == nullptr) {68return;69}7071if (!generator->healthy()) {72return;73}7475// As this is a battery monitor instance report voltage76// Report fixed voltage of 1V77_state.voltage = 1.0f;7879// This is a bodge to display tank level as a percentage on GCS. Users should set _params.pack_capacity == 100 to get a clear percentage in GCS80_state.consumed_mah = (1 - generator->get_fuel_remaining()) * _params._pack_capacity.get();8182// If we got this far then must be healthy83_state.healthy = true;84_state.last_time_micros = AP_HAL::micros();85}8687/*88Electrical class89*/90bool AP_BattMonitor_Generator_Elec::has_current(void) const91{92// Get pointer to generator singleton93AP_Generator *generator = AP::generator();9495if (generator == nullptr) {96return false;97}9899return generator->has_current();100}101102bool AP_BattMonitor_Generator_Elec::has_consumed_energy(void) const103{104// Get pointer to generator singleton105AP_Generator *generator = AP::generator();106107if (generator == nullptr) {108return false;109}110111return generator->has_consumed_energy();112}113114// Read the electrical measurements from the generator115void AP_BattMonitor_Generator_Elec::read()116{117_state.healthy = false;118119// Get pointer to generator singleton120AP_Generator *generator = AP::generator();121122// Not healthy if we can't find a generator123if (generator == nullptr) {124return;125}126127if (!generator->healthy()) {128return;129}130131// Update readings132_state.voltage = generator->get_voltage();133134_state.current_amps = generator->get_current();135136// Always reset consumed value, integration is done in AP_Generator library137_state.consumed_mah = generator->get_batt_consumed();138_state.consumed_wh = 0.001f * _state.consumed_mah * _state.voltage;139140// If we got this far then must be healthy141_state.healthy = true;142_state.last_time_micros = AP_HAL::micros();143}144145AP_BattMonitor::Failsafe AP_BattMonitor_Generator_Elec::update_failsafes()146{147AP_BattMonitor::Failsafe failsafe = AP_BattMonitor::Failsafe::None;148149AP_Generator *generator = AP::generator();150151// Only check for failsafes on the electrical monitor152// no point in having the same failsafe on two battery monitors153if (generator != nullptr) {154failsafe = generator->update_failsafes();155}156return MAX(AP_BattMonitor_Backend::update_failsafes(), failsafe);157}158#endif // AP_BATTERY_GENERATOR_ENABLED159160161