-->
Page 1 of 2

Control / pause / resume background tasks

PostPosted: Sat Sep 30, 2017 12:03 pm
by ris8_allo_zen0
Hi all,
TL:DR; I'd like to know if putting WiFi to sleep will stop all background tasks that would otherwise run across loop()s and in wait()s, and if there are any others, how to stop them too.

I’m working on a project that involves controlling a motor using PID control techniques. The code works great with an Arduino Micro, now I'm porting it on an ESP8266 with Arduino platform.

Most of the time the system is idle and ready to accept commands from internet. But sometimes I want the processor to be completely dedicated to one task, i.e. not running any other task, for 4-5 seconds. To be more specific, during those 4-5 seconds a routine in my code should be invoked every 30-31ms.

This because if the PID loop is not run at regular intervals, the motor may spin more than necessary and potentially cause some mechanical damage.
Right now the code runs fine, but I see fairly long pauses (even 4 seconds) every once in a while. This is probably due to the Wi-Fi stack running some background tasks.

I know that the ESP8266 runs several tasks between loop()s and in wait()s. I know that most of them are Wi-Fi related, so I guess that putting the Wi-Fi on sleep will make for more predictable timings.
Is it true? Or are there more time-consuming background tasks? Can I stop them too, without disrupting general functionality of the ESP?
What happens if I simply give no opportunity for these tasks to run, by not returning from loop() and not calling any wait() until my important task is over?

Thanks!
Kind regards,
Enrico

Re: Control / pause / resume background tasks

PostPosted: Wed Oct 04, 2017 10:37 pm
by zdewitt
My understanding is that other functions of the WiFi etc. are only serviced when the loop() finishes, if you call a delay(), or if you call yield(). Other than that, your code has a lock on the processor, except for interrupts of course if they are turned on. Note that delayMicroseconds() does NOT release to other functions.

Several seconds sounds unusual to me for wifi to be blocking other functions, but I dunno maybe that's not unheard of? As for your PID code, you cannot block the WiFi from servicing for more than around 100ms or so I think. If you do then it's likely you will get a WDT reset or the WiFi will lose data.

If the PID needs a 30-31ms cycle for several seconds, you may need to use a timer interrupt that will run even when other functions are executing.

Re: Control / pause / resume background tasks

PostPosted: Thu Oct 05, 2017 8:43 pm
by mrburnette
ris8_allo_zen0 wrote:Hi all,
...
Most of the time the system is idle and ready to accept commands from internet. But sometimes I want the processor to be completely dedicated to one task, i.e. not running any other task, for 4-5 seconds. To be more specific, during those 4-5 seconds a routine in my code should be invoked every 30-31ms.
...
What happens if I simply give no opportunity for these tasks to run, by not returning from loop() and not calling any wait() until my important task is over?


Arduino on ESP8266 as implemented by igrr's code runs in a pseudo thread managed by the non-OS library. When I was working on the project
https://www.hackster.io/rayburne/esp826 ... ime-1df8ae
I still had to relinquish to the RF tasks to avoid aborts. The published max_time in the Arduino section is 50mS, but I prefer around 30mS myself.

Ray

Re: Control / pause / resume background tasks

PostPosted: Wed Oct 11, 2017 3:47 pm
by ris8_allo_zen0
zdewitt wrote:Several seconds sounds unusual to me for wifi to be blocking other functions, but I dunno maybe that's not unheard of?

I saw once that my code stopped printing on the serial terminal for quite some time. But I'm not entirely sure that my loop() hasn't been called during that time. I'll do some more tests.
zdewitt wrote:As for your PID code, you cannot block the WiFi from servicing for more than around 100ms or so I think. If you do then it's likely you will get a WDT reset or the WiFi will lose data.

If the PID needs a 30-31ms cycle for several seconds, you may need to use a timer interrupt that will run even when other functions are executing.

I think that's the most reliable way to go. Thanks!