PID library from Ahmed Lazreg and Dany Rosseel

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Bonca
Posts: 319
Joined: 18 Mar 2009 07:55

PID library from Ahmed Lazreg and Dany Rosseel

#1 Post by Bonca » 15 Oct 2011 07:21

Hi!
Due to the high interest I would like to ask some help in translating the PID library written by above mentioned gentlemen. If we find the final solution this could be uploaded to the libstock. (If Ahmed and Daniel agree, of course.)
First, let me show you the basic-C translation. Please help to develop the code together.

I couldn't find the definition of the variable Result in the Basic source, so I defined as global variable. If I made a mistake, please correct it.

Code: Select all

/*******************************************************************************
 ----------------------------------------------------------
 ---- PID library with fixed calculation time interval ----
 ----------------------------------------------------------

 Ahmed Lazreg
 ahmed.lazreg@pocketmt.com

 Translated from original mikroPascal code from D.Rosseel

 D. Rosseel
 Original: 27-09-2011
 Latest update: 27-09-2011

 History:
  29-09-2011: Translation from Pascal Code to MikroBasic
  27-09-2011: Original Version (in mikroPascal).


 Documentation: http://en.wikipedia.org/wiki/PID_controller/ and
 http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/

*******************************************************************************/

Init_PID(float Kp, float Ki, float Kd, float MinOutput, float MaxOutput);
/* Initialises the PID engine
   Kp = the "proportional" error multiplier
   Ki = the "integrated value" error multiplier
   Kd = the "derivative" error multiplier
   MinOutput = the minimal value the output value can have (should be < 0)
   MaxOutput = the maximal value the output can have (should be > 0)
 */

Reset_PID();
/* Re-initialises the PID engine without change of settings */

PID_Calculate(float Setpoint, float InputValue);
/* To be called at a regular time interval (e.g. every 100 msec)
   Setpoint: the target value for "InputValue" to be reached
   InputValue: the actual value measured in the system
   Function: PID function of (SetPoint - InputValue),
     a positive value means "InputValue" is too low (< SetPoint),
     the process should take action to increase it
     a negative value means "InputValue" is too high (> SetPoint),
     the process should take action to decrease it
*/

float PID_Kp, PID_Ki, PID_Kd;
float PID_Integrated;
float PID_Prev_Input;
float PID_MinOutput, PID_MaxOutput;
float Result;
char PID_First_Time;

void Reset_PID() {
    PID_Integrated = 0.0;
    PID_Prev_Input = 0.0;
    PID_First_Time = 1;
}

void Init_PID(float Kp, float Ki, float Kd, float MinOutput, float MaxOutput) {
    PID_Kp         = Kp;
    PID_Ki         = Ki;
    PID_Kd         = Kd;
    PID_MinOutput  = MinOutput;
    PID_MaxOutput  = MaxOutput;
    PID_Integrated = 0.0;
    PID_Prev_Input = 0.0;
    PID_First_Time = 1;
}

void PID_Calculate(float Setpoint, float InputValue) {
   float TheErr, ErrValue, DiffValue;

    TheErr = SetPoint - InputValue;

    // --- calculate proportional value ---
    ErrValue  = TheErr * PID_Kp;

    // --- Calculate integrated value ---
    PID_Integrated = PID_Integrated + (TheErr * PID_Ki);
    // limit it to output minimum and maximum
    if(PID_Integrated < PID_MinOutput then) {PID_Integrated = PID_MinOutput;}
    if(PID_Integrated > PID_MaxOutput) {PID_Integrated = PID_MaxOutput;}

    // --- calculate derivative value ---
    if(PID_First_Time) {  // to avoid a huge DiffValue the first time (PID_Prev_Input = 0)
        PID_First_Time = 0;
        PID_Prev_Input = InputValue;
    }

    DiffValue = (InputValue - PID_Prev_Input) * PID_Kd;
    PID_Prev_Input = InputValue;

    // --- calculate total ---
    Result = ErrValue + PID_Integrated - DiffValue; // mind the minus sign!!!

    // limit it to output minimum and maximum
    if(Result < PID_MinOutput) {Result = PID_MinOutput;}
    if(Result > PID_MaxOutput) {Result = PID_MaxOutput;}
}
Bonca
Last edited by Bonca on 15 Oct 2011 20:25, edited 1 time in total.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: PID library from Ahmed Lazreg and Daniel Rosseel

#2 Post by Dany » 15 Oct 2011 11:01

Bonca wrote:(If Ahmed and Daniel agree, of course.)
Hi, my name is Dany, not Daniel :D. No problem for me, the PID lib I published (mP version) is open source. :D

In the mean time 2 improvements have been made to the PID library (mP version):
- more than one PID controller can be made in the same program,
- an "Integration Improvement" has been added: improves the "I" behaviour (less overshoot, ringing, oscillation etc...).

Success!
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

octal
Posts: 534
Joined: 09 Feb 2005 14:15
Location: France
Contact:

Re: PID library from Ahmed Lazreg and Daniel Rosseel

#3 Post by octal » 15 Oct 2011 16:54

Hi Bonca, Hi Dany(iel :mrgreen:)

@Bonca,
the Result variable is automatically defined by the mikroBasic and mikroPascal compilers. Its type is the return type of the function and it contains the result of the function.
In C language, you don't have it, as C functions return their result using the keyword "return (...)".

As for the PID_Lib, I already does the C version but never posted it. In fact I does it the same time I does Basic one, but never got time to test it.

Here it is. Dany can add it to the Libstock.

@Dany
this is the port of the first version you does, it's equivalent to the mikroBasic version I posted. I'll take time by the start of this week to download your last version and to update both Basic and C versions.

to use this "C" version, just include the header file in your program and dont forgot to add both files (*.c and *.h) to your project.

Best regards
Ahmed Lazreg
Attachments
PIDLib_C.zip
(1.51 KiB) Downloaded 752 times
http://www.pocketmt.com

Bonca
Posts: 319
Joined: 18 Mar 2009 07:55

Re: PID library from Ahmed Lazreg and Dany Rosseel

#4 Post by Bonca » 15 Oct 2011 20:43

Dany
Sorry for mis-writing your name, I have fixed it.

octal
Thank you for explaining and sharing the C source. I hope, everyone will be satisfied who has been looking for it.

Bonca

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: PID library from Ahmed Lazreg and Daniel Rosseel

#5 Post by Dany » 17 Oct 2011 10:58

octal wrote:As for the PID_Lib, I already does the C version but never posted it. In fact I does it the same time I does Basic one, but never got time to test it.
Here it is. Dany can add it to the Libstock.
Best regards
Ahmed Lazreg
Done, see http://www.libstock.com/projects/view/161/pid-library. :-)
Thanks Octal!
Bonca wrote:Dany
Sorry for mis-writing your name, I have fixed it.
Bonca
No problem, more people do that, thanks for the correction. :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Post Reply

Return to “mikroC PRO for PIC General”