#### Knowledge Base

# PID (Proportional, Integral, Derivative) Control

## Introduction

The SmartMotor™ includes a very high quality, high performance brushless servomotor with extremely powerful rare earth magnets and a stator (the outside, stationary part): a densely wound, multi-slotted electromagnet.

Controlling the position of a brushless servo’s rotor with only electromagnetism working as a lever is like pulling a sled with a rubber band. Accurate control would seem impossible.

The parameters that make it all work are found in the PID (Proportional, Integral, Derivative) control section. These are the three fundamental coefficients to a mathematical algorithm that intelligently recalculates and delivers the power needed by the motor 8,000 times per second. The input to the PID control is the instantaneous desired position minus the actual position, be it at rest, or part of an ongoing trajectory. This difference is called the position error.

The Proportional parameter of the PID control creates a simple spring constant. The further the shaft is rotated away from its target position, the more power is delivered to return it. With this as the only parameter, the motor shaft would respond just as the end of a spring would if it was grabbed and twisted.

If the spring is twisted and let go, it will vibrate wildly. This sort of vibration is hazardous to most mechanisms. In this scenario, a shock absorber is added to dampen the vibrations, which is the equivalent of what the Derivative parameter does. If a person sat on the fender of a car, it would dip down because of the additional weight based on the constant of the car’s spring. It would not be known if the shocks were good or bad. However, if someone jumped up and down on the bumper, it would quickly become apparent whether the shock absorbers were working or not. That’s because they are not activated by position but rather by speed. The Derivative parameter steals power away as a function of the rate of change of the overall PID control output. The parameter gets its name from the fact that the derivative of position is speed. Electronically stealing power based on the magnitude of the motor shaft’s vibration has the same effect as putting a shock absorber in the system, and the algorithm never goes bad.

Even with the two parameters working, a situation can arise that will cause the servo to leave its target created by “dead weight”. If a constant torque is applied to the end of the shaft, the shaft will comply until the deflection causes the Proportional parameter to rise to the equivalent torque. There is no speed so the Derivative parameter has no effect. As long as the torque is there, the motor’s shaft will be off of its intended target.

That’s where the Integral parameter comes in. The Integral parameter mounts an opposing force that is a function of time. As time passes and there is a deflection present, the Integral parameter will add a little force to bring it back on target with each PID cycle. There is also a separate parameter (KL) used to limit the Integral parameter’s scope of what it can do so as not to overreact.

Each of these parameters has its own scaling factor to tailor the overall performance of the PID control to the specific load conditions of any one particular application.

The scaling factors are as follows:

KD | Proportional |

KI | Integral |

KD | Derivative |

KL | Integral Limit |

**Tuning the PID Control**

The task of tuning the PID control is complicated by the fact that the parameters are so interdependent. A change in one can shift the optimal settings of the others. The automatic utility makes all of the settings easy, but it still may be necessary to know how to tune a servo.

When tuning the motor it is useful to have the status monitor running which will monitor various bits of information that will reflect the motors performance.

KP=exp | Set KP, proportial coefficient |

KI=exp | Set KI, time-error coefficient |

KD=exp | Set KD, damping coefficient |

KS=exp | Set KS, derivative coefficient |

KL=exp | Set KL, time-error coefficient limit |

F | Update PID control |

The main objective in tuning a servo is to get KP as high as possible, while maintaining stability. The higher the KP, the stiffer the system and the more under control it is. A good start is to simply query what the beginning point is (RKP) and then start increasing it 10% to 20% at a time. It is a good idea to start with KI equal to zero. Keep in mind that the new settings do not take effect until the F command is issued. Each time KP is raised, try to physically destabilize the system by bumping or twisting it or have a program loop cycling that invokes abrupt motions. As long as the motor always settles to a quiet rest, keep raising KP. Of course if the SMI Tuning Utility is being used, it will employ a step function and show more precisely what the reaction is.

As soon as the SmartMotor starts to find it difficult to maintain stability, find the appropriate derivative compensation. Move KD up and down until the value is found that gives the quickest stability. If KD is too high, there will be a grinding sound. It is not really grinding, but it is a sign to go the other way. A good tune is not only stable, but reasonably quiet. The level of noise immunity in the KD term is controlled by KS.

