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