TOP Contributors

  1. MIKROE (2638 codes)
  2. Alcides Ramos (347 codes)
  3. Shawon Shahryiar (307 codes)
  4. jm_palomino (112 codes)
  5. Chisanga Mumba (90 codes)
  6. S P (73 codes)
  7. dany (71 codes)
  8. MikroBUS.NET Team (35 codes)
  9. NART SCHINACKOW (34 codes)
  10. Armstrong Subero (27 codes)

Most Downloaded

  1. Timer Calculator (136089 times)
  2. FAT32 Library (69394 times)
  3. Network Ethernet Library (55671 times)
  4. USB Device Library (45934 times)
  5. Network WiFi Library (41605 times)
  6. FT800 Library (40698 times)
  7. GSM click (28759 times)
  8. PID Library (26312 times)
  9. mikroSDK (25989 times)
  10. microSD click (25087 times)
Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

< Back
Library

PID Library

Rating:

25

Author: dany

Last Updated: 2020-05-08

Package Version: 2.0.0.0

Category: Other Codes

Downloaded: 26312 times

Followed by: 6 users

License: MIT license  

PID library with fixed calculation time interval.
A PID Usage example is added.
Octal delivered the mB and an mC version.

No Abuse Reported

Do you want to subscribe in order to receive notifications regarding "PID Library" changes.

Do you want to unsubscribe in order to stop receiving notifications regarding "PID Library" changes.

Do you want to report abuse regarding "PID Library".

  • Information
  • Comments (9)

Library Blog

A PID library with a fixed calculation time interval.

Available libraries:

- PID_Lib

Interface:

type TPIDController =
     record
       PID_Kp, PID_Ki, PID_Kd: real;
       PID_Integrated : real;
       //PID_Prev_Input: real; // deleted  05-05-2020
       PID_Prev_Error: real;   // added  05-05-2020
       PID_MinOutput, PID_MaxOutput: real;
       // PID_First_Time, // deleted 5-5-2020
       PID_Int_Improvement, PID_Diff_Improvement: boolean;
       PID_Prev_AbsError: real;
       PID_Offset: real;
       PID_Min_Value, PID_Max_Value: real;
     end;

procedure Init_PID(var Controller: TPIDController; Kp, Ki, Kd, MinOutput, MaxOutput: real);
// Initialises the PID engine of "Controller"
// 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  // changed 05-05-2020
// MaxOutput = the maximal value the output can have        // changed  05-05-2020

procedure Set_PID_Int_Improvement(var Controller: TPIDController; On_: boolean);
// Switches on or off the "Integration Improvement" mechanism of "Controller". This mechanism prevents overshoot/ringing/oscillation
// due to integration. To be used after "Init_PID", which switches off the mechanism for compatibility reasons.

procedure Set_PID_Diff_Improvement(var Controller: TPIDController; On_: boolean);
// Switches on or off the "Differentiation Improvement" mechanism of "Controller". This mechanism prevents unneccessary correction
// delay when the actual value is changing towards the setpoint.
// To be used after "Init_PID", which switches off the mechanism for compatibility reasons.

procedure Reset_PID(var Controller: TPIDController);
// Re-initialises the PID engine of "Controller" without change of settings

function  PID_Calculate(var Controller: TPIDController; Setpoint, InputValue: real): real;
// 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
// Functionresult: PID function of (SetPoint - InputValue) of "Controller",
//   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
// Timing: A call to this routine costs 140 uS time (P18F2550 running at 48 Mhz)

implementation


Have fun!

-----------------------------------------------------------------------------------------------------------------------------------------------

The "PID_Usage_Example":

 This program uses AN1 (pin 3) to read the process output value (value across C3, see schematic diagram).
 Every 6 secs the process output becomes disturbed (jump in load driven by portA.0, pin 2).
 The PID controller tries to keep the process output value stable via PWM output CCP2 (pin 12),
  this control activity occurs every 10 milliseconds.

 The "process" has also some characteristics:
  - it has an hardware time contant of 47 millisecs (R1 * C3), with a high spread (47 is the minimum, the maximum can be as high as 150 ms).
  - It can handle only correction values (coming from the PID controller) of -100..+100. These value is added to the "dutycycle" value of the PWN giving the process output.
    Since only every 10 millisecs the dutycycle value can he lowered/highened with max 100, it takes approximately 10 times 10 millisecs to change the duty cycle from minimum to maximum.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

"PID Usage Example" Schematic

"PID Usage Example" Schematic

The PWM with its smoothing circuit (R1/C3) and its switching load (R2). The process output voltage is accross C3.

View full image
"PID Usage Example" Process behaviour with Kp = 0.1, Ki = 0.0, Kd = 0.0

"PID Usage Example" Process behaviour with Kp = 0.1, Ki = 0.0, Kd = 0.0

Only Kp (proportional) is used here. The value is kept too low for this process, especially to show the residual error. In this and following images the blue graph is the process output voltage (across C3), and the red graph is PortA.0 (load switching).

View full image
"PID Usage Example" Process behavour with Kp = 0.1, Ki =0.01, Kd = 0.0

"PID Usage Example" Process behavour with Kp = 0.1, Ki =0.01, Kd = 0.0

Here there is some (rather high value) of the integration factor added. The resudual error is gone, but a lot of ringing on the output voltage occurs.

View full image
Same as above, but with Integration Improvement switched ON

Same as above, but with Integration Improvement switched ON

Here one can see the ringing behaviour is mostly gone.

View full image
"PID Usage Example" Process behaviour with Kp = 0.1, Ki = 0.1, Kd = 0.0

