Now we can implement discrete time PID control function.In the sample C code *(See code sheet below)*The names of variables and constants closely match the corresponding elements in mathematical equations. This code can be executed on each iteration of the PID firmware, typically within a timer interrupt configured to trigger each iteration. *time* millisecond.

/* Current Error - Proportional term */ e = r - y; /* Accumulated Error - Integral term */ totalError += e; /* Difference of Error - Derivative term */ deltaError = e - previousError; /* PID control */ u = Kp * e + Ki * (totalError * T) + Kd * (deltaError / T); /* Also prepare for next iteration – set previous to Current Error */ previousError = e;

All that’s left to do is assign an appropriate value to the PID gain *K _{p}*,

*K*,

_{I}*and*

*K*So that the system performs correctly when asked to move to the selected set point. We will manually select different values for these gains to study their effect on position control, and in the process demonstrate a general approach to tuning them. We’ll also take a closer look at the purpose of each item in the control function.

_{d}### PID gain tuning

There are several criteria for evaluating system performance, including *time to die*, *Rise Time*, *overshoot*, *stable schedule*and *Steady-state error*. Although performance expectations should be defined based on these criteria before adjusting PID gains, such expectations depend on the requirements of the application. Therefore, for the purposes of this article, it will suffice to provide some sense of when adjustments to different gains affect various criteria.

Every *K _{p}*,

*K*,

_{I}*and*

*K*Given the selected setpoint, the gains will be adjusted individually, and in that order. More specifically, the code will set one of the gains to a different value each time it is executed, and set the value of r to 700.

_{d}As for the relevance of this value of 700, the reader should remember **Current position **is a digital value representing the voltage obtained from the actuator potentiometer, and **Current position** Now represented by the variable y in our code.this **Set location**–represent** **The variable r in our code is a numeric value in the same range, as mentioned before, between 0 and 1023 if the ADC has 10-bit resolution. Therefore, a setpoint value of 700 is reasonable, albeit arbitrary.

**Tuning K _{p} Close to target location**

*K _{p}* is the proportional gain. The proportional term of the control function compensates for the current error by moving the linear actuator with a signal proportional to the current error. It makes sense to use a proportional term to bring the current position closer to the target, since the error is the difference between the actuator’s set position and its current position. The proportional term causes the control function to seek to reduce it to zero.

In the first step of tuning, we set the integral and differential gains *K _{I}* and

*K*Zero and increase proportional gain

_{d}*K*until the actuator stabilizes near the target position (700). Proportional gain that is too high can cause oscillation.

_{p}chart in *Figure 5* Shows how the actuator’s current position changes over time for different values *K _{p}*.we will choose

*K*= 1, it is observed that it causes the current position to stabilize near the target with the fastest stabilization time.

_{p}