Firmware
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 
42 #pragma once
43 
44 #include <uORB/topics/battery_status.h>
45 #include <drivers/drv_hrt.h>
46 #include <px4_module_params.h>
47 
48 
49 class Battery : public ModuleParams
50 {
51 public:
52  Battery();
53 
57  void reset(battery_status_s *battery_status);
58 
62  int cell_count() { return _param_bat_n_cells.get(); }
63 
67  float empty_cell_voltage() { return _param_bat_v_empty.get(); }
68 
72  float full_cell_voltage() { return _param_bat_v_charged.get(); }
73 
84  void updateBatteryStatus(hrt_abstime timestamp, float voltage_v, float current_a,
85  bool connected, bool selected_source, int priority,
86  float throttle_normalized,
87  bool armed, battery_status_s *status);
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 
98  DEFINE_PARAMETERS(
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;
117  float _remaining = -1.f;
118  float _scale = 1.f;
119  uint8_t _warning;
120  hrt_abstime _last_timestamp;
121 };
Definition: px4_param.h:313
float full_cell_voltage()
Get the full voltage per cell.
Definition: battery.h:72
High-resolution timer with callouts and timekeeping.
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
Definition: px4_param.h:318
C++ base class for modules/classes using configuration parameters.
Definition: px4_module_params.h:46
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
void reset(battery_status_s *battery_status)
Reset all battery stats and report invalid/nothing.
Definition: battery.cpp:55
int cell_count()
Get the battery cell count.
Definition: battery.h:62
float empty_cell_voltage()
Get the empty voltage per cell.
Definition: battery.h:67
Definition: battery.h:49