Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Python module

kinematics_sim runs the simulator from Python: the machines, their configs, runs, the cycle analysis, the verdicts with their error bands, the catalogue sizing, the cycle optimiser, the frequency sweep and the fitting and identification from measured traces, the loop analyses (step, Bode, tuning, margins, modes), sensitivity, replay, effect comparison, the sizing report, the dataset check and the FMU export. Every command of sim-cli is a function (list is machines; what the functions leave out is listed at the end), and they give the same numbers (the tests in crates/sim-py/tests compare them). Results come as NumPy arrays and as dicts of equal-length columns; pandas.DataFrame(result) takes every table as it is.

Source: crates/sim-py (PyO3 0.30.1, built with maturin 1.16.0). The wheel is abi3: one wheel per platform serves every CPython from 3.10 on. Type stubs and the py.typed marker are in the wheel. The extra kinematics-sim[mcp] adds an MCP server for LLM clients: MCP server and coding agents.

Install

pip install kinematics-sim

PyPI has the wheels of each release for Windows, Linux (x86-64) and macOS (Apple silicon); the extra mcp is there from 1.1.0 on. For the state of the repository, build the wheel there and install it:

pip install maturin==1.16.0
cd crates/sim-py
maturin build --release --out ../../target/wheels
pip install ../../target/wheels/kinematics_sim-*.whl

maturin develop --release installs it into the active virtual environment instead. A Rust toolchain (1.98.1) is needed for either. CI builds the wheel for Windows and Linux and keeps it as an artifact (wheel-windows-latest, wheel-ubuntu-latest).

Use

import kinematics_sim as ks
import pandas as pd

ks.machines()                       # [{"id": "t-gantry", "name": "T-gantry"}, ...]; ks.machines("de") in German

cfg = ks.Config("t-gantry")         # the default config; Config("t-gantry", path="my.toml") or text="..."
cfg["mass.payload_kg"]              # 2.0
cfg["mass.payload_kg"] = 3.5        # the value at a dotted path
cfg.set('parts.motor = "1FK2104-5AK"')   # exactly as sim-cli --set: the value in TOML syntax
cfg.to_toml()                       # the config as TOML

