1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use crate::core::scalar::ControlScalar;
use crate::core::traits::Plant;
/// Simplified DC motor electrical + mechanical simulation.
///
/// State: [i (A), ω (rad/s)]
/// di/dt = (V - R*i - Ke*ω) / L
/// dω/dt = (Kt*i - B*ω - τ_load) / J
#[derive(Debug, Clone, Copy)]
pub struct DcMotorSim<S: ControlScalar> {
/// Armature resistance (Ω).
pub r: S,
/// Armature inductance (H).
pub l: S,
/// Back-EMF constant (V·s/rad).
pub k_e: S,
/// Torque constant (N·m/A).
pub k_t: S,
/// Rotor inertia (kg·m²).
pub j: S,
/// Viscous friction (N·m·s/rad).
pub b: S,
/// Armature current (A).
current: S,
/// Angular velocity (rad/s).
omega: S,
/// Angular position (rad).
theta: S,
/// External load torque (N·m).
tau_load: S,
}
impl<S: ControlScalar> DcMotorSim<S> {
/// Create a DC motor simulation.
pub fn new(r: S, l: S, k_e: S, k_t: S, j: S, b: S) -> Self {
Self {
r,
l,
k_e,
k_t,
j,
b,
current: S::ZERO,
omega: S::ZERO,
theta: S::ZERO,
tau_load: S::ZERO,
}
}
/// Small DC motor (100W class): R=1Ω, L=1mH, Ke=0.05, Kt=0.05, J=0.001, B=0.001
pub fn small() -> Self {
Self::new(
S::from_f64(1.0),
S::from_f64(0.001),
S::from_f64(0.05),
S::from_f64(0.05),
S::from_f64(0.001),
S::from_f64(0.001),
)
}
/// Apply voltage input and advance simulation by `dt`.
pub fn step_voltage(&mut self, v: S, dt: S) {
let di_dt = (v - self.r * self.current - self.k_e * self.omega) / self.l;
let domega_dt = (self.k_t * self.current - self.b * self.omega - self.tau_load) / self.j;
self.current += di_dt * dt;
self.omega += domega_dt * dt;
self.theta += self.omega * dt;
}
pub fn set_load_torque(&mut self, tau: S) {
self.tau_load = tau;
}
pub fn current(&self) -> S {
self.current
}
pub fn omega(&self) -> S {
self.omega
}
pub fn theta(&self) -> S {
self.theta
}
pub fn reset(&mut self) {
self.current = S::ZERO;
self.omega = S::ZERO;
self.theta = S::ZERO;
self.tau_load = S::ZERO;
}
}
impl<S: ControlScalar> Plant<S> for DcMotorSim<S> {
fn step(&mut self, input: S, dt: S) {
self.step_voltage(input, dt);
}
fn output(&self) -> S {
self.omega // Output is speed
}
fn state(&self) -> &[S] {
// Return omega as the primary state (current is internal)
core::slice::from_ref(&self.omega)
}
}
/// PMSM dq-frame simulation (reuses motor model from `motor/model/pmsm.rs` concept).
///
/// Minimal standalone version for simulation purposes.
/// Full model is in `motor::model::pmsm::PmsmModel`.
#[derive(Debug, Clone, Copy)]
pub struct PmsmSim<S: ControlScalar> {
/// d-axis inductance (H).
pub l_d: S,
/// q-axis inductance (H).
pub l_q: S,
/// Stator resistance (Ω).
pub r_s: S,
/// Permanent magnet flux linkage (Wb).
pub psi_f: S,
/// Number of pole pairs.
pub n_p: S,
/// Rotor inertia (kg·m²).
pub j: S,
/// Viscous friction (N·m·s/rad).
pub b: S,
// State
id: S,
iq: S,
omega_m: S, // mechanical speed (rad/s)
theta_e: S, // electrical angle (rad)
tau_load: S,
}
impl<S: ControlScalar> PmsmSim<S> {
pub fn new(l_d: S, l_q: S, r_s: S, psi_f: S, n_p: S, j: S, b: S) -> Self {
Self {
l_d,
l_q,
r_s,
psi_f,
n_p,
j,
b,
id: S::ZERO,
iq: S::ZERO,
omega_m: S::ZERO,
theta_e: S::ZERO,
tau_load: S::ZERO,
}
}
/// Small PMSM (100W class).
pub fn small() -> Self {
Self::new(
S::from_f64(0.001), // Ld
S::from_f64(0.002), // Lq
S::from_f64(0.5), // Rs
S::from_f64(0.05), // ψf
S::from_f64(2.0), // pole pairs
S::from_f64(0.001), // J
S::from_f64(0.0001), // B
)
}
/// Advance dq-frame dynamics by dt using Euler integration.
pub fn step_dq(&mut self, v_d: S, v_q: S, dt: S) {
let omega_e = self.omega_m * self.n_p;
let did_dt = (v_d - self.r_s * self.id + omega_e * self.l_q * self.iq) / self.l_d;
let diq_dt =
(v_q - self.r_s * self.iq - omega_e * (self.l_d * self.id + self.psi_f)) / self.l_q;
let torque = S::from_f64(1.5)
* self.n_p
* (self.psi_f * self.iq + (self.l_d - self.l_q) * self.id * self.iq);
let domega_dt = (torque - self.b * self.omega_m - self.tau_load) / self.j;
self.id += did_dt * dt;
self.iq += diq_dt * dt;
self.omega_m += domega_dt * dt;
self.theta_e += omega_e * dt;
// Wrap electrical angle to [0, 2π)
let two_pi = S::TWO * S::PI;
while self.theta_e >= two_pi {
self.theta_e -= two_pi;
}
while self.theta_e < S::ZERO {
self.theta_e += two_pi;
}
}
pub fn id(&self) -> S {
self.id
}
pub fn iq(&self) -> S {
self.iq
}
pub fn omega_m(&self) -> S {
self.omega_m
}
pub fn theta_e(&self) -> S {
self.theta_e
}
pub fn torque(&self) -> S {
S::from_f64(1.5)
* self.n_p
* (self.psi_f * self.iq + (self.l_d - self.l_q) * self.id * self.iq)
}
pub fn set_load_torque(&mut self, tau: S) {
self.tau_load = tau;
}
pub fn reset(&mut self) {
self.id = S::ZERO;
self.iq = S::ZERO;
self.omega_m = S::ZERO;
self.theta_e = S::ZERO;
self.tau_load = S::ZERO;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dc_motor_spins_up_under_voltage() {
let mut motor = DcMotorSim::small();
let dt = 0.0001;
for _ in 0..10000 {
motor.step_voltage(12.0, dt); // Apply 12V
}
// Motor should be spinning
assert!(motor.omega() > 10.0, "ω={:.2}", motor.omega());
}
#[test]
fn dc_motor_zero_voltage_decelerates() {
let mut motor = DcMotorSim::small();
let dt = 0.0001;
// Spin up
for _ in 0..10000 {
motor.step_voltage(12.0, dt);
}
let omega_init = motor.omega();
// Coast down
for _ in 0..10000 {
motor.step_voltage(0.0, dt);
}
assert!(motor.omega() < omega_init * 0.9, "Should decelerate");
}
#[test]
fn pmsm_iq_drive_generates_torque() {
let mut pmsm = PmsmSim::small();
let dt = 0.0001;
// Apply Vq for a short time (100ms = 1000 steps) before back-EMF reduces Iq.
// At t=0 and low speed, Iq rises to Vq/Rs = 10A transiently.
// Check Iq > 0.01A (positive torque direction) and omega is positive.
for _ in 0..500 {
pmsm.step_dq(0.0, 5.0, dt);
}
assert!(pmsm.iq() > 0.01, "Iq={:.3}", pmsm.iq());
assert!(pmsm.omega_m() > 0.0, "ω={:.3}", pmsm.omega_m());
}
#[test]
fn dc_motor_implements_plant_trait() {
let mut motor = DcMotorSim::<f64>::small();
motor.step(12.0, 0.001);
assert!(motor.output() >= 0.0);
}
}