BLOG

Code-as-a-Policy and the Sim-to-Real Gap: Transfer Across Robots and Tasks

TL;DR

  • In our previous post, we said the main cause of the sim-to-real gap was the robot itself, not the task. This post tests that claim twice, with transfer across robots and with transfer across tasks.
  • Across robots: we moved the same task from the dual-arm Robot A to the single-arm Robot B. The task carried over smoothly. Most of the effort went into characterizing the robot itself.
  • Across tasks: keeping Robot B, we changed the parts, the base the pegs go into, and the placement positions. The control programs improved in simulation worked on the real robot.
  • If the gap is in the robot, the fixes should target the robot. In the second half, we go through domain randomization and three other approaches (real-to-sim, active exploration, and online adaptation), and describe two cases from Physical RSI: reflecting real-robot data in the simulator, and a spiral search.
Transfer across robots and transfer across tasks, side by side. Left, across robots: the same task moved from the dual-arm Robot A to the single-arm Robot B. The task carried over smoothly, and the gap was mainly in the robot itself. Right, across tasks: on the same Robot B, with different parts, a different base and different placement positions, the improved program ran smoothly and almost no new gap appeared. The line below says our checks point to the robot, not the task, as the main cause of the sim-to-real gap.
Figure 1. Left: transfer across robots, with the same task. Right: transfer across tasks, with the same robot. Robot A is a photo of the real robot, and Robot B is shown in simulation.

1. What we wanted to check

Physical RSI (recursive self-improvement) adds a self-improvement loop to Agent-as-a-Policy, in which a coding agent operates the robot (our first post). The loop improves a control program, that is, code, and that code runs as the robot's policy. In this post we call it a Code-as-a-Policy. In our previous post, we ran the improved program on real robots and saw that the main cause of the sim-to-real gap is the robot itself, not the task.

If that is right, two things should follow:

  • Transfer across robots (cross-embodiment transfer): when we move the same task to a different robot, the task carries over unchanged, and the gap shows up as differences in the robot.
  • Transfer across tasks: when we change the task on the same robot, almost no new gap appears.

We checked both on real hardware (Figure 1).

2. Transfer across robots: most of the work was in the robot

At the end of our previous post, we described moving the task to a second robot, Robot B. Robot B is a single-arm robot. It differs from the dual-arm Robot A in the number of arms and joints, and in its motors, gripper, and cameras.

We kept the task flow that we had set in simulation almost unchanged: pick up a part, insert its pegs into the holes of the base plate, and place the part in the output tray. The task moved over smoothly (Figure 2).

Figure 2. Robot B at work on the real hardware, replayed sped up from images taken intermittently by the fixed camera. The parts are blurred.

Most of the work went into characterizing the robot itself. For example:

  • A joint stops short of the commanded position.
  • Camera images arrive some time after they were captured.
  • The gripper moves differently from its simulation model.

Each of these differs from machine to machine, and none of them is in the simulator or the documentation. They are the same kind of problem we found on Robot A.

We handled them the same way as before. We had the agent measure the robot on the real hardware, and kept the results as robot data (measurements and models specific to that robot) and skills (reusable procedures).

3. Transfer across tasks: almost no new gap appeared

Next, we kept Robot B and tried different tasks. For example, we changed the parts, the base the pegs go into, and where the parts are placed.

For each task, the agent improved the control program in simulation, and we ran it on the real robot. Because the robot was the same, what we had learned about it still held. The programs ran smoothly.

Parts, base, and placement can all be varied in simulation. The robot's own properties do not change when the task changes. If the main cause of the gap is the robot, then as long as the robot stays the same, changing the task should produce almost no new gap. Within what we tried, that is what we saw on the real robot.

4. If the gap is in the robot, how do we deal with it?

The robot's actual latency, friction, joint zero positions, and so on are not known exactly in advance. Approaches to the gap differ in where they handle that uncertainty, and we sort them into four (Figure 3). Our previous post covered domain randomization, and robot data and skills. Here we go through all four briefly.

Figure 3 is a schematic in which the spread of simulator settings (latency, friction, and so on) is drawn as dots in two dimensions. The star is the real robot's true value, which we usually do not know exactly. The crosses are measurements on the real robot, the dashed ellipse is the range of settings, and the rings are estimates.

Four approaches to the sim-to-real gap in one schematic. The star is the real robot's true value, the dots are simulator settings, the crosses are real-robot measurements, the dashed ellipse is the range of settings, and the rings are estimates. Domain randomization widens the range so the star falls inside. Real-to-sim reflects real measurements in the simulator and narrows the range. Active exploration picks only the most useful measurements. Online adaptation moves its estimate toward the star while the robot runs.
Figure 3. The four approaches in one schematic.

Domain randomization

The uncertainty is covered by the training range. We train over a wide range of simulator conditions so that the real robot's values fall somewhere inside it. It works even when the real values are unknown. But because the policy has to work under every condition, a range that is too wide can make it conservative. For an accessible introduction, see Lilian Weng's blog post.

Real-to-sim

The uncertainty goes into the simulator. This reverses the usual direction: real-robot data comes back into the simulator and brings it closer to the robot. Examples include system identification, which estimates mass and friction from the robot's motion, and learning a motor model from real data (Hwangbo et al., 2019). Domain randomization covers a wide range, while real-to-sim narrows it to a region near the real robot's values.

We use this approach in Physical RSI as well. After a few runs on the real robot, we hand the logs to the agent, which analyzes them and estimates two things:

  • ① The robot's parameters (mass, friction, and so on).
  • ② The pattern of errors on the real robot (for example, how far a joint stops short of its target, or how a grasped part shifts in the hand).