run = pd.DataFrame(ks.run(cfg, cycle=True, time=2.0))   # every signal, one column each, "time" first
pd.DataFrame(ks.analyze(cfg))                           # one row per motor, the columns of sim-cli analyze
pd.DataFrame(ks.verdicts(cfg, bands=True))              # one row per check and motor, with low, high, worst_ok
pd.DataFrame(ks.sizing(cfg, series="1FK2"))             # every catalogue motor on this cycle, passing first
functionwhat it doesas on the command line
machines(lang="en")the machines of the registry, with their names in English or Germansim-cli list
Config(machine, text=None, path=None)a machine’s default config, a config in TOML text, or a config file--config FILE
Config(machine_file=FILE, text=None, path=None)the same for a machine from a machine file (machine files); an error in the file names the file and line--machine-file FILE
cfg[path] = value, cfg.set("path = value")a value set on top. A part named under [parts] (cfg["parts.motor"] = "1FK2104-5AK") acts as --motor and the other part options: all named parts come in from the catalogue together, and every other value set stays on top of theirs, in whatever order the two were set--set, --motor and the other part options
signals(cfg){"id": [...], "unit": [...]}, the signals in the order run gives themthe header of sim-cli run
label(cfg, key, lang="en")the text of a message key for the machine, e.g. "check-motor-thermal" (what a verdict judges, with its unit); a key with no text comes back as it isthe texts of sim-app and the report
run(cfg, cycle=, move_to=, path=, trajectory=, time_optimal=, time=2.0, every=16, disable_at=){signal id: array} over time seconds, sampled every every base stepssim-cli run
analyze(cfg, path=, trajectory=, time_optimal=, points=False)the load per motor over one pass of the cycle, or over a path; points=True adds the operating points per motor as an (n, 2) array of speed (rad/s) and torque (N m); torque_unit and speed_unit give each motor’s units ("N m" and "rad/s", on a linear motor "N" and "m/s")sim-cli analyze (the column names are fixed: on a linear motor *_torque_nm holds N and peak_speed_rad_s m/s; the CLI’s trailing torque_unit,speed_unit columns, and those of --points and --breakdown)
verdicts(cfg, bands=False, samples=None, seed=1, worst=False, path=None, path_file=None, time_optimal=False)the checks; with bands their lowest and highest value over the [bounds] and whether the worst case passes; with samples N runs drawn between the bounds and the share that passed; with bands=True, worst=True the worst corner per check: worst, limit, worst_ok, at_high_bound and at_low_bound (lists of parameters), and runs; with path (the text of a path file or robot CSV, or with path_file a program by that name’s extension) the path’s run is judged in place of the cyclesim-cli verdicts [--bands] [--samples N --seed S] [--worst] [--path FILE [--time-optimal]]
sizing(cfg, supply=None, vendor=None, series="", gearboxes=None, axes=False, motor_id=None, drives="paired", rank_by="cost", duties=None, path=None, path_file=None, time_optimal=False)every catalogue motor on the config’s cycle, passing and cheapest first; supply one of the catalogue’s ("400v" when left out), vendor a maker’s supplies or "all" makers’, with the columns vendor and supply; drives "paired", "neighbours" or "all", rank_by as --rank-by ("energy", "cost=1,energy=0.5", …), duties a duty mix as a list of (Config, weight), the first the config searched on. The columns of the wide table: rank, front, cycle_s, energy_j, thermal_utilisation, drive_current_utilisation, following_error_m, settling_error_m, inertia_ratio, failing and estimated (lists), u_<check> per check (NaN where a candidate has none), torque_unit (of peak_torque_nm: "N" on a machine of linear motors, else "N m", the CLI’s trailing column); with path each candidate runs the path in place of the cyclesim-cli search [--drives] [--rank-by] [--config --config --weights] [--path FILE [--time-optimal]]
sweep(cfg, motor=0, from_hz=5.0, to_hz=500.0, count=120, amplitude=None)the frequency response from a motor’s torque to its speed: frequency_hz, gain_rad_s_per_nm, phase_degsim-cli sweep
optimise(cfg, runs=40, nominal=False, worst=False, pareto=False, path=None, path_file=None, time_optimal=False)a shorter cycle that passes every verdict: cycle_s, dwell_error_peak_m and dwell_angle_error_peak_rad as (before, after), runs, changes (columns setting, before, after), robust (what the bands cost, or None), with pareto the front as columns; with path the path’s time is shortened in place of the cycle’ssim-cli optimise [--path FILE [--time-optimal]]
fit(cfg, trace_file=, map_file=, stiffness=False, stiffness_key=, sweep_file=, sweep_motor=0, amplitude=)what a measured trace or sweep says the config should be: quantity, of, config, fitted, standard_error (NaN where the command line leaves the cell empty), units, the unit of each row ("" where it has none; the rigid values moved_mass_kg, viscous_friction_n_s_m, coulomb_friction_n, constant_force_n are kg, N s/m, N, N on a linear axis and kg m², N m s/rad, N m, N m on a rotary one; the CLI’s trailing units column), advice, what to change, and errors (see below)sim-cli fit
identify(trace_file=, map_file=, config=, motor=0, from_hz=, to_hz=, step_from=, step_to=, motor_inertia=, sweep_file=)one motor’s drive train from its own trace: quantity, of, value, standard_error, ci95_low, ci95_high, notes (what the trace could not tell), overlay (measured and modelled signals with time) and errorssim-cli identify [--overlay]
step(cfg, axis_index=0, size=0.001, time=0.5)an axis’s answer to a setpoint step: time_s, setpoint, response, and rise_time_s, overshoot, settling_time_ssim-cli step
bode(cfg, axis_index=0, from_hz=1.0, to_hz=200.0, count=40, amplitude=0.0005)the closed-loop response of an axis’s position loop: frequency_hz, gain, gain_db, phase_degsim-cli bode
tuning(cfg, size=0.001, amplitude=0.0005)per axis rise_time_s, overshoot, settling_time_s, bandwidth_hz (NaN where the response stays above -3 dB to 200 Hz)sim-cli tuning
margins(cfg, bands=False)per run and motor the velocity loop’s crossover_hz, phase_margin_deg, gain_margin_db, peak_sensitivity, peak_frequency_hz, ok; crossover_hz and phase_margin_deg are NaN where the loop has no gain crossover in the band (not measurable; the CLI leaves the cells empty), and ok is then the gain margin’s alonesim-cli margins [--bands]
modes(cfg, axis_index=0, points=21)the modes over an axis’s stroke: position, mode<i>_hz, mode<i>_damping (NaN where a place lacks one), and mode<i>_small_hz where the machine’s small-amplitude modes differ (a gearbox’s hysteresis stiffness K1 + sigma, the 3-axis delta)sim-cli modes
sensitivity(cfg, delta=0.2, groups=None)each parameter at its low and high value, strongest first: parameter, value, low, high, the relative changes of peak and RMS torque and cycle time, the least belt tensionsim-cli sensitivity [--group G]
global_sensitivity(cfg, method="morris", samples=None, seed=1, groups=None, results=None)all values of [bounds] at once, ranked per result: result, rank, parameter, and mu_star, mu, sigma (Morris) or first, first_low, first_high, total, total_low, total_high (Sobol’)sim-cli sensitivity --method morris|sobol
replay(cfg, trace_file=, map_file=None)a trace’s setpoints through the simulator: per motor torque and following error apart (RMS, peak), and overlay, measured and simulated side by sidesim-cli replay [--out FILE]
compare(cfg, effect=, signals=None)the cycle with effects.<effect> on and off: time, <id>.on, <id>.off, and differences (signal, unit, peak_difference, rms_difference)sim-cli compare [--differences FILE]
report(cfg, path, bands=False, lang="en")the sizing report as PDF (a path ending in .pdf) or HTML; returns whether every check passedsim-cli report
check_dataset(path)what keeps a dataset folder from being published, one text per problem; empty: readysim-cli check-dataset
fmu(cfg, path, binaries=[...], fmi=2, lang="en")the machine as an FMI 2.0 or 3.0 FMU (FMI) carrying the sim-fmi binaries named (cargo build -p sim-fmi --release builds the one of this platform)sim-cli fmu [--binary FILE]

path and trajectory take the text of a path file or a trajectory file (formats: sim_components::path), not a file name: ks.analyze(cfg, path=open("pick.path").read()). trace_file, map_file and sweep_file take file names, as the command line does (a vendor export is decoded from its bytes). A refused config, command, path or trace raises ValueError with the simulator’s message in English; an unknown machine or config key read with cfg[path] raises KeyError; an int beyond 64 bits set with cfg[path] = value raises OverflowError (TOML’s integers are 64-bit; before 14 November 2026 it became a float silently). run takes at most 1e8 base steps and samples at most 2.5e8 values (2 GB of arrays); a longer run raises ValueError at once instead of seeming to hang or exhausting the memory: raise every, or run in pieces.

A config change is checked as a config file is: a key the machine’s default config does not have is refused by name.

fit and identify run several fits, and one may find nothing (a measured sweep with no resonance, a trace with no ringing after step_from, no two-mass model). The command line writes what the others found and then fails; these two functions return what the others found, and list each fit that found nothing in result["errors"] (its rows are not in the table). Check it: an empty list means every fit asked for is there. What keeps every fit from starting (a refused config, an unreadable trace or sweep file) still raises ValueError.

Test your own controller

ks.Sim(cfg) is a simulation you step yourself, so that a controller written in Python closes the loop on the simulated mechanics:

sim = ks.Sim(cfg)                      # at rest at the start pose, drives on, every motor in position mode
out = sim.step(torque=[0.3])           # one cycle (sim.cycle = profile.setpoint_interval_s) in torque mode, N m per motor
out = sim.step(velocity=[20.0], time=0.001)   # 1 ms in velocity mode, rad/s per motor (m/s on a linear motor)
out = sim.step(setpoint=[0.5])         # position mode: a setpoint per tool axis, interpolated as from a PLC
out["motor.a.angle"], out["motor.a.speed"], out["motor.a.current"]   # every signal at the end of the step
sim.signals(), sim.time, sim.dt, sim.modes   # ["torque"], ...
sim.analyze()                          # the columns of ks.analyze over the run since the start or reset()
sim.verdicts()                         # the verdicts on that run: check, motor, value, limit, ok
sim.stop(1)                            # a category 1 stop (0: torque off, 2: brake and hold); step() runs it
sim.enable()                           # the drives on again
sim.reset()

The modes are a servo drive’s: in torque mode your torque replaces the velocity loop’s output, in velocity mode your speed replaces the position loop’s; either way it still passes the drive’s torque filter, torque limit and delay, the current loop, the winding and the DC link, so a torque above the limit is clamped and current, voltage and winding temperature are as real. A step with none of torque, velocity, setpoint keeps the modes and setpoints. A mode switch does not make the torque jump where the drive’s own loops take over (their PI starts from the torque last asked for); a setpoint or another plan command is refused while a drive is out of position mode, and a setpoint puts every drive back in it. FMI export has the details, which are the same.

