Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
battery_monitor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2015 The Paparazzi Team
3  * 2017, Utah State University, http://aggieair.usu.edu/
4  * Michal Podhradsky, michal.podhradsky@aggiemail.usu.edu
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, write to
20  * the Free Software Foundation, 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
70 #ifndef BATTERY_MONITOR_H
71 #define BATTERY_MONITOR_H
72 
73 #include "std.h"
74 #include "mcu_periph/i2c.h"
75 #include "mcu_periph/sys_time.h"
76 #include "subsystems/electrical.h"
77 
78 // revision information
79 #if BATTERY_MONITOR_REV4
80 /* revision 4 */
81 #define BATTERY_MONITOR_BIT_RES 10 // define bit resolution: 12 for AD7998 (rev4)
82 #else
83 /* revision 5 */
84 #define BATTERY_MONITOR_BIT_RES 12 // define bit resolution: 10 for AD7997 (rev5)
85 #endif /* BATTERY_MONITOR_REV4 */
86 
87 // default reference (5 Volts)
88 #define BATTERY_MONITOR_VREF 5000 //mV
89 
90 // reading multiplier
91 #define BATTERY_MONITOR_VREF_MULT BATTERY_MONITOR_VREF/(1<<BATTERY_MONITOR_BIT_RES)
92 
93 // we are using 6 cells by default
94 #define BATTERY_CELLS_NB 6
95 
96 // we are using 6 temp sensors right now
97 #define TEMP_SENSORS_NB 6
98 
99 /*
100  * cell ADC gains
101  * representing inverse values of the voltage dividers
102  */
103 #if BATTERY_MONITOR_REV4
104 /* revision 4 */
105 #define GAIN_CELL_1 1.001
106 #define GAIN_CELL_2 2.011
107 #define GAIN_CELL_3 3.027
108 #define GAIN_CELL_4 4.063
109 #define GAIN_CELL_5 4.217
110 #define GAIN_CELL_6 4.973
111 #else
112 /* revision 5 */
113 #define GAIN_CELL_1 1.0
114 #define GAIN_CELL_2 2.0
115 #define GAIN_CELL_3 3.0
116 #define GAIN_CELL_4 4.03
117 #define GAIN_CELL_5 5.0
118 #define GAIN_CELL_6 6.0
119 #endif /* BATTERY_MONITOR_REV4 */
122 
129 static const uint8_t battery_monitor_cellmap[] = {2,4,6,8,5,7}; // 6s cellmap (battery1)
130 // for 4 cell battery the last two readings should be zero (channel tied down to GND)
131 // or HIGH (channel tied to VCC)
132 
133 // define channels
134 #define BATTERY_MONITOR_BUS_CURRENT_CHANNEL 2
135 #define BATTERY_MONITOR_BUS_VOLTAGE_CHANNEL 4
136 
137 /*
138  * Channels for the temp sensors
139  */
140 static const uint8_t battery_monitor_tempmap[] = {1,3,5,6,7,8};
141 
142 /*
143  * Define bus voltage gain based on
144  * values of voltage divider at
145  * power board
146  */
147 #if BATTERY_MONITOR_REV4
148 /* revision 4 */
149 static const float BatmonVbusGain = 4.953980661;
150 #else
151 /* revision 5 */
152 static const float BatmonVbusGain = 6.0;
153 #endif /* BATTERY_MONITOR_REV4 */
154 
155 /*
156  * Define current sensor calibration
157  * include default values
158  * See datasheet for your current sensor
159  * to get the typical values
160  */
161 #ifndef BATTERY_MONITOR_CURRENT_OFFSET //mV
162 #ifdef BATTERY_MONITOR_REV4
163 /* revision 4 */
164 #define BATTERY_MONITOR_CURRENT_OFFSET -120
165 #else
166 /* revision 5 */
167 #define BATTERY_MONITOR_CURRENT_OFFSET -657
168 #endif /* BATTERY_MONITOR_REV4 */
169 #endif /* BATTERY_MONITOR_CURRENT_OFFSET */
170 
171 #ifndef BATTERY_MONITOR_CURRENT_SENSITIVITY //mV/A
172 #define BATTERY_MONITOR_CURRENT_SENSITIVITY 25.6
173 #endif
174 
175 /*
176  * Calibration for the temperature sensors
177  * (for now just one for all of them)
178  * values for TMP35
179  */
180 #ifndef BATTERY_MONITOR_TEMP_OFFSET //mV
181 #define BATTERY_MONITOR_TEMP_OFFSET 250
182 #endif
183 #ifndef BATTERY_MONITOR_TEMP_SENSITIVITY //mV/C
184 #define BATTERY_MONITOR_TEMP_SENSITIVITY 10
185 #endif
186 
187 // can be tuned via datalink
189 extern float batmon_current_sensitivity;
191 extern float batmon_temp_sensitivity;
192 
193 
204 };
205 
210 struct BatMonBus {
211  struct i2c_transaction bus_trans; // transaction
212  enum BatmonBusStatus bus_status; // device status
213  uint16_t data; // raw 16bit read from adc
214  uint8_t addr; // i2c device address
215 
216  // Current readings
218  float bus_current; // A
219 
220  // Bus voltage readings
222 
223  // Temperature readings
224  float bus_brd_tmp; // C
227  uint8_t t_idx; // temp sensor index
228 
229 };
230 
235 struct BatMonBal {
236  struct i2c_transaction bus_trans; // transaction
237  uint16_t data; // raw 16bit read from adc
238  uint8_t addr; // i2c device address
239 
240  //Balance ADC data
242 
243  // index for reading cells
245 };
246 
247 extern struct BatMonBus batmonbus;
248 extern struct BatMonBal batmonbal1;
249 extern struct BatMonBal batmonbal2;
250 
251 void battery_monitor_init(void);
252 void battery_monitor_init_bus(void);
254 
255 void battery_monitor_read_bus(void);
259 
260 void battery_monitor_event(void);
262 
264 
265 #endif /* BATTERY_MONITOR_H */
void battery_monitor_read_balance_ports(struct BatMonBal *)
Read balance ADC.
unsigned short uint16_t
Definition: types.h:16
uint16_t data
uint8_t battery_monitor_get_address(uint8_t channel)
Translates the channel number to the value for address pointer register Channels are numbered from 1 ...
void battery_monitor_read_balance_ports_1(void)
Read Balance ADC 1.
#define GAIN_CELL_4
int16_t batmon_current_offset
#define GAIN_CELL_6
struct BatMonBal batmonbal1
struct i2c_transaction bus_trans
float bus_current
void battery_monitor_init_bus(void)
Initializes bus ADC.
float bus_temp[TEMP_SENSORS_NB]
#define GAIN_CELL_2
uint8_t t_idx
void battery_monitor_read_bus(void)
Read bus (current, voltage and temperature sensors)
static const float battery_monitor_cellgains[]
Battery monitor Bus ADC struct.
Battery monitor Balance ADC struct.
void battery_monitor_check_i2c_transaction(struct i2c_transaction *t)
Complete i2c transactions once they succeed or fail.
float batmon_current_sensitivity
uint16_t data
uint16_t bus_current_mvolts
void battery_monitor_read_balance_ports_2(void)
Read Balance ADC 2.
uint16_t bus_voltage_mvolts
enum BatmonBusStatus bus_status
#define GAIN_CELL_5
uint8_t addr
Interface for electrical status: supply voltage, current, battery status, etc.
Architecture independent timing functions.
void battery_monitor_init(void)
Init variables.
#define GAIN_CELL_3
signed short int16_t
Definition: types.h:17
#define BATTERY_CELLS_NB
uint16_t bat_cell_mvolts[BATTERY_CELLS_NB]
I2C transaction structure.
Definition: i2c.h:93
BatmonBusStatus
Status for Bus ADC.
float bus_brd_tmp
unsigned char uint8_t
Definition: types.h:14
struct BatMonBus batmonbus
uint8_t addr
#define GAIN_CELL_1
static const uint8_t battery_monitor_tempmap[]
static const uint8_t battery_monitor_cellmap[]
Cell map - which cell is which channel Channels are 1-indexed, ie.e.
int16_t batmon_temp_offset
void battery_monitor_event(void)
Event function Check i2c transaction status for each device.
struct BatMonBal batmonbal2
#define TEMP_SENSORS_NB
uint8_t cell_index
static const float BatmonVbusGain
struct i2c_transaction bus_trans
float batmon_temp_sensitivity
static uint8_t channel
Definition: ADS8344.c:80
void battery_monitor_init_balance(struct BatMonBal *)
Initalizes balance ADC.
uint16_t bus_tempsensors_mvolts[TEMP_SENSORS_NB]
Architecture independent I2C (Inter-Integrated Circuit Bus) API.