Home
Downloads
Licenses Contact

OshonSoft

20th Anniversary

2021 - 20th Anniversary of the
OSHONSOFT PROJECT
OshonSoft © 2001 - 2024

PID Controller implementation
(with the auto tuning feature)

The new OshonSoft PID Controller and PID Auto Tuning libraries for the integrated basic compilers in AVR Simulator IDE, PIC Simulator IDE, PIC18 Simulator IDE and PIC16 Simulator IDE have been developed.
It is now possible to easily implement reliable PID controllers with the functional auto tuning feature.
All that can be effectively simulated with the PID Controller Process Simulator tool.
Detailed information about all the libraries declarations can be found in the Libraries Info Panel.


• OshonSoft PID Controller Library

The library contains implementation of the PID_Init, PID_SetTunings and PID_Compute statements.

Both PID_Init and PID_Compute statements require the current value of the process variable and it should be assigned to the PIDinput library variable. Also, the user basic program should insure that the library variable PIDtimeMS always contains the real time value in milliseconds.

The PID_Init statement should be called once, before starting regular calls to PID_Compute. PID_Init has one argument - the initial process variable setpoint value, that will be loaded to the PIDsetpoint library variable. Later, if needed, the setpoint can be changed using the PIDsetpoint variable.

Immediately after the PID_Init statement, one should use PID_SetTunings statement to set the initial (or desired) values for the PID tuning parameters. It has three arguments containing the new values for the proportional gain (Kp), the integral gain (Ki) and the derivative gain (Kd). PID_SetTunings statement can be used later again to change the tuning parameters.

The PID_Compute statement has no arguments. It should be called regularly by the user basic program. The actual PID computation will take place only if the PID_sampleTime interval has passed since the last computation. Otherwise, PID_Compute exits immediately. After every actual PID computation, the value in the PIDoutput control variable will be updated and the user basic program should 'forward' that value to the process being controlled.

Before using the PID Controller library statements, the library parameters should be properly set.

PID_outMin - This parameter defines the minimal value for the PIDoutput variable, that is evaluated according to the PID algorithm by the PID_Compute statement. (allowed range: 0-4095; default: 0)
PID_outMax - Defines the maximal value for the PIDoutput variable. (allowed range: 0-4095; default: 255)
PID_sampleTime - Sets the desired time interval in milliseconds between two consecutive PID computations. (allowed range: 100-10000; default: 500)
PID_ctrlAction - The parameter determines the control action type, direct (1) or reverse (2). The process is direct acting if an increase in the output causes an increase in the input. (allowed range: 1-2; default: 1)

When the PID_ctrlAction parameter is set to the reverse control action, the computed value in the PIDoutput control variable will be negative. The value of the PID_outMax parameter is expected to be added to the PIDoutput to get the proper value that can be used to control the process.


• OshonSoft PID Auto Tuning Library

The library contains implementation of the PID_AutoTuningInit and PID_AutoTuningRunning statements.

The auto tuning implemented in the library is based on the so-called 'Relay method' (wiki). While the auto tuning operation is running, the output is switched between two values of the control variable (pidAToutputStart + pidAT_outStep) and (pidAToutputStart - pidAT_outStep). The values must be chosen so the process will cross the setpoint (PIDsetpoint) that is previously set by the PID_Init statement. That will result in the process variable oscillations around the setpoint. For actual processes, by choosing suitable values, dangerous oscillations can be avoided.

Before using the PID Auto Tuning library statements, the library parameters should be properly set.

pidAT_ctrlType - This parameter defines the choice for the tuning gains calculation at the end of the operation between the PI controller (proportional-integral controller) (value 0) and the full PID controller (value 1). (allowed range: 0-1; default: 1)
pidAT_noiseBand - Sets the level of noise present in the process variable value. The PID output switching occurs when the process variable crosses the setpoint value. By using the correct pidAT_noiseBand value multiple (and false) crossings will be avoided. The result is the correct auto tuning operation. (allowed range: 0-10; default: 0.5)
pidAT_inputMax - Defines the maximal value for the PIDinput process variable. (allowed range: 0-4095; default: 100)
pidAT_outStep - This parameter will be used to change the initial control variable value (pidAToutputStart library variable) in both directions to create the desired oscillations. (allowed range: 0-1000; default: 30)
pidAT_sampleTime - Sets the desired time interval in milliseconds between two actual consecutive PID_AutoTuningRunning statement executions. (allowed range: 100-10000; default: 250)
pidAT_timeout - Defines the timeout interval (in seconds) that the PID_AutoTuningRunning statement must wait for the process variable to cross the setpoint value, before stopping the auto tuning operation with the error code 3. (allowed range: 10-1800; default: 180)
pidAT_ctrlAction - The parameter determines the control action type, direct (1) or reverse (2). Generally, it should have the same value as the PID_ctrlAction parameter. (allowed range: 1-2; default: 1)

