idf-extra-components icon indicating copy to clipboard operation
idf-extra-components copied to clipboard

pid_ctrl: support use IQmath to compute (IEC-54)

Open lijunru-hub opened this issue 2 years ago • 3 comments

Is your feature request related to a problem?

No response

Describe the solution you'd like.

int bldc_pid_operation(bldc_pid_t* pid, int currentVale)
{
    _iq16 q16Up, q16Ui, q16Ud;

    currentVale = ABS(currentVale);

    pid->error = (pid->SetPoint - currentVale);

    q16Up = _IQ16mpy(_IQ16(pid->P), _IQ16(pid->error));
    q16Ui = _IQ16mpy(_IQ16(pid->I), _IQ16(pid->error));
    q16Ud = _IQ16mpy(_IQ16(pid->D), _IQ16(pid->error - pid->error_prev));

    pid->Up = _IQ16toF(q16Up);
    pid->Ui += _IQ16toF(q16Ui);
    pid->Ud = _IQ16toF(q16Ud);
    LIMIT_OUT(pid->Ui, pid->UiMax, pid->UiMin);

    float output = pid->Up + pid->Ui + pid->Ud;
    LIMIT_OUT(output, pid->OutMax, pid->OutMin);
    pid->error_prev = pid->error;
    return (int)output;
}

Describe alternatives you've considered.

No response

Additional context.

In chips like the esp32c3 that lack a floating-point computation unit, using floating-point calculations can incur significant system overhead

lijunru-hub avatar Sep 21 '23 03:09 lijunru-hub

After testing, it seems that modifying it to use IQmath calculations does not accelerate the PID computation time. Using IQmath on the ESP32C3 is actually slower than not using it.

It's worth mentioning that with the same code, the computation speed on ESP32S3 is 11 times faster than on ESP32C3. Is there a way to make the computation on ESP32C3 faster?

image

lijunru-hub avatar Sep 21 '23 07:09 lijunru-hub

On ESP32-C2, C6 and P4, there is an alternative floating point library (rvfplib) in ROM. You can try enabling it using CONFIG_COMPILER_FLOAT_LIB_FROM_RVFPLIB option in menuconfig.

(You can also post a link to your benchmarking code, if you don't have time to try rvfplib.)

If enabling this option helps, we can open a feature request in IDF to support rvfplib also on chips which don't have it in the ROM (C3, H2).

igrr avatar Oct 04 '23 09:10 igrr

On ESP32-C2, C6 and P4, there is an alternative floating point library (rvfplib) in ROM. You can try enabling it using CONFIG_COMPILER_FLOAT_LIB_FROM_RVFPLIB option in menuconfig.

..., we can open a feature request in IDF to support rvfplib also on chips which don't have it in the ROM (C3, H2).

Yes, iggr, please do open that feature request (to support RVfplib on the C3, etc) ! Floating point operations are fundamental (i.e. this need goes way beyond any particular component, like pid_ctrl), so this makes so much sense given that chips like the C3 don't have it in ROM.

https://www.research-collection.ethz.ch/handle/20.500.11850/582612

who-has-seen-the-wind avatar Oct 11 '23 19:10 who-has-seen-the-wind