libstp
Robotics library for Botball
Loading...
Searching...
No Matches
auto_tuner.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <map>
5#include <optional>
6#include <string>
7#include <vector>
8
9#include "autotune/types.hpp"
10#include "drive/drive.hpp"
11#include "hal/IMotor.hpp"
12#include "hal/odometry.hpp"
14
16{
27 {
28 // Phase enable flags (in execution order).
29 bool tune_vel_lpf{true};
31 bool tune_firmware_pid{false};
32 bool tune_encoder_cal{true};
34 bool tune_velocity{true};
35 bool tune_motion{true};
36 bool tune_tolerances{true};
37
38 // Per-phase configurations.
47
48 // Axes / parameters per phase (empty → auto-fill from kinematics support).
49 std::vector<std::string> characterize_axes;
50 std::vector<std::string> velocity_axes;
51 std::vector<std::string> motion_params;
52
53 // Map of axis name → max velocity (m/s or rad/s) for the velocity phase.
54 // If empty, populated from the characterize phase results (or sensible
55 // defaults when characterize is disabled).
56 std::map<std::string, double> max_velocities;
57
58 // Optional pre-supplied max-BEMF speeds for firmware PID tuning.
59 // If unset, the AutoTuner runs a short power sweep to estimate them.
60 std::optional<std::map<int, int>> max_bemf_speeds{};
61 };
62
70 {
71 std::map<int, VelLpfResult> vel_lpf{};
72 std::map<int, StaticFrictionResult> static_friction{};
73 std::map<int, FirmwarePidResult> firmware_pid{};
75 std::map<std::string, AxisResult> characterize{};
76 std::map<std::string, VelocityTuneResult> velocity{};
77 std::map<std::string, MotionTuneResult> motion{};
79
80 bool vel_lpf_ran{false};
82 bool firmware_pid_ran{false};
83 bool encoder_cal_ran{false};
84 bool characterize_ran{false};
85 bool velocity_ran{false};
86 bool motion_ran{false};
87 bool tolerances_ran{false};
88 };
89
110 {
111 public:
113 using ConfirmCallback = std::function<void(const std::string& phase_key)>;
114
117 motion::UnifiedMotionPidConfig& motion_pid_config);
118
132
135
136 // --------------------------------------------------------------
137 // Individual phases — usable standalone from Python wrappers.
138 // --------------------------------------------------------------
139
140 std::map<int, VelLpfResult>
142
143 std::map<int, StaticFrictionResult>
145
146 std::map<int, FirmwarePidResult>
148 std::optional<std::map<int, int>> max_bemf_speeds = std::nullopt);
149
152
153 std::map<std::string, AxisResult>
154 runCharacterizePhase(const std::vector<std::string>& axes,
155 const CharacterizeConfig& cfg);
156
157 std::map<std::string, VelocityTuneResult>
158 runVelocityPhase(const std::vector<std::string>& axes,
159 const std::map<std::string, double>& max_velocities,
160 const VelocityTuneConfig& cfg);
161
162 std::map<std::string, MotionTuneResult>
163 runMotionPhase(const std::vector<std::string>& params,
164 const MotionTuneConfig& cfg);
165
167 runTolerancesPhase(const std::map<std::string, MotionTuneResult>& motion_results,
168 const MotionTuneConfig& motion_cfg,
169 const ToleranceConfig& cfg);
170
171 private:
173 void confirm(const std::string& phase_key);
174
176 std::vector<hal::motor::IMotor*> motors();
177
179 std::optional<std::map<int, int>> estimateMaxBemfSpeeds();
180
182 bool bemfAvailable();
183
184 drive::Drive& drive_;
185 odometry::IOdometry& odometry_;
186 motion::UnifiedMotionPidConfig& motion_pid_config_;
187 ConfirmCallback confirm_cb_{};
188 };
189
190} // namespace libstp::autotune
std::function< void(const std::string &phase_key)> ConfirmCallback
EncoderCalResult runEncoderCalibrationPhase(const EncoderCalConfig &cfg)
std::map< std::string, MotionTuneResult > runMotionPhase(const std::vector< std::string > &params, const MotionTuneConfig &cfg)
std::map< int, StaticFrictionResult > runStaticFrictionPhase(const StaticFrictionConfig &cfg)
AutoTuner(drive::Drive &drive, odometry::IOdometry &odometry, motion::UnifiedMotionPidConfig &motion_pid_config)
std::map< std::string, AxisResult > runCharacterizePhase(const std::vector< std::string > &axes, const CharacterizeConfig &cfg)
AutoTuneResult tuneAll(const AutoTuneConfig &cfg)
std::map< int, VelLpfResult > runVelLpfPhase(const VelLpfConfig &cfg)
std::map< int, FirmwarePidResult > runFirmwarePidPhase(const FirmwarePidConfig &cfg, std::optional< std::map< int, int > > max_bemf_speeds=std::nullopt)
ToleranceResult runTolerancesPhase(const std::map< std::string, MotionTuneResult > &motion_results, const MotionTuneConfig &motion_cfg, const ToleranceConfig &cfg)
void setConfirmCallback(ConfirmCallback cb)
Install a callback invoked before every phase in tuneAll().
std::map< std::string, VelocityTuneResult > runVelocityPhase(const std::vector< std::string > &axes, const std::map< std::string, double > &max_velocities, const VelocityTuneConfig &cfg)
Aggregated configuration for the full auto-tune pipeline.
FirmwarePidConfig firmware_pid_cfg
std::map< std::string, double > max_velocities
VelocityTuneConfig velocity_cfg
std::vector< std::string > motion_params
CharacterizeConfig characterize_cfg
std::vector< std::string > characterize_axes
std::vector< std::string > velocity_axes
std::optional< std::map< int, int > > max_bemf_speeds
StaticFrictionConfig static_friction_cfg
Aggregated results of the auto-tune pipeline.
std::map< std::string, AxisResult > characterize
std::map< int, VelLpfResult > vel_lpf
std::map< std::string, MotionTuneResult > motion
std::map< std::string, VelocityTuneResult > velocity
std::map< int, FirmwarePidResult > firmware_pid
std::map< int, StaticFrictionResult > static_friction
Configuration for the DriveCharacterizer.
Definition types.hpp:32
Configuration for the EncoderCalibrator.
Definition types.hpp:446
Encoder calibration result.
Definition types.hpp:462
Configuration for the FirmwarePidTuner.
Definition types.hpp:216
Configuration for the MotionTuner (Hooke-Jeeves coordinate descent).
Definition types.hpp:160
Configuration for the StaticFrictionMeasurer.
Definition types.hpp:317
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
Configuration for the VelocityTuner.
Definition types.hpp:106