Scenario API
skelarm.scenario
Load a full control scenario (robot + task + simulator + controller) from one TOML file.
A combined config mirrors the per-section schema used by :meth:Skeleton.from_toml:
[skeleton] and [initial] describe the robot and its start state, [task]
the goal (for a reach, a task-space target with a movement duration), [simulator]
the integration settings, and [controller] which controller drives the motion.
:func:load_scenario reads them all.
Example::
[task]
type = "reaching" # the task kind (required)
target = [0.55, 1.21] # endpoint goal (x, y) in meters (required for reaching)
duration = 2.0 # simulated time (s)
[simulator]
dt = 0.002 # simulation and control step (s)
[controller]
type = "computed_torque"
kp = 200.0
kd = 30.0
Scenario
dataclass
A loaded control scenario: the robot, the task, the simulator, and the controller.
source_config keeps the original combined config (the parsed [skeleton] /
[initial] / [task] / [simulator] / [controller] tables) when the
scenario was loaded from a file, so a run can embed it for an exact, editable re-run;
it is None for scenarios built programmatically.
Source code in src/skelarm/scenario.py
Simulator
dataclass
Numerical-integration parameters for the dynamics, from the [simulator] table.
These are run/execution concerns, kept separate from the task (the desired motion).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
float
|
Control / integration step (seconds). Also the MPC prediction step. |
0.002
|
enforce_limits
|
bool
|
Apply the joint limits as hard stops in the dynamics (default). When |
True
|
Source code in src/skelarm/scenario.py
from_dict(section)
classmethod
Build a Simulator from a [simulator] mapping (defaults when keys are absent).
Source code in src/skelarm/scenario.py
Task
dataclass
A task and the run conditions for it.
The required field is type — the kind of task, which determines what else is
needed. reaching requires a target. The other built-in types
(multi_target_reaching, periodic_curve, trajectory_tracking, and
joint_trajectory_tracking) and custom types registered with
:func:register_task_type carry their own data on :attr:params and may omit the
target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
str
|
The task kind (the |
required |
target
|
NDArray[float64] | None
|
The endpoint goal |
None
|
label
|
str | None
|
Optional name for the target (for |
None
|
color
|
str
|
Marker color for the target (any Qt/SVG color name or |
_DEFAULT_TARGET_COLOR
|
tolerance
|
float | None
|
Optional success distance in meters: the reach succeeds when the tip is
within this distance of the target, and the target marker's ring is drawn
at this radius. |
None
|
duration
|
float
|
Total simulated time / planned-motion horizon (seconds). |
2.0
|
schedule
|
str
|
Time-scaling schedule for planned-trajectory controllers. |
'minimum_jerk'
|
params
|
dict[str, Any]
|
Any extra |
dict()
|
Source code in src/skelarm/scenario.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
from_dict(section)
classmethod
Build a Task from a [task] mapping.
type is required; it selects the task kind. The reaching type also
requires a target — either a [x, y] array (position only) or a table
with a pos and optional label / color / tolerance. Other task
types may omit the target. Any keys beyond the recognized ones are kept on
:attr:params for custom task types.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/skelarm/scenario.py
from_toml(file_path)
classmethod
Build a Task from the [task] table of a TOML file.
Source code in src/skelarm/scenario.py
require_target()
Return the task target, raising if this task type has none.
Use this from a controller that needs a point goal (e.g. the reaching controllers), so a task wired without a target fails with a clear message.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the task has no target. |
Source code in src/skelarm/scenario.py
to_dict()
Return the [task] mapping that :meth:from_dict builds this task from.
The inverse of :meth:from_dict: the target becomes a table with its
position, color, and (when set) label and tolerance; the duration, the
schedule, and the :attr:params follow. Values are plain lists, numbers,
and strings, so the mapping can be written to TOML or stored in a log.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The |
Source code in src/skelarm/scenario.py
active_target_index(task)
apply_active_target(task)
Set a multi-target task's first-class target (and marker attrs) to its active candidate.
Lets the active goal flow through the ordinary reaching pipeline (controller build,
marker drawing); the GUI switches it live by reassigning task.target / the
controller's target.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the active index is out of range. |
Source code in src/skelarm/scenario.py
build_controller(source, *, skeleton, task, dt=0.002)
Construct the controller described by a [controller] config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | Path | Mapping[str, Any]
|
A TOML file path (its |
required |
skeleton
|
Skeleton
|
The robot, posed at its start state; used to build joint references. |
required |
task
|
Task
|
The reaching task supplying the target and run conditions. |
required |
dt
|
float
|
The control / integration step (from |
0.002
|
Returns:
| Type | Description |
|---|---|
Controller
|
The configured controller. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the config has no |
Source code in src/skelarm/scenario.py
controller_types()
export_scenario_toml(log, path)
Write the log's embedded scenario config to an editable TOML file.
The output is a standard combined config ([skeleton] / [initial] /
[task] / [simulator] / [controller]) that :func:load_scenario
reads back. It is the resolved scenario config the run used (section
overrides merged, reference samples inlined — not necessarily the input TOML
byte-for-byte): re-running it unedited reproduces a headless, config-driven
run exactly for the deterministic controllers, but call-time overrides (a
duration= or
enforce_limits= argument) live only in the run metadata and are not
exported, and an interactive run's mouse-applied forces are not part of the
config. Individual values can be edited for comparison studies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log
|
StateLog
|
A log carrying an embedded source config (written by :func: |
required |
path
|
str | Path
|
Destination |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the log carries no embedded scenario config. |
Source code in src/skelarm/scenario.py
load_reference_log(path)
Load a .sklog.npz reference trajectory (e.g. from tools/trajectory_recorder.py).
Source code in src/skelarm/scenario.py
load_scenario(file_path)
Load a robot, task, and controller from one combined TOML file.
multi_target_specs(task)
Parse a multi-target task's targets list into (pos, label, color, tolerance) tuples.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the task carries no |
Source code in src/skelarm/scenario.py
reference_builders()
register_controller(name, builder)
Register a controller builder so [controller].type = name builds it.
The builder receives the [controller] table (without type) as a mapping,
the posed Skeleton, and the Task, and returns a :class:~skelarm.Controller.
Re-registering an existing name replaces it. Custom controllers are also usable
directly (construct the instance and pass it to :func:run_scenario /
:func:~skelarm.simulate_controlled); registration is only needed for the
config-driven path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The |
required |
builder
|
ControllerBuilder
|
A callable |
required |
Source code in src/skelarm/scenario.py
register_reference_builder(task_type, builder)
Register a joint-reference builder for a [task].type.
A tracking controller (computed torque, joint PD, inverse-dynamics PD, MPC) builds its reference by calling the builder registered for the scenario's task type. Use this to add a new trajectory-style task that drives the existing controllers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_type
|
str
|
The |
required |
builder
|
ReferenceBuilder
|
A callable |
required |
Source code in src/skelarm/scenario.py
register_task_type(name)
Allow name as a [task].type.
Task types are a label that a controller interprets; a custom task carries its
parameters on :attr:Task.params (any extra [task] keys). Register the type
so :meth:Task.from_dict accepts it, then read task.params from a matching
controller built via :func:register_controller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The task-type string to allow (idempotent). |
required |
Source code in src/skelarm/scenario.py
rerun_log(log)
Reconstruct and re-simulate a scenario from its recorded config and run parameters.
Rebuilds the scenario by reparsing the embedded source config (identical input
gives identical state) and re-runs with the recorded run parameters. For a
headless :func:run_scenario log, deterministic controllers reproduce the
recorded channels exactly (MPC, which calls :func:scipy.optimize.minimize,
reproduces within a small numerical tolerance on the same platform). For an
interactive simulator log the re-run is the unperturbed scenario:
mouse-applied forces (the ext_force channel) and live target switches
(the active_target channel) are recorded but not replayed, so the re-run
reaches the configured active target.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the log carries no reproduction metadata. |
Source code in src/skelarm/scenario.py
run_scenario(scenario, *, duration=None, dt=None, grav_vec=None, enforce_limits=None)
Simulate a scenario and embed its full config for later reproduction.
Runs :func:~skelarm.control.simulate_controlled and, when the scenario was
loaded from a config, stores the task, controller, initial state, and run
parameters in the log's [extra] table so :func:rerun_log can reconstruct
and re-simulate it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scenario
|
Scenario
|
The robot, task, and controller to run. |
required |
duration
|
float | None
|
Simulated duration (seconds); defaults to |
None
|
dt
|
float | None
|
Control / integration step; defaults to |
None
|
grav_vec
|
NDArray[float64] | None
|
Gravity vector; defaults to zero (planar motion). |
None
|
enforce_limits
|
bool | None
|
Apply the joint limits as hard stops in the dynamics. |
None
|
Returns:
| Type | Description |
|---|---|
StateLog
|
The recorded run, with reproduction metadata when available. |
Source code in src/skelarm/scenario.py
scenario_from_config(data)
Build a Scenario from a parsed combined config and keep the config for re-runs.
Reuses the real parsers (:meth:Skeleton.from_config, :meth:Task.from_dict,
:func:build_controller), so rebuilding from an identical mapping reproduces the
robot, initial pose, task, and controller exactly.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the config has no |
Source code in src/skelarm/scenario.py
scenario_from_log(log)
Reconstruct the scenario and run parameters embedded in a log.
Works with logs written by :func:run_scenario and by the interactive
scenario simulators; run keys a log does not record (e.g. an interactive
run's duration) fall back to the scenario's own values.
Returns:
| Type | Description |
|---|---|
tuple[Scenario, Mapping[str, Any]]
|
The rebuilt scenario (skeleton posed at the recorded initial state) and the
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the log carries no reproduction metadata (it was recorded without an embedded scenario config). |
Source code in src/skelarm/scenario.py
scenario_run_metadata(scenario, *, dt, enforce_limits, duration=None, grav_vec=None)
Assemble the [extra] payload that lets a run be reconstructed and re-run.
Embeds the resolved scenario config (the editable [skeleton] / [initial] /
[task] / [simulator] / [controller] tables as the run used them —
section overrides merged and file-based reference samples inlined), the
actual run parameters (including the resolved enforce_limits, so a call-time
override is captured), and the package versions. Returns None when the
scenario carries no source_config (e.g. a programmatically built controller).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scenario
|
Scenario
|
The scenario whose source config to embed. |
required |
dt
|
float
|
The control / integration step actually used. |
required |
enforce_limits
|
bool
|
The resolved joint-limit hard-stop setting actually used. |
required |
duration
|
float | None
|
Simulated duration (seconds). |
None
|
grav_vec
|
NDArray[float64] | None
|
Gravity vector actually used; |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
The |