WPILibC++ 2025.0.0-alpha-1-9-ga2beb75
SysIdRoutineLog.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 <string>
8#include <string_view>
9
10#include <units/acceleration.h>
11#include <units/angle.h>
14#include <units/current.h>
15#include <units/length.h>
16#include <units/velocity.h>
17#include <units/voltage.h>
18#include <wpi/DataLog.h>
19
20namespace frc::sysid {
21
22/**
23 * Possible state of a SysId routine.
24 */
25enum class State {
26 /// Quasistatic forward test.
28 /// Quasistatic reverse test.
30 /// Dynamic forward test.
32 /// Dynamic reverse test.
34 /// No test.
35 kNone
36};
37
38/**
39 * Utility for logging data from a SysId test routine. Each complete routine
40 * (quasistatic and dynamic, forward and reverse) should have its own
41 * SysIdRoutineLog instance, with a unique log name.
42 */
46
47 public:
48 /** Logs data from a single motor during a SysIdRoutine. */
49 class MotorLog {
50 public:
51 /**
52 * Log a generic data value from the motor.
53 *
54 * @param name The name of the data field being recorded.
55 * @param value The numeric value of the data field.
56 * @param unit The unit string of the data field.
57 * @return The motor log (for call chaining).
58 */
60
61 /**
62 * Log the voltage applied to the motor.
63 *
64 * @param voltage The voltage to record.
65 * @return The motor log (for call chaining).
66 */
67 MotorLog& voltage(units::volt_t voltage) {
68 return value("voltage", voltage.value(), voltage.name());
69 }
70
71 /**
72 * Log the linear position of the motor.
73 *
74 * @param position The linear position to record.
75 * @return The motor log (for call chaining).
76 */
77 MotorLog& position(units::meter_t position) {
78 return value("position", position.value(), position.name());
79 }
80
81 /**
82 * Log the angular position of the motor.
83 *
84 * @param position The angular position to record.
85 * @return The motor log (for call chaining).
86 */
87 MotorLog& position(units::turn_t position) {
88 return value("position", position.value(), position.name());
89 }
90
91 /**
92 * Log the linear velocity of the motor.
93 *
94 * @param velocity The linear velocity to record.
95 * @return The motor log (for call chaining).
96 */
97 MotorLog& velocity(units::meters_per_second_t velocity) {
98 return value("velocity", velocity.value(), velocity.name());
99 }
100
101 /**
102 * Log the angular velocity of the motor.
103 *
104 * @param velocity The angular velocity to record.
105 * @return The motor log (for call chaining).
106 */
107 MotorLog& velocity(units::turns_per_second_t velocity) {
108 return value("velocity", velocity.value(), velocity.name());
109 }
110
111 /**
112 * Log the linear acceleration of the motor.
113 *
114 * This is optional; SysId can perform an accurate fit without it.
115 *
116 * @param acceleration The linear acceleration to record.
117 * @return The motor log (for call chaining).
118 */
119 MotorLog& acceleration(units::meters_per_second_squared_t acceleration) {
120 return value("acceleration", acceleration.value(), acceleration.name());
121 }
122
123 /**
124 * Log the angular acceleration of the motor.
125 *
126 * This is optional; SysId can perform an accurate fit without it.
127 *
128 * @param acceleration The angular acceleration to record.
129 * @return The motor log (for call chaining).
130 */
131 MotorLog& acceleration(units::turns_per_second_squared_t acceleration) {
132 return value("acceleration", acceleration.value(), acceleration.name());
133 }
134
135 /**
136 * Log the current applied to the motor.
137 *
138 * This is optional; SysId can perform an accurate fit without it.
139 *
140 * @param current The current to record.
141 * @return The motor log (for call chaining).
142 */
143 MotorLog& current(units::ampere_t current) {
144 return value("current", current.value(), current.name());
145 }
146
147 private:
148 friend class SysIdRoutineLog;
149 /**
150 * Create a new SysId motor log handle.
151 *
152 * @param motorName The name of the motor whose data is being logged.
153 * @param logName The name of the SysIdRoutineLog that this motor belongs
154 * to.
155 * @param logEntries The DataLog entries of the SysIdRoutineLog that this
156 * motor belongs to.
157 */
158 MotorLog(std::string_view motorName, std::string_view logName,
159 LogEntries* logEntries);
160 std::string m_motorName;
161 std::string m_logName;
162 LogEntries* m_logEntries;
163 };
164
165 /**
166 * Create a new logging utility for a SysId test routine.
167 *
168 * @param logName The name for the test routine in the log. Should be unique
169 * between complete test routines (quasistatic and dynamic, forward and
170 * reverse). The current state of this test (e.g. "quasistatic-forward")
171 * will appear in WPILog under the "sysid-test-state-logName" entry.
172 */
174
175 /**
176 * Records the current state of the SysId test routine. Should be called once
177 * per iteration during tests with the type of the current test, and once upon
178 * test end with state `none`.
179 *
180 * @param state The current state of the SysId test routine.
181 */
183
184 /**
185 * Log data from a motor during a SysId routine.
186 *
187 * @param motorName The name of the motor.
188 * @return Handle with chainable callbacks to log individual data fields.
189 */
191
192 static std::string StateEnumToString(State state);
193
194 private:
195 LogEntries m_logEntries;
196 std::string m_logName;
197 bool m_stateInitialized = false;
199};
200} // namespace frc::sysid
Logs data from a single motor during a SysIdRoutine.
Definition: SysIdRoutineLog.h:49
MotorLog & acceleration(units::meters_per_second_squared_t acceleration)
Log the linear acceleration of the motor.
Definition: SysIdRoutineLog.h:119
MotorLog & acceleration(units::turns_per_second_squared_t acceleration)
Log the angular acceleration of the motor.
Definition: SysIdRoutineLog.h:131
MotorLog & current(units::ampere_t current)
Log the current applied to the motor.
Definition: SysIdRoutineLog.h:143
MotorLog & velocity(units::turns_per_second_t velocity)
Log the angular velocity of the motor.
Definition: SysIdRoutineLog.h:107
MotorLog & value(std::string_view name, double value, std::string_view unit)
Log a generic data value from the motor.
MotorLog & position(units::meter_t position)
Log the linear position of the motor.
Definition: SysIdRoutineLog.h:77
MotorLog & position(units::turn_t position)
Log the angular position of the motor.
Definition: SysIdRoutineLog.h:87
MotorLog & velocity(units::meters_per_second_t velocity)
Log the linear velocity of the motor.
Definition: SysIdRoutineLog.h:97
MotorLog & voltage(units::volt_t voltage)
Log the voltage applied to the motor.
Definition: SysIdRoutineLog.h:67
Utility for logging data from a SysId test routine.
Definition: SysIdRoutineLog.h:43
static std::string StateEnumToString(State state)
MotorLog Motor(std::string_view motorName)
Log data from a motor during a SysId routine.
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.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:116
Log string values.
Definition: DataLog.h:709
basic_string_view< char > string_view
Definition: core.h:518
state
Definition: core.h:2305
Definition: SysIdRoutineLog.h:20
State
Possible state of a SysId routine.
Definition: SysIdRoutineLog.h:25
@ kQuasistaticForward
Quasistatic forward test.
@ kDynamicReverse
Dynamic reverse test.
@ kDynamicForward
Dynamic forward test.
@ kQuasistaticReverse
Quasistatic reverse test.
@ kNone
No edge configuration (neither rising nor falling).
Type representing an arbitrary unit.
Definition: base.h:886