Skip to content

Berthing

Abstract

This guide walks through a model of a robotic arm, in the style of Canadarm2, that grabs a visiting vehicle, carries it to a berthing point on a space station and lets it go. The arm has seven joints, each driven by a position servo, and mink, an inverse-kinematics library built on MuJoCo, calculates the joint angles that move the arm's end effector to a goal position and orientation.

The berthing run, filmed by its overview camera. The label shows the gap to the grapple fixture, the arm's clearance and whether the weld is on. Once the vehicle is grabbed, the gap is no longer measured and the label drops it.

This example puts the feature guides to work:

  • Proximity measures the gap between the arm and the grapple fixture it is closing on, and the clearance of the arm from the vehicle, the station and itself.
  • Equality Constraints provide the capture weld, which is off until the arm is in place and then holds the vehicle to the arm.
  • Video Recording films the run from a camera on the arm and from a fixed camera that follows the vehicle.

It also follows the Handoff pattern of the runtime script: the generator keeps what the runtime will need in user_data, and the runtime reads it back. And it shows how Mojo works with an established MuJoCo library: mink moves the arm, and it reads the same compiled model that Mojo builds. Install it with pip install mink before running the example.


Project Layout

Code for the example lives in the berthing folder, with one file for each part of the model:

File Holds
simulation.py The parts put together, with the generate and runtime entry points.
mission.py The distances, speeds and times that steer the berthing, and its phases.
station.py The station: a cylinder with a grapple fixture on top for the arm's base, and a berthing point.
visitor.py The visiting vehicle: a cylindrical hull with a grapple fixture, free to move.
fixture.py A short post with a site on its end, which the arm grabs. The visitor carries one, and so does the station.
arm.py The arm, with its servos, the inverse-kinematics solver and the camera on its free end.
end_effector.py A cylinder with a site on its end that latches onto a grapple fixture. The arm has the same one at each end.
capture.py The weld that joins the arm's free end effector to the visitor's grapple fixture.
overview.py A fixed camera that follows the arm's free end.
environment.py A dark gradient sky and the sun.
shapes.py Helpers shared by the parts.

How the Parts Fit Together

Each part is a class that holds what the runtime will need later, as UserData, and has a new method that builds the part into the model and returns it. Berthing is the root. It holds the five parts, and Berthing.new builds each one in turn:

Python
class Berthing(mojo.UserData):
    """Root user data: the parts the runtime needs. `generate` stores it on `mojo_model.user_data`, and the runtime reads it back with `get_user_data`."""

    station: Station
    """The station, with the fixture the arm's base is attached to."""

    arm: Arm
    """The arm, with its joints, servos, end effectors and camera."""

    visitor: Visitor
    """The visiting vehicle, with the fixture the arm grabs."""

    overview: Overview
    """The fixed camera that follows the visitor."""

    capture: Capture
    """The weld that joins the arm to the visitor."""

    @classmethod
    def new(cls, mojo_model: mojo.MojoModel) -> Self:
        """Builds every part and puts it in the model."""
        mojo_model.mjcf.model = mojo.ModelName("Berthing")
        mojo_model.mjcf.worldbody = mojo.WorldBody()

        station = Station.new(mojo_model)
        arm = Arm.new(mojo_model, station.fixture)
        Environment.new(mojo_model)
        visitor = Visitor.new(mojo_model)
        overview = Overview.new(mojo_model, arm.free_end)
        capture = Capture.new(mojo_model, arm, visitor)

        return cls(
            station=station,
            arm=arm,
            visitor=visitor,
            overview=overview,
            capture=capture,
        )

generate calls Berthing.new and stores the result on mojo_model.user_data, so the runtime can read it back with get_user_data(Berthing). It also sets the options for the whole model. Contacts stay on, with contact settings on each geom deciding which parts can touch.

Python
def generate(mojo_model: mojo.MojoModel, *args, **kwargs) -> mojo.MojoModel:
    """Generates the MJCF model."""
    mojo_model.us = US
    mojo_model.user_data = Berthing.new(mojo_model)

    mojo_model.mjcf.options.append(
        mojo.Option(
            timestep=0.002 * US.s,
            gravity=np.array((0, 0, 0)) * US["m/s**2"],
            # the servos are stiff and heavily damped, which only an implicit integrator keeps stable at this timestep
            integrator=mojo.Integrator.IMPLICITFAST,
        )
    )

    # fixed at one meter because the viewer sizes frames and joint axes by the mean body size, which the large station inflates
    mojo_model.mjcf.statistics.append(mojo.Statistic(meansize=1.0 * US.m))
    mojo_model.mjcf.visuals.append(
        mojo.Visual(scale=mojo.VisualScale(framelength=0.5, framewidth=0.03))
    )
    return mojo_model

Running It

A run has four phases: the arm approaches and lines up with the fixture, moves in slowly and switches the weld on, carries the vehicle to the berthing point, and lets go. The runtime is a loop that steps through them, and each part has methods for the controls it needs, which the loop only calls.

To watch it, run the Reloaded script from the example's folder:

Bash
./reloaded.sh

It runs mujoco-mojo reloaded -g simulation.generate -r simulation.runtime, which opens the model in Reloaded and runs it.

You can also look around the model in your browser with MuJoCo Live. It is the model as generate leaves it, before any runtime behavior, so nothing drives the arm, the weld is off, and no distance is measured.

Open the model in MuJoCo Live


Success

That is the lay of the land. In the next guide we build the station and the visitor.