Chromapi dancing in the MuJoCo simulator

Don’t have the robot yet, or want to experiment with movements on Chromapi without risking breaking it? There is a simulated version of Chromapi in MuJoCo: the same code drives the simulated robot and the real one.

Prerequisites

  • Chromapi’s SDK must be installed with the sim extra: see the SDK installation page.

Running the examples

The examples/ folder contains a few programs to try Chromapi out. They are not included in the pip package: download them from the repository, or install the SDK from source. Each one runs in MuJoCo with the --sim flag:

python examples/walk.py --sim              # a round trip: forward, sideways, quarter turn
python examples/walk.py --sim --follow     # the camera follows the robot
python examples/dance.py --sim             # dance in place
python examples/gamepad.py --sim           # drive with a controller paired to the PC

Without --sim, these programs also run on the real robot.

Web app in simulation

The web app also works with the simulated robot, with the command:

chromapi run --sim

The web app is then available at https://localhost:5000. Its certificate is self-signed: on the first visit, the browser shows a security warning that you must accept to continue.

In your code

When you write a program for Chromapi, you choose between simulation and the real robot with the backend parameter:

from chromapi.chromapi import Chromapi

with Chromapi(backend="mujoco", launch_viewer=True) as robot:
    robot.wake_up(wait=True)
    robot.walk_distance(0.3)  # [m]
    robot.rest()

With this code, a MuJoCo window opens, the robot stands up, walks forward 30 cm then lies down. Replace "mujoco" with "hardware" to run exactly the same code on the real robot.

Here are the main parameters of a Chromapi instance:

ParameterEffect
backend="mujoco"Drive the robot in simulation
backend="hardware"Drive the real robot. This is the default value
backend="sim-server"Connect to a simulation running separately with chromapi sim (see the section below)
launch_viewer=TrueOpen a MuJoCo window. Without it, the simulation runs with no display
follow_robot=TrueWith launch_viewer=True, the camera follows the robot (“Esc” releases it)
mjcf_path=...With backend="mujoco", load a scene other than the default one

Keeping the simulation open

With backend="mujoco", the simulation lives inside your program: every run reopens the window and makes the robot stand up again. When you are tuning a script and rerunning it often, run the simulation separately instead, in a first terminal:

chromapi sim              # the MuJoCo window, robot lying down
chromapi sim --follow     # the camera follows the robot
chromapi sim --headless   # no window (Ctrl+C to stop it)

In a second terminal, your script connects to it with backend="sim-server":

from chromapi.chromapi import Chromapi

with Chromapi(backend="sim-server") as robot:
    robot.wake_up(wait=True)
    robot.walk_distance(0.3)  # [m]
    robot.rest(wait=True)
  • Your script keeps the whole SDK. The simulation only handles physics and display, like the motherboard on the real robot.
  • When the script stops, the simulation keeps running and the robot stays where it is: the next run starts from there. To start from scratch, restart chromapi sim.
  • Only one script drives the simulation at a time, as on the robot. A second one stops automatically (A script is already driving the simulation).
  • If chromapi sim is not running, the script does not start (No simulation on localhost:7447).
  • Closing the window, or stopping chromapi sim, also stops the running script.

The simulation only accepts programs from your own computer.

Differences with the real robot

On the robotIn simulation
Ring of 18 LEDsThe ring shows the average color of the 18 LEDs
Robot speakerSound goes in and out through the computer
Battery chargeNot simulated: battery_percent is None
Servo temperatureNot simulated
Camera and microphoneNot simulated

Walking is open-loop: the robot does not measure the distance it actually travels. Distances therefore differ a little between simulation and a real floor, which is more or less slippery.