The derivative term KD requires estimating the derivative of the position error. While the simplest method is a backward difference, KS=0, this is inherently noisy. The choices of KS=1, 2 and 3 provide increasing levels of noise immunity at the expense of slightly increasing latency in the estimation. Since higher latency will typically result in lower achievable PID loop gains, choose the best compromise between smoothness and tracking performance. The default setting is KS=1.

After optimizing KD, it may be possible to raise KP a little more. Keep going back and forth until there’s nothing left to improve the stiffness of the system. After that it’s time to take a look at KI.

KI, in most cases, is used to compensate for friction; without it the SmartMotor will never exactly reach the target. Begin with KI equal to zero and KL equal to 1000. Move the motor off target and start increasing KI and KL. Keep KL at least ten times KI during this phase.

Continue to increase KI until the motor always reaches its target, and once that happens add about 30% to KI and start bringing down KL until it hampers the ability for the KI term to close the position precisely to target. Once that point is reached, increase KL by about 30% as well. The Integral term needs to be strong enough to overcome friction, but the limit needs to be set so that an unruly amount of power will not be delivered if the mechanism were to jam or simply find itself against one of its ends of travel.

### EL=expression Set Maximum Position Error

The difference between where the motor shaft is supposed to be and where it is actually positioned, is appropriately called the “position error”. The magnitude and sign of the error are delivered to the motor in the form of torque after it is put through the PID control. The higher the error, the more out of control the motor will become. Therefore, it is often useful to put a limit on the allowable error after which time the motor will be turned off. Which is why the EL command is exists. It defaults to 1,000, but can be set from 0 to 262,143. This feature can be disabled by setting EL=-1.

There are still more parameters that can be utilized to reduce the position error of a dynamic application. Most of the forces that aggravate a PID loop through the execution of a motion trajectory are unpredictable, but there are some that can be predicted and further eliminated preemptively.

### KG=expression Set KG, Gravity Offset Term

The simplest of these is gravity. Why burden the PID loop with the effects of gravity in a vertical load application if it can simply be weeded out? If in a particular application motion would occur with the power off due to gravity, a constant offset can be incorporated into the PID control to balance the system. KG is the term. KG can range from -8,388,608 to 8,388,607. To tune KG, simply make changes to KG until the load equally favors upward and downward motion.

### KV=expression Set KV, Velocity Feed Forward

Another predictable cause of position error is the natural latency of the PID loop itself. At higher speeds, because the calculation takes a finite amount of time, the result is somewhat “old news”. The higher the speed, the more the actual motor position will slightly lag the trajectory calculated position. This can be programmed out with the KV term. KV can range from zero to 65,535. Typical values range in the low hundreds. To tune KV simply run the motor at a constant speed, if the application will allow, and increase KV until the error gets reduced to near zero and stays there. The error can be seen in real time by activating the Monitor Status window in the SMI program.

### KA=expression Set KA, Acceleration Feed Forward

Force equals mass times acceleration. If the SmartMotor is accelerating a mass, it will be exerting a force during that acceleration. This force will disappear immediately upon reaching the cruising speed. This momentary torque during acceleration is also predictable and need not aggravate the PID control. Its effects can be programmed out with the KA term. KA can range from zero to 65,535. It is a little more difficult to tune KA, especially with hardware attached. The objective is to arrive at a value that will close the position error during the acceleration and deceleration phases. It is better to tune KA with KI set to zero because KI will address this constant force in another way. It is best to have KA address 100% of the forces due to acceleration, and leave the KI term to adjust for friction.

The PID rate of the SmartMotor can be slowed down.

PID1 | Set highest PID update rate, 16 KHz |

PID2 | Divide highest PID update rate by 2, default of 8 KHz |

PID4 | Divide highest PID update rate by 4, 4 KHz |

PID8 | Divide highest PID update rate by 8, 2 KHz |

The trajectory and PID control calculations occur within the SmartMotor at the “sample rate” selected by the PIDn command. Although 16 KHz is available, 8 KHz corresponding to PID2 is the default, providing a reasonable compromise between very good control and the SmartMotor application program execution rate. This program execution rate can be increased by reducing the PID rate using PID4 and PID8 in applications where the lower “sample rate” still results in satisfactory control.

If the PID rate is lowered, keep in mind the “sample rate” is the basis for velocity values, acceleration values and the PID coefficients. If the rate is cut in half, expect to do the following to keep all else the same:

- Double Velocity
- Increase Acceleration by a factor of 4