Skip to content

Commit 5d524ee

Browse files
committed
refactor bat
1 parent 438ade4 commit 5d524ee

File tree

11 files changed

+1016
-1025
lines changed

11 files changed

+1016
-1025
lines changed
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ cfg.BAT_CAL_I //BatteryADC current conversion factor, set this to 1 and enable p
1111

1212
#pragma once
1313

14-
#include "../bat_interface.h"
14+
#include "bat_interface.h"
15+
#include "../cfg/cfg_interface.h"
1516

1617
class BatteryADC: public Battery {
1718
public:
18-
//float i;// = 0; //Battery current (A)
19-
//float v;// = 0; //battery voltage (V)
20-
//float mah;// = 0; //battery usage (Ah)
21-
//float wh;// = 0; //battery usage (Wh)
2219
float factor_v;// = 8.04/13951; //voltage conversion factor, set this to 1 and enable print_bat(), then enter here: Actual Volt / bat_v ADC reading
2320
float factor_i;// = 1.0/847; //current conversion factor, set this to 1 and enable print_bat(), then enter here: Actual Amperes / bat_i ADC reading
2421
uint32_t interval_us = 10000; //update interval in us
2522

26-
void setup() {
23+
void begin(MF_I2C *i2c, int8_t i2c_adr) override {
24+
(void)i2c; (void)i2c_adr; //does not use i2c
2725
i = 0;
2826
v = 0;
2927
mah = 0;
@@ -38,7 +36,7 @@ class BatteryADC: public Battery {
3836
}
3937

4038
//returns true if battery was updated
41-
bool update() {
39+
bool update() override {
4240
static uint32_t ts = micros();
4341
uint32_t now = micros();
4442
if(now - ts >= interval_us) {

src/madflight/bat/BatteryINA226.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
#include "bat_interface.h"
4+
#include "../common/MF_I2C.h"
5+
#include "INA226/INA226.h"
6+
7+
class BatteryINA226: public Battery {
8+
protected:
9+
INA226 bat_ina226;
10+
11+
public:
12+
void begin(MF_I2C *i2c, int8_t i2c_adr) override {
13+
if(i2c_adr == 0) i2c_adr = 0x40; //default address
14+
bat_ina226.begin(i2c, i2c_adr);
15+
16+
// Configure INA226 -> sample time = 2 * 128 * 140us = 36ms => 28Hz
17+
bat_ina226.configure(INA226_AVERAGES_128, INA226_BUS_CONV_TIME_140US, INA226_SHUNT_CONV_TIME_140US, INA226_MODE_SHUNT_BUS_CONT);
18+
19+
float Rshunt = cfg.BAT_CAL_I; //ohm
20+
bat_ina226.calibrate(Rshunt);
21+
}
22+
23+
bool update() override {
24+
static uint32_t ts = micros();
25+
if(!bat_ina226.isConversionReady()) return false;
26+
uint32_t now = micros();
27+
float dt_h = (now - ts) / 3600e6;
28+
ts = now;
29+
i = bat_ina226.readShuntCurrent();
30+
v = bat_ina226.readBusVoltage();
31+
//w = bat_ina226.readBusPower(); //note w is not always equal to v * i, because w, v, and i are averaged values
32+
w = v * i; //This appears to be more accurate, specially for low values.
33+
mah += i * dt_h * 1000;
34+
wh += w * dt_h;
35+
return true;
36+
}
37+
};

src/madflight/bat/BatteryINA228.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
#include "bat_interface.h"
4+
#include "../common/MF_I2C.h"
5+
#include "INA228/INA228.h"
6+
7+
class BatteryINA228: public Battery {
8+
protected:
9+
INA228 bat_ina228;
10+
11+
public:
12+
void begin(MF_I2C *i2c, int8_t i2c_adr) override {
13+
if(i2c_adr == 0) i2c_adr = 0x40; //default address
14+
15+
float Rshunt = cfg.BAT_CAL_I; //ohm
16+
17+
// Configure INA226 -> sample time = 512 * 2 * 50us = 51.2ms => 20Hz
18+
bat_ina228.begin(i2c, i2c_adr);
19+
bat_ina228.setADCRange(false); // false => 164 mV, true => 41 mV
20+
bat_ina228.setBusVoltageConversionTime(INA228_50_us);
21+
bat_ina228.setShuntVoltageConversionTime(INA228_50_us);
22+
bat_ina228.setTemperatureConversionTime(INA228_50_us);
23+
bat_ina228.setAverage(INA228_512_SAMPLES);
24+
bat_ina228.calibrate(Rshunt);
25+
bat_ina228.setMode(INA228_MODE_CONT_BUS_SHUNT);
26+
}
27+
28+
bool update() override {
29+
if(!bat_ina228.isConversionReady()) return false;
30+
i = bat_ina228.getAmpere();
31+
v = bat_ina228.getBusVolt();
32+
w = bat_ina228.getWatt();
33+
mah = bat_ina228.getCoulomb() * (1000.0 / 3600);
34+
wh = bat_ina228.getWattHour();
35+
return true;
36+
}
37+
};

src/madflight/bat/BatteryNone.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include "bat_interface.h"
4+
5+
class BatteryNone: public Battery {
6+
public:
7+
void begin(MF_I2C *i2c, int8_t i2c_adr) override { (void)i2c; (void)i2c_adr; }
8+
bool update() override { return false; }
9+
};

0 commit comments

Comments
 (0)