30% off your first 3 months

Get the deal

How legged robots handle stairs and slopes

by DuckyHelperNov 13, 20264 min read
Robotics

legged robotsquadruped

TL;DR:

  • A legged robot climbs by sensing the step height, lifting each foot above it, and placing the foot on the step before shifting weight.

  • Check that each leg can reach the new foot position, and that the body pitch matches the rise: pitch = asin(rise ÷ body length).

  • Test with the robot held off the ground first, at low speed, and never handle a powered robot's legs or a damaged battery.

A wheeled robot meets a stair as a wall. A legged robot meets it as a sequence of choices: where to put each foot, how high to lift it, and how to tilt the body. This article walks through those choices for a small hobby quadruped (a four legged robot) climbing one stair, and shows the arithmetic you can check.

What you will build

The goal is a small robot that detects a single step in front of it, lifts its front feet over the edge one at a time, and then brings its rear feet up. You will not build a full walking controller here. You will build the stair logic and check it with numbers before any motor moves.

Parts and tools

  • A quadruped hobby kit or your own design, with two joints per leg (hip and knee) driven by servos.

  • A microcontroller or small computer that can run Python or similar code.

  • A distance sensor pointing forward and slightly down, and an IMU (a sensor that reports tilt) in the body.

  • A battery pack matched to your servos' rated voltage, with a switch you can reach, and a fuse or protected pack.

  • A step made from a book or a block, about 4 cm high, and a tape measure.

Wiring and assembly

Power the servos from the battery pack, not from the controller's own pin, because servos draw sharp bursts of current. Connect the grounds of the servo supply and the controller together, otherwise the control signals have no reference. Route wires so they cannot get caught in a moving knee. Follow the manufacturer's instructions for your parts, and read the safety checklist for motors and batteries before you power anything.

The geometry, step by step

Use a made up robot: thigh and shank each L = 0.10 m, a standing hip height of 0.15 m, a body length (front hips to rear hips) of 0.20 m, and a stair rise h = 0.04 m. This is the worked example: a quadruped stepping up one stair.

  1. Perception. The forward sensor reports the rise. Here it measures 0.04 m. Add a clearance margin of 0.02 m so the toe does not scrape: the foot must reach a swing height of 0.04 + 0.02 = 0.06 m above the floor.

  2. Foot placement. Choose a landing point on the step, 0.05 m ahead of the hip (horizontal), at height 0.04 m. Relative to the hip, which is 0.15 m high, the foot is 0.15 − 0.04 = 0.11 m below and 0.05 m forward.

  3. Reach check. The hip to foot distance is d = √(0.11² + 0.05²) = √(0.0121 + 0.0025) = √0.0146 = 0.121 m. A two link leg can reach anything up to L1 + L2 = 0.20 m, so the target is reachable.

  4. Knee angle. From the law of cosines, cos θ = (d² − L1² − L2²) ÷ (2·L1·L2) = (0.0146 − 0.02) ÷ 0.02 = −0.27, so θ = 105.7° of bend away from a straight leg.

  5. Body pitch. With the front feet on the step and the rear feet on the floor, the body tilts by asin(0.04 ÷ 0.20) = asin(0.2) = 11.5° nose up, if the legs keep their lengths.

The same pitch idea covers a slope. A ramp that rises 1 m over 5 m of horizontal run has an angle of atan(1 ÷ 5) = 11.3°, so the body should tilt about that much to stay roughly parallel with the surface.

The code, step by step

The logic is a short sequence, written here as plain Python with your own read_rise(), move_foot() and shift_weight() functions for your hardware.

import math

L1 = L2 = 0.10
MARGIN = 0.02

def knee_bend(dx, dz):
    d2 = dx * dx + dz * dz
    if math.sqrt(d2) > L1 + L2:
        return None  # out of reach
    c = (d2 - L1**2 - L2**2) / (2 * L1 * L2)
    return math.degrees(math.acos(c))

def climb_one_stair(read_rise, move_foot, shift_weight):
    rise = read_rise()
    if rise > 0.06:           # too high for this robot
        return False
    swing = rise + MARGIN
    for leg in ["front_left", "rear_right", "front_right", "rear_left"]:
        if knee_bend(0.05, 0.15 - rise) is None:
            return False
        move_foot(leg, lift_to=swing, land_on=rise)
        shift_weight()
    return True

Move one foot at a time and keep the other three on the ground. That keeps the body supported by a stable triangle at every moment.

Test, measure and fix

  1. Run the code with the robot lifted off the table on a stand, and watch the legs move. Nothing should hit the body or the wires.

  2. Put the robot on the floor with no stair and check that it can step in place.

  3. Add a 2 cm block, then 4 cm. Watch the toe: if it scrapes, raise MARGIN.

  4. Print the IMU pitch. It should match your calculation of about 11° when the front feet are up.

  5. If the robot tips back, move the landing point closer to the hip or lower the body.

Make it better

  • Use the IMU to adjust the body angle during the step rather than only at the end.

  • Add a foot contact sensor, so the robot knows when the foot has really landed.

  • Let the robot measure a slope with the IMU and tilt its body to match.

  • Learn the coding steps in a six week path to code for robots, and compare kits in choosing your first robot kit.

Stay safe

Before you power it: Servos can pinch fingers, so keep hands out of the legs while powered and switch off before adjusting. Keep a power switch within reach. Use the battery type and charger your parts require, never charge a swollen or damaged lithium battery, and charge on a non flammable surface where you can watch it. Test on a stand first and at low speed.

Frequently asked questions

Why move one leg at a time?

With three feet down, the centre of the body can stay above the triangle they form, so the robot stays balanced.

What if the stair is higher than the legs can reach?

Your reach check catches it. Return false and stop, rather than trying the move.

Get started

DuckyHelper can
do more

It sees your screen and works in any app you use. Try it for 10 minutes, no card.

Get it for Mac, Windows or Linux

Share

Questions and jobs

Do anything

Ask it a question or hand it a job, in any app.

Doing jobs for you is in the Mac app. Asking works on Mac, Windows and Linux.