Aircoookie / WLED

Control WS2812B and many more types of digital RGB LEDs with an ESP8266 or ESP32 over WiFi!
https://kno.wled.ge
MIT License
15.01k stars 3.24k forks source link

Usermod "Battery" features #2998

Closed itCarl closed 4 months ago

itCarl commented 1 year ago

First of all this isn't a real Bug neither a FR. sorry. I just want to get in touch with people that want to improve the Usermod.

I have seen that recently many good improvements have been added to the usermod. In fact, I am aware of the "bugs"/missing features. That's why I started to write a software to simulate a battery (battery packs will be supported later).

The Software is still far from finished but a start has been made. The main goal, however, is to use the software to better understand and analyze batteries in order to significantly improve the usermod. I think the software becomes very interesting especially when you have varying currents and you want to look at the runtime when a certain wled effect is activated. In my head I also have such features as "SOH, State Of Health", "Battery types" or "Smart battery calibration". Knowing these parameters from a battery for just a usermod might be a bit too much though.

Some Screenshots

batteryDashboard01 batteryDashboard02

This Software is still kinda basic. But can fully calculate parameters for static loads.

ℹ️ Sources

PS: I would have really liked to put this in the Repo dicussion section. But there isn't a dicussion section.

softhack007 commented 1 year ago

Hi @itCarl, I just added some initial support for LiPo batteries, as I have an esp32 device with built-in LiPo connector/charger.

61HJ1j4GzcL _AC_SL1000_

The framed connector is for attaching a 1S LiPo rechargeable battery like the one below, and the board includes a 500mA charging circuit.

61ms9EeuIIS _AC_SL1500_

I found some interesting things:

I'm happy with my results now, and I think it will also work on other esp32 with build-in LiPo connector.

Screenshot_20230104-222908

As you see, it's well-aligned with the "rcgroups" table.

Actually I don't really understand how the voltage measurements on ESP8266 work, as the original calculation seems to use some "hidden magic numbers", and the results on esp32 looked very very wrong...

softhack007 commented 1 year ago

In general I think it's not correct to assume that voltage and capacity have a linear relation. Might be the case for 18650 (I don't know), however seems to be different for other types of battery, especially the LiPo cells.

softhack007 commented 1 year ago

Another thought on the "remaining battery time".

itCarl commented 1 year ago

In general I think it's not correct to assume that voltage and capacity have a linear relation. Might be the case for 18650 (I don't know), however seems to be different for other types of battery, especially the LiPo cells.

That fact that the Charge/Discharge Curve of Batteries isn't linear is 100% correct. At the time i created this usermod i didn't know any better i just needed a rough estimate of the battery charge / voltage.

Another thought on the "remaining battery time".

  • the downside of using strip.currentMilliamps will be that this value is momentary. It changes from one millisecond to the other - literally. And it assumes that there is a linear relation between LED power usage and led brightness, which generally is not true.
  • another option could be
    • monitor how battery capacity (batterylevel) drops during 15 minutes or so. Like store a battery level value every 5 minutes in a small array with 4 entries, and always use the oldest value.
    • calculate how long it would take at the same rate (that you get for last 15+x min) to go down to 10% -> remaining battery time.
    • might be more reliable, and you can even do that without knowing the total ampere that the battery can store.
    • would even work with worn-out batteries that do not reach their design capacity any more

A really huge problem ist that the battery voltage drops when a load is applied. As you can imagine that is a huge Problem because the voltage is the base parameter that all calculations are based of rn. For a reliable measurement, the LEDs must be switched off via a transistor so that the battery voltage can settle. Only then can the voltage be measured. If you want to achive a near perfect estimation of the battery capacity you would have to build a piece of Hardware that does exactly that. For example a SparkFun Coulomb Counter Breakout - LTC4150 could do that. That method of estimating the battery capacity is called "Coulomb counting". It's based on current / how many C's got discharged. The downside is that it has to be re-calibrated on a regular basis. That little boi could also be another option for reading relibly the battery capacityFuel Gauge: MAX17043

Reading voltages with the analog pin of a microcontroller just by itself is not so accurate as you may think. Have a look at this Video by Andreas Spiess How good are the ADCs inside Arduinos, ESP8266, and ESP32? And external ADCs (ADS1115)

softhack007 commented 1 year ago

A really huge problem ist that the battery voltage drops when a load is applied. As you can imagine that is a huge Problem because the voltage is the base parameter that all calculations are based of rn.

