Home β€Ί Motor Algorithms & PID Control
⚑ Module 03 · Intermediate

Give Your Robot a Brain for the Obstacle Course!

Ever wondered how a self-balancing robot stays upright, or a drone zips through a race course? It’s not just GO and STOP. It's a clever algorithm that lets a robot feel its way to the target. This is the secret brainpower for any robot athlete. Ready to build it? Let's go! ✨ PAN'S RULE: A little bit of math can look like a lot of magic! ✨

⚠️ SAFETY DIRECTIVE: We only work with safe 5V USB power or AA/AAA batteries. NEVER plug homemade electronics into a wall outlet or mess with mains electricity. Keep the magic safely low-voltage!
πŸ•ΉοΈ

The "Hot and Cold" Game

Imagine you're playing the "hot and cold" game, blindfolded. Your brain uses a strategy that's almost exactly how a PID Controller works. It's the secret sauce for smooth, accurate robot movement. Think of it as a team of three specialists inside your robot's brain.

πŸ“Ί This video shows how PID controllers work in the real world, from balancing robots to drones!

Meet the Brain-Team!

Click on a card to meet the specialists running your robot's brain.

πŸ…ΏοΈ

The Sprinter

"I see how far we are from the goal and I RUN! The farther away we are, the faster I go!"

Why it matters:

Without me, your robot is lazy and won't even know it needs to move!

πŸ…Έ

The Memory

"I remember if we've been stuck for a while and give us an extra NUDGE to get going again!"

Why it matters:

Without me, your robot might get stuck on a bumpy carpet and give up!

πŸ…³

The Fortune-Teller

"I see us getting close and I yell 'BRAKES!' so we don't zoom right past the target!"

Why it matters:

Without me, your robot is a clumsy oaf that always trips over the finish line!

🏁 The Overshoot Game

What happens when your robot *only* has "The Sprinter" (P) on its team? It only knows one thing: the farther from the finish line, the faster it goes. Let's see it in action!

πŸ€–
βš™οΈ

Obstacle Course Training: The Precision Stop

Your first mission is the Precision Stop! You need to get your bot to the target line *fast*, but without flying right past it. A robot with just a "P" brain can be tricky to control. If it's too aggressive, it will overshoot. Too gentle, and it will take forever!

Your goal: Tune the slider so the robot gets to the target (`position` > 99) in under 7 steps without overshooting (`position` < 105). Can you do it?

Gentle 🐒 Aggressive πŸ†
Simulation output will appear here...

πŸ‘¨β€πŸ‘©β€πŸ‘§ Parent Corner

Hey parents! "PID" sounds like complex engineering, but it's everywhere. You are a human PID controller every day!

Think about filling a bathtub: The 'P' (Proportional) is you turning the faucet on full blast at the start. The 'I' (Integral) is you noticing the water is still too cold after a minute and turning up the hot water a bit more. The 'D' (Derivative) is you turning the faucet way down as the water approaches the top, so it doesn't overflow. You're predicting the future to prevent a mess!

πŸš€

Advanced Challenge: Pro-Tuner

Every great obstacle course has a wobbly bridge or a tight corner. To master them, your robot needs the full brain-team working in perfect harmony. Time to become a Pro-Tuner! Now you control all three values: `Kp` (The Sprinter), `Ki` (The Memory), and `Kd` (The Fortune-Teller). Your goal is to get the robot to the target fast, with minimal overshoot and wiggling. Watch the graph to see how your robot behaves over time!

Try these combos to see what happens:

Pro Tip πŸ€“: The process you just didβ€”adjusting Kp, Ki, and Kdβ€”is called 'PID tuning.' In real robotics and industrial control, engineers can spend days finding the 'perfect tune' for a system. Getting a feel for how these values interact is a legitimate engineering skill. You're doing real-world stuff right now!

πŸš€ Deploy to a Real Microcontroller

Awesome! You've mastered the theory. The next step is to use a real PID library on an Arduino. This code might look scary, but it's doing the exact same thing our simulator did. Look at the comments!

#include <PID_v1.h>

// Define Variables we'll connect to our robot's world
double Setpoint, Input, Output;

// Specify the links and initial tuning parameters
// This is where you put your Kp(2), Ki(5), and Kd(1) values!
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, DIRECT);

void setup() {
  // Setpoint is our target, just like in the simulator
  Setpoint = 100;
  // This turns the robot's brain on!
  myPID.SetMode(AUTOMATIC);
}

void loop() {
  // Read the sensor value (e.g., from an ultrasonic sensor)
  Input = readSensor(); 
  // The magic happens here! The library does all the P, I, and D math for you.
  myPID.Compute();
  // Apply the calculated power to the motor
  analogWrite(MOTOR_PIN, Output); 
}