# moon-foc

> Working-tree verification (2026-11-22): native, js, wasm-gc and wasm each pass 181/181. Native strict checks and formatting pass.

[中文](./README.md) · English

PMSM field-oriented control numerical library in MoonBit: a closed current-loop
controller, a simplified motor model, plus SVPWM modulation and Clarke/Park transforms.

Pure numerical core for field-oriented control: space-vector PWM (six-sector
detection, T1/T2 active-vector timing, overmodulation clamping), Clarke /
Park / inverse-Park transforms, angle conversion, a `controller/` closed
current-loop object, a current-loop design layer that turns motor R/L plus a
target bandwidth into PI gains, two-phase current sampling (third-phase
reconstruction plus ADC sample-window checks), a speed outer loop with a
single-inertia mechanical model, and a simplified `motor/`
electrical model for offline simulation. No C FFI, no I/O. Current working-tree verification is listed above.
On the simplified model, `examples/foc_sim` holds id_ref=0, iq_ref=2.0 A: a
native local run on 2026-11-13 tracks iq to 1.9999975 A within 20 ms with id=0,
and from 0.04 s reaches iq≈2.0 A, id=0, steady vq≈1.0 V.

## Install

Published to mooncakes as `Duckweed/moon-foc@0.1.17` (release 0.1.17, MIT,
published 2026-11-22):

```
moon add Duckweed/moon-foc@0.1.17
```

The mooncakes namespace is `Duckweed` while the GitHub repository lives under the `Duckweed-yhb` account. Same author: `moon add` uses the former, repository URLs use the latter.

The bundled `examples/demo` can still be run directly from this repository
without installing the package.

## Quick start

```moon
// examples/demo/main.mbt
let (alpha, beta) = @coord.clarke(1.0, -0.5, -0.5)   // → alpha=1, beta=0
let (d, q) = @coord.park(alpha, beta, 0.7)            // rotate by theta=0.7 rad
let sector = @svpwm.sector_of(alpha, beta)            // → 1
let (da, db, dc) = @svpwm.duties(alpha, beta, 0.0001, 24.0)  // Vdc=24V, Ts=100us
```

Run the bundled demo (the default wasm target works, and other targets are
supported too):

```
moon run examples/demo                    # default wasm
moon run examples/demo --target native
moon run examples/demo --target js
```

Conventions:

- Amplitude-invariant Clarke: `alpha = (2/3)(ia - ib/2 - ic/2)`, `beta = (1/√3)(ib - ic)`.
- Park: `d = alpha*cosθ + beta*sinθ`, `q = -alpha*sinθ + beta*cosθ`.
- SVPWM duties are centered around 0 (range [-1, 1]); 0 means 50% on-time.
- Linear modulation limit: `|Vref| <= Vdc/√3`. Beyond that, T1/T2 are scaled
  so `T1+T2 = Ts`, which keeps the reference direction and pushes the magnitude
  onto the hexagon edge.
- How much is clipped can be computed per direction: `hexagon_radius(alpha, beta,
  Vdc)` returns the hexagon boundary along that direction (exactly `Vdc/√3` on a
  sector axis, `2·Vdc/3` at a vertex), `project_to_hexagon` keeps the direction and
  lands the vector on that boundary, and `clipping_ratio` reports the fraction of
  magnitude removed. The proportional clamp and this geometry reach the same
  magnitude; the tests cross-check by projecting first and then running
  `action_times`, whose T1+T2 must come out at exactly Ts. Mode II (hold the
  magnitude, compress the phase, vertex-hold segments) needs per-fundamental angle
  state and is not implemented; only phase-preserving clipping is, so a 30 V
  reference is untouched at 0° and clipped by 7.62% at 30° with `Vdc = 48 V`.
  `examples/design_report` prints these boundary readings at the end.

## Current-loop controller and simplified motor model

`controller/` wires the existing blocks into a per-step object: sample three-phase
currents → Clarke → Park → d/q dual PI → inverse Park → SVPWM, returning observed
d/q currents, d/q voltage commands and three-phase duties. Transforms, PI and
modulation reuse the packages above; this package only orchestrates the chain and
keeps internal state.

