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