Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lijunru-hub opened this issue Sep 21, 2023 · 3 comments
Open

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

lijunru-hub opened this issue Sep 21, 2023 · 3 comments

Comments

@lijunru-hub
Copy link

lijunru-hub commented Sep 21, 2023

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

@github-actions github-actions bot changed the title pid_ctrl: support use IQmath to compute pid_ctrl: support use IQmath to compute (IEC-54) Sep 21, 2023
@lijunru-hub
Copy link
Author

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

@igrr
Copy link
Member

igrr commented Oct 4, 2023

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).

@who-has-seen-the-wind
Copy link

who-has-seen-the-wind commented Oct 11, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants