Hexapod, part 2 of 4
Gait & kinematics
How a velocity command becomes twenty servo positions: the geometry of one leg, the equations that place its foot, the path each foot follows, and the timing that keeps three feet on the ground at all times.
One leg, three joints
Each leg is a three-joint chain. The coxa joint swings the leg forward and back around a vertical axis. The femur and tibia joints lift and extend it in a vertical plane. Six of these legs sit around the body: the middle pair points straight out, and the front and rear pairs are angled at 45°.
Inverse kinematics
The gait works in foot positions, not joint angles: it decides where each foot should be, and a small solver turns that target (x, y, z), measured from the coxa joint, into three angles. The coxa angle comes straight from the direction of the target. With the coxa set, the femur and tibia form a triangle with the line d from the femur joint to the foot, and the law of cosines gives both angles:
If a target is out of reach, d is clamped to the length of the fully stretched leg so the arccosines stay defined, and the leg simply extends as far as it can. The same lengths run the other way in the odometry node, where forward kinematics turns measured joint angles back into foot positions (see part 3).
The path of a foot
Each step has two phases. In stance the foot is on the ground and slides backwards in a straight line relative to the body, which pushes the body forwards. In swing it lifts off, travels forward through the air and lands at the front of its range. The swing follows a cubic Bézier curve. Its four control points make the foot leave and land almost vertically, so it doesn't scuff the ground at either end.
To walk in any direction without turning first, the whole stroke is rotated by atan2(vy, vx) before solving the kinematics. To turn, the strokes on the left and right sides run in opposite directions, scaled by the yaw rate, much like a tracked vehicle. When the command contains both, the two strokes are blended.
Timing the legs
A gait is just a phase offset per leg. The default is the tripod: legs R1, R3 and L2 move together, alternating with R2, L3 and L1, so there are always at least three feet on the ground forming a triangle under the centre of mass. Each stance phase also gets a 20% overlap, so between swings all six feet are on the ground for a moment.
The phase offsets are ROS parameters, so the gait can be changed while the robot runs:
| Gait | Legs moving at once | Character |
|---|---|---|
| Tripod | 3 | Fastest; always a stable triangle of support |
| Bipod | 2 | Four feet down at any time; slower, sturdier |
| Ripple | 1 | Legs swing one after another around the body; slowest, most stable |
Cycle time, step length, step height and body height are live parameters too. With the default 3-second cycle and 150 mm stroke, the body moves at about 7 cm/s. Shortening the cycle makes it walk faster.
From node to servos
The gait controller is a ROS 2 node that listens to a standard cmd_vel
velocity command, so a gamepad, a voice command or the Nav2 planner can all drive it
the same way. Ten times per second it computes the six foot targets, solves the
kinematics and sends each leg a short trajectory goal to reach in 0.15 s.
- ros2_control runs seven joint-trajectory controllers at 50 Hz, one per leg and one for the head, which interpolate between goals.
- Hardware: the Dynamixel hardware interface drives the 20 servos (IDs 1–20) over a single 1 Mbit/s serial bus.
- Simulation: the same controllers load in Gazebo, through its ros2_control plugin, and in Isaac Sim through a bridge client. The gait code doesn't know which one it is driving.
- Head: the gamepad's D-pad pans the camera head ±72° and tilts it ±37°.
- Stopping: when the command drops to zero, the legs return to the standing pose one at a time, in two moves each, so the body never sags.
Limits of a scripted gait
The gait is open-loop. It doesn't sense when a foot touches the ground, so on uneven terrain a foot can land early or late without the timing adapting. It also assumes all six legs work. Those two limits are what led to the learned controllers in part 4.