> For AI agents: guidance on navigating Viam documentation is available at https://docs.viam.com/llms.txt.

# Phase 6: Deploy the policy

Deploy your trained ACT policy with the policy and controller services, then watch the xArm stack a block on its own.
> Source: https://docs.viam.com/tutorials/xarm-act-stacking/deploy-the-policy/


With a trained `xarm-stacking` model, you can put it in the loop. You add two services: one that runs the model, and one that reads the cameras, asks the model what to do, and moves the arm. That's the second milestone: you press start, and the xArm stacks the red block on the green one with no human input.

The controller must feed the policy exactly what it saw in training: the same cameras, the same joint order, the same units, and the same gripper scale. Most failures in this phase come from one of those not matching.

## Two services, one loop {#two-services-one-loop}

The `viam-labs:vla` module provides two services:

- `act-policy` (`viam-labs:vla:policy`) runs the model. It knows nothing about the robot. You send it images, joint state, and a task string, and it returns a chunk of actions.
- `act-controller` (`viam-labs:vla:controller`) runs the loop. It reads the cameras and joints, asks the policy for a chunk of actions, clamps them, and moves the arm.

Splitting them lets you test the policy with no robot attached, and develop the control loop with no GPU.

## Stop teleop first {#stop-teleop-first}

Only one thing can drive the xArm at a time, so turn teleop off before you start the controller.

1. Open the `teleop` service's DoCommand test panel and send `{"command": "stop"}`.
2. Open the **CONTROL** tab and set `follower-home` to position 2 ("go to") to home the xArm.
3. Move the leader arm out of the workspace. The policy only sees the follower, but you don't want to knock the leader while the xArm moves.

## Add the policy {#add-the-policy}

Your trained model is in the registry, so you can add it from the Blocks dialog. Adding it also adds the `viam-labs:vla` module and a policy service that points at the model. The full policy and controller configuration is in the companion repo's `config/policy.json`.

1. On the **CONFIGURE** tab, click **+** and select **Blocks**. Search for `xarm-stacking` and select your model.
2. Choose the version your training job produced in [Phase 5](/tutorials/xarm-act-stacking/train-a-policy/).
3. Name the policy service `act-policy` and click **Deploy**. The `viam-labs:vla` module installs automatically as a supporting module.

   ![The Add generic dialog, which says it runs inference using xarm-stacking, with the generic/policy model, the name act-policy, one supporting module installed automatically, and a Deploy button.](/tutorials/xarm-act-stacking/deploy-act-policy.png)

   The service's `model_path` comes preconfigured:

   ```json
   {
     "model_path": "${packages.ml_model.xarm-stacking}"
   }
   ```

   `viam-server` replaces `${packages.ml_model.xarm-stacking}` with the real path to the downloaded model before the module sees the config. `device` defaults to `auto`, which tries `cuda`, then `mps`, then `cpu`. Add it only to force one.

   The module's LeRobot version has to match the one that trained your model. `viam-labs:vla` 0.6.0 runs LeRobot 02655292c, the same as the training container from [Phase 5](/tutorials/xarm-act-stacking/train-a-policy/). If the module card shows a newer version, pin it to 0.6.0. The module needs `curl` and `git` on the machine. Without `curl`, the module's setup fails. Without `git`, setup succeeds, but `act-policy` reports `"state": "failed"` with an error naming `git` when it tries to install LeRobot.

4. Save. The model downloads and loads in the background. The first time the policy loads, it also installs PyTorch and LeRobot, which takes several minutes and needs network access. Later loads skip the install. In the `act-policy` DoCommand test panel, send this until `state` is `ready`:

   ```json
   { "command": "status" }
   ```

   ```json
   { "state": "ready", "error": "" }
   ```

   `state` is one of `idle`, `loading`, `ready`, or `failed`. It stays `loading` through the first install. If it's `failed`, `error` says why.

![The act-policy service on the CONFIGURE tab, marked READY, with the xarm-stacking package loaded and the model_path attribute set.](/tutorials/xarm-act-stacking/act-policy-ready.png)

## Ask what the policy needs {#ask-what-the-policy-needs}

The policy knows what it was trained on, so ask it:

```json
{ "command": "specs" }
```

The response is long, but these are the fields the controller cares about:

```json
{
  "policy_type": "act",
  "action_dim": 7,
  "state_dim": 7,
  "image_feature_keys": [
    "observation.images.scene_cam",
    "observation.images.wrist_cam"
  ],
  "n_action_steps": 20
}
```

Illustrative output; your values come from your checkpoint.

- `image_feature_keys` lists the cameras the policy expects: here, the scene camera and the wrist camera. The controller refuses to start unless you map every key.
- `state_dim` is the length of the state the policy reads. For this workshop it's 7: six joints plus the gripper.
- `action_dim` is the length of the action the policy emits, also 7.
- `n_action_steps` is the number of actions in each chunk, the 20 you set with `extra` in [Phase 5](/tutorials/xarm-act-stacking/train-a-policy/).

Each key is `observation.images.` plus the camera's component name, with hyphens and periods replaced by underscores. Match the names you see in `specs`.

## Add the controller {#add-the-controller}

Add the controller service from the same module:

1. On the **CONFIGURE** tab, click **+** and select **Blocks**. Search for `vla` and select the `generic / vla:controller` result.
2. Name it `act-controller` and click **Add**.
3. Paste these attributes, then save. The `cameras` keys must match `image_feature_keys` from `specs`.

```json
{
  "policy_service": "act-policy",
  "arm": "xarm",
  "task": "stack the red block on the green block",
  "cameras": {
    "observation.images.scene_cam": "scene-cam",
    "observation.images.wrist_cam": "wrist-cam"
  },
  "state_joint_indices": [0, 1, 2, 3, 4, 5],
  "gripper": {
    "type": "do_command",
    "name": "xarm-gripper",
    "read_key": "pos",
    "open_value": 850.0,
    "closed_value": 0.0
  },
  "arm_write": "stream",
  "fps": 10.0,
  "rpc_timeout_s": 12.0,
  "state_units": "degrees",
  "action_units": "degrees",
  "safety": {
    "max_vel_degs_per_sec": 60.0,
    "max_start_delta_degs": 15.0,
    "stop_on_error": true
  }
}
```

- `policy_service` and `arm` name the policy and the xArm.
- `cameras` maps each image feature key from `specs` to a camera. Every key must be covered.
- `state_joint_indices` maps the arm's joints, base to wrist, onto the state positions the checkpoint expects. These are indices, not names. The six xArm joints are `0` to `5`.
- `gripper` adds the seventh channel. `read_key`, `open_value`, and `closed_value` must match the `gripper_key`, `gripper_open`, and `gripper_closed` you passed in [Phase 5](/tutorials/xarm-act-stacking/train-a-policy/). The controller turns the reading into a value from `0.0` (open) to `1.0` (closed), the same scale training used.
- `arm_write: "stream"` sends the arm a smooth stream of interpolated points, 100 a second by default, instead of one setpoint per tick. With the default, `"setpoint"`, the xArm snaps to each new target 10 times a second and holds it, a visible buzz. The xArm's servo mode wants 100 Hz or more.
- `fps` is the loop rate. Keep it at `10.0` to match training.
- `rpc_timeout_s` bounds each arm and gripper call. The xArm gripper's `set` blocks until the jaw stops moving, up to 10 seconds. The controller sends gripper writes from a background task, so a slow write never holds up the arm, but a write that times out still stops the run. That's why this is raised to 12 seconds, above the jaw's settle time, from the 5-second default for a `do_command` gripper.
- `state_units` and `action_units` are the units the checkpoint was trained in. Viam arms always report and accept degrees, and this checkpoint trained on the xArm's joint values in degrees, so `"degrees"` is right. Both default to `"degrees"`; they're shown here to make the choice visible. Set them to `"radians"` only if your training data was in radians.
- `safety.max_vel_degs_per_sec` and `max_start_delta_degs` use conservative starting values from the [module README](https://github.com/viam-labs/vla-inference-service)'s example: 60 and 15. `max_vel_degs_per_sec` caps joint speed. The controller derives a per-tick limit from it: `60.0 / 10.0 = 6` degrees per tick. `max_start_delta_degs` makes `start` refuse if the policy's first action is more than 15 degrees from where the arm is now. `stop_on_error` halts the loop if a camera read or the policy call fails.

`task` is the instruction the policy receives. ACT ignores it, but the controller refuses to `start` without one, so set it to the task you recorded in [Phase 3](/tutorials/xarm-act-stacking/record-demonstrations/). A language-conditioned policy, such as SmolVLA, follows it. There you can also pass a different instruction for one run with `{"command": "start", "task": "..."}`.

`safety.joint_limits_degs` is optional. Without it, the controller relies on the xArm driver to enforce joint limits.

![The act-controller service on the CONFIGURE tab, marked READY, showing its policy_service, arm, task, cameras, state_joint_indices, and gripper attributes.](/tutorials/xarm-act-stacking/act-controller-ready.png)

## How the controller paces the chunks {#how-the-controller-paces-the-chunks}

The policy returns 20 actions at a time, 2 seconds of motion at 10 Hz. The controller places each action on a timeline: action 1 is due one tick after the camera frame it came from, action 2 two ticks after, and so on. Each tick, the arm moves to whichever action is due.

The controller asks for the next chunk before the current one runs out. It measures how long inference takes and fires the next request early enough that the new chunk lands while the arm is still moving, then blends it in over two ticks. When chunks land in time, actions whose moment has passed are skipped, never run late.

If inference takes too long, the next chunk can't land in time. When twice the inference time, plus four ticks, reaches the 2-second chunk, the controller switches to the `sequential` regime: the arm finishes each chunk, stops, waits for the next one, then moves again.

## Run it {#run-it}

First, learn the way out. This command halts the loop, and the arm stays where it is:

```json
{ "command": "stop" }
```

Send it to `act-controller` any time something looks wrong. The xArm also has a physical emergency stop. Keep it within reach for every run, and use it before the arm hits anything.

Now run it.

1. Place the red and green blocks at one of the start positions you used when recording.
2. Make sure the arm is at home and nothing is in its path.
3. In the `act-controller` DoCommand test panel, send:

   ```json
   { "command": "start" }
   ```

   `start` returns immediately. It doesn't wait for the arm to move, so watch the arm and poll `status` to see what happened. Before the arm moves, the controller runs one throwaway inference to warm up the cameras and the policy.

4. Watch the arm. Keep a hand near the emergency stop.
5. When the stack is complete or the arm goes idle, send `{"command": "stop"}` to `act-controller`. The loop keeps running until it gets `stop`. Then home the arm: on the `follower-home` switch, set the position to 2 ("go to").

## Read `status` {#read-status}

While the loop runs, send `{"command": "status"}` to `act-controller` and watch these fields:

| Field           | What it tells you                                                                                                              |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `state`         | `idle`, `waiting_for_policy`, `running`, `stopped`, or `error`.                                                                |
| `regime`        | `overlap` or `sequential`. You want `overlap`. `sequential` means inference is too slow, and the arm pauses between chunks.    |
| `measured_fps`  | The actual loop rate. It should track `fps`. Well below it means each tick takes too long, for example a slow gripper write.   |
| `avg_latency_s` | How long one policy call takes, from camera frame to landed chunk.                                                             |
| `clamp_counts`  | How many times the safety limits trimmed an action, split into `delta`, `limit`, and `gripper`. Non-zero counts mean trimming. |
| `starved_ticks` | Ticks where the arm held still waiting for a chunk. Under `overlap` it should stay near zero.                                  |
| `last_error`    | The last failed tick's error. It stays set even if the loop kept going.                                                        |

Check `clamp_counts` first. It should stay near zero. A count that keeps climbing almost always means wrong units or wrong joint order, not a safety margin doing its job. One exception: if your demonstrations moved faster than `max_vel_degs_per_sec`, `clamp_counts.delta` climbs even with correct units. Work through it in this order:

1. Rule out units and joint order first. Wrong units clamp on every tick from the first one. A speed limit clamps only during fast moves.
2. If it's really speed, raise `max_vel_degs_per_sec` in small steps, for example 10 degrees per second at a time. Keep it well below the xArm's rated joint speed, because this clamp is the only velocity bound on the arm. Keep the physical emergency stop within reach.
3. If the count still climbs only during the fastest moves, you can leave it. The clamp only makes the arm move a little slower than your demonstrations did.

> **Checkpoint:**
> 
> From a fresh block layout, send `{"command": "start"}` to `act-controller`. The xArm stacks the red block on the green one with no human input.





  
  
    
    
    
  


  
  
    
    
    
  
<video class="video-shortcode lozad " preload="none" controls alt="The xArm6, running the trained ACT policy with no human input, picks up the red block, carries it over the green block, lowers it, and lets go, leaving the red block stacked on the green one." style="width: 480px; " data-poster="/tutorials/xarm-act-stacking/policy-rollout-poster.jpg"><source data-src="/tutorials/xarm-act-stacking/policy-rollout.webm" type="video/webm"><source data-src="/tutorials/xarm-act-stacking/policy-rollout.mp4" type="video/mp4">There should have been a video here but your browser does not seem
  to support it.
</video>
<noscript>
  <video class="video-shortcode " preload="none" controls alt="The xArm6, running the trained ACT policy with no human input, picks up the red block, carries it over the green block, lowers it, and lets go, leaving the red block stacked on the green one." style="width: 480px; " poster="/tutorials/xarm-act-stacking/policy-rollout-poster.jpg"><source data-src="/tutorials/xarm-act-stacking/policy-rollout.webm" type="video/webm"><source data-src="/tutorials/xarm-act-stacking/policy-rollout.mp4" type="video/mp4"></video>
</noscript>

## Troubleshooting {#troubleshooting}

| Symptom                                            | Likely cause                                                                                                                                                               | Fix                                                                                                                                                                                                    |
| -------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| The arm moves the wrong way or by the wrong amount | Wrong units. A mismatch usually shows up as a climbing `clamp_counts.delta`.                                                                                               | Set `state_units` and `action_units` to match the training data, then check `state_joint_indices`.                                                                                                     |
| The gripper never closes                           | `open_value` and `closed_value` are reversed, or don't match training. Reversed values invert the gripper but stay within 0 to 1, so `clamp_counts.gripper` won't show it. | Set them to the `gripper_open` and `gripper_closed` values from [Phase 5](/tutorials/xarm-act-stacking/train-a-policy/). A reading more than 0.25 outside 0 to 1 is refused and shows in `last_error`. |
| The arm moves, stops, and moves again              | `regime` is `sequential`, so inference is too slow for the 2-second chunk.                                                                                                 | Check `avg_latency_s`, and confirm `device` isn't `cpu` on a machine with a GPU.                                                                                                                       |
| The arm freezes for seconds at a gripper change    | You're running a module version before 0.6.0, where the controller waits for the xArm gripper's `set`, which blocks until the jaw settles.                                 | Pin `viam-labs:vla` to 0.6.0 or later, which writes the gripper from a background task.                                                                                                                |
| The run stops at a gripper change                  | A gripper write took longer than `rpc_timeout_s`. `last_error` names the timeout.                                                                                          | Raise `rpc_timeout_s`.                                                                                                                                                                                 |
| It works only from some start positions            | The demonstrations didn't vary enough.                                                                                                                                     | Record more varied starts in [Phase 7](/tutorials/xarm-act-stacking/evaluate-and-iterate/).                                                                                                            |

<nav class="workshop-nav" aria-label="Workshop phase navigation"><p class="workshop-progress">Phase 6 of 7</p><div class="workshop-nav-links"><a class="workshop-nav-prev" href="/tutorials/xarm-act-stacking/train-a-policy/">&larr; Previous</a><a class="workshop-nav-next" href="/tutorials/xarm-act-stacking/evaluate-and-iterate/">Next &rarr;</a></div>
</nav>


