Register / Login  |  Desktop view  |  Jump to bottom of page

IoAbstraction & TaskManagerIO » TaskmanagerIo tasks in an array

Author: werner
23/02/2022 11:21:35
Hi,
how can I add TaskmanagerIo tasks in an array ?
I have an array of classes which includes a function controlValve().
I have tried this :

thisPiControl=PiControl[picIdx];
               
       taskIdPiControl[picIdx] = taskManager.scheduleFixedRate(VdmConfig.configFlash.valvesControlConfig.valveControlConfig[picIdx].ts*1000, [] {
                    UART_DBG.println("pic call");
                    thisPiControl.controlValve();
                });


But the call is always the same index of the class.

best regards
Werner

Author: davetcc
23/02/2022 13:55:43
I think you've misunderstood C++ memory model here.

If thisPiControl is a global variable, then changing it changes it for every task.
However, if thisPiControl is a local variable, you're lucky the sketch hasn't crashed as you'd be accessing memory that no longer had the same purpose as it did before.

If you are using a 32 bit board that support std::function, you can enable lamba support at the cost of your tasks using a bit more memory. Given it has memory side effects it needs to be enabled by setting the compiler flag TM_ENABLE_CAPTURED_LAMBDAS. With this enabled you could capture thisPiControl each time.

The other option without needing that support is to write your tasks as classes extending from Executable, or use ExecWithParameter.

All of these are covered within the examples.

Author: davetcc
23/02/2022 13:58:01
For your case, I would make all those classes extend Executable, and implement the exec() method, the examples show how to do this. Basically for scheduling, you just then pass a pointer to your class instance instead.

Author: werner
23/02/2022 15:22:54
I have tried this according your example:
class CPiControl: public Executable
{
public:
  CPiControl();
  float piCtrl(float e);
  .....
}

extern CPiControl PiControl[ACTUATOR_COUNT];


But the compiler shows an error : an array of an abstract class is not allowed

Author: davetcc
26/02/2022 07:51:32
That example is incomplete. If the class is abstract it means in this case you have not implemented the methods required by the Executable interface.

You will need to implement exec() on that class; which will be called on the schedule.

Author: werner
26/02/2022 13:12:03
Thx, it works




Register / Login  |  Desktop view  |  Jump to top of page