Welcome back! In our last session, we reached a monumental milestone: we wrote and implemented a full PID control loop. Our robot now has a brain that can measure its speed, compare it to a goal, and compute a correction. It is officially a closed-loop system.
However, if you uploaded the code, you probably noticed the wheel’s behavior was less than perfect. It might have been sluggish, overshot the target speed wildly, or oscillated back and forth without ever settling down. A working controller is one thing; a well-behaved controller is another entirely.
The difference lies in the art and science of PID tuning. Today, we’re going to become control system artists. We will learn how to adjust our three magic knobs—the gains Kp
, Ki
, and Kd
—to transform our robot’s response from clumsy and chaotic to crisp, stable, and precise. 1
What Makes a “Good” Tune?
Before we start turning the knobs, we need to define our goal. What does a perfect response look like? In control theory, we’re generally aiming for a system that is: 2
- Fast: It reaches the target speed (the setpoint) quickly.
- Stable: It doesn’t oscillate wildly out of control.
- Accurate: It settles exactly at the target speed with zero steady-state error.
- Minimally Overshot: It doesn’t fly past the target speed too much before settling down.
Achieving the perfect balance of these traits is the core challenge of tuning.
The Personality of the Gains
To tune effectively, you must have an intuitive feel for what each gain does to the system’s behavior. Let’s revisit our three specialists from Session 3 and see what happens when they are too aggressive or too timid. 3
Gain | Role | Too Low | Too High |
Kp (Proportional) | The “Right Now” Responder | The system is slow and sluggish; it takes a long time to react to errors. | The system reacts very fast but becomes unstable, oscillating wildly around the setpoint. |
Ki (Integral) | The “Past-Focused” Historian | It takes a very long time to eliminate the final steady-state error. | The system builds up too much momentum, causing significant overshoot and oscillations. This is “integral windup.” |
Kd (Derivative) | The “Future-Predicting” Damper | The system overshoots the setpoint and takes longer to settle. | The system becomes overly sensitive to sensor noise, leading to jittery, nervous movements. |
Tuning is the process of finding the “Goldilocks” value for each gain—not too high, not too low, but just right.
A Practical Method: Manual Tuning by Trial and Error
There are many advanced, mathematical methods for PID tuning, but the most common, intuitive, and effective method for a project like ours is manual tuning. 4 We will systematically adjust each gain while observing the wheel’s response.
For this process, it’s invaluable to get visual feedback. The Arduino IDE has a wonderful built-in tool called the Serial Plotter. It turns printed data into a live graph. To use it, we just need to modify the Serial.println
line in our code from Session 9 to print the target and actual RPM, separated by a comma.
Change this line:
Serial.println(motor_power);
To this:
C++
Serial.print(target_rpm);
Serial.print(",");
Serial.println(actual_rpm);
Now, when you upload the code and open Tools > Serial Plotter
, you’ll see a live graph of your target speed and the wheel’s actual speed, making it easy to see overshoot and oscillations.
Let’s begin the tuning process. Place your robot on blocks so the wheel can spin freely.
Step 1: Zero Out Ki and Kd
First, we want to isolate the Proportional term. In your code, set Ki
and Kd
to zero, and set Kp
to a small, safe value, like 1.0.
C++
float Kp = 1.0;
float Ki = 0.0;
float Kd = 0.0;
Upload the code. The wheel will likely spin up very slowly and settle at a speed far below your target_rpm
. This is expected.
Step 2: Tune the Proportional Gain (Kp)
The goal here is to find a Kp
value that gives a fast response without being too unstable.
- Gradually increase
Kp
(e.g., from 1.0 to 2.0, then 3.0, 5.0, etc.), uploading the code after each change. - Watch the wheel and the Serial Plotter. You will see the wheel get faster and the response time shorten.
- Keep increasing
Kp
until the wheel starts to oscillate—meaning it consistently overshoots the target, then undershoots, then overshoots again in a sustained wave. - Once you find the
Kp
value that causes sustained oscillation, set your finalKp
to about half of that value. For example, if it starts oscillating atKp = 10.0
, a good starting point for your finalKp
is5.0
.
At this point, you should have a system that responds quickly but likely has a noticeable steady-state error (it settles below the target).
Step 3: Tune the Integral Gain (Ki)
Now we’ll bring in our historian to eliminate that steady-state error. Keep your new Kp
value and Kd
at zero.
- Start increasing
Ki
from a very small value (e.g., 0.1). - Upload and observe. You should see the steady-state error slowly disappear as the integral term builds up and pushes the speed to the target.
- Your goal is to find a
Ki
value that eliminates the error in a reasonable amount of time without causing too much overshoot. If you see the RPM swing wildly past the target, yourKi
is too high. - Keep tweaking
Ki
until the wheel settles nicely at the target RPM.
Step 4: Tune the Derivative Gain (Kd)
Finally, we’ll use our strategist to dampen any remaining overshoot from the P and I terms. Keep your Kp
and Ki
values.
- Start increasing
Kd
from a very small value (e.g., 0.01). - Upload and observe. The
Kd
term should act like a brake as the wheel approaches the target speed, reducing the overshoot and helping it settle faster. - Be very careful with
Kd
. Too much can make the motor sound “jittery” or “nervous” as it overreacts to tiny fluctuations in the RPM reading. Find a value that smooths the response without introducing instability.
This is an iterative process. After adding Kd
, you might find you can slightly increase Kp
for a faster response, which might then require a small tweak to Ki
or Kd
again. Play with the values until you are happy with the performance.
What’s Next?
Congratulations! You have successfully tuned your first PID controller. You’ve taken a raw algorithm and calibrated it to the unique physics of your specific motor, transforming chaotic motion into controlled, predictable behavior.
You now have one, perfectly tuned, intelligent wheel. The next and final step in our software journey is to scale this up. We need to manage four PID controllers simultaneously and integrate the inverse kinematics from Session 4 to translate high-level commands like “move forward” or “strafe right” into the correct target RPMs for each of our four tuned wheels.
In our next session, we will write the final, complete program for our mecanum robot.
Hola, volia saber el seu preu.