🤔 hmmm good point. Will this drop be there while the load stays connected, or is it just happening initially but then it settles and comes back to "normal voltage"? Looking from signal processing perspective - If its an initial short-time voltage drop, then maybe a Kalman filter can compensate the drop, and the Kalman "quality" parameter could help to ignore measurements with "low confidence".

Edit: a simple Kalman implementation is here: https://github.com/denyssene/SimpleKalmanFilter

itCarl commented 1 year ago

ifdef USERMOD_BATTERY_USE_LIPO

// LiPo "1S" Batteries should not be dischared below 3V !!

define USERMOD_BATTERY_MIN_VOLTAGE 3.2f

else

define USERMOD_BATTERY_MIN_VOLTAGE 2.6f

endif

@softhack007 - I saw your change in the last commit 35832b0 and i'm not sure that it is 100% correct. As example, a TP4056 charging module that also features a discharge protection circuit/chip (the DW01) has a "Overdischarge Protection Voltage" at 2.6 Volts. I'm aware that the DW01 is not the state of the art thing but it is so very commonly used. I rarely see a FS312F-G used for discharge protection. The second thing is that the Datasheet of the 18650 Batteries that i use have a Discharge voltage of 2.5 Volts. Thats why i choose the 2.7 v as the default for minBatteryVoltage AD310-2

Does the TTGO-T7 have discharge protection circuit/chip? At the first glance i can't find a chip that handles discharge control. I can only find the LTH7B that you have mentioned earlier. 🤔 How does it proted it self.

I just saw that the battery Makerfocus 9065115 Battery 10000mAh that you use has a 1C discharge rating... but the datasheet says "maximum discharge current" 5000mA => 5A. But if it has a capacity of 10A and a CRate of 1C than the "maximum discharge current" should be 10A. Also The battery Discharge CutOff is at 3V but the protection circuit has a discharge detection at 2.5v. I'm confused big times 😕.

softhack007 commented 1 year ago

@itCarl maybe it really depends on the battery type. I was thinking of these "China flat rechargeable" batteries like the one from makerfocus, that are very popular for RC models/drones, small DIY devices, or as RasPi power packs. These usually have a warning note like "don't discharge below 3V". Even when some 18650 might also be LiPo-based, it can be that 18650 LiPo have different characteristics.

Most "flat rechargeables" have a builtin under-voltage protection circuit (notice the small chips under yellow Kapton tape), so it might be that the T7-v15 does not need additional protection.

I can double-check later what is correct, but just to be save, I think that 3.2V as default low voltage limit might be better for "flat LiPo". Any way users can change that as they want, its just a preset.

softhack007 commented 1 year ago

This is another typical "flat LiPo" from China. "Battery Protection Circuit (PCM) included".

61viYawdDHL _AC_SL1500_

71GMJlh8dvL _AC_SL1500_

Here in Germany, I see them for 10-15€ on Amazon, maybe even cheaper on Ali.

itCarl commented 1 year ago

Also The battery Discharge CutOff is at 3V but the protection circuit has a discharge detection at 2.5v. I'm confused big times 😕.

Do you have a multimeter or other device to measure the battery voltage? It would be interesting to see what voltage the battery actually discharges to.

softhack007 commented 1 year ago

Do you have a multimeter or other device to measure the battery voltage?

Yes, both 😉 I can connect a battery and let it run out, then measure remaining voltage - I'll tell you tomorrow or on the weekend.

itCarl commented 1 year ago

@softhack007 Very nice, Thanks!

I just thought about the calibration feature/value. Does it make sense to calculate it instead of having a correction value entered manually? So what i mean by that correctionValue = measured voltage by multimeter or something / sensed voltage by the microcontroller. And then the correctionValue gets multiplied by the calculated voltage.

I'm also thinking about to add support for a transistor switch pin. It would be nice to lower the leakage current from the voltage divider circuit. But thats probably to 'advanced' for a lot users.

Found an interesting resource: A Page about Battery reviews with Charts 'n Stuff

softhack007 commented 1 year ago

Do you have a multimeter or other device to measure the battery voltage? It would be interesting to see what voltage the battery actually discharges to.

Hi, I just manged to "watch a LiPo die", and took Multi Meter measurements during the last minutes.


Edit: my observation (unstable below 3.0V) seems to also match with what is written in the ESP32 datasheet:

• Chips with a 3.3 V flash or PSRAM embedded: this minimum voltage is 3.0 V; • Chips with no flash or PSRAM embedded: this minimum voltage is 2.3 V • When writing eFuse, VDD3P3_RTC should be at least 3.3 V.

I assume that writing FLASH (LittleFS filesystem) also requires ~3.3V, as EEPROM and FLASH are actually the same on ESP32.

So in summary it seems that power supply for ESP32 should not drop below 3.3V, Maybe we can go down to 3.2V but not below.

itCarl commented 1 year ago

I really appreciate the work you have done. It has answered some questions about batteries for me. And I think this will be a good source of information for the future.

Edit: a simple Kalman implementation is here: https://github.com/denyssene/SimpleKalmanFilter

That sounds and looks like a good idea 👍 I've seen a similar smoothing method. It looked very very simple and easy to use without any libs. But I haven't had time to take a closer look yet.

softhack007 commented 1 year ago

It looked very very simple and easy to use without any libs.

A very simple (but effective) smoothing method is using an exponential filter, like this:

constexpr float alpha = 0.1; // between 0 and 1, to control strength of the filter
static float filteredValue = 0;   // smoothed output - kept between filter runs

float raw = analogRead(34); // raw value
filteredValue = filteredValue + alpha * (raw-filteredValue);  // apply exponential filtering

We use this simple method a lot in the audioreactive usermod.

itCarl commented 1 year ago

Interesting. The code snippet i found looks kinda similar and is propably doing the identical think:

x = (99.0f * x + value) / 100.0f

softhack007 commented 1 year ago

x = (99.0f * x + value) / 100.0f

yes its basically doing the same, just that my "audioreactive filter" variant is a bit more efficient (avoids division), and it keeps more precision (avoids the 99 and /100). There are many ways to write an exponential filter. `x = x + 0.01 (value -x)`

itCarl commented 1 year ago

Okay. So I will implement your method of smoothing if it's ok for you.

I'm struggling come up with a charging indicator.

softhack007 commented 1 year ago

Okay. So I will implement your method of smoothing if it's ok for you.

Hi, sure go ahead, that's fine with me. The tricky part might be to find a good smoothing parameter. Due to low sampling rate, I'd suggest to start with 0.25 or 0.1, and then try reducing the value until you are happy with the smoothed results.

Very busy right now, so It'll take some time before I can review you latest code...

Cheers ✌️

Easyrunner commented 1 year ago

Hey guys, I'm running the battery usermod and select this auto off function. What happen when this switches to off? How it will switch on again? I have now the problem, that the threshold was set to 3.0V, but battery was not fully empty, so the Wemos was always powered. Now I charge the battery, but the Wemos seems "dead" Does it needs a power cycle then by Reset or short power-less?

Thx!

itCarl commented 1 year ago

Hi @Easyrunner, Thank you for using this usermod. It does not switch on automatically after you charged the battery. Note: The battery has to be charged above the autoOffThreshold in order to turn the LEDs (master On/Off) manually on again.

github-actions[bot] commented 7 months ago

Hey! This issue has been open for quite some time without any new comments now. It will be closed automatically in a week if no further activity occurs. Thank you for using WLED! ✨

softhack007 commented 4 months ago

你好,我是一个新手,请问是否可以请教一下IIC通信的代码呀?调试时貌似通信地址不正确,希望能得到你们的帮助,十分感谢!

Could you please translate to englisch?

itCarl commented 4 months ago

I think we should close this issue after we have helped JohnShen04. I don't want this issue/discussion to turn into a Battery usermod help ticket. People should create separate issues for their questions. Otherwise this Ticket/Issue will get too confusing.

itCarl commented 4 months ago

@JohnShen04 i dont quite know what u mean by LLC (l2c?) Address and what it has todo with the Battery Usermod.

Hello, I am a beginner. May I ask for the code for IIC communication? It seems that the communication address is incorrect during debugging. I hope to receive your help. Thank you very much!

softhack007 commented 4 months ago

我认为我们应该在帮助 JohnShen04 之后关闭这个问题。我不希望这个问题/讨论变成电池 usermod 帮助票。人们应该为他们的问题创建单独的问题。否则,这张票/问题会变得太混乱了.

Yes .I think you're right. I will delete my question, but before that, can I ask what the IIC address is? I think my problem lies here/(ㄒoㄒ)/~~

@JohnShen04 I am not sure I fully understand the question. Some ideas