```moon
let ctrl = @controller.FocController::new(
  2.0, 1000.0,   // d-axis kp / ki
  2.0, 1000.0,   // q-axis kp / ki
  1.0e-4, 24.0,  // Ts / Vdc
  -12.0, 12.0,   // voltage command clamp
)
let (id, iq, vd, vq, da, db, dc) = ctrl.step(ia, ib, ic, theta_e, id_ref, iq_ref)
```

- If any sampled input is non-finite it does not enter PI state; observed values and
  duties return zero together; `reset()` clears both d/q integrators.
- `motor/` is an offline simplified PMSM model: R, Ld, Lq, ψf and pole pairs are
  parameterized; d/q first-order equations are integrated with explicit Euler, and
  electrical speed is given externally. The d-axis `−ωe·Lq·iq` cross-coupling term
  is off by default and `enable_cross_coupling()` turns it on to match the full d/q
  equations; keeping it off leaves every existing example and reading unchanged.
  `motor/mechanical.mbt` adds a single-inertia
  mechanical equation (`J·dωm/dt = Te − TL − B·ωm`) that integrates the torque from
  the currents into speed for the speed loop.
- `examples/foc_sim` steps the current loop on this simplified model at standstill,
  iq_ref=2.0 A, id_ref=0 A; the native local convergence numbers are listed in the
  opening paragraph above.

```
moon run examples/foc_sim --target native
```

Implemented and verified: d/q current tracking, voltage clamp, PI anti-windup,
the first-order dynamics of the simplified model, the optional d/q cross-coupling
term (off by default), model-based decoupling
feedforward, the speed loop and the single-inertia mechanical equation, and a
steady-state field-weakening reference derived from the voltage limit. Not
implemented (out of plan): sensorless observer, four-quadrant operation, real
I/O and FFI.

### Current-loop design

`controller/design.mbt` answers the inverse question: given the motor, what should
the gains be. The plant is taken as first order `P(s) = 1/(Ls + R)` and the
controller as `C(s) = Kp + Ki/s`; with zero-pole cancellation `Ki/Kp = R/L` the
loop magnitude reduces to `|C·P| = Kp/(L·ω)`, so the crossover lands at
`ω_c = Kp/(2πL)` with a 90° phase margin. Bandwidth therefore becomes a design
input:

```moon
let g = @controller.design_current_loop(0.5, 0.002, 800.0)  // R / L / target Hz
// g.kp() → 10.05 V/A, g.ki() → 2513 V/(A·s), Ki/Kp = 250 = R/L
let fc = @controller.crossover_hz(0.5, 0.002, g.kp(), g.ki())        // → 800 Hz
let pm = @controller.phase_margin_deg(0.5, 0.002, g.kp(), g.ki())    // → 90°
let loss = @controller.delay_phase_loss_deg(fc, 50.0e-6)             // → 14.4°
let after = @controller.margin_after_delay_deg(0.5, 0.002, g.kp(), g.ki(), 50.0e-6)  // → 75.6°
```

The 90° from the ideal model is only the starting point: a digital implementation
also carries one sample of delay (sampling plus PWM update), costing `360·f_c·Ts`
degrees. Pushing the same machine to 4 kHz with `Ts = 50 µs` burns 72° of that and
leaves 18°, which is what actually caps the achievable bandwidth.
`design_is_usable(..., ts, min_margin_deg)` judges against the post-delay margin;
the threshold is up to the caller's own engineering rules.

`examples/design_report` runs the whole flow: print the gains and margins, then feed
the same gains back into `FocController` + `PmsmModel`. Native local run
(2026-11-14, toolchain 0.1.20261104) with R=0.5 Ω, L=2 mH, Ts=50 µs, Vdc=48 V and a
target bandwidth of 800 Hz gives Kp=10.053 V/A, Ki=2513.3 V/(A·s), crossover 800 Hz,
ideal margin 90°, delay loss 14.4°, post-delay margin 75.6°, usable at a 45° floor;
the closed loop reaches iq=1.99999 A at 20 ms for a 2.0 A reference and settles to a
steady-state error of 2.2e-16 at 0.2 s.

```
moon run examples/design_report --target native
```

### Decoupling feedforward

