# Teleoperation and Learning Policies with Robosuite

[**Robosuite**](https://robosuite.ai/) is a simulation framework powered by the [MuJoCo](http://mujoco.org/) physics engine for robot learning. It also offers a suite of benchmark environments for reproducible research. MagiClaw provides simple and intuitive operation, real-time output of 6D pose and gripper angle status information, making it very suitable as a remote control device for Robosite. This page will introduce how to teleoperate robots using MagiClaw, collect data and train policies in robosuite.

<figure><img src="/files/Gl5FliltkKvygklAgVVC" alt=""><figcaption></figcaption></figure>

## Install RoboSuite

Robosuite officially supports macOS and Linux on Python 3. It can be run with an on-screen display for visualization or in a headless mode for model training, with or without a GPU.

The base installation requires the MuJoCo physics engine (with [mujoco](https://github.com/deepmind/mujoco), refer to link for troubleshooting the installation and further instructions) and [numpy](http://www.numpy.org/). To avoid interfering with system packages, it is recommended to install it in a virtual environment by first running `virtualenv -p python3 . && source bin/activate` or setting up a Conda environment by installing [Anaconda](https://www.anaconda.com/) and running `conda create -n robosuite python=3.10`.

**Step 1.** Clone the latest repository modified with MagiClaw:

```bash
git clone https://github.com/asMagiClaw/magiclaw-robosuite.git
cd magiclaw-robosuite
```

**Step 2.** Install the base requirements:

```bash
pip install -r requirements.txt
```

**Step 3.** Test the installation:

```bash
python robosuite/demos/demo_random_action.py
```

{% hint style="warning" %}

### WARNING

Mac users who wish to use the default mjviewer renderer need to prepend the “python” command with “mj”: `mjpython ...`
{% endhint %}

## Teleoperation with MagiClaw

The in-hand MagiClaw works as an I/O device in robosuite, providing the TCP pose and gripper angle to the robot controllers.&#x20;

**Step 1.** Start the MagiClaw via the app, CLI or dashboard.

**Step 2.** To start controlling a robot in robosuite using the data streamed from the MagiClaw, run:

```bash
python robosuite/demos/demo_device_control.py --host <host>
```

where `<host>` refers to the IP address of the MagiClaw control box.&#x20;

And an OpenCV window created by the mujoco renderer.

**Step 3.** Use MagiClaw to control the robot in robosuite, and the control command is shown below.

| Control                        | Command                                          |
| ------------------------------ | ------------------------------------------------ |
| Move MagiClaw laterally        | mover arm horizontally in x-y plane              |
| Mover MagiClaw vertically      | move arm vertically                              |
| Twist MagiClaw about an axis   | rotate arm about a corresponding axis            |
| Press/release MagiClaw trigger | open/close gripper                               |
| Ctrl+C                         | quit                                             |
| Ctrl+Q                         | reset simulation                                 |
| B                              | toggle arm/base mode (if applicable)             |
| S                              | switch active arm (if multi-armed robot)         |
| =                              | switch active robot (if multi-robot environment) |

In `robosuite/demos/demo_device_control.py`, we defined a `PandaWithMagiClaw` class and registered as a `FixedBaseRobot`:&#x20;

```python
@register_robot_class("FixedBaseRobot")
class PandaWithMagiClaw(Panda):
    """
    Panda robot with MagiClaw gripper.
    """

    @property
    def default_gripper(self):
        return {"right": "MagiClaw"}
    
    @property
    def init_qpos(self):
        return np.array([0.00, 0.00, 0.00, -np.pi / 2.0, 0.00, np.pi / 2.0, np.pi / 4])
```

This is a Franka Panda mounted with a MagiClaw (on-robot type). If you want to change to another robot,  just create another class, modify the `default_gripper` and `init_qpos` properties, and add `@register_robot_class`.&#x20;

## Collecting Human Demonstrations

Robosuite provides teleoperation utilities that allow users to control the robots with input devices. Such functionality allows us to collect a dataset of human demonstrations for learning. We provide an example script to illustrate how to collect demonstrations. The [collect\_human\_demonstrations](https://github.com/asMagiClaw/magiclaw-robosuite/blob/master/robosuite/scripts/collect_human_demonstrations.py) script has the following arguments:

* `directory:` path to a folder for where to store the pickle file of collected demonstrations
* `environment:` name of the environment you would like to collect the demonstrations for
* `device:` “keyboard” or “magiclaw”
* `renderer:` Mujoco’s builtin interactive viewer (mjviewer) or OpenCV viewer (mujoco)
* `camera:` Pass multiple camera names to enable multiple views. Note that the “mujoco” renderer must be enabled when using multiple views, while “mjviewer” is not supported.

## Replaying Human Demonstrations

We have included an example script that illustrates how demonstrations can be loaded and played back. Our [playback\_demonstrations\_from\_hdf5](https://github.com/asMagiClaw/magiclaw-robosuite/blob/master/robosuite/scripts/playback_demonstrations_from_hdf5.py) script selects demonstration episodes at random from a demonstration pickle file and replays them.

## Structure of collected demonstrations

Every set of demonstrations is collected as a `demo.hdf5` file. The `demo.hdf5` file is structured as follows.

* data (group)
  * date (attribute) - date of collection
  * time (attribute) - time of collection
  * repository\_version (attribute) - repository version used during collection
  * env (attribute) - environment name on which demos were collected
  * demo1 (group) - group for the first demonstration (every demonstration has a group)
    * model\_file (attribute) - the xml string corresponding to the MJCF mujoco model
    * states (dataset) - flattened mujoco states, ordered by time
    * actions (dataset) - environment actions, ordered by time
  * demo2 (group) - group for the second demonstration

    …

    (and so on)

The reason for storing mujoco states instead of raw observations is to make it easy to retrieve different kinds of observations in a postprocessing step. This also saves disk space (image datasets are much larger).

## Using Demonstrations for Learning

The [robomimic](https://arise-initiative.github.io/robomimic-web/) framework makes it easy to train policies using your own [datasets collected with robosuite](https://robomimic.github.io/docs/datasets/robosuite.html). The framework also contains many useful examples for how to integrate hdf5 datasets into your own learning pipeline.

The robosuite repository also has some utilities for using the demonstrations to alter the start state distribution of training episodes for learning RL policies - this has proved effective in [several](https://arxiv.org/abs/1802.09564) [prior](https://arxiv.org/abs/1807.06919) [works](https://arxiv.org/abs/1804.02717). For more information see the `DemoSamplerWrapper` class.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://magiclaw.gitbook.io/magiclaw-docs/documentation/resources/teleoperation-and-imitation-learning-with-robosuite.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
