11 years, 1 month ago.

mbed DSP Library PID Controller

Does anybody know how to actually use the PID controller code in the CMSIS DSP library? I have some experience with PID controllers (I wrote my final report on them in school), but I can't figure out how this one actually works... Where do I set the set point? I'm trying to build a microcontroller regulated boost converter, and I need the code to be as efficient as possible since the microcontroller will be doing other things as well. I've got the circuit more or less working with this library, but I'd still like to get the CMSIS library to work if I could:

Import libraryPID

Proportional, integral, derivative controller library. Ported from the Arduino PID library by Brett Beauregard.

Question relating to:

1 Answer

11 years, 1 month ago.

Solved it through brute force experimentation. It looks like the "input sample" argument for arm_pid_f32() is actually the error. Hence, the proper syntax is float out = arm_pid_f32(&pid, set_point - process_variable). I've attached my code here:

CMSIS-DSP PID Code

#include "mbed.h"
#include "dsp.h"

Ticker ticker;
AnalogIn fb(P0_11);
PwmOut sw(P0_8);
arm_pid_instance_f32 pid;
float set_point = 0.19829462;

void controlLoop(void)
{
    //Process the PID controller
    float out = arm_pid_f32(&pid, set_point - fb.read());
    
    //Range limit the output
    if (out < 0.0)
        out = 0.0;
    else if (out > 0.9)
        out = 0.9;
    
    //Set the new output duty cycle
    sw = out;
}

int main()
{
    //Set the switch period to 2µs (500kHz)
    sw.period_us(2);

    //Set the initial duty cycle to 0%
    sw = 0.0;
    
    //Initialize the PID instance structure
    pid.Kp = 1.0;
    pid.Ki = 1.0;
    pid.Kd = 0.0;
    arm_pid_init_f32(&pid, 1);

    //Run the PID control loop every 10ms
    ticker.attach(&controlLoop, 0.01);

    while(1) {
        //Nothing to do here (except sleep maybe)
    }
}

Accepted Answer

I am running your code with gyro and trying to stabilize one axis using gyro, however when I start moving (perturbing) my system, pid output goes to zero and does not change anymore, even though sensor can have extreme values. It just stops reacting. Have you ever experienced something similar ?

posted by Mateusz Kaduk 11 Jul 2015

Hi Mateusz. Actually, I gave up on this library for similar reasons. I'm surprised by how little documentation and support there is for the CMSIS-DSP library...

posted by Neil Thiessen 11 Jul 2015

I don't think there is even "little" support. Sometimes I can't even post on forum... The idea of mbed platform was very nice, but how it is maintained it's a very different story :(

posted by Mateusz Kaduk 15 Jul 2015

You may have used this CMSIS-DSP reference, but if not, here it is: http://www.keil.com/pack/doc/CMSIS/DSP/html/group__PID.html

posted by Nathan Hopkins 06 Dec 2016

Is there any limit on number of PID instance we can create?

posted by Kapil Rawat 20 Mar 2019