Refactored to work with new battery_status module

This commit is contained in:
Timothy Scott
2019-12-05 16:38:04 +01:00
committed by Julian Oes
parent d7bb5d46bb
commit 993fa5bd37
35 changed files with 1101 additions and 1837 deletions
+4 -1
View File
@@ -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();
}
+104
View File
@@ -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;
};
@@ -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);
@@ -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 &parameter_handles);
/**
* Read out the parameters using the handles into the parameters struct.
* @return 0 on success, <0 on error
*/
int update_parameters(const ParameterHandles &parameter_handles, Parameters &parameters);
} /* namespace sensors */
PARAM_DEFINE_INT32(BAT_ADC_CHANNEL, -1);
+39 -82
View File
@@ -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
);
}
}
}
+50
View File
@@ -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]
-119
View File
@@ -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 &parameter_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 &parameter_handles, Parameters &parameters)
{
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, &parameters.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, &parameters.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, &parameters.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, &parameters.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 */