# MOTION

Robot or cobot station whose service is a cycle of timed moves, picks, places and tool changes, so cycle time comes from motion data, not a distribution.

A station whose service is a cycle of motions -- moves timed from a motion profile (trapezoidal, payload-derated, slowed for human-robot collaboration) plus picks, places, tool changes, dwells and process steps. Seizing, setups, failures, preemption, workers and blocking are PROCESS's.

Family: work. Python builder: `m.motion(...)`.

## Typical uses

Use MOTION for a robot or cobot station whose cycle time should come from the motions it makes: moves, picks, places, tool changes and dwells. It is a [PROCESS](process.md) whose service time is the sum of its segments. If a single distribution describes the service well enough, PROCESS is simpler.

## Behaviour and rules

The `segments` run in order. A `move` has a distance in metres and an optional payload in kilograms. The other kinds (`pick`, `place`, `tool_change`, `dwell`, `process`) have a `time`, a number or an expression, in the block's `time_unit`, which is seconds by default. Moves are always computed in seconds.

A move is timed from the `profile`, a record in `motion_profiles` with speed, acceleration, payload derating and a collaborative speed. A move is timed when it starts, because the separation mode looks at where the workers are at that moment. A move in progress is never re-timed.

With `hrc` set to `collaborative`, every move runs at the collaborative speed. With `separation`, a move runs at that speed only when a worker from a worker fleet is within the zone's radius. Seizing, setups, failures, preemption, workers and blocking work as in PROCESS.

The `motion_summary` frame breaks the cycle time down by segment kind and reports the share of moves run in collaboration.

## Ports

- Inputs: any number of incoming links.
- Outputs: one outgoing link. With `balk_if` or `renege_after` set, a second link (index 1) takes the entities that leave without service.

## Parameters