Both PID_AutoTuningInit and PID_AutoTuningRunning statements require the current value of the process variable and it should be assigned to the PIDinput library variable. Also, the user basic program should insure that the library variable PIDtimeMS always contains the real time value in milliseconds.

The only argument of the PID_AutoTuningInit statement is the starting value for the PIDoutput control variable that will be stored in the library defined pidAToutputStart.

The PID_AutoTuningInit statement should be called once, before regular calls to the PID_AutoTuningRunning start.

Before using PID_AutoTuningInit, the PID_Init statement from the PID Controller library should be executed to preset the PIDsetpoint variable and perform the other needed setups.

The PID_AutoTuningRunning statement has no arguments. It should be called regularly by the user basic program. The actual statement execution will take place only if the pidAT_sampleTime interval has passed since the last execution. Otherwise, PID_AutoTuningRunning exits immediately. After every actual statement execution, the value in the PIDoutput control variable could be updated, so the user basic program should 'forward' that value to the process being controlled.

While the auto tuning operation is in progress, the pidATrunning library variable will have the value 1. Once the operation has been completed, the pidATcompleted variable will be set to 1. In case of an error that canceled the auto tuning process, pidATerror will get a positive value representing the error code.

If (pidAToutputStart + pidAT_outStep) or (pidAToutputStart - pidAT_outStep) values are beyond the limits defined by the PID_outMin and PID_outMax from the PID Controller library, the error codes in pidATerror will be 1 or 2. The error code 3 will be returned if it times out while waiting the process variable to cross the setpoint value.

When the pidATcompleted variable is finally set to 1 (the auto tuning has been completed), the calculated gains will be stored in the PID Auto Tuning Library variables pidATKp, pidATKi and pidATKd.


• PID Controller Process Simulator

This tool can be used to effectively simulate the real operation and the auto tuning feature of the PID controller that is implemented using the OshonSoft PID Controller and PID Auto Tuning libraries for the OshonSoft basic compilers.

When the Process Simulator tool is opened and the simulation started, it automatically loads the addresses of the PID Controller library defined variables PIDtimeMS, PIDsetpoint, PIDinput and PIDoutput to be able to interact with those values during the code simulation.

PIDtimeMS is continuously updated with the (faster) simulated real time values.

The value in the PIDoutput variable is the output of the PID controller and it is used to calculate the changes of the process variable with the simulated time based on the defined process parameters.

The current value of the process variable is continuously loaded into the PIDinput variable that is used as the input from the process in the PID controller basic program implementation.

The desired value of the setpoint stored in the PIDsetpoint variable by the PID controller will be plotted on the display, together with the PIDinput and PIDoutput values, to create the function graphs.

The Process Simulator tool keeps the process variable in the range 0-100.

The following setup parameters are available:


(screenshot)

Clock cycles for 1ms - The number of clock cycles that will represent 1ms of the simulated real time. The calculated (faster) real time in milliseconds will be continuously loaded into the PIDtimeMS global variable declared by the PID Controller library for the purpose of the code simulation. (allowed range: 10-1000; default: 100)

Display interval (seconds) - The time interval (in seconds, simulated real time) that will be used for the PID Controller Process Simulator display interval. (allowed range: 10-200; default: 60)

Max process output change (per sec) - The process variable is in the range 0-100. This parameter defines the maximal process output change per second of the simulated real time at the full drive from the PID controller output (Process variable = 0, PID output = Max). The value is scaled down as the process variable approaches the value corresponding to the PID output. The higher the parameter value, the faster process output response. (allowed range: 3-100; default: 30)

Process output change step time (ms) - The process variable is not updated in every cycle. This parameter defines the simulated real time step that will be used as the period for the process variable updates during the code simulation. (allowed range: 1-100; default: 10)

The simulated process response to the PID controller output can be considered to be generally proportional, although the proportional factor is variable. With the help of the following two parameters the simulated process can become more similar to some real systems, with the significant integral and derivative influence.

Process inertness degree - The process inertness here represents the inherent delay of the process response in relation to the output of the PID controller. The higher the parameter value, the higher system inertness. Set to 0 for no inertness. (allowed range: 0-300; default: 100)

