Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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 */
electrical.h
uint16_t
unsigned short uint16_t
Definition: types.h:16
BatMonBal::data
uint16_t data
Definition: battery_monitor.h:237
batmon_temp_sensitivity
float batmon_temp_sensitivity
Definition: battery_monitor.c:42
channel
static uint8_t channel
Definition: ADS8344.c:80
GAIN_CELL_1
#define GAIN_CELL_1
Definition: battery_monitor.h:113
BATTERY_MONITOR_BUS_TEMPERATURE_READ
@ BATTERY_MONITOR_BUS_TEMPERATURE_READ
Definition: battery_monitor.h:203
BATTERY_MONITOR_BUS_VOLTAGE_READ
@ BATTERY_MONITOR_BUS_VOLTAGE_READ
Definition: battery_monitor.h:201
battery_monitor_read_bus
void battery_monitor_read_bus(void)
Read bus (current, voltage and temperature sensors)
Definition: battery_monitor.c:197
BATTERY_MONITOR_BUS_TEMPERATURE_REQ
@ BATTERY_MONITOR_BUS_TEMPERATURE_REQ
Definition: battery_monitor.h:202
BatMonBus
Battery monitor Bus ADC struct.
Definition: battery_monitor.h:210
BatMonBal
Battery monitor Balance ADC struct.
Definition: battery_monitor.h:235
batmonbus
struct BatMonBus batmonbus
Definition: battery_monitor.c:34
GAIN_CELL_4
#define GAIN_CELL_4
Definition: battery_monitor.h:116
BatMonBal::bus_trans
struct i2c_transaction bus_trans
Definition: battery_monitor.h:236
BatMonBus::addr
uint8_t addr
Definition: battery_monitor.h:214
battery_monitor_init
void battery_monitor_init(void)
Init variables.
Definition: battery_monitor.c:140
BatMonBal::bat_cell_mvolts
uint16_t bat_cell_mvolts[BATTERY_CELLS_NB]
Definition: battery_monitor.h:241
BatMonBus::t_idx
uint8_t t_idx
Definition: battery_monitor.h:227
BATTERY_CELLS_NB
#define BATTERY_CELLS_NB
Definition: battery_monitor.h:94
battery_monitor_event
void battery_monitor_event(void)
Event function Check i2c transaction status for each device.
Definition: battery_monitor.c:161
BatMonBus::bus_temp
float bus_temp[TEMP_SENSORS_NB]
Definition: battery_monitor.h:226
battery_monitor_init_bus
void battery_monitor_init_bus(void)
Initializes bus ADC.
Definition: battery_monitor.c:102
battery_monitor_init_balance
void battery_monitor_init_balance(struct BatMonBal *)
Initalizes balance ADC.
Definition: battery_monitor.c:130
GAIN_CELL_3
#define GAIN_CELL_3
Definition: battery_monitor.h:115
battery_monitor_tempmap
static const uint8_t battery_monitor_tempmap[]
Definition: battery_monitor.h:140
battery_monitor_read_balance_ports_2
void battery_monitor_read_balance_ports_2(void)
Read Balance ADC 2.
Definition: battery_monitor.c:347
std.h
BatmonBusStatus
BatmonBusStatus
Status for Bus ADC.
Definition: battery_monitor.h:197
BATTERY_MONITOR_BUS_VOLTAGE_REQ
@ BATTERY_MONITOR_BUS_VOLTAGE_REQ
Definition: battery_monitor.h:200
BatMonBus::bus_tempsensors_mvolts
uint16_t bus_tempsensors_mvolts[TEMP_SENSORS_NB]
Definition: battery_monitor.h:225
BATTERY_MONITOR_BUS_CURRENT_READ
@ BATTERY_MONITOR_BUS_CURRENT_READ
Definition: battery_monitor.h:199
BatmonVbusGain
static const float BatmonVbusGain
Definition: battery_monitor.h:152
BatMonBal::cell_index
uint8_t cell_index
Definition: battery_monitor.h:244
int16_t
signed short int16_t
Definition: types.h:17
sys_time.h
Architecture independent timing functions.
batmonbal1
struct BatMonBal batmonbal1
Definition: battery_monitor.c:35
uint8_t
unsigned char uint8_t
Definition: types.h:14
BatMonBus::bus_brd_tmp
float bus_brd_tmp
Definition: battery_monitor.h:224
BatMonBus::data
uint16_t data
Definition: battery_monitor.h:213
i2c_transaction
I2C transaction structure.
Definition: i2c.h:93
BatMonBus::bus_trans
struct i2c_transaction bus_trans
Definition: battery_monitor.h:211
GAIN_CELL_2
#define GAIN_CELL_2
Definition: battery_monitor.h:114
battery_monitor_cellmap
static const uint8_t battery_monitor_cellmap[]
Cell map - which cell is which channel Channels are 1-indexed, ie.e.
Definition: battery_monitor.h:129
batmonbal2
struct BatMonBal batmonbal2
Definition: battery_monitor.c:36
battery_monitor_cellgains
static const float battery_monitor_cellgains[]
Definition: battery_monitor.h:120
batmon_current_sensitivity
float batmon_current_sensitivity
Definition: battery_monitor.c:40
batmon_current_offset
int16_t batmon_current_offset
Definition: battery_monitor.c:39
battery_monitor_check_i2c_transaction
void battery_monitor_check_i2c_transaction(struct i2c_transaction *t)
Complete i2c transactions once they succeed or fail.
Definition: battery_monitor.c:170
BatMonBus::bus_current
float bus_current
Definition: battery_monitor.h:218
TEMP_SENSORS_NB
#define TEMP_SENSORS_NB
Definition: battery_monitor.h:97
batmon_temp_offset
int16_t batmon_temp_offset
Definition: battery_monitor.c:41
BatMonBal::addr
uint8_t addr
Definition: battery_monitor.h:238
GAIN_CELL_6
#define GAIN_CELL_6
Definition: battery_monitor.h:118
BatMonBus::bus_voltage_mvolts
uint16_t bus_voltage_mvolts
Definition: battery_monitor.h:221
battery_monitor_get_address
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 ...
Definition: battery_monitor.c:91
battery_monitor_read_balance_ports
void battery_monitor_read_balance_ports(struct BatMonBal *)
Read balance ADC.
Definition: battery_monitor.c:354
GAIN_CELL_5
#define GAIN_CELL_5
Definition: battery_monitor.h:117
battery_monitor_read_balance_ports_1
void battery_monitor_read_balance_ports_1(void)
Read Balance ADC 1.
Definition: battery_monitor.c:340
i2c.h
BATTERY_MONITOR_BUS_CURRENT_REQ
@ BATTERY_MONITOR_BUS_CURRENT_REQ
Definition: battery_monitor.h:198
BatMonBus::bus_current_mvolts
uint16_t bus_current_mvolts
Definition: battery_monitor.h:217
BatMonBus::bus_status
enum BatmonBusStatus bus_status
Definition: battery_monitor.h:212