| Parameter | Type | Default | Required | Quantity | Unit key | Description |
|---|---|---|---|---|---|---|
| `action` | str | `'Seize Delay Release'` |  |  |  | Which of seize/delay/release run: "Seize Delay Release", "Seize Delay", "Delay Release" or "Delay". |
| `profile` | str | `''` |  |  |  | Name of a record in "motion_profiles": speed, acceleration, payload derating and collaborative speed of whoever does the motions. |
| `robot` | str | `''` |  |  |  | Name of a record in "robots": the robot whose ptp and lin moves are timed from its axis speeds and accelerations. |
| `targets` | dict | `{}` |  |  |  | Named targets of the ptp and lin moves, in cell coordinates (the floor plan's x and y in metres, z up): {"joints": [...]}, {"xyz": [x, y, z], "yaw_deg"?, "rpy_deg"?} with the tool pointing down unless rpy_deg says otherwise, or {"on": <block>, "at"?: centre|start|end, "offset"?: [dx, dy, dz], "yaw_deg"?} in that block's own frame: dx along its facing, dy toward the floor plan's +y side at rotation 0 (as x and y), dz up; or {"belt": <CONVEYOR>, "divert": <index>, "z"?: <metres above the floor>, "yaw_deg"?}, a part met on the moving belt, the target of the cycle's first move. |
| `hrc` | str | `'none'` |  |  |  | Human-robot collaboration: "none", "collaborative" (every move at the profile's collaborative_speed) or "separation" (a move starts at collaborative_speed when a worker-fleet unit is within the zone's radius of its node, by path or straight distance). |
| `zone` | dict | `{}` |  |  |  | With hrc "separation": {"name": <a "zones" record>} -- the record's node and radius win -- or, with no name, {"node": <network node>, "radius_m": <metres>, "measure"?: "path"|"straight"}. |
| `segments` | list | `[]` |  | time | `time_unit` | The cycle, in order: {"kind": "move", "distance_m", "payload_kg"?}, {"kind": pick|place|tool_change|dwell|process, "time": <number or expression>}, or a robot move {"kind": ptp|lin, "to": <target>, "time"?: <number, at least what the move needs>, "payload_kg"?}. The service time is their sum. |
| `time_unit` | str | `'seconds'` |  |  |  | Unit of every segment `time`: "base" or seconds/minutes/hours/days. Moves are always computed in seconds. |
| `priority` | int | `0` |  |  |  | Lower draws first when the resource's queue is contested. One number for the block (an entity attribute is not read): to rank entity types, route each to its own block on the shared resource. |
| `quantity` | int | `1` |  | count |  | Number of resource units requested/released. |
| `resource_name` | str | `''` |  |  |  | Name of the resource or resource set; required unless action is "Delay" or a worker_fleet does the work alone. |
| `setup` | str | `''` |  | time | `setup_unit` | Sequence-dependent setup time: an expression charged after the seize when this block's resource changes over, e.g. TABLE("Setups", previous_key, key). `previous_key` and `key` are in scope here only. Empty = no setup. |
| `setup_unit` | str | `'base'` |  |  |  | Unit of `setup`: "base" (the model's) or one of its quantity's units. |
| `setup_key` | str | `''` |  |  |  | Expression identifying what a job is set up for; empty uses the entity type. A setup is charged only when it differs from the previous job's key at this block. |
| `initial_key` | str | `''` |  |  |  | What the resource is already set up for at time 0; empty means the first job is charged no setup. |
| `setup_cost` | str | `''` |  | money |  | Money charged ONCE PER CHANGEOVER, not per job -- an expression like TABLE("Changeover cost", previous_key, key), with `previous_key` and `key` in scope exactly as in "setup". It becomes an event cost item named after the block, joins Total Cost and resets at warm-up. Empty = free changeovers. |
| `setup_cost_category` | str | `''` |  |  |  | Cost category the changeover cost is grouped under in the cost_summary frame (default "Changeover"). |
| `balk_if` | str | `''` |  |  |  | Condition evaluated on arrival; when true the entity does not join the queue and leaves by the second outgoing link, e.g. "queue_length >= 5". `queue_length` is the queue it would have joined. Empty = never balk. |
| `renege_after` | str | `''` |  | time | `renege_time_unit` | How long the entity waits before giving up, e.g. "EXPO(15)" or "entity.patience"; it then leaves by the second outgoing link. A grant in the same instant wins. Empty = wait for ever. |
| `renege_time_unit` | str | `'base'` |  |  |  | Unit `renege_after` is expressed in: "base" (the model's own unit) or one of seconds/minutes/hours/days. |
| `preempt` | bool | `False` |  |  |  | When no unit is free, interrupt the lowest-priority job in service on this resource (a strictly higher priority wins). Only with action "Seize Delay Release", and not on a resource set. |
| `queue_capacity` | int | `0` |  | count |  | Places in this station's own input queue. 0 (the default) is an unlimited queue; with a positive value a part takes a place on arrival and frees it when service starts, so an upstream machine with nowhere to put its finished part stays blocked. `balk_if` sees the places taken as `queue_length`, which is how a LOSS system is written instead of a blocking one. |
| `hold_until_space` | bool | `False` |  |  |  | Blocking BEFORE service: after seizing, do not start the job until the downstream space has a free place, and reserve it for this part. The station waits idle (reported as Blocked %). Needs a downstream BUFFER with capacity > 0 or a SEIZE/PROCESS with queue_capacity > 0. Off (the default) is blocking AFTER service. |
| `space_of` | str | `''` |  |  |  | Which block's space `hold_until_space` reserves in; empty = the target of this block's first outgoing link. The part must reach that block. |
| `worker_fleet` | str | `''` |  |  |  | A fleet with kind "worker". After this block's own resource (if any) is seized, a worker is requested, walks EMPTY on the network to `node`, and is held for the service. Empty = no worker. |
| `worker_skill` | str | `''` |  |  |  | Only a worker whose skills contain this name may serve; empty = any worker of the fleet. |
| `node` | str | `''` |  |  |  | Network node this block sits on; required with worker_fleet (where the worker walks to). |
| `worker_when_blocked` | str | `'leaves'` |  |  |  | What the worker does while this station holds a finished part that nothing downstream has room for. "leaves" (the default): the worker is free when the work ends. "stays": the worker waits with the part and is free when it is accepted downstream. The same either way where nothing downstream is finite. |
| `on_preempt` | str | `'resume'` |  |  |  | What THIS block's job does when something interrupts it and it gets the resource back: "resume" (finish the remaining time) or "restart" (the whole service again, same sample). Set on the block being interrupted, not on the one doing the interrupting. |
| `resample` | bool | `False` |  |  |  | With on_preempt "restart" only: draw a FRESH service time from this block's own random stream on each restart, instead of repeating the same sample. Off (the default) repeats it. |

## Statistics

Every run fills `system` and `entity_summary` (see the [KPI reference](../kpis.md)). The example below also fills:

| Frame | Columns |
|---|---|
| [`resource_summary`](../kpis.md#resource_summary) | `Run`, `Resource`, `Total Seizes`, `Usage % (snapshot)`, `Idle %`, `Scheduled Capacity Avg`, `Down %`, `Busy Cost`, `Idle Cost`, `Usage Cost`, `Total Cost`, `Queue Length Avg`, `Queue Length Max`, `Queue Length at End`, `Queue Length Mode`, `Queue Length Mode %`, `Queue Len P95`, `Queue Len P99`, `Queue Len Std`, `Time in Queue Avg`, `Time in Queue Avg (incl. waiting)`, `Time in Queue Max`, `Time in Queue Mode`, `Time in Queue Mode %`, `Wait P95`, `Wait P99`, `Wait Std`, `Bottleneck Score`, `Bottleneck %` |
| [`motion_summary`](../kpis.md#motion_summary) | `Run`, `Motion`, `Block`, `Segment Kind`, `Segments`, `Mean Time`, `Total Time`, `Cycle Share %`, `Collaborative %` |
| `material_flow_summary` | `Run`, `From`, `To`, `Entities` |

## Problem codes

A check of the model reports these about this block:

| Code | Meaning |
|---|---|
| `E_INITIAL_CYCLE` | A MOTION job mid-cycle in initial_state does not fit the model: a cycle not at a MOTION job in service, a segment it does not have, a time that is no duration of it, or an end beyond the segment in progress. |
| `E_MOTION_PROFILE_UNKNOWN` | A MOTION block names a motion profile the model does not declare. |
| `E_MOTION_SEGMENT` | A MOTION segment has an unknown kind or field, no distance/time, or a negative value; or the cycle has no segments. |
| `E_MOTION_ZONE` | A MOTION block's hrc mode is unknown, or a separation zone names no network node, has radius_m <= 0 or an unknown measure. |
| `E_MOTION_ZONE_UNKNOWN` | A MOTION block's zone names a zone the model does not declare under "zones". |
| `E_ROBOT_UNKNOWN` | A MOTION block, or a BATCH or SEPARATE that works by a pallet pattern, names a robot the model does not declare, or has robot moves and names none. |

## Example

The snippet seeds itself; its output is from a run made when this page was generated.

```python
from nagasim import build as nb

m = nb.Model(time_limit=3600, time_unit="seconds")
m.add_resource(name="Cell")
m.add_motion_profile(name="Cobot", kind="robot", speed=1.5, accel=4.0, payload_max=10.0, payload_derate=0.7)
arrive = m.create(distribution=nb.Constant(value=60))
cycle = m.motion(profile="Cobot", resource_name="Cell", segments=[
    {"kind": "move", "distance_m": 1.0},
    {"kind": "pick", "time": "1.0"},
    {"kind": "move", "distance_m": 1.0, "payload_kg": 2.0},
    {"kind": "place", "time": "1.0"},
])
arrive >> cycle >> m.dispose()

results = m.run(seed=1)
print(results["motion_summary"].round(2).to_string(index=False))
```

```text
 Run         Motion    Block Segment Kind  Segments  Mean Time  Total Time  Cycle Share %  Collaborative %
   1  motion_1/move motion_1         move       120       1.05       126.2          51.26              0.0
   1  motion_1/pick motion_1         pick        60       1.00        60.0          24.37              NaN
   1 motion_1/place motion_1        place        60       1.00        60.0          24.37              NaN
   1 motion_1/cycle motion_1        cycle        60       4.10       246.2         100.00              NaN
```

## Used in

Bundled models: [Robot cell against manual assembly](../../gallery/manufacturing.md), [Automation floor (robot cells, AMR, palletiser)](../../gallery/manufacturing.md), [Palletizing line](../../gallery/manufacturing.md).

Guides: [Motion cycles](../../modelling-guide/motion.md), [Mobile cobots: machine tending](../../modelling-guide/tending.md), [Digital twin](../../digital-twin.md), [Finite buffers and blocking after service](../../modelling-guide/finite-buffers.md).

## See also

- [All blocks](index.md), [KPI reference](../kpis.md), [Model file keys](../schema.md), [Model file format](../../model-file-format.md)
- Same family (work): [CHARGE](charge.md), [DELAY](delay.md), [PROCESS](process.md)
