Reinforcement learning for locomotion
How a walking controller can be learned by trial and error in simulation: Markov decision processes, returns and value functions, the policy-gradient theorem, GAE, PPO, and the exact observation, action and reward used for Strido, plus how domain randomisation narrows the sim-to-real gap.
In Strido
The environment is StridoEnv in packages/strido-sim/src/strido_sim/rl/env.py (Gymnasium + MuJoCo); training is rl/train.py (stable-baselines3 PPO on CPU). Shared constants (CONTROL_DT, ACTION_SCALE, COMMAND_RANGES) live in packages/strido-core/src/strido/policy.py, and the robot runs the exported network in ros2/strido_control/strido_control/policy_node.py. Workflow: Simulation and RL and docs/simulation.md.
The Markov decision process
An MDP is a tuple
Returns and discounting
The return from time
Discounting keeps the sum finite and expresses a preference for sooner reward. The effective horizon is about train.py) at a policy rate of
The objective is the expected return
Value functions
They satisfy the Bellman equation
The advantage
The policy-gradient theorem
We want
In words: increase the log-probability of actions that turned out better than average, decrease it for worse ones. Only samples from the simulator and the gradient of the network's own log-probability are needed. Using the advantage (rather than
Generalised advantage estimation (GAE)
The advantage is estimated from a learned critic
GAE takes an exponentially weighted sum of these:
gae_lambda in train.py). On termination (a fall) the bootstrap
PPO: proximal policy optimisation
Plain policy gradient takes one step per batch, and a step that is too large can wreck the policy. PPO reuses each batch for several epochs while preventing the new policy from drifting far from the one that collected the data. With the probability ratio
the clipped surrogate objective is
For a good action (
For continuous actions the policy is a diagonal Gaussian
Strido's settings (train.py): ent_coef=0.0: exploration comes only from the Gaussian's own spread, log_std_init=-1.0), VecNormalize, observations clipped at
Worked example. Suppose an action had
The Strido environment
Timing. Policy at CONTROL_DT = 0.02), physics at
Observation (OBSERVATION_SIZE):
: body angular velocity from the IMU gyro; : the unit gravity direction in the base frame, from the IMU quaternion (encodes roll and pitch, not yaw); : the velocity command, sampled per episode from , , , and zero with probability ; : the previous action; - a gait clock
with ( GAIT_PERIOD_S), matching the scripted trot.
Every input is available on the real robot from /imu/data and /cmd_vel; there is no base velocity or joint feedback (the servos have none).
Action.
ACTION_SCALE slew_limit caps joint speed at clamp_targets applies the joint and knee-crank limits in the same order as the firmware, so the policy never learns to rely on motions the robot would refuse.
Reward. With base-frame linear velocity
The two exponential terms reward tracking the command (at most CONTROL_DT * 50, which equals
Termination. The episode ends with a
Worked example. Commanded
Domain randomisation and the sim-to-real gap
A policy that exploits simulator quirks fails on hardware: the sim-to-real gap. Sources include unmodelled servo dynamics and backlash, friction, mass and inertia errors, sensor noise and latency. Domain randomisation trains on a distribution of simulators so the real robot is, hopefully, just one more sample:
where StridoEnv._randomise samples per episode:
| Parameter | Range |
|---|---|
| servo stiffness | |
| base mass | |
| floor and foot friction | |
| gyro noise | |
| gravity-vector noise | |
| initial joint angles |
Because the policy cannot identify which
Check yourself
What is the effective horizon in seconds for
at , and what if the policy ran at with the same ? Answer
About
steps ; at it halves to , so should rise to about to keep the same horizon. Why does subtracting
in the policy gradient not bias it? Answer
, so any state-only baseline has zero expected contribution. With
and , for which does the PPO term stop giving a gradient? Answer
For
: the action's probability has already been reduced enough. Why does the observation contain the gravity direction rather than Euler angles including yaw?
Answer
Walking does not depend on heading, and yaw from a magnetometer-less IMU drifts; the gravity vector is drift-free and continuous.
Name two sim-to-real effects not covered by Strido's randomisation.
Answer
For example sensor and actuation latency, servo backlash or deadband, external pushes, uneven terrain.
Further reading
- R. S. Sutton and A. G. Barto, Reinforcement Learning: An Introduction, 2nd ed., MIT Press, 2018.
- J. Schulman, F. Wolski, P. Dhariwal, A. Radford and O. Klimov, "Proximal Policy Optimization Algorithms", arXiv:1707.06347, 2017.
- J. Schulman, P. Moritz, S. Levine, M. Jordan and P. Abbeel, "High-Dimensional Continuous Control Using Generalized Advantage Estimation", ICLR, 2016.
- J. Tobin et al., "Domain Randomization for Transferring Deep Neural Networks from Simulation to the Real World", IROS, 2017.
- J. Hwangbo et al., "Learning agile and dynamic motor skills for legged robots", Science Robotics, 2019.