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