# Simulation control with RPC

> Documentation version: 3.0
> HTML: https://docs.flarelive.manifolddynamics.com/manual/control/simulation.html
> [Complete contents](https://docs.flarelive.manifolddynamics.com/contents.md) · [Agent index](https://docs.flarelive.manifolddynamics.com/llms.txt)

Connect to a GUI or headless instance using [Workspace RPC](https://docs.flarelive.manifolddynamics.com/manual/operation/rpc.md) first. This page covers initialization, time advancement and actuator/sensor I/O through that connection.

## First trial: initialize and advance

This complete script runs a temporary trial of the selected Document. Begin with the [prepared P600 project](https://docs.flarelive.manifolddynamics.com/manual/tutorials/p600/index.md) or your own valid scene, with Simulation stopped and no Experiment active.

Save the script as `step_trial.py`:

```python
from pathlib import Path
from flarelive_client import connect

output = Path("trial-output").resolve()
output.mkdir(parents=True, exist_ok=True)

with connect() as client:
    ws = client.workspace

    # Prepare a temporary trial, leaving the saved scene unchanged.
    ws.begin_experiment()
    ws.set_property(ws.world_handle(), "working_directory", str(output))
    ws.initialize_simulation()

    # Advance ten steps and read the actual simulation time.
    sample = ws.exchange_simulation_io(steps=10)
    print("Completed steps:", sample.simulated_steps)
    print("Simulation time:", sample.simulated_time, "s")
    print("Time step:", sample.time_step, "s")
    ws.clear_simulation()
    ws.end_experiment()
    print("Returned to Document.")
```

Run in the activated Python environment:

```sh
python step_trial.py
```

The script prints the number of completed steps, elapsed simulated time and step size, then returns to the original Document. If initialization or advancement reports an error, inspect the log and current state; the script leaves its Experiment available for diagnosis.

## Add a controller and sensor

A control tick usually has three parts:

1. Apply actuator inputs.
2. Advance to the next control time.
3. Read sensor outputs.

`exchange_simulation_io` performs these together. The following fragment belongs **after initialization** in a scene containing a Simple Servo at `Robot/Joint/Servo` and a Position-mode Joint Sensor at `Robot/Joint/Angle`. Replace these two paths with your actual hierarchy paths.

```python
actuator = ws.find_scene_object("Robot/Joint/Servo")
sensor = ws.find_scene_object("Robot/Joint/Angle")
sample = ws.exchange_simulation_io(outputs=[sensor])
end_time = sample.simulated_time + 2.0

while sample.simulated_time + 0.5 * sample.time_step < end_time:
    deadline = min(end_time, sample.simulated_time + max(0.02, sample.time_step))
    sample = ws.exchange_simulation_io(
        inputs=[{"object": actuator, "values": [0.5]}],
        outputs=[sensor],
        until_time=deadline,
    )
    print(sample.simulated_time, sample.outputs[0].values)
```

A Simple Servo command of `0.5` requests half its model's maximum angle. The Position Joint Sensor returns the measured angle in radians. See [drives](https://docs.flarelive.manifolddynamics.com/manual/rigid-bodies/drives.md) and [Joint Sensor](https://docs.flarelive.manifolddynamics.com/reference/sensors-and-io/joint-sensor.md).

For a downloadable articulated example, run the [KOI tutorial](https://docs.flarelive.manifolddynamics.com/manual/tutorials/koi/index.md).

To prescribe a Camera or visual object's pose, attach [Transform Driver](https://docs.flarelive.manifolddynamics.com/reference/scene/transform-driver.md) to that object and send its position/orientation vector through the same `exchange_simulation_io` method.

## Advancement choices

| Request | Effect |
| --- | --- |
| `exchange_simulation_io(inputs=..., outputs=...)` | Apply/read I/O at the current simulation time. |
| `exchange_simulation_io(..., steps=10)` | Apply input, advance ten steps and read outputs. |
| `exchange_simulation_io(..., until_time=2.0)` | Apply input, advance to the requested simulated deadline and read outputs. |
| `advance_simulation(steps=10)` | Advance without exchanging sensor data. |

A deadline is reached at a simulation-step boundary. Use the returned `simulated_time` and `time_step` for your next control tick. Keep automatic advancement paused while a script owns the loop.

Initialization switches to a Simulation world. Find actuator and sensor objects again after initialization so their handles refer to that world.

For longer tasks, cancellation and error recovery, see [tracked Workspace operations](https://docs.flarelive.manifolddynamics.com/reference/api/workspace.md#tasks-and-failures). Use the same client connection throughout a control loop.
