The PID Controller – Part 3 of 4

← Back to Part 1 – Introduction and some math
← Back to Part 2 – Some more math 🙂
Part 4 – Application to the Ev3 Medium Motor position regulation

As described in Part 2, we need a new  PIDController class that performs the following computation:

K_1 = K_p + K_i + K_d ,
K_2 = -K_p - 2K_d,
K_3 = K_d,
e_k = r_k - y_k,
\Delta u_k = K_1 e_k + K_2 e_{k-1} + K_3 e_{k-2},
u_k = u_{k-1} + \Delta u_k.

You can compile the code I propose in this page using Xamarin Studio 5.10.1 (Build 6), with Mono 4.2.1 as active runtime and the Monobrick Firmware Library 1.2.0.39486 to let it work on board the Lego Mindstorms Ev3 with the Monobrick firmware.

Properties

So first we define the main properties: the InputSignal, the OutputsSignal and the SetPoint. As you may notice the OutputSignal property is set as overridable in derived classes, to allow any useful post-computation you may think of.

Next, we need to set up the SampleTime, the MinPower, and the MaxPower.
The SampleTime is expressed in milliseconds (one thousandth of second) and represent the time interval between two successive algorithm executions; choosing the correct sampling time for your process goes beyond the scope of this article, but as a general rule of thumb if you think that the system under control is phisically able to perform its average task in a time T ( i.e. if T is the expected or desired settling time) then choosing
T_s \in [\frac{T}{50}; \frac{T}{40}]
can do the trick.
MinPower and MaxPower define the range in which the OutputSignal is allowed to vary; if for any reason the OutputSignal calculated by the controller grows out of the allowed range, its value gets trimmed to the violated boundary.

The last properties to be defined are the three Ks: the proportional constant K_p, the integrative constant K_i, and the derivative constant K_d. The setter of each of these properties calls  UpdateKs() to update the K_1K_2, and K_3.

Fields

The fields are just K_1K_2, and K_3, the main thread and its wait handle as follows:

Constructors

The  PIDController class defines just the default constructor for fields initialization:

Public methods

The public methods of the  PIDController start and stop the  PIDThread .

Private methods

The PIDController  class has just two private methods: a very simple  UpdateKs()  to calculate K_1K_2 and K_3, and the  PIDThreadComputation() which in the end score the goal; note how the control signal is saturated to the boundaries before assignment to the output.

The whole code

Here comes the full code. This page will be shortly updated with a link to a zip file to download. You can compile the above code using Xamarin Studio 5.10.1 (Build 6), with Mono 4.2.1 as active runtime and the Monobrick Firmware Library 1.2.0.39486.

Check Part 4 for the application of a PID Controller to the position regulation of the Ev3 Medium Motor

The source code happily shared above is subject to the Code Project Open Licence (CPOL) 1.02.

The PID Controller – Part 3 of 4
Tagged on: