Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·881 lines (746 loc) · 31.3 KB
/
Copy pathmain.py
File metadata and controls
executable file
·881 lines (746 loc) · 31.3 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
#!pyctrl/bin/python
import os
import sys
import time
import matplotlib.pyplot as plt
import numpy as np
sys.path.append(os.getcwd())
from typing import Any, Dict, List, Optional, Type, Union
from numpy.typing import NDArray
import helpers.config as config
from core.analysis import get_stability_margins
from core.control_utils import PIDController
from core.ekf import ExtendedKalmanFilter
from core.estimator import KalmanFilter
from core.state_space import StateSpace
from core.transfer_function import TransferFunction
from helpers.config import BATTERY_KF, BATTERY_PID
from helpers.exit import flush, kill, stop
from helpers.plot import (
plot_analysis_dashboard,
plot_custom_simulation,
plot_estimation_history,
plot_mpc_response,
plot_time_response,
plot_ukf_estimation,
)
from helpers.simulation_runner import (
run_custom_nonlinear_simulation,
run_ekf_simulation,
run_linear_simulation,
run_mpc_simulation,
run_ukf_simulation,
)
from helpers.system_registry import (
SYSTEM_REGISTRY,
SystemDescriptor,
load_available_systems,
)
from modules.interactive_lab import InteractiveLab, pendulum_lqr_controller
class PyControlsApp:
"""
Main Application Controller for the PyControls Engineering Suite.
This class handles the lifecycle of the application, including:
1. User interaction via a CLI menu.
2. Management of system parameters and configurations.
3. Orchestration of various simulation modes (Time-domain, Frequency analysis,
Parameter estimation, Nonlinear estimation, MPC, and Interactive Labs).
"""
def __init__(self) -> None:
"""
Initialize the application, load systems, and set up default parameters.
It attempts to load available systems from the registry. If successful,
it initializes the default system (DC Motor), loads configuration
parameters (PID, LQR, Simulation settings), and prepares the environment.
"""
self.available_systems: Dict[str, Type[Any]] = load_available_systems()
if not self.available_systems:
print("Error: No valid system classes found in systems/ folder.")
sys.exit(1)
self.current_system_id: str = "dc_motor"
self.current_descriptor: SystemDescriptor = SYSTEM_REGISTRY[
self.current_system_id
]
self.system_name: str = self.current_descriptor.display_name.replace(" ", "")
self.SystemClass: Optional[Type[Any]] = self.current_descriptor.system_class
if self.SystemClass is not None:
temp_instance: Any = self.SystemClass()
self.active_params: Dict[str, float] = temp_instance.params.copy()
else:
self.active_params = {}
if self.current_system_id == "dc_motor" and hasattr(config, "MOTOR_PARAMS"):
self.active_params.update(config.MOTOR_PARAMS)
elif self.current_system_id == "pendulum" and hasattr(
config, "PENDULUM_PARAMS"
):
self.active_params.update(config.PENDULUM_PARAMS)
elif self.current_system_id == "thermistor" and hasattr(
config, "THERMISTOR_PARAMS"
):
self.active_params.update(config.THERMISTOR_PARAMS)
elif self.current_system_id == "battery" and hasattr(config, "BATTERY_PARAMS"):
self.active_params.update(config.BATTERY_PARAMS)
self.system: Any = (
self.current_descriptor.system_class()
if self.current_descriptor.system_class is not None
else None
)
self.controllers: List[Dict[str, Any]] = config.CONTROLLERS.copy()
self.sim_params: Dict[str, float] = config.SIM_PARAMS.copy()
self.dist_params: Dict[str, Any] = config.DISTURBANCE_PARAMS.copy()
self.running: bool = True
def clear_screen(self) -> None:
"""
Clears the terminal screen using OS-specific commands.
Uses 'cls' for Windows (nt) and 'clear' for Unix-like systems.
"""
os.system("cls" if os.name == "nt" else "clear")
def print_header(self) -> None:
"""
Prints the application banner and current system active parameters.
Truncates the parameter string if it exceeds display width to maintain layout.
"""
print("\n" + "=" * 60)
print(f" PyControls Engineering Suite | System: {self.system_name} ")
print("=" * 60)
param_list: List[str] = [f"{k}={v}" for k, v in self.active_params.items()]
param_str: str = ", ".join(param_list)
if len(param_str) > 55:
print(f"Params: {param_str[:55]}...")
else:
print(f"Params: {param_str}")
print("-" * 60)
def main_menu(self) -> None:
"""
Displays the main menu loop and handles user input routing.
Provides access to:
- Standard Simulations
- System Analysis (Bode/Poles)
- Parameter Editing
- Advanced Demos (EKF, UKF, MPC)
- Interactive Labs
"""
self.clear_screen()
while self.running:
self.print_header()
print("[1] Run Standard Simulation (Time-Domain Response)")
print("[2] Run System Analysis (Frequency, Poles, Phase Plane)")
print("[3] Edit System Parameters")
print("[4] Edit Disturbance Settings")
print("[5] Switch Active System")
print("[6] Run Parameter Estimation Demo (EKF)")
print("[7] Run Nonlinear State Est. Demo (UKF)")
print("[8] Run Model Predictive Control Demo (MPC)")
print("[9] Interactive Lab (Supports Hardware HIL)")
print("[10] Run Custom Non-Linear Simulation")
print("[q] Exit")
choice: str = input("\nSelect Option: ").strip()
if choice == "1":
self.run_preset_dashboard()
elif choice == "2":
self.run_analysis_dashboard()
elif choice == "3":
self.edit_params_menu()
elif choice == "4":
self.edit_disturbance_menu()
elif choice == "5":
self.switch_system_menu()
elif choice == "6":
self.run_ekf()
elif choice == "7":
self.run_ukf()
elif choice == "8":
self.run_mpc()
elif choice == "9":
self.run_interactive_lab()
elif choice == "10":
self.run_custom_simulation()
elif choice == "q":
self.running = False
else:
input("Invalid option. Press Enter...")
def run_preset_dashboard(self) -> None:
"""
Executes Option 1: Time-Domain Response Dashboard.
Instantiates the current system and loops through defined controllers
(e.g., PID presets or LQR) to generate comparative plots.
Calculates and displays Step Response Metrics (Rise Time, Overshoot, Settling Time)
in the terminal for each controller.
Visualizations:
- 2x2 Subplot grid.
- Motor: Speed, Current, Voltage, Estimated Disturbance (vs True Load).
- Pendulum: Angle, Cart Position, Force, Angular Velocity.
"""
if not self.current_descriptor.supports_analysis:
print("\nAnalysis & metrics are only available for linear systems.")
input("Press Enter to return to menu...")
return
print(f"\nSimulating Time Response for {self.system_name}...")
try:
assert self.SystemClass is not None
current_system: Any = self.SystemClass(**self.active_params)
except Exception as e:
print(f"Error instantiating {self.system_name}: {e}")
return
if self.current_system_id == "dc_motor":
labels: List[str] = [
"Speed (rad/s)",
"Current (A)",
"Voltage (V)",
"Est. Disturbance (Nm)",
]
indices: List[int] = [0, 1]
loop_controllers: List[Dict[str, Any]] = self.controllers
else:
labels = [
"Angle (rad)",
"Cart Pos (m)",
"Force (N)",
"Ang. Velocity (rad/s)",
]
indices = [2, 0]
loop_controllers = [{"name": "LQR Stabilizer", "color": "k"}]
y_real_hist: Dict[str, NDArray[np.float64]] = {}
x_est_hist: Dict[str, NDArray[np.float64]] = {}
u_hist_map: Dict[str, NDArray[np.float64]] = {}
t = np.zeros(0)
for ctrl in loop_controllers:
t: NDArray[np.float64]
y_real: NDArray[np.float64]
x_est: NDArray[np.float64]
u_hist: NDArray[np.float64]
ctrl: Dict[str, Any]
t, y_real, x_est, u_hist = run_linear_simulation(
current_system,
self.current_system_id,
ctrl,
self.sim_params,
self.dist_params,
)
if len(y_real) > 0:
y_real_hist[ctrl["name"]] = y_real
x_est_hist[ctrl["name"]] = x_est
u_hist_map[ctrl["name"]] = u_hist
plot_time_response(
t,
y_real_hist,
x_est_hist,
u_hist_map,
labels,
indices,
loop_controllers,
self.current_system_id,
self.dist_params,
)
def run_analysis_dashboard(self) -> None:
"""
Executes Option 2: System Analysis.
Performs linear analysis on the State Space model.
Calculates and displays Stability Margins (Gain Margin, Phase Margin)
in the terminal.
Visualizations:
1. Bode Plot: Frequency response from Input -> Primary Output (Speed/Angle).
2. Pole-Zero Map: Visualizes system stability in the S-Plane.
3. Kalman Estimate Error: Difference between True State and Estimated State.
4. Phase Plane Trajectory:
- Pendulum: Theta vs Theta_dot.
- Motor: Speed vs Current.
"""
if not self.current_descriptor.supports_analysis:
print("\nAnalysis & metrics are only available for linear systems.")
input("Press Enter to return to menu...")
return
print(f"\nGenerating Analysis for {self.system_name}...")
try:
assert self.SystemClass is not None
current_system: Any = self.SystemClass(**self.active_params)
ss: StateSpace = current_system.get_state_space()
except Exception:
print("System does not support state-space analysis.")
return
print("-" * 60)
tf_ol: Optional[Union["TransferFunction", Any]] = None
ctrl_name: str = "Unknown"
if self.current_system_id == "dc_motor":
print("Stability Analysis (Loop Gain with Default PID)")
pid_cfg: Dict[str, Any] = self.controllers[2]
ctrl_name = pid_cfg["name"]
try:
tf_ol = current_system.get_open_loop_tf(
pid_cfg["Kp"], pid_cfg["Ki"], pid_cfg["Kd"]
)
except Exception as e:
print(f"Could not construct TF: {e}")
elif self.current_system_id == "pendulum":
print("Stability Analysis (LQR Loop Gain)")
print("Note: Margins represent robustness at the plant input.")
ctrl_name = "LQR"
try:
K_lqr: NDArray[np.float64] = current_system.dlqr_gain()
tf_ol = current_system.get_open_loop_tf(K_lqr)
except Exception as e:
print(f"Could not construct TF: {e}")
if tf_ol is not None:
try:
gm, pm, w_pc, w_gc = get_stability_margins(tf_ol)
print(f"Controller: {ctrl_name}")
print(f" Gain Margin: {gm:.2f} dB")
print(f" Phase Margin: {pm:.2f} deg")
print(f" Phase Crossover: {w_pc:.2f} rad/s")
print(f" Gain Crossover: {w_gc:.2f} rad/s")
except Exception as e:
print(f"Could not calculate margins: {e}")
else:
print("Margin analysis unavailable for this configuration.")
print("-" * 60)
w: NDArray[np.float64] = np.logspace(*config.PLOT_PARAMS["bode_range"])
out_idx: int = 2 if self.current_system_id == "pendulum" else 0
mags, phases = ss.get_frequency_response(w, input_idx=0, output_idx=out_idx)
t, y_real, x_est, _ = run_linear_simulation(
current_system,
self.current_system_id,
self.controllers[1],
self.sim_params,
self.dist_params,
)
plot_analysis_dashboard(
ss,
w,
mags,
phases,
t,
y_real,
x_est,
self.current_system_id,
)
def edit_params_menu(self) -> None:
"""
Provides an interactive CLI to edit the physical parameters of the current system.
Updates self.active_params in real-time.
"""
print(f"\nCurrent Parameters for {self.system_name}:")
for k, v in self.active_params.items():
print(f" [{k}] : {v}")
key: str = input("\nEnter parameter key to edit (or 'b' to go back): ").strip()
if key == "b":
return
if key in self.active_params:
try:
val: float = float(input(f"New value for {key}: "))
self.active_params[key] = val
except ValueError:
print("Invalid number.")
else:
print("Unknown parameter.")
def edit_disturbance_menu(self) -> None:
"""
Provides a menu to configure external disturbance injection.
Allows toggling the disturbance on/off and setting magnitude/start time.
"""
print(f"\n[1] Toggle Enable (Current: {self.dist_params['enabled']})")
print(f"[2] Set Magnitude (Current: {self.dist_params['magnitude']})")
print(f"[3] Set Time (Current: {self.dist_params['time']})")
print("[b] Back")
choice: str = input("\nChoice: ").strip()
if choice == "b":
return
if choice == "1":
self.dist_params["enabled"] = not self.dist_params["enabled"]
elif choice == "2":
try:
self.dist_params["magnitude"] = float(input("New Magnitude: "))
except Exception as e:
print("Error occurred!", e, sep="\n")
elif choice == "3":
try:
self.dist_params["time"] = float(input("New Time: "))
except Exception as e:
print("Error occurred!", e, sep="\n")
def switch_system_menu(self) -> None:
"""
Displays a menu to switch between available dynamic systems (e.g., DC Motor, Pendulum).
Updates the internal state (current_system_id, active_params, SystemClass)
based on the user's selection from the SYSTEM_REGISTRY.
"""
self.clear_screen()
print("\nAvailable Systems:")
system_ids: List[str] = list(SYSTEM_REGISTRY.keys())
for i, sys_id in enumerate(system_ids):
desc: SystemDescriptor = SYSTEM_REGISTRY[sys_id]
print(f"[{i + 1}] {desc.display_name}")
print("[b] Back")
sel: str = input("\nSelect System: ").strip()
if sel == "b":
return
try:
idx: int = int(sel) - 1
if 0 <= idx < len(system_ids):
new_id: str = system_ids[idx]
if new_id != self.current_system_id:
self.current_system_id = new_id
self.current_descriptor = SYSTEM_REGISTRY[new_id]
self.SystemClass = self.current_descriptor.system_class
self.system_name = self.current_descriptor.display_name.replace(
" ", ""
)
if self.SystemClass is not None:
self.system = self.SystemClass()
self.active_params = self.SystemClass().params.copy()
if new_id == "dc_motor" and hasattr(config, "MOTOR_PARAMS"):
self.active_params.update(config.MOTOR_PARAMS)
elif new_id == "pendulum" and hasattr(
config, "PENDULUM_PARAMS"
):
self.active_params.update(config.PENDULUM_PARAMS)
elif new_id == "thermistor" and hasattr(
config, "THERMISTOR_PARAMS"
):
self.active_params.update(config.THERMISTOR_PARAMS)
elif new_id == "battery" and hasattr(config, "BATTERY_PARAMS"):
self.active_params.update(config.BATTERY_PARAMS)
self.system.params = self.active_params.copy()
else:
self.system = None
self.active_params = {}
print(f"\nSwitched to {self.current_descriptor.display_name}")
time.sleep(1)
self.clear_screen()
except ValueError:
print("Invalid option!")
def run_ekf(self) -> None:
"""
Runs the Extended Kalman Filter (EKF) demo for Joint State & Parameter Estimation.
This demo augments the state vector to include system parameters (unknowns)
and estimates them over time using the EKF.
Configurations:
1. DC Motor:
- Estimates: Inertia (J) and Friction (b).
- State Vector: [Speed, Current, log(J), log(b)].
- Measurements: [Speed, Current].
2. Pendulum:
- Estimates: Mass (m) and Length (l).
- State Vector: [x, v, theta, omega, log(m), log(l)].
- Measurements: [x, theta] (Positions only).
The simulation creates a 'True' system with predefined parameters and an
'Estimator' initialized with guesses. It plots the convergence of the
parameters over time.
"""
if not self.current_descriptor.supports_estimation:
print("\nEstimation is not supported for this system.")
input("Press Enter to return to menu...")
return
print(f"\n--- Parameter Estimation Demo ({self.system_name}) ---")
est_cfg = {}
param_names = []
plot_labels = []
if self.current_system_id == "dc_motor":
est_cfg: Dict[str, Any] = config.MOTOR_ESTIMATION_PARAMS
param_names: List[str] = ["Inertia (J)", "Friction (b)"]
plot_labels: List[str] = ["Speed (rad/s)", "Current (A)"]
elif self.current_system_id == "pendulum":
est_cfg = config.PENDULUM_ESTIMATION_PARAMS
param_names = ["Mass (m)", "Length (l)"]
plot_labels = ["Position (m)", "Angle (rad)"]
assert self.SystemClass is not None
t_vals, history, true_params, param_keys = run_ekf_simulation(
self.SystemClass,
self.current_system_id,
est_cfg,
)
true_vals_list: List[float] = [true_params[k] for k in param_keys]
print("Simulating...")
plot_estimation_history(
t_vals, history, plot_labels, true_vals_list, param_names
)
def run_ukf(self) -> None:
"""
Runs the Unscented Kalman Filter (UKF) demo for Non-linear State Estimation.
This demo uses the Sigma-Point transform to handle non-linearities in dynamics
or measurement models.
Scenarios:
1. DC Motor: Sine wave input to test estimation through zero-crossings (stiction).
- Measures both Speed and Current.
2. Pendulum: Free swing dynamics.
- Measures Angle, but estimates Angular Velocity via the filter.
Plots comparison between True State, Noisy Measurements, and UKF Estimate.
"""
if not hasattr(self.system, "get_nonlinear_dynamics"):
print(f"\nUKF is not supported/implemented for {self.system_name}.")
input("Press Enter to return to menu...")
return
if self.system:
self.system.params = self.active_params.copy()
print(f"\n--- Non-Linear State Estimation (UKF) - {self.system_name} ---")
if self.current_system_id == "pendulum":
cfg: Dict[str, Any] = config.UKF_PENDULUM_PARAMS
labels: List[str] = ["Angle (rad)", "Velocity (rad/s)"]
elif self.current_system_id == "dc_motor":
cfg = config.UKF_MOTOR_PARAMS
labels = ["Speed (rad/s)", "Current (A)"]
else:
print("Unknown system config.")
return
t_vals, true_states, est_states, measurements = run_ukf_simulation(
self.system,
self.current_system_id,
cfg,
)
plot_ukf_estimation(
t_vals,
true_states,
est_states,
measurements,
labels,
)
def run_mpc(self) -> None:
"""
Runs the Model Predictive Control (MPC) demo.
Demonstrates optimal control over a finite horizon.
Algorithms:
1. DC Motor: Uses Linear MPC (ADMM solver).
- Goal: Track a target speed while minimizing voltage spikes.
2. Pendulum: Uses Nonlinear MPC (iLQR solver).
- Goal: Swing up from the Down position (Pi) to the Up position (0).
Plots the system output against the reference, showing the optimized control inputs.
"""
if not hasattr(self.system, "get_mpc_model"):
print(f"\nMPC is not supported for {self.system_name}.")
input("Press Enter to return to menu...")
return
if self.system:
self.system.params = self.active_params.copy()
print(f"\n--- Model Predictive Control (MPC) - {self.system_name} ---")
if self.current_system_id == "dc_motor":
plot_labels: List[str] = ["Speed (rad/s)", "Voltage (V)"]
cfg: Dict[str, Any] = config.MPC_MOTOR_PARAMS
elif self.current_system_id == "pendulum":
plot_labels = ["Angle (rad)", "Force (N)"]
cfg = config.MPC_PENDULUM_PARAMS
else:
return
t_vals, x_hist, u_hist, ref, plot_idx = run_mpc_simulation(
self.system,
self.current_system_id,
cfg,
)
plot_mpc_response(
t_vals,
x_hist,
u_hist,
ref,
plot_labels,
plot_idx,
self.current_system_id,
cfg,
)
def run_interactive_lab(self) -> None:
"""
Runs the Interactive Laboratory mode.
This mode provides a real-time (or quasi-real-time) simulation loop where
users can interact with the system or observe automatic controllers.
Features:
- DC Motor: Manual control or Auto-PID speed control.
- Pendulum: Manual control or LQR stabilization (if supported).
- Visualization: Updates a textual UI with current state values (headless mode).
- Estimator: Can run an EKF in parallel if configured.
"""
if not self.current_descriptor.supports_interactive_lab:
print("\nInteractive Lab not supported for this system.")
input("Press Enter to continue...")
return
params: Dict[str, float] = self.active_params.copy()
lab = InteractiveLab(
system_descriptor=self.current_descriptor,
params=params,
dt=0.01,
)
lab.initialize()
lab.init_visualization()
if self.current_system_id == "dc_motor":
omega_ref: float = (
params["omega_ref"]
if "omega_ref" in params
else config.INTERACTIVE_LAB_PARAMS["omega_ref"]
)
target_config: Dict[str, Any] = config.CONTROLLERS[2]
print(f"\n[INFO] Loaded Controller: {target_config['name']}")
print(
f" Gains: Kp={target_config['Kp']}, Ki={target_config['Ki']}, Kd={target_config['Kd']}"
)
pid = PIDController(
Kp=target_config["Kp"],
Ki=target_config["Ki"],
Kd=target_config["Kd"],
derivative_on_measurement=True,
output_limits=(None, None),
)
last_t: float = 0.0
def controller_wrapper(state: Any, t: float) -> float:
nonlocal last_t
dt: float = t - last_t
if dt <= 0:
dt = config.INTERACTIVE_LAB_PARAMS["controller_min_dt"]
last_t = t
return pid.update(measurement=state[0], setpoint=omega_ref, dt=dt)
lab.set_auto_controller(controller_wrapper)
elif self.current_system_id == "pendulum":
assert self.SystemClass is not None
current_sys_instance: Any = self.SystemClass(**self.active_params)
if hasattr(current_sys_instance, "dlqr_gain"):
K: NDArray[np.float64] = current_sys_instance.dlqr_gain()
lab.set_auto_controller(pendulum_lqr_controller(K))
else:
print("[WARN] System does not support LQR. Auto-control disabled.")
x0: NDArray[np.float64] = np.array(config.INTERACTIVE_LAB_PARAMS["ekf_x0"])
Q: NDArray[np.float64] = np.diag(
config.INTERACTIVE_LAB_PARAMS["ekf_Q_diag"]
)
R: NDArray[np.float64] = np.diag(
config.INTERACTIVE_LAB_PARAMS["ekf_R_diag"]
)
if hasattr(current_sys_instance, "dynamics_continuous") and hasattr(
current_sys_instance, "measurement"
):
ekf = ExtendedKalmanFilter(
f_dynamics=current_sys_instance.dynamics_continuous,
h_measurement=current_sys_instance.measurement,
Q=Q,
R=R,
x0=x0,
)
lab.set_estimator(
ekf, measurement_func=current_sys_instance.measurement
)
elif self.current_system_id == "battery":
pid = PIDController(
Kp=BATTERY_PID["Kp"],
Ki=BATTERY_PID["Ki"],
Kd=BATTERY_PID["Kd"],
output_limits=(0, 255),
)
ss: StateSpace = self.system.get_state_space()
phi: NDArray[np.float64] = np.array([[np.exp(ss.A[0, 0] * lab.dt)]])
gamma: NDArray[np.float64] = (phi - 1) * (1 / ss.A[0, 0]) * ss.B
kf = KalmanFilter(
phi,
gamma,
ss.C,
Q=np.array([BATTERY_KF["Q"]]),
R=np.array([BATTERY_KF["R"]]),
x0=[0.0],
)
lab.set_estimator(kf)
def battery_ctrl(state_est: NDArray[np.float64], t: float) -> float:
u_raw: float = pid.update(
state_est[0], self.active_params["setpoint"], lab.dt
)
pwm: float = np.clip(u_raw, 0, 255)
return pwm
lab.set_auto_controller(battery_ctrl)
print("\nInteractive Lab running (headless mode).")
print(f"Control mode: {lab.control_mode}")
print("Press Ctrl+C to stop.\n")
try:
if lab.control_mode == "MANUAL":
print("\nControls:")
print(" a/d : decrease/increase input")
print(" s : zero input")
print(" m : manual mode")
print(" o : auto mode")
print(" q : quit\n")
else:
print("\nAUTO mode enabled.")
print("Press Ctrl+C to stop or wait for completion.\n")
while lab.running and lab.status == "RUNNING":
lab.step()
lab.update_visualization()
actual_u: float = lab.last_u
omega: float
current: float
theta: float
x_pos: float
T: float
v_est: float
v_avg: float
if lab.descriptor.system_id == "dc_motor":
assert lab.state is not None
omega, current = lab.state
print(
f"\rω={omega:.2f}, i={current:.2f}, u={actual_u:.2f}",
end="",
)
elif lab.descriptor.system_id == "pendulum":
assert lab.state is not None
theta = lab.state[2]
x_pos = lab.state[0]
print(
f"\rθ={theta:.3f}, x={x_pos:.3f}, u={actual_u:.2f}",
end="",
)
elif lab.descriptor.system_id == "thermistor":
assert lab.state is not None
T = lab.state[0]
u: float = actual_u
print(f"\rT={T:.2f}°C, u={u:.0f}/255", end="")
elif lab.descriptor.system_id == "battery":
assert lab.state_est is not None
v_est = lab.state_est[0]
u = lab.last_u
v_avg = lab.last_avg
print(
f"\rV_est: {v_est:.2f}V | PWM: {u:.0f} | V_avg: {v_avg:.2f}",
end="",
)
time.sleep(lab.dt if not lab.descriptor.is_hardware else lab.dt * 2)
plt.ioff()
plt.show()
print(f"\nLab finished with status: {lab.status}")
if lab.failure_reason:
print(f"Reason: {lab.failure_reason}")
except KeyboardInterrupt:
pass
except EOFError:
print("\n\nClosing the terminal...")
time.sleep(0.1)
kill()
input("\nPress Enter to return to menu...")
def run_custom_simulation(self) -> None:
"""
Runs an adaptive step-size solver (RK45) on user-defined non-linear equations.
Prompts the user for a differential equation string (e.g., "sin(t) - x").
Uses `make_system_func` to parse the string into a callable and solves it
using the NonlinearSolver with adaptive time stepping.
"""
print("\n--- Custom Non-Linear Simulation (Adaptive RK45) ---")
eqn: str = input("Enter dx/dt = f(t, x, u): ").strip()
try:
t_vals, y_vals = run_custom_nonlinear_simulation(
eqn,
config.CUSTOM_SIM_PARAMS,
)
plot_custom_simulation(
t_vals,
y_vals,
eqn,
)
except Exception as e:
print(f"\nError: {e}")
if __name__ == "__main__":
app = PyControlsApp()
KILLED: bool = False
try:
app.main_menu()
except KeyboardInterrupt:
print("\n\nExiting...")
time.sleep(0.5)
except EOFError:
print("\n\nClosing the terminal...")
time.sleep(0.2)
KILLED = True
finally:
flush()
if KILLED:
kill()
else:
app.clear_screen()
stop()