Class 3 · Build the body¶
The sensor and motors work on the bench. Today they become a robot: three decks, motors at the bottom, electronics in the middle, battery on top. Before it ever stands on the floor it gets a power switch and a tilt cutoff. Then you try the simplest controller there is, P only, and watch it catch itself and wobble.
By the end of class
The robot is built, its motors stop when it falls past 45°, and you have a video of it catching itself under P control and oscillating, with an explanation of why.
New parts today¶
| Part | What it's for |
|---|---|
| Three decks (printed or plywood) | Motors, electronics, battery |
| M3 threaded rod, nuts, washers, M3 screws | Hold the decks together |
| Two motor brackets | Mount the motors under the bottom deck |
| Toggle switch | Motor power on/off |
| Power bank (not connected yet) | Its weight on top, from today on |
Build it¶
- Bottom deck: motors. Screw each motor into its bracket (M3 screws into the gearbox face; use the short screws that came with it, since long ones can jam the gears), then bolt the brackets under the bottom deck, shafts pointing out to the sides on the same line. Wheels on, hubs tight against the flat of the shaft. Route the encoder cables up to the middle deck so they can't reach the wheels.
- Rods. Four M3 threaded rods through the corner holes, with a nut above and below each deck so you can set the height and lock it. Make the robot tall: about 25–30 cm from the axle to the top deck.
- Middle deck: electronics. The Uno and the breadboard with the IMU and the TB6612. Mount the IMU flat, centered over the axle, X arrow along the axle, held firmly with screws or double-sided foam tape. Once you start tuning, don't move it: every angle depends on it.
- Top deck: battery. Strap the power bank on top now, even though it isn't powering anything yet. Tuning with the weight in place means the robot won't change when you cut the tether in Class 6.
- Power switch. Put the toggle switch in the +12 V wire between the barrel jack adapter and the TB6612's
VM. Mount it on the top deck where you can reach it while the robot moves. - The tether. Bundle the 12 V lead and the USB cable and tie them to the top deck, so a tug on the cables pulls on the frame, not on the breadboard. Nothing may hang near the wheels.
Power off while you build
12 V unplugged and USB unplugged while you mount and rewire. Before powering up again, check that no wire can touch a wheel and that the switch actually cuts the motor power (multimeter on volts from VM to GND, switch off: 0 V).
Why is tall easier to balance?
Try it: balance a pencil on your fingertip, then a broom. The broom is easier, because it falls more slowly. A tipping stick's angle speeds up at a rate of about (g ÷ L) × angle, where L is the height of its center of mass. Double the height and it tips over half as fast, which gives your code (or your hand) more time to catch it. That's why the heaviest part, the battery, goes on top. It feels backwards, because a low center of mass makes things stable when they're standing on a wide base. An inverted pendulum has no base, so you want the opposite.
The tilt cutoff¶
Mission 1 · Off when fallen
Write this before the robot stands on the floor, and test it with the robot in your hands.
- When the angle goes past ±45°, the robot has fallen: set both motors to 0, set
STBY(pin 4) LOW so the driver switches off and the wheels go limp, and remember it's fallen (abool). - While fallen, ignore the controller completely.
- Re-arm only when someone stands it back up: the angle is within 3° of upright. Then set
STBYHIGH again.
Done when
With the switch on, tip the robot past 45° in your hands: the wheels stop and spin freely by hand. Stand it up: it re-arms. Tip it again: it stops again. Try it in both directions.
P control¶
Tool: proportional control
A controller compares where something is with where it should be (the error) and does something about it. P (proportional) control pushes in proportion to the error: twice as far off, twice the push.
Here's a pure simulation, no hardware: a cart on a track that you want at position 100. P control decides how hard to push it, the push changes its speed, and the speed changes its position. Run it in a separate PlatformIO project and watch it in Teleplot.
#include <Arduino.h>
float pos = 0; // where the cart is
float vel = 0; // how fast it's moving
const float target = 100;
const float Kp = 2.0;
const float dt = 0.01; // pretend each loop is 10 ms
void setup() {
Serial.begin(115200);
}
void loop() {
float error = target - pos;
float push = Kp * error; // P: push harder the farther away
vel += push * dt; // pushing changes speed
pos += vel * dt; // speed changes position
Serial.print(">pos:"); Serial.println(pos);
delay(10);
}
The cart overshoots 100, comes back, overshoots again, forever. Try a bigger and a smaller Kp. Hold on to this example: you'll fix it in Class 4.
For the robot, the error is how far it leans from its balance point, and the push is the motor PWM. Each loop:
Limit u to ±255 with constrain() and send it to both motors with your setMotor() from Class 2 (dead zone included). It's the same line as last class's tilt chase; what's new is the balance point, the cutoff, a real body and a much bigger Kp.
What does PWM do to a wheel?
PWM sets the motor's voltage. While the wheel is speeding up, more voltage means a harder push on it (more torque), the way you whip your hand faster under a falling broom. That's what push is in the cart example: u decides how hard the wheels shove the bottom of the robot, and the robot's tilt is like the cart's position. As the wheel gets faster, the motor pushes back with its own voltage, so at a steady PWM the wheel settles at a steady speed. That gives the robot a little natural braking, but not enough to stop the wobble you'll see.
Mission 2 · Find the balance point
The robot's balance point is probably not exactly 0°: the IMU isn't perfectly level and the weight isn't perfectly centered. With the motors off, hold the robot on its wheels and find the angle where it feels weightless, neither falling forward nor back. Read the angle in Teleplot. That's balancePoint, a const float in your code.
Mission 3 · Stand it up with P
- Put in the P controller with your balance point. Start with
Kp = 15(PWM per degree). - Hold the robot upright on the floor, with your hands loosely around it (not touching), switch on, and let go slowly.
- If it falls without the wheels trying to catch it, the sign is wrong: flip the sign of
u. If the wheels try but it still falls slowly, raiseKp. - Keep raising
Kpuntil the robot catches itself. Then film it from the side, in slow motion if your phone can.
Done when
You have a video of the robot catching itself and rocking back and forth (it may grow until it falls), and your group can explain why P alone oscillates, using the cart example. Count the wobbles per second in the video and write it down.
If you finish early¶
- Try three values of
Kpand film each. How does the wobble change? - Add
>error,>uand the measured wheel speed to Teleplot and record a run. Where in the wobble is the wheel speed biggest? - Move the power bank to the bottom deck and try the same
Kp. Is it harder or easier? Put it back on top.
Sign-off with Dr. Alvear
- Body built: motors bottom, electronics middle, battery top; IMU flat and fixed
- Power switch cuts the motor supply
- Tilt cutoff stops the wheels past ±45° and re-arms near upright
- Video of P-only oscillation, and your group explains why it happens
- Build log updated (photo + two sentences)