Skip to content

Commit

Permalink
Rebased for 0_15
Browse files Browse the repository at this point in the history
Rebased the updated Battery usermod for 0_15.

Added a constant so the delay can be modified via my_config.h.

Small adjustments have been made to make the PR compatible again after the recent restructuring in this PR: (Aircoookie#3003).

Thankyou!
  • Loading branch information
adamsthws committed May 9, 2024
1 parent 075004b commit 01e6377
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
6 changes: 6 additions & 0 deletions usermods/Battery/battery_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
#endif
#endif

// The initial delay before the first battery voltage reading after power-on.
// This allows the voltage to stabilize before readings are taken, improving accuracy of initial reading.
#ifndef USERMOD_BATTERY_INITIAL_DELAY
#define USERMOD_BATTERY_INITIAL_DELAY 10000 // (milliseconds)
#endif

// the frequency to check the battery, 30 sec
#ifndef USERMOD_BATTERY_MEASUREMENT_INTERVAL
#define USERMOD_BATTERY_MEASUREMENT_INTERVAL 30000
Expand Down
5 changes: 5 additions & 0 deletions usermods/Battery/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ define `USERMOD_BATTERY` in `wled00/my_config.h`
| ----------------------------------------------- | ----------- |-------------------------------------------------------------------------------------- |
| `USERMOD_BATTERY` | | define this (in `my_config.h`) to have this usermod included wled00\usermods_list.cpp |
| `USERMOD_BATTERY_MEASUREMENT_PIN` | | defaults to A0 on ESP8266 and GPIO35 on ESP32 |
| `USERMOD_BATTERY_INITIAL_DELAY` | ms | delay before initial reading. defaults to 10 seconds to allow voltage stabilization
| `USERMOD_BATTERY_MEASUREMENT_INTERVAL` | ms | battery check interval. defaults to 30 seconds |
| `USERMOD_BATTERY_{TYPE}_MIN_VOLTAGE` | v | minimum battery voltage. default is 2.6 (18650 battery standard) |
| `USERMOD_BATTERY_{TYPE}_MAX_VOLTAGE` | v | maximum battery voltage. default is 4.2 (18650 battery standard) |
Expand Down Expand Up @@ -88,6 +89,10 @@ Specification from: [Molicel INR18650-M35A, 3500mAh 10A Lithium-ion battery, 3.

2024-04-30

- improved initial reading accuracy by delaying initial measurement to allow voltage to stabilize at power-on

2024-04-30

- integrate factory pattern to make it easier to add other / custom battery types
- update readme

Expand Down
30 changes: 9 additions & 21 deletions usermods/Battery/usermod_v2_Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,14 @@ class UsermodBattery : public Usermod
UMBattery* bat = new UnkownUMBattery();
batteryConfig cfg;

// Initial delay before first reading to allow voltage stabilization
unsigned long initialDelay = USERMOD_BATTERY_INITIAL_DELAY;
bool initialDelayComplete = false;
bool isFirstVoltageReading = true;
// how often to read the battery voltage
unsigned long readingInterval = USERMOD_BATTERY_MEASUREMENT_INTERVAL;
unsigned long nextReadTime = 0;
unsigned long lastReadTime = 0;
// Initial delay before first reading (in milliseconds)
unsigned long initialDelay = 10000;
// Flag to check if the initial delay is completed
bool initialDelayComplete = false;
// Flag to check if the initial reading is complete
bool isFirstReading = true;
// battery min. voltage
float minBatteryVoltage = USERMOD_BATTERY_MIN_VOLTAGE;
// battery max. voltage
float maxBatteryVoltage = USERMOD_BATTERY_MAX_VOLTAGE;
// all battery cells summed up
unsigned int totalBatteryCapacity = USERMOD_BATTERY_TOTAL_CAPACITY;
// raw analog reading
float rawValue = 0.0f;
// calculated voltage
float voltage = maxBatteryVoltage;
// between 0 and 1, to control strength of voltage smoothing filter
float alpha = USERMOD_BATTERY_AVERAGING_ALPHA;

Expand Down Expand Up @@ -165,7 +153,7 @@ class UsermodBattery : public Usermod
pinMode(batteryPin, INPUT);
#endif

// Delay the first voltage reading to allow voltage stabilization after powering up
// First voltage reading is delayed to allow voltage stabilization after powering up
nextReadTime = millis() + initialDelay;
lastReadTime = millis();

Expand Down Expand Up @@ -205,11 +193,11 @@ class UsermodBattery : public Usermod
nextReadTime = millis() + readingInterval;
}

// Make the first voltage reading once the initial delay has elapsed
if (isFirstReading)
// Make the first voltage reading after the initial delay has elapsed
if (isFirstVoltageReading)
{
voltage = readVoltage();
isFirstReading = false;
bat->setVoltage(readVoltage());
isFirstVoltageReading = false;
}

// check the battery level every USERMOD_BATTERY_MEASUREMENT_INTERVAL (ms)
Expand Down

0 comments on commit 01e6377

Please sign in to comment.