Most robotic applications require Proportional-Integral-Derivative (PID) control at a low level to control several processes. Be it the speed of a rover according to the distance to its target, or the thrust of each propeller of a multirotor to hover in one place.

Regardless of how basic PID control is, it can be sometimes a bit tricky to understand and tune a PID controller.

#### Explanation of a PID controller

###
__Initial setup__

To explain how a PID controller works, I will make use of a simple example. Imagine we have built a space lift. It can travel only vertically, as it is attached to a vertical rail.

Now, in order for the lift to move, imagine we attach a rocket to it (Wile E. Coyote would be proud of us). But, this rocket is special in the sense that the amount of thrust it delivers is completely regulable from 0 to 100%. Being 100% more than enough to shoot up our lift. Also, ¿who needs breaks? If we want stay at a given altitude, we hoover using the rocket's thrust adjusted to compensate exactly gravity's pull.

Because we want our lift to be more-less autonomous, we throw a robot on top of it. His mission is to control the amount of thrust the rocket delivers, and for it, the robot will make use of a PID control. Sometimes the lift has to move more weight or less according to the payload. But do not worry, the PID control will take care of it and auto-adjust accordingly.

Finally, the robot needs to know what our current altitude is, else it woud not know where to get us. For it he has a precise laser pointing down, from which he can read the exact altitude (remember: this is not true in reality, sensors always have noise).

###
__Behaviour__

So, we have our space lift compeltely assembled. When we enter it, the robot asks us what altitude we want to go to and then adjust the rocket exaust. It will deliver more thrust if we want to go up, and less to allow for gravity to return us back down.

It the robot did a simple control of delivering 100% thrust to go up, and 0% down, and then periodicaly check if we have reached our destination to turn of the engine it would be quite problematic.

Imagine we are ascending. We would be traveling at maximum speed when we get to our destination, then the engines would turn off, yet we would continue toa scend because of inertia. Then we would loose momentum and start falling down (again accelerating) and pass our desired destination. Engines would turn on again but our inertia would drag us again and so forth.

This is why a PID controller is so useful. If this time we use a controller, we would start accelerating at maximum at first (for example) and then gradually the controller would start reducing thrust so that when we get to our desired location our speed would be exactly zero. This time inertia does not drag on. Neither do we start falling down again, because the PID controller would be compensating the gravity pull.

###
__Parameters__

## Input

The desired height we want to travel to. It can be established to a constant value the moment we enter the lift and mantain it all the time until we get there, our we could be continually changing it regardless if we have arrived or not.

## Feedback

The current altitude, as measured by our sensor. A PID controller always needs feedback, else it can not adjust itself.

## Error

Its the difference between the input and the feedback. The further away we are from the position we want to reach, the bigger the error. The error can have sign, being positive if we are below our destination, or negative if we are above. Error is continuously reevaluated in time, as input and feedback will be changing.

## PID controller output

The amount of thrust the PID controller is telling the rocket to deliver. The PID controller output is composed of three terms, namely P, I and D, with each depending on a paremeter. These are the Proportional constant (Kp), the Integral constant (Ki) and the derivative constant (Kd). This parameters act individually and their summ is the output of the controller.

###
__PID terms__

The PID controller is
composed of three elements, each of their responses are evaluated
separately for the same input error, and then added together to obtain
the final output.

## Proportional

The output is equal the error times Kp, with other words, the output is linearly proportional to the error. Small errors cause a small autput while big errors cause a big output.In our example this means that the further away we are from the destination themore lift will be demanded from the rocket (or less gravity will be compensated if we are descending)

## Integral

The outpur is equal to the error integrated over time and multiplied by Ki. As long as an error is present, the output of the I term of the controller will continue to increase.In our example this causes the thrust to increase while an error is present. This is useful in two situations:

