# 4.2.8. PID-algorithm for DC engine control¶

## 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:

, where:

- is the control action

- is difference between the running coordinate and the current motor coordinate

- 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:

, where:

- is the PWM fill factor

- is the nominal (maximum) motor voltage, (see Motor limiters).

- is the current supply voltage

- 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.

## Particular properties of the algorithm¶

### 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 is constant and equal to the motor nominal voltage . With this assumption PWM fill factor will be equal to 1 in the following cases:

- - if target position is ahead of real position by 256 motor shaft revolutions
- - if integral in the formula above is equal to 52.5 revolutions second
- - if real motor speed is higher than the required speed by 96000 rpm.

### 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.

## 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.

- It is recommended to disable all DC motor limits including smooth acceleration/deceleration when you start tuning PID regulator.
- It is recommended to tune PID regulator to maintain constant speed first.
- It is recommended to start tuning PID regulator with integral and differential coefficients set to zero.
- Set proportional coefficient
- Set required movement speed and start the movement far away from the limit switches.
- Gradually increase and observe current speed graph. Find optimal which makes the controller maintain current speed the best and makes time to reach target speed less dependent on . One should be aware of possible noise level increase.
- It is recommended to tune next. Integral PID regulator coefficient has more influence on position reaching behavior.
- Set integral coefficient and start movement to position. It is convenient to use "shift on offset" command for this.
- It is advisable to use sufficiently long shifts so that motor reaches at least 20% of required speed.
- Increase of makes the controller stop at the target position faster.
- Achieve faster reaching of the target position by gradually increasing . 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.
- Adjust up or down to decrease oscillations at the target position.
- After one adjusts one should check the speed maintenance quality and noise levels. If these parameters are unsatisfactory then one should adjust coefficients towards their previous values.
- After setting and you can set up .
- It is recommended to start tuning in speed maintenance mode and then check other quality parameters.
- One should increase until speed oscillations around target speed stop decreasing.
- In case of target position slip one should increase . 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 .

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.