"PID Usage Example" Process behaviour with Kp = 0.1, Ki = 0.1, Kd = 0.0

Here the value of Ki is chosen too high with respect to Kp, resulting in an "oscillating" output.

View full image
Same as above, but with Integration Improvement switched ON

Same as above, but with Integration Improvement switched ON

Here one can see there is no oscillation any more, the stability is not good however.

View full image
"PID Usage Example" Process behaviour with Kp = 0.1, Ki = 0.005, Kd = 0.0

"PID Usage Example" Process behaviour with Kp = 0.1, Ki = 0.005, Kd = 0.0

Here Ki is chosen such that the residual error is gone and the ringing is rather limited.

View full image
Same as above, but with Integration Improvement switched ON

Same as above, but with Integration Improvement switched ON

Also here the overshoot/ringing is much better.

View full image
"PID Usage Example" Process behaviour with Kp = 0.1, Ki = 0.005, Kd = 1.0

"PID Usage Example" Process behaviour with Kp = 0.1, Ki = 0.005, Kd = 1.0

Here also the differential factor is added with respect to the previous image. Observe the better stability.

View full image
Same as above, but with Integration Improvement switched ON

Same as above, but with Integration Improvement switched ON

Also here the overshoot/ringing is much better.

View full image

------------------------------------------------------------------------------------------------------------------------------------------------

Addition on 2011-09-30: the mikroBasic version (zip file), made by "octal", see

http://www.mikroe.com/forum/viewtopic.php?f=148&t=44638

------------------------------------------------------------------------------------------------------------------------------------------------

An improvement in the PID library has been added: prevention of overshoot/instability/oscillation due to Integration actions.

Following procedure has been added:

procedure Set_PID_Int_Improvement(var Controller: TPIDController; On_: boolean);
// Switches on or off the "Integration Improvement" mechanism of "Controller". This mechanism  prevents overshoot/ringing/oscillation due to integration. To be used after "Init_PID", which switches off the mechanism for compatibility reasons.


If the procedure above is not called at all (or called with parameter "false"), then the PID library behaves exactly as before. This is done for compatibility with the previous lib version.

See above the images of the process behaviour with this improvement switched on are shown.

-------------------------------------------------------------------------------------------------------------------------------------------------

2011-10-12: The PID Lib has been extended with the possibility to make more than one PID controller in a program.

-------------------------------------------------------------------------------------------------------------------------------------------

2011-10-17: added the C version from Octal (see http://www.mikroe.com/forum/viewtopic.php?f=88&t=44901&start=2).

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.


Thanks Octal!

-------------------------------------------------------------------------------------------------------------------------------------------------
2011-10-17: A PIC24 example has been added.

Attention: the PWM output pin is not the same as in the P18 example circuit diagram. The oscillator is an internal one here, so no crystal required.

========================================================================

Version 2.0.0.0: added the "Differential behaviour" improvement to the PID controller in the Pascal versions.

Following procedure has been added:

procedure Set_PID_Diff_Improvement(var Controller: TPIDController; On_: boolean);
// Switches on or off the "Differentiation Improvement" mechanism of "Controller". This mechanism prevents unneccessary correction delay when the actual value is changing towards the setpoint.
// To be used after "Init_PID", which switches off the mechanism for compatibility reasons.


If the procedure above is not called at all (or called with parameter "false"), then the PID library behaves exactly as before. This is done for compatibility with the previous lib version.


The mB and mC PIC versions do not have yet the "Integration" and "Differential" behaviour improvements.

A small explanation about these 2 improvements.

Have fun!

------------------------------------------------------------------------------------------------------------------------------------------------
2012-11-12: The microBasic package(s) ha(s)(ve) been added.

Only the .mcl files are available as "library" files: they are copied from the mikroPascal version (the mP and mB .mcl files are compatible - luckely - ).

If you want to see some sources: look into the mP versions.

The example files are still in mP, not in mB. (I do not have an mB licence, so I can not compile them).

Some of the helpfiles are also still referring to mP, but all library "interfaces" are available in the mB language.

-------------------------------------------------------------------------------------------------------------------------------------------------
2013-02-07: published the dsPIC packages.

----------------------------------------------------------------------------------------------------------------------------------------

2014-03-13: The mikroBasic files and zip files have been updated. All mB sources are present now (not tested if compilable though).


2015-01-26: Replaced the mB files: all mB files are compilable now (which e.g. means that all "with" statements are dealt with). I could however not link and test the libraries (no mB licence).


2015-07-04: The example for PIC32 has been updated: it is for the mini32 board now (P32MX534F064H on 80 Mhz).

--------------------------------------------------------------------------------------------------------------------------------------------------

Version 05-05-2020: Added an output 'offset' calculated from Min and Maxoutput (made output symmetrically)

ALSO FROM THIS AUTHOR

Schedulers

11

Scheduler (in a broad sense) related libs:
"Timebase"
"Timers"
"COS"
"COS_TimedOnly"
"StateMachine"
"StateMachine_2"

[Learn More]

Tool: PIC uart to USB interface

0

Converts PIC level Uart signals to USB in both directions. This project is meant for use with PC's that do not have an RS232 interface any more. It makes it possible to handle the pic uart signals with a PC without an RS232 interface.

[Learn More]

Wifi Plus click HTTP Client

0

With this example one can load an internet page from an HTTP server. At this moment the remote IP address is fixed in the code and it loads the default file from the server. All data received from the HTTP server is sent to Uart1 where it can be picked up by a uart program to display or to save to a file.

[Learn More]