Skip to content

Commit

Permalink
vbat ibat config refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rtlopez committed Oct 26, 2024
1 parent fa1dffd commit 7740f9e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/Espfc/src/Actuator.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class Actuator
{
_model.state.buzzer.play(BUZZER_RX_LOST);
}
if(_model.state.battery.warn(_model.config.vbatCellWarning))
if(_model.state.battery.warn(_model.config.vbat.cellWarning))
{
_model.state.buzzer.play(BUZZER_BAT_LOW);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Espfc/src/Blackbox/Blackbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ int Blackbox::begin()

featureConfigMutable()->enabledFeatures = _model.config.featureMask;

batteryConfigMutable()->currentMeterSource = (currentMeterSource_e)_model.config.ibatSource;
batteryConfigMutable()->voltageMeterSource = (voltageMeterSource_e)_model.config.vbatSource;
batteryConfigMutable()->vbatwarningcellvoltage = _model.config.vbatCellWarning;
batteryConfigMutable()->currentMeterSource = (currentMeterSource_e)_model.config.ibat.source;
batteryConfigMutable()->voltageMeterSource = (voltageMeterSource_e)_model.config.vbat.source;
batteryConfigMutable()->vbatwarningcellvoltage = _model.config.vbat.cellWarning;
batteryConfigMutable()->vbatmaxcellvoltage = 420;
batteryConfigMutable()->vbatmincellvoltage = 340;

Expand Down
18 changes: 9 additions & 9 deletions lib/Espfc/src/Cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,15 @@ class Cli
Param(PSTR("board_align_pitch"), &c.boardAlignment[1]),
Param(PSTR("board_align_yaw"), &c.boardAlignment[2]),

Param(PSTR("vbat_source"), &c.vbatSource, voltageSourceChoices),
Param(PSTR("vbat_scale"), &c.vbatScale),
Param(PSTR("vbat_mul"), &c.vbatResMult),
Param(PSTR("vbat_div"), &c.vbatResDiv),
Param(PSTR("vbat_cell_warn"), &c.vbatCellWarning),

Param(PSTR("ibat_source"), &c.ibatSource, currentSourceChoices),
Param(PSTR("ibat_scale"), &c.ibatScale),
Param(PSTR("ibat_offset"), &c.ibatOffset),
Param(PSTR("vbat_source"), &c.vbat.source, voltageSourceChoices),
Param(PSTR("vbat_scale"), &c.vbat.scale),
Param(PSTR("vbat_mul"), &c.vbat.resMult),
Param(PSTR("vbat_div"), &c.vbat.resDiv),
Param(PSTR("vbat_cell_warn"), &c.vbat.cellWarning),

Param(PSTR("ibat_source"), &c.ibat.source, currentSourceChoices),
Param(PSTR("ibat_scale"), &c.ibat.scale),
Param(PSTR("ibat_offset"), &c.ibat.offset),

Param(PSTR("fusion_mode"), &c.fusion.mode, fusionModeChoices),
Param(PSTR("fusion_gain_p"), &c.fusion.gain),
Expand Down
27 changes: 18 additions & 9 deletions lib/Espfc/src/ModelConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,22 @@ struct RpmFilterConfig
uint8_t fade = 30;
};

struct VBatConfig
{
int16_t cellWarning = 350;
uint8_t scale = 100;
uint8_t resDiv = 10;
uint8_t resMult = 1;
int8_t source = 0;
};

struct IBatConfig
{
int8_t source = 0;
int16_t scale = 100;
int16_t offset = 0;
};

// persistent data
class ModelConfig
{
Expand Down Expand Up @@ -637,15 +653,8 @@ class ModelConfig

char modelName[MODEL_NAME_LEN + 1];

int16_t vbatCellWarning = 350;
uint8_t vbatScale = 100;
uint8_t vbatResDiv = 10;
uint8_t vbatResMult = 1;
int8_t vbatSource = 0;

int8_t ibatSource = 0;
int16_t ibatScale = 100;
int16_t ibatOffset = 0;
VBatConfig vbat;
IBatConfig ibat;

int8_t debugMode = DEBUG_NONE;
uint8_t debugAxis = 1;
Expand Down
36 changes: 18 additions & 18 deletions lib/Espfc/src/Msp/MspProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,27 +351,27 @@ class MspProcessor
case MSP_BATTERY_CONFIG:
r.writeU8(34); // vbatmincellvoltage
r.writeU8(42); // vbatmaxcellvoltage
r.writeU8((_model.config.vbatCellWarning + 5) / 10); // vbatwarningcellvoltage
r.writeU8((_model.config.vbat.cellWarning + 5) / 10); // vbatwarningcellvoltage
r.writeU16(0); // batteryCapacity
r.writeU8(_model.config.vbatSource); // voltageMeterSource
r.writeU8(_model.config.ibatSource); // currentMeterSource
r.writeU8(_model.config.vbat.source); // voltageMeterSource
r.writeU8(_model.config.ibat.source); // currentMeterSource
r.writeU16(340); // vbatmincellvoltage
r.writeU16(420); // vbatmaxcellvoltage
r.writeU16(_model.config.vbatCellWarning); // vbatwarningcellvoltage
r.writeU16(_model.config.vbat.cellWarning); // vbatwarningcellvoltage
break;

case MSP_SET_BATTERY_CONFIG:
m.readU8(); // vbatmincellvoltage
m.readU8(); // vbatmaxcellvoltage
_model.config.vbatCellWarning = m.readU8() * 10; // vbatwarningcellvoltage
_model.config.vbat.cellWarning = m.readU8() * 10; // vbatwarningcellvoltage
m.readU16(); // batteryCapacity
_model.config.vbatSource = toVbatSource(m.readU8()); // voltageMeterSource
_model.config.ibatSource = toIbatSource(m.readU8()); // currentMeterSource
_model.config.vbat.source = toVbatSource(m.readU8()); // voltageMeterSource
_model.config.ibat.source = toIbatSource(m.readU8()); // currentMeterSource
if(m.remain() >= 6)
{
m.readU16(); // vbatmincellvoltage
m.readU16(); // vbatmaxcellvoltage
_model.config.vbatCellWarning = m.readU16();
_model.config.vbat.cellWarning = m.readU16();
}
break;

Expand Down Expand Up @@ -414,9 +414,9 @@ class MspProcessor
r.writeU8(5); // frame size (5)
r.writeU8(i + 10); // id (10-19 vbat adc)
r.writeU8(0); // type resistor divider
r.writeU8(_model.config.vbatScale); // scale
r.writeU8(_model.config.vbatResDiv); // resdivval
r.writeU8(_model.config.vbatResMult); // resdivmultiplier
r.writeU8(_model.config.vbat.scale); // scale
r.writeU8(_model.config.vbat.resDiv); // resdivval
r.writeU8(_model.config.vbat.resMult); // resdivmultiplier
}
break;

