WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
SysIdRoutineLog.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 <string>
8#include <string_view>
9
11#include "wpi/units/acceleration.hpp"
12#include "wpi/units/angle.hpp"
13#include "wpi/units/angular_acceleration.hpp"
14#include "wpi/units/angular_velocity.hpp"
15#include "wpi/units/current.hpp"
16#include "wpi/units/length.hpp"
17#include "wpi/units/velocity.hpp"
18#include "wpi/units/voltage.hpp"
19
20namespace wpi::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.
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 */
45 using LogEntries = wpi::util::StringMap<MotorEntries>;
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 */
59 MotorLog& value(std::string_view name, double value, std::string_view unit);
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(wpi::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(wpi::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(wpi::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(wpi::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(wpi::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(
120 wpi::units::meters_per_second_squared_t acceleration) {
121 return value("acceleration", acceleration.value(), acceleration.name());
122 }
123
124 /**
125 * Log the angular acceleration of the motor.
126 *
127 * This is optional; SysId can perform an accurate fit without it.
128 *
129 * @param acceleration The angular acceleration to record.
130 * @return The motor log (for call chaining).
131 */
132 MotorLog& acceleration(
133 wpi::units::turns_per_second_squared_t acceleration) {
134 return value("acceleration", acceleration.value(), acceleration.name());
135 }
136
137 /**
138 * Log the current applied to the motor.
139 *
140 * This is optional; SysId can perform an accurate fit without it.
141 *
142 * @param current The current to record.
143 * @return The motor log (for call chaining).
144 */
145 MotorLog& current(wpi::units::ampere_t current) {
146 return value("current", current.value(), current.name());
147 }
148
149 private:
150 friend class SysIdRoutineLog;
151 /**
152 * Create a new SysId motor log handle.
153 *
154 * @param motorName The name of the motor whose data is being logged.
155 * @param logName The name of the SysIdRoutineLog that this motor belongs
156 * to.
157 * @param logEntries The DataLog entries of the SysIdRoutineLog that this
158 * motor belongs to.
159 */
160 MotorLog(std::string_view motorName, std::string_view logName,
161 LogEntries* logEntries);
162 std::string m_motorName;
163 std::string m_logName;
164 LogEntries* m_logEntries;
165 };
166
167 /**
168 * Create a new logging utility for a SysId test routine.
169 *
170 * @param logName The name for the test routine in the log. Should be unique
171 * between complete test routines (quasistatic and dynamic, forward and
172 * reverse). The current state of this test (e.g. "quasistatic-forward")
173 * will appear in WPILog under the "sysid-test-state-logName" entry.
174 */
175 explicit SysIdRoutineLog(std::string_view logName);
176
177 /**
178 * Records the current state of the SysId test routine. Should be called once
179 * per iteration during tests with the type of the current test, and once upon
180 * test end with state `none`.
181 *
182 * @param state The current state of the SysId test routine.
183 */
184 void RecordState(State state);
185
186 /**
187 * Log data from a motor during a SysId routine.
188 *
189 * @param motorName The name of the motor.
190 * @return Handle with chainable callbacks to log individual data fields.
191 */
192 MotorLog Motor(std::string_view motorName);
193
194 static std::string StateEnumToString(State state);
195
196 private:
197 LogEntries m_logEntries;
198 std::string m_logName;
199 bool m_stateInitialized = false;
201};
202} // namespace wpi::sysid
@ name
Definition base.h:690
Log string values.
Definition DataLog.hpp:849
Logs data from a single motor during a SysIdRoutine.
Definition SysIdRoutineLog.hpp:49
MotorLog & position(wpi::units::meter_t position)
Log the linear position of the motor.
Definition SysIdRoutineLog.hpp:77
MotorLog & acceleration(wpi::units::turns_per_second_squared_t acceleration)
Log the angular acceleration of the motor.
Definition SysIdRoutineLog.hpp:132
MotorLog & current(wpi::units::ampere_t current)
Log the current applied to the motor.
Definition SysIdRoutineLog.hpp:145
friend class SysIdRoutineLog
Definition SysIdRoutineLog.hpp:150
MotorLog & velocity(wpi::units::meters_per_second_t velocity)
Log the linear velocity of the motor.
Definition SysIdRoutineLog.hpp:97
MotorLog & voltage(wpi::units::volt_t voltage)
Log the voltage applied to the motor.
Definition SysIdRoutineLog.hpp:67
MotorLog & velocity(wpi::units::turns_per_second_t velocity)
Log the angular velocity of the motor.
Definition SysIdRoutineLog.hpp:107
MotorLog & value(std::string_view name, double value, std::string_view unit)
Log a generic data value from the motor.
MotorLog & acceleration(wpi::units::meters_per_second_squared_t acceleration)
Log the linear acceleration of the motor.
Definition SysIdRoutineLog.hpp:119
MotorLog & position(wpi::units::turn_t position)
Log the angular position of the motor.
Definition SysIdRoutineLog.hpp:87
MotorLog Motor(std::string_view motorName)
Log data from a motor during a SysId routine.
static std::string StateEnumToString(State state)
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.
StringMap is a sorted associative container that contains key-value pairs with unique string keys.
Definition StringMap.hpp:26
Definition SysIdRoutineLog.hpp:20
State
Possible state of a SysId routine.
Definition SysIdRoutineLog.hpp:25
@ kNone
No test.
Definition SysIdRoutineLog.hpp:35
@ kQuasistaticForward
Quasistatic forward test.
Definition SysIdRoutineLog.hpp:27
@ kDynamicReverse
Dynamic reverse test.
Definition SysIdRoutineLog.hpp:33
@ kDynamicForward
Dynamic forward test.
Definition SysIdRoutineLog.hpp:31
@ kQuasistaticReverse
Quasistatic reverse test.
Definition SysIdRoutineLog.hpp:29