Both are reflected in the simulator (Figure 4). The robot data in our previous post, a simulation model matched to the real robot plus measured parameters, belongs here.

We also use the identified parameters to estimate force. Robot B has no force sensor, so we estimate external force from the motor torques. The left side of Figure 4 (①) shows an example: it compares a period when nothing touches the arm (the estimate should be zero) with a period when the arm presses on a surface with a steady force. With the CAD parameters, the estimate has an offset even without contact; with the identified parameters, that offset becomes smaller. While pressing, some difference remains. The right side of Figure 4 (②) is a schematic of the error pattern: the errors on the real robot have a bias and a spread, and we put errors with the same tendency into the simulator.

The real-to-sim flow. We run the real robot a few times and collect logs; the agent analyzes them, estimates (1) the robot's parameters and (2) the error pattern, and reflects both in the simulator. Bottom left (1): external force estimated from motor torque over time, with a period without contact and a period of pressing side by side. The dashed line is zero without contact and a steady target force while pressing. The estimate with CAD parameters sits below zero even without contact and reads low while pressing. The estimate with identified parameters is close to the dashed line without contact and reads slightly above it while pressing. Bottom right (2): a schematic in which the errors on the real robot have a bias and a spread, and errors with the same tendency are put into the simulator.
Figure 4. The real-to-sim flow. From real-robot logs, the agent estimates ① the parameters and ② the error pattern and reflects them in the simulator. The curves in ① show external force estimated from motor torque (momentum observer), computed with the CAD parameters and with parameters identified from ordinary run logs; they are redrawn from a figure of one real recording. The dashed line is zero without contact and a steady target force while pressing. ② is a schematic.

Even so, differences outside the range we measured and factors missing from the model remain.

Active exploration

Active exploration is a way to do real-to-sim with fewer measurements. It chooses which experiments to run on the real robot for system identification so that they yield the most information (active system identification). It is close to optimal experimental design, and its aim differs from the "exploration" of reinforcement learning, which seeks reward. For example, ASID trains an exploration policy in simulation to maximize the information gained about the unknown parameters (Fisher information), then runs it on the real robot (Memmel et al., 2024). Real-robot trials cost time and money, so needing fewer measurements is an advantage. The effort of running the robot to measure it does not go away.

Online adaptation

The uncertainty is made up for with signals available while the robot runs. While the robot is moving, it uses those signals to estimate properties that were not known beforehand, or to switch its motion. For example, RMA estimates a latent summary of properties such as friction or the weight of what the robot is holding from its recent motion, and feeds that estimate to the control program (Kumar et al., 2021). It can handle changes that cannot be measured beforehand. Structurally it resembles classical adaptive control, with an estimator and a controller, but both are learned and there is no stability guarantee. Also, most RMA-style methods are trained in simulation with randomized conditions, so they only handle changes that fall within that range and are slow enough for the estimate to keep up. So online adaptation adds an estimating module on top of domain randomization.

Physical RSI's control programs also contain an example of online adaptation: a spiral search for putting the pegs into the holes, which a human added by giving the agent the idea (Figure 5). The robot presses the part onto the surface with roughly constant force and moves it in a spiral until the pegs find the holes. The pressing force is measured with the torque-based estimate (the same method as ① in Figure 4), and the controller keeps that value steady. The insertion position error does not need to be known in advance: the search finds the holes from contact signals alone. It does assume that the error falls within the search range.

The spiral search. Top: a loop that repeats while the robot runs: measure contact signals (pressing force, height), judge whether the part is in the hole, and change the motion (keep spiralling until it is in, then push it in). It is labeled as an example of online adaptation. Bottom left: the path of the gripper seen from above, a spiral going outward from the contact point, with the hole somewhere along it; the offset of the hole is not known in advance. Bottom right: pressing force and height over time, split into before contact, pressing and searching, and in the hole pushing. The force is held roughly steady around a target; when the part drops into the hole the height falls and the force dips and then recovers.
Figure 5. The spiral search and a real-robot record. Top: the loop of measuring contact signals, judging, and changing the motion, repeated while the robot runs. Bottom left: a schematic of the gripper path seen from above. Bottom right: pressing force and height over time, redrawn from a real-robot record. The dotted lines mark the target force and the contact height.

But the self-improvement loop did not find the spiral search on its own. The loop picks programs by their score in the simulator. The simulator did not reproduce the insertion position error that occurs on the real robot, and domain randomization did not cover it either, so the spiral search was never selected. We got it only after a human engineer put the idea into the prompt: find the holes by feeling the contact force.

5. Summary and limitations

Within what we tried, the two checks support the view that the main cause of the sim-to-real gap is the robot itself. Once a robot has been characterized, we expect a new task to come down mostly to improving the Code-as-a-Policy, which can be done in simulation with compute.

The four approaches differ in where they handle the uncertainty about the robot. The more of it we can measure on the real robot and put into the simulator (real-to-sim), the more problems we can solve in simulation. What still remains is covered by the randomization range or by online adaptation.

There are three limitations:

  • We checked only two robots and one family of tasks: picking up a part, inserting its pegs, and placing it.
  • Errors that the simulator does not contain can be hard for the self-improvement loop to handle alone. A human supplied the idea behind the spiral search. Whether the loop can find such methods on its own is still an open question.
  • When we measure a robot, people still clamp it in place and start the measurement by hand.

References

← Back to Latest