Background
I'd done a fair bit of simulation and FEA and wanted to get my hands on some real electronics and hardware, so I decided my next robot would have a drivetrain I'd printed and a board I'd designed myself. Admittedly, that's a slightly ambitious way to learn electronics haha.
The robot is a small base where every wheel steers and drives on its own, so it can move in any direction while it turns. The trick is a differential swerve module: each wheel is driven by two N20 gear motors turning two ring gears. Spin both rings the same way and the module steers, spin them in opposite directions and the wheel drives. Both motors stay fixed to the chassis, so no wires pass through the steering joint and a module can spin without limit (no slip ring!). A Raspberry Pi 5 will handle LiDAR mapping and path planning, with an ESP32 running the eight motors and four steering sensors.
Doing the maths first
Before printing anything I wanted numbers, so I started with three targets: cruise at 1 m/s (the RPLIDAR C1 scans at 10 Hz, so that's 10 cm between scans), accelerate at 1 m/s², and turn a module 180° in 0.3 s, which is 100 rpm of steering. Everything else is sized from those plus a mass from CAD, and the script is here if you want to rerun it.
The gearing was the fun part. Each ring has its own motor driving it through a 15-tooth pinion into 112 teeth on the ring's outer edge (a 7.47 : 1 reduction). On the ring's flat face, 92 more teeth mesh with a 16-tooth gear on the wheel axle (a 5.75 : 1 step-up). Both rings turning the same way carries the axle gear round without spinning, so the module steers. Opposite ways spins the axle gear, so the wheel drives. The reduction sits on the ring stage because that stage does the steering, and the step-up brings the wheel back to a useful speed. Net result: the wheel turns at 0.77 × motor speed and the module at 0.134 × (all teeth are 1 mm module, so the pinion sits 63.5 mm from the ring centre).
| Stage | Teeth | Ratio |
|---|---|---|
| Motor pinion to ring | 15 to 112 | 7.47 : 1 reduction |
| Ring face to axle gear | 92 to 16 | 5.75 : 1 step-up |
Next, a mass. I added up CAD volumes × PLA density (100% infill, so it's an upper bound) plus nominal weights for the bought parts (80 g of N20 motors, 140 g of wheels, 110 g for the RPLIDAR, 130 g for a ~1500 mAh 3S LiPo, 46 g for the Pi 5, 30 g for the ESP32 and controller, and 80 g of bearings, screws and wiring), which I haven't put on a scale yet. It comes to 1.62 kg, or 4.0 N on each wheel, and the eight ring gears alone are about 200 g of that. (Gravity is 9.81 m/s² here, so 1.62 kg × 9.81 ÷ 4 wheels is the 4.0 N.)
| Part | Mass |
|---|---|
| Chassis (bases, frames, covers) | 431 g |
| Swerve module printed parts, x4 | 490 g |
| Centre bay (tray, battery frame, cap) | 85 g |
| Bought parts (motors, wheels, LiDAR, battery, Pi, controller, hardware) | 616 g |
| Total | 1.62 kg |
The robot uses the 12 V, 1600 rpm N20 motors I bought. For rolling resistance (Crr, the drag from the tyre squashing on the floor as a fraction of the weight) I took a conservative 0.03. Splitting the force over four wheels, then two motors per wheel through the 0.77 ratio, gives 7.6 mN·m per motor. Cruising needs 255 rpm at the 75 mm wheel, which is 331 rpm at the motor and steering needs 747 rpm, which together is 67% of the no-load speed.
| Quantity | Result |
|---|---|
| Total force, m(a + Crr g) | 2.10 N |
| Wheel torque (37.5 mm radius) | 19.7 mN·m |
| Torque per motor | 7.6 mN·m |
| Motor speed to cruise at 1 m/s | 331 rpm |
| Motor speed to steer at 100 rpm | 747 rpm |
Here's the catch: I couldn't find a datasheet stall torque, so I've assumed 105 mN·m until I measure it (it's also my FEA load). On that, the acceleration needs just 7% of stall, or 12% with gear losses, so torque isn't the problem. Speed is. At 1 m/s the motors run at only 21% of their no-load speed, which is coarse for PWM. A 600 rpm N20 I modelled in CAD would cruise at about 55% of its speed with roughly 2.7 times the torque.
Then the gears, because I didn't want the teeth to be the weak point if a module jams, so the FEA runs both motors stalled at that 105 mN·m. That's 14 N on a pinion tooth (over its 7.5 mm pitch radius) and 34 N on the axle gear, 17 N per mesh. The Lewis hand calc (Y = 0.29, 7 mm face width) gives 6.9 MPa in the pinion and FEA peaks at about 12 MPa, higher because it includes the stress concentration at the root fillet. Against about 30 MPa for PLA across its layers that's a safety factor of 2.5, so no metal inserts. And honestly the tyre slips first: stall would push the wheel with 7.3 N, but grip is only 0.8 × 4.0 N = 3.2 N.
| Quantity | Result |
|---|---|
| Tooth force at stall, pinion / axle gear | 14 N / 34 N |
| Pinion stress, Lewis hand calc / FEA peak | 6.9 MPa / 12 MPa |
| Safety factor, PLA across layers | 2.5 |
| Wheel force at stall vs traction limit | 7.3 N vs 3.2 N |
The structure is fine too. Under 15 N per module (3.8 times the static wheel load, for bumps and drops) the bearing bracket peaks at 1.55 MPa (0.008 mm deflection) and the swerve base at 0.82 MPa (0.009 mm), so there's weight to take out later. So the numbers settled the design: 15 : 112 for steering, 92 : 16 for driving, two N20s on every wheel and printed PLA gears with no inserts. The one loose end is that assumed stall torque, so measuring it is next.
Building it in CAD
I modelled everything in Fusion 360: four swerve modules with ring-gear hold-downs, a four-piece printed chassis sized to my printer bed, and a centre bay for the Pi 5 and ESP32 under a slim cap, with the RPLIDAR C1 on top. Then came print-and-fit cycles. The first print used two internal fixtures to keep the rings stacked and it didn't work: the bottom gear kept catching on the top one and the module locked up (not ideal for a swerve drive haha 😅). A third fixture evened out the pressure, and after that print I added an internal rail so the motor wires couldn't get pulled into the mesh. Finally, an interference analysis checked that every part fits.
The model has kept up with the real robot since then. These are screenshots of the current Fusion assembly (November), with the camera tilt head on one corner and the centre cap that now holds the LiDAR.
Wiring it up on a perfboard
Before ordering a PCB I proved one module on a hand-wired perfboard: an ESP32, one TB6612FNG driver, both motors for the module and the hall input, laid out hole by hole so the pin mapping matches the final PCB. The test firmware runs the motors at 20 kHz PWM (above hearing, so no whine), ramps speed instead of jumping, stores calibration in flash, and has a hall-sensor homing routine that I've written but haven't run on the bench yet haha. I drive it over USB serial, for example m 0 50 2 for one motor or d 1 40 0 3 for a module without steering. Getting it running meant chasing three faults: a USB-C power quirk, a solder bridge on the serial pins, and a resistor across the driver's signal pins. After that the module drove and steered on command, so the differential drive works on real hardware.
MixingsetModule
void setModule(int m, float drive, float steer) {
// same sign on both rings = module spins in place
// opposite signs = wheel drives, module stays pointed
setMotorPct(2 * m, drive + steer);
setMotorPct(2 * m + 1, -drive + steer);
}
Each module is two motors. Same number on both rings and it steers. Equal and opposite and the wheel just drives.
Pins and PWMIN1, IN2, 20 kHz
const int STBY_PIN = 2;
const int IN1[8] = { 4, 13, 16, 18, 21, 23, 26, 32 };
const int IN2[8] = { 5, 14, 17, 19, 22, 25, 27, 33 };
const int HALL[4] = { 34, 35, 36, 39 };
const uint32_t PWM_FREQ = 20000;
const uint8_t PWM_BITS = 8;
const float RAMP_PER_MS = 0.004f;
void writeMotor(int i, float v) {
v = constrain(v, -1.0f, 1.0f);
uint32_t duty = (uint32_t)(fabsf(v) * 255.0f + 0.5f);
if (v > 0) { ledcWrite(IN1[i], duty); ledcWrite(IN2[i], 0); }
else if (v < 0) { ledcWrite(IN1[i], 0); ledcWrite(IN2[i], duty); }
else { ledcWrite(IN1[i], 0); ledcWrite(IN2[i], 0); }
}
STBY stays low until I type en. I don't want eight motors spinning when the
board boots. PWM is 20 kHz so the N20s don't whistle. Duty ramps to full in 250 ms instead
of slamming the gearbox.
Serial commandsm, d, home
// m 0 50 2 motor 0 at 50% for 2 seconds
// d 1 40 0 3 module 1, drive 40%, no steer, 3 seconds
// home 1 turn module 1 until the hall sees the magnet
else if (k == "m" && n >= 3) {
int i = (int)a;
setMotorPct(i, b);
armTimer(n >= 4 ? d : 0);
}
else if (k == "d" && n >= 4) {
int m = (int)a;
for (int j = 0; j < 4; j++)
if (m == 0 || m == j + 1) setModule(j, b, d);
armTimer(n >= 5 ? e : 0);
}
One motor at a time while I was debugging wiring. Then the d command once a
module actually mixed. Homing is written. I have not run it on the bench yet.
The custom PCBs
Hand-wiring four drivers would have meant two stacked perfboards and dozens of solder links, so I designed a controller PCB in KiCad: 33 × 88 mm, two layers, with the ESP32, four TB6612FNG drivers for the eight motors, JST-PH sockets for the motors and hall sensors, and a 12 V input with bulk decoupling. I autorouted it with Freerouting, added ground pours and a motor-power pour, and checked every net against the firmware pin map. It passes DRC with zero errors and zero unconnected nets, and the USB port lines up with the slot in the printed cap. PCBWay made it with the drivers and passives already assembled, and I soldered on the sockets and plugs.
Here are both boards raytraced in KiCad's 3D viewer: the motor controller, and the small tilt-head board that carries an RP2040-Zero and a TB6612FNG module for the camera.
Building it for real
With the PCB in, I printed and assembled the rest: four modules, the chassis, the centre bay with the Pi 5, the battery and the LiDAR, and a camera on a tilting head with its own little RP2040 board. A robot that steers by timing has to be measured before it can drive, so the robot tool on the Pi walks through eight setup steps: motor and hall checks, steering direction, homing each wheel on its magnet, timing how fast each module steers, drive balance, floor checks with the LiDAR, and calibrating the tilt head. These clips are from 18 and 21 November, filmed on my phone and sped up.
How the software fits together
The software runs on three chips. The Pi 5 reads the LiDAR and camera, builds an obstacle map and a map of people, plans a route and picks a driving direction by sweeping the robot's footprint along each option. The ESP32 runs the eight motors with timed steering and checks every command against hard limits. The RP2040 holds the camera's tilt angle with an AS5600 sensor. The red path in the drawing is the safety layer: it reads the raw LiDAR scan, not the map, and goes straight to the motor controller.
Navigation results
To tune the driving without wearing out the robot I built a simulator that copies its flaws: wheels that drift 3° a second, steering timed rather than measured, and stops to re-check each wheel on its magnet. The real driving code runs against it unchanged. This is 42 point-to-point trips across three rooms, before and after a round of tuning:
| Measure | Before | After |
|---|---|---|
| Average time per trip | 34.3 s | 30.0 s (12.5 % faster) |
| Average miss at the goal | 7.4 cm | 4.1 cm |
| Worst miss at the goal | 11.7 cm | 8.5 cm |
| Stops to re-check a wheel (about 5 s each) | 118 | 70 |
| Collisions | 0 | 0 |
Every change also has to pass 18 behaviour scenarios (doorways, a 50 cm corridor, a U-shaped dead end, a person walking across, the LiDAR unplugged mid-drive). These are simulation numbers; I'm collecting the same figures on the real robot now.
Hand-written planner vs reinforcement learning
I also wanted to see whether a learned driver could beat the hand-written one, so I trained a policy with PPO: 4,096 simulated robots at once, 131 million steps in roughly half an hour on a Hugging Face GPU. The simulator randomised rooms, wheel lag, slip, LiDAR noise and command delay so it couldn't overfit one world. It sees 72 LiDAR ranges plus the goal and outputs the same velocity command as the planner. Then both drove 200 random cluttered rooms behind the same safety layer:
| Planner | RL policy | |
|---|---|---|
| Reached the goal | 94 % | 84.5 % |
| Median time to goal (rooms both finished) | 8.5 s | 7.6 s |
| Rooms where it passed closer than 3 cm | 0 | 31 |
| Collisions | 0 | 0 |
The policy is about 11 % faster when both succeed, but it reaches the goal less often and in 31 rooms it cut within 3 cm of something. The planner never came closer than 39 mm. On a robot whose motor drivers have already burned out, I kept the hand-written planner in charge, and the policy stays as a benchmark to beat with more training.
What broke, and how I fixed it
Ten real faults from two weeks of testing, in order. I found them on the robot with a multimeter, screen recordings and the robot's own logs. Tap one to open it.
13 Nov · The tilt motor driver smokedtilt driver, 6 V cap
During the first tilt-head tests the driver on the tilt board smoked and took the RP2040 with it. The tilt motor is a 3 to 6 V N20, but my test scripts could push it at up to 100 % of a 12.6 V battery, and a stalled motor at double its voltage pulls more than the TB6612's 1.2 A.
Fix: a hard 6 V cap in the head's firmware (power limited to 6 ÷ battery volts whatever is asked), a soft start, a stall stop, and the uncapped test scripts retired.
15 Nov · ...and the replacement died toomultimeter diagnosis
The head stopped moving both ways. With a multimeter: battery present, supply present at the driver, control signals present, nothing at the motor plug. The driver's output stage was dead after many stalls at the 6 V limit.
Fix: stall stop cut to 0.4 s, a 4 s cool-down between retries, give up after three stops, and a replacement tilt board that I bench-tested from the Pi and recalibrated on 21 Nov.
15 Nov · It slid instead of turningwheel re-set during turns
The first real people search stopped with "it slid 35 cm instead of turning": wheel 2 had swung 45° off. Turning on the spot scrubs the tyres round a tight circle, and nothing holds a differential swerve wheel's angle except the balance of its two motors.
Fix: the robot now re-sets the wheels on their magnets during the turn (or as soon as the LiDAR sees it slide 6 cm) and learns a steering nudge per wheel, so later turns need fewer stops.
15 Nov · One person, seven peoplelens self-measure
The live map listed seven people when only I was in the room. The camera picture showed the ceiling, a fan and a window at once: the lens was far wider than the 45° the code assumed, so every frame put me in a different direction.
Fix: the camera now measures its own lens while the robot turns, using the LiDAR's turn angle as the reference. A sighting has to be seen for 0.7 s, and nearby sightings merge.
16 Nov · The motor drivers burned on the controller boardfirmware v7.4 limits
During setup after a rebuild the drivers on the main board burned out, so: new board, new ESP32. Most likely the ESP32 sat low enough to touch the drivers. An audit of the firmware also found that a motor command with no time limit ran forever, and nothing limited a stalled motor at full power.
Fix: firmware v7.4. Every motor command now ends by itself (30 s at most), long full-power pushes are held to 80 %, and the Pi stops when the wheels push but the LiDAR shows no movement.
18 Nov · It rebooted whenever a wheel passed its magnetsolder blob
The ESP32 restarted each time hall sensors 1, 3 or 4 saw a magnet, but never sensor 2. A solder blob joined the ESP32's reset pin to the hall-3 input on the header row next to it.
Fix: found it and removed it. The restarts stopped.
19 Nov · Stuck beside a box for two minutessix causes in the code
From a screen recording: 1.4 m from me, it kept stopping to re-check wheels that were only 7 to 9° out, and the planned route flipped between scans. There were six causes in the code, including a wheel re-check due every 4 s and a route planner that counted the stops for wheel checks as "not making progress".
Fix: trust the LiDAR's position fix between checks, hold a direction while it's still clear, stop counting wheel stops as lost time, and give up after 45 s with no progress and try another side.
19 Nov · A flat battery that looked like a software buglow-battery note
The next run went to and fro and gave up: the wheels pushed 35 cm but the robot moved 7. Nothing was wrong in the code. The battery was nearly flat.
Fix: the robot now notices when it makes under 55 % of the speed it should and says the battery is probably low.
19 Nov · The ESP32 didn't start at power-onUSB restart
"No ESP32 found" with only the ESP32 plugged in, and pressing its reset button brought it back.
Fix: the robot tool now restarts the ESP32 over USB, captures its start-up message and says exactly what's wrong instead of guessing.
22 Nov · A dead hall socket, then a shortnew board
Hall socket 4 stopped working (the sensor was fine in another socket): its 5 V pin had no continuity, so a broken track. My workaround moved the sensor to pin D15, but that pin is read at boot so flashing failed, and later a jumper by the 3V3 and GND header pins shorted them. The board scorched and two ESP32s died.
Fix: a new board and the original firmware. Lesson learned: measure resistance both ways before refitting an ESP32, and keep wires well away from the power pins.
How I used Claude
I wrote the first versions of the code myself, then used Claude (Anthropic) to check it over and debug it, and to help me extend it as the robot grew: the firmware's safety limits, navigation, the simulator, vision and the RL setup. It also helped with some of the Fusion modelling and the PCB checks. The idea, the printing and assembly, the soldering, every test on the real robot, the measurements and the decisions about where to take it were mine.
Conclusion
Two months ago one module turned on a perfboard. Now there's a whole robot that calibrates itself, finds people and drives to them round furniture, with a safety layer underneath that has already earned its keep. Next up is collecting real-robot numbers to match the simulation, getting the camera's depth estimate working for low obstacles the LiDAR can't see, and working out a Mk2 with brushless motors and a payload deck.