Loading [MathJax]/jax/output/HTML-CSS/config.js
CTRE Phoenix 6 C++ 25.4.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PositionVoltage.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
11#include <sstream>
12
13#include <units/frequency.h>
14#include <units/time.h>
15#include <units/angle.h>
16#include <units/angular_velocity.h>
17#include <units/voltage.h>
18
19namespace ctre {
20namespace phoenix6 {
21namespace controls {
22
23/**
24 * Request PID to target position with voltage feedforward
25 *
26 * This control mode will set the motor's position setpoint to the position specified by the user. In
27 * addition, it will apply an additional voltage as an arbitrary feedforward value.
28 */
30{
31 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, std::shared_ptr<ControlRequest> &req) const override
32 {
33 if (req.get() != this)
34 {
35 auto const reqCast = dynamic_cast<PositionVoltage *>(req.get());
36 if (reqCast != nullptr)
37 {
38 *reqCast = *this;
39 }
40 else
41 {
42 req = std::make_shared<PositionVoltage>(*this);
43 }
44 }
45
47 }
48
49public:
50 /**
51 * \brief Position to drive toward in rotations.
52 *
53 * - Units: rotations
54 *
55 */
56 units::angle::turn_t Position;
57 /**
58 * \brief Velocity to drive toward in rotations per second. This is typically
59 * used for motion profiles generated by the robot program.
60 *
61 * - Units: rotations per second
62 *
63 */
64 units::angular_velocity::turns_per_second_t Velocity = 0.0_tps;
65 /**
66 * \brief Set to true to use FOC commutation (requires Phoenix Pro), which
67 * increases peak power by ~15% on supported devices (see
68 * hardware#traits#SupportsFOC). Set to false to use trapezoidal commutation.
69 *
70 * FOC improves motor performance by leveraging torque (current) control.
71 * However, this may be inconvenient for applications that require specifying
72 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
73 * combines the performances gains of FOC while still allowing applications to
74 * provide duty cycle or voltage demand. This not to be confused with simple
75 * sinusoidal control or phase voltage control which lacks the performance
76 * gains.
77 */
78 bool EnableFOC = true;
79 /**
80 * \brief Feedforward to apply in volts
81 *
82 * - Units: Volts
83 *
84 */
85 units::voltage::volt_t FeedForward = 0.0_V;
86 /**
87 * \brief Select which gains are applied by selecting the slot. Use the
88 * configuration api to set the gain values for the selected slot before
89 * enabling this feature. Slot must be within [0,2].
90 */
91 int Slot = 0;
92 /**
93 * \brief Set to true to static-brake the rotor when output is zero (or within
94 * deadband). Set to false to use the NeutralMode configuration setting
95 * (default). This flag exists to provide the fundamental behavior of this
96 * control when output is zero, which is to provide 0V to the motor.
97 */
99 /**
100 * \brief Set to true to force forward limiting. This allows users to use other
101 * limit switch sensors connected to robot controller. This also allows use of
102 * active sensors that require external power.
103 */
104 bool LimitForwardMotion = false;
105 /**
106 * \brief Set to true to force reverse limiting. This allows users to use other
107 * limit switch sensors connected to robot controller. This also allows use of
108 * active sensors that require external power.
109 */
110 bool LimitReverseMotion = false;
111 /**
112 * \brief Set to true to ignore hardware limit switches and the
113 * LimitForwardMotion and LimitReverseMotion parameters, instead allowing
114 * motion.
115 *
116 * This can be useful on mechanisms such as an intake/feeder, where a limit
117 * switch stops motion while intaking but should be ignored when feeding to a
118 * shooter.
119 *
120 * The hardware limit faults and Forward/ReverseLimit signals will still report
121 * the values of the limit switches regardless of this parameter.
122 */
124 /**
125 * \brief Set to true to delay applying this control request until a timesync
126 * boundary (requires Phoenix Pro and CANivore). This eliminates the impact of
127 * nondeterministic network delays in exchange for a larger but deterministic
128 * control latency.
129 *
130 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
131 * Additionally, when this is enabled, the UpdateFreqHz of this request should
132 * be set to 0 Hz.
133 */
134 bool UseTimesync = false;
135
136 /**
137 * \brief The period at which this control will update at.
138 * This is designated in Hertz, with a minimum of 20 Hz
139 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
140 *
141 * If this field is set to 0 Hz, the control request will
142 * be sent immediately as a one-shot frame. This may be useful
143 * for advanced applications that require outputs to be
144 * synchronized with data acquisition. In this case, we
145 * recommend not exceeding 50 ms between control calls.
146 */
147 units::frequency::hertz_t UpdateFreqHz{100_Hz};
148
149 /**
150 * \brief Request PID to target position with voltage feedforward
151 *
152 * \details This control mode will set the motor's position setpoint to the
153 * position specified by the user. In addition, it will apply an
154 * additional voltage as an arbitrary feedforward value.
155 *
156 * \param Position Position to drive toward in rotations.
157 */
158 PositionVoltage(units::angle::turn_t Position) : ControlRequest{"PositionVoltage"},
159 Position{std::move(Position)}
160 {}
161
162 /**
163 * \brief Modifies this Control Request's Position parameter and returns itself for
164 * method-chaining and easier to use request API.
165 *
166 * Position to drive toward in rotations.
167 *
168 * - Units: rotations
169 *
170 *
171 * \param newPosition Parameter to modify
172 * \returns Itself
173 */
174 PositionVoltage &WithPosition(units::angle::turn_t newPosition)
175 {
176 Position = std::move(newPosition);
177 return *this;
178 }
179
180 /**
181 * \brief Modifies this Control Request's Velocity parameter and returns itself for
182 * method-chaining and easier to use request API.
183 *
184 * Velocity to drive toward in rotations per second. This is typically used for
185 * motion profiles generated by the robot program.
186 *
187 * - Units: rotations per second
188 *
189 *
190 * \param newVelocity Parameter to modify
191 * \returns Itself
192 */
193 PositionVoltage &WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
194 {
195 Velocity = std::move(newVelocity);
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 PositionVoltage &WithEnableFOC(bool newEnableFOC)
219 {
220 EnableFOC = std::move(newEnableFOC);
221 return *this;
222 }
223
224 /**
225 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
226 * method-chaining and easier to use request API.
227 *
228 * Feedforward to apply in volts
229 *
230 * - Units: Volts
231 *
232 *
233 * \param newFeedForward Parameter to modify
234 * \returns Itself
235 */
236 PositionVoltage &WithFeedForward(units::voltage::volt_t newFeedForward)
237 {
238 FeedForward = std::move(newFeedForward);
239 return *this;
240 }
241
242 /**
243 * \brief Modifies this Control Request's Slot parameter and returns itself for
244 * method-chaining and easier to use request API.
245 *
246 * Select which gains are applied by selecting the slot. Use the configuration
247 * api to set the gain values for the selected slot before enabling this
248 * feature. Slot must be within [0,2].
249 *
250 * \param newSlot Parameter to modify
251 * \returns Itself
252 */
254 {
255 Slot = std::move(newSlot);
256 return *this;
257 }
258
259 /**
260 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
261 * method-chaining and easier to use request API.
262 *
263 * Set to true to static-brake the rotor when output is zero (or within
264 * deadband). Set to false to use the NeutralMode configuration setting
265 * (default). This flag exists to provide the fundamental behavior of this
266 * control when output is zero, which is to provide 0V to the motor.
267 *
268 * \param newOverrideBrakeDurNeutral Parameter to modify
269 * \returns Itself
270 */
271 PositionVoltage &WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
272 {
273 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
274 return *this;
275 }
276
277 /**
278 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
279 * method-chaining and easier to use request API.
280 *
281 * Set to true to force forward limiting. This allows users to use other limit
282 * switch sensors connected to robot controller. This also allows use of active
283 * sensors that require external power.
284 *
285 * \param newLimitForwardMotion Parameter to modify
286 * \returns Itself
287 */
288 PositionVoltage &WithLimitForwardMotion(bool newLimitForwardMotion)
289 {
290 LimitForwardMotion = std::move(newLimitForwardMotion);
291 return *this;
292 }
293
294 /**
295 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
296 * method-chaining and easier to use request API.
297 *
298 * Set to true to force reverse limiting. This allows users to use other limit
299 * switch sensors connected to robot controller. This also allows use of active
300 * sensors that require external power.
301 *
302 * \param newLimitReverseMotion Parameter to modify
303 * \returns Itself
304 */
305 PositionVoltage &WithLimitReverseMotion(bool newLimitReverseMotion)
306 {
307 LimitReverseMotion = std::move(newLimitReverseMotion);
308 return *this;
309 }
310
311 /**
312 * \brief Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for
313 * method-chaining and easier to use request API.
314 *
315 * Set to true to ignore hardware limit switches and the LimitForwardMotion and
316 * LimitReverseMotion parameters, instead allowing motion.
317 *
318 * This can be useful on mechanisms such as an intake/feeder, where a limit
319 * switch stops motion while intaking but should be ignored when feeding to a
320 * shooter.
321 *
322 * The hardware limit faults and Forward/ReverseLimit signals will still report
323 * the values of the limit switches regardless of this parameter.
324 *
325 * \param newIgnoreHardwareLimits Parameter to modify
326 * \returns Itself
327 */
328 PositionVoltage &WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
329 {
330 IgnoreHardwareLimits = std::move(newIgnoreHardwareLimits);
331 return *this;
332 }
333
334 /**
335 * \brief Modifies this Control Request's UseTimesync parameter and returns itself for
336 * method-chaining and easier to use request API.
337 *
338 * Set to true to delay applying this control request until a timesync boundary
339 * (requires Phoenix Pro and CANivore). This eliminates the impact of
340 * nondeterministic network delays in exchange for a larger but deterministic
341 * control latency.
342 *
343 * This requires setting the ControlTimesyncFreqHz config in MotorOutputConfigs.
344 * Additionally, when this is enabled, the UpdateFreqHz of this request should
345 * be set to 0 Hz.
346 *
347 * \param newUseTimesync Parameter to modify
348 * \returns Itself
349 */
350 PositionVoltage &WithUseTimesync(bool newUseTimesync)
351 {
352 UseTimesync = std::move(newUseTimesync);
353 return *this;
354 }
355 /**
356 * \brief Sets the period at which this control will update at.
357 * This is designated in Hertz, with a minimum of 20 Hz
358 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
359 *
360 * If this field is set to 0 Hz, the control request will
361 * be sent immediately as a one-shot frame. This may be useful
362 * for advanced applications that require outputs to be
363 * synchronized with data acquisition. In this case, we
364 * recommend not exceeding 50 ms between control calls.
365 *
366 * \param newUpdateFreqHz Parameter to modify
367 * \returns Itself
368 */
369 PositionVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
370 {
371 UpdateFreqHz = newUpdateFreqHz;
372 return *this;
373 }
374 /**
375 * \brief Returns a string representation of the object.
376 *
377 * \returns a string representation of the object.
378 */
379 std::string ToString() const override
380 {
381 std::stringstream ss;
382 ss << "Control: PositionVoltage" << std::endl;
383 ss << " Position: " << Position.to<double>() << " rotations" << std::endl;
384 ss << " Velocity: " << Velocity.to<double>() << " rotations per second" << std::endl;
385 ss << " EnableFOC: " << EnableFOC << std::endl;
386 ss << " FeedForward: " << FeedForward.to<double>() << " Volts" << std::endl;
387 ss << " Slot: " << Slot << std::endl;
388 ss << " OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
389 ss << " LimitForwardMotion: " << LimitForwardMotion << std::endl;
390 ss << " LimitReverseMotion: " << LimitReverseMotion << std::endl;
391 ss << " IgnoreHardwareLimits: " << IgnoreHardwareLimits << std::endl;
392 ss << " UseTimesync: " << UseTimesync << std::endl;
393 return ss.str();
394 }
395
396 /**
397 * \brief Gets information about this control request.
398 *
399 * \returns Map of control parameter names and corresponding applied values
400 */
401 std::map<std::string, std::string> GetControlInfo() const override
402 {
403 std::map<std::string, std::string> controlInfo;
404 std::stringstream ss;
405 controlInfo["Name"] = GetName();
406 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
407 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
408 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
409 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
410 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
411 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
412 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
413 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
414 ss << IgnoreHardwareLimits; controlInfo["IgnoreHardwareLimits"] = ss.str(); ss.str(std::string{});
415 ss << UseTimesync; controlInfo["UseTimesync"] = ss.str(); ss.str(std::string{});
416 return controlInfo;
417 }
418};
419
420}
421}
422}
423
CTREXPORT int c_ctre_phoenix6_RequestControlPositionVoltage(const char *canbus, uint32_t ecuEncoding, double updateFrequency, double Position, double Velocity, bool EnableFOC, double FeedForward, int Slot, bool OverrideBrakeDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion, bool IgnoreHardwareLimits, bool UseTimesync)
Abstract Control Request class that other control requests extend for use.
Definition ControlRequest.hpp:30
std::string const & GetName() const
Definition ControlRequest.hpp:53
Request PID to target position with voltage feedforward.
Definition PositionVoltage.hpp:30
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition PositionVoltage.hpp:147
bool UseTimesync
Set to true to delay applying this control request until a timesync boundary (requires Phoenix Pro an...
Definition PositionVoltage.hpp:134
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15% on supp...
Definition PositionVoltage.hpp:78
PositionVoltage & WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
Modifies this Control Request's Velocity parameter and returns itself for method-chaining and easier ...
Definition PositionVoltage.hpp:193
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition PositionVoltage.hpp:110
PositionVoltage & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition PositionVoltage.hpp:253
PositionVoltage & WithFeedForward(units::voltage::volt_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition PositionVoltage.hpp:236
PositionVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition PositionVoltage.hpp:218
PositionVoltage & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition PositionVoltage.hpp:288
PositionVoltage & WithIgnoreHardwareLimits(bool newIgnoreHardwareLimits)
Modifies this Control Request's IgnoreHardwareLimits parameter and returns itself for method-chaining...
Definition PositionVoltage.hpp:328
std::string ToString() const override
Returns a string representation of the object.
Definition PositionVoltage.hpp:379
units::angle::turn_t Position
Position to drive toward in rotations.
Definition PositionVoltage.hpp:56
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition PositionVoltage.hpp:401
units::voltage::volt_t FeedForward
Feedforward to apply in volts.
Definition PositionVoltage.hpp:85
PositionVoltage & WithUseTimesync(bool newUseTimesync)
Modifies this Control Request's UseTimesync parameter and returns itself for method-chaining and easi...
Definition PositionVoltage.hpp:350
int Slot
Select which gains are applied by selecting the slot.
Definition PositionVoltage.hpp:91
bool IgnoreHardwareLimits
Set to true to ignore hardware limit switches and the LimitForwardMotion and LimitReverseMotion param...
Definition PositionVoltage.hpp:123
PositionVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition PositionVoltage.hpp:369
PositionVoltage & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition PositionVoltage.hpp:305
PositionVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition PositionVoltage.hpp:271
PositionVoltage & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition PositionVoltage.hpp:174
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition PositionVoltage.hpp:98
units::angular_velocity::turns_per_second_t Velocity
Velocity to drive toward in rotations per second.
Definition PositionVoltage.hpp:64
PositionVoltage(units::angle::turn_t Position)
Request PID to target position with voltage feedforward.
Definition PositionVoltage.hpp:158
bool LimitForwardMotion
Set to true to force forward limiting.
Definition PositionVoltage.hpp:104
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
Definition Diff_PositionDutyCycle_Position.hpp:15
Definition span.hpp:401