# Command Routines

> Learn how Wandelbots NOVA OS plans a command routine into one trajectory and executes it on a controller as a run, with its I/O commands at fixed places on the path.

A command routine is a sequence of motions and I/O commands for one motion group: move to a pose, set an
output, and wait for an input. Command routines can be stored in
[datasets](/nova/26.9/nova-api/core-concepts/datasets). NOVA OS plans a command routine into one joint trajectory and
executes exactly that trajectory on a controller as a run.

> **Experimental:** Command routines are experimental. The routes are served under `/api/v2/experimental/`, and the API and its
> behavior can change in future releases.

## From command routine to run

### Step 1: Resolve the command routine

Planning needs every pose in `world`. Resolve a command routine of a dataset with
[resolveDatasetCommandRoutine](https://portal.wandelbots.io/docs/api/v2/ui/#/operations/resolveDatasetCommandRoutine)
(`GET .../datasets/{dataset}/command-routines/{command_routine}/resolve`). It replaces every pose reference and
every pose in a dataset frame or controller coordinate system with an inline pose in `world`.

### Step 2: Plan the command routine

Send the resolved command routine and the controller to
[planCommandRoutine](https://portal.wandelbots.io/docs/api/v2/ui/#/operations/planCommandRoutine)
(`POST .../cells/{cell}/command-routines/plan`):

```jsonc
{
  "command_routine": { ... },   // the resolved command routine
  "controller": "kuka-kr16-r2010"
}
```

The plan contains the `joint_trajectory` through all motions, starting at the command routine's `start_joint_position`,
and every command of the command routine with the `location` where it happens on that trajectory. Draw the plan before
you run it: nothing is planned again when it runs. If a motion cannot be planned, the `422` response names the
command and, if anything was planned before it, the trajectory up to there.

### Step 3: Start a run

Send the plan as you received it to
[startCommandRoutineRun](https://portal.wandelbots.io/docs/api/v2/ui/#/operations/startCommandRoutineRun)
(`POST .../cells/{cell}/controllers/{controller}/command-routines/runs`). The robot must stand at the start of the
plan's trajectory. The response is the new run, and its `Location` header is the address of the run.

Without parameters, the call returns as soon as the run is accepted (`PREPARING`). With `?timeout=<ms>` it
returns once the run has ended. If the timeout expires first, or if your client disconnects, the robot is
stopped and the run fails.

### Step 4: Follow or stop the run

Read the run with
[getCommandRoutineRun](https://portal.wandelbots.io/docs/api/v2/ui/#/operations/getCommandRoutineRun), or
subscribe to every change of it on [NATS](/nova/26.9/communication-protocols/nats):

```
{instance}.v2.cells.{cell}.controllers.{controller}.command-routines.runs.{run}
```

Stop it with
[stopCommandRoutineRun](https://portal.wandelbots.io/docs/api/v2/ui/#/operations/stopCommandRoutineRun). List
the runs of a controller, or of all controllers of the cell, with `GET .../command-routines/runs`.

## Commands

| Command | What happens in a run |
| - | - |
| `motion_command` | The robot moves along the planned trajectory to the target. |
| `set_io` | The output is set. Between two motions, the controller sets it while the robot passes, without stopping. |
| `wait_for_io` | The robot stands until the condition is `true`. If `timeout_ms` expires first, the run fails. |
| `wait_for_time` | The robot stands for the duration. |
| `pause_on_io` | From its location to the end of the trajectory, the robot pauses on its path while the condition is `true` and continues when it becomes `false`. |
| `marker` | Has no effect on the motion. |

A command routine with `motion_group_setup: null` is I/O-only: it runs its commands in order without moving a robot.
Its `CONTROLLER` inputs and outputs are those of the controller the run is started on.

### Where commands happen

Every command has a `location` on the planned trajectory. The `n`-th motion command moves the robot from
location `n` to `n + 1`. Any other command happens at its place in the sequence, or where its `at` trigger
places it. This could be halfway along the motion before it (`path_fraction`), or a time or distance along that motion.
The robot stops only where it has to wait. Outputs between two motions are set on the move.

## Runs

A run is one execution of a plan on a controller. Its `state` follows the states of a program run:

| State | Meaning |
| - | - |
| `PREPARING` | The start is checked, or the run was accepted. |
| `RUNNING` | The run executes its commands, waits included. `current_command_index` names the current command, `current_location` where the robot is. |
| `COMPLETED` | The run executed to the end. |
| `FAILED` | The run ended with an error. The `error` names its `cause`, the command that failed and where the robot stopped. |
| `STOPPED` | The run was stopped. |

Runs execute side by side:

- Each motion group executes one run at a time, and is not taken over while a program, a trajectory execution,
  or jogging moves it.
- An I/O-only run claims no motion group, so it runs beside the robot, e.g., a PLC handshake.
- Two runs never write the same output: a start is refused while another active run writes one of its outputs.
- A run with motions is refused during an emergency stop, and an emergency stop during the run fails it.
  I/O-only runs, e.g., one that sets a stack light, also run during an emergency stop.

A refused start moves nothing and writes nothing. Runs are kept in memory, so they are gone after a restart of
the service.