The d and q axes are not independent in the rotating frame: `vd` carries
`−ωe·Lq·iq`, `vq` carries `ωe·(Ld·id + ψf)`. Both are disturbances from the PI's
point of view, and the faster the rotor turns the harder it is for the integrator
to grow them. `controller/feedforward.mbt` computes them from the motor data and
adds them before the voltage clamp:

```moon
let (vd_ff, vq_ff) = @controller.decoupling_terms(omega_e, ld, lq, psi_f, id, iq)
// ωe=400, Lq=1.2 mH, iq=5 A → vd_ff = -2.4 V
// Ld·id + ψf = 0.0524 Wb → vq_ff = 20.96 V
let (vd, vq) = @controller.apply_feedforward(vd_pi, vq_pi, vd_ff, vq_ff, 48.0)
```

`apply_feedforward` limits the magnitude of the combined vector
`sqrt(vd²+vq²)` to `Vdc/√3`, the same linear-region boundary `svpwm` uses. When the
sum exceeds it, both components are scaled by one factor so the direction is
preserved; clamping `vd` and `vq` separately would rotate the reference. A non-positive
or non-finite `vdc` disables the clamp and only sums, and non-finite components
return `(0, 0)` instead of injecting NaN.

`examples/feedforward_loop` runs the two variants side by side with identical
parameters and gains (iq step 0 → 5 A, Vdc 48 V, Ts 10 µs, PI tuned to 400 Hz).
Native local run (2026-11-14): at ωe=200 the time to enter the ±2 % band drops from
5.95 ms to 1.5 ms and the peak deviation from 4.999 A to 4.9157 A; at ωe=400 from
6.99 ms to 1.51 ms and 7.0343 A to 4.9357 A; at ωe=480 from 7.25 ms to 2.58 ms and
8.1312 A to 4.9691 A. All runs converge to 5 A at the end because the PI integrator
gives zero steady-state error anyway; the difference is entirely in the transient,
where the plain loop first has to integrate the back-EMF and overshoots on the way.

Limits: the feedforward uses nameplate model parameters, so any mismatch in
`Ld`/`Lq`/`ψf` shows up as residual error. There is no parameter identification,
no current prediction, and no dead-time term here; the module removes only these
two known disturbances.

### Speed loop and mechanical model

The earlier sections stop at the current layer. `motor/mechanical.mbt` adds the
mechanical equation and `controller/speed.mbt` builds the speed PI on top, so the
two loops can run together as "speed PI hands out iq, current loop follows iq":

```moon
let mech = @motor.MechanicalModel::new(0.002, 0.0005, 4, 0.0)   // J / B / pole pairs / initial speed
let te = @motor.electromagnetic_torque(id, iq, ld, lq, psi_f, pole_pairs)
mech.step(te, 1.0, 1.0e-4)   // torque / load / step, J·dωm/dt = Te − TL − B·ωm

let g = @controller.design_speed_loop(0.002, 0.0005, 0.3, 40.0)  // J / B / Kt / bandwidth Hz
let sc = @controller.SpeedController::new(g.kp(), g.ki(), 1.0e-4, -8.0, 8.0)
let iq_ref = sc.step(100.0, mech.omega_m())
```

The tuning rule differs from the current loop: the current loop cancels the
electrical pole `R/L`, the speed loop does not cancel the mechanical pole. `B/J`
is typically a few tenths of a rad/s, and cancelling it leaves the loop behaving
like a pure integrator: all damping rides on `Kp` and one very slow mode remains.
That number comes from a local run, not from theory - after a load step
`examples/speed_loop` returned only to 98.3 rad/s within 1 s with iq already at its
steady value, which looks like a steady-state error but is the slow mode still
settling. The module instead puts the integral time constant at one fifth of the
bandwidth: `Kp = ω_bw·J/Kt`, `Ki = Kp·ω_bw/5`.

The public `simulation.SpeedDrive` runs the same cascaded controller one sample at a time. Call `drive.step(speed_ref, load_torque)` with speed in rad/s and load in N·m. The single-direction model rejects negative speed commands before changing state. Its constructor accepts inductances of at least 1 nH and inertia of at least 1e-12 kg·m² so the PI design and simulated motor use the same parameters.

`examples/speed_loop` uses R=0.5 Ω, Ld=0.8 mH, Lq=1.2 mH, ψf=0.05 Wb, pp=4
(Kt=0.3 N·m/A), J=2e-3 kg·m², B=5e-4, Vdc=48 V, Ts=0.1 ms, current loop 400 Hz,
speed loop 40 Hz, iq reference limited to ±8 A. Each current PI uses its own axis inductance. Native local run (2026-11-18, toolchain
0.1.20261104):

- 0 → 100 rad/s: 10 % point at 8.7 ms, ±2 % band at 83.5 ms, peak 100.4753 rad/s
  (0.48 % overshoot), iq reference peak 8 A and simulated iq peak 8.0000 A to four decimal places.
- 1 N·m load step at 1.0 s: maximum dip 1.5569 rad/s, back inside ±1 % after 20.5 ms.
- Last 0.2 s: speed steady at 100 rad/s, final iq 3.5 A (1 N·m / Kt = 3.333 A plus
  the damping term B·ω/Kt = 0.167 A).

```
moon run examples/speed_loop --target native
```

Limits: the mechanical model is a single inertia with linear friction only, and
speed is clamped non-negative (no four-quadrant or reverse operation); step size
stability is the caller's job. The speed loop has PI plus output limiting only -
no acceleration feedforward, no observer; field weakening is not inside the
speed loop but attached through `field_weakening_id` (see the next section). The
electrical model in this example has no d/q cross-coupling, so the voltage
feedforward adds back-EMF only; injecting `vd_ff` would feed the model a
disturbance it does not have and falsify the readings.

### Field weakening

Above base speed, `ωe·ψf` alone fills the voltage budget, and a PI pushing harder
just runs into the clamp. `controller/field_weakening.mbt` inverts the steady-state
voltage equations `vd = −ωe·Lq·iq`, `vq = ωe·(Ld·id + ψf)` under
`vd² + vq² ≤ Vmax²` to produce a d-axis reference:

```moon
let h = @controller.flux_headroom(600.0, 0.0008, 0.0012, 0.05, 0.0, 27.7128)  // → −0.003812 Wb
let id = @controller.field_weakening_id(600.0, 0.0008, 0.0012, 0.05, 0.0, 27.7128, -8.0)
// → −4.7650 A
```

`flux_headroom = sqrt(Vmax² − (ωe·Lq·iq)²)/|ωe| − ψf`. A positive value means the
machine is still in the constant-torque region and `field_weakening_id` returns 0;
only a negative value pushes `id = headroom/Ld`, clamped into `[id_min, 0]`. q-axis
load tightens the budget: at ωe=600 with iq=5 A, `ωe·Lq·iq` takes 3.6 V first and
weakening reaches −5.2542 A. When no solution exists
(`Vmax² − (ωe·Lq·iq)² ≤ 0`), the function returns the current floor instead of
inventing a root.

Limits: steady-state approximation only - no resistive drop, no `di/dt`, no phase
lag from current-loop bandwidth, and no optimal current allocation inside the
weakening region. With `vmax = Vdc/√3` the boundary coincides with the linear region
of `svpwm`.

### Field weakening in the speed loop

The previous section only answers "given speed and iq, what id should be". 
`examples/weak_flux_loop` closes that into the speed loop: the reference ramps to
170 rad/s at 150 rad/s², the q axis comes from the speed PI, the d axis runs
`field_weakening_id` every step, and the electrical model uses the flux
`ψf + Ld·id` on the q axis. The same parameters run twice with only the weakening
switch flipped:

```
moon run examples/weak_flux_loop --target native
```

Native readings on this machine (2026-11-15, toolchain 0.1.20261104):

- weakening off (id fixed at 0): speed stalls at 137.99 rad/s, iq 0.23 A at the
  tail, voltage peak 27.71 V, 11150 steps sitting on the limit.
- weakening on: ceiling 156.54 rad/s, tail id −8 A and iq 0.2452 A, same 27.71 V
  voltage peak.
- the ceiling moves up by 18.55 rad/s, or 13.44 %; the deepest id is −8 A, exactly
  the `id_min` clamp.
- tail tracking: 156.54 of 170 rad/s with weakening, 137.99 of 170 rad/s without -
  neither reaches the reference, 48 V simply runs out at the top of the linear
  region.

The weakening-off ceiling matches `Vmax/(pp·ψf) = 27.7128/(4·0.05) = 138.56 rad/s`;
the difference comes from `R·iq` and vector limiting. With weakening the flux drops
to `0.05 − 0.0008·8 = 0.0436 Wb`, which by the same formula allows 158.9 rad/s, and
the measured 156.54 again loses the rest to resistive drop.

The model changed as well: the q-axis equation in `motor/` went from `ωe·ψf` to
`ωe·(ψf + Ld·id)`. In the old form id had no effect on back-EMF at all, so
weakening did nothing in simulation - without that term the two runs above would
print the same number. The d axis still omits the `−ωe·Lq·iq` cross term, and
the separate torque helper includes the `(Ld-Lq)·id·iq` reluctance term,
so the weakening depth reads optimistic against a real machine.

Limits: only the "given speed, hit the voltage limit" layer is wired up. There is
no MTPA / maximum-torque-per-voltage allocation in the weakening region, no
closed-loop id tracking against the voltage margin, and no desaturation strategy;
bus voltage and machine parameters come from example constants rather than online
identification.

### Current sampling

The three phase currents sum to zero at every instant, so production hardware
usually carries only two sensors or two shunt resistors and reconstructs the third
phase in software. `controller/sampling.mbt` exposes that step together with its
checks:

```moon
let ic = @controller.reconstruct_third(1.984, -0.972)          // → -1.012
let r = @controller.zero_sequence_residual(1.984, -0.972, ic)  // → 0
// single/double-shunt sampling needs a long enough low-side on-time
let margin = @controller.sample_window_margin(0.52, 50.0e-6, 1.5e-6)  // → 22.5 µs
let ok = @controller.window_ok(0.88, 50.0e-6, 1.5e-6)                 // → true, 4.5 µs left
let pairs = @controller.pick_sample_pair((0.52, 0.31, 0.88), 50.0e-6, 1.5e-6)
// → [(0, 1), (0, 2), (1, 2)], ranked by the smaller margin of each pair
```

An empty array from `pick_sample_pair` means none of the three phases has a usable
window, so this sample should be dropped rather than read as noise.
`zero_sequence_residual` is not part of the control path; it is the first signal for
an offset, a broken channel or two unsynchronised channels, and
`currents_consistent` turns that into a boolean against a tolerance.

`FocController::step_from_two_phases(ia, ib, θe, id_ref, iq_ref)` pushes the
reconstructed ic into one current-loop tick, with the sampled phases fixed to a/b.
When the pair changes tick to tick, `pair_currents(pair, i1, i2)` scatters the two
measurements back onto a/b/c and `FocController::step_from_pair(pair, i1, i2, θe,
id_ref, iq_ref)` drives a tick directly. The caller always chooses the pair; the
controller never picks it implicitly. An out-of-range index, a repeated phase or a
non-finite sample is rejected at the entry with a zero tuple instead of being fed
into the PI as a genuine zero current.

`examples/sampling_loop` closes the loop on two-phase sampling: one motor and one
set of design gains run two trajectories, A on three-phase ground truth and B on
two measured phases with the pair picked from the previous tick's duties. Local
native readings (2026-11-14): `|iq(A) − iq(B)|` stays at 0 across the whole run,
both settle at 1.9999999999999998 A by 0.2 s, the pair switches once (`(0,1)` to
`(0,2)`), and no tick lacks a usable pair. Adding a 0.02 A offset on phase a puts
iq at 1.98845 A, 0.01155 A off the reference; with only two sensors the residual
stays 0 - the third phase is computed, so that check cannot see a single-channel
offset; with an extra true phase c the residual reads 0.02 A.

```
moon run examples/sampling_loop --target native
```

`examples/design_report` prints these readings at the end: for duties
`(0.52, 0.31, 0.88)`, `Ts = 50 µs` and a 1.5 µs settling floor, the margins are
22.5 / 33 / 4.5 µs, the preferred pair is `(0,1)`, and the reconstruction gives
`ic = -1.012` with zero residual.

### Sampling step and numerical stability

`examples/stability_scan` keeps one set of current-loop gains designed for 400 Hz
(R=0.5 Ω, L=1 mH, standstill, Vdc=24 V, ±12 V limits) and only changes the
simulation step. Local native readings (2026-11-21):

| Step | Peak | Overshoot | Back in ±2% band | Final error |
|---|---|---|---|---|
| 50 µs | 2.0000 A | 0 | 0 | 3.1e-13 A |
| 100 µs | 2.0000 A | 0 | 0 | 8.8e-13 A |
| 200 µs | 2.0000 A | 0 | 0 | 3.3e-12 A |
| 500 µs | 3.1416 A | 57.1% | 4 ms | 3.9e-11 A |
| 1 ms | 8.2301 A | 311.5% | not settled | 10 A (sustained oscillation) |

The usable step of the explicit-Euler plant is set by the closed-loop gain as
well as `L/R`: the same gains are clean up to 200 µs, show visible overshoot at
500 µs, and oscillate at 1 ms. `simulation/stability_scan_test.mbt` pins the
100 µs / 500 µs / 1 ms readings so a change to the PI or the limiter cannot move
that boundary silently.

```bash
moon run examples/stability_scan --target native
```

This is an offline observation tool, not a sizing rule; a real controller still
has to account for switching frequency, sampling delay and current ripple.

## Browser demo (`docs/demo`)

`examples/web_demo` evaluates a whole parameter grid and prints JSON lines to the console (one `META` line with design readouts, one `DATA` line per panel); `docs/demo/index.html` captures those lines, draws them, and adds parameter sliders. Every number on the page comes from a function in `controller/` or `motor/` - the JavaScript never re-derives the model.

The demo uses a grid evaluated at build time. Sliders select stored results; this build does not expose a callable solver interface to the page.

Three slider groups:

- Current-loop bandwidth from 100 to 1000 Hz in 19 steps of 50 Hz. Each step carries the `design_current_loop` readouts (Kp, Ki, phase margin, margin after delay) plus a 240-tick closed-loop step response (iq reference 0 to 5 A, dt 0.1 ms). Three fixed reference traces at 200 / 400 / 800 Hz stay on the chart.
- Speed-loop load from 0.5 to 2.5 N m in 5 steps of 0.5. Each step runs 20000 ticks; speed and iq charts share the same slider. The 2.5 N m step exceeds the torque available at the 8 A iq limit (about 2.4 N m), so the speed keeps falling after the load is applied and the readouts flag it as out of range.
- Field-weakening iq from 2 to 8 A in 7 steps of 1 A. Each step gives the id reference and the voltage headroom from 300 to 900 rad/s, plus one comparison trace at iq = 5 A with the id lower bound clamped at -8 A. The base speed is linearly interpolated where the headroom crosses zero.

```
pwsh -File scripts/build-demo.ps1      # build the JS artifact and copy it into docs/demo
python -m http.server 8765             # run from the repo root, then open http://127.0.0.1:8765/docs/demo/
```

The artifact sits next to the page and needs no CDN, so opening `docs/demo/index.html` directly also works. The full grid takes about 127 ms under node, so page load has no perceptible delay. The step counts live in two places - the sweep ranges in `examples/web_demo/main.mbt` and the slider ranges on the page - and both must be changed together. GitHub Pages hosting needs `docs/` to be set as the publishing source in the repository settings; that step is outside the repo.
## Test

CI runs formatting, a wasm / wasm-gc / js / native matrix of strict checks, builds and tests, five native examples (demo, waveform, reject_demo, foc_sim, speed_loop), and release metadata checks. Current working-tree tests pass 181/181 on native, js, wasm-gc and wasm.

```
moon check --deny-warn --target wasm-gc
moon test --target wasm-gc
# local additional targets:
moon check --deny-warn --target wasm
moon test --target wasm
moon check --deny-warn --target native
moon test --target native
moon check --deny-warn --target js
moon test --target js
```

159 cases cover: Clarke balanced three-phase and round-trip, Park at θ=0 and
θ=π/2, Park/inverse-Park round-trip, SVPWM sector detection across all six
sectors, linear and overmodulation timing, a hand-computed absolute baseline for
T1/T2 and the three-phase duties in the linear region, duty range and per-sector
order, angle normalization, 20 independent numeric fixtures, single-axis PI
proportional/integral/clamp/anti-windup, controller chain consistency and
closed-loop step tracking, the simplified motor model's first-order RL
response, pole-pair scaling, zero three-phase sum, and clamping of non-positive
pole pairs and inductance, plus 9 regressions on current-loop design (cancellation
ratio, crossover at target bandwidth, 90° ideal margin, delay loss conversion,
usability judged after delay, bandwidth monotonicity against margin, margin drop
when cancellation is off, invalid-input clamping, zero crossover when the loop
never crosses 0 dB), plus 12 regressions on current sampling (reconstruction closing
the three-phase sum, non-finite inputs clamped to zero, the sensor consistency
check including invalid tolerances, window margin as duty and period vary, invalid
window configuration rejected, sample-pair ranking, no candidate when all phases
are too narrow, the two-phase step matching the three-phase step bit for bit, phase-order restoration from a pair, a malformed pair or bad sample rejected, any pair matching the three-phase step, a bad pair leaving the PI state untouched, plus 7 regressions on overmodulation geometry (boundary equal to the inscribed radius on a sector axis, two thirds of the bus at a vertex, analytic boundary matching the clamped magnitude, projection preserving direction, a linear-region vector returned untouched, and a clipping ratio that is zero inside the linear region and positive beyond), plus 8 regressions on decoupling feedforward (back-EMF equal to ωe·ψf with zero speed and non-finite inputs degrading to zero, zero feedforward at standstill, the signs of the voltage equation including a negative vq shift when id is negative, a linear-region sum returned untouched, a magnitude clamp that keeps the direction, a bus-unknown case that only sums, and non-finite components kept out of the command), plus 5 regressions on the mechanical model (SPMSM and salient-pole torque conversion, torque-driven acceleration and coasting, load pulling the speed down, settling at the Te/B balance with 1e-6 tolerance, invalid step and non-finite inputs clamped, non-negative speed clamp and pole-pair scaling), plus 7 regressions on the speed loop (gains following inertia and bandwidth, zero gains without a torque constant or bandwidth, inertia clamping, the bandwidth ratio check, output sign following the error, limiting and a bad feedback sample producing zero without touching the integrator, and reset clearing state), plus 6 regressions on field weakening (positive headroom below base speed with a zero id, negative headroom above base speed producing headroom/Ld, q-axis current tightening the budget and deepening the weakening, the id clamped to the current floor, an unsolvable budget returning that floor, and zero speed, zero bus, zero inductance or non-finite inputs returning zero).

## Independent numeric fixtures

`coord/coord_test.mbt` and `svpwm/svpwm_test.mbt` each carry 20 fixed cases whose
expected values were computed independently by `scripts/gen_fixtures.py` (plain
Python, textbook formulas), not taken from this library's output. The script
sweeps current amplitudes 1 / 5 / 10 / 20 A and Park angles from 0 to 1.9π, so a
few cases land in the overmodulation region and pin the `T1+T2 = Ts` clamping.

Regenerate the printed block if the formulas ever change:

```
python scripts/gen_fixtures.py
```

The numeric fixtures in this repository are the in-tree oracle, generated by the
Python script above. A MATLAB / Simulink model with the same motor parameters is
optional and not required to reproduce them.

## License

MIT © 2026 Duckweed-yhb

## AI tool usage

This project was built with AI assistance, but the goal, scope, quality boundaries, and
open-source compliance are controlled by the author:

- **AI assisted with**: boilerplate for Clarke / Park / SVPWM functions, test case generation,
  initial README draft.
- **Human controlled**: project selection (motor control numerical kernel), the pure-math
  boundary (no I/O), the differentiation from `hwlxmm/dsp` (SVPWM chain vs instantaneous
  power), the design of the 20 fixture inputs, hand-computed expected values.
- **How AI-generated code is verified**: each AI-generated test is checked against textbook
  formulas; Clarke/Park round-trip tests verify mathematical consistency; SVPWM sector
  detection uses 6 angle points; CI must be green before merge.
- **What is not done**: no code copied from TI C2000 / ST MCSDK (only math steps cross-referenced);
  no self-hosting expected values; no AI-generated test data taken as truth.