Expand All @@ -425,9 +425,9 @@ class MspProcessor
int id = m.readU8();
if(id == 10 + 0) // id (10-19 vbat adc, allow only 10)
{
_model.config.vbatScale = m.readU8();
_model.config.vbatResDiv = m.readU8();
_model.config.vbatResMult = m.readU8();
_model.config.vbat.scale = m.readU8();
_model.config.vbat.resDiv = m.readU8();
_model.config.vbat.resMult = m.readU8();
}
}
break;
Expand All @@ -439,8 +439,8 @@ class MspProcessor
r.writeU8(6); // frame size (6)
r.writeU8(i + 10); // id (10-19 ibat adc)
r.writeU8(1); // type adc
r.writeU16(_model.config.ibatScale); // scale
r.writeU16(_model.config.ibatOffset); // offset
r.writeU16(_model.config.ibat.scale); // scale
r.writeU16(_model.config.ibat.offset); // offset
}
break;

Expand All @@ -449,8 +449,8 @@ class MspProcessor
int id = m.readU8();
if(id == 10 + 0) // id (10-19 ibat adc, allow only 10)
{
_model.config.ibatScale = m.readU16();
_model.config.ibatOffset = m.readU16();
_model.config.ibat.scale = m.readU16();
_model.config.ibat.offset = m.readU16();
}
}
break;
Expand Down
14 changes: 7 additions & 7 deletions lib/Espfc/src/Sensor/VoltageSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ class VoltageSensor: public BaseSensor
int readVbat()
{
#ifdef ESPFC_ADC_0
if(_model.config.vbatSource != 1 || _model.config.pin[PIN_INPUT_ADC_0] == -1) return 0;
if(_model.config.vbat.source != 1 || _model.config.pin[PIN_INPUT_ADC_0] == -1) return 0;
// wemos d1 mini has divider 3.2:1 (220k:100k)
// additionaly I've used divider 5.7:1 (4k7:1k)
// total should equals ~18.24:1, 73:4 resDiv:resMult should be ideal,
// but ~52:1 is real, did I miss something?
_model.state.battery.rawVoltage = analogRead(_model.config.pin[PIN_INPUT_ADC_0]);
float volts = _vFilterFast.update(_model.state.battery.rawVoltage * ESPFC_ADC_SCALE);

volts *= _model.config.vbatScale * 0.1f;
volts *= _model.config.vbatResMult;
volts /= _model.config.vbatResDiv;
volts *= _model.config.vbat.scale * 0.1f;
volts *= _model.config.vbat.resMult;
volts /= _model.config.vbat.resDiv;

_model.state.battery.voltageUnfiltered = volts;
_model.state.battery.voltage = _vFilter.update(_model.state.battery.voltageUnfiltered);
Expand Down Expand Up @@ -94,14 +94,14 @@ class VoltageSensor: public BaseSensor
int readIbat()
{
#ifdef ESPFC_ADC_1
if(_model.config.ibatSource != 1 && _model.config.pin[PIN_INPUT_ADC_1] == -1) return 0;
if(_model.config.ibat.source != 1 && _model.config.pin[PIN_INPUT_ADC_1] == -1) return 0;

_model.state.battery.rawCurrent = analogRead(_model.config.pin[PIN_INPUT_ADC_1]);
float volts = _iFilterFast.update(_model.state.battery.rawCurrent * ESPFC_ADC_SCALE);
float milivolts = volts * 1000.0f;

volts += _model.config.ibatOffset * 0.001f;
volts *= _model.config.ibatScale * 0.1f;
volts += _model.config.ibat.offset * 0.001f;
volts *= _model.config.ibat.scale * 0.1f;

_model.state.battery.currentUnfiltered = volts;
_model.state.battery.current = _iFilter.update(_model.state.battery.currentUnfiltered);
Expand Down

0 comments on commit 7740f9e

Please sign in to comment.