CTRE Phoenix C++ 5.33.1
VictorSPX.h
Go to the documentation of this file.
1/* Copyright (C) Cross The Road Electronics 2024 */
2#pragma once
3#if defined(WIN32) || defined(_WIN32) || defined(_WIN64)
4#pragma warning (push)
5#pragma warning (disable : 4250)
6#endif
7
10
11namespace ctre {
12namespace phoenix {
13namespace motorcontrol{
14namespace can {
15
16/**
17 * Configurables available to VictorSPX's PID
18 */
20 /**
21 * Feedback device for a particular PID loop.
22 */
24
27 {
28 }
29
30 /**
31 * @return String representation of all the configs
32 */
33 std::string toString() {
34 return toString("");
35 }
36
37 /**
38 * @param prependString
39 * String to prepend to all the configs
40 * @return String representation of all the configs
41 */
42 std::string toString(std::string prependString) {
43
44 std::string retstr = prependString + ".selectedFeedbackSensor = " + FeedbackDeviceRoutines::toString(selectedFeedbackSensor) + ";\n";
45 retstr += BasePIDSetConfiguration::toString(prependString);
46 return retstr;
47 }
48};
49
50/**
51 * Util class to help with VictorSPX's PID configs
52 */
54 private:
55 static VictorSPXPIDSetConfiguration _default;
56 public:
57 /**
58 * Determine if specified value is different from default
59 * @param settings settings to compare against
60 * @return if specified value is different from default
61 * @{
62 */
63 static bool SelectedFeedbackSensorDifferent (const VictorSPXPIDSetConfiguration & settings) { return (!(settings.selectedFeedbackSensor == _default.selectedFeedbackSensor)); }
65 /** @} */
66};
67
68/**
69 * Configurables available to VictorSPX
70 */
72 /**
73 * Primary PID configuration
74 */
76 /**
77 * Auxiliary PID configuration
78 */
80 /**
81 * Forward Limit Switch Source
82 *
83 * User can choose between the feedback connector, remote Talon SRX, CANifier, or deactivate the feature
84 */
86 /**
87 * Reverse Limit Switch Source
88 *
89 * User can choose between the feedback connector, remote Talon SRX, CANifier, or deactivate the feature
90 */
92 /**
93 * Forward limit switch device ID
94 *
95 * Limit Switch device id isn't used unless device is a remote
96 */
98 /**
99 * Reverse limit switch device ID
100 *
101 * Limit Switch device id isn't used unless device is a remote
102 */
104 /**
105 * Forward limit switch normally open/closed
106 */
108 /**
109 * Reverse limit switch normally open/closed
110 */
112 /**
113 * Feedback Device for Sum 0 Term
114 */
116 /**
117 * Feedback Device for Sum 1 Term
118 */
120 /**
121 * Feedback Device for Diff 0 Term
122 */
124 /**
125 * Feedback Device for Diff 1 Term
126 */
128
140 {
141 }
142
143 /**
144 * @return String representation of all the configs
145 */
146 std::string toString() {
147 return toString("");
148 }
149
150 /**
151 * @param prependString
152 * String to prepend to all the configs
153 * @return String representation of all the configs
154 */
155 std::string toString(std::string prependString) {
156 std::string retstr = primaryPID.toString(prependString + ".primaryPID");
157 retstr += auxiliaryPID.toString(prependString + ".auxiliaryPID");
158 retstr += prependString + ".forwardLimitSwitchSource = " + LimitSwitchRoutines::toString(forwardLimitSwitchSource) + ";\n";
159 retstr += prependString + ".reverseLimitSwitchSource = " + LimitSwitchRoutines::toString(reverseLimitSwitchSource) + ";\n";
160 retstr += prependString + ".forwardLimitSwitchDeviceID = " + std::to_string(forwardLimitSwitchDeviceID) + ";\n";
161 retstr += prependString + ".reverseLimitSwitchDeviceID = " + std::to_string(reverseLimitSwitchDeviceID) + ";\n";
162 retstr += prependString + ".forwardLimitSwitchNormal = " + LimitSwitchRoutines::toString(forwardLimitSwitchNormal) + ";\n";
163 retstr += prependString + ".reverseLimitSwitchNormal = " + LimitSwitchRoutines::toString(reverseLimitSwitchNormal) + ";\n";
164 retstr += prependString + ".sum0Term = " + FeedbackDeviceRoutines::toString(sum0Term) + ";\n";
165 retstr += prependString + ".sum1Term = " + FeedbackDeviceRoutines::toString(sum1Term) + ";\n";
166 retstr += prependString + ".diff0Term = " + FeedbackDeviceRoutines::toString(diff0Term) + ";\n";
167 retstr += prependString + ".diff1Term = " + FeedbackDeviceRoutines::toString(diff1Term) + ";\n";
168 retstr += BaseMotorControllerConfiguration::toString(prependString);
169
170 return retstr;
171 }
172};
173
174/**
175 * Util class to help with VictorSPX configs
176 */
178 private:
179 static struct VictorSPXConfiguration _default;
180 public:
181 /**
182 * Determine if specified value is different from default
183 * @param settings settings to compare against
184 * @return if specified value is different from default
185 * @{
186 */
187 static bool ForwardLimitSwitchSourceDifferent (const VictorSPXConfiguration & settings) { return (!(settings.forwardLimitSwitchSource == _default.forwardLimitSwitchSource)) || !settings.enableOptimizations; }
188 static bool ReverseLimitSwitchSourceDifferent (const VictorSPXConfiguration & settings) { return (!(settings.reverseLimitSwitchSource == _default.reverseLimitSwitchSource)) || !settings.enableOptimizations; }
191 static bool ForwardLimitSwitchNormalDifferent (const VictorSPXConfiguration & settings) { return (!(settings.forwardLimitSwitchNormal == _default.forwardLimitSwitchNormal)) || !settings.enableOptimizations; }
192 static bool ReverseLimitSwitchNormalDifferent (const VictorSPXConfiguration & settings) { return (!(settings.reverseLimitSwitchNormal == _default.reverseLimitSwitchNormal)) || !settings.enableOptimizations; }
193 static bool Sum0TermDifferent (const VictorSPXConfiguration & settings) { return (!(settings.sum0Term == _default.sum0Term)) || !settings.enableOptimizations; }
194 static bool Sum1TermDifferent (const VictorSPXConfiguration & settings) { return (!(settings.sum1Term == _default.sum1Term)) || !settings.enableOptimizations; }
195 static bool Diff0TermDifferent (const VictorSPXConfiguration & settings) { return (!(settings.diff0Term == _default.diff0Term)) || !settings.enableOptimizations; }
196 static bool Diff1TermDifferent (const VictorSPXConfiguration & settings) { return (!(settings.diff1Term == _default.diff1Term)) || !settings.enableOptimizations; }
197
200 }
203 }
204 /** @} */
205};
206
207/**
208 * VEX Victor SPX Motor Controller when used on CAN Bus.
209 *
210 * <pre>
211 * {@code
212 * // Example usage of a VictorSPX motor controller
213 * VictorSPX motor{0}; // creates a new VictorSPX with ID 0
214 *
215 * motor.Set(VictorSPXControlMode::PercentOutput, 0.5); // runs the motor at 50% power
216 *
217 * std::cout << motor.GetMotorOutputPercent() << std::endl; // prints the percent output of the motor (0.5)
218 * std::cout << motor.GetBusVoltage() << std::endl; // prints the bus voltage seen by the motor controller
219 *
220 * ErrorCode error = motor.GetLastError(); // gets the last error generated by the motor controller
221 * Faults faults;
222 * ErrorCode faultsError = motor.GetFaults(faults); // fills faults with the current motor controller faults; returns the last error generated
223 * }
224 * </pre>
225 */
228
229private:
230 /**
231 * Gets all PID set persistant settings.
232 *
233 * @param pid Object with all of the PID set persistant settings
234 * @param pidIdx 0 for Primary closed-loop. 1 for auxiliary closed-loop.
235 * @param timeoutMs
236 * Timeout value in ms. If nonzero, function will wait for
237 * config success and report an error if it times out.
238 * If zero, no blocking or checking is performed.
239 * @param enableOptimizations Enable the optimization technique
240 */
241 ctre::phoenix::ErrorCode ConfigurePID(const VictorSPXPIDSetConfiguration &pid, int pidIdx, int timeoutMs, bool enableOptimizations);
242public:
243 /**
244 * Constructor
245 *
246 * @param deviceNumber
247 * [0,62]
248 */
249 VictorSPX(int deviceNumber);
250
251#ifndef __FRC_ROBORIO__
252 /**
253 * Constructor so non-FRC platforms can specify a CAN 2.0 socketcan bus
254 * @param deviceNumber CAN Device ID of VictorSPX
255 * @param canbus String specifying the bus
256 */
257 VictorSPX(int deviceNumber, std::string const &canbus);
258#endif
259
260 virtual ~VictorSPX() {
261 }
262 VictorSPX(VictorSPX const&) = delete;
263 VictorSPX& operator=(VictorSPX const&) = delete;
264
265 // ------ Set output routines. ----------//
266 /**
267 * Sets the appropriate output on the motor controller, depending on the mode.
268 * @param mode The output mode to apply.
269 * In PercentOutput, the output is between -1.0 and 1.0, with 0.0 as stopped.
270 * In Velocity mode, output value is in position change / 100ms.
271 * In Position mode, output value is in encoder ticks or an analog value,
272 * depending on the sensor.
273 * In Follower mode, the output value is the integer device ID of the motor controller to
274 * duplicate.
275 *
276 * @param value The setpoint value, as described above.
277 *
278 *
279 * Standard Driving Example:
280 * victorLeft.set(ControlMode.PercentOutput, leftJoy);
281 * victorRght.set(ControlMode.PercentOutput, rghtJoy);
282 */
284 /**
285 * @param mode Sets the appropriate output on the motor controller, depending on the mode.
286 * @param demand0 The output value to apply.
287 * such as advanced feed forward and/or auxiliary close-looping in firmware.
288 * In PercentOutput, the output is between -1.0 and 1.0, with 0.0 as stopped.
289 * In Velocity mode, output value is in position change / 100ms.
290 * In Position mode, output value is in encoder ticks or an analog value,
291 * depending on the sensor. See
292 * In Follower mode, the output value is the integer device ID of the motor controller to
293 * duplicate.
294 *
295 * @param demand1Type The demand type for demand1.
296 * Neutral: Ignore demand1 and apply no change to the demand0 output.
297 * AuxPID: Use demand1 to set the target for the auxiliary PID 1. Auxiliary
298 * PID is always executed as standard Position PID control.
299 * ArbitraryFeedForward: Use demand1 as an arbitrary additive value to the
300 * demand0 output. In PercentOutput the demand0 output is the motor output,
301 * and in closed-loop modes the demand0 output is the output of PID0.
302 * @param demand1 Supplmental output value.
303 * AuxPID: Target position in Sensor Units
304 * ArbitraryFeedForward: Percent Output between -1.0 and 1.0
305 *
306 *
307 * Arcade Drive Example:
308 * _victorLeft.set(ControlMode.PercentOutput, joyForward, DemandType.ArbitraryFeedForward, +joyTurn);
309 * _victorRght.set(ControlMode.PercentOutput, joyForward, DemandType.ArbitraryFeedForward, -joyTurn);
310 *
311 * Drive Straight Example:
312 * Note: Selected Sensor Configuration is necessary for both PID0 and PID1.
313 * _victorLeft.follow(_victorRght, FollwerType.AuxOutput1);
314 * _victorRght.set(ControlMode.PercentOutput, joyForward, DemandType.AuxPID, desiredRobotHeading);
315 *
316 * Drive Straight to a Distance Example:
317 * Note: Other configurations (sensor selection, PID gains, etc.) need to be set.
318 * _victorLeft.follow(_victorRght, FollwerType.AuxOutput1);
319 * _victorRght.set(ControlMode.MotionMagic, targetDistance, DemandType.AuxPID, desiredRobotHeading);
320 */
322
323 //------ Simulation API ----------//
324 /**
325 * @return object that can set simulation inputs.
326 */
328
329 //------ All Configs ----------//
330 /**
331 * Gets all PID set persistant settings.
332 *
333 * @param pid Object with all of the PID set persistant settings
334 * @param pidIdx 0 for Primary closed-loop. 1 for auxiliary closed-loop.
335 * @param timeoutMs
336 * Timeout value in ms. If nonzero, function will wait for
337 * config success and report an error if it times out.
338 * If zero, no blocking or checking is performed.
339 */
340 void GetPIDConfigs(VictorSPXPIDSetConfiguration &pid, int pidIdx = 0, int timeoutMs = 50);
341 /**
342 * Configures all persistent settings.
343 *
344 * @param allConfigs Object with all of the persistant settings
345 * @param timeoutMs
346 * Timeout value in ms. If nonzero, function will wait for
347 * config success and report an error if it times out.
348 * If zero, no blocking or checking is performed.
349 *
350 * @return Error Code generated by function. 0 indicates no error.
351 */
352 virtual ctre::phoenix::ErrorCode ConfigAllSettings(const VictorSPXConfiguration &allConfigs, int timeoutMs = 50);
353 /**
354 * Gets all persistant settings.
355 *
356 * @param allConfigs Object with all of the persistant settings
357 * @param timeoutMs
358 * Timeout value in ms. If nonzero, function will wait for
359 * config success and report an error if it times out.
360 * If zero, no blocking or checking is performed.
361 */
362 virtual void GetAllConfigs(VictorSPXConfiguration &allConfigs, int timeoutMs = 50);
363
364 /* ----- virtual re-directs ------- */
365 virtual void Set(ctre::phoenix::motorcontrol::ControlMode mode, double value);
366 virtual void Set(ctre::phoenix::motorcontrol::ControlMode mode, double demand0, ctre::phoenix::motorcontrol::DemandType demand1Type, double demand1);
367
368};// class VictorSPX
369} // namespace can
370} // namespace motorcontrol
371} // namespace phoenix
372} // namespace ctre
373
374#if defined(WIN32) || defined(_WIN32) || defined(_WIN64)
375#pragma warning (pop)
376#endif
static std::string toString(FeedbackDevice value)
Gets the string representation of selected feedback device.
Definition: FeedbackDevice.h:260
Interface for motor controllers.
Definition: IMotorController.h:35
static std::string toString(LimitSwitchSource value)
Definition: LimitSwitchType.h:110
Collection of simulation commands available to a VictorSPX motor controller.
Definition: VictorSPXSimCollection.h:26
Base motor controller features for all CTRE CAN motor controllers.
Definition: BaseMotorController.h:578
Util class to help with VictorSPX configs.
Definition: VictorSPX.h:177
static bool ReverseLimitSwitchDifferent(const VictorSPXConfiguration &settings)
Definition: VictorSPX.h:201
static bool ReverseLimitSwitchSourceDifferent(const VictorSPXConfiguration &settings)
Definition: VictorSPX.h:188
static bool ForwardLimitSwitchDeviceIDDifferent(const VictorSPXConfiguration &settings)
Definition: VictorSPX.h:189
static bool Diff1TermDifferent(const VictorSPXConfiguration &settings)
Definition: VictorSPX.h:196
static bool Sum1TermDifferent(const VictorSPXConfiguration &settings)
Definition: VictorSPX.h:194
static bool ForwardLimitSwitchNormalDifferent(const VictorSPXConfiguration &settings)
Definition: VictorSPX.h:191
static bool ForwardLimitSwitchDifferent(const VictorSPXConfiguration &settings)
Definition: VictorSPX.h:198
static bool Diff0TermDifferent(const VictorSPXConfiguration &settings)
Definition: VictorSPX.h:195
static bool ForwardLimitSwitchSourceDifferent(const VictorSPXConfiguration &settings)
Determine if specified value is different from default.
Definition: VictorSPX.h:187
static bool ReverseLimitSwitchDeviceIDDifferent(const VictorSPXConfiguration &settings)
Definition: VictorSPX.h:190
static bool Sum0TermDifferent(const VictorSPXConfiguration &settings)
Definition: VictorSPX.h:193
static bool ReverseLimitSwitchNormalDifferent(const VictorSPXConfiguration &settings)
Definition: VictorSPX.h:192
VEX Victor SPX Motor Controller when used on CAN Bus.
Definition: VictorSPX.h:227
virtual ctre::phoenix::ErrorCode ConfigAllSettings(const VictorSPXConfiguration &allConfigs, int timeoutMs=50)
Configures all persistent settings.
virtual void GetAllConfigs(VictorSPXConfiguration &allConfigs, int timeoutMs=50)
Gets all persistant settings.
void Set(ctre::phoenix::motorcontrol::VictorSPXControlMode mode, double value)
Sets the appropriate output on the motor controller, depending on the mode.
VictorSPX(int deviceNumber)
Constructor.
virtual void Set(ctre::phoenix::motorcontrol::ControlMode mode, double value)
Sets the appropriate output on the talon, depending on the mode.
virtual ~VictorSPX()
Definition: VictorSPX.h:260
ctre::phoenix::motorcontrol::VictorSPXSimCollection & GetSimCollection()
void Set(ctre::phoenix::motorcontrol::VictorSPXControlMode mode, double demand0, ctre::phoenix::motorcontrol::DemandType demand1Type, double demand1)
VictorSPX(int deviceNumber, std::string const &canbus)
Constructor so non-FRC platforms can specify a CAN 2.0 socketcan bus.
virtual void Set(ctre::phoenix::motorcontrol::ControlMode mode, double demand0, ctre::phoenix::motorcontrol::DemandType demand1Type, double demand1)
VictorSPX & operator=(VictorSPX const &)=delete
void GetPIDConfigs(VictorSPXPIDSetConfiguration &pid, int pidIdx=0, int timeoutMs=50)
Gets all PID set persistant settings.
ControlMode
Choose the control mode for a motor controller.
Definition: ControlMode.h:11
DemandType
How to interpret a demand value.
Definition: DemandType.h:10
@ None
Same as SetInverted(false)
LimitSwitchNormal
Choose whether the limit switch is normally open or normally closed.
Definition: LimitSwitchType.h:62
@ LimitSwitchNormal_NormallyOpen
Limit Switch is tripped when the circuit is closed.
Definition: LimitSwitchType.h:67
RemoteFeedbackDevice
Choose the remote feedback device for a motor controller.
Definition: FeedbackDevice.h:179
RemoteLimitSwitchSource
Remote Limit switch source enum.
Definition: LimitSwitchType.h:35
@ RemoteLimitSwitchSource_Deactivated
Don't use a limit switch.
Definition: LimitSwitchType.h:55
VictorSPXControlMode
Choose the control mode for a Victor SPX.
Definition: ControlMode.h:156
ErrorCode
Definition: ErrorCode.h:13
namespace ctre
Definition: paramEnum.h:5
bool enableOptimizations
Enable optimizations for ConfigAll (defaults true)
Definition: CustomParamConfiguration.h:23
Configurables available to base motor controllers.
Definition: BaseMotorController.h:276
std::string toString()
Definition: BaseMotorController.h:474
Base set of configurables related to PID.
Definition: BaseMotorController.h:50
double selectedFeedbackCoefficient
Feedback coefficient of selected sensor.
Definition: BaseMotorController.h:55
std::string toString()
Definition: BaseMotorController.h:65
Configurables available to VictorSPX.
Definition: VictorSPX.h:71
int reverseLimitSwitchDeviceID
Reverse limit switch device ID.
Definition: VictorSPX.h:103
VictorSPXPIDSetConfiguration auxiliaryPID
Auxiliary PID configuration.
Definition: VictorSPX.h:79
std::string toString(std::string prependString)
Definition: VictorSPX.h:155
LimitSwitchNormal forwardLimitSwitchNormal
Forward limit switch normally open/closed.
Definition: VictorSPX.h:107
VictorSPXPIDSetConfiguration primaryPID
Primary PID configuration.
Definition: VictorSPX.h:75
RemoteFeedbackDevice diff0Term
Feedback Device for Diff 0 Term.
Definition: VictorSPX.h:123
RemoteFeedbackDevice diff1Term
Feedback Device for Diff 1 Term.
Definition: VictorSPX.h:127
LimitSwitchNormal reverseLimitSwitchNormal
Reverse limit switch normally open/closed.
Definition: VictorSPX.h:111
int forwardLimitSwitchDeviceID
Forward limit switch device ID.
Definition: VictorSPX.h:97
RemoteLimitSwitchSource reverseLimitSwitchSource
Reverse Limit Switch Source.
Definition: VictorSPX.h:91
RemoteFeedbackDevice sum0Term
Feedback Device for Sum 0 Term.
Definition: VictorSPX.h:115
RemoteFeedbackDevice sum1Term
Feedback Device for Sum 1 Term.
Definition: VictorSPX.h:119
std::string toString()
Definition: VictorSPX.h:146
RemoteLimitSwitchSource forwardLimitSwitchSource
Forward Limit Switch Source.
Definition: VictorSPX.h:85
Util class to help with VictorSPX's PID configs.
Definition: VictorSPX.h:53
static bool SelectedFeedbackCoefficientDifferent(const VictorSPXPIDSetConfiguration &settings)
Definition: VictorSPX.h:64
static bool SelectedFeedbackSensorDifferent(const VictorSPXPIDSetConfiguration &settings)
Determine if specified value is different from default.
Definition: VictorSPX.h:63
Configurables available to VictorSPX's PID.
Definition: VictorSPX.h:19
std::string toString()
Definition: VictorSPX.h:33
std::string toString(std::string prependString)
Definition: VictorSPX.h:42
RemoteFeedbackDevice selectedFeedbackSensor
Feedback device for a particular PID loop.
Definition: VictorSPX.h:23