Refactored battery library for multiple instances

This commit is contained in:
Timothy Scott
2019-07-24 10:58:06 +02:00
committed by Julian Oes
parent d7b95870b9
commit bff1df7080
18 changed files with 1039 additions and 277 deletions
+1
View File
@@ -43,6 +43,7 @@ px4_add_module(
sensors.cpp
parameters.cpp
temperature_compensation.cpp
power.cpp
DEPENDS
airspeed
+91
View File
@@ -0,0 +1,91 @@
/****************************************************************************
*
* 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]->vChannel == 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) {
/* 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(bat_voltage_cnt[b], bat_current_cnt[b], hrt_absolute_time(),
selected_source == b, b, throttle, armed);
}
#endif /* BOARD_NUMBER_BRICKS > 0 */
}
+112
View File
@@ -0,0 +1,112 @@
/****************************************************************************
*
* 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 <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
/**
* 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:
* ```
* Battery1 _battery0;
* Battery2 _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.
*/
// TODO: Add digital batteries
#if BOARD_NUMBER_BRICKS > 0
Battery1 _battery0;
#if BOARD_NUMBER_BRICKS > 1
Battery2 _battery1;
#endif
BatteryBase *_analogBatteries[BOARD_NUMBER_BRICKS] {
&_battery0,
#if BOARD_NUMBER_BRICKS > 1
&_battery1,
#endif
}; // End _analogBatteries
#endif // End #if BOARD_NUMBER_BRICKS > 0
};
@@ -1,57 +0,0 @@
/****************************************************************************
*
* Copyright (c) 2012-2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* Battery voltage divider (V divider)
*
* This is the divider from battery voltage to 3.3V ADC voltage.
* If using e.g. Mauch power modules the value from the datasheet
* can be applied straight here. A value of -1 means to use
* the board default.
*
* @group Battery Calibration
* @decimal 8
*/
PARAM_DEFINE_FLOAT(BAT_V_DIV, -1.0);
/**
* Battery current per volt (A/V)
*
* The voltage seen by the 3.3V ADC multiplied by this factor
* will determine the battery current. A value of -1 means to use
* the board default.
*
* @group Battery Calibration
* @decimal 8
*/
PARAM_DEFINE_FLOAT(BAT_A_PER_V, -1.0);
-1
View File
@@ -142,7 +142,6 @@ private:
VotedSensorsUpdate _voted_sensors_update;
VehicleAcceleration _vehicle_acceleration;
VehicleAngularVelocity _vehicle_angular_velocity;
+1 -1
View File
@@ -239,7 +239,7 @@ private:
hrt_abstime _last_sitl_timestamp{0};
// Lib used to do the battery calculations.
Battery _battery {};
Battery1 _battery {};
battery_status_s _battery_status{};
#ifndef __PX4_QURT
+1 -4
View File
@@ -370,10 +370,7 @@ 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(now_us, vbatt, ibatt, true, true, 0, throttle, armed, &_battery_status);
// publish the battery voltage
_battery_pub.publish(_battery_status);
_battery.updateBatteryStatus(vbatt, ibatt, now_us, true, 0, throttle, armed);
}
}