mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-14 02:30:34 +08:00
Created new AnalogBattery class
This commit is contained in:
committed by
Julian Oes
parent
bff1df7080
commit
d7bb5d46bb
@@ -44,6 +44,7 @@ px4_add_module(
|
||||
parameters.cpp
|
||||
temperature_compensation.cpp
|
||||
power.cpp
|
||||
analog_battery.cpp
|
||||
|
||||
DEPENDS
|
||||
airspeed
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
#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
|
||||
@@ -0,0 +1,163 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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
|
||||
@@ -59,11 +59,11 @@ void Power::update(px4_adc_msg_t buf_adc[PX4_MAX_ADC_CHANNELS], int nchannels, f
|
||||
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]->vChannel == buf_adc[i].am_channel) {
|
||||
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]->iChannel == buf_adc[i].am_channel) {
|
||||
} 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;
|
||||
}
|
||||
@@ -82,7 +82,7 @@ void Power::update(px4_adc_msg_t buf_adc[PX4_MAX_ADC_CHANNELS], int nchannels, f
|
||||
selected_source = b;
|
||||
}
|
||||
|
||||
_analogBatteries[b]->updateBatteryStatusRawADC(bat_voltage_cnt[b], bat_current_cnt[b], hrt_absolute_time(),
|
||||
_analogBatteries[b]->updateBatteryStatusRawADC(hrt_absolute_time(), bat_voltage_cnt[b], bat_current_cnt[b],
|
||||
selected_source == b, b, throttle, armed);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,13 +40,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <board_config.h>
|
||||
#include <battery/battery.h>
|
||||
|
||||
#ifdef BOARD_NUMBER_DIGITAL_BRICKS
|
||||
#define TOTAL_BRICKS (BOARD_NUMBER_BRICKS + BOARD_NUMBER_DIGITAL_BRICKS)
|
||||
#else
|
||||
#define TOTAL_BRICKS BOARD_NUMBER_BRICKS
|
||||
#endif
|
||||
#include "analog_battery.h"
|
||||
|
||||
/**
|
||||
* Measures voltage, current, etc. of all batteries connected to the vehicle, both
|
||||
@@ -80,8 +74,8 @@ private:
|
||||
*
|
||||
* For an example of what this looks like after preprocessing, assume that BOARD_NUMBER_BRICKS = 2:
|
||||
* ```
|
||||
* Battery1 _battery0;
|
||||
* Battery2 _battery1;
|
||||
* AnalogBattery1 _battery0;
|
||||
* AnalogBattery2 _battery1;
|
||||
*
|
||||
* BatteryBase *_analogBatteries[2] {
|
||||
* &_battery0,
|
||||
@@ -92,17 +86,14 @@ private:
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// TODO: Add digital batteries
|
||||
|
||||
#if BOARD_NUMBER_BRICKS > 0
|
||||
Battery1 _battery0;
|
||||
AnalogBattery1 _battery0;
|
||||
|
||||
#if BOARD_NUMBER_BRICKS > 1
|
||||
Battery2 _battery1;
|
||||
AnalogBattery2 _battery1;
|
||||
#endif
|
||||
|
||||
BatteryBase *_analogBatteries[BOARD_NUMBER_BRICKS] {
|
||||
AnalogBattery *_analogBatteries[BOARD_NUMBER_BRICKS] {
|
||||
&_battery0,
|
||||
#if BOARD_NUMBER_BRICKS > 1
|
||||
&_battery1,
|
||||
|
||||
@@ -72,7 +72,7 @@
|
||||
|
||||
#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.h>
|
||||
#include <battery/battery_base.h>
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <drivers/drv_rc_input.h>
|
||||
#include <lib/drivers/accelerometer/PX4Accelerometer.hpp>
|
||||
@@ -183,8 +183,6 @@ private:
|
||||
|
||||
_gps.writeData(&gps_data);
|
||||
|
||||
_battery_status.timestamp = hrt_absolute_time();
|
||||
|
||||
_px4_accel.set_sample_rate(250);
|
||||
_px4_gyro.set_sample_rate(250);
|
||||
}
|
||||
@@ -237,10 +235,24 @@ private:
|
||||
|
||||
hrt_abstime _last_sim_timestamp{0};
|
||||
hrt_abstime _last_sitl_timestamp{0};
|
||||
hrt_abstime _last_battery_timestamp{0};
|
||||
|
||||
// Lib used to do the battery calculations.
|
||||
Battery1 _battery {};
|
||||
battery_status_s _battery_status{};
|
||||
// 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
|
||||
{
|
||||
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 {};
|
||||
|
||||
#ifndef __PX4_QURT
|
||||
|
||||
|
||||
@@ -346,7 +346,7 @@ void Simulator::handle_message_hil_sensor(const mavlink_message_t *msg)
|
||||
static uint64_t last_integration_us = 0;
|
||||
|
||||
// battery simulation (limit update to 100Hz)
|
||||
if (hrt_elapsed_time(&_battery_status.timestamp) >= 10_ms) {
|
||||
if (hrt_elapsed_time(&_last_battery_timestamp) >= 10_ms) {
|
||||
|
||||
const float discharge_interval_us = _param_sim_bat_drain.get() * 1000 * 1000;
|
||||
|
||||
@@ -370,7 +370,9 @@ void Simulator::handle_message_hil_sensor(const mavlink_message_t *msg)
|
||||
vbatt *= _battery.cell_count();
|
||||
|
||||
const float throttle = 0.0f; // simulate no throttle compensation to make the estimate predictable
|
||||
_battery.updateBatteryStatus(vbatt, ibatt, now_us, true, 0, throttle, armed);
|
||||
_battery.updateBatteryStatus(now_us, vbatt, ibatt, true, true, 0, throttle, armed, true);
|
||||
|
||||
_last_battery_timestamp = now_us;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user