CTRE Phoenix 6 C++ 26.0.0-beta-1
Loading...
Searching...
No Matches
DifferentialVoltage.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
10#include <units/frequency.h>
11#include <units/time.h>
12#include <units/voltage.h>
13#include <units/angle.h>
14
15namespace ctre {
16namespace phoenix6 {
17namespace controls {
18
19/**
20 * Request a specified voltage with a differential position closed-loop.
21 *
22 * This control mode will attempt to apply the specified voltage to the motor. If the supply voltage is below
23 * the requested voltage, the motor controller will output the supply voltage. It will also set the motor's
24 * differential position setpoint to the specified position.
25 */
26class DifferentialVoltage final : public ControlRequest {
27 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, std::shared_ptr<ControlRequest> &req) const override;
28
29public:
30 /**
31 * \brief Voltage to attempt to drive at on the Average axis.
32 *
33 * - Units: Volts
34 *
35 */
36 units::voltage::volt_t AverageOutput;
37 /**
38 * \brief Differential position to drive towards in rotations.
39 *
40 * - Units: rotations
41 *
42 */
43 units::angle::turn_t DifferentialPosition;
44 /**
45 * \brief Set to true to use FOC commutation (requires Phoenix Pro), which
46 * increases peak power by ~15% on supported devices (see
47 * hardware#traits#SupportsFOC). Set to false to use trapezoidal commutation.
48 *
49 * FOC improves motor performance by leveraging torque (current) control.
50 * However, this may be inconvenient for applications that require specifying
51 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
52 * combines the performances gains of FOC while still allowing applications to
53 * provide duty cycle or voltage demand. This not to be confused with simple
54 * sinusoidal control or phase voltage control which lacks the performance
55 * gains.
56 */
57 bool EnableFOC = true;
58 /**
59 * \brief Select which gains are applied to the differential controller by
60 * selecting the slot. Use the configuration api to set the gain values for the
61 * selected slot before enabling this feature. Slot must be within [0,2].
62 */
64 /**
65 * \brief Set to true to static-brake the rotor when output is zero (or within
66 * deadband). Set to false to use the NeutralMode configuration setting
67 * (default). This flag exists to provide the fundamental behavior of this
68 * control when output is zero, which is to provide 0V to the motor.
69 */
71 /**
72 * \brief Set to true to force forward limiting. This allows users to use other
73 * limit switch sensors connected to robot controller. This also allows use of
74 * active sensors that require external power.
75 */
76 bool LimitForwardMotion = false;
77 /**
78 * \brief Set to true to force reverse limiting. This allows users to use other
79 * limit switch sensors connected to robot controller. This also allows use of
80 * active sensors that require external power.
81 */
82 bool LimitReverseMotion = false;
83 /**
84 * \brief Set to true to ignore hardware limit switches and the
85 * LimitForwardMotion and LimitReverseMotion parameters, instead allowing
86 * motion.
87 *
88 * This can be useful on mechanisms such as an intake/feeder, where a limit
89 * switch stops motion while intaking but should be ignored when feeding to a
90 * shooter.
91 *
92 * The hardware limit faults and Forward/ReverseLimit signals will still report
93 * the values of the limit switches regardless of this parameter.
94 */
96 /**
97 * \brief Set to true to ignore software limits, instead allowing motion.
98 *
99 * This can be useful when calibrating the zero point of a mechanism such as an
100 * elevator.
101 *
102 * The software limit faults will still report the values of the software limits
103 * regardless of this parameter.
104 */
106 /**
107 * \brief Set to true to delay applying this control request until a timesync
108 * boundary (requires Phoenix Pro and CANivore). This eliminates the impact of
109 * nondeterministic network delays in exchange for a larger but deterministic
110 * control latency.
111 *
112 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
113 * Additionally, when this is enabled, the UpdateFreqHz of this request should
114 * be set to 0 Hz.
115 */
116 bool UseTimesync = false;
117
118 /**
119 * \brief The frequency at which this control will update.
120 * This is designated in Hertz, with a minimum of 20 Hz
121 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
122 * Some update frequencies are not supported and will be
123 * promoted up to the next highest supported frequency.
124 *
125 * If this field is set to 0 Hz, the control request will
126 * be sent immediately as a one-shot frame. This may be useful
127 * for advanced applications that require outputs to be
128 * synchronized with data acquisition. In this case, we
129 * recommend not exceeding 50 ms between control calls.
130 */
131 units::frequency::hertz_t UpdateFreqHz{100_Hz};
132
133 /**
134 * \brief Request a specified voltage with a differential position closed-loop.
135 *
136 * \details This control mode will attempt to apply the specified voltage to the
137 * motor. If the supply voltage is below the requested voltage, the
138 * motor controller will output the supply voltage. It will also set
139 * the motor's differential position setpoint to the specified
140 * position.
141 *
142 * \param AverageOutput Voltage to attempt to drive at on the Average axis.
143 * \param DifferentialPosition Differential position to drive towards in
144 * rotations.
145 */
146 constexpr DifferentialVoltage(units::voltage::volt_t AverageOutput, units::angle::turn_t DifferentialPosition) : ControlRequest{},
147 AverageOutput{std::move(AverageOutput)},
149 {}
150
151 constexpr ~DifferentialVoltage() override {}
152
153 /**
154 * \brief Gets the name of this control request.
155 *
156 * \returns Name of the control request
157 */
158 constexpr std::string_view GetName() const override
159 {
160 return "DifferentialVoltage";
161 }
162
163 /**
164 * \brief Modifies this Control Request's AverageOutput parameter and returns itself for
165 * method-chaining and easier to use request API.
166 *
167 * Voltage to attempt to drive at on the Average axis.
168 *
169 * - Units: Volts
170 *
171 *
172 * \param newAverageOutput Parameter to modify
173 * \returns Itself
174 */
175 constexpr DifferentialVoltage &WithAverageOutput(units::voltage::volt_t newAverageOutput)
176 {
177 AverageOutput = std::move(newAverageOutput);
178 return *this;
179 }
180
181 /**
182 * \brief Modifies this Control Request's DifferentialPosition parameter and returns itself for
183 * method-chaining and easier to use request API.
184 *
185 * Differential position to drive towards in rotations.
186 *
187 * - Units: rotations
188 *
189 *
190 * \param newDifferentialPosition Parameter to modify
191 * \returns Itself
192 */
193 constexpr DifferentialVoltage &WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
194 {
195 DifferentialPosition = std::move(newDifferentialPosition);
196 return *this;
197 }
198
199 /**
200 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
201 * method-chaining and easier to use request API.
202 *
203 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
204 * peak power by ~15% on supported devices (see hardware#traits#SupportsFOC).
205 * Set to false to use trapezoidal commutation.
206 *
207 * FOC improves motor performance by leveraging torque (current) control.
208 * However, this may be inconvenient for applications that require specifying
209 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
210 * combines the performances gains of FOC while still allowing applications to
211 * provide duty cycle or voltage demand. This not to be confused with simple
212 * sinusoidal control or phase voltage control which lacks the performance
213 * gains.
214 *
215 * \param newEnableFOC Parameter to modify
216 * \returns Itself
217 */
218 constexpr DifferentialVoltage &WithEnableFOC(bool newEnableFOC)
219 {
220 EnableFOC = std::move(newEnableFOC);
221 return *this;
222 }
223
224 /**
225 * \brief Modifies this Control Request's DifferentialSlot parameter and returns itself for
226 * method-chaining and easier to use request API.
227 *
228 * Select which gains are applied to the differential controller by selecting
229 * the slot. Use the configuration api to set the gain values for the selected
230 * slot before enabling this feature. Slot must be within [0,2].
231 *
232 * \param newDifferentialSlot Parameter to modify
233 * \returns Itself
234 */
235 constexpr DifferentialVoltage &WithDifferentialSlot(int newDifferentialSlot)
236 {
237 DifferentialSlot = std::move(newDifferentialSlot);
238 return *this;
239 }
240
241 /**
242 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
243 * method-chaining and easier to use request API.
244 *
245 * Set to true to static-brake the rotor when output is zero (or within
246 * deadband). Set to false to use the NeutralMode configuration setting
247 * (default). This flag exists to provide the fundamental behavior of this
248 * control when output is zero, which is to provide 0V to the motor.
249 *
250 * \param newOverrideBrakeDurNeutral Parameter to modify
251 * \returns Itself
252 */
253 constexpr DifferentialVoltage &WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
254 {
255 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
256 return *this;
257 }
258
259 /**
260 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
261 * method-chaining and easier to use request API.
262 *
263 * Set to true to force forward limiting. This allows users to use other limit
264 * switch sensors connected to robot controller. This also allows use of active
265 * sensors that require external power.
266 *
267 * \param newLimitForwardMotion Parameter to modify
268 * \returns Itself
269 */
270 constexpr DifferentialVoltage &WithLimitForwardMotion(bool newLimitForwardMotion)
271 {
272 LimitForwardMotion = std::move(newLimitForwardMotion);
273 return *this;
274 }
275
276 /**
277 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
278 * method-chaining and easier to use request API.
279 *
280 * Set to true to force reverse limiting. This allows users to use other limit
281 * switch sensors connected to robot controller. This also allows use of active
282 * sensors that require external power.
283 *
284 * \param newLimitReverseMotion Parameter to modify
285 * \returns Itself
286 */
287 constexpr DifferentialVoltage &WithLimitReverseMotion(bool newLimitReverseMotion)
288 {
289 LimitReverseMotion = std::move(newLimitReverseMotion);
290 return *this;
291 }
292
293 /**
294 * \brief Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for
295 * method-chaining and easier to use request API.
296 *
297 * Set to true to ignore hardware limit switches and the LimitForwardMotion and
298 * LimitReverseMotion parameters, instead allowing motion.
299 *
300 * This can be useful on mechanisms such as an intake/feeder, where a limit
301 * switch stops motion while intaking but should be ignored when feeding to a
302 * shooter.
303 *
304 * The hardware limit faults and Forward/ReverseLimit signals will still report
305 * the values of the limit switches regardless of this parameter.
306 *
307 * \param newIgnoreHardwareLimits Parameter to modify
308 * \returns Itself
309 */
310 constexpr DifferentialVoltage &WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
311 {
312 IgnoreHardwareLimits = std::move(newIgnoreHardwareLimits);
313 return *this;
314 }
315
316 /**
317 * \brief Modifies this Control Request's IgnoreSoftwareLimits parameter and returns itself for
318 * method-chaining and easier to use request API.
319 *
320 * Set to true to ignore software limits, instead allowing motion.
321 *
322 * This can be useful when calibrating the zero point of a mechanism such as an
323 * elevator.
324 *
325 * The software limit faults will still report the values of the software limits
326 * regardless of this parameter.
327 *
328 * \param newIgnoreSoftwareLimits Parameter to modify
329 * \returns Itself
330 */
331 constexpr DifferentialVoltage &WithIgnoreSoftwareLimits(bool newIgnoreSoftwareLimits)
332 {
333 IgnoreSoftwareLimits = std::move(newIgnoreSoftwareLimits);
334 return *this;
335 }
336
337 /**
338 * \brief Modifies this Control Request's UseTimesync parameter and returns itself for
339 * method-chaining and easier to use request API.
340 *
341 * Set to true to delay applying this control request until a timesync boundary
342 * (requires Phoenix Pro and CANivore). This eliminates the impact of
343 * nondeterministic network delays in exchange for a larger but deterministic
344 * control latency.
345 *
346 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
347 * Additionally, when this is enabled, the UpdateFreqHz of this request should
348 * be set to 0 Hz.
349 *
350 * \param newUseTimesync Parameter to modify
351 * \returns Itself
352 */
353 constexpr DifferentialVoltage &WithUseTimesync(bool newUseTimesync)
354 {
355 UseTimesync = std::move(newUseTimesync);
356 return *this;
357 }
358
359 /**
360 * \brief Sets the frequency at which this control will update.
361 * This is designated in Hertz, with a minimum of 20 Hz
362 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
363 * Some update frequencies are not supported and will be
364 * promoted up to the next highest supported frequency.
365 *
366 * If this field is set to 0 Hz, the control request will
367 * be sent immediately as a one-shot frame. This may be useful
368 * for advanced applications that require outputs to be
369 * synchronized with data acquisition. In this case, we
370 * recommend not exceeding 50 ms between control calls.
371 *
372 * \param newUpdateFreqHz Parameter to modify
373 * \returns Itself
374 */
375 constexpr DifferentialVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
376 {
377 UpdateFreqHz = newUpdateFreqHz;
378 return *this;
379 }
380
381 /**
382 * \brief Returns a string representation of the object.
383 *
384 * \returns a string representation of the object.
385 */
386 std::string ToString() const override;
387
388 /**
389 * \brief Gets information about this control request.
390 *
391 * \returns Map of control parameter names and corresponding applied values
392 */
393 std::map<std::string, std::string> GetControlInfo() const override;
394};
395
396}
397}
398}
399
Common interface implemented by all control requests.
Definition ControlRequest.hpp:27
Request a specified voltage with a differential position closed-loop.
Definition DifferentialVoltage.hpp:26
bool IgnoreHardwareLimits
Set to true to ignore hardware limit switches and the LimitForwardMotion and LimitReverseMotion param...
Definition DifferentialVoltage.hpp:95
constexpr ~DifferentialVoltage() override
Definition DifferentialVoltage.hpp:151
units::voltage::volt_t AverageOutput
Voltage to attempt to drive at on the Average axis.
Definition DifferentialVoltage.hpp:36
constexpr DifferentialVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition DifferentialVoltage.hpp:218
constexpr DifferentialVoltage & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition DifferentialVoltage.hpp:287
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition DifferentialVoltage.hpp:116
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15% on supp...
Definition DifferentialVoltage.hpp:57
bool LimitForwardMotion
Set to true to force forward limiting.
Definition DifferentialVoltage.hpp:76
units::angle::turn_t DifferentialPosition
Differential position to drive towards in rotations.
Definition DifferentialVoltage.hpp:43
int DifferentialSlot
Select which gains are applied to the differential controller by selecting the slot.
Definition DifferentialVoltage.hpp:63
constexpr DifferentialVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the frequency at which this control will update.
Definition DifferentialVoltage.hpp:375
constexpr DifferentialVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition DifferentialVoltage.hpp:253
constexpr DifferentialVoltage(units::voltage::volt_t AverageOutput, units::angle::turn_t DifferentialPosition)
Request a specified voltage with a differential position closed-loop.
Definition DifferentialVoltage.hpp:146
bool IgnoreSoftwareLimits
Set to true to ignore software limits, instead allowing motion.
Definition DifferentialVoltage.hpp:105
constexpr std::string_view GetName() const override
Gets the name of this control request.
Definition DifferentialVoltage.hpp:158
units::frequency::hertz_t UpdateFreqHz
The frequency at which this control will update.
Definition DifferentialVoltage.hpp:131
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition DifferentialVoltage.hpp:82
constexpr DifferentialVoltage & WithAverageOutput(units::voltage::volt_t newAverageOutput)
Modifies this Control Request's AverageOutput parameter and returns itself for method-chaining and ea...
Definition DifferentialVoltage.hpp:175
std::string ToString() const override
Returns a string representation of the object.
constexpr DifferentialVoltage & WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
Modifies this Control Request's DifferentialPosition parameter and returns itself for method-chaining...
Definition DifferentialVoltage.hpp:193
constexpr DifferentialVoltage & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition DifferentialVoltage.hpp:270
constexpr DifferentialVoltage & WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for method-chaining...
Definition DifferentialVoltage.hpp:310
constexpr DifferentialVoltage & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition DifferentialVoltage.hpp:353
constexpr DifferentialVoltage & WithDifferentialSlot(int newDifferentialSlot)
Modifies this Control Request's DifferentialSlot parameter and returns itself for method-chaining and...
Definition DifferentialVoltage.hpp:235
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
constexpr DifferentialVoltage & WithIgnoreSoftwareLimits(bool newIgnoreSoftwareLimits)
Modifies this Control Request's IgnoreSoftwareLimits parameter and returns itself for method-chaining...
Definition DifferentialVoltage.hpp:331
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition DifferentialVoltage.hpp:70
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:28
Definition motor_constants.h:14