WPILibC++ 2024.3.2
SysIdRoutine.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <string>
9#include <string_view>
10#include <utility>
11
12#include <frc/Timer.h>
14
17
18namespace frc2::sysid {
19
22
23/** Hardware-independent configuration for a SysId test routine. */
24class Config {
25 public:
26 /// The voltage ramp rate used for quasistatic test routines.
28
29 /// The step voltage output used for dynamic test routines.
30 units::volt_t m_stepVoltage{7_V};
31
32 /// Safety timeout for the test routine commands.
33 units::second_t m_timeout{10_s};
34
35 /// Optional handle for recording test state in a third-party logging
36 /// solution.
37 std::function<void(frc::sysid::State)> m_recordState;
38
39 /**
40 * Create a new configuration for a SysId test routine.
41 *
42 * @param rampRate The voltage ramp rate used for quasistatic test routines.
43 * Defaults to 1 volt per second if left null.
44 * @param stepVoltage The step voltage output used for dynamic test routines.
45 * Defaults to 7 volts if left null.
46 * @param timeout Safety timeout for the test routine commands. Defaults to 10
47 * seconds if left null.
48 * @param recordState Optional handle for recording test state in a
49 * third-party logging solution. If provided, the test routine state will be
50 * passed to this callback instead of logged in WPILog.
51 */
52 Config(std::optional<ramp_rate_t> rampRate,
53 std::optional<units::volt_t> stepVoltage,
54 std::optional<units::second_t> timeout,
55 std::optional<std::function<void(frc::sysid::State)>> recordState) {
56 if (rampRate) {
57 m_rampRate = rampRate.value();
58 }
59 if (stepVoltage) {
60 m_stepVoltage = stepVoltage.value();
61 }
62 if (timeout) {
63 m_timeout = timeout.value();
64 }
65 if (recordState) {
66 m_recordState = recordState.value();
67 }
68 }
69};
70
71class Mechanism {
72 public:
73 /// Sends the SysId-specified drive signal to the mechanism motors during test
74 /// routines.
75 std::function<void(units::volt_t)> m_drive;
76
77 /// Returns measured data (voltages, positions, velocities) of the mechanism
78 /// motors during test routines.
79 std::function<void(frc::sysid::SysIdRoutineLog*)> m_log;
80
81 /// The subsystem containing the motor(s) that is (or are) being
82 /// characterized.
84
85 /// The name of the mechanism being tested. Will be appended to the log entry
86 /// title for the routine's test state, e.g. "sysid-test-state-mechanism".
87 std::string m_name;
88
89 /**
90 * Create a new mechanism specification for a SysId routine.
91 *
92 * @param drive Sends the SysId-specified drive signal to the mechanism motors
93 * during test routines.
94 * @param log Returns measured data of the mechanism motors during test
95 * routines. To return data, call `Motor(string motorName)` on the supplied
96 * `SysIdRoutineLog` instance, and then call one or more of the chainable
97 * logging handles (e.g. `voltage`) on the returned `MotorLog`. Multiple
98 * motors can be logged in a single callback by calling `Motor` multiple
99 * times.
100 * @param subsystem The subsystem containing the motor(s) that is (or are)
101 * being characterized. Will be declared as a requirement for the returned
102 * test commands.
103 * @param name The name of the mechanism being tested. Will be appended to the
104 * log entry * title for the routine's test state, e.g.
105 * "sysid-test-state-mechanism". Defaults to the name of the subsystem if
106 * left null.
107 */
108 Mechanism(std::function<void(units::volt_t)> drive,
109 std::function<void(frc::sysid::SysIdRoutineLog*)> log,
110 frc2::Subsystem* subsystem, std::string_view name)
111 : m_drive{std::move(drive)},
112 m_log{std::move(log)},
113 m_subsystem{subsystem},
114 m_name{name} {}
115
116 /**
117 * Create a new mechanism specification for a SysId routine. Defaults the
118 * mechanism name to the subsystem name.
119 *
120 * @param drive Sends the SysId-specified drive signal to the mechanism motors
121 * during test routines.
122 * @param log Returns measured data of the mechanism motors during test
123 * routines. To return data, call `Motor(string motorName)` on the supplied
124 * `SysIdRoutineLog` instance, and then call one or more of the chainable
125 * logging handles (e.g. `voltage`) on the returned `MotorLog`. Multiple
126 * motors can be logged in a single callback by calling `Motor` multiple
127 * times.
128 * @param subsystem The subsystem containing the motor(s) that is (or are)
129 * being characterized. Will be declared as a requirement for the returned
130 * test commands. The subsystem's `name` will be appended to the log entry
131 * title for the routine's test state, e.g. "sysid-test-state-subsystem".
132 */
133 Mechanism(std::function<void(units::volt_t)> drive,
134 std::function<void(frc::sysid::SysIdRoutineLog*)> log,
135 frc2::Subsystem* subsystem)
136 : m_drive{std::move(drive)},
137 m_log{std::move(log)},
138 m_subsystem{subsystem},
139 m_name{m_subsystem->GetName()} {}
140};
141
142/**
143 * Motor direction for a SysId test.
144 */
146 /// Forward.
148 /// Reverse.
151
152/**
153 * A SysId characterization routine for a single mechanism. Mechanisms may have
154 * multiple motors.
155 *
156 * A single subsystem may have multiple mechanisms, but mechanisms should not
157 * share test routines. Each complete test of a mechanism should have its own
158 * SysIdRoutine instance, since the log name of the recorded data is determined
159 * by the mechanism name.
160 *
161 * The test state (e.g. "quasistatic-forward") is logged once per iteration
162 * during test execution, and once with state "none" when a test ends. Motor
163 * frames are logged every iteration during test execution.
164 *
165 * Timestamps are not coordinated across data, so motor frames and test state
166 * tags may be recorded on different log frames. Because frame alignment is not
167 * guaranteed, SysId parses the log by using the test state flag to determine
168 * the timestamp range for each section of the test, and then extracts the motor
169 * frames within the valid timestamp ranges. If a given test was run multiple
170 * times in a single logfile, the user will need to select which of the tests to
171 * use for the fit in the analysis tool.
172 */
174 public:
175 /**
176 * Create a new SysId characterization routine.
177 *
178 * @param config Hardware-independent parameters for the SysId routine.
179 * @param mechanism Hardware interface for the SysId routine.
180 */
181 SysIdRoutine(Config config, Mechanism mechanism)
182 : SysIdRoutineLog(mechanism.m_name),
183 m_config(config),
184 m_mechanism(mechanism),
185 m_recordState(config.m_recordState ? config.m_recordState
186 : [this](frc::sysid::State state) {
187 this->RecordState(state);
188 }) {}
189
192
193 private:
194 Config m_config;
195 Mechanism m_mechanism;
196 units::volt_t m_outputVolts{0};
197 std::function<void(frc::sysid::State)> m_recordState;
198 frc::Timer timer;
199};
200} // namespace frc2::sysid
A wrapper around std::unique_ptr<Command> so commands have move-only semantics.
Definition: CommandPtr.h:29
A robot subsystem.
Definition: Subsystem.h:43
Hardware-independent configuration for a SysId test routine.
Definition: SysIdRoutine.h:24
std::function< void(frc::sysid::State)> m_recordState
Optional handle for recording test state in a third-party logging solution.
Definition: SysIdRoutine.h:37
ramp_rate_t m_rampRate
The voltage ramp rate used for quasistatic test routines.
Definition: SysIdRoutine.h:27
units::second_t m_timeout
Safety timeout for the test routine commands.
Definition: SysIdRoutine.h:33
Config(std::optional< ramp_rate_t > rampRate, std::optional< units::volt_t > stepVoltage, std::optional< units::second_t > timeout, std::optional< std::function< void(frc::sysid::State)> > recordState)
Create a new configuration for a SysId test routine.
Definition: SysIdRoutine.h:52
units::volt_t m_stepVoltage
The step voltage output used for dynamic test routines.
Definition: SysIdRoutine.h:30
Definition: SysIdRoutine.h:71
std::function< void(frc::sysid::SysIdRoutineLog *)> m_log
Returns measured data (voltages, positions, velocities) of the mechanism motors during test routines.
Definition: SysIdRoutine.h:79
std::string m_name
The name of the mechanism being tested.
Definition: SysIdRoutine.h:87
Mechanism(std::function< void(units::volt_t)> drive, std::function< void(frc::sysid::SysIdRoutineLog *)> log, frc2::Subsystem *subsystem)
Create a new mechanism specification for a SysId routine.
Definition: SysIdRoutine.h:133
std::function< void(units::volt_t)> m_drive
Sends the SysId-specified drive signal to the mechanism motors during test routines.
Definition: SysIdRoutine.h:75
frc2::Subsystem * m_subsystem
The subsystem containing the motor(s) that is (or are) being characterized.
Definition: SysIdRoutine.h:83
Mechanism(std::function< void(units::volt_t)> drive, std::function< void(frc::sysid::SysIdRoutineLog *)> log, frc2::Subsystem *subsystem, std::string_view name)
Create a new mechanism specification for a SysId routine.
Definition: SysIdRoutine.h:108
A SysId characterization routine for a single mechanism.
Definition: SysIdRoutine.h:173
SysIdRoutine(Config config, Mechanism mechanism)
Create a new SysId characterization routine.
Definition: SysIdRoutine.h:181
frc2::CommandPtr Quasistatic(Direction direction)
frc2::CommandPtr Dynamic(Direction direction)
A timer class.
Definition: Timer.h:36
Utility for logging data from a SysId test routine.
Definition: SysIdRoutineLog.h:43
void RecordState(State state)
Records the current state of the SysId test routine.
SysIdRoutineLog(std::string_view logName)
Create a new logging utility for a SysId test routine.
constexpr underlying_type value() const noexcept
unit value
Definition: base.h:2107
basic_string_view< char > string_view
Definition: core.h:501
dimensionless::scalar_t log(const ScalarUnit x) noexcept
Compute natural logarithm.
Definition: math.h:349
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition: base.h:1434
state
Definition: core.h:2271
Definition: SysIdRoutine.h:18
Direction
Motor direction for a SysId test.
Definition: SysIdRoutine.h:145
@ kReverse
Reverse.
Definition: SysIdRoutine.h:149
@ kForward
Forward.
Definition: SysIdRoutine.h:147
State
Possible state of a SysId routine.
Definition: SysIdRoutineLog.h:25
Definition: AprilTagPoseEstimator.h:15
Definition: array.h:89