PID Controller for the Lego Mindstorms Ev3

The Proportional Integrative Derivative or PID controller is one of the simpler, if not the simplest, yet one of the most effective control algorithm ever designed; it’s also one of the most popular: from modern cars, to dish washers, to airliners you can safely bet that somewhere inside a PID controller is keeping the things running the correct way.

The purpose of the PID controller is mainly to be a regulator; given a system that has one input and one output signal, a PID controller modulates the input signal to the system to keep its output as close as possible to a given reference. The reference is not supposed to vary, but if it does it must vary slower than the overall system dynamics.

A bit of math about the PID Controller

If you want just to grab the code, see here!

Consider the scenario described in the following picture: a discrete plant P has an input signal u_k and an output signal y_k tied together by some sort of equation y_k=f(u_k) ,
where the independent variable k represents the succession of discrete instants or discrete time instants.

Classical representation of a discrete control loop
Classical representation of a discrete control loop

The controller C takes as input the regulation error e_k=r_k - y_k and computes the control signal u_k that is also the input to the plant P. In the case of the PID controller, u_k is calculated as follows:

u_k = K_p e_k + K_i \sum_{i=0}^{k}{e_i} + K_d (e_k - e_{k-1}),

where:

  • K_p is the proportional constant; the controller reacts to the error proportionally to this constant. The greater is the regulation error, the greater is the controller reaction to it. The smaller is the regulation error, the smaller is the reaction. Ideally when the error goes to zero, also the reaction reduces to zero. However if there is even a tiny perturbation to the model, or if there is some sort of disturbance, the proportional controller alone it’s not enough to lay the output upon the reference.
  • K_i is the integral constant; the controller reacts to historical values of the error proportionally to this constant. The greater is the sum of all the error values, the greater is the controller reaction. When the sum of all the error values compensates itself to a smaller value, the controller reaction becomes smaller. In other words, the integral controller reacts to the length of the time interval during which there has been error multiplied by the intensity of the error accumulated during that interval. Or in some other words, this constant express the reaction of the controller to the memory of the regulation error.
    The integral controller is generally very capable to react to the effects of external disturbance.
  • K_d is the derivative constant; the controller reacts to the direction of change of the error before the effects can accumulate to wake up the integral controller. A greater value for this constant makes the controller to react faster. In other words this constant express the impatience of the controller: the greater is the K_d, the greater is the impatience of the controller to react; however if the constant is too big, the controller can react to measurement noise and the output can become shaky.

The PID controller can be viewed as three controllers working in parallel as the following picture better explains:

Representation of a discrete PID controller as three independent element acting in parallel
Representation of a discrete PID controller as three independent elements acting in parallel

The picture expresses the concept that according to the needs one, two or all the three controllers can be used by selectively zeroing the constants corresponding to the effects that you don’t want to use.

So, how to use this controller on board our small robots?

The equation of the controller shown above cannot be implemented in practice. As you may have noticed, the equation contains a sum that must be computed at each step, whose number of addenda becomes bigger and bigger at each iteration, ideally infinite. What to do?

Check Part 2!

PID Controller for the Lego Mindstorms Ev3