Skip to main content

Crate sim_wasm

Crate sim_wasm 

Source
Expand description

The simulator in the browser: core, machines and analysis built for wasm32-unknown-unknown, behind a few C-ABI functions that take and give UTF-8 text. web/index.html loads the module and runs a sizing check with it.

The page writes a request into memory from alloc, calls call(kind, ptr, len) and reads the answer, JSON, from out_ptr() and out_len(). call returns 0 when the answer is a result and 1 when it is {"error": "..."}.

kindrequest linesanswer
0language[{"id", "name", "file"}], every machine
1machine id{"config"}, the machine’s default config (TOML)
2machine id, language and options, a config (TOML)the run’s load per motor and its verdicts
3machine id, language and options, a config (TOML){"dt_s", "centre", "radius", "motors", "sample"}: the 3D view starts
4file name, language, then a machine file (TOML){"id", "name", "config"}: the machine to use as an id
5machine id, language and options, a config (TOML){"motor", "points"}: frequency response of one motor
6machine id, language, a config, ---, a trace (CSV){"rows", "advice"}: masses and friction fitted to the trace
7language, then translation keys, one a line{key: text}: the page’s texts and those keys
8machine id, language, a config (TOML){"params", "parts"}: the values the page’s form edits
9machine id, language, a config, ---, path = value lines{"config", "changed", "compact"}: the values set in the config’s own text
10machine id, language, a config (TOML){"step_s", "time", "signals", "motors"}: the scopes of one pass of the cycle

An empty config is the machine’s default one. Parts the config names ([parts]) come from the built-in catalogue, as in every front end. WebAssembly starts no threads, so analyses that share the processors natively run one after another here.

Options follow the language on its line, separated by spaces: path runs the path (format: sim_components::path) that follows the config after a line --- instead of the cycle, timed within the axis limits or with time-optimal as fast as the motors allow (kinds 2 and 3); motor=K, from=HZ, to=HZ, count=N and amplitude=NM set the sweep of kind 5 as sim-cli sweep takes them. A line --- is never part of a TOML config.

Machines: the registered ones by their id, the machine files of machines/ built into the module as file:<name>.toml, and a file the page hands in (kind 4) as upload:<name>.

The 3D view: kind 3 sets the machine at its start pose and starts its cycle or path; centre and radius are the sphere round its scene there, to fit the camera to. Then the page calls frame(seconds), which runs the simulation on by that many seconds and returns where frame_len() 32-bit floats lie, no JSON: a head of HEAD floats (time_s, cycle_s, the tool’s x, y, z, the number of parts, of obstacles and of samples; NaN for what is not known yet), RECORD floats per part and per obstacle, as record lays them out, and sample floats per sample as Player::sample lays them out: the plots’ and the trail’s data since the last frame. A request that is not a finite time from 0 to MAX_FRAME_S is refused: frame returns null and the error is the answer at out_ptr.

Constants§

HEAD
Floats in front of the parts in a frame.
MAX_FRAME_S
The most simulated time one frame runs, s: more is refused rather than left to hang the page.
MOTOR_SIGNALS
Signals per motor in a sample, each as its lowest and highest value over the sample: torque, torque command, speed, speed command, following error.
OBSTACLE_MESH
The mesh number of an obstacle: a cube drawn see-through.
RECORD
Floats per part in a frame.
SAMPLE_HEAD
Floats of a sample in front of its motors: time, the tool’s x, y, z, and its distance from the plan.
SAMPLE_S
How long a sample spans, s: the plots get every peak, as the lowest and highest value within it.
SCOPE_ROWS
The most rows a scope answer has: one a millisecond up to a pass of 30 s.
SCOPE_SIGNALS
The signals of the scopes (kind 10), per motor, by the end of their id motor.<id>.<signal>.

Functions§

alloc
Room for a request of len bytes, which the caller writes and hands to call; call frees it.
bounds
The box with edges along the axes round every part of records (RECORD floats each): lowest and highest corner.
call⚠
Safety
frame
Runs the 3D view’s machine on by seconds and returns its frame (see the module’s documentation); null before kind 3 has set one up, and null with the error as the answer at out_ptr for a time that is not finite, below 0 or over MAX_FRAME_S (an infinite one ran the steps’ count to the largest integer and hung the page).
frame_len
How many floats the last frame has.
handle
The answer to a request as call gets it, or the message of why there is none.
out_len
How long the last answer is, in bytes.
out_ptr
Where the last answer is.
record
One part as the page draws it, RECORD floats: the mesh (0 a cube of side 1, 1 a cylinder of diameter and length 1 along y, both centred on the origin; turned as Part::flat says), the matrix that takes the mesh to the part in scene coordinates (4 × 4, column by column, as WebGL takes it), and the colour (part_color, red, green, blue in 0..1). A segment is a cube stretched from pos to to, width wide in the depth (y) and 4 mm thick, as in sim-app.
record_obstacle
An obstacle as the page draws it, RECORD floats as record lays them out: mesh OBSTACLE_MESH (the unit cube, drawn see-through), stretched to the obstacle’s box, grey, or red while a part of the machine touches it.
sample_len
Floats per sample for a machine of motors motors.
torque_speed_curve
The torque-speed curve of a motor’s drive (DriveParams::torque_curve) as an outline of (speed rad/s, torque N m): the highest torque from the no-load speed backwards to the no-load speed forwards, then the lowest torque back, at the curve’s corners and 40 speeds between (a field-weakened curve bends). And the continuous (S1) rating as the app and the motor-s1 verdict have it (charts::torque_speed): its line for positive torque, then the one for negative torque back, closed.