Use your forum account to login.

LOGIN

TOP Contributors

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

Most Downloaded

  1. Timer Calculator (71770 times)
  2. Network Ethernet Library (35698 times)
  3. FAT32 Library (34033 times)
  4. USB Device Library (26847 times)
  5. Network WiFi Library (26174 times)
  6. FT800 Library (18529 times)
  7. PID Library (18080 times)
  8. GSM click - Example (17915 times)
  9. microSD click - Example (15146 times)
  10. GPS click - Example (12100 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: 18080 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

PIC32 Interrupts, article

Small article about PIC32 interrupts.

[Learn More]

Keypad_I2c

Library for a 4x4 keypad connected to the PIC via an PCF8574 I2c expander (Two I2c lines in stead of 8 I/O pins). This saves a lot of PIC I/O pins. Permits also small PIC's (with low pin count) to control a keypad.

[Learn More]

Seven Segment

Library to drive 4 seven segment digits with common anode. High level routines are provided, next to the low (raw) level ones. Nothing fancy...

[Learn More]