PX4 Firmware
PX4 Autopilot Software http://px4.io
battery.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2016, 2017 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file battery.h
36  *
37  * Library calls for battery functionality.
38  *
39  * @author Julian Oes <julian@oes.ch>
40  */
41 
42 #pragma once
43 
45 #include <drivers/drv_hrt.h>
46 #include <px4_platform_common/module_params.h>
47 
48 
49 class Battery : public ModuleParams
50 {
51 public:
52  Battery();
53 
54  /**
55  * Reset all battery stats and report invalid/nothing.
56  */
58 
59  /**
60  * Get the battery cell count
61  */
62  int cell_count() { return _param_bat_n_cells.get(); }
63 
64  /**
65  * Get the empty voltage per cell
66  */
67  float empty_cell_voltage() { return _param_bat_v_empty.get(); }
68 
69  /**
70  * Get the full voltage per cell
71  */
72  float full_cell_voltage() { return _param_bat_v_charged.get(); }
73 
74  /**
75  * Update current battery status message.
76  *
77  * @param voltage_v: current voltage in V
78  * @param current_a: current current in A
79  * @param connected: Battery is connected
80  * @param selected_source: This battery is on the brick that the selected source for selected_source
81  * @param priority: The brick number -1. The term priority refers to the Vn connection on the LTC4417
82  * @param throttle_normalized: throttle from 0 to 1
83  */
84  void updateBatteryStatus(hrt_abstime timestamp, float voltage_v, float current_a,
85  bool connected, bool selected_source, int priority,
86  float throttle_normalized,
88 
89 private:
90  void filterVoltage(float voltage_v);
91  void filterThrottle(float throttle);
92  void filterCurrent(float current_a);
93  void sumDischarged(hrt_abstime timestamp, float current_a);
94  void estimateRemaining(float voltage_v, float current_a, float throttle, bool armed);
95  void determineWarning(bool connected);
96  void computeScale();
97 
99  (ParamFloat<px4::params::BAT_V_EMPTY>) _param_bat_v_empty,
100  (ParamFloat<px4::params::BAT_V_CHARGED>) _param_bat_v_charged,
101  (ParamInt<px4::params::BAT_N_CELLS>) _param_bat_n_cells,
102  (ParamFloat<px4::params::BAT_CAPACITY>) _param_bat_capacity,
103  (ParamFloat<px4::params::BAT_V_LOAD_DROP>) _param_bat_v_load_drop,
104  (ParamFloat<px4::params::BAT_R_INTERNAL>) _param_bat_r_internal,
105  (ParamFloat<px4::params::BAT_LOW_THR>) _param_bat_low_thr,
106  (ParamFloat<px4::params::BAT_CRIT_THR>) _param_bat_crit_thr,
107  (ParamFloat<px4::params::BAT_EMERGEN_THR>) _param_bat_emergen_thr
108  )
109 
110  bool _battery_initialized = false;
111  float _voltage_filtered_v = -1.f;
112  float _throttle_filtered = -1.f;
113  float _current_filtered_a = -1.f;
114  float _discharged_mah = 0.f;
115  float _discharged_mah_loop = 0.f;
116  float _remaining_voltage = -1.f; ///< normalized battery charge level remaining based on voltage
117  float _remaining = -1.f; ///< normalized battery charge level, selected based on config param
118  float _scale = 1.f;
119  uint8_t _warning;
121 };
float _discharged_mah
Definition: battery.h:114
static struct vehicle_status_s status
Definition: Commander.cpp:138
float _scale
Definition: battery.h:118
float _current_filtered_a
Definition: battery.h:113
int cell_count()
Get the battery cell count.
Definition: battery.h:62
void filterThrottle(float throttle)
Definition: battery.cpp:134
float _throttle_filtered
Definition: battery.h:112
DEFINE_PARAMETERS((ParamFloat< px4::params::BAT_V_EMPTY >) _param_bat_v_empty,(ParamFloat< px4::params::BAT_V_CHARGED >) _param_bat_v_charged,(ParamInt< px4::params::BAT_N_CELLS >) _param_bat_n_cells,(ParamFloat< px4::params::BAT_CAPACITY >) _param_bat_capacity,(ParamFloat< px4::params::BAT_V_LOAD_DROP >) _param_bat_v_load_drop,(ParamFloat< px4::params::BAT_R_INTERNAL >) _param_bat_r_internal,(ParamFloat< px4::params::BAT_LOW_THR >) _param_bat_low_thr,(ParamFloat< px4::params::BAT_CRIT_THR >) _param_bat_crit_thr,(ParamFloat< px4::params::BAT_EMERGEN_THR >) _param_bat_emergen_thr) bool _battery_initialized
void reset(battery_status_s *battery_status)
Reset all battery stats and report invalid/nothing.
Definition: battery.cpp:55
hrt_abstime _last_timestamp
Definition: battery.h:120
float _discharged_mah_loop
Definition: battery.h:115
void sumDischarged(hrt_abstime timestamp, float current_a)
Definition: battery.cpp:148
High-resolution timer with callouts and timekeeping.
float _remaining_voltage
normalized battery charge level remaining based on voltage
Definition: battery.h:116
float full_cell_voltage()
Get the full voltage per cell.
Definition: battery.h:72
void filterVoltage(float voltage_v)
Definition: battery.cpp:105
void estimateRemaining(float voltage_v, float current_a, float throttle, bool armed)
Definition: battery.cpp:170
float empty_cell_voltage()
Get the empty voltage per cell.
Definition: battery.h:67
static struct actuator_armed_s armed
Definition: Commander.cpp:139
void computeScale()
Definition: battery.cpp:226
void determineWarning(bool connected)
Definition: battery.cpp:209
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
void updateBatteryStatus(hrt_abstime timestamp, float voltage_v, float current_a, bool connected, bool selected_source, int priority, float throttle_normalized, bool armed, battery_status_s *status)
Update current battery status message.
Definition: battery.cpp:68
float _remaining
normalized battery charge level, selected based on config param
Definition: battery.h:117
float _voltage_filtered_v
Definition: battery.h:111
uint8_t _warning
Definition: battery.h:119
void filterCurrent(float current_a)
Definition: battery.cpp:120
Battery()
Definition: battery.cpp:47