libstp
Robotics library for Botball
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstdint>
5#include <string>
6#include <vector>
7
8#include "foundation/motor.hpp" // PidGains, Feedforward
9
10namespace libstp::autotune
11{
19 {
21 double max_velocity{0.0};
23 double acceleration{0.0};
25 double deceleration{0.0};
26 };
27
32 {
34 int power_percent{100};
36 int trials{3};
38 double accel_timeout{3.0};
40 double decel_timeout{3.0};
42 int sample_hz{500};
43 };
44
45 // ========================================================================
46 // Phase 2 — VelocityTuner types
47 // ========================================================================
48
56 {
57 std::vector<double> times;
58 std::vector<double> commanded;
59 std::vector<double> measured;
60 };
61
67 {
68 double Ks{0.0};
69 double Tu{0.0};
70 double Tg{0.0};
71 std::string method;
73 };
74
101
106 {
108 double step_duration_s{2.0};
110 double step_command_frac{0.50};
112 int sample_hz{200};
113
114 // CHR gain scaling factors.
115 double chr_kp_scale{0.3};
116 double chr_ki_scale{0.6};
117 double chr_kd_scale{0.15};
118
119 // Rejection thresholds.
121 double min_response_frac{0.10};
126
127 // ---- MCU-chassis velocity-command-gain calibration ----
129 double gain_min{0.3};
130 double gain_max{3.0};
134 double steady_state_frac{0.5};
135 };
136
137 // ========================================================================
138 // Phase 3 — MotionTuner types
139 // ========================================================================
140
145 {
146 std::string param_name;
147 double initial_kp{0.0};
148 double initial_kd{0.0};
149 double final_kp{0.0};
150 double final_kd{0.0};
151 double initial_score{0.0};
152 double final_score{0.0};
154 };
155
160 {
161 int sample_hz{100};
162 double settle_s{0.5};
164 double initial_delta_frac{0.25};
165 double min_delta{0.01};
166 double delta_shrink{0.5};
167
168 // Trial parameters.
169 // Every linear trial drives a FIXED distance (not speed-derived) so the
170 // robot reliably uses the ~1 m runway the operator cleared. Forward to
171 // linear_test_distance_m (recorded for scoring), then a closed return to
172 // the start; repeat. Decoupled from max_velocity on purpose — a 0.2 m/s
173 // chassis would otherwise only ever test ~0.2 m and never characterise
174 // settle/overshoot at a realistic distance.
176 double turn_test_angle_deg{90.0};
177 double motion_timeout_s{10.0};
178 double min_timeout_s{4.0};
179 double stuck_timeout_s{1.5};
185
186 // Scoring weights.
192
193 // Soft limits for constraint-aware scoring.
196 double turn_overshoot_soft_rad{0.05236};
202 };
203
204 // ========================================================================
205 // Phase 4 — FirmwarePidTuner types
206 // ========================================================================
207
216 {
221 double step_fraction{0.5};
225 double step_duration_s{5.0};
226 double decel_duration_s{1.0};
227 int sample_hz{200};
230 double chr_kp_scale{0.6};
231 double chr_ki_scale{0.6};
232 double chr_kd_scale{0.3};
233 double min_response_frac{0.10};
247 std::string csv_dir{};
248
249 // --------------------------------------------------------------------
250 // ControlTheory differential-evolution gain computation.
251 //
252 // When use_control_theory is true the per-motor candidate gains are
253 // computed by (1) fitting a PT1 plant to the raw-PWM step response via
254 // DE system-identification, then (2) DE-optimizing physical firmware
255 // kp/ki/kd against the simulated closed loop using the dt-EXPLICIT
256 // ControlElementPID (which mirrors STM32 pid.c). The CHR heuristic is
257 // kept only as a fallback when the DE fit fails.
258 //
259 // Scales:
260 // * plant K is BEMF-units-per-%PWM (tens-to-hundreds for the wombat
261 // motors at ~70% PWM), hence the large de_plant_max default.
262 // * gains are physical firmware kp/ki/kd (per-second for ki/kd).
263 // * de_max_overshoot is in output BEMF units.
264 // * de_sim_horizon_s sets the internal sim dt = horizon / 5000
265 // (default 5 s -> 1 ms). With the dt-explicit PID the optimized
266 // gains are physical, so the sim dt need not equal the firmware
267 // rate; the horizon only needs to cover settling.
268 // --------------------------------------------------------------------
271 double de_plant_min{0.0001};
272 double de_plant_max{5000.0};
274 double de_gain_max{10.0};
275 double de_sim_horizon_s{5.0};
276 double de_max_overshoot{50.0};
277 double de_weight_ctrl{0.0};
279 // Actuator saturation modeled in the closed-loop sim: the simulated PID
280 // output and integral term are clamped to +/-de_output_max, matching the
281 // firmware MOTOR_MAX_DUTYCYCLE (399). Without this the unbounded sim
282 // output produces fictional overshoot and DE picks uselessly small gains.
283 double de_output_max{399.0};
284 std::uint32_t de_seed{0xC0FFEEu};
285 };
286
291 {
292 int motor_port{-1};
294 float kp{0.0f};
295 float ki{0.0f};
296 float kd{0.0f};
297 double baseline_ise{0.0};
298 double tuned_ise{0.0};
299 bool accepted{false};
302 std::string pid_method{"chr"};
304 double plant_K{0.0};
306 double plant_T{0.0};
307 };
308
309 // ========================================================================
310 // Phase 5 — StaticFrictionMeasurer types
311 // ========================================================================
312
317 {
318 int start_pct{2};
319 int max_pct{50};
320 int step_pct{1};
321 int dwell_ms{50};
324 };
325
330 {
331 int pwm_pct{0};
332 int median_bemf{0};
333 };
334
339 {
340 int motor_port{-1};
344 bool measured{false};
346 std::vector<StaticFrictionSample> forward_sweep{};
348 std::vector<StaticFrictionSample> reverse_sweep{};
351 };
352
353 // ========================================================================
354 // Phase 6 — VelLpfTuner types
355 // ========================================================================
356
361 {
362 double alpha_min{0.05};
363 double alpha_max{0.95};
364 double alpha_step{0.05};
366 int sample_hz{200};
367 double noise_weight{1.0};
368 double lag_weight{0.5};
374 double settle_s{0.5};
375 };
376
382 {
383 double alpha{0.0};
384 double variance{0.0};
385 double lag_change_rate{0.0};
386 double score{0.0};
387 };
388
393 {
394 int motor_port{-1};
395 double initial_alpha{0.5};
396 double tuned_alpha{0.5};
397 double min_score{0.0};
398 bool applied{false};
402 std::vector<int> raw_bemf{};
404 std::vector<VelLpfSweepPoint> sweep{};
405 };
406
407 // ========================================================================
408 // Phase 7 — ToleranceDeriver types
409 // ========================================================================
410
415 {
416 double margin_factor{1.5};
417 // Floor kept at 2 mm so tolerance autotune can't clamp the completion
418 // band back above the uniform 2 mm target (motion_config.hpp /
419 // executor DISTANCE_TOL_M).
424 };
425
431 {
434 bool distance_updated{false};
435 bool angle_updated{false};
436 };
437
438 // ========================================================================
439 // Phase 8 — EncoderCalibrator types
440 // ========================================================================
441
446 {
448 double total_angle_rad{4.0 * 3.14159265358979};
449 double settle_s{0.5};
450 int sample_hz{100};
451 double min_required_angle_rad{3.14159};
452 };
453
462 {
463 std::array<double, 4> ticks_to_rad{};
464 std::array<double, 4> scale_factors{};
467 bool success{false};
468 std::string failure_reason{};
469 };
470
471} // namespace libstp::autotune
Measured physical limits for a single chassis axis.
Definition types.hpp:19
double acceleration
Acceleration from 10–90% of max velocity (m/s² or rad/s²).
Definition types.hpp:23
double deceleration
Deceleration from 90–10% during coast-down (m/s² or rad/s²).
Definition types.hpp:25
double max_velocity
Peak velocity at full power (m/s or rad/s).
Definition types.hpp:21
Configuration for the DriveCharacterizer.
Definition types.hpp:32
double accel_timeout
Maximum seconds to wait for the acceleration phase.
Definition types.hpp:38
int sample_hz
Sampling rate in Hz for position sampling (e.g. 500).
Definition types.hpp:42
int power_percent
Raw PWM percentage applied during characterization (1–100).
Definition types.hpp:34
double decel_timeout
Maximum seconds to record the deceleration (coast-down) phase.
Definition types.hpp:40
int trials
Number of independent trials per axis; median is taken.
Definition types.hpp:36
Configuration for the EncoderCalibrator.
Definition types.hpp:446
double total_angle_rad
2 full turns.
Definition types.hpp:448
double min_required_angle_rad
At least 180° to compute scale.
Definition types.hpp:451
Encoder calibration result.
Definition types.hpp:462
std::array< double, 4 > scale_factors
Per-motor scale factor (same for all).
Definition types.hpp:464
std::array< double, 4 > ticks_to_rad
New per-motor ticks_to_rad.
Definition types.hpp:463
Configuration for the FirmwarePidTuner.
Definition types.hpp:216
double decel_duration_s
Extra samples after raw PWM is set to zero.
Definition types.hpp:226
Result of tuning a single motor's firmware-side velocity PID.
Definition types.hpp:291
double plant_T
PT1 plant time constant from the DE fit (s). 0 if unused.
Definition types.hpp:306
double plant_K
PT1 plant gain from the DE fit (BEMF units per PWM). 0 if unused.
Definition types.hpp:304
Configuration for the MotionTuner (Hooke-Jeeves coordinate descent).
Definition types.hpp:160
double min_timeout_s
Minimum computed timeout (s).
Definition types.hpp:178
double delta_shrink
Factor to shrink deltas on no improvement.
Definition types.hpp:166
double primary_speed_scale
Speed scale for trial motions.
Definition types.hpp:182
double initial_delta_frac
Fraction of initial gain for first delta.
Definition types.hpp:164
int sample_hz
Update loop rate (Hz).
Definition types.hpp:161
double min_linear_distance_m
Minimum clamped test distance (m).
Definition types.hpp:183
double stuck_angular_progress_deg
Angular progress threshold (deg).
Definition types.hpp:181
double turn_final_error_soft_rad
2 degrees in radians.
Definition types.hpp:197
double max_linear_distance_m
Maximum clamped test distance (m).
Definition types.hpp:184
double turn_test_angle_deg
Turn test angle (degrees).
Definition types.hpp:176
double stuck_linear_progress_m
Linear progress threshold (m).
Definition types.hpp:180
double turn_overshoot_soft_rad
3 degrees in radians.
Definition types.hpp:196
double settle_s
Post-trial settle time (s).
Definition types.hpp:162
double min_delta
Minimum delta before stopping.
Definition types.hpp:165
double stuck_timeout_s
Time without progress → stuck (s).
Definition types.hpp:179
double motion_timeout_s
Maximum trial duration (s).
Definition types.hpp:177
double linear_test_distance_m
Linear test distance per trial (m).
Definition types.hpp:175
int max_iterations
Maximum Hooke-Jeeves iterations.
Definition types.hpp:163
Result of tuning a single motion PID parameter (distance, lateral, or heading).
Definition types.hpp:145
First-order-plus-dead-time (FOPDT) plant parameters extracted from a step-response recording.
Definition types.hpp:67
double Tg
Time constant (lag) in seconds.
Definition types.hpp:70
double Ks
Static gain (steady-state output / step input).
Definition types.hpp:68
double Tu
Dead time in seconds.
Definition types.hpp:69
Configuration for the StaticFrictionMeasurer.
Definition types.hpp:317
int motion_threshold
Median |BEMF| above this counts as "moving".
Definition types.hpp:323
int start_pct
Lower bound of the PWM sweep (inclusive).
Definition types.hpp:318
int samples_per_step
BEMF samples taken per step (median).
Definition types.hpp:322
int max_pct
Upper bound of the PWM sweep (inclusive).
Definition types.hpp:319
int dwell_ms
Dwell after each setSpeed() call (ms).
Definition types.hpp:321
int step_pct
PWM increment per sweep step.
Definition types.hpp:320
Per-motor static-friction measurement result (kS in PWM percent).
Definition types.hpp:339
std::vector< StaticFrictionSample > forward_sweep
Forward sweep PWM-vs-BEMF curve (pwm_pct > 0), in sweep order.
Definition types.hpp:346
int motion_threshold
Motion threshold used (median |BEMF| above this counts as "moving").
Definition types.hpp:350
std::vector< StaticFrictionSample > reverse_sweep
Reverse sweep PWM-vs-BEMF curve (pwm_pct < 0), in sweep order.
Definition types.hpp:348
One PWM-vs-BEMF sample taken during a static-friction sweep.
Definition types.hpp:330
int median_bemf
Median |BEMF| (ADC counts) at this step.
Definition types.hpp:332
int pwm_pct
Signed PWM percent commanded (+ forward, − reverse).
Definition types.hpp:331
Raw step-response recording for one velocity axis.
Definition types.hpp:56
std::vector< double > measured
Measured velocity (m/s or rad/s).
Definition types.hpp:59
std::vector< double > times
Elapsed time (s) since step onset.
Definition types.hpp:57
std::vector< double > commanded
Commanded velocity (m/s or rad/s).
Definition types.hpp:58
Configuration for deriveTolerances().
Definition types.hpp:415
Result of deriveTolerances(): the derived tolerances and flags indicating which UnifiedMotionPidConfi...
Definition types.hpp:431
Configuration for the VelLpfTuner (encoder-velocity LPF alpha).
Definition types.hpp:361
double measure_duration_s
Sampling duration per motor (s).
Definition types.hpp:365
int sample_hz
Sampling rate (Hz).
Definition types.hpp:366
double alpha_max
Maximum alpha to evaluate.
Definition types.hpp:363
double alpha_min
Minimum alpha to evaluate.
Definition types.hpp:362
double settle_s
Settle time after spin-up before sampling (s).
Definition types.hpp:374
double noise_weight
Weight on filtered variance in the score.
Definition types.hpp:367
double alpha_step
Sweep increment.
Definition types.hpp:364
Per-motor LPF-alpha tuning result.
Definition types.hpp:393
std::vector< VelLpfSweepPoint > sweep
Score-vs-alpha sweep curve (one entry per evaluated alpha).
Definition types.hpp:404
std::vector< int > raw_bemf
Definition types.hpp:402
One point of the alpha sweep: the score (and its parts) the candidate alpha achieved on the captured ...
Definition types.hpp:382
double lag_change_rate
Std-dev of |Δfilt| (movement proxy).
Definition types.hpp:385
double variance
Variance of the filtered series.
Definition types.hpp:384
double score
Combined noise+lag score (lower is better).
Definition types.hpp:386
Configuration for the VelocityTuner.
Definition types.hpp:106
double low_dead_time_ratio
Tu/Tg below this → skip CHR (near-integrator / very fast plant).
Definition types.hpp:123
double steady_state_frac
Fraction of the run (from the end) used to fit steady-state velocity.
Definition types.hpp:134
int sample_hz
Step-response sampling rate in Hz.
Definition types.hpp:112
double low_dead_time_gain_tol
|Ks - 1| tolerance accompanying the low-dead-time early-return.
Definition types.hpp:125
double step_duration_s
Duration of each step-response recording in seconds.
Definition types.hpp:108
double step_command_frac
Fraction of max_velocity used as the step command (0–1).
Definition types.hpp:110
double republish_settle_s
Settle time (s) after re-publishing the kinematics config to the STM32.
Definition types.hpp:132
double min_response_frac
Tail response must exceed this fraction of |command| to accept.
Definition types.hpp:121
double gain_min
Clamp range for the calibrated per-axis command gain (1/Ks).
Definition types.hpp:129
Result of tuning a single velocity axis.
Definition types.hpp:79
double measured_gain_before
Measured steady-state gain (achieved/commanded) before tuning.
Definition types.hpp:92
foundation::Feedforward ff
kV=1 passthrough default
Definition types.hpp:83
double measured_gain_after
Measured steady-state gain (achieved/commanded) after tuning.
Definition types.hpp:94
StepResponseData tuned_response
Raw tuned step-response recording (empty when CHR was skipped).
Definition types.hpp:99
StepResponseData baseline_response
Definition types.hpp:97
std::string axis
"vx", "vy", or "wz"
Definition types.hpp:80
Feedforward terms for velocity and acceleration control.
Definition motor.hpp:24
Basic PID gains without integrator and output shaping parameters.
Definition motor.hpp:18