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