Skip to content

Commit

Permalink
Update Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
itCarl committed Sep 9, 2023
1 parent b8c61b5 commit a9d6a15
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 154 deletions.
23 changes: 22 additions & 1 deletion usermods/Battery/battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Battery
float voltage;
int8_t level = 100;
float calibration; // offset or calibration value to fine tune the calculated voltage
float voltageMultiplier; // ratio for the voltage divider

float linearMapping(float v, float min, float max, float oMin = 0.0f, float oMax = 100.0f)
{
Expand All @@ -26,7 +27,9 @@ class Battery
public:
Battery()
{

this->setVoltage(this->getVoltage());
this->setVoltageMultiplier(USERMOD_BATTERY_VOLTAGE_MULTIPLIER);
this->setCalibration(USERMOD_BATTERY_CALIBRATION);
}

virtual void update(batteryConfig cfg) = 0;
Expand Down Expand Up @@ -127,6 +130,24 @@ class Battery
{
calibration = offset;
}

/*
* Get the configured calibration value
* a value to set the voltage divider ratio
*/
virtual float getVoltageMultiplier()
{
return voltageMultiplier;
}

/*
* Set the voltage multiplier value
* a value to set the voltage divider ratio.
*/
virtual void setVoltageMultiplier(float multiplier)
{
voltageMultiplier = voltageMultiplier;
}
};

#endif
25 changes: 5 additions & 20 deletions usermods/Battery/battery_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@
#ifndef USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE
#define USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE 4.2f
#endif
#ifndef USERMOD_BATTERY_UNKOWN_CALIBRATION
// offset or calibration value to fine tune the calculated voltage
#define USERMOD_BATTERY_UNKOWN_CALIBRATION 0
#endif

/*
*
* Lithium polymer (Li-Po) defaults
Expand All @@ -56,9 +53,7 @@
#ifndef USERMOD_BATTERY_LIPO_MAX_VOLTAGE
#define USERMOD_BATTERY_LIPO_MAX_VOLTAGE 4.2f
#endif
#ifndef USERMOD_BATTERY_LIPO_CALIBRATION
#define USERMOD_BATTERY_LIPO_CALIBRATION 0
#endif

/*
*
* Lithium-ion (Li-Ion) defaults
Expand All @@ -71,12 +66,8 @@
#ifndef USERMOD_BATTERY_LION_MAX_VOLTAGE
#define USERMOD_BATTERY_LION_MAX_VOLTAGE 4.2f
#endif
#ifndef USERMOD_BATTERY_LION_CALIBRATION
// offset or calibration value to fine tune the calculated voltage
#define USERMOD_BATTERY_LION_CALIBRATION 0
#endif

//the default ratio for the voltage divider
// the default ratio for the voltage divider
#ifndef USERMOD_BATTERY_VOLTAGE_MULTIPLIER
#ifdef ARDUINO_ARCH_ESP32
#define USERMOD_BATTERY_VOLTAGE_MULTIPLIER 2.0f
Expand All @@ -85,13 +76,8 @@
#endif
#endif

#ifndef USERMOD_BATTERY_MAX_VOLTAGE
#define USERMOD_BATTERY_MAX_VOLTAGE 4.2f
#endif

// a common capacity for single 18650 battery cells is between 2500 and 3600 mAh
#ifndef USERMOD_BATTERY_TOTAL_CAPACITY
#define USERMOD_BATTERY_TOTAL_CAPACITY 3100
#ifndef USERMOD_BATTERY_AVERAGING_ALPHA
#define USERMOD_BATTERY_AVERAGING_ALPHA 0.1f
#endif

// offset or calibration value to fine tune the calculated voltage
Expand Down Expand Up @@ -138,7 +124,6 @@ typedef struct bconfig_t
batteryType type;
float minVoltage;
float maxVoltage;
unsigned int capacity; // current capacity
float voltage; // current voltage
int8_t level; // current level
float calibration; // offset or calibration value to fine tune the calculated voltage
Expand Down
3 changes: 1 addition & 2 deletions usermods/Battery/types/lion.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ class Lion : public Battery

public:
Lion()
: Battery()
{
this->setMinVoltage(USERMOD_BATTERY_LION_MIN_VOLTAGE);
this->setMaxVoltage(USERMOD_BATTERY_LION_MAX_VOLTAGE);
this->setVoltage(this->getVoltage());
this->setCalibration(USERMOD_BATTERY_LION_CALIBRATION);
}

void update(batteryConfig cfg)
Expand Down
3 changes: 1 addition & 2 deletions usermods/Battery/types/lipo.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ class Lipo : public Battery

public:
Lipo()
: Battery()
{
this->setMinVoltage(USERMOD_BATTERY_LIPO_MIN_VOLTAGE);
this->setMaxVoltage(USERMOD_BATTERY_LIPO_MAX_VOLTAGE);
this->setVoltage(this->getVoltage());
this->setCalibration(USERMOD_BATTERY_LIPO_CALIBRATION);
}

void update(batteryConfig cfg)
Expand Down
4 changes: 1 addition & 3 deletions usermods/Battery/types/unkown.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ class Unkown : public Battery

public:
Unkown()
: Battery()
{
this->setMinVoltage(USERMOD_BATTERY_UNKOWN_MIN_VOLTAGE);
this->setMaxVoltage(USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE);
this->setVoltage(this->getVoltage());
this->setCalibration(USERMOD_BATTERY_UNKOWN_CALIBRATION);
}

void update(batteryConfig cfg)
{
if(cfg.minVoltage) this->setMinVoltage(cfg.minVoltage); else this->setMinVoltage(USERMOD_BATTERY_UNKOWN_MIN_VOLTAGE);
if(cfg.maxVoltage) this->setMaxVoltage(cfg.maxVoltage); else this->setMaxVoltage(USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE);
if(cfg.calibration) this->setCalibration(cfg.calibration); else this->setCalibration(USERMOD_BATTERY_UNKOWN_CALIBRATION);
}

float mapVoltage(float v, float min, float max) override
Expand Down
Loading

0 comments on commit a9d6a15

Please sign in to comment.