Class 5 · Stay put, then move¶
Your robot stands, but it probably wanders: it rolls slowly off in one direction until it runs out of floor or the tether. Today you add a second, slower loop that watches the wheels through the encoders and leans the robot just enough to bring it back to a stop. Once it can stay put, moving is the same trick: lean it a little, and it rolls.
By the end of class
The robot balances in one spot, and rolls forward and back when you send it a command.
New parts today¶
None. You'll reuse the three knobs on A0–A2 for new jobs.
Why it drifts¶
The PID loop holds the robot at balancePoint. If that number is a tenth of a degree off from the true balance point, the robot leans slightly the whole time, and a leaning robot has to keep pushing to stay up. Nothing in the PID loop knows or cares that the wheels are rolling. You could fiddle with balancePoint forever: the true value changes with the battery, the floor and anything you set on top.
The fix is to let the robot find its own balance point by watching the wheels.
Tool: one loop inside another
Think about how you stand on a moving bus. One part of you keeps you from falling over, fast and without thinking. Another part, slower, decides "I'm drifting toward the back of the bus" and leans you forward a little. The fast part then balances you around that new lean.
That's a cascade: the outer loop doesn't drive the motors. It sets the target for the inner loop.
outer loop: targetAngle = balancePoint + Ks * (desiredSpeed - wheelSpeed)
inner loop: error = angle - targetAngle
(the same PID as before)
If the robot is rolling forward and you want it still, desiredSpeed - wheelSpeed is negative, so the target angle tilts it back a little. To lean back, the wheels first have to roll forward faster, which tips the body back; then the lean slows it down. The outer loop must be much gentler than the inner one: a target angle of a few degrees at most.
Measured speed, not commanded speed¶
wheelSpeed comes from the encoders: the average of the two wheels' speeds in mm/s, from Class 2. Smooth it with the blend from Class 2 (avg = 0.95 * avg + 0.05 * speed) so the outer loop doesn't chase every jiggle.
Why not use what you told the motors? Because your code never tells them a speed. It sends a PWM, a push, and how fast that push actually rolls the wheels depends on the battery (speed sags as it drains), the floor, the load on top and how the robot is leaning. The same u = 80 can mean rolling forward, standing still, or rolling backward. The encoders report what the wheels really did, so the outer loop corrects the real drift, not a guess. And because they count every bit of turning, they also tell you how far the robot has gone, which lets it come back to where it started instead of just stopping wherever it ended up.
Mission 1 · Stop the drift
- Hard-code your Class 4 gains as
const floats. The knobs are free for new jobs: A0 will beKs. - Compute
wheelSpeed, the smoothed average of the two measured speeds (mm/s). Plot it in Teleplot. - Add
targetAnglewithdesiredSpeed = 0for now.Ksrange on the knob: 0 to 0.02 degrees per mm/s (at 250 mm/s, the top of that range leans the robot 5°). Limit the outer loop's part to ±5°:targetAngle = balancePoint + constrain(Ks * (desiredSpeed - wheelSpeed), -5, 5). - Use
targetAnglein place ofbalancePointin the PID error. - Start with the
Ksknob at zero (the robot behaves like Class 4), then turn it up slowly.
If turning up Ks makes it run away faster, the sign is wrong: flip it. Like the PID gains, the Ks range is a starting point; widen it if your best setting is at the end of the knob.
Done when
Let it go and watch: it no longer rolls steadily off in one direction. Plot wheelSpeed: it hovers around 0 instead of sitting at a steady value. Write down Ks.
Mission 2 · Come back to the tape
Stopping the drift isn't the same as staying put: each little wander leaves the robot somewhere new. Add the distance the wheels have rolled.
- Compute
positionin mm from the average of the two encoder counts (counts × mm per count). - Add a second term to the outer loop:
targetAngle = balancePoint + constrain(Ks * (desiredSpeed - wheelSpeed) - Kpos * position, -5, 5). PutKposon the A1 knob, 0 to 0.02 degrees per mm (100 mm away from the start leans it back up to 2°). - When the tilt cutoff trips, reset
wheelSpeedto 0 and make the current spot the new start: save both encoder counts at that moment and measurepositionfrom them. Then wherever you stand it back up is position 0. - Turn
Kposup slowly from zero. If it runs away from the tape, flip the sign ofKpos's term. Too much and it rocks back and forth across the tape in long, slow swings; back off.
Done when
Mark the floor with tape where you let it go. For 60 s, untouched, it stays within about 30 cm of the tape. Write down Ks and Kpos, and hard-code them.
Moving on command¶
Once the robot holds still, driving is just a different desiredSpeed. Set it to 150 mm/s and the outer loop leans the robot forward until it rolls at that speed, measured.
Tool: reading the keyboard over Serial
You've used Serial to print. It also works the other way: characters you type in the Serial Monitor arrive at the Uno. Serial.available() says how many are waiting; Serial.read() takes one.
Example, not the project: type o to turn on the Uno's built-in LED and x to turn it off.
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
if (c == 'o') {
digitalWrite(LED_BUILTIN, HIGH);
} else if (c == 'x') {
digitalWrite(LED_BUILTIN, LOW);
}
}
}
In the PlatformIO Serial Monitor, each key goes out as you type it; no Enter needed. Only one program can hold the port, so close Teleplot while you use the Serial Monitor.
Mission 3 · Forward, back, stop
- Add commands:
fsetsdesiredSpeedto forward,bto backward,sback to 0. Start at ±100 mm/s. - Don't jump: each loop, move
desiredSpeeda small step toward the commanded speed (for example 0.5 mm/s per 5 ms loop), so the robot leans into it gently. - While it's driving, the position term would pull it back to the start. Turn it off while
desiredSpeedisn't 0, and resetpositionto 0 the moment it reaches 0 again, so it holds the new spot. - Put the speeds in
consts at the top and raise them once it works.
Use the Serial tool above. The USB cable is already in the tether.
With Ks and Kpos hard-coded, the A2 knob is free. Turn it into a speed: center = stop, all the way one way = full forward, the other way = full back. Ignore small turns near the center (a dead band of about ±50 out of 1023) so it really stops. Put the knob on a small breadboard on long wires along the tether.
Clear the floor
Give it a few meters of open floor and a person at each end. It will overshoot a stop at first. The switch stays within reach.
Done when
On command, it rolls forward about a meter, stops and balances in place, then rolls back. Film it.
If you finish early¶
- Turning. Add a
turnvalue: left motor getsu + turn, right getsu - turn. Commandslandr. The balance doesn't care, because the average of the two wheels stays the same. Use the difference between the two encoder counts to check how far it turned. - Drive a set distance. Add a command that rolls exactly 500 mm, using
position, then stops. Measure how close it gets. - Put a hard-cover book on the top deck. Does it still stay put? The outer loop should find the new balance point by itself. Compare with your Class 4 code.
- Plot
targetAnglewhile it stays put. How far off frombalancePointdoes it settle?
Sign-off with Dr. Alvear
- Outer loop on measured wheel speed and position: stays within about 30 cm for 60 s
- Your group explains why the measured speed works better than the PWM you sent
- Forward, back and stop on command (video)
- Gains (Kp, Ki, Kd, Ks, Kpos) written in the build log
- Build log updated (photo + two sentences)