Replies: 2 comments 1 reply
-
I think you might be missing the point of my_activity. This is what you wrote: My_sleep is not a great name. I think the intention of it is the start time of the routine the checks how much time we need to sleep. My_activity, calculates the elapsed time of the loop, i.e. the amount of time all the functions took. This is (from the original code):
This check sets the SleepDelay to the expected value (50ms normally) but subtracts out the amount of time this loop took, so the next loop occurs at the right time. The last bit of code in the function is calculating the load average. It is not a lot of code, but it will increase the amount of time between loops (but it should be a pretty constant value). For your #2, that is a personal preference. But, this one doesn't even appear to be necessary: For #3, the current code is already doing what you want. For #4, there is always more than one way to do things. Tasmota is using the non-OS version of the ESP code. This means all tasks need to cooperate and NOT over use the CPU. While it probably would be possible to add a check to see if serial was enabled, it is not clear this is an easy check to make (given how much support for serial there is in Tasmota). Most of the time there is no serial available so that check is simple and then does nothing. I am working on a project where timing is very important (integrating power to get energy). I am using CCOUNT to get the processor cycles that have transpired between now and the last time I checked. This allows it handle different loop timings and get the correct answer. But, even before I did that, I found the timing to be accurate. How are you checking that the timing is off? |
Beta Was this translation helpful? Give feedback.
-
Nice observation. The Tasmota code looks like this:
I chked the core code and found this for the esp8266:
and this for the esp32:
It seems the esp8266 I'll change the code for the esp8266 allowing you to do some more tests. |
Beta Was this translation helpful? Give feedback.
-
Hi there,
now that I'm more involved with Tasmota, I'm also looking at the code.
I am not a programmer and therefore have limited skills as far as programming is concerned.
However, I noticed that the dynamic sleep mode does not always wait exactly 50ms with
sleep 50
, but sometimes a bit more.I tried it myself (via Gitpot) to see if it could be better "aligned" and I had an idea.
However, I do not dare to create a pull request, because I never before did that with code.
So I write once my idea in here. If they are good (at least in part), perhaps someone can point me in the right direction how to incorporate them. I am ready to learn how to do a pull request, that is not directly crushed down because of beginners mistaces...
Here you can see the changes that I did:
Bascht74@571a8fd
I moved the code for the calculation of
TasmotaGlobal.loop_load_avg
to the beginning of the look, so that the time spent this calculations it is taken into account in the timing stuff.Currently this seems not to be the case. This causes the loops to shift a bit every time.
I have often seen that people replace simple if-loops with the following constructs (and they also simplify the code visually):
uint32_t my_activity = millis() - my_sleep || 1; // We cannot divide by 0;
instead of:
I changed that, too for my changes, but I could easily can revert it as it is only optical stuff.
SleepDelay
, so that it always determines the next aligned "slot" and waits until then.This way, even with longer runtime, the time segments are kept much more exact than right now.
This way no
FUNC_EVERY_50_MSECOND
will be skipped every now and then, as a cause of a missing alignment.Example where you can see this quite well (looks clearer now as everything is executed almost the same time slot):
Is it really necessary to statically use
delay(1)
andif (Serial.available()) { break; } )
in those loops?If I get rid of the loop and use
delay(mseconds)
directly, e.g. the network response is much better and I don't see as many retransmissions on the network (only one from time to time).Could this be solved in another way, e.g. only use that part if you really need it (e.g. only for a device that has serial TX/RX GPIO configured).
Sebastian
Beta Was this translation helpful? Give feedback.
All reactions