Main Assembly

Main Assembly

33 ratings
Brief introduction to PID controller
By Ownedpilot
This guide would help you understand what PID controller is and why it is important without getting into too much technical details. I hope this guide will help you implement PID controller into your build and benefit from it.
   
Award
Favorite
Favorited
Unfavorite
Gratitude
Special thanks to Ms. Ruccolo, my technical writing teacher, who helped me made this guide easier to read via peer criticism in class.
Introduction to the guide
This is a very brief guide of what PID controller is, how it works and how to implement it in the game. To understand this guide, you do not need to know about Laplace transform and transfer function, but some basic understanding of calculus is required.

Consider this situation, a machine has a mechanical arm pointing downward. The arm is attached to a motor. The task is to rotate the arm to the horizontal direction when a key is pressed. The arm and the motor form a dynamical system whose rotational location and speed change overtime. The key press telling the arm to rotate to horizontal direction is an input.

When a dynamic system receives an input, ideally, the system would immediately reach the desired input value. In this situation, the arm would immediately point to horizontal direction if the conditions were ideal. However, due to physical limitations, the system would behave more like what is shown in the picture below. It would not accelerate infinitely fast. It would overshoot. It would vibrate. It might not even stop at the value we wanted. These problems are what system engineers would try to solve.

We can quantify the performance of the system by measuring the value of these “problems”. The time it takes for the system to reach from 10% of the desired input value to 90% of the desired value is defined as rise time. When the system reaches the desired value, in most cases, there would be an overshoot. The overshoot value normalized into percentage relative to input value is defined as percent overshoot. The time it takes for the system to stabilize within ±2% of the input value is defined as settling time. The target is to have a system that has low rise time, percent overshoot and settling time.


As suggested by the title, a good solution to achieve the target is PID controller. PID controller stands for Proportional, Integral, and Derivative controller, thus, PID. The function of each part would be explained in the next section.
How does PID controller work?
For PID controller to work, an error function must be calculated. Error is calculated by subtracting the current value of the dynamic system from the input value. The error value is then recorded over the period of machine operation to form an error function. For the application in game, the game would create the error function for you as long as you subtract the current value of the dynamic system from the input value.

From the previous section, we can see that a PID controller consists of three parts. Each part aims to drive different aspects of error function between input and output to zero. We can start with “P” controller or proportional controller.

Proportion controller takes the unmodified error value at time t, multiplies it by a constant Kp and sends the result to the actuators. This controller aims to drive the current error to zero. Increasing the value of Kp would decrease rise time and steady state error, but a Kp value that is too high would lead to high overshoot and vibration which are undesirable. Kp has little impact on settling time.


“I” controller or integral controller takes the integral of error function from 0 to t, multiplies it by a constant Ki and sends the result to the actuators. This controller aims to drive the past error to zero since an integral is the accumulation of the past of a function. Increasing the value of Ki would decrease rise time and steady state error, but a Ki value that is too high would lead to high overshoot and long settling time which are bad.


“D” controller or derivative controller takes the derivative of error function at time t, multiplies it by a constant Kd and sends the result to the actuators. This controller aims to drive the future error to zero since derivative can be used to predict the trend of a function. In other words, this controller would try to make the response curve as flat as possible. Increasing the value of Kd would decrease percent overshoot and settling time. But doing so would slow down the system and leads to high rise time. Kd has very little effect on rise time and steady state error.


Table below is a summary of what each controller gain would do to the system. The effects displayed in the table are very important when tuning the PID controller.

Potential problems
A potential problem that can be caused by the integral controller is called wind up problem. It happens when the set point is too large, and the controller output is limited. The limited controller output means that the system cannot response as fast as integral controller wanted, the integrated error accumulates until the system overshoots to balance out the accumulated error. Leading to a crazy amount of overshoot. This problem is very common if you ever used PID controller in the game. We will discuss the solution in the next section.

Derivative controller is the source of another potential problem. When the error signal is too noisy, the derivative of such noisy signal will be greatly amplified. Amplified noise derivative would lead to oscillation which is undesirable. However, this problem is not that severe in the game because of limited simulation frequency and sampling frequency of sensors. These two factors worked as a low pass filter which filters out high frequency noise signals. When the noise is filtered out, the derivative controller is less likely to go haywire.
Implementation
With the knowledge of how PID controller works, it would be useless if you guys do not know how to implement the controller in game. The PID controller can be found in state category when advanced mode is active. The PID block has two inputs, the first one is where error signal is fed in, the second one is used to reset the controller if the controller go haywire. For normal players who are not system engineer (students), here is a procedure that works most of the time:

1. Set Ki, Kd to zero, increase Kp until system oscillates. A good way to do that is to increase by orders of magnitude. If 1 does work then try 10, if 10 does not work then try 100. At some point, the system would start to oscillate. Then it is time to divide Kp by 2 over and over until the oscillation is barely visible.

2. Then increase Kd until system oscillates. The method should be similar to step one. Then reduce Kd until the oscillation is barely visible.

3. Increase Ki if you are concerned that the system is too slow or the steady state error is too high.

The key to PID tuning is the balance between three variables. Again, the proposed procedure will work just fine if you are a normal player and the performance is not too big of a concern. For us system engineers (students), we have all sorts of computational tools and knowledge to help us derive the best PID controller for the system. But because the method we use is too complicated to be explained here, I choose to left that out.

And do you still remember the two problems of integral controller and differential controller brings? Well, the Devs now only provided a make shift solution. Clamp integral and clamp derivative can somewhat reduce the effect of the two problems but these problems would still exist.

To reduce the effect of the problem to the minimum, I have a few suggestions:

1. Do not use Ki if possible.

2. Average the signal sent into the PID to reduce noise.

3. If Ki is necessary for the performance of the machine, reset the PID controller if the wind-up is out of control. Only turn on the PID controller when necessary to make sure that the integral has no garbage memory to cause issues.

And I hope you have a good time tuning your machine!!
7 Comments
ZombieDEW2012 22 Feb, 2024 @ 12:30pm 
DEFEND AGAINST DOGE, BOB, CUBE, T-REX, SHREK, AND ANY OTHER CREATURES
AND SAVE STEAM!!!
░░░░███████ ]▄▄▄▄▄▄▄▄
▂▄▅████████▅▄▃▂
I███████████████████]
◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤
COPY AND PASTE TO JOIN THE WAR
LONG LIVE THE REBELLION, END THE SPAM LOL
DEFEND AGAINST DOGE, BOB, CUBE, T-REX, SHREK, AND ANY OTHER CREATURES
AND SAVE STEAM!!
AlexOrSmthnIdk 9 Apr, 2022 @ 5:49pm 
stuPIDly complex but i like it
Clompi104 10 Sep, 2021 @ 6:46am 
Damn. Absolute legend.
tylereads704 7 Sep, 2021 @ 4:04pm 
yes
Jester_BaZe 5 Aug, 2021 @ 8:22pm 
My brain hurts now, but I like it. Awesome guide. I'll get it one day.
Hamburgerman_23 31 May, 2021 @ 5:50am 
that was in depth
AR | MENDES 16 Feb, 2021 @ 2:36am 
Great guide!