1

I want to control 2 independent processes with 2 PID controllers. Is it possible to run each controller on a separate core? I'm using this PID library. According to its documentation, the PID::Compute() function should be called once every loop(). How can this be achieved when using RTOS tasks (for multi core execution)?

  • why not put both PID::Compute() and PID2::Compute() in loop()? it still will be called once per instance – Juraj Jul 2 at 13:35
  • No that would work but the PID computation algorithm will only run on a single core and that's not what I want to achieve. – glamis Jul 2 at 14:09
  • still one core runs WiFi – Juraj Jul 2 at 16:14
0

Sure. The existing Arduino setup and loop are just executed in their own task:

void loopTask(void *pvParameters)
{
    setup();
    for(;;) {
        if(loopTaskWDTEnabled){
            esp_task_wdt_reset();
        }
        loop();
    }
}

extern "C" void app_main()
{
    loopTaskWDTEnabled = false;
    initArduino();
    xTaskCreateUniversal(loopTask, "loopTask", 8192, NULL, 1, &loopTaskHandle, CONFIG_ARDUINO_RUNNING_CORE);
}

So there's no reason not to replicate that to give yourself an entire new setup and loop, or just a single function that does it all if you prefer.

TaskHandle_t secondPidTaskHandle;

void secondPidLoop() {
    // setup code here
    for (;;) {
        // PID loop code here
    }
}

void setup() {
    xTaskCreateUniversal(secondPidLoop, "secondPidLoop", 8192, NULL, 1, &secondPidTaskHandle, 0);
}

void loop() {
    // First PID code
}

The last parameter to xTaskCreateUniversal() is the core to execute on. The default setup/loop runs on core 1:

#define CONFIG_ARDUINO_RUNNING_CORE 1

So just use 0 and it'll run it on core 0.

| improve this answer | |

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.