- It allows the lift to hoover at the desired altitude. When the lift is exactly where we want it to be, the error is zero, thus the P term will cease and the lift would drop back to earth until the proportional part starts to lift us up again. Meanwhile, as long as an error was present, the integral part has been steadily increasing until the I termalone is big enough to compensate gravity. At that point we will reach the desired altitude and only the integral part will hold us in place, as for the proportional part is zero.

- Sometimes the P output is not enough to even get moving. Imagine that we are lifting some really heavy load, and that to get off the ground we need 90% output or more. But, imagine that the error not big enough, so that the P term is telling the engine to only deliver 60%.

Because wit 60% of thrust the lift will not move, there will be a permanent error. This error will be summing up in time and adding to the output as I term, which will eventually reach 30%. 60% + 30% = 90%, enough to get moving. Once the lift starts moving, the error will diminish, causing the P term to fall below 60%, but do not worry, the I term will continue growing to compensate.

## Derivative

the D term of the control is proportional to the ratio of change of the error. The D term tries to limit the rate of change of the error.In our example the D term has the impact of limit how fast we rise or fall. Imagine that we are really far away from our destination and that the lift is empty. The P term tells the engine to run at 100%, and having somuch thrust, the lift will start building up quite some speed. The faster the lift is, the faster the error will diminish, or in other words, the rate of change will be negative (it is gettin lower) but with a high absolute value.So the P term is tellin our lift to accelerate, but because of the big negative rate of change, the D term will kick in and try to reduce the thrust trying to break. In the end an equilibrium will be reached, in which the P term is telling to go up, but the D term limiting how fast. Or in other words, the D term is setting the maximum allowable velocity (rate of change of the error).Also the D term has a useful function. It gives the system inertia, meaning that it compensates for external agents, making it for them more difficult to alte our system. For example, if the lift is hovering at a given altitude, the P term will be zero and the I term will be exactly enough to compensate gravity. If suddenly someone heavy steps in, the lift will fall a bit until the P term kicks back in. But, because of the sudden acceleration caused by the mass of the new passenger, the lift will start moving (non zero velocity) causing the D term to damp this movement.

#### Tuning rule of thumb

A quick reminder of the meaning of each term:

**P:**It's the main actuator, getting stronger as the error does.**I:**Slowly adds up until the error is gone, allowing to get really close to the desired input.**D:**Limits the rate of change, compensating for over actuation of the other terms or external agents.

###
**P term**

**P term**

The output from a system can only go from 0% to 100%. This means there must be a limit for the error from where on bigger errors do not imply an increase or decrease of the output, because it is already saturated. Because the output of the P term depends on the Kp constant, we can move this "limit error" up and down changing the value of Kp.

Determine the maximum error under which you want your system to regulate. Now divide your maximum output (100% of it) between this maximum error, this will be your first approximation of Kp.

In our example, we could say that if we are 20 meters or less away from our destination, we want the P term to act smoothly, if farther away we want the maximum output of it. So Kp = 100/20 = 5. Now, as soon as the lift gets 20 meters close to the destination (from above or below) the P component will gradually cease to act.

###
__I term__

__I term__

I am still trying to figure this one out. Coming soon...

###
__D term __

__D term__

Divide the maximum output by the maximum allowed rate of change (velocity) of your system.

In our example, we could say that 50m/s it the maximum allowed velocity just because we want so. Kd =100/50 = 2. This will effectively stop the P or I component to accelerate the lift beyond 50m/s, but allow any other speed bellow this limit. Note that this limit is applied both ways, when ascending and when descending.

In our example, we could say that 50m/s it the maximum allowed velocity just because we want so. Kd =100/50 = 2. This will effectively stop the P or I component to accelerate the lift beyond 50m/s, but allow any other speed bellow this limit. Note that this limit is applied both ways, when ascending and when descending.

###
**Disclaimer **

**Disclaimer**

This way you have a quick estimation of Kp, Ki and Kd. These values serve only as orientation, and requirie the system to be fine-tuned to obtain the best possible result. Also, this rule does not allow to determine the behavior of the system, like limiting the overshot or number of oscillations.

## No comments :

## Post a Comment