analyze() and verdicts() judge your controller’s run as ks.analyze and ks.verdicts judge the built-in drive on the cycle: peak and RMS torque, thermal utilisation, winding rise, energy and the DC link, the gearbox and belt checks. “cycle_s” is the time run. The path and tracking errors there are measured against the controller’s plan, which stands still while you drive: judge the following of your own setpoints yourself.

crates/sim-py/examples/own_controller.py is a worked example: it measures the inertia at the motor with two torques (1.344e-4 kg m^2 on the belt axis, by hand 6 kg (0.02/5)^2 + 0.26 kg of belt (0.02/5)^2 + 1e-4/25 + 0.3e-4), tunes a PD on the motor angle for 20 Hz, runs it at 1 kHz on the move the built-in cascade runs, and prints both runs’ load and failing checks. The PD needs about the RMS torque of the cascade (0.172 against 0.174 N m) and, having no integrator, stops 0.05 rad short against the guides’ friction.

Parameter sweeps

Every long call releases Python’s global interpreter lock, so a thread pool runs several on all processors:

from concurrent.futures import ThreadPoolExecutor

def peak(mass):
    c = cfg.copy()
    c["mass.payload_kg"] = mass
    return ks.analyze(c)["peak_torque_nm"].max()

with ThreadPoolExecutor() as pool:
    peaks = list(pool.map(peak, [1.0, 2.0, 4.0, 8.0]))

crates/sim-py/examples/parameter_sweep.ipynb is a worked example: the T-gantry’s payload from 0.5 kg to 8 kg, peak and RMS torque, thermal utilisation and the verdicts per mass, and the signals of one run as a DataFrame. The test suite runs it from top to bottom.

Cancel a long call

A Job counts the runs of the analyses called inside it and stops them from another thread (the MCP server does this when a call times out). job.cancel() starts no new run; job.run raises kinematics_sim.Cancelled (an Exception) once the runs under way have ended:

import threading

job = ks.Job()
t = threading.Thread(target=lambda: job.run(lambda: ks.verdicts(cfg, bands=True, samples=2000)))
t.start()
print(job.done, "of", job.total)  # runs finished, runs planned so far (later batches add to it)
job.cancel(); t.join()            # the thread's job.run raised ks.Cancelled

Every function the callable calls on that thread belongs to the job; one that raises something else keeps its error. A job that is cancelled before run ends the first batch at once. Calls without a job run to the end as before.

Tests

cargo build -p sim-cli -p sim-fmi --release   # the tests compare with them
pip install "pytest==9.2.0" pandas matplotlib nbformat nbclient ipykernel
pytest crates/sim-py/tests

The suite checks the machine list, config reading, setting and refusals, the TOML round trip, run against sim-cli run (same samples to 1e-8), analyze and verdicts against the command line, that a heavier payload needs more torque, the order of the sizing table, that two threads get the same result, and the notebook. 11 tests pass on Windows with CPython 3.14 (13 November 2026). Added on 14 November 2026: a machine file run against sim-cli run --machine-file, its refusals, sweep, optimise, fit and identify against the command line on one pass of the cycle, the overflow of 2^63 (one more than the largest 64-bit integer) and the guard on run(time=1e12); those 10 pass on Windows with CPython 3.14. Added later that day, each against the command line on a shipped config: step, bode, tuning, margins, modes, sensitivity, replay (with its overlay), compare (with its differences), report (the same HTML), check_dataset, fmu (the same FMU bytes, FMI 2.0 and 3.0), verdicts at the worst corner, sizing of one motor of a machine file and by vendor, and fit and identify with a sweep that has no resonance (the same rows as the command line writes before it fails, and the failure in errors); those 13 pass on Windows with CPython 3.14 in 19 minutes, most of it the worst-corner search and the sizing.

Not covered

What the command line writes to the error stream beside its table (the clearance of a planned path to obstacles, the thermal start of the measured pass, the conveyor’s picks, the search’s timing and screened count, the deviation of a planned path) has no place in the results. analyze’s --breakdown and --budget tables, optimise’s TOML of the new cycle, and --catalog DIR (another catalogue folder) have no argument. path= takes the text of a path file only, not a G-code, KRL, RAPID or robot CSV program as --path FILE does. fmu needs the sim-fmi binaries named. sim-cli search --robust, transmission and compare-kinematics have no function yet. Messages are in English only.