mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-18 19:23:34 +08:00
Refactored to work with new battery_status module
This commit is contained in:
committed by
Julian Oes
parent
d7bb5d46bb
commit
993fa5bd37
@@ -1,6 +1,6 @@
|
||||
############################################################################
|
||||
#
|
||||
# Copyright (c) 2018 PX4 Development Team. All rights reserved.
|
||||
# Copyright (c) 2019 PX4 Development Team. All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions
|
||||
@@ -31,4 +31,7 @@
|
||||
#
|
||||
############################################################################
|
||||
|
||||
px4_add_library(battery battery_base.cpp battery.cpp)
|
||||
px4_add_library(battery battery.cpp)
|
||||
|
||||
# TODO: Add an option in px4_add_library function to add module config file
|
||||
set_property(GLOBAL APPEND PROPERTY PX4_MODULE_CONFIG_FILES ${CMAKE_CURRENT_SOURCE_DIR}/module.yaml)
|
||||
|
||||
+343
-8
@@ -1,14 +1,349 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2019 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file battery.cpp
|
||||
*
|
||||
* Library calls for battery functionality.
|
||||
*
|
||||
* @author Julian Oes <julian@oes.ch>
|
||||
* @author Timothy Scott <timothy@auterion.com>
|
||||
*/
|
||||
|
||||
#include "battery.h"
|
||||
#include <mathlib/mathlib.h>
|
||||
#include <cstring>
|
||||
#include <px4_platform_common/defines.h>
|
||||
|
||||
Battery1::Battery1()
|
||||
Battery::Battery(int index, ModuleParams *parent) :
|
||||
ModuleParams(parent),
|
||||
_index(index < 1 || index > 9 ? 1 : index),
|
||||
_warning(battery_status_s::BATTERY_WARNING_NONE),
|
||||
_last_timestamp(0)
|
||||
{
|
||||
updateParams();
|
||||
if (index > 9 || index < 1) {
|
||||
PX4_ERR("Battery index must be between 1 and 9 (inclusive). Received %d. Defaulting to 1.", index);
|
||||
}
|
||||
|
||||
migrateParam(_param_old_bat_v_empty, _param_bat_v_empty, "V_EMPTY", 3.5f);
|
||||
migrateParam(_param_old_bat_v_charged, _param_bat_v_charged, "V_CHARGED", 4.05f);
|
||||
migrateParam(_param_old_bat_v_load_drop, _param_bat_v_load_drop, "V_LOAD_DROP", 0.3f);
|
||||
migrateParam(_param_old_bat_r_internal, _param_bat_r_internal, "R_INTERNAL", -1.0f);
|
||||
migrateParam(_param_old_bat_n_cells, _param_bat_n_cells, "N_CELLS", 0);
|
||||
migrateParam(_param_old_bat_capacity, _param_bat_capacity, "CAPACITY", -1.0f);
|
||||
// 16 chars for parameter name + null terminator
|
||||
char param_name[17];
|
||||
|
||||
snprintf(param_name, sizeof(param_name), "BAT%d_V_EMPTY", _index);
|
||||
_param_handles.v_empty = param_find(param_name);
|
||||
|
||||
if (_param_handles.v_empty == PARAM_INVALID) {
|
||||
PX4_ERR("Could not find parameter with name %s", param_name);
|
||||
}
|
||||
|
||||
snprintf(param_name, sizeof(param_name), "BAT%d_V_CHARGED", _index);
|
||||
_param_handles.v_charged = param_find(param_name);
|
||||
|
||||
snprintf(param_name, sizeof(param_name), "BAT%d_N_CELLS", _index);
|
||||
_param_handles.n_cells = param_find(param_name);
|
||||
|
||||
snprintf(param_name, sizeof(param_name), "BAT%d_CAPACITY", _index);
|
||||
_param_handles.capacity = param_find(param_name);
|
||||
|
||||
snprintf(param_name, sizeof(param_name), "BAT%d_V_LOAD_DROP", _index);
|
||||
_param_handles.v_load_drop = param_find(param_name);
|
||||
|
||||
snprintf(param_name, sizeof(param_name), "BAT%d_R_INTERNAL", _index);
|
||||
_param_handles.r_internal = param_find(param_name);
|
||||
|
||||
snprintf(param_name, sizeof(param_name), "BAT%d_SOURCE", _index);
|
||||
_param_handles.source = param_find(param_name);
|
||||
|
||||
_param_handles.low_thr = param_find("BAT_LOW_THR");
|
||||
_param_handles.crit_thr = param_find("BAT_CRIT_THR");
|
||||
_param_handles.emergen_thr = param_find("BAT_EMERGEN_THR");
|
||||
|
||||
_param_handles.v_empty_old = param_find("BAT_V_EMPTY");
|
||||
_param_handles.v_charged_old = param_find("BAT_V_CHARGED");
|
||||
_param_handles.n_cells_old = param_find("BAT_N_CELLS");
|
||||
_param_handles.capacity_old = param_find("BAT_CAPACITY");
|
||||
_param_handles.v_load_drop_old = param_find("BAT_V_LOAD_DROP");
|
||||
_param_handles.r_internal_old = param_find("BAT_R_INTERNAL");
|
||||
_param_handles.source_old = param_find("BAT_SOURCE");
|
||||
|
||||
updateParams();
|
||||
}
|
||||
|
||||
void
|
||||
Battery::reset()
|
||||
{
|
||||
memset(&_battery_status, 0, sizeof(_battery_status));
|
||||
_battery_status.current_a = -1.f;
|
||||
_battery_status.remaining = 1.f;
|
||||
_battery_status.scale = 1.f;
|
||||
_battery_status.cell_count = _params.n_cells;
|
||||
// TODO: check if it is sane to reset warning to NONE
|
||||
_battery_status.warning = battery_status_s::BATTERY_WARNING_NONE;
|
||||
_battery_status.connected = false;
|
||||
_battery_status.capacity = _params.capacity;
|
||||
_battery_status.temperature = NAN;
|
||||
_battery_status.id = (uint8_t) _index;
|
||||
}
|
||||
|
||||
void
|
||||
Battery::updateBatteryStatus(hrt_abstime timestamp, float voltage_v, float current_a,
|
||||
bool connected, bool selected_source, int priority,
|
||||
float throttle_normalized,
|
||||
bool armed, bool should_publish)
|
||||
{
|
||||
reset();
|
||||
_battery_status.timestamp = timestamp;
|
||||
filterVoltage(voltage_v);
|
||||
filterThrottle(throttle_normalized);
|
||||
filterCurrent(current_a);
|
||||
sumDischarged(timestamp, current_a);
|
||||
estimateRemaining(_voltage_filtered_v, _current_filtered_a, _throttle_filtered, armed);
|
||||
computeScale();
|
||||
|
||||
if (_battery_initialized) {
|
||||
determineWarning(connected);
|
||||
}
|
||||
|
||||
if (_voltage_filtered_v > 2.1f) {
|
||||
_battery_initialized = true;
|
||||
_battery_status.voltage_v = voltage_v;
|
||||
_battery_status.voltage_filtered_v = _voltage_filtered_v;
|
||||
_battery_status.scale = _scale;
|
||||
_battery_status.current_a = current_a;
|
||||
_battery_status.current_filtered_a = _current_filtered_a;
|
||||
_battery_status.discharged_mah = _discharged_mah;
|
||||
_battery_status.warning = _warning;
|
||||
_battery_status.remaining = _remaining;
|
||||
_battery_status.connected = connected;
|
||||
_battery_status.system_source = selected_source;
|
||||
_battery_status.priority = priority;
|
||||
}
|
||||
|
||||
_battery_status.timestamp = timestamp;
|
||||
|
||||
if (should_publish) {
|
||||
publish();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Battery::publish()
|
||||
{
|
||||
orb_publish_auto(ORB_ID(battery_status), &_orb_advert, &_battery_status, &_orb_instance, ORB_PRIO_DEFAULT);
|
||||
}
|
||||
|
||||
void
|
||||
Battery::swapUorbAdvert(Battery &other)
|
||||
{
|
||||
orb_advert_t tmp = _orb_advert;
|
||||
_orb_advert = other._orb_advert;
|
||||
other._orb_advert = tmp;
|
||||
}
|
||||
|
||||
void
|
||||
Battery::filterVoltage(float voltage_v)
|
||||
{
|
||||
if (!_battery_initialized) {
|
||||
_voltage_filtered_v = voltage_v;
|
||||
}
|
||||
|
||||
// TODO: inspect that filter performance
|
||||
const float filtered_next = _voltage_filtered_v * 0.99f + voltage_v * 0.01f;
|
||||
|
||||
if (PX4_ISFINITE(filtered_next)) {
|
||||
_voltage_filtered_v = filtered_next;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Battery::filterCurrent(float current_a)
|
||||
{
|
||||
if (!_battery_initialized) {
|
||||
_current_filtered_a = current_a;
|
||||
}
|
||||
|
||||
// ADC poll is at 100Hz, this will perform a low pass over approx 500ms
|
||||
const float filtered_next = _current_filtered_a * 0.98f + current_a * 0.02f;
|
||||
|
||||
if (PX4_ISFINITE(filtered_next)) {
|
||||
_current_filtered_a = filtered_next;
|
||||
}
|
||||
}
|
||||
|
||||
void Battery::filterThrottle(float throttle)
|
||||
{
|
||||
if (!_battery_initialized) {
|
||||
_throttle_filtered = throttle;
|
||||
}
|
||||
|
||||
const float filtered_next = _throttle_filtered * 0.99f + throttle * 0.01f;
|
||||
|
||||
if (PX4_ISFINITE(filtered_next)) {
|
||||
_throttle_filtered = filtered_next;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Battery::sumDischarged(hrt_abstime timestamp, float current_a)
|
||||
{
|
||||
// Not a valid measurement
|
||||
if (current_a < 0.f) {
|
||||
// Because the measurement was invalid we need to stop integration
|
||||
// and re-initialize with the next valid measurement
|
||||
_last_timestamp = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// Ignore first update because we don't know dt.
|
||||
if (_last_timestamp != 0) {
|
||||
const float dt = (timestamp - _last_timestamp) / 1e6;
|
||||
// mAh since last loop: (current[A] * 1000 = [mA]) * (dt[s] / 3600 = [h])
|
||||
_discharged_mah_loop = (current_a * 1e3f) * (dt / 3600.f);
|
||||
_discharged_mah += _discharged_mah_loop;
|
||||
}
|
||||
|
||||
_last_timestamp = timestamp;
|
||||
}
|
||||
|
||||
void
|
||||
Battery::estimateRemaining(float voltage_v, float current_a, float throttle, bool armed)
|
||||
{
|
||||
// remaining battery capacity based on voltage
|
||||
float cell_voltage = voltage_v / _params.n_cells;
|
||||
|
||||
// correct battery voltage locally for load drop to avoid estimation fluctuations
|
||||
if (_params.r_internal >= 0.f) {
|
||||
cell_voltage += _params.r_internal * current_a;
|
||||
|
||||
} else {
|
||||
// assume linear relation between throttle and voltage drop
|
||||
cell_voltage += throttle * _params.v_load_drop;
|
||||
}
|
||||
|
||||
_remaining_voltage = math::gradual(cell_voltage, _params.v_empty, _params.v_charged, 0.f, 1.f);
|
||||
|
||||
// choose which quantity we're using for final reporting
|
||||
if (_params.capacity > 0.f) {
|
||||
// if battery capacity is known, fuse voltage measurement with used capacity
|
||||
if (!_battery_initialized) {
|
||||
// initialization of the estimation state
|
||||
_remaining = _remaining_voltage;
|
||||
|
||||
} else {
|
||||
// The lower the voltage the more adjust the estimate with it to avoid deep discharge
|
||||
const float weight_v = 3e-4f * (1 - _remaining_voltage);
|
||||
_remaining = (1 - weight_v) * _remaining + weight_v * _remaining_voltage;
|
||||
// directly apply current capacity slope calculated using current
|
||||
_remaining -= _discharged_mah_loop / _params.capacity;
|
||||
_remaining = math::max(_remaining, 0.f);
|
||||
}
|
||||
|
||||
} else {
|
||||
// else use voltage
|
||||
_remaining = _remaining_voltage;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Battery::determineWarning(bool connected)
|
||||
{
|
||||
if (connected) {
|
||||
// propagate warning state only if the state is higher, otherwise remain in current warning state
|
||||
if (_remaining < _params.emergen_thr) {
|
||||
_warning = battery_status_s::BATTERY_WARNING_EMERGENCY;
|
||||
|
||||
} else if (_remaining < _params.crit_thr) {
|
||||
_warning = battery_status_s::BATTERY_WARNING_CRITICAL;
|
||||
|
||||
} else if (_remaining < _params.low_thr) {
|
||||
_warning = battery_status_s::BATTERY_WARNING_LOW;
|
||||
|
||||
} else {
|
||||
_warning = battery_status_s::BATTERY_WARNING_NONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Battery::computeScale()
|
||||
{
|
||||
const float voltage_range = (_params.v_charged - _params.v_empty);
|
||||
|
||||
// reusing capacity calculation to get single cell voltage before drop
|
||||
const float bat_v = _params.v_empty + (voltage_range * _remaining_voltage);
|
||||
|
||||
_scale = _params.v_charged / bat_v;
|
||||
|
||||
if (_scale > 1.3f) { // Allow at most 30% compensation
|
||||
_scale = 1.3f;
|
||||
|
||||
} else if (!PX4_ISFINITE(_scale) || _scale < 1.f) { // Shouldn't ever be more than the power at full battery
|
||||
_scale = 1.f;
|
||||
}
|
||||
}
|
||||
|
||||
void Battery::updateParams()
|
||||
{
|
||||
if (_index == 1) {
|
||||
migrateParam<float>(_param_handles.v_empty_old, _param_handles.v_empty, &_params.v_empty_old, &_params.v_empty,
|
||||
_first_parameter_update);
|
||||
migrateParam<float>(_param_handles.v_charged_old, _param_handles.v_charged, &_params.v_charged_old, &_params.v_charged,
|
||||
_first_parameter_update);
|
||||
migrateParam<int>(_param_handles.n_cells_old, _param_handles.n_cells, &_params.n_cells_old, &_params.n_cells,
|
||||
_first_parameter_update);
|
||||
migrateParam<float>(_param_handles.capacity_old, _param_handles.capacity, &_params.capacity_old, &_params.capacity,
|
||||
_first_parameter_update);
|
||||
migrateParam<float>(_param_handles.v_load_drop_old, _param_handles.v_load_drop, &_params.v_load_drop_old,
|
||||
&_params.v_load_drop, _first_parameter_update);
|
||||
migrateParam<float>(_param_handles.r_internal_old, _param_handles.r_internal, &_params.r_internal_old,
|
||||
&_params.r_internal, _first_parameter_update);
|
||||
migrateParam<int>(_param_handles.source_old, _param_handles.source, &_params.source_old, &_params.source,
|
||||
_first_parameter_update);
|
||||
|
||||
} else {
|
||||
param_get(_param_handles.v_empty, &_params.v_empty);
|
||||
param_get(_param_handles.v_charged, &_params.v_charged);
|
||||
param_get(_param_handles.n_cells, &_params.n_cells);
|
||||
param_get(_param_handles.capacity, &_params.capacity);
|
||||
param_get(_param_handles.v_load_drop, &_params.v_load_drop);
|
||||
param_get(_param_handles.r_internal, &_params.r_internal);
|
||||
param_get(_param_handles.source, &_params.source);
|
||||
}
|
||||
|
||||
param_get(_param_handles.low_thr, &_params.low_thr);
|
||||
param_get(_param_handles.crit_thr, &_params.crit_thr);
|
||||
param_get(_param_handles.emergen_thr, &_params.emergen_thr);
|
||||
|
||||
ModuleParams::updateParams();
|
||||
|
||||
_first_parameter_update = false;
|
||||
}
|
||||
|
||||
+204
-133
@@ -31,157 +31,228 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "battery_base.h"
|
||||
|
||||
#include <px4_log.h>
|
||||
#include <math.h>
|
||||
|
||||
/**
|
||||
* @file battery.h
|
||||
* Basic implementation of BatteryBase. Battery1 is calibrated by BAT1_* parameters. Battery2 is calibrated
|
||||
* by BAT2_* parameters.
|
||||
*
|
||||
* The multiple batteries all share the same logic for calibration. The only difference is which parameters are used
|
||||
* (Battery 1 uses `BAT1_*`, while Battery 2 uses `BAT2_*`). To avoid code duplication, inheritance is being used.
|
||||
* The problem is that the `ModuleParams` class depends on a macro which defines member variables. You can't override
|
||||
* member variables in C++, so we have to declare virtual getter functions in BatteryBase, and implement them here.
|
||||
* Library calls for battery functionality.
|
||||
*
|
||||
* The alternative would be to avoid ModuleParams entirely, and build parameter names dynamically, like so:
|
||||
* ```
|
||||
* char param_name[17]; //16 max length of parameter name, + null terminator
|
||||
* int battery_index = 1; // Or 2 or 3 or whatever
|
||||
* snprintf(param_name, 17, "BAT%d_N_CELLS", battery_index);
|
||||
* param_find(param_name); // etc
|
||||
* ```
|
||||
*
|
||||
* This was decided against because the newer ModuleParams API provides more type safety and avoids code duplication.
|
||||
*
|
||||
* To add a third battery, follow these steps:
|
||||
* - Copy/Paste all of Battery2 to make Battery3
|
||||
* - Change all "BAT2_*" parameters to "BAT3_*" in Battery3
|
||||
* - Copy the file "battery_params_2.c" to "battery_params_3.c"
|
||||
* - Change all of the "BAT2_*" params in "battery_params_3.c" to "BAT3_*"
|
||||
* This is not done now because there is not yet any demand for a third battery, and adding parameters uses up space.
|
||||
* @author Julian Oes <julian@oes.ch>
|
||||
* @author Timothy Scott <timothy@auterion.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <uORB/uORB.h>
|
||||
#include <uORB/Subscription.hpp>
|
||||
#include <uORB/topics/battery_status.h>
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <px4_platform_common/module_params.h>
|
||||
#include <parameters/param.h>
|
||||
#include <drivers/drv_adc.h>
|
||||
#include <board_config.h>
|
||||
#include <px4_platform_common/board_common.h>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
|
||||
/**
|
||||
* Battery1 represents a battery calibrated by BAT1_* parameters.
|
||||
* BatteryBase is a base class for any type of battery.
|
||||
*
|
||||
* You can use this class on its own. Or, if you need to implement a custom battery type,
|
||||
* you can inherit from this class. See, for example, src/modules/battery_status/AnalogBattery.h
|
||||
*/
|
||||
class Battery1 : public BatteryBase
|
||||
class Battery : public ModuleParams
|
||||
{
|
||||
public:
|
||||
Battery1();
|
||||
Battery(int index = 1, ModuleParams *parent = nullptr);
|
||||
|
||||
/**
|
||||
* This function migrates the old deprecated parameters like BAT_N_CELLS to the new parameters like BAT1_N_CELLS.
|
||||
* It checks if the old parameter is non-defaulT AND the new parameter IS default, and if so, it:
|
||||
* - Issues a warning using PX4_WARN(...)
|
||||
* - Copies the value of the old parameter over to the new parameter
|
||||
* - Resets the old parameter to its default
|
||||
*
|
||||
* The 'name' parameter should be only the part of the parameter name that comes after "BAT1_" or "BAT_". It is
|
||||
* used only for the warning message. For example, for parameter BAT1_N_CELLS, name should be "N_CELLS".
|
||||
* (See the implementation of this function for why I have taken this strange choice)
|
||||
*
|
||||
* In an ideal world, this function would be protected so that only child classes of Battery1 could access it.
|
||||
* However, the way ModuleParams works makes it very difficult to inherit from a ModuleParams class.
|
||||
* For example, the AnalogBattery class in the Sensors module does not inherit this class; it just contains
|
||||
* a Battery1 member variable.
|
||||
*
|
||||
* The templating is complicated because every parameter is technically a different type. However, in normal
|
||||
* use, the template can just be ignored. See the implementation of Battery1::Battery1() for example usage.
|
||||
*
|
||||
* @tparam P1 Type of the first parameter
|
||||
* @tparam P2 Type of the second parameter
|
||||
* @tparam T Data type for the default value
|
||||
* @param oldParam Reference to the old parameter, as a ParamFloat<...>, ParamInt<...>, or ParamBool<...>
|
||||
* @param newParam Reference to the new paramater, as a ParamFloat<...>, ParamInt<...>, or ParamBool<...>
|
||||
* @param name The name of the parameter, WITHOUT the "BAT_" or "BAT1_" prefix. This is used only for logging.
|
||||
* @param defaultValue Default value of the parameter, as specified in PARAM_DEFINE_*(...)
|
||||
* Reset all battery stats and report invalid/nothing.
|
||||
*/
|
||||
template<class P1, class P2, typename T> static void
|
||||
migrateParam(P1 &oldParam, P2 &newParam, const char *name, T defaultValue)
|
||||
{
|
||||
float diffOld = fabs((float) oldParam.get() - defaultValue);
|
||||
float diffNew = fabs((float) newParam.get() - defaultValue);
|
||||
void reset();
|
||||
|
||||
if (diffOld > 0.0001f && diffNew < 0.0001f) {
|
||||
PX4_WARN("Parameter BAT_%s is deprecated. Copying value to BAT1_%s.", name, name);
|
||||
newParam.set(oldParam.get());
|
||||
oldParam.set(defaultValue);
|
||||
newParam.commit();
|
||||
oldParam.commit();
|
||||
/**
|
||||
* Get the battery cell count
|
||||
*/
|
||||
int cell_count() { return _params.n_cells; }
|
||||
|
||||
/**
|
||||
* Get the empty voltage per cell
|
||||
*/
|
||||
float empty_cell_voltage() { return _params.v_empty; }
|
||||
|
||||
/**
|
||||
* Get the full voltage per cell
|
||||
*/
|
||||
float full_cell_voltage() { return _params.v_charged; }
|
||||
|
||||
int source() { return _params.source; }
|
||||
|
||||
/**
|
||||
* Update current battery status message.
|
||||
*
|
||||
* @param voltage_raw: Battery voltage, in Volts
|
||||
* @param current_raw: Battery current, in Amps
|
||||
* @param timestamp: Time at which the ADC was read (use hrt_absolute_time())
|
||||
* @param selected_source: This battery is on the brick that the selected source for selected_source
|
||||
* @param priority: The brick number -1. The term priority refers to the Vn connection on the LTC4417
|
||||
* @param throttle_normalized: Throttle of the vehicle, between 0 and 1
|
||||
* @param armed: Arming state of the vehicle
|
||||
* @param should_publish If True, this function published a battery_status uORB message.
|
||||
*/
|
||||
void updateBatteryStatus(hrt_abstime timestamp, float voltage_v, float current_a, bool connected,
|
||||
bool selected_source, int priority, float throttle_normalized, bool armed, bool should_publish);
|
||||
|
||||
/**
|
||||
* Publishes the uORB battery_status message with the most recently-updated data.
|
||||
*/
|
||||
void publish();
|
||||
|
||||
/**
|
||||
* Some old functionality expects the primary battery to be published on instance 0. To maintain backwards
|
||||
* compatibility, this function allows the advertisements (and therefore instances) of 2 batteries to be swapped.
|
||||
* However, this should not be relied upon anywhere, and should be considered for all intents deprecated.
|
||||
*
|
||||
* The proper way to uniquely identify batteries is by the `id` field in the `battery_status` message.
|
||||
*/
|
||||
void swapUorbAdvert(Battery &other);
|
||||
|
||||
protected:
|
||||
struct {
|
||||
param_t v_empty;
|
||||
param_t v_charged;
|
||||
param_t n_cells;
|
||||
param_t capacity;
|
||||
param_t v_load_drop;
|
||||
param_t r_internal;
|
||||
param_t low_thr;
|
||||
param_t crit_thr;
|
||||
param_t emergen_thr;
|
||||
param_t source;
|
||||
|
||||
// TODO: These parameters are depracated. They can be removed entirely once the
|
||||
// new version of Firmware has been around for long enough.
|
||||
param_t v_empty_old;
|
||||
param_t v_charged_old;
|
||||
param_t n_cells_old;
|
||||
param_t capacity_old;
|
||||
param_t v_load_drop_old;
|
||||
param_t r_internal_old;
|
||||
param_t source_old;
|
||||
} _param_handles;
|
||||
|
||||
struct {
|
||||
float v_empty;
|
||||
float v_charged;
|
||||
int n_cells;
|
||||
float capacity;
|
||||
float v_load_drop;
|
||||
float r_internal;
|
||||
float low_thr;
|
||||
float crit_thr;
|
||||
float emergen_thr;
|
||||
int source;
|
||||
|
||||
// TODO: These parameters are depracated. They can be removed entirely once the
|
||||
// new version of Firmware has been around for long enough.
|
||||
float v_empty_old;
|
||||
float v_charged_old;
|
||||
int n_cells_old;
|
||||
float capacity_old;
|
||||
float v_load_drop_old;
|
||||
float r_internal_old;
|
||||
int source_old;
|
||||
} _params;
|
||||
|
||||
battery_status_s _battery_status;
|
||||
|
||||
const int _index;
|
||||
|
||||
bool _first_parameter_update{false};
|
||||
virtual void updateParams() override;
|
||||
|
||||
/**
|
||||
* This function helps with migrating to new parameters. It performs several tasks:
|
||||
* - Update both the old and new parameter values using `param_get(...)`
|
||||
* - Check if either parameter changed just now
|
||||
* - If so, display a warning if the deprecated parameter was used
|
||||
* - Copy the new value over to the other parameter
|
||||
* - If this is the first time the parameters are fetched, check if they are equal
|
||||
* - If not, display a warning and copy the value of the deprecated parameter over to the new one
|
||||
* @tparam T Type of the parameter (int or float)
|
||||
* @param old_param Handle to the old deprecated parameter (for example, param_find("BAT_N_CELLS")
|
||||
* @param new_param Handle to the new replacement parameter (for example, param_find("BAT1_N_CELLS")
|
||||
* @param old_val Pointer to the value of the old deprecated parameter
|
||||
* @param new_val Pointer to the value of the new replacement parameter
|
||||
* @param firstcall If true, then this function will not check to see if the values have changed
|
||||
* (Since the old values are uninitialized)
|
||||
* @return True iff either of these parameters changed just now and the migration was done.
|
||||
*/
|
||||
template<typename T>
|
||||
bool migrateParam(param_t old_param, param_t new_param, T *old_val, T *new_val, bool firstcall)
|
||||
{
|
||||
|
||||
T previous_old_val = *old_val;
|
||||
T previous_new_val = *new_val;
|
||||
|
||||
param_get(old_param, old_val);
|
||||
param_get(new_param, new_val);
|
||||
|
||||
if (!firstcall) {
|
||||
if ((float) fabs((float) *old_val - (float) previous_old_val) > FLT_EPSILON
|
||||
&& (float) fabs((float) *old_val - (float) *new_val) > FLT_EPSILON) {
|
||||
// TODO fix this error message. I was getting hardfaults while using this message, but they stopped
|
||||
// after removing this message. I was unable to determine why I was getting them.
|
||||
PX4_WARN("Detected change of deprecated parameter %s. Please use the new parameter %s instead. "
|
||||
"The new value of the deprecated parameter will be copied to the new parameter.",
|
||||
param_name(old_param), param_name(new_param));
|
||||
param_set_no_notification(new_param, old_val);
|
||||
param_get(new_param, new_val);
|
||||
return true;
|
||||
|
||||
} else if ((float) fabs((float) *new_val - (float) previous_new_val) > FLT_EPSILON
|
||||
&& (float) fabs((float) *old_val - (float) *new_val) > FLT_EPSILON) {
|
||||
PX4_INFO("Copying new value for %s to deprecated parameter %s.",
|
||||
param_name(new_param), param_name(old_param));
|
||||
param_set_no_notification(old_param, new_val);
|
||||
param_get(old_param, old_val);
|
||||
return true;
|
||||
}
|
||||
|
||||
} else {
|
||||
if ((float) fabs((float) *old_val - (float) *new_val) > FLT_EPSILON) {
|
||||
PX4_WARN("At boot, deprecated parameter %s is different from its replacement %s."
|
||||
" The new value will be overwritten by the old one. This should happen only once.",
|
||||
param_name(old_param), param_name(new_param));
|
||||
param_set_no_notification(new_param, old_val);
|
||||
param_get(new_param, new_val);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
void filterVoltage(float voltage_v);
|
||||
void filterThrottle(float throttle);
|
||||
void filterCurrent(float current_a);
|
||||
void sumDischarged(hrt_abstime timestamp, float current_a);
|
||||
void estimateRemaining(float voltage_v, float current_a, float throttle, bool armed);
|
||||
void determineWarning(bool connected);
|
||||
void computeScale();
|
||||
|
||||
DEFINE_PARAMETERS(
|
||||
(ParamFloat<px4::params::BAT_V_EMPTY>) _param_old_bat_v_empty,
|
||||
(ParamFloat<px4::params::BAT_V_CHARGED>) _param_old_bat_v_charged,
|
||||
(ParamInt<px4::params::BAT_N_CELLS>) _param_old_bat_n_cells,
|
||||
(ParamFloat<px4::params::BAT_CAPACITY>) _param_old_bat_capacity,
|
||||
(ParamFloat<px4::params::BAT_V_LOAD_DROP>) _param_old_bat_v_load_drop,
|
||||
(ParamFloat<px4::params::BAT_R_INTERNAL>) _param_old_bat_r_internal,
|
||||
bool _battery_initialized = false;
|
||||
float _voltage_filtered_v = -1.f;
|
||||
float _throttle_filtered = -1.f;
|
||||
float _current_filtered_a = -1.f;
|
||||
float _discharged_mah = 0.f;
|
||||
float _discharged_mah_loop = 0.f;
|
||||
float _remaining_voltage = -1.f; ///< normalized battery charge level remaining based on voltage
|
||||
float _remaining = -1.f; ///< normalized battery charge level, selected based on config param
|
||||
float _scale = 1.f;
|
||||
uint8_t _warning;
|
||||
hrt_abstime _last_timestamp;
|
||||
|
||||
(ParamFloat<px4::params::BAT1_V_EMPTY>) _param_bat_v_empty,
|
||||
(ParamFloat<px4::params::BAT1_V_CHARGED>) _param_bat_v_charged,
|
||||
(ParamInt<px4::params::BAT1_N_CELLS>) _param_bat_n_cells,
|
||||
(ParamFloat<px4::params::BAT1_CAPACITY>) _param_bat_capacity,
|
||||
(ParamFloat<px4::params::BAT1_V_LOAD_DROP>) _param_bat_v_load_drop,
|
||||
(ParamFloat<px4::params::BAT1_R_INTERNAL>) _param_bat_r_internal,
|
||||
|
||||
(ParamFloat<px4::params::BAT_LOW_THR>) _param_bat_low_thr,
|
||||
(ParamFloat<px4::params::BAT_CRIT_THR>) _param_bat_crit_thr,
|
||||
(ParamFloat<px4::params::BAT_EMERGEN_THR>) _param_bat_emergen_thr,
|
||||
(ParamInt<px4::params::BAT_SOURCE>) _param_source
|
||||
)
|
||||
|
||||
float _get_bat_v_empty() override {return _param_bat_v_empty.get(); }
|
||||
float _get_bat_v_charged() override {return _param_bat_v_charged.get(); }
|
||||
int _get_bat_n_cells() override {return _param_bat_n_cells.get(); }
|
||||
float _get_bat_capacity() override {return _param_bat_capacity.get(); }
|
||||
float _get_bat_v_load_drop() override {return _param_bat_v_load_drop.get(); }
|
||||
float _get_bat_r_internal() override {return _param_bat_r_internal.get(); }
|
||||
float _get_bat_low_thr() override {return _param_bat_low_thr.get(); }
|
||||
float _get_bat_crit_thr() override {return _param_bat_crit_thr.get(); }
|
||||
float _get_bat_emergen_thr() override {return _param_bat_emergen_thr.get(); }
|
||||
int _get_source() override {return _param_source.get(); }
|
||||
orb_advert_t _orb_advert{nullptr};
|
||||
int _orb_instance;
|
||||
};
|
||||
|
||||
/**
|
||||
* Battery2 represents a battery calibrated by BAT2_* parameters.
|
||||
*/
|
||||
class Battery2 : public BatteryBase
|
||||
{
|
||||
public:
|
||||
Battery2() {}
|
||||
private:
|
||||
|
||||
DEFINE_PARAMETERS(
|
||||
(ParamFloat<px4::params::BAT2_V_EMPTY>) _param_bat_v_empty,
|
||||
(ParamFloat<px4::params::BAT2_V_CHARGED>) _param_bat_v_charged,
|
||||
(ParamInt<px4::params::BAT2_N_CELLS>) _param_bat_n_cells,
|
||||
(ParamFloat<px4::params::BAT2_CAPACITY>) _param_bat_capacity,
|
||||
(ParamFloat<px4::params::BAT2_V_LOAD_DROP>) _param_bat_v_load_drop,
|
||||
(ParamFloat<px4::params::BAT2_R_INTERNAL>) _param_bat_r_internal,
|
||||
|
||||
(ParamFloat<px4::params::BAT_LOW_THR>) _param_bat_low_thr,
|
||||
(ParamFloat<px4::params::BAT_CRIT_THR>) _param_bat_crit_thr,
|
||||
(ParamFloat<px4::params::BAT_EMERGEN_THR>) _param_bat_emergen_thr,
|
||||
(ParamInt<px4::params::BAT_SOURCE>) _param_source
|
||||
)
|
||||
|
||||
float _get_bat_v_empty() override {return _param_bat_v_empty.get(); }
|
||||
float _get_bat_v_charged() override {return _param_bat_v_charged.get(); }
|
||||
int _get_bat_n_cells() override {return _param_bat_n_cells.get(); }
|
||||
float _get_bat_capacity() override {return _param_bat_capacity.get(); }
|
||||
float _get_bat_v_load_drop() override {return _param_bat_v_load_drop.get(); }
|
||||
float _get_bat_r_internal() override {return _param_bat_r_internal.get(); }
|
||||
float _get_bat_low_thr() override {return _param_bat_low_thr.get(); }
|
||||
float _get_bat_crit_thr() override {return _param_bat_crit_thr.get(); }
|
||||
float _get_bat_emergen_thr() override {return _param_bat_emergen_thr.get(); }
|
||||
int _get_source() override {return _param_source.get(); }
|
||||
};
|
||||
@@ -1,256 +0,0 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2019 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file battery_base.cpp
|
||||
*
|
||||
* Library calls for battery functionality.
|
||||
*
|
||||
* @author Julian Oes <julian@oes.ch>
|
||||
* @author Timothy Scott <timothy@auterion.com>
|
||||
*/
|
||||
|
||||
#include "battery_base.h"
|
||||
#include <mathlib/mathlib.h>
|
||||
#include <cstring>
|
||||
#include <px4_platform_common/defines.h>
|
||||
|
||||
BatteryBase::BatteryBase() :
|
||||
ModuleParams(nullptr),
|
||||
_warning(battery_status_s::BATTERY_WARNING_NONE),
|
||||
_last_timestamp(0)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
BatteryBase::reset()
|
||||
{
|
||||
memset(&_battery_status, 0, sizeof(_battery_status));
|
||||
_battery_status.current_a = -1.f;
|
||||
_battery_status.remaining = 1.f;
|
||||
_battery_status.scale = 1.f;
|
||||
_battery_status.cell_count = _get_bat_n_cells();
|
||||
// TODO: check if it is sane to reset warning to NONE
|
||||
_battery_status.warning = battery_status_s::BATTERY_WARNING_NONE;
|
||||
_battery_status.connected = false;
|
||||
_battery_status.capacity = _get_bat_capacity();
|
||||
}
|
||||
|
||||
void
|
||||
BatteryBase::updateBatteryStatus(hrt_abstime timestamp, float voltage_v, float current_a, bool connected,
|
||||
bool selected_source, int priority, float throttle_normalized, bool armed, bool should_publish)
|
||||
{
|
||||
reset();
|
||||
_battery_status.timestamp = timestamp;
|
||||
filterVoltage(voltage_v);
|
||||
filterThrottle(throttle_normalized);
|
||||
filterCurrent(current_a);
|
||||
sumDischarged(timestamp, current_a);
|
||||
estimateRemaining(_voltage_filtered_v, _current_filtered_a, _throttle_filtered, armed);
|
||||
computeScale();
|
||||
|
||||
if (_battery_initialized) {
|
||||
determineWarning(connected);
|
||||
}
|
||||
|
||||
if (_voltage_filtered_v > 2.1f) {
|
||||
_battery_initialized = true;
|
||||
_battery_status.voltage_v = voltage_v;
|
||||
_battery_status.voltage_filtered_v = _voltage_filtered_v;
|
||||
_battery_status.scale = _scale;
|
||||
_battery_status.current_a = current_a;
|
||||
_battery_status.current_filtered_a = _current_filtered_a;
|
||||
_battery_status.discharged_mah = _discharged_mah;
|
||||
_battery_status.warning = _warning;
|
||||
_battery_status.remaining = _remaining;
|
||||
_battery_status.connected = connected;
|
||||
_battery_status.system_source = selected_source;
|
||||
_battery_status.priority = priority;
|
||||
}
|
||||
|
||||
_battery_status.timestamp = timestamp;
|
||||
|
||||
if (should_publish) {
|
||||
publish();
|
||||
}
|
||||
|
||||
_battery_status.temperature = NAN;
|
||||
}
|
||||
|
||||
void
|
||||
BatteryBase::publish()
|
||||
{
|
||||
orb_publish_auto(ORB_ID(battery_status), &_orbAdvert, &_battery_status, &_orbInstance, ORB_PRIO_DEFAULT);
|
||||
}
|
||||
|
||||
void
|
||||
BatteryBase::filterVoltage(float voltage_v)
|
||||
{
|
||||
if (!_battery_initialized) {
|
||||
_voltage_filtered_v = voltage_v;
|
||||
}
|
||||
|
||||
// TODO: inspect that filter performance
|
||||
const float filtered_next = _voltage_filtered_v * 0.99f + voltage_v * 0.01f;
|
||||
|
||||
if (PX4_ISFINITE(filtered_next)) {
|
||||
_voltage_filtered_v = filtered_next;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
BatteryBase::filterCurrent(float current_a)
|
||||
{
|
||||
if (!_battery_initialized) {
|
||||
_current_filtered_a = current_a;
|
||||
}
|
||||
|
||||
// ADC poll is at 100Hz, this will perform a low pass over approx 500ms
|
||||
const float filtered_next = _current_filtered_a * 0.98f + current_a * 0.02f;
|
||||
|
||||
if (PX4_ISFINITE(filtered_next)) {
|
||||
_current_filtered_a = filtered_next;
|
||||
}
|
||||
}
|
||||
|
||||
void BatteryBase::filterThrottle(float throttle)
|
||||
{
|
||||
if (!_battery_initialized) {
|
||||
_throttle_filtered = throttle;
|
||||
}
|
||||
|
||||
const float filtered_next = _throttle_filtered * 0.99f + throttle * 0.01f;
|
||||
|
||||
if (PX4_ISFINITE(filtered_next)) {
|
||||
_throttle_filtered = filtered_next;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
BatteryBase::sumDischarged(hrt_abstime timestamp, float current_a)
|
||||
{
|
||||
// Not a valid measurement
|
||||
if (current_a < 0.f) {
|
||||
// Because the measurement was invalid we need to stop integration
|
||||
// and re-initialize with the next valid measurement
|
||||
_last_timestamp = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// Ignore first update because we don't know dt.
|
||||
if (_last_timestamp != 0) {
|
||||
const float dt = (timestamp - _last_timestamp) / 1e6;
|
||||
// mAh since last loop: (current[A] * 1000 = [mA]) * (dt[s] / 3600 = [h])
|
||||
_discharged_mah_loop = (current_a * 1e3f) * (dt / 3600.f);
|
||||
_discharged_mah += _discharged_mah_loop;
|
||||
}
|
||||
|
||||
_last_timestamp = timestamp;
|
||||
}
|
||||
|
||||
void
|
||||
BatteryBase::estimateRemaining(float voltage_v, float current_a, float throttle, bool armed)
|
||||
{
|
||||
// remaining battery capacity based on voltage
|
||||
float cell_voltage = voltage_v / _get_bat_n_cells();
|
||||
|
||||
// correct battery voltage locally for load drop to avoid estimation fluctuations
|
||||
if (_get_bat_r_internal() >= 0.f) {
|
||||
cell_voltage += _get_bat_r_internal() * current_a;
|
||||
|
||||
} else {
|
||||
// assume linear relation between throttle and voltage drop
|
||||
cell_voltage += throttle * _get_bat_v_load_drop();
|
||||
}
|
||||
|
||||
_remaining_voltage = math::gradual(cell_voltage, _get_bat_v_empty(), _get_bat_v_charged(), 0.f, 1.f);
|
||||
|
||||
// choose which quantity we're using for final reporting
|
||||
if (_get_bat_capacity() > 0.f) {
|
||||
// if battery capacity is known, fuse voltage measurement with used capacity
|
||||
if (!_battery_initialized) {
|
||||
// initialization of the estimation state
|
||||
_remaining = _remaining_voltage;
|
||||
|
||||
} else {
|
||||
// The lower the voltage the more adjust the estimate with it to avoid deep discharge
|
||||
const float weight_v = 3e-4f * (1 - _remaining_voltage);
|
||||
_remaining = (1 - weight_v) * _remaining + weight_v * _remaining_voltage;
|
||||
// directly apply current capacity slope calculated using current
|
||||
_remaining -= _discharged_mah_loop / _get_bat_capacity();
|
||||
_remaining = math::max(_remaining, 0.f);
|
||||
}
|
||||
|
||||
} else {
|
||||
// else use voltage
|
||||
_remaining = _remaining_voltage;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
BatteryBase::determineWarning(bool connected)
|
||||
{
|
||||
if (connected) {
|
||||
// propagate warning state only if the state is higher, otherwise remain in current warning state
|
||||
if (_remaining < _get_bat_emergen_thr()) {
|
||||
_warning = battery_status_s::BATTERY_WARNING_EMERGENCY;
|
||||
|
||||
} else if (_remaining < _get_bat_crit_thr()) {
|
||||
_warning = battery_status_s::BATTERY_WARNING_CRITICAL;
|
||||
|
||||
} else if (_remaining < _get_bat_low_thr()) {
|
||||
_warning = battery_status_s::BATTERY_WARNING_LOW;
|
||||
|
||||
} else {
|
||||
_warning = battery_status_s::BATTERY_WARNING_NONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
BatteryBase::computeScale()
|
||||
{
|
||||
const float voltage_range = (_get_bat_v_charged() - _get_bat_v_empty());
|
||||
|
||||
// reusing capacity calculation to get single cell voltage before drop
|
||||
const float bat_v = _get_bat_v_empty() + (voltage_range * _remaining_voltage);
|
||||
|
||||
_scale = _get_bat_v_charged() / bat_v;
|
||||
|
||||
if (_scale > 1.3f) { // Allow at most 30% compensation
|
||||
_scale = 1.3f;
|
||||
|
||||
} else if (!PX4_ISFINITE(_scale) || _scale < 1.f) { // Shouldn't ever be more than the power at full battery
|
||||
_scale = 1.f;
|
||||
}
|
||||
}
|
||||
@@ -1,183 +0,0 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2019 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file battery_base.h
|
||||
*
|
||||
* Library calls for battery functionality.
|
||||
*
|
||||
* @author Julian Oes <julian@oes.ch>
|
||||
* @author Timothy Scott <timothy@auterion.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <uORB/uORB.h>
|
||||
#include <uORB/topics/battery_status.h>
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <px4_module_params.h>
|
||||
#include <drivers/drv_adc.h>
|
||||
#include <board_config.h>
|
||||
#include <drivers/boards/common/board_common.h>
|
||||
|
||||
/**
|
||||
* BatteryBase is a base class for any type of battery.
|
||||
*
|
||||
* Each of the virtual _get_*() functions corresponds to a parameter used for calibration. However, depending
|
||||
* on the type of battery, these values may not come from parameters.
|
||||
*
|
||||
* Most implementations of BatteryBase will also inherit ModuleParams, and most of the virtual function
|
||||
* implementations will look something like:
|
||||
* float _get_bat_v_empty() override {return _param_bat_v_empty().get();}
|
||||
*
|
||||
* For a full implementation, see src/modules/sensors/analog_battery.{cpp, h}
|
||||
* For a minimal implementation, see src/modules/simulator/simulator.h
|
||||
*/
|
||||
class BatteryBase : ModuleParams
|
||||
{
|
||||
public:
|
||||
BatteryBase();
|
||||
|
||||
/**
|
||||
* Reset all battery stats and report invalid/nothing.
|
||||
*/
|
||||
void reset();
|
||||
|
||||
/**
|
||||
* Get the battery cell count
|
||||
*/
|
||||
int cell_count() { return _get_bat_n_cells(); }
|
||||
|
||||
/**
|
||||
* Get the empty voltage per cell
|
||||
*/
|
||||
float empty_cell_voltage() { return _get_bat_v_empty(); }
|
||||
|
||||
/**
|
||||
* Get the full voltage per cell
|
||||
*/
|
||||
float full_cell_voltage() { return _get_bat_v_charged(); }
|
||||
|
||||
/**
|
||||
* Update current battery status message.
|
||||
*
|
||||
* @param voltage_raw Battery voltage read from ADC, in Volts
|
||||
* @param current_raw Voltage of current sense resistor, in Amps
|
||||
* @param timestamp Time at which the ADC was read (use hrt_absolute_time())
|
||||
* @param selected_source This battery is on the brick that the selected source for selected_source
|
||||
* @param priority: The brick number -1. The term priority refers to the Vn connection on the LTC4417
|
||||
* @param throttle_normalized Throttle of the vehicle, between 0 and 1
|
||||
* @param armed Arming state of the vehicle
|
||||
* @param should_publish If True, this function published a battery_status uORB message.
|
||||
*/
|
||||
void updateBatteryStatus(hrt_abstime timestamp, float voltage_v, float current_a, bool connected,
|
||||
bool selected_source, int priority, float throttle_normalized, bool armed, bool should_publish);
|
||||
|
||||
/**
|
||||
* Publishes the uORB battery_status message with the most recently-updated data.
|
||||
*/
|
||||
void publish();
|
||||
|
||||
// TODO: Check that there was actually a parameter update
|
||||
void updateParams() {ModuleParams::updateParams();}
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @return Value, in volts, at which a single cell of the battery would be considered empty.
|
||||
*/
|
||||
virtual float _get_bat_v_empty() = 0;
|
||||
/**
|
||||
* @return Value, in volts, at which a single cell of the battery would be considered full.
|
||||
*/
|
||||
virtual float _get_bat_v_charged() = 0;
|
||||
/**
|
||||
* @return Number of cells in series in the battery.
|
||||
*/
|
||||
virtual int _get_bat_n_cells() = 0;
|
||||
/**
|
||||
* @return Total capacity of the battery, in mAh
|
||||
*/
|
||||
virtual float _get_bat_capacity() = 0;
|
||||
/**
|
||||
* @return Voltage drop per cell at full throttle. This implicitly defines the internal resistance of the
|
||||
* battery. If _get_bat_r_internal() >= to 0, then _get_bat_v_load_drop() is ignored.
|
||||
*/
|
||||
virtual float _get_bat_v_load_drop() = 0;
|
||||
/**
|
||||
* @return Internal resistance of the battery, in Ohms. If non-negative, then this is used instead of
|
||||
* _get_bat_v_load_drop().
|
||||
*/
|
||||
virtual float _get_bat_r_internal() = 0;
|
||||
/**
|
||||
* @return Low battery threshold, as fraction between 0 and 1.
|
||||
*/
|
||||
virtual float _get_bat_low_thr() = 0;
|
||||
/**
|
||||
* @return Critical battery threshold, as fraction between 0 and 1.
|
||||
*/
|
||||
virtual float _get_bat_crit_thr() = 0;
|
||||
/**
|
||||
* @return Emergency battery threshold, as fraction between 0 and 1.
|
||||
*/
|
||||
virtual float _get_bat_emergen_thr() = 0;
|
||||
/**
|
||||
* @return Source of battery data. 0 = internal power module. 1 = external, via Mavlink
|
||||
*/
|
||||
virtual int _get_source() = 0;
|
||||
|
||||
battery_status_s _battery_status;
|
||||
|
||||
private:
|
||||
void filterVoltage(float voltage_v);
|
||||
void filterThrottle(float throttle);
|
||||
void filterCurrent(float current_a);
|
||||
void sumDischarged(hrt_abstime timestamp, float current_a);
|
||||
void estimateRemaining(float voltage_v, float current_a, float throttle, bool armed);
|
||||
void determineWarning(bool connected);
|
||||
void computeScale();
|
||||
|
||||
bool _battery_initialized = false;
|
||||
float _voltage_filtered_v = -1.f;
|
||||
float _throttle_filtered = -1.f;
|
||||
float _current_filtered_a = -1.f;
|
||||
float _discharged_mah = 0.f;
|
||||
float _discharged_mah_loop = 0.f;
|
||||
float _remaining_voltage = -1.f; ///< normalized battery charge level remaining based on voltage
|
||||
float _remaining = -1.f; ///< normalized battery charge level, selected based on config param
|
||||
float _scale = 1.f;
|
||||
uint8_t _warning;
|
||||
hrt_abstime _last_timestamp;
|
||||
|
||||
orb_advert_t _orbAdvert{nullptr};
|
||||
int _orbInstance;
|
||||
};
|
||||
@@ -1,184 +0,0 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2013-2019 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file battery_params_1.c
|
||||
* @author Timothy Scott <timothy@auterion.com>
|
||||
*
|
||||
* Defines parameters for Battery 1. For backwards compatibility, the
|
||||
* parameter names do not have a "1" in them.
|
||||
*/
|
||||
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <parameters/param.h>
|
||||
|
||||
/**
|
||||
* Empty cell voltage (5C load)
|
||||
*
|
||||
* Defines the voltage where a single cell of battery 1 is considered empty.
|
||||
* The voltage should be chosen before the steep dropoff to 2.8V. A typical
|
||||
* lithium battery can only be discharged down to 10% before it drops off
|
||||
* to a voltage level damaging the cells.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @unit V
|
||||
* @decimal 2
|
||||
* @increment 0.01
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT1_V_EMPTY, 3.5f);
|
||||
|
||||
/**
|
||||
* Full cell voltage (5C load)
|
||||
*
|
||||
* Defines the voltage where a single cell of battery 1 is considered full
|
||||
* under a mild load. This will never be the nominal voltage of 4.2V
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @unit V
|
||||
* @decimal 2
|
||||
* @increment 0.01
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT1_V_CHARGED, 4.05f);
|
||||
|
||||
/**
|
||||
* Voltage drop per cell on full throttle
|
||||
*
|
||||
* This implicitely defines the internal resistance
|
||||
* to maximum current ratio for battery 1 and assumes linearity.
|
||||
* A good value to use is the difference between the
|
||||
* 5C and 20-25C load. Not used if BAT1_R_INTERNAL is
|
||||
* set.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @unit V
|
||||
* @min 0.07
|
||||
* @max 0.5
|
||||
* @decimal 2
|
||||
* @increment 0.01
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT1_V_LOAD_DROP, 0.3f);
|
||||
|
||||
/**
|
||||
* Explicitly defines the per cell internal resistance for battery 1
|
||||
*
|
||||
* If non-negative, then this will be used in place of
|
||||
* BAT1_V_LOAD_DROP for all calculations.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @unit Ohms
|
||||
* @min -1.0
|
||||
* @max 0.2
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT1_R_INTERNAL, -1.0f);
|
||||
|
||||
|
||||
/**
|
||||
* Number of cells for battery 1.
|
||||
*
|
||||
* Defines the number of cells the attached battery consists of.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @unit S
|
||||
* @value 0 Unconfigured
|
||||
* @value 2 2S Battery
|
||||
* @value 3 3S Battery
|
||||
* @value 4 4S Battery
|
||||
* @value 5 5S Battery
|
||||
* @value 6 6S Battery
|
||||
* @value 7 7S Battery
|
||||
* @value 8 8S Battery
|
||||
* @value 9 9S Battery
|
||||
* @value 10 10S Battery
|
||||
* @value 11 11S Battery
|
||||
* @value 12 12S Battery
|
||||
* @value 13 13S Battery
|
||||
* @value 14 14S Battery
|
||||
* @value 15 15S Battery
|
||||
* @value 16 16S Battery
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_INT32(BAT1_N_CELLS, 0);
|
||||
|
||||
/**
|
||||
* Battery 1 capacity.
|
||||
*
|
||||
* Defines the capacity of battery 1.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @unit mAh
|
||||
* @decimal 0
|
||||
* @min -1.0
|
||||
* @max 100000
|
||||
* @increment 50
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT1_CAPACITY, -1.0f);
|
||||
|
||||
/**
|
||||
* Battery 1 voltage divider (V divider)
|
||||
*
|
||||
* This is the divider from battery 1 voltage to 3.3V ADC voltage.
|
||||
* If using e.g. Mauch power modules the value from the datasheet
|
||||
* can be applied straight here. A value of -1 means to use
|
||||
* the board default.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @decimal 8
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT1_V_DIV, -1.0);
|
||||
|
||||
/**
|
||||
* Battery 1 current per volt (A/V)
|
||||
*
|
||||
* The voltage seen by the 3.3V ADC multiplied by this factor
|
||||
* will determine the battery current. A value of -1 means to use
|
||||
* the board default.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @decimal 8
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT1_A_PER_V, -1.0);
|
||||
|
||||
/**
|
||||
* Battery 1 ADC Channel
|
||||
*
|
||||
* This parameter specifies the ADC channel used to monitor voltage of main power battery.
|
||||
* A value of -1 means to use the board default.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
*/
|
||||
PARAM_DEFINE_INT32(BAT1_ADC_CHANNEL, -1);
|
||||
@@ -1,180 +0,0 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2012-2019 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file battery_params_2.c
|
||||
* @author Timothy Scott <timothy@auterion.com>
|
||||
*
|
||||
* Defines parameters for Battery 2.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Empty cell voltage (5C load)
|
||||
*
|
||||
* Defines the voltage where a single cell of battery 2 is considered empty.
|
||||
* The voltage should be chosen before the steep dropoff to 2.8V. A typical
|
||||
* lithium battery can only be discharged down to 10% before it drops off
|
||||
* to a voltage level damaging the cells.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @unit V
|
||||
* @decimal 2
|
||||
* @increment 0.01
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT2_V_EMPTY, 3.5f);
|
||||
|
||||
/**
|
||||
* Full cell voltage (5C load)
|
||||
*
|
||||
* Defines the voltage where a single cell of battery 2 is considered full
|
||||
* under a mild load. This will never be the nominal voltage of 4.2V
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @unit V
|
||||
* @decimal 2
|
||||
* @increment 0.01
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT2_V_CHARGED, 4.05f);
|
||||
|
||||
/**
|
||||
* Voltage drop per cell on full throttle
|
||||
*
|
||||
* This implicitely defines the internal resistance
|
||||
* to maximum current ratio for battery 2 and assumes linearity.
|
||||
* A good value to use is the difference between the
|
||||
* 5C and 20-25C load. Not used if BAT2_R_INTERNAL is
|
||||
* set.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @unit V
|
||||
* @min 0.07
|
||||
* @max 0.5
|
||||
* @decimal 2
|
||||
* @increment 0.01
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT2_V_LOAD_DROP, 0.3f);
|
||||
|
||||
/**
|
||||
* Explicitly defines the per cell internal resistance for battery 2
|
||||
*
|
||||
* If non-negative, then this will be used in place of
|
||||
* BAT2_V_LOAD_DROP for all calculations.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @unit Ohms
|
||||
* @min -1.0
|
||||
* @max 0.2
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT2_R_INTERNAL, -1.0f);
|
||||
|
||||
|
||||
/**
|
||||
* Number of cells for battery 2.
|
||||
*
|
||||
* Defines the number of cells the attached battery consists of.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @unit S
|
||||
* @value 0 Unconfigured
|
||||
* @value 2 2S Battery
|
||||
* @value 3 3S Battery
|
||||
* @value 4 4S Battery
|
||||
* @value 5 5S Battery
|
||||
* @value 6 6S Battery
|
||||
* @value 7 7S Battery
|
||||
* @value 8 8S Battery
|
||||
* @value 9 9S Battery
|
||||
* @value 10 10S Battery
|
||||
* @value 11 11S Battery
|
||||
* @value 12 12S Battery
|
||||
* @value 13 13S Battery
|
||||
* @value 14 14S Battery
|
||||
* @value 15 15S Battery
|
||||
* @value 16 16S Battery
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_INT32(BAT2_N_CELLS, 0);
|
||||
|
||||
/**
|
||||
* Battery 2 capacity.
|
||||
*
|
||||
* Defines the capacity of battery 2.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @unit mAh
|
||||
* @decimal 0
|
||||
* @min -1.0
|
||||
* @max 100000
|
||||
* @increment 50
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT2_CAPACITY, -1.0f);
|
||||
|
||||
/**
|
||||
* Battery 2 voltage divider (V divider)
|
||||
*
|
||||
* This is the divider from battery 2 voltage to 3.3V ADC voltage.
|
||||
* If using e.g. Mauch power modules the value from the datasheet
|
||||
* can be applied straight here. A value of -1 means to use
|
||||
* the board default.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @decimal 8
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT2_V_DIV, -1.0);
|
||||
|
||||
/**
|
||||
* Battery 2 current per volt (A/V)
|
||||
*
|
||||
* The voltage seen by the 3.3V ADC multiplied by this factor
|
||||
* will determine the battery current. A value of -1 means to use
|
||||
* the board default.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @decimal 8
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT2_A_PER_V, -1.0);
|
||||
|
||||
/**
|
||||
* Battery 2 ADC Channel
|
||||
*
|
||||
* This parameter specifies the ADC channel used to monitor voltage of main power battery.
|
||||
* A value of -1 means to use the board default.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
*/
|
||||
PARAM_DEFINE_INT32(BAT2_ADC_CHANNEL, -1);
|
||||
@@ -39,10 +39,6 @@
|
||||
* @author Julian Oes <julian@oes.ch>
|
||||
*/
|
||||
|
||||
#include <px4_config.h>
|
||||
#include <parameters/param.h>
|
||||
|
||||
|
||||
/**
|
||||
* Low threshold
|
||||
*
|
||||
@@ -92,53 +88,3 @@ PARAM_DEFINE_FLOAT(BAT_CRIT_THR, 0.07f);
|
||||
* @reboot_required true
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT_EMERGEN_THR, 0.05f);
|
||||
|
||||
/**
|
||||
* Scaling from ADC counts to volt on the ADC input (battery voltage)
|
||||
*
|
||||
* This is not the battery voltage, but the intermediate ADC voltage.
|
||||
* A value of -1 signifies that the board defaults are used, which is
|
||||
* highly recommended.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @decimal 8
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT_CNT_V_VOLT, -1.0f);
|
||||
|
||||
/**
|
||||
* Scaling from ADC counts to volt on the ADC input (battery current)
|
||||
*
|
||||
* This is not the battery current, but the intermediate ADC voltage.
|
||||
* A value of -1 signifies that the board defaults are used, which is
|
||||
* highly recommended.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @decimal 8
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT_CNT_V_CURR, -1.0);
|
||||
|
||||
/**
|
||||
* Offset in volt as seen by the ADC input of the current sensor.
|
||||
*
|
||||
* This offset will be subtracted before calculating the battery
|
||||
* current based on the voltage.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @decimal 8
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT_V_OFFS_CURR, 0.0);
|
||||
|
||||
/**
|
||||
* Battery monitoring source.
|
||||
*
|
||||
* This parameter controls the source of battery data. The value 'Power Module'
|
||||
* means that measurements are expected to come from a power module. If the value is set to
|
||||
* 'External' then the system expects to receive mavlink battery status messages.
|
||||
*
|
||||
* @min 0
|
||||
* @max 1
|
||||
* @value 0 Power Module
|
||||
* @value 1 External
|
||||
* @group Battery Calibration
|
||||
*/
|
||||
PARAM_DEFINE_INT32(BAT_SOURCE, 0);
|
||||
|
||||
@@ -146,36 +146,18 @@ PARAM_DEFINE_INT32(BAT_N_CELLS, 0);
|
||||
PARAM_DEFINE_FLOAT(BAT_CAPACITY, -1.0f);
|
||||
|
||||
/**
|
||||
* This parameter is deprecated. Please use BAT1_V_DIV instead.
|
||||
* This parameter is deprecated. Please use BAT1_SOURCE instead.
|
||||
*
|
||||
* This is the divider from battery 1 voltage to 3.3V ADC voltage.
|
||||
* If using e.g. Mauch power modules the value from the datasheet
|
||||
* can be applied straight here. A value of -1 means to use
|
||||
* the board default.
|
||||
* Battery monitoring source.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @decimal 8
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT_V_DIV, -1.0);
|
||||
|
||||
/**
|
||||
* This parameter is deprecated. Please use BAT1_A_PER_V instead.
|
||||
*
|
||||
* The voltage seen by the 3.3V ADC multiplied by this factor
|
||||
* will determine the battery current. A value of -1 means to use
|
||||
* the board default.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
* @decimal 8
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT_A_PER_V, -1.0);
|
||||
|
||||
/**
|
||||
* This parameter is deprecated. Please use BAT1_ADC_CHANNEL instead.
|
||||
*
|
||||
* This parameter specifies the ADC channel used to monitor voltage of main power battery.
|
||||
* A value of -1 means to use the board default.
|
||||
* This parameter controls the source of battery data. The value 'Power Module'
|
||||
* means that measurements are expected to come from a power module. If the value is set to
|
||||
* 'External' then the system expects to receive mavlink battery status messages.
|
||||
*
|
||||
* @min 0
|
||||
* @max 1
|
||||
* @value 0 Power Module
|
||||
* @value 1 External
|
||||
* @group Battery Calibration
|
||||
*/
|
||||
PARAM_DEFINE_INT32(BAT_ADC_CHANNEL, -1);
|
||||
PARAM_DEFINE_INT32(BAT_SOURCE, 0);
|
||||
@@ -0,0 +1,140 @@
|
||||
__max_num_config_instances: &max_num_config_instances 2
|
||||
|
||||
module_name: battery
|
||||
|
||||
parameters:
|
||||
- group: Battery Calibration
|
||||
definitions:
|
||||
BAT${i}_V_EMPTY:
|
||||
description:
|
||||
short: Empty cell voltage (5C load)
|
||||
long: |
|
||||
Defines the voltage where a single cell of battery 1 is considered empty.
|
||||
The voltage should be chosen before the steep dropoff to 2.8V. A typical
|
||||
lithium battery can only be discharged down to 10% before it drops off
|
||||
to a voltage level damaging the cells.
|
||||
|
||||
type: float
|
||||
unit: V
|
||||
decimal: 2
|
||||
increment: 0.01
|
||||
reboot_required: true
|
||||
num_instances: *max_num_config_instances
|
||||
instance_start: 1
|
||||
default: [3.5, 3.5]
|
||||
|
||||
BAT${i}_V_CHARGED:
|
||||
description:
|
||||
short: Full cell voltage (5C load)
|
||||
long: |
|
||||
Defines the voltage where a single cell of battery 1 is considered full
|
||||
under a mild load. This will never be the nominal voltage of 4.2V
|
||||
|
||||
type: float
|
||||
unit: V
|
||||
decimal: 2
|
||||
increment: 0.01
|
||||
reboot_required: true
|
||||
num_instances: *max_num_config_instances
|
||||
instance_start: 1
|
||||
default: [4.05, 4.05]
|
||||
|
||||
BAT${i}_V_LOAD_DROP:
|
||||
description:
|
||||
short: Voltage drop per cell on full throttle
|
||||
long: |
|
||||
This implicitely defines the internal resistance
|
||||
to maximum current ratio for battery 1 and assumes linearity.
|
||||
A good value to use is the difference between the
|
||||
5C and 20-25C load. Not used if BAT${i}_R_INTERNAL is
|
||||
set.
|
||||
|
||||
type: float
|
||||
unit: V
|
||||
min: 0.07
|
||||
max: 0.5
|
||||
decimal: 2
|
||||
increment: 0.01
|
||||
reboot_required: true
|
||||
num_instances: *max_num_config_instances
|
||||
instance_start: 1
|
||||
default: [0.3, 0.3]
|
||||
|
||||
BAT${i}_R_INTERNAL:
|
||||
description:
|
||||
short: Explicitly defines the per cell internal resistance for battery ${i}
|
||||
long: |
|
||||
If non-negative, then this will be used in place of
|
||||
BAT${i}_V_LOAD_DROP for all calculations.
|
||||
|
||||
type: float
|
||||
unit: Ohm
|
||||
min: -1.0
|
||||
max: 0.2
|
||||
decimal: 2
|
||||
increment: 0.01
|
||||
reboot_required: true
|
||||
num_instances: *max_num_config_instances
|
||||
instance_start: 1
|
||||
default: [-1.0, -1.0]
|
||||
|
||||
BAT${i}_N_CELLS:
|
||||
description:
|
||||
short: Number of cells for battery ${i}.
|
||||
long: |
|
||||
Defines the number of cells the attached battery consists of.
|
||||
|
||||
type: enum
|
||||
values:
|
||||
2: 2S Battery
|
||||
3: 3S Battery
|
||||
4: 4S Battery
|
||||
5: 5S Battery
|
||||
6: 6S Battery
|
||||
7: 7S Battery
|
||||
8: 8S Battery
|
||||
9: 9S Battery
|
||||
10: 10S Battery
|
||||
11: 11S Battery
|
||||
12: 12S Battery
|
||||
13: 13S Battery
|
||||
14: 14S Battery
|
||||
15: 15S Battery
|
||||
16: 16S Battery
|
||||
reboot_required: true
|
||||
num_instances: *max_num_config_instances
|
||||
instance_start: 1
|
||||
default: [0, 0]
|
||||
|
||||
BAT${i}_CAPACITY:
|
||||
description:
|
||||
short: Battery ${i} capacity.
|
||||
long: |
|
||||
Defines the capacity of battery ${i} in mAh.
|
||||
type: float
|
||||
unit: mAh
|
||||
min: -1.0
|
||||
max: 100000
|
||||
decimal: 0
|
||||
increment: 50
|
||||
reboot_required: true
|
||||
num_instances: *max_num_config_instances
|
||||
instance_start: 1
|
||||
default: [-1.0, -1.0]
|
||||
|
||||
BAT${i}_SOURCE:
|
||||
description:
|
||||
short: Battery ${i} monitoring source.
|
||||
long: |
|
||||
This parameter controls the source of battery data. The value 'Power Module'
|
||||
means that measurements are expected to come from a power module. If the value is set to
|
||||
'External' then the system expects to receive mavlink battery status messages.
|
||||
type: enum
|
||||
unit: mAh
|
||||
values:
|
||||
0: Power Module
|
||||
1: External
|
||||
reboot_required: true
|
||||
num_instances: *max_num_config_instances
|
||||
instance_start: 1
|
||||
default: [0, 0]
|
||||
Reference in New Issue
Block a user