Process output smoothing degree - The process output smoothing can be used to get a smooth process response to the changes of the PID controller output. The higher the parameter value, the higher smoothing effect. Set to 0 for no smoothing. (allowed range: 0-200; default: 50)

The last two degree units are relative. The effect also depends on the 'Process output change step time' value.

Process control action - Direct acting process (1) or Reverse acting process (2) (allowed range: 1-2; default: 1)

Desired action when the display is full - Switch the simulation to the Step By Step mode (1) or Clear the display and continue the simulation (2) (allowed range: 1-2; default: 1)


• And Now, Some Practical Simulation Tests

Test Example 1

basic program:
#define PID_outMin = 0
#define PID_outMax = 255
#define PID_sampleTime = 500
#define PID_ctrlAction = 1

#define pidAT_ctrlType = 1
#define pidAT_noiseBand = 0.5
#define pidAT_inputMax = 100
#define pidAT_outStep = 80
#define pidAT_sampleTime = 250
#define pidAT_timeout = 10
#define pidAT_ctrlAction = 1

PID_Init 61.23
PID_SetTunings 1, 1, 0

mainloop:
    PID_Compute
    If PIDtimeMS > 25000 Then Goto mainloopexit
Goto mainloop
mainloopexit:

PID_AutoTuningInit PIDoutput 'use current value
mainloop2:
    PID_AutoTuningRunning
    If pidATcompleted = 1 Then Goto mainloopexit2
    If pidATerror > 0 Then Goto mainloopexit2
Goto mainloop2
mainloopexit2:

End

The basic program will first start the PID controller with the conservative gain settings: 1, 1, 0.
After 25 seconds of the simulated real time, the auto tuning will be started. After the operation has been completed the detected infinite loop will stop the simulation.
The calculated gains will be stored in the PID Auto Tuning Library variables pidATKp, pidATKi and pidATKd.
For the best performance the Basic Program Tracking option should be turned off, the Ultimate simulation rate selected (main simulator window) and the Track Variable Write Access option also turned off (Watch Variables window).


(screenshot)

Test Example 2

basic program:
#define PID_outMin = 0
#define PID_outMax = 255
#define PID_sampleTime = 500
#define PID_ctrlAction = 1

PID_Init 61.23
PID_SetTunings 6.07, 2.21, 4.17

Dim phase As Byte
phase = 1

mainloop:
    PID_Compute
    Select Case phase
    Case 1
        If PIDtimeMS > 20000 Then
            PIDsetpoint = 80
            phase = 2
        Endif
    Case 2
        If PIDtimeMS > 35000 Then
            PIDsetpoint = 30
            phase = 3
        Endif
    Case 3
        If PIDtimeMS > 50000 Then
            PIDsetpoint = 50
            phase = 4
        Endif
    Case 4
        If PIDtimeMS > 61000 Then Goto mainloopexit
    EndSelect
Goto mainloop
mainloopexit:

End

The second test will start the PID controller with the auto tuned gain settings: 6.07, 2.21, 4.17.
The difference in the process control will be obvious. And the program can now test the process control reaction to the changes of the PIDsetpoint library variable at the appropriate simulated real time points.
When the display is full, the simulation will be switched to the Step By Step mode.
On the process simulation graph it can be seen that the auto tuning feature provided very usable calculated gains for the PID control of the simulated process.


(screenshot)

Test Example 3

basic program:
#define PID_outMin = 0
#define PID_outMax = 255
#define PID_sampleTime = 500
#define PID_ctrlAction = 1

#define pidAT_ctrlType = 1
#define pidAT_noiseBand = 0.5
#define pidAT_inputMax = 100
#define pidAT_outStep = 80
#define pidAT_sampleTime = 250
#define pidAT_timeout = 10
#define pidAT_ctrlAction = 1

PID_Init 40 'value for PIDsetpoint
PID_AutoTuningInit 100 'value for pidAToutputStart
mainloop2:
    PID_AutoTuningRunning
    If pidATcompleted = 1 Then Goto mainloopexit2
    If pidATerror > 0 Then Goto mainloopexit
Goto mainloop2
mainloopexit2:

PID_SetTunings pidATKp, pidATKi, pidATKd
PIDsetpoint = 80
mainloop:
    PID_Compute
    If PIDtimeMS > 61000 Then Goto mainloopexit
Goto mainloop
mainloopexit:

End

The auto tuning operation can be started immediately, without any previous time of process control with the PID algorithm.
As can be seen on the Watch Variables window, the calculated pidATKp, pidATKi and pidATKd tuning gains are very similar to the first results.
The basic program applied the obtained new gains, and started the PID process control.


(screenshot)