2

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 | |
1

esp32_technical_reference_manual_en.pdf

Page 620

As mentioned before, only an elevated process with PID of 0/1 can initiate a process switch. The new process may have any PID from 0 ~ 7 after the process switch. The key for successful proactive process switching is that Espressif Systems 620 Submit Documentation Feedback ESP32 Technical Reference Manual V4.3 29. PID Controller when the last command of the current process switches to the first command of the new process, PID should switch from 0/1 to that of the new process. The software procedure for proactive process switching is as follows:

  1. Mask all the interrupts except NMI by using software.
  2. Set register PIDCTRL_NMI_MASK_ENABLE_REG to 1 to generate a CPU NMI Interrupt Mask signal.
  3. Configure registers PIDCTRL_PID_DELAY_REG and PIDCTRL_NMI_DELAY_REG.
  4. Configure register PIDCTRL_PID_NEW_REG.
  5. Configure register PIDCTRL_LEVEL_REG and PIDCTRL_FROM_n_REG.
  6. Set register PIDCTRL_PID_CONFIRM_REG and register PIDCTRL_NMI_MASK_DISABLE_REG to 1.
  7. Revoke the masking of all interrupts but NMI.
  8. Switch to the new process and fetch instruction. Though we can deal with interrupt nesting, an elevated process should not be interrupted during the process switching, and therefore the interrupts have been masked in step 1 and step 2. In step 3, the configured values of registers PIDCTRL_PID_DELAY_REG and PIDCTRL_NMI_DELAY_REG will affect step 6. In step 4, the configured value of register PIDCTRL_PID_NEW_REG will be the new PID after step 6. If the system is currently in a nested interrupt and needs to revert to the previous interrupt, register PIDCTRL_LEVEL_REG must be restored based on the information recorded in register PIDCTRL_FROM_n_REG in step 5. In step 6, after the values of register PIDCTRL_PID_CONFIRM_REG and register PIDCTRL_NMI_MASK_DISABLE_REG are set to 1, PID Controller will not immediately switch PID to the value of register PIDCTRL_PID_NEW_REG, nor disable CPU NMI Interrupt Mask signal at once. Instead, PID Controller performs each task after a different number of clock cycles. The numbers of clock cycles are the values specified in register PIDCTRL_PID_DELAY_REG and PIDCTRL_NMI_DELAY_REG respectively. In step 7, other tasks can be implemented as well. To do this, the cost of those tasks should be included when configuring registers PIDCTRL_PID_DELAY_REG and PIDCTRL_NMI_DELAY_REG in step 3. Espressif Systems 621 Submit Documentation
| 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.