# 4.2.8. PID-algorithm for DC engine control¶

## 4.2.8.1. Algorithm description¶

DC engine is controlled by the PID regulator, with the coordinate as the controlled parameter. The controlled coordinate changes according to motion settings and incoming commands to provide motion capability. We will call controller coordinate the running position. DC engine winding PWM signal fill factor is the control signal of the regulator.

The control action is calculated according to the following formula:

$$U (t) = I + P + D = K_P \cdot E (t) + K_I\int E (t)dt + K_D\frac{dE(t)}{dt}$$, where:

$$U (t)$$ - is the control action

$$E (t)$$ - is difference between the running coordinate and the current motor coordinate

$$K_P, K_I, K_D$$ - are proportional, integral and differential coefficients of the regulator. Regulator coefficients are set on PID settings page of the XILab program or programmatically by calling set_pid_settings() function of the libximc library (see Programming guide).

The resulting value is normalized according to the following formula to make PID regulator action independent of motor type, feedback sensor and working voltage:

$$DC (t) = \frac{U(t) \cdot U_{nom}}{U_{supp}(t) \cdot IPS}$$ , where:

$$DC (t)$$ - is the PWM fill factor

$$U_{nom}$$ - is the nominal (maximum) motor voltage, (see Motor limiters).

$$U_{supp}(t)$$ - is the current supply voltage

$$IPS$$ - is the feedback encoder resolution in counts per revolution

This approach allows to change motor type, feedback sensor and voltage supply unit without reconfiguring PID regulator.

Warning

Do not forget to change PID regulator coefficents accoring to abovementioned formula if you are going to change maximum motor voltage.

## 4.2.8.2. Particular properties of the algorithm¶

### 4.2.8.2.1. PID regulator coefficients¶

User set values are normalized to keep optimal PID regulator coefficients in [0..65535] range.

Let’s consider the effects different components have for better understanding.

We will assume the supply voltage $$U_{supp}(t)$$ is constant and equal to the motor nominal voltage $$U_{nom}$$. With this assumption PWM fill factor will be equal to 1 in the following cases:

1. $$K_p = 1,\; K_I = 0,\; K_D = 0$$ - if target position is ahead of real position by 256 motor shaft revolutions
2. $$K_p = 0,\; K_I = 1,\; K_D = 0$$ - if integral in the formula above is equal to 52.5 revolutions $$\cdot$$ second
3. $$K_p = 0,\; K_I = 0,\; K_D = 1$$ - if real motor speed is higher than the required speed by 96000 rpm.

### 4.2.8.2.2. Reaching target position¶

Target position is considered to be reached when motor shaft reaches the target position. Some oscillations around target position are possible. Motor will need some time to stop and return to correct position if smooth deceleration is not used and an immediate stop command is received or an emergency stop by limit switch has happened.

Warning

Long time oscillations around the target position while the motion is considered finished are possible if the PID regulator is set up incorrectly.

## 4.2.8.3. PID regulator tuning recommendations¶

There are three quality criterions used when tuning PID regulator:

• Speed maintenance accuracy, defined as a mean deviation of current speed from desired speed. Speed maintenance is considered to work optimally if the speed takes no more than three distinct values when moving. It is impossible to achieve greater accuracy because speed values are quantized.
• Position reach quality, defined by the following criterions:
• Time until final stop in target position (lower is better).
• No slip on approach to the target position.
• No oscillations around target position before stopping.
• No spontaneous shifts from the target position after stopping.
• Low noise when moving. Noise level is increased when only one of the PID coefficients is increased.

One can choose individual criterion priority depending on the task at hand when tuning PID regulator.

1. It is recommended to disable all DC motor limits including smooth acceleration/deceleration when you start tuning PID regulator.
2. It is recommended to tune PID regulator to maintain constant speed first.
3. It is recommended to start tuning PID regulator with integral and differential coefficients set to zero.
4. Set proportional coefficient $$K_P \leq 10$$
5. Set required movement speed and start the movement far away from the limit switches.
6. Gradually increase $$K_P$$ and observe current speed graph. Find optimal $$K_P$$ which makes the controller maintain current speed the best and makes time to reach target speed less dependent on $$K_P$$. One should be aware of possible noise level increase.
7. It is recommended to tune $$K_I$$ next. Integral PID regulator coefficient has more influence on position reaching behavior.
8. Set integral coefficient $$K_I \leq 10$$ and start movement to position. It is convenient to use “shift on offset” command for this.
9. It is advisable to use sufficiently long shifts so that motor reaches at least $$20\%$$ of required speed.
10. Increase of $$K_I$$ makes the controller stop at the target position faster.
11. Achieve faster reaching of the target position by gradually increasing $$K_I$$. One should stop increasing this coefficient when either quality of position reaching degrades substantially or noise levels increase. Slip at the target position will remain regardless.
12. Adjust $$K_P$$ up or down to decrease oscillations at the target position.
13. After one adjusts $$K_P$$ one should check the speed maintenance quality and noise levels. If these parameters are unsatisfactory then one should adjust coefficients towards their previous values.
14. After setting $$K_I$$ and $$K_P$$ you can set up $$K_D$$.
15. It is recommended to start tuning $$K_D$$ in speed maintenance mode and then check other quality parameters.
16. One should increase $$K_D$$ until speed oscillations around target speed stop decreasing.
17. In case of target position slip one should increase $$K_D$$. However, further increases of this coefficent will lead to oscillations around target position. One should keep a balance between oscillations and target position reaching speed.

Turn on smooth acceleration/deceleration if you plan on using this mode. This can lead to the slip at the target position. To compensate one should increase $$K_I$$.

After these steps the initial setup of PID algorithm is complete. The obtained coefficients are in most cases suitable for motor operation. To further optimize these coefficients one can vary them while continuously monitoring quality according to the relevance of chosen quality criterions to the particular task.

Note

It is not recommended to adjust more than one coefficient at a time while tuning the PID regulator.

Important

It is extremely not recommended to start with large values of PID regulator coefficients or to rapidly adjust them. This can lead to self-excitation of speed oscillations and motor failure.