WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
SysIdRoutine.hpp
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
15#include "wpi/system/Timer.hpp"
16
17namespace wpi::cmd::sysid {
18
19using ramp_rate_t = wpi::units::unit_t<wpi::units::compound_unit<
20 wpi::units::volt, wpi::units::inverse<wpi::units::second>>>;
21
22/** Hardware-independent configuration for a SysId test routine. */
23class Config {
24 public:
25 /// The voltage ramp rate used for quasistatic test routines.
27
28 /// The step voltage output used for dynamic test routines.
29 wpi::units::volt_t m_stepVoltage{7_V};
30
31 /// Safety timeout for the test routine commands.
32 wpi::units::second_t m_timeout{10_s};
33
34 /// Optional handle for recording test state in a third-party logging
35 /// solution.
36 std::function<void(wpi::sysid::State)> m_recordState;
37
38 /**
39 * Create a new configuration for a SysId test routine.
40 *
41 * @param rampRate The voltage ramp rate used for quasistatic test routines.
42 * Defaults to 1 volt per second if left null.
43 * @param stepVoltage The step voltage output used for dynamic test routines.
44 * Defaults to 7 volts if left null.
45 * @param timeout Safety timeout for the test routine commands. Defaults to 10
46 * seconds if left null.
47 * @param recordState Optional handle for recording test state in a
48 * third-party logging solution. If provided, the test routine state will be
49 * passed to this callback instead of logged in WPILog.
50 */
51 Config(std::optional<ramp_rate_t> rampRate,
52 std::optional<wpi::units::volt_t> stepVoltage,
53 std::optional<wpi::units::second_t> timeout,
54 std::function<void(wpi::sysid::State)> recordState)
55 : m_recordState{std::move(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 }
66};
67
68class Mechanism {
69 public:
70 /// Sends the SysId-specified drive signal to the mechanism motors during test
71 /// routines.
72 std::function<void(wpi::units::volt_t)> m_drive;
73
74 /// Returns measured data (voltages, positions, velocities) of the mechanism
75 /// motors during test routines.
76 std::function<void(wpi::sysid::SysIdRoutineLog*)> m_log;
77
78 /// The subsystem containing the motor(s) that is (or are) being
79 /// characterized.
81
82 /// The name of the mechanism being tested. Will be appended to the log entry
83 /// title for the routine's test state, e.g. "sysid-test-state-mechanism".
84 std::string m_name;
85
86 /**
87 * Create a new mechanism specification for a SysId routine.
88 *
89 * @param drive Sends the SysId-specified drive signal to the mechanism motors
90 * during test routines.
91 * @param log Returns measured data of the mechanism motors during test
92 * routines. To return data, call `Motor(string motorName)` on the supplied
93 * `SysIdRoutineLog` instance, and then call one or more of the chainable
94 * logging handles (e.g. `voltage`) on the returned `MotorLog`. Multiple
95 * motors can be logged in a single callback by calling `Motor` multiple
96 * times.
97 * @param subsystem The subsystem containing the motor(s) that is (or are)
98 * being characterized. Will be declared as a requirement for the returned
99 * test commands.
100 * @param name The name of the mechanism being tested. Will be appended to the
101 * log entry * title for the routine's test state, e.g.
102 * "sysid-test-state-mechanism". Defaults to the name of the subsystem if
103 * left null.
104 */
105 Mechanism(std::function<void(wpi::units::volt_t)> drive,
106 std::function<void(wpi::sysid::SysIdRoutineLog*)> log,
107 wpi::cmd::Subsystem* subsystem, std::string_view name)
108 : m_drive{std::move(drive)},
109 m_log{log ? std::move(log) : [](wpi::sysid::SysIdRoutineLog* log) {}},
110 m_subsystem{subsystem},
111 m_name{name} {}
112
113 /**
114 * Create a new mechanism specification for a SysId routine. Defaults the
115 * mechanism name to the subsystem name.
116 *
117 * @param drive Sends the SysId-specified drive signal to the mechanism motors
118 * during test routines.
119 * @param log Returns measured data of the mechanism motors during test
120 * routines. To return data, call `Motor(string motorName)` on the supplied
121 * `SysIdRoutineLog` instance, and then call one or more of the chainable
122 * logging handles (e.g. `voltage`) on the returned `MotorLog`. Multiple
123 * motors can be logged in a single callback by calling `Motor` multiple
124 * times.
125 * @param subsystem The subsystem containing the motor(s) that is (or are)
126 * being characterized. Will be declared as a requirement for the returned
127 * test commands. The subsystem's `name` will be appended to the log entry
128 * title for the routine's test state, e.g. "sysid-test-state-subsystem".
129 */
130 Mechanism(std::function<void(wpi::units::volt_t)> drive,
131 std::function<void(wpi::sysid::SysIdRoutineLog*)> log,
132 wpi::cmd::Subsystem* subsystem)
133 : m_drive{std::move(drive)},
134 m_log{log ? std::move(log) : [](wpi::sysid::SysIdRoutineLog* log) {}},
135 m_subsystem{subsystem},
136 m_name{m_subsystem->GetName()} {}
137};
138
139/**
140 * Motor direction for a SysId test.
141 */
143 /// Forward.
145 /// Reverse.
147};
148
149/**
150 * A SysId characterization routine for a single mechanism. Mechanisms may have
151 * multiple motors.
152 *
153 * A single subsystem may have multiple mechanisms, but mechanisms should not
154 * share test routines. Each complete test of a mechanism should have its own
155 * SysIdRoutine instance, since the log name of the recorded data is determined
156 * by the mechanism name.
157 *
158 * The test state (e.g. "quasistatic-forward") is logged once per iteration
159 * during test execution, and once with state "none" when a test ends. Motor
160 * frames are logged every iteration during test execution.
161 *
162 * Timestamps are not coordinated across data, so motor frames and test state
163 * tags may be recorded on different log frames. Because frame alignment is not
164 * guaranteed, SysId parses the log by using the test state flag to determine
165 * the timestamp range for each section of the test, and then extracts the motor
166 * frames within the valid timestamp ranges. If a given test was run multiple
167 * times in a single logfile, the user will need to select which of the tests to
168 * use for the fit in the analysis tool.
169 */
171 public:
172 /**
173 * Create a new SysId characterization routine.
174 *
175 * @param config Hardware-independent parameters for the SysId routine.
176 * @param mechanism Hardware interface for the SysId routine.
177 */
178 SysIdRoutine(Config config, Mechanism mechanism)
179 : SysIdRoutineLog(mechanism.m_name),
180 m_config(config),
181 m_mechanism(mechanism),
182 m_recordState(config.m_recordState ? config.m_recordState
183 : [this](wpi::sysid::State state) {
184 this->RecordState(state);
185 }) {}
186
189
190 private:
191 Config m_config;
192 Mechanism m_mechanism;
193 wpi::units::volt_t m_outputVolts{0};
194 std::function<void(wpi::sysid::State)> m_recordState;
195 wpi::Timer timer;
196};
197} // namespace wpi::cmd::sysid
@ name
Definition base.h:690
A timer class.
Definition Timer.hpp:36
A wrapper around std::unique_ptr<Command> so commands have move-only semantics.
Definition CommandPtr.hpp:28
A robot subsystem.
Definition Subsystem.hpp:42
virtual std::string GetName() const
Gets the name of this Subsystem.
Hardware-independent configuration for a SysId test routine.
Definition SysIdRoutine.hpp:23
wpi::units::second_t m_timeout
Safety timeout for the test routine commands.
Definition SysIdRoutine.hpp:32
Config(std::optional< ramp_rate_t > rampRate, std::optional< wpi::units::volt_t > stepVoltage, std::optional< wpi::units::second_t > timeout, std::function< void(wpi::sysid::State)> recordState)
Create a new configuration for a SysId test routine.
Definition SysIdRoutine.hpp:51
wpi::units::volt_t m_stepVoltage
The step voltage output used for dynamic test routines.
Definition SysIdRoutine.hpp:29
ramp_rate_t m_rampRate
The voltage ramp rate used for quasistatic test routines.
Definition SysIdRoutine.hpp:26
std::function< void(wpi::sysid::State)> m_recordState
Optional handle for recording test state in a third-party logging solution.
Definition SysIdRoutine.hpp:36
Definition SysIdRoutine.hpp:68
Mechanism(std::function< void(wpi::units::volt_t)> drive, std::function< void(wpi::sysid::SysIdRoutineLog *)> log, wpi::cmd::Subsystem *subsystem, std::string_view name)
Create a new mechanism specification for a SysId routine.
Definition SysIdRoutine.hpp:105
std::function< void(wpi::sysid::SysIdRoutineLog *)> m_log
Returns measured data (voltages, positions, velocities) of the mechanism motors during test routines.
Definition SysIdRoutine.hpp:76
wpi::cmd::Subsystem * m_subsystem
The subsystem containing the motor(s) that is (or are) being characterized.
Definition SysIdRoutine.hpp:80
Mechanism(std::function< void(wpi::units::volt_t)> drive, std::function< void(wpi::sysid::SysIdRoutineLog *)> log, wpi::cmd::Subsystem *subsystem)
Create a new mechanism specification for a SysId routine.
Definition SysIdRoutine.hpp:130
std::string m_name
The name of the mechanism being tested.
Definition SysIdRoutine.hpp:84
std::function< void(wpi::units::volt_t)> m_drive
Sends the SysId-specified drive signal to the mechanism motors during test routines.
Definition SysIdRoutine.hpp:72
wpi::cmd::CommandPtr Quasistatic(Direction direction)
SysIdRoutine(Config config, Mechanism mechanism)
Create a new SysId characterization routine.
Definition SysIdRoutine.hpp:178
wpi::cmd::CommandPtr Dynamic(Direction direction)
Utility for logging data from a SysId test routine.
Definition SysIdRoutineLog.hpp:43
SysIdRoutineLog(std::string_view logName)
Create a new logging utility for a SysId test routine.
void RecordState(State state)
Records the current state of the SysId test routine.
Definition StringMap.hpp:773
Definition SysIdRoutine.hpp:17
Direction
Motor direction for a SysId test.
Definition SysIdRoutine.hpp:142
@ kForward
Forward.
Definition SysIdRoutine.hpp:144
@ kReverse
Reverse.
Definition SysIdRoutine.hpp:146
wpi::units::unit_t< wpi::units::compound_unit< wpi::units::volt, wpi::units::inverse< wpi::units::second > > > ramp_rate_t
Definition SysIdRoutine.hpp:19
Definition DataLogReader.hpp:17
State
Possible state of a SysId routine.
Definition SysIdRoutineLog.hpp:25
Definition CvSource.hpp:15