mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-08 16:40:35 +08:00
Refactored to work with new battery_status module
This commit is contained in:
committed by
Julian Oes
parent
d7bb5d46bb
commit
993fa5bd37
@@ -36,7 +36,10 @@ px4_add_module(
|
||||
MAIN battery_status
|
||||
SRCS
|
||||
battery_status.cpp
|
||||
parameters.cpp
|
||||
analog_battery.cpp
|
||||
|
||||
MODULE_CONFIG
|
||||
module.yaml
|
||||
|
||||
DEPENDS
|
||||
battery
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
#include <lib/battery/battery.h>
|
||||
#include "analog_battery.h"
|
||||
|
||||
// Defaults to use if the parameters are not set
|
||||
#if BOARD_NUMBER_BRICKS > 0
|
||||
#if defined(BOARD_BATT_V_LIST) && defined(BOARD_BATT_I_LIST)
|
||||
static constexpr int DEFAULT_V_CHANNEL[BOARD_NUMBER_BRICKS] = BOARD_BATT_V_LIST;
|
||||
static constexpr int DEFAULT_I_CHANNEL[BOARD_NUMBER_BRICKS] = BOARD_BATT_I_LIST;
|
||||
#else
|
||||
static constexpr int DEFAULT_V_CHANNEL[BOARD_NUMBER_BRICKS] = {0};
|
||||
static constexpr int DEFAULT_I_CHANNEL[BOARD_NUMBER_BRICKS] = {0};
|
||||
#endif
|
||||
#else
|
||||
static constexpr int DEFAULT_V_CHANNEL[1] = {0};
|
||||
static constexpr int DEFAULT_I_CHANNEL[1] = {0};
|
||||
#endif
|
||||
|
||||
static constexpr float DEFAULT_VOLTS_PER_COUNT = 3.3f / 4096.0f;
|
||||
|
||||
AnalogBattery::AnalogBattery(int index, ModuleParams *parent) :
|
||||
Battery(index, parent)
|
||||
{
|
||||
char param_name[17];
|
||||
|
||||
_analog_param_handles.cnt_v_volt = param_find("BAT_CNT_V_VOLT");
|
||||
|
||||
_analog_param_handles.cnt_v_curr = param_find("BAT_CNT_V_CURR");
|
||||
|
||||
_analog_param_handles.v_offs_cur = param_find("BAT_V_OFFS_CURR");
|
||||
|
||||
snprintf(param_name, sizeof(param_name), "BAT%d_V_DIV", index);
|
||||
_analog_param_handles.v_div = param_find(param_name);
|
||||
|
||||
snprintf(param_name, sizeof(param_name), "BAT%d_A_PER_V", index);
|
||||
_analog_param_handles.a_per_v = param_find(param_name);
|
||||
|
||||
snprintf(param_name, sizeof(param_name), "BAT%d_ADC_CHANNEL", index);
|
||||
_analog_param_handles.adc_channel = param_find(param_name);
|
||||
|
||||
_analog_param_handles.v_div_old = param_find("BAT_V_DIV");
|
||||
_analog_param_handles.a_per_v_old = param_find("BAT_A_PER_V");
|
||||
_analog_param_handles.adc_channel_old = param_find("BAT_ADC_CHANNEL");
|
||||
}
|
||||
|
||||
void
|
||||
AnalogBattery::updateBatteryStatusRawADC(hrt_abstime timestamp, int32_t voltage_raw, int32_t current_raw,
|
||||
bool selected_source, int priority, float throttle_normalized,
|
||||
bool armed)
|
||||
{
|
||||
float voltage_v = (voltage_raw * _analog_params.cnt_v_volt) * _analog_params.v_div;
|
||||
float current_a = ((current_raw * _analog_params.cnt_v_curr) - _analog_params.v_offs_cur) * _analog_params.a_per_v;
|
||||
|
||||
bool connected = voltage_v > BOARD_ADC_OPEN_CIRCUIT_V &&
|
||||
(BOARD_ADC_OPEN_CIRCUIT_V <= BOARD_VALID_UV || is_valid());
|
||||
|
||||
|
||||
Battery::updateBatteryStatus(timestamp, voltage_v, current_a, connected,
|
||||
selected_source, priority, throttle_normalized, armed, _params.source == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the ADC channel for the voltage of this battery is valid.
|
||||
* Corresponds to BOARD_BRICK_VALID_LIST
|
||||
*/
|
||||
bool AnalogBattery::is_valid()
|
||||
{
|
||||
#ifdef BOARD_BRICK_VALID_LIST
|
||||
bool valid[BOARD_NUMBER_BRICKS] = BOARD_BRICK_VALID_LIST;
|
||||
return valid[_index - 1];
|
||||
#else
|
||||
// TODO: Maybe return false instead?
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
int AnalogBattery::get_voltage_channel()
|
||||
{
|
||||
if (_analog_params.adc_channel >= 0) {
|
||||
return _analog_params.adc_channel;
|
||||
|
||||
} else {
|
||||
return DEFAULT_V_CHANNEL[_index - 1];
|
||||
}
|
||||
}
|
||||
|
||||
int AnalogBattery::get_current_channel()
|
||||
{
|
||||
// TODO: Possibly implement parameter for current sense channel
|
||||
return DEFAULT_I_CHANNEL[_index - 1];
|
||||
}
|
||||
|
||||
void
|
||||
AnalogBattery::updateParams()
|
||||
{
|
||||
if (_index == 1) {
|
||||
migrateParam<float>(_analog_param_handles.v_div_old, _analog_param_handles.v_div, &_analog_params.v_div_old,
|
||||
&_analog_params.v_div, _first_parameter_update);
|
||||
migrateParam<float>(_analog_param_handles.a_per_v_old, _analog_param_handles.a_per_v, &_analog_params.a_per_v_old,
|
||||
&_analog_params.a_per_v, _first_parameter_update);
|
||||
migrateParam<int>(_analog_param_handles.adc_channel_old, _analog_param_handles.adc_channel,
|
||||
&_analog_params.adc_channel_old, &_analog_params.adc_channel, _first_parameter_update);
|
||||
|
||||
} else {
|
||||
param_get(_analog_param_handles.v_div, &_analog_params.v_div);
|
||||
param_get(_analog_param_handles.a_per_v, &_analog_params.a_per_v);
|
||||
param_get(_analog_param_handles.adc_channel, &_analog_params.adc_channel);
|
||||
}
|
||||
|
||||
param_get(_analog_param_handles.cnt_v_volt, &_analog_params.cnt_v_volt);
|
||||
param_get(_analog_param_handles.cnt_v_curr, &_analog_params.cnt_v_curr);
|
||||
param_get(_analog_param_handles.v_offs_cur, &_analog_params.v_offs_cur);
|
||||
|
||||
/* scaling of ADC ticks to battery voltage */
|
||||
if (_analog_params.cnt_v_volt < 0.0f) {
|
||||
/* apply scaling according to defaults if set to default */
|
||||
_analog_params.cnt_v_volt = (BOARD_ADC_POS_REF_V_FOR_VOLTAGE_CHAN / px4_arch_adc_dn_fullcount());
|
||||
param_set_no_notification(_analog_param_handles.cnt_v_volt, &_analog_params.cnt_v_volt);
|
||||
}
|
||||
|
||||
/* scaling of ADC ticks to battery current */
|
||||
if (_analog_params.cnt_v_curr < 0.0f) {
|
||||
/* apply scaling according to defaults if set to default */
|
||||
_analog_params.cnt_v_curr = (BOARD_ADC_POS_REF_V_FOR_CURRENT_CHAN / px4_arch_adc_dn_fullcount());
|
||||
param_set_no_notification(_analog_param_handles.cnt_v_curr, &_analog_params.cnt_v_curr);
|
||||
}
|
||||
|
||||
if (_analog_params.v_div <= 0.0f) {
|
||||
/* apply scaling according to defaults if set to default */
|
||||
_analog_params.v_div = BOARD_BATTERY1_V_DIV;
|
||||
param_set_no_notification(_analog_param_handles.v_div, &_analog_params.v_div);
|
||||
|
||||
if (_index == 1) {
|
||||
_analog_params.v_div_old = BOARD_BATTERY1_V_DIV;
|
||||
param_set_no_notification(_analog_param_handles.v_div_old, &_analog_params.v_div_old);
|
||||
}
|
||||
}
|
||||
|
||||
if (_analog_params.a_per_v <= 0.0f) {
|
||||
/* apply scaling according to defaults if set to default */
|
||||
|
||||
_analog_params.a_per_v = BOARD_BATTERY1_A_PER_V;
|
||||
param_set_no_notification(_analog_param_handles.a_per_v, &_analog_params.a_per_v);
|
||||
|
||||
if (_index == 1) {
|
||||
_analog_params.a_per_v_old = BOARD_BATTERY1_A_PER_V;
|
||||
param_set_no_notification(_analog_param_handles.a_per_v_old, &_analog_params.a_per_v_old);
|
||||
}
|
||||
}
|
||||
|
||||
Battery::updateParams();
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <battery/battery.h>
|
||||
#include <parameters/param.h>
|
||||
|
||||
class AnalogBattery : public Battery
|
||||
{
|
||||
public:
|
||||
AnalogBattery(int index = 1, ModuleParams *parent = nullptr);
|
||||
|
||||
/**
|
||||
* Update current battery status message.
|
||||
*
|
||||
* @param voltage_raw Battery voltage read from ADC, in raw ADC counts
|
||||
* @param current_raw Voltage of current sense resistor, in raw ADC counts
|
||||
* @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
|
||||
*/
|
||||
void updateBatteryStatusRawADC(hrt_abstime timestamp, int32_t voltage_raw, int32_t current_raw,
|
||||
bool selected_source, int priority, float throttle_normalized,
|
||||
bool armed);
|
||||
|
||||
/**
|
||||
* Whether the ADC channel for the voltage of this battery is valid.
|
||||
* Corresponds to BOARD_BRICK_VALID_LIST
|
||||
*/
|
||||
bool is_valid();
|
||||
|
||||
/**
|
||||
* Which ADC channel is used for voltage reading of this battery
|
||||
*/
|
||||
int get_voltage_channel();
|
||||
|
||||
/**
|
||||
* Which ADC channel is used for current reading of this battery
|
||||
*/
|
||||
int get_current_channel();
|
||||
|
||||
protected:
|
||||
|
||||
struct {
|
||||
param_t cnt_v_volt;
|
||||
param_t cnt_v_curr;
|
||||
param_t v_offs_cur;
|
||||
param_t v_div;
|
||||
param_t a_per_v;
|
||||
param_t adc_channel;
|
||||
|
||||
param_t v_div_old;
|
||||
param_t a_per_v_old;
|
||||
param_t adc_channel_old;
|
||||
} _analog_param_handles;
|
||||
|
||||
struct {
|
||||
float cnt_v_volt;
|
||||
float cnt_v_curr;
|
||||
float v_offs_cur;
|
||||
float v_div;
|
||||
float a_per_v;
|
||||
int adc_channel;
|
||||
|
||||
float v_div_old;
|
||||
float a_per_v_old;
|
||||
int adc_channel_old;
|
||||
} _analog_params;
|
||||
|
||||
virtual void updateParams() override;
|
||||
};
|
||||
-25
@@ -65,28 +65,3 @@ PARAM_DEFINE_FLOAT(BAT_CNT_V_CURR, -1.0);
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* Battery 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(BAT_ADC_CHANNEL, -1);
|
||||
+16
-45
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2016 PX4 Development Team. All rights reserved.
|
||||
* 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
|
||||
@@ -31,54 +31,25 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @file parameters.h
|
||||
* This parameter is deprecated. Please use BAT1_V_DIV.
|
||||
*
|
||||
* defines the list of parameters that are used within the sensors module
|
||||
* @group Battery Calibration
|
||||
* @decimal 8
|
||||
*/
|
||||
PARAM_DEFINE_FLOAT(BAT_V_DIV, -1.0);
|
||||
|
||||
/**
|
||||
* This parameter is deprecated. Please use BAT1_A_PER_V.
|
||||
*
|
||||
* @author Beat Kueng <beat-kueng@gmx.net>
|
||||
* @group Battery Calibration
|
||||
* @decimal 8
|
||||
*/
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <drivers/drv_rc_input.h>
|
||||
|
||||
#include <parameters/param.h>
|
||||
#include <mathlib/mathlib.h>
|
||||
|
||||
namespace battery_status
|
||||
{
|
||||
|
||||
struct Parameters {
|
||||
float battery_voltage_scaling;
|
||||
float battery_current_scaling;
|
||||
float battery_current_offset;
|
||||
float battery_v_div;
|
||||
float battery_a_per_v;
|
||||
int32_t battery_source;
|
||||
int32_t battery_adc_channel;
|
||||
};
|
||||
|
||||
struct ParameterHandles {
|
||||
param_t battery_voltage_scaling;
|
||||
param_t battery_current_scaling;
|
||||
param_t battery_current_offset;
|
||||
param_t battery_v_div;
|
||||
param_t battery_a_per_v;
|
||||
param_t battery_source;
|
||||
param_t battery_adc_channel;
|
||||
};
|
||||
PARAM_DEFINE_FLOAT(BAT_A_PER_V, -1.0);
|
||||
|
||||
/**
|
||||
* initialize ParameterHandles struct
|
||||
* This parameter is deprecated. Please use BAT1_ADC_CHANNEL.
|
||||
*
|
||||
* @group Battery Calibration
|
||||
*/
|
||||
void initialize_parameter_handles(ParameterHandles ¶meter_handles);
|
||||
|
||||
|
||||
/**
|
||||
* Read out the parameters using the handles into the parameters struct.
|
||||
* @return 0 on success, <0 on error
|
||||
*/
|
||||
int update_parameters(const ParameterHandles ¶meter_handles, Parameters ¶meters);
|
||||
|
||||
} /* namespace sensors */
|
||||
PARAM_DEFINE_INT32(BAT_ADC_CHANNEL, -1);
|
||||
@@ -66,10 +66,9 @@
|
||||
|
||||
#include <DevMgr.hpp>
|
||||
|
||||
#include "parameters.h"
|
||||
#include "analog_battery.h"
|
||||
|
||||
using namespace DriverFramework;
|
||||
using namespace battery_status;
|
||||
using namespace time_literals;
|
||||
|
||||
/**
|
||||
@@ -137,8 +136,18 @@ private:
|
||||
uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)}; /**< notification of parameter updates */
|
||||
uORB::Subscription _vcontrol_mode_sub{ORB_ID(vehicle_control_mode)}; /**< vehicle control mode subscription */
|
||||
|
||||
orb_advert_t _battery_pub[BOARD_NUMBER_BRICKS] {}; /**< battery status */
|
||||
Battery _battery[BOARD_NUMBER_BRICKS]; /**< Helper lib to publish battery_status topic. */
|
||||
AnalogBattery _battery1;
|
||||
|
||||
#if BOARD_NUMBER_BRICKS > 1
|
||||
AnalogBattery _battery2;
|
||||
#endif
|
||||
|
||||
AnalogBattery *_analogBatteries[BOARD_NUMBER_BRICKS] {
|
||||
&_battery1,
|
||||
#if BOARD_NUMBER_BRICKS > 1
|
||||
&_battery2,
|
||||
#endif
|
||||
}; // End _analogBatteries
|
||||
|
||||
#if BOARD_NUMBER_BRICKS > 1
|
||||
int _battery_pub_intance0ndx {0}; /**< track the index of instance 0 */
|
||||
@@ -146,16 +155,6 @@ private:
|
||||
|
||||
perf_counter_t _loop_perf; /**< loop performance counter */
|
||||
|
||||
hrt_abstime _last_config_update{0};
|
||||
|
||||
Parameters _parameters{}; /**< local copies of interesting parameters */
|
||||
ParameterHandles _parameter_handles{}; /**< handles for interesting parameters */
|
||||
|
||||
/**
|
||||
* Update our local parameter cache.
|
||||
*/
|
||||
int parameters_update();
|
||||
|
||||
/**
|
||||
* Do adc-related initialisation.
|
||||
*/
|
||||
@@ -178,13 +177,13 @@ private:
|
||||
BatteryStatus::BatteryStatus() :
|
||||
ModuleParams(nullptr),
|
||||
ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::hp_default),
|
||||
_battery1(1, this),
|
||||
#if BOARD_NUMBER_BRICKS > 1
|
||||
_battery2(2, this),
|
||||
#endif
|
||||
_loop_perf(perf_alloc(PC_ELAPSED, MODULE_NAME))
|
||||
{
|
||||
initialize_parameter_handles(_parameter_handles);
|
||||
|
||||
for (int b = 0; b < BOARD_NUMBER_BRICKS; b++) {
|
||||
_battery[b].setParent(this);
|
||||
}
|
||||
updateParams();
|
||||
}
|
||||
|
||||
BatteryStatus::~BatteryStatus()
|
||||
@@ -192,19 +191,6 @@ BatteryStatus::~BatteryStatus()
|
||||
ScheduleClear();
|
||||
}
|
||||
|
||||
int
|
||||
BatteryStatus::parameters_update()
|
||||
{
|
||||
/* read the parameter values into _parameters */
|
||||
int ret = update_parameters(_parameter_handles, _parameters);
|
||||
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
BatteryStatus::adc_init()
|
||||
{
|
||||
@@ -228,7 +214,6 @@ BatteryStatus::parameter_update_poll(bool forced)
|
||||
_parameter_update_sub.copy(&pupdate);
|
||||
|
||||
// update parameters from storage
|
||||
parameters_update();
|
||||
updateParams();
|
||||
}
|
||||
}
|
||||
@@ -242,30 +227,15 @@ BatteryStatus::adc_poll()
|
||||
/* read all channels available */
|
||||
int ret = _h_adc.read(&buf_adc, sizeof(buf_adc));
|
||||
|
||||
//todo:abosorb into new class Power
|
||||
|
||||
/* For legacy support we publish the battery_status for the Battery that is
|
||||
* associated with the Brick that is the selected source for VDD_5V_IN
|
||||
* Selection is done in HW ala a LTC4417 or similar, or may be hard coded
|
||||
* Like in the FMUv4
|
||||
*/
|
||||
|
||||
/* The ADC channels that are associated with each brick, in power controller
|
||||
* priority order highest to lowest, as defined by the board config.
|
||||
*/
|
||||
int bat_voltage_v_chan[BOARD_NUMBER_BRICKS] = BOARD_BATT_V_LIST;
|
||||
int bat_voltage_i_chan[BOARD_NUMBER_BRICKS] = BOARD_BATT_I_LIST;
|
||||
|
||||
if (_parameters.battery_adc_channel >= 0) { // overwrite default
|
||||
bat_voltage_v_chan[0] = _parameters.battery_adc_channel;
|
||||
}
|
||||
|
||||
/* The valid signals (HW dependent) are associated with each brick */
|
||||
bool valid_chan[BOARD_NUMBER_BRICKS] = BOARD_BRICK_VALID_LIST;
|
||||
|
||||
/* Per Brick readings with default unread channels at 0 */
|
||||
float bat_current_a[BOARD_NUMBER_BRICKS] = {0.0f};
|
||||
float bat_voltage_v[BOARD_NUMBER_BRICKS] = {0.0f};
|
||||
int32_t bat_current_adc_readings[BOARD_NUMBER_BRICKS] {};
|
||||
int32_t bat_voltage_adc_readings[BOARD_NUMBER_BRICKS] {};
|
||||
|
||||
/* Based on the valid_chan, used to indicate the selected the lowest index
|
||||
* (highest priority) supply that is the source for the VDD_5V_IN
|
||||
@@ -284,7 +254,7 @@ BatteryStatus::adc_poll()
|
||||
/* Once we have subscriptions, Do this once for the lowest (highest priority
|
||||
* supply on power controller) that is valid.
|
||||
*/
|
||||
if (_battery_pub[b] != nullptr && selected_source < 0 && valid_chan[b]) {
|
||||
if (selected_source < 0 && _analogBatteries[b]->is_valid()) {
|
||||
/* Indicate the lowest brick (highest priority supply on power controller)
|
||||
* that is valid as the one that is the selected source for the
|
||||
* VDD_5V_IN
|
||||
@@ -294,54 +264,41 @@ BatteryStatus::adc_poll()
|
||||
|
||||
/* Move the selected_source to instance 0 */
|
||||
if (_battery_pub_intance0ndx != selected_source) {
|
||||
|
||||
orb_advert_t tmp_h = _battery_pub[_battery_pub_intance0ndx];
|
||||
_battery_pub[_battery_pub_intance0ndx] = _battery_pub[selected_source];
|
||||
_battery_pub[selected_source] = tmp_h;
|
||||
_analogBatteries[_battery_pub_intance0ndx]->swapUorbAdvert(
|
||||
*_analogBatteries[selected_source]
|
||||
);
|
||||
_battery_pub_intance0ndx = selected_source;
|
||||
}
|
||||
|
||||
# endif /* BOARD_NUMBER_BRICKS > 1 */
|
||||
}
|
||||
|
||||
// todo:per brick scaling
|
||||
/* look for specific channels and process the raw voltage to measurement data */
|
||||
if (bat_voltage_v_chan[b] == buf_adc[i].am_channel) {
|
||||
if (_analogBatteries[b]->get_voltage_channel() == buf_adc[i].am_channel) {
|
||||
/* Voltage in volts */
|
||||
bat_voltage_v[b] = (buf_adc[i].am_data * _parameters.battery_voltage_scaling) * _parameters.battery_v_div;
|
||||
bat_voltage_adc_readings[b] = buf_adc[i].am_data;
|
||||
|
||||
} else if (bat_voltage_i_chan[b] == buf_adc[i].am_channel) {
|
||||
bat_current_a[b] = ((buf_adc[i].am_data * _parameters.battery_current_scaling)
|
||||
- _parameters.battery_current_offset) * _parameters.battery_a_per_v;
|
||||
} else if (_analogBatteries[b]->get_current_channel() == buf_adc[i].am_channel) {
|
||||
bat_current_adc_readings[b] = buf_adc[i].am_data;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (_parameters.battery_source == 0) {
|
||||
for (int b = 0; b < BOARD_NUMBER_BRICKS; b++) {
|
||||
|
||||
/* Consider the brick connected if there is a voltage */
|
||||
bool connected = bat_voltage_v[b] > BOARD_ADC_OPEN_CIRCUIT_V;
|
||||
|
||||
/* In the case where the BOARD_ADC_OPEN_CIRCUIT_V is
|
||||
* greater than the BOARD_VALID_UV let the HW qualify that it
|
||||
* is connected.
|
||||
*/
|
||||
if (BOARD_ADC_OPEN_CIRCUIT_V > BOARD_VALID_UV) {
|
||||
connected &= valid_chan[b];
|
||||
}
|
||||
|
||||
for (int b = 0; b < BOARD_NUMBER_BRICKS; b++) {
|
||||
if (_analogBatteries[b]->source() == 0) {
|
||||
actuator_controls_s ctrl{};
|
||||
_actuator_ctrl_0_sub.copy(&ctrl);
|
||||
|
||||
battery_status_s battery_status{};
|
||||
_battery[b].updateBatteryStatus(hrt_absolute_time(), bat_voltage_v[b], bat_current_a[b],
|
||||
connected, selected_source == b, b,
|
||||
ctrl.control[actuator_controls_s::INDEX_THROTTLE],
|
||||
_armed, &battery_status);
|
||||
int instance;
|
||||
orb_publish_auto(ORB_ID(battery_status), &_battery_pub[b], &battery_status, &instance, ORB_PRIO_DEFAULT);
|
||||
_analogBatteries[b]->updateBatteryStatusRawADC(
|
||||
hrt_absolute_time(),
|
||||
bat_voltage_adc_readings[b],
|
||||
bat_current_adc_readings[b],
|
||||
selected_source == b,
|
||||
b,
|
||||
ctrl.control[actuator_controls_s::INDEX_THROTTLE],
|
||||
_armed
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
__max_num_config_instances: &max_num_config_instances 2
|
||||
|
||||
module_name: battery_status
|
||||
|
||||
parameters:
|
||||
- group: Battery Calibration
|
||||
definitions:
|
||||
BAT${i}_V_DIV:
|
||||
description:
|
||||
short: Battery ${i} voltage divider (V divider)
|
||||
long: |
|
||||
This is the divider from battery ${i} 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.
|
||||
|
||||
type: float
|
||||
decimal: 8
|
||||
reboot_required: true
|
||||
num_instances: *max_num_config_instances
|
||||
instance_start: 1
|
||||
default: [-1.0, -1.0]
|
||||
|
||||
BAT${i}_A_PER_V:
|
||||
description:
|
||||
short: Battery ${i} current per volt (A/V)
|
||||
long: |
|
||||
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.
|
||||
|
||||
type: float
|
||||
decimal: 8
|
||||
reboot_required: true
|
||||
num_instances: *max_num_config_instances
|
||||
instance_start: 1
|
||||
default: [-1.0, -1.0]
|
||||
|
||||
BAT${i}_ADC_CHANNEL:
|
||||
description:
|
||||
short: Battery ${i} ADC Channel
|
||||
long: |
|
||||
This parameter specifies the ADC channel used to monitor voltage of main power battery.
|
||||
A value of -1 means to use the board default.
|
||||
|
||||
type: int32
|
||||
reboot_required: true
|
||||
num_instances: *max_num_config_instances
|
||||
instance_start: 1
|
||||
default: [-1, -1]
|
||||
@@ -1,119 +0,0 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2016-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 parameters.cpp
|
||||
*
|
||||
* @author Beat Kueng <beat-kueng@gmx.net>
|
||||
*/
|
||||
|
||||
#include "parameters.h"
|
||||
|
||||
#include <px4_platform_common/log.h>
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <drivers/drv_adc.h>
|
||||
|
||||
namespace battery_status
|
||||
{
|
||||
|
||||
void initialize_parameter_handles(ParameterHandles ¶meter_handles)
|
||||
{
|
||||
parameter_handles.battery_voltage_scaling = param_find("BAT_CNT_V_VOLT");
|
||||
parameter_handles.battery_current_scaling = param_find("BAT_CNT_V_CURR");
|
||||
parameter_handles.battery_current_offset = param_find("BAT_V_OFFS_CURR");
|
||||
parameter_handles.battery_v_div = param_find("BAT_V_DIV");
|
||||
parameter_handles.battery_a_per_v = param_find("BAT_A_PER_V");
|
||||
parameter_handles.battery_source = param_find("BAT_SOURCE");
|
||||
parameter_handles.battery_adc_channel = param_find("BAT_ADC_CHANNEL");
|
||||
}
|
||||
|
||||
int update_parameters(const ParameterHandles ¶meter_handles, Parameters ¶meters)
|
||||
{
|
||||
int ret = PX4_OK;
|
||||
|
||||
const char *paramerr = "FAIL PARM LOAD";
|
||||
|
||||
/* scaling of ADC ticks to battery voltage */
|
||||
if (param_get(parameter_handles.battery_voltage_scaling, &(parameters.battery_voltage_scaling)) != OK) {
|
||||
PX4_WARN("%s", paramerr);
|
||||
|
||||
} else if (parameters.battery_voltage_scaling < 0.0f) {
|
||||
/* apply scaling according to defaults if set to default */
|
||||
parameters.battery_voltage_scaling = (BOARD_ADC_POS_REF_V_FOR_VOLTAGE_CHAN / px4_arch_adc_dn_fullcount());
|
||||
param_set_no_notification(parameter_handles.battery_voltage_scaling, ¶meters.battery_voltage_scaling);
|
||||
}
|
||||
|
||||
/* scaling of ADC ticks to battery current */
|
||||
if (param_get(parameter_handles.battery_current_scaling, &(parameters.battery_current_scaling)) != OK) {
|
||||
PX4_WARN("%s", paramerr);
|
||||
|
||||
} else if (parameters.battery_current_scaling < 0.0f) {
|
||||
/* apply scaling according to defaults if set to default */
|
||||
parameters.battery_current_scaling = (BOARD_ADC_POS_REF_V_FOR_CURRENT_CHAN / px4_arch_adc_dn_fullcount());
|
||||
param_set_no_notification(parameter_handles.battery_current_scaling, ¶meters.battery_current_scaling);
|
||||
}
|
||||
|
||||
if (param_get(parameter_handles.battery_current_offset, &(parameters.battery_current_offset)) != OK) {
|
||||
PX4_WARN("%s", paramerr);
|
||||
|
||||
}
|
||||
|
||||
if (param_get(parameter_handles.battery_v_div, &(parameters.battery_v_div)) != OK) {
|
||||
PX4_WARN("%s", paramerr);
|
||||
parameters.battery_v_div = 0.0f;
|
||||
|
||||
} else if (parameters.battery_v_div <= 0.0f) {
|
||||
/* apply scaling according to defaults if set to default */
|
||||
|
||||
parameters.battery_v_div = BOARD_BATTERY1_V_DIV;
|
||||
param_set_no_notification(parameter_handles.battery_v_div, ¶meters.battery_v_div);
|
||||
}
|
||||
|
||||
if (param_get(parameter_handles.battery_a_per_v, &(parameters.battery_a_per_v)) != OK) {
|
||||
PX4_WARN("%s", paramerr);
|
||||
parameters.battery_a_per_v = 0.0f;
|
||||
|
||||
} else if (parameters.battery_a_per_v <= 0.0f) {
|
||||
/* apply scaling according to defaults if set to default */
|
||||
|
||||
parameters.battery_a_per_v = BOARD_BATTERY1_A_PER_V;
|
||||
param_set_no_notification(parameter_handles.battery_a_per_v, ¶meters.battery_a_per_v);
|
||||
}
|
||||
|
||||
param_get(parameter_handles.battery_source, &(parameters.battery_source));
|
||||
param_get(parameter_handles.battery_adc_channel, &(parameters.battery_adc_channel));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
} /* namespace battery_status */
|
||||
@@ -693,7 +693,8 @@ protected:
|
||||
if (_battery_status_sub[i]->update(&_battery_status_timestamp[i], &battery_status)) {
|
||||
/* battery status message with higher resolution */
|
||||
mavlink_battery_status_t bat_msg{};
|
||||
bat_msg.id = i;
|
||||
// TODO: Determine how to better map between battery ID within the firmware and in MAVLink
|
||||
bat_msg.id = battery_status.id - 1;
|
||||
bat_msg.battery_function = MAV_BATTERY_FUNCTION_ALL;
|
||||
bat_msg.type = MAV_BATTERY_TYPE_LIPO;
|
||||
bat_msg.current_consumed = (battery_status.connected) ? battery_status.discharged_mah : -1;
|
||||
|
||||
@@ -43,8 +43,6 @@ px4_add_module(
|
||||
sensors.cpp
|
||||
parameters.cpp
|
||||
temperature_compensation.cpp
|
||||
power.cpp
|
||||
analog_battery.cpp
|
||||
|
||||
DEPENDS
|
||||
airspeed
|
||||
|
||||
@@ -1,141 +0,0 @@
|
||||
#include "analog_battery.h"
|
||||
|
||||
// Defaults to use if the parameters are not set
|
||||
#if BOARD_NUMBER_BRICKS > 0
|
||||
#if defined(BOARD_BATT_V_LIST) && defined(BOARD_BATT_I_LIST)
|
||||
static constexpr int DEFAULT_V_CHANNEL[BOARD_NUMBER_BRICKS] = BOARD_BATT_V_LIST;
|
||||
static constexpr int DEFAULT_I_CHANNEL[BOARD_NUMBER_BRICKS] = BOARD_BATT_I_LIST;
|
||||
#else
|
||||
static constexpr int DEFAULT_V_CHANNEL[BOARD_NUMBER_BRICKS] = {0};
|
||||
static constexpr int DEFAULT_I_CHANNEL[BOARD_NUMBER_BRICKS] = {0};
|
||||
#endif
|
||||
#else
|
||||
static constexpr int DEFAULT_V_CHANNEL[1] = {0};
|
||||
static constexpr int DEFAULT_I_CHANNEL[1] = {0};
|
||||
#endif
|
||||
|
||||
static constexpr float DEFAULT_VOLTS_PER_COUNT = 3.3f / 4096.0f;
|
||||
|
||||
AnalogBattery::AnalogBattery() :
|
||||
ModuleParams(nullptr)
|
||||
{}
|
||||
|
||||
void
|
||||
AnalogBattery::updateBatteryStatusRawADC(hrt_abstime timestamp, int32_t voltage_raw, int32_t current_raw,
|
||||
bool selected_source, int priority, float throttle_normalized,
|
||||
bool armed)
|
||||
{
|
||||
// TODO: Check that there was actually a parameter update
|
||||
_get_battery_base().updateParams();
|
||||
updateParams();
|
||||
|
||||
float voltage_v = (voltage_raw * _get_cnt_v_volt()) * _get_v_div();
|
||||
float current_a = ((current_raw * _get_cnt_v_curr()) - _get_v_offs_cur()) * _get_a_per_v();
|
||||
|
||||
bool connected = voltage_v > BOARD_ADC_OPEN_CIRCUIT_V &&
|
||||
(BOARD_ADC_OPEN_CIRCUIT_V <= BOARD_VALID_UV || is_valid());
|
||||
|
||||
_get_battery_base().updateBatteryStatus(timestamp, voltage_v, current_a, connected,
|
||||
selected_source, priority, throttle_normalized, armed, false);
|
||||
|
||||
// Before refactoring and adding the BatteryBase and AnalogBattery classes, the only place that checked the
|
||||
// value of the BAT_SOURCE parameter was in the ADC polling in sensors.cpp. So I keep that logic here for now.
|
||||
if (_get_source() == 0) {
|
||||
_get_battery_base().publish();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the ADC channel for the voltage of this battery is valid.
|
||||
* Corresponds to BOARD_BRICK_VALID_LIST
|
||||
*/
|
||||
bool AnalogBattery::is_valid()
|
||||
{
|
||||
#ifdef BOARD_BRICK_VALID_LIST
|
||||
bool valid[BOARD_NUMBER_BRICKS] = BOARD_BRICK_VALID_LIST;
|
||||
return valid[_get_brick_index()];
|
||||
#else
|
||||
// TODO: Maybe return false instead?
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
int AnalogBattery::getVoltageChannel()
|
||||
{
|
||||
if (_get_adc_channel() >= 0) {
|
||||
return _get_adc_channel();
|
||||
|
||||
} else {
|
||||
return DEFAULT_V_CHANNEL[_get_brick_index()];
|
||||
}
|
||||
}
|
||||
|
||||
int AnalogBattery::getCurrentChannel()
|
||||
{
|
||||
// TODO: Possibly implement parameter for current sense channel
|
||||
return DEFAULT_I_CHANNEL[_get_brick_index()];
|
||||
}
|
||||
|
||||
float
|
||||
AnalogBattery::_get_cnt_v_volt()
|
||||
{
|
||||
float val = _get_cnt_v_volt_raw();
|
||||
|
||||
|
||||
if (val < 0.0f) {
|
||||
// TODO: This magic constant was hardcoded into sensors.cpp before I did the refactor. I don't know
|
||||
// what the best way is to make it not a magic number.
|
||||
return DEFAULT_VOLTS_PER_COUNT;
|
||||
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
float
|
||||
AnalogBattery::_get_cnt_v_curr()
|
||||
{
|
||||
float val = _get_cnt_v_curr_raw();
|
||||
|
||||
if (val < 0.0f) {
|
||||
return DEFAULT_VOLTS_PER_COUNT;
|
||||
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
float
|
||||
AnalogBattery::_get_v_div()
|
||||
{
|
||||
float val = _get_v_div_raw();
|
||||
|
||||
if (val <= 0.0f) {
|
||||
return BOARD_BATTERY1_V_DIV;
|
||||
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
float
|
||||
AnalogBattery::_get_a_per_v()
|
||||
{
|
||||
float val = _get_a_per_v_raw();
|
||||
|
||||
if (val <= 0.0f) {
|
||||
return BOARD_BATTERY1_A_PER_V;
|
||||
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
#if BOARD_NUMBER_BRICKS > 0
|
||||
AnalogBattery1::AnalogBattery1()
|
||||
{
|
||||
Battery1::migrateParam(_param_old_a_per_v, _param_a_per_v, "A_PER_V", -1.0f);
|
||||
Battery1::migrateParam(_param_old_adc_channel, _param_adc_channel, "ADC_CHANNEL", -1);
|
||||
Battery1::migrateParam(_param_old_v_div, _param_v_div, "V_DIV", -1.0f);
|
||||
}
|
||||
#endif
|
||||
@@ -1,163 +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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <battery/battery_base.h>
|
||||
#include <battery/battery.h>
|
||||
|
||||
class AnalogBattery : public ModuleParams
|
||||
{
|
||||
public:
|
||||
AnalogBattery();
|
||||
|
||||
/**
|
||||
* Update current battery status message.
|
||||
*
|
||||
* @param voltage_raw Battery voltage read from ADC, in raw ADC counts
|
||||
* @param current_raw Voltage of current sense resistor, in raw ADC counts
|
||||
* @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
|
||||
*/
|
||||
void updateBatteryStatusRawADC(hrt_abstime timestamp, int32_t voltage_raw, int32_t current_raw,
|
||||
bool selected_source, int priority, float throttle_normalized,
|
||||
bool armed);
|
||||
|
||||
/**
|
||||
* Whether the ADC channel for the voltage of this battery is valid.
|
||||
* Corresponds to BOARD_BRICK_VALID_LIST
|
||||
*/
|
||||
bool is_valid();
|
||||
|
||||
/**
|
||||
* Which ADC channel is used for voltage reading of this battery
|
||||
*/
|
||||
int getVoltageChannel();
|
||||
|
||||
/**
|
||||
* Which ADC channel is used for current reading of this battery
|
||||
*/
|
||||
int getCurrentChannel();
|
||||
|
||||
protected:
|
||||
virtual float _get_cnt_v_volt_raw() = 0;
|
||||
virtual float _get_cnt_v_curr_raw() = 0;
|
||||
virtual float _get_v_offs_cur() = 0;
|
||||
virtual float _get_v_div_raw() = 0;
|
||||
virtual float _get_a_per_v_raw() = 0;
|
||||
virtual int _get_adc_channel() = 0;
|
||||
virtual int _get_source() = 0;
|
||||
|
||||
virtual int _get_brick_index() = 0;
|
||||
|
||||
virtual BatteryBase &_get_battery_base() = 0;
|
||||
|
||||
float _get_cnt_v_volt();
|
||||
float _get_cnt_v_curr();
|
||||
float _get_v_div();
|
||||
float _get_a_per_v();
|
||||
};
|
||||
|
||||
#if BOARD_NUMBER_BRICKS > 0
|
||||
class AnalogBattery1 : public AnalogBattery
|
||||
{
|
||||
public:
|
||||
AnalogBattery1();
|
||||
protected:
|
||||
|
||||
Battery1 _base{};
|
||||
|
||||
DEFINE_PARAMETERS(
|
||||
(ParamFloat<px4::params::BAT_V_DIV>) _param_old_v_div,
|
||||
(ParamFloat<px4::params::BAT_A_PER_V>) _param_old_a_per_v,
|
||||
(ParamInt<px4::params::BAT_ADC_CHANNEL>) _param_old_adc_channel,
|
||||
|
||||
(ParamFloat<px4::params::BAT1_V_DIV>) _param_v_div,
|
||||
(ParamFloat<px4::params::BAT1_A_PER_V>) _param_a_per_v,
|
||||
(ParamInt<px4::params::BAT1_ADC_CHANNEL>) _param_adc_channel,
|
||||
|
||||
(ParamFloat<px4::params::BAT_CNT_V_VOLT>) _param_cnt_v_volt,
|
||||
(ParamFloat<px4::params::BAT_CNT_V_CURR>) _param_cnt_v_curr,
|
||||
(ParamFloat<px4::params::BAT_V_OFFS_CURR>) _param_v_offs_cur,
|
||||
(ParamInt<px4::params::BAT_SOURCE>) _param_source
|
||||
)
|
||||
|
||||
float _get_v_div_raw() override {return _param_v_div.get(); }
|
||||
float _get_a_per_v_raw() override {return _param_a_per_v.get(); }
|
||||
int _get_adc_channel() override {return _param_adc_channel.get(); }
|
||||
float _get_cnt_v_volt_raw() override {return _param_cnt_v_volt.get(); }
|
||||
float _get_cnt_v_curr_raw() override {return _param_cnt_v_curr.get(); }
|
||||
float _get_v_offs_cur() override {return _param_v_offs_cur.get(); }
|
||||
int _get_source() override {return _param_source.get();}
|
||||
|
||||
int _get_brick_index() override {return 0; }
|
||||
|
||||
BatteryBase &_get_battery_base() override {return _base;}
|
||||
};
|
||||
#endif // #if BOARD_NUMBER_BRICKS > 0
|
||||
|
||||
#if BOARD_NUMBER_BRICKS > 1
|
||||
class AnalogBattery2 : public AnalogBattery
|
||||
{
|
||||
protected:
|
||||
|
||||
Battery2 _base{};
|
||||
|
||||
DEFINE_PARAMETERS(
|
||||
(ParamFloat<px4::params::BAT2_V_DIV>) _param_v_div,
|
||||
(ParamFloat<px4::params::BAT2_A_PER_V>) _param_a_per_v,
|
||||
(ParamInt<px4::params::BAT2_ADC_CHANNEL>) _param_adc_channel,
|
||||
|
||||
(ParamFloat<px4::params::BAT_CNT_V_VOLT>) _param_cnt_v_volt,
|
||||
(ParamFloat<px4::params::BAT_CNT_V_CURR>) _param_cnt_v_curr,
|
||||
(ParamFloat<px4::params::BAT_V_OFFS_CURR>) _param_v_offs_cur,
|
||||
(ParamInt<px4::params::BAT_SOURCE>) _param_source
|
||||
)
|
||||
|
||||
|
||||
float _get_v_div_raw() override {return _param_v_div.get(); }
|
||||
float _get_a_per_v_raw() override {return _param_a_per_v.get(); }
|
||||
int _get_adc_channel() override {return _param_adc_channel.get(); }
|
||||
float _get_cnt_v_volt_raw() override {return _param_cnt_v_volt.get(); }
|
||||
float _get_cnt_v_curr_raw() override {return _param_cnt_v_curr.get(); }
|
||||
float _get_v_offs_cur() override {return _param_v_offs_cur.get(); }
|
||||
int _get_source() override {return _param_source.get();}
|
||||
|
||||
int _get_brick_index() override {return 1; }
|
||||
|
||||
BatteryBase &_get_battery_base() override {return _base;}
|
||||
};
|
||||
#endif // #if BOARD_NUMBER_BRICKS > 1
|
||||
@@ -1,91 +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 power.cpp
|
||||
*
|
||||
* @author Timothy Scott <timothy@auterion.com>
|
||||
*/
|
||||
#include "power.h"
|
||||
|
||||
Power::Power() {}
|
||||
|
||||
void Power::update(px4_adc_msg_t buf_adc[PX4_MAX_ADC_CHANNELS], int nchannels, float throttle, bool armed)
|
||||
{
|
||||
|
||||
#if BOARD_NUMBER_BRICKS > 0
|
||||
/* For legacy support we publish the battery_status for the Battery that is
|
||||
* associated with the Brick that is the selected source for VDD_5V_IN
|
||||
* Selection is done in HW ala a LTC4417 or similar, or may be hard coded
|
||||
* Like in the FMUv4
|
||||
*/
|
||||
|
||||
/* Per Brick readings with default unread channels at 0 */
|
||||
int32_t bat_current_cnt[BOARD_NUMBER_BRICKS] = {0};
|
||||
int32_t bat_voltage_cnt[BOARD_NUMBER_BRICKS] = {0};
|
||||
|
||||
// The channel readings are not necessarily in a nice order, so we iterate through
|
||||
// to find every relevant channel.
|
||||
for (int i = 0; i < nchannels; i++) {
|
||||
for (int b = 0; b < BOARD_NUMBER_BRICKS; b++) {
|
||||
/* look for specific channels and process the raw voltage to measurement data */
|
||||
if (_analogBatteries[b]->getVoltageChannel() == buf_adc[i].am_channel) {
|
||||
/* Voltage in ADC counts */
|
||||
bat_voltage_cnt[b] = buf_adc[i].am_data;
|
||||
|
||||
} else if (_analogBatteries[b]->getCurrentChannel() == buf_adc[i].am_channel) {
|
||||
/* Voltage at current sense resistor in ADC counts */
|
||||
bat_current_cnt[b] = buf_adc[i].am_data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Based on the valid_chan, used to indicate the selected the lowest index
|
||||
* (highest priority) supply that is the source for the VDD_5V_IN
|
||||
* When < 0 none selected
|
||||
*/
|
||||
|
||||
int selected_source = -1;
|
||||
|
||||
for (int b = 0; b < BOARD_NUMBER_BRICKS; b++) {
|
||||
if (_analogBatteries[b]->is_valid() && selected_source < 0) {
|
||||
selected_source = b;
|
||||
}
|
||||
|
||||
_analogBatteries[b]->updateBatteryStatusRawADC(hrt_absolute_time(), bat_voltage_cnt[b], bat_current_cnt[b],
|
||||
selected_source == b, b, throttle, armed);
|
||||
}
|
||||
|
||||
#endif /* BOARD_NUMBER_BRICKS > 0 */
|
||||
|
||||
}
|
||||
@@ -1,103 +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 power.h
|
||||
*
|
||||
* @author Timothy Scott <timothy@auterion.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <board_config.h>
|
||||
#include "analog_battery.h"
|
||||
|
||||
/**
|
||||
* Measures voltage, current, etc. of all batteries connected to the vehicle, both
|
||||
* analog and digital.
|
||||
*/
|
||||
class Power
|
||||
{
|
||||
public:
|
||||
Power();
|
||||
|
||||
/**
|
||||
* Updates the measurements of each battery.
|
||||
*
|
||||
* If the parameter `BAT_SOURCE` == 0, this function will also publish an instance of the uORB topic
|
||||
* `battery_status` for each battery. For reasons of backwards compability, instance 0 will always be the
|
||||
* primary battery. However, this may change in the future! In the future, please use orb_priority() to find
|
||||
* the primary battery.
|
||||
* @param buf_adc Buffer of ADC readings
|
||||
* @param nchannels Number of valid ADC readings in `buf_adc`
|
||||
* @param throttle Normalized throttle (between 0 and 1, or maybe between -1 and 1 in the future)
|
||||
* @param armed True if the vehicle is armed
|
||||
*/
|
||||
void update(px4_adc_msg_t buf_adc[PX4_MAX_ADC_CHANNELS], int nchannels, float throttle, bool armed);
|
||||
|
||||
private:
|
||||
|
||||
/*
|
||||
* All of these #if's are doing one thing: Building an array of `BatteryBase` objects, one
|
||||
* for each possible connected battery. A `BatteryBase` object does not mean that a battery IS connected,
|
||||
* it just means that one CAN be connected.
|
||||
*
|
||||
* For an example of what this looks like after preprocessing, assume that BOARD_NUMBER_BRICKS = 2:
|
||||
* ```
|
||||
* AnalogBattery1 _battery0;
|
||||
* AnalogBattery2 _battery1;
|
||||
*
|
||||
* BatteryBase *_analogBatteries[2] {
|
||||
* &_battery0,
|
||||
* &_battery1,
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* The #if BOARD_NUMBER_BRICKS > 0 wraps the entire declaration because otherwise, an empty array is declared
|
||||
* which then is unused. In some configurations, an unused variable throws a compile error.
|
||||
*/
|
||||
#if BOARD_NUMBER_BRICKS > 0
|
||||
AnalogBattery1 _battery0;
|
||||
|
||||
#if BOARD_NUMBER_BRICKS > 1
|
||||
AnalogBattery2 _battery1;
|
||||
#endif
|
||||
|
||||
AnalogBattery *_analogBatteries[BOARD_NUMBER_BRICKS] {
|
||||
&_battery0,
|
||||
#if BOARD_NUMBER_BRICKS > 1
|
||||
&_battery1,
|
||||
#endif
|
||||
}; // End _analogBatteries
|
||||
#endif // End #if BOARD_NUMBER_BRICKS > 0
|
||||
};
|
||||
@@ -72,7 +72,6 @@
|
||||
|
||||
#include "parameters.h"
|
||||
#include "voted_sensors_update.h"
|
||||
#include "power.h"
|
||||
#include "vehicle_acceleration/VehicleAcceleration.hpp"
|
||||
#include "vehicle_angular_velocity/VehicleAngularVelocity.hpp"
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <battery/battery_base.h>
|
||||
#include <battery/battery.h>
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <drivers/drv_rc_input.h>
|
||||
#include <lib/drivers/accelerometer/PX4Accelerometer.hpp>
|
||||
@@ -73,6 +73,7 @@
|
||||
|
||||
#include <v2.0/common/mavlink.h>
|
||||
#include <v2.0/mavlink_types.h>
|
||||
#include <lib/battery/battery.h>
|
||||
|
||||
namespace simulator
|
||||
{
|
||||
@@ -237,22 +238,22 @@ private:
|
||||
hrt_abstime _last_sitl_timestamp{0};
|
||||
hrt_abstime _last_battery_timestamp{0};
|
||||
|
||||
// Because the simulator doesn't actually care about the real values of these, just stick
|
||||
// in some good defaults.
|
||||
// This is an anonymous class, because BatteryBase is abstract and can't directly be instantiated.
|
||||
class : public BatteryBase
|
||||
class : public Battery
|
||||
{
|
||||
float _get_bat_v_empty() override { return 3.5; }
|
||||
float _get_bat_v_charged() override { return 4.05; }
|
||||
int _get_bat_n_cells() override { return 4; }
|
||||
float _get_bat_capacity() override { return 10.0; }
|
||||
float _get_bat_v_load_drop() override { return 0; }
|
||||
float _get_bat_r_internal() override { return 0; }
|
||||
float _get_bat_low_thr() override { return 0.15; }
|
||||
float _get_bat_crit_thr() override { return 0.07; }
|
||||
float _get_bat_emergen_thr() override { return 0.05; }
|
||||
int _get_source() override { return 0; }
|
||||
} _battery {};
|
||||
virtual void updateParams() override
|
||||
{
|
||||
_params.v_empty = 3.5f;
|
||||
_params.v_charged = 4.05f;
|
||||
_params.n_cells = 4;
|
||||
_params.capacity = 10.0f;
|
||||
_params.v_load_drop = 0.0f;
|
||||
_params.r_internal = 0.0f;
|
||||
_params.low_thr = 0.15f;
|
||||
_params.crit_thr = 0.07f;
|
||||
_params.emergen_thr = 0.05f;
|
||||
_params.source = 0;
|
||||
}
|
||||
} _battery;
|
||||
|
||||
#ifndef __PX4_QURT
|
||||
|
||||
|
||||
Reference in New Issue
Block a user