cli
cli
Interactive commissioning and motion tooling for gantry position recording.
MotionSession is available without any extras — it has no
dependency on prompt_toolkit or rich.
The interactive REPL (applied_motion.cli.cli) and the
cli optional-dependency extra require the cli extra::
1 | |
Typical usage::
1 2 3 4 5 6 | |
To launch the interactive REPL::
1 | |
MotionSession
Records gantry positions interactively or programmatically.
Provides step-jog and position capture on top of a connected
Gantry. Captured positions are kept
in positions and can be persisted to and from JSON via
save / load.
This class has no knowledge of the gantry backend (Modbus vs FPosBAPI)
and no dependency on prompt_toolkit or rich. Backend-specific
post-capture actions (e.g. sending TEACH_POS to the PLC) are
performed by the caller through the on_capture hook.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gantry
|
Gantry
|
Connected and homed |
required |
on_capture
|
OnCaptureHook | None
|
Optional hook called immediately after each successful
|
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
positions |
dict[str, dict[str, float]]
|
Example::
1 2 3 4 5 6 7 8 9 | |
Source code in src/applied_motion/cli/session.py
jog
jog(
axis_name: str,
direction: str,
step_mm: float,
velocity: float = 10.0,
timeout: int = 15,
) -> dict[str, float]
Step-move one axis by step_mm in direction.
Reads the current axis position, computes an absolute target, and
dispatches a single-axis move via
Gantry.move_to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis_name
|
str
|
Name of the axis to jog (must be a key in
|
required |
direction
|
str
|
|
required |
step_mm
|
float
|
Distance to step in millimetres. Must be positive. |
required |
velocity
|
float
|
Jog speed in mm/s. Defaults to |
10.0
|
timeout
|
int
|
Maximum seconds to wait for the move to complete.
When exceeded the axis backend issues a stop command and
the move is abandoned. Defaults to |
15
|
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Full gantry location dict after the move completes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If direction is not |
KeyError
|
If axis_name is not registered with the gantry. |
Source code in src/applied_motion/cli/session.py
98 99 100 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 | |
capture
Record the current gantry position under label.
Calls Gantry.get_location, stores
the result in positions, then invokes on_capture if one
was provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
Unique name for this position (e.g. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
The recorded position dict |
Source code in src/applied_motion/cli/session.py
save
Write all captured positions to a JSON file.
The output format is a plain JSON object mapping label →
{axis_name: position_mm}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Destination file path. Parent directories must exist. |
required |
Source code in src/applied_motion/cli/session.py
load
Merge positions from a JSON file into positions.
Existing labels are overwritten by entries from path; labels absent in path are preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Source JSON file previously written by |
required |
Raises:
| Type | Description |
|---|---|
OSError
|
If path cannot be opened. |
ValueError
|
If the file contains invalid JSON. |
Source code in src/applied_motion/cli/session.py
Command
dataclass
Command(
name: str,
handler: CommandHandler,
usage: str = "",
help: str = "",
completions: Callable[[], list[str]] | None = None,
)
A single named leaf command in a command group.
CommandGroup
A composable namespace of commands and nested child groups.
Initialise an empty command group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The group's own name. |
required |
help
|
str
|
Short description of the group. |
''
|
Source code in src/applied_motion/cli/compose/core.py
add_command
add_child
Mount a child group under a name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
CommandGroup
|
The child group to mount. |
required |
name
|
str | None
|
Mount key. Defaults to the child group's name. |
None
|
Returns:
| Type | Description |
|---|---|
CommandGroup
|
The mounted child group. |
Source code in src/applied_motion/cli/compose/core.py
dispatch
Route a token sequence to a child group or a local command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
Sequence[str]
|
The whitespace-split command line. |
required |
Raises:
| Type | Description |
|---|---|
UnknownCommandError
|
If no matching child or command is found. |
Source code in src/applied_motion/cli/compose/core.py
iter_paths
Yield the fully-qualified path of every command in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
Sequence[str]
|
Internal prefix accumulator. |
()
|
Yields:
| Type | Description |
|---|---|
tuple[str, ...]
|
Tuples of namespace names ending in a command name. |
Source code in src/applied_motion/cli/compose/core.py
format_help
Build indented help lines for this group and all descendants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indent
|
int
|
Current indentation depth. |
0
|
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of formatted help lines. |
Source code in src/applied_motion/cli/compose/core.py
UnknownCommandError
Bases: CommandError
Raise when a token matches neither a child group nor a local command.
UsageError
Bases: CommandError
Raise when a command is invoked with missing or invalid arguments.
build_standalone_motion_parser
build_standalone_motion_parser(
*,
prog: str = "applied-motion",
extensions: Sequence[MotionCliExtension] = (),
) -> argparse.ArgumentParser
Build standalone motion CLI parser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prog
|
str
|
Program name shown in help output. |
'applied-motion'
|
extensions
|
Sequence[MotionCliExtension]
|
Optional extension hooks for additional subcommands. |
()
|
Returns:
| Type | Description |
|---|---|
ArgumentParser
|
Fully configured standalone parser. |
Source code in src/applied_motion/cli/cli.py
dispatch_motion_command
Dispatch parsed motion command namespace.
Initialises the gantry once and passes it to the selected handler.
Guarantees gantry.close() is called on exit regardless of outcome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Parsed argument namespace from a motion parser. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Process exit code. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no command handler is available in args. |
Source code in src/applied_motion/cli/cli.py
register_motion_cli
register_motion_cli(
parent_subparsers: _SubParsersAction,
*,
command_name: str = "motion",
extensions: Sequence[MotionCliExtension] = (),
) -> argparse.ArgumentParser
Attach motion CLI subtree to parent parser.
Designed for higher-level system CLIs that compose multiple domains
(for example motion and fluid) side-by-side.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent_subparsers
|
_SubParsersAction
|
Parent parser subcommand registry. |
required |
command_name
|
str
|
Name used for mounted motion command subtree. |
'motion'
|
extensions
|
Sequence[MotionCliExtension]
|
Optional extension hooks that can add extra motion subcommands. |
()
|
Returns:
| Type | Description |
|---|---|
ArgumentParser
|
Mounted motion command parser. |
Source code in src/applied_motion/cli/cli.py
run_repl
Launch the interactive teach-in REPL for a connected gantry.
Presents a prompt-toolkit REPL that accepts jog, capture,
where, home, teach pos, teach tray, list, save,
load, help, and quit commands. Tab-completion and command
history are provided automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
MotionSession
|
A |
required |
gantry
|
Gantry
|
The connected |
required |
Source code in src/applied_motion/cli/cli.py
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |