How to Make Your FLL Robot Drive Straight
A gyro, a loop, and one magic number. The exact SPIKE Prime code our team uses, and why it works.
- You'll need
- SPIKE Prime and the SPIKE App 3 word blocks
- Setup
- Drive motors on ports A and E
- You'll end up with
- One reusable DRIVE STRAIGHT block
If your FIRST LEGO League team has ever watched the robot drift off course halfway across the mat, this one's for you.
Tell both wheels "go forward at 50" and the robot will still curve. Every time. It isn't your build and it isn't your code. No two motors are exactly alike. One tire grips a little better, one side of the mat is a little bumpier, and those tiny differences add up. Over a long drive, being off by a single degree puts you nowhere near where you aimed.
The fix isn't more careful building. It's a robot that notices it's drifting and corrects itself, over and over, faster than you can see. That sounds advanced. It's about ten blocks. Here's the whole thing:
01The big idea: the hub knows which way it's facing
Inside the SPIKE hub is a gyro sensor. It reports the yaw angle: how many degrees the robot has turned left or right. The program resets yaw to 0 at the start, so from then on 0 means "pointing straight."
While yaw stays at 0, both wheels run at the same speed. As soon as the robot starts to drift, yaw stops being 0. The program speeds up one wheel and slows down the other just enough to steer back. Then it checks again, and again, many times a second.
It's like riding a bike. You never hold the handlebars perfectly still. You make constant tiny corrections without thinking about it. This program does the same thing.
02Block by block
Each number on the code matches a note.
- 1Your own block. You hand it two numbers, how far (CM) and how fast (SPEED), and it handles the rest. Build it once, then use it every time the robot needs to go straight.
- 2Which motors are the wheels. Ours are plugged into ports A and E. If yours are on different ports, change the letters here and in every motor block below.
- 3"Whichever way I'm facing right now is straight." This resets the gyro so 0 means straight ahead.
- 4Zero the trip meter. From here on, motor A counts how many degrees it has turned.
- 5Keep going until we've gone far enough. CM × 20.5 turns centimeters into wheel degrees. abs strips the minus sign off motor A's count. Both are explained below. Everything inside this loop runs over and over, many times a second.
- 6How far off are we? Read the yaw angle and multiply by −1.5. Drifted a little: small fix. Drifted a lot: bigger fix. Pointing straight: yaw is 0, so the fix is 0.
- 7Wheel A runs at SPEED + CORRECTION. The
0 −in front flips the sign, because motor A is mounted backwards. - 8Wheel E runs at SPEED − CORRECTION. One wheel speeds up and the other slows down by the same amount, so the robot steers itself back toward 0.
- 9We're there. Stop both wheels.
- 10Use it. This is our test run: 500 cm at speed 50. A long drive makes any drift easy to see. For a mission, plug in your own distance and speed.
03Three things to watch out for
1. Why does wheel A get "0 −" and "abs"?
The two drive motors face opposite directions, like mirror images. "Forward" for motor E is backward for motor A. Give them both +50 and the robot spins in place instead of driving.
Putting 0 − in front of wheel A's number flips it negative, which is forward for that motor.
The same mirror trick explains abs (absolute value) in the repeat block. As the robot drives forward, motor A's position counts down: −100, −500, −1,000. Without abs, "relative position > 1,025" would never be true, and the robot would drive forever. abs drops the minus sign so the count goes up.
2. Why −1.5 and not 1.5?
The minus sign makes the fix push the right way. Whether yaw goes up or down when the robot drifts depends on how the hub is mounted, so we found the sign by testing. With +1.5, every "correction" made the turn worse and the robot spiraled off. With −1.5 it locked onto the line. If yours spirals, flip the sign.
The 1.5 part is how hard the robot corrects. Too small and it drifts anyway. Too big and it overcorrects and wiggles. 1.5 was the sweet spot for our robot. Yours might be different (see "Try it" below).
3. Where does 20.5 come from?
Motors count in degrees, but we want to think in centimeters. Every time the wheel turns all the way around (360°), the robot rolls forward one wheel-circumference.
The standard SPIKE Prime wheel is 5.6 cm across.
Around the edge: 3.14 × 5.6 ≈ 17.6 cm per full turn.
Degrees per centimeter: 360 ÷ 17.6 ≈ 20.5.
So to drive 50 cm, the loop keeps going until motor A has turned 50 × 20.5 = 1,025 degrees.
04Try it: change the −1.5
This is the best five minutes your team can spend with this program. Change the number, have everyone predict what will happen, then run it.
0: no correction at all. Does it still go straight over a long drive?
−1.5: our setting. Small, smooth fixes.
−5: corrects way too hard. Watch for wiggling side to side. Why do you think it overshoots?
+1.5: the fix goes the wrong way. What happens when every "fix" makes the turn worse?
For the curious: what you've built has a real name. It's a proportional controller (the "P" in PID), and −1.5 is its gain. The bigger the error, the bigger the correction. The same idea helps keep drones level and cars in their lane.