0

I'm trying to use the last version of leOS2 scheduler from Leonardo Miliani ( https://github.com/leomil72/leOS2 ) but I have a strange behavior with a very simplistic ONETIME task.

Platform : Arduino Uno clone, so Atmel Atmega 328P CPU.

My code is very straightforward :

#include "leOS2.h"
leOS2 myOS; //create a new istance of the class leOS

bool test = true;

void setup()
{
    myOS.begin(); //initialize the scheduler
}
void loop()
{
    // Do some stuff ...

    if(test)
    {
       DoAndUndo(3000);
       test = false;
    }
}

void DoAndUndo(unsigned long duration)
{
    doSomething();

    myOS.addTask(cancelTheThing, myOS.convertMs(duration), ONETIME);
}


void doSomething()
{
    Serial.println("thing on");
}

void cancelTheThing()
{
    Serial.println("thing is off");
}

In case it can be some kind of conflict, my code also relies on the following :

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>

The problem is : myOS.addTask(cancelTheThing, myOS.convertMs(duration), ONETIME); is supposed to call asynchronously the "cancelTheThing" method 3 seconds later, and for only one time, and then forget it. But for an unknown reason, it's called after the expected delay, but it loops infinitely on this method ! In the example case above, my serial stream is filled with a lot of "thing is off" messages, and the µc is unresponsive to anything apart a reset.

The call to DoAndUndo() will produce a such like output :

 thing on

And 3s later, in a very fast infinite loop :

 thing is off
 thing is off
 thing is off
 thing is off

If I remove the "ONETIME" flag, the task is called every three seconds (exactly as it's suppose to do, so the scheduler works, but it's not what I want in my use case).

note : As a try of workaround, I've tried some tricks like calling haltScheduler() inside my cancelTheThing() and reenable it with restartScheduler() in DoAndUndo(), and also tried to call removeTask once it has ran, but it didn't help.

Thank you for your help

1

There appears to be a fault with the library whereby if the only registered task is a one-shot task it doesn't get cancelled when it has executed.

Adding another periodic task in setup() (say to blink an LED every 500ms) so there are now 2 tasks instead of one works around the problem.

The way the system is written it will always try an execute task number 0 even when there are 0 tasks registered because they erroneously used a "do {...} while" construct instead of a "while { ...}" construct.


I have filed a bug report, corrected the error, and raised a pull request with the fix in it. Until it gets accepted you can obtain the fixed version here: https://github.com/majenkotech/leOS2

|improve this answer|||||
  • Hello, thanks to have answered but it's not the solution. The above sample was not accurate (I've edited it, sorry for the lack of precision). The "infinite loop" issue I encounter effectively comes from the scheduler as I explained, not a such trivial issue as you thought. If it was a so simple mistake, my "doSomething" method would have loop too... – AFract Oct 19 '15 at 17:54
  • @AFract If you want our help you will have to provide a piece of code that compiles and executes exactly as shown and demonstrates the problem. An approximation is of no use at all. – Majenko Oct 19 '15 at 18:03
  • 1
    It's NOT "to keep me happy"! It is to make it physically possible for us (NOT JUST ME) to HELP you! Quite frankly, to be brutally honest, I really don't know if I can be bothered now. You probably won't listen to the answer because apparently you already know everything! – Majenko Oct 19 '15 at 19:07
  • 1
    @AFract Now that I have re-written your program so that it actually works, I can replicate your results - something that I couldn't have done before. Now I can actually analyse the problem and find the cause. – Majenko Oct 19 '15 at 19:16
  • 2
    No, I had to re-write it. I have traced the problem and found a workaround for it - I have also corrected the underlying problem and raised a bug report and pull request with the repository. More info in my answer. – Majenko Oct 19 '15 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.