WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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::function<void(frc::sysid::State)> recordState)
56 : m_recordState{recordState} {
57 if (rampRate) {
58 m_rampRate = rampRate.value();
59 }
60 if (stepVoltage) {
61 m_stepVoltage = stepVoltage.value();
62 }
63 if (timeout) {
64 m_timeout = timeout.value();
65 }
66 }
67};
68
69class Mechanism {
70 public:
71 /// Sends the SysId-specified drive signal to the mechanism motors during test
72 /// routines.
73 std::function<void(units::volt_t)> m_drive;
74
75 /// Returns measured data (voltages, positions, velocities) of the mechanism
76 /// motors during test routines.
77 std::function<void(frc::sysid::SysIdRoutineLog*)> m_log;
78
79 /// The subsystem containing the motor(s) that is (or are) being
80 /// characterized.
82
83 /// The name of the mechanism being tested. Will be appended to the log entry
84 /// title for the routine's test state, e.g. "sysid-test-state-mechanism".
85 std::string m_name;
86
87 /**
88 * Create a new mechanism specification for a SysId routine.
89 *
90 * @param drive Sends the SysId-specified drive signal to the mechanism motors
91 * during test routines.
92 * @param log Returns measured data of the mechanism motors during test
93 * routines. To return data, call `Motor(string motorName)` on the supplied
94 * `SysIdRoutineLog` instance, and then call one or more of the chainable
95 * logging handles (e.g. `voltage`) on the returned `MotorLog`. Multiple
96 * motors can be logged in a single callback by calling `Motor` multiple
97 * times.
98 * @param subsystem The subsystem containing the motor(s) that is (or are)
99 * being characterized. Will be declared as a requirement for the returned
100 * test commands.
101 * @param name The name of the mechanism being tested. Will be appended to the
102 * log entry * title for the routine's test state, e.g.
103 * "sysid-test-state-mechanism". Defaults to the name of the subsystem if
104 * left null.
105 */
106 Mechanism(std::function<void(units::volt_t)> drive,
107 std::function<void(frc::sysid::SysIdRoutineLog*)> log,
108 frc2::Subsystem* subsystem, std::string_view name)
109 : m_drive{std::move(drive)},
110 m_log{log ? std::move(log) : [](frc::sysid::SysIdRoutineLog* log) {}},
111 m_subsystem{subsystem},
112 m_name{name} {}
113
114 /**
115 * Create a new mechanism specification for a SysId routine. Defaults the
116 * mechanism name to the subsystem name.
117 *
118 * @param drive Sends the SysId-specified drive signal to the mechanism motors
119 * during test routines.
120 * @param log Returns measured data of the mechanism motors during test
121 * routines. To return data, call `Motor(string motorName)` on the supplied
122 * `SysIdRoutineLog` instance, and then call one or more of the chainable
123 * logging handles (e.g. `voltage`) on the returned `MotorLog`. Multiple
124 * motors can be logged in a single callback by calling `Motor` multiple
125 * times.
126 * @param subsystem The subsystem containing the motor(s) that is (or are)
127 * being characterized. Will be declared as a requirement for the returned
128 * test commands. The subsystem's `name` will be appended to the log entry
129 * title for the routine's test state, e.g. "sysid-test-state-subsystem".
130 */
131 Mechanism(std::function<void(units::volt_t)> drive,
132 std::function<void(frc::sysid::SysIdRoutineLog*)> log,
133 frc2::Subsystem* subsystem)
134 : m_drive{std::move(drive)},
135 m_log{log ? std::move(log) : [](frc::sysid::SysIdRoutineLog* log) {}},
136 m_subsystem{subsystem},
137 m_name{m_subsystem->GetName()} {}
138};
139
140/**
141 * Motor direction for a SysId test.
142 */
144 /// Forward.
146 /// Reverse.
149
150/**
151 * A SysId characterization routine for a single mechanism. Mechanisms may have
152 * multiple motors.
153 *
154 * A single subsystem may have multiple mechanisms, but mechanisms should not
155 * share test routines. Each complete test of a mechanism should have its own
156 * SysIdRoutine instance, since the log name of the recorded data is determined
157 * by the mechanism name.
158 *
159 * The test state (e.g. "quasistatic-forward") is logged once per iteration
160 * during test execution, and once with state "none" when a test ends. Motor
161 * frames are logged every iteration during test execution.
162 *
163 * Timestamps are not coordinated across data, so motor frames and test state
164 * tags may be recorded on different log frames. Because frame alignment is not
165 * guaranteed, SysId parses the log by using the test state flag to determine
166 * the timestamp range for each section of the test, and then extracts the motor
167 * frames within the valid timestamp ranges. If a given test was run multiple
168 * times in a single logfile, the user will need to select which of the tests to
169 * use for the fit in the analysis tool.
170 */
172 public:
173 /**
174 * Create a new SysId characterization routine.
175 *
176 * @param config Hardware-independent parameters for the SysId routine.
177 * @param mechanism Hardware interface for the SysId routine.
178 */
179 SysIdRoutine(Config config, Mechanism mechanism)
180 : SysIdRoutineLog(mechanism.m_name),
181 m_config(config),
182 m_mechanism(mechanism),
183 m_recordState(config.m_recordState ? config.m_recordState
184 : [this](frc::sysid::State state) {
185 this->RecordState(state);
186 }) {}
187
190
191 private:
192 Config m_config;
193 Mechanism m_mechanism;
194 units::volt_t m_outputVolts{0};
195 std::function<void(frc::sysid::State)> m_recordState;
196 frc::Timer timer;
197};
198} // namespace frc2::sysid
A wrapper around std::unique_ptr<Command> so commands have move-only semantics.
Definition CommandPtr.h:28
A robot subsystem.
Definition Subsystem.h:43
virtual std::string GetName() const
Gets the name of this Subsystem.
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
units::volt_t m_stepVoltage
The step voltage output used for dynamic test routines.
Definition SysIdRoutine.h:30
Config(std::optional< ramp_rate_t > rampRate, std::optional< units::volt_t > stepVoltage, std::optional< units::second_t > timeout, std::function< void(frc::sysid::State)> recordState)
Create a new configuration for a SysId test routine.
Definition SysIdRoutine.h:52
Definition SysIdRoutine.h:69
std::function< void(frc::sysid::SysIdRoutineLog *)> m_log
Returns measured data (voltages, positions, velocities) of the mechanism motors during test routines.
Definition SysIdRoutine.h:77
std::string m_name
The name of the mechanism being tested.
Definition SysIdRoutine.h:85
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:131
std::function< void(units::volt_t)> m_drive
Sends the SysId-specified drive signal to the mechanism motors during test routines.
Definition SysIdRoutine.h:73
frc2::Subsystem * m_subsystem
The subsystem containing the motor(s) that is (or are) being characterized.
Definition SysIdRoutine.h:81
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:106
A SysId characterization routine for a single mechanism.
Definition SysIdRoutine.h:171
SysIdRoutine(Config config, Mechanism mechanism)
Create a new SysId characterization routine.
Definition SysIdRoutine.h:179
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
constexpr underlying_type value() const noexcept
unit value
Definition base.h:2111
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition base.h:1438
Definition SysIdRoutine.h:18
Direction
Motor direction for a SysId test.
Definition SysIdRoutine.h:143
@ kReverse
Reverse.
Definition SysIdRoutine.h:147
@ kForward
Forward.
Definition SysIdRoutine.h:145
State
Possible state of a SysId routine.
Definition SysIdRoutineLog.h:25
Definition CAN.h:11
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280