Use your forum account to login.

LOGIN

TOP Contributors

  1. MikroElektronika Team (428 codes)
  2. Shawon Shahryiar (243 codes)
  3. Alcides Ramos (96 codes)
  4. Dany (66 codes)
  5. O B (61 codes)
  6. MikroBUS.NET Team (35 codes)
  7. NART SCHINACKOW (34 codes)
  8. Alberto Trasimeni (25 codes)
  9. Roman Toropov (25 codes)
  10. Arezki YOUYOU (24 codes)

Most Downloaded

  1. Timer Calculator (76138 times)
  2. Network Ethernet Library (37331 times)
  3. FAT32 Library (35918 times)
  4. USB Device Library (28029 times)
  5. Network WiFi Library (26891 times)
  6. FT800 Library (19535 times)
  7. PID Library (18697 times)
  8. GSM click - Example (18365 times)
  9. microSD click - Example (15685 times)
  10. GPS click - Example (12579 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:

Author: Dany

Last Updated: 2015-07-04

Category: Other Codes

Downloaded: 18697 times

Followed by: 6 users

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;
       PID_MinOutput, PID_MaxOutput: real;
       PID_First_Time, PID_Int_Improvement, PID_Diff_Improvement: boolean;
       PID_Prev_AbsError: 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 (should be < 0)
// MaxOutput = the maximal value the output can have (should be > 0)

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)

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

ALSO FROM THIS AUTHOR

Menus

Menu machine capable of handling text based menus. It only handles the logical behaviour of the menu(s), not their physical appearance e.g. on an LCD: the using project should still define the display or drawing procedures.

[Learn More]

WattMeter (Power meter)

This project is a power meter (Wattmeter). It is made for 230V~ (nominal, rms), 2.5A~ (max, rms) and 50Hz, but is rather easily adaptable to other voltages, currents and frequencies. It can handle both sinusoidal and non sinusoidal voltages and currents, both with and without DC component.

[Learn More]

Tool: User Libraries in the .MLK file

This tool permits to change the order of "user" libraries defined in the .MLK file of a certain processor.
For mP, mC and mB, PIC, dsPIC and PIC32.

[Learn More]