CTRE Phoenix 6 C++ 24.2.0
DifferentialPositionVoltage.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/frequency.h>
15#include <units/time.h>
16
17
18namespace ctre {
19namespace phoenix6 {
20namespace controls {
21
22/**
23 * Request PID to target position with a differential position setpoint
24 *
25 * This control mode will set the motor's position setpoint to the position specified by the user. It will
26 * also set the motor's differential position setpoint to the specified position.
27 */
29{
30 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
31 {
32 if (req.get() != this)
33 {
34 auto const reqCast = dynamic_cast<DifferentialPositionVoltage *>(req.get());
35 if (reqCast != nullptr)
36 {
37 *reqCast = *this;
38 }
39 else
40 {
41 req = std::make_shared<DifferentialPositionVoltage>(*this);
42 }
43 }
44
46 }
47
48public:
49 /**
50 * Average position to drive toward in rotations.
51 */
52 units::angle::turn_t TargetPosition;
53 /**
54 * Differential position to drive toward in rotations.
55 */
56 units::angle::turn_t DifferentialPosition;
57 /**
58 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
59 * peak power by ~15%. Set to false to use trapezoidal commutation. FOC
60 * improves motor performance by leveraging torque (current) control. However,
61 * this may be inconvenient for applications that require specifying duty cycle
62 * or voltage. CTR-Electronics has developed a hybrid method that combines the
63 * performances gains of FOC while still allowing applications to provide duty
64 * cycle or voltage demand. This not to be confused with simple sinusoidal
65 * control or phase voltage control which lacks the performance gains.
66 */
68 /**
69 * Select which gains are applied to the primary controller by selecting the
70 * slot. Use the configuration api to set the gain values for the selected slot
71 * before enabling this feature. Slot must be within [0,2].
72 */
74 /**
75 * Select which gains are applied to the differential controller by selecting
76 * the slot. Use the configuration api to set the gain values for the selected
77 * slot before enabling this feature. Slot must be within [0,2].
78 */
80 /**
81 * Set to true to static-brake the rotor when output is zero (or within
82 * deadband). Set to false to use the NeutralMode configuration setting
83 * (default). This flag exists to provide the fundamental behavior of this
84 * control when output is zero, which is to provide 0V to the motor.
85 */
87 /**
88 * Set to true to force forward limiting. This allows users to use other limit
89 * switch sensors connected to robot controller. This also allows use of active
90 * sensors that require external power.
91 */
93 /**
94 * Set to true to force reverse limiting. This allows users to use other limit
95 * switch sensors connected to robot controller. This also allows use of active
96 * sensors that require external power.
97 */
99
100 /**
101 * \brief The period at which this control will update at.
102 * This is designated in Hertz, with a minimum of 20 Hz
103 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
104 *
105 * If this field is set to 0 Hz, the control request will
106 * be sent immediately as a one-shot frame. This may be useful
107 * for advanced applications that require outputs to be
108 * synchronized with data acquisition. In this case, we
109 * recommend not exceeding 50 ms between control calls.
110 */
111 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
112
113 /**
114 * \brief Request PID to target position with a differential position setpoint
115 *
116 * \details This control mode will set the motor's position setpoint to the
117 * position specified by the user. It will also set the motor's
118 * differential position setpoint to the specified position.
119 *
120 * \param TargetPosition Average position to drive toward in rotations.
121 * \param DifferentialPosition Differential position to drive toward in
122 * rotations.
123 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
124 * Pro), which increases peak power by ~15%. Set to false to
125 * use trapezoidal commutation. FOC improves motor
126 * performance by leveraging torque (current) control.
127 * However, this may be inconvenient for applications that
128 * require specifying duty cycle or voltage.
129 * CTR-Electronics has developed a hybrid method that
130 * combines the performances gains of FOC while still
131 * allowing applications to provide duty cycle or voltage
132 * demand. This not to be confused with simple sinusoidal
133 * control or phase voltage control which lacks the
134 * performance gains.
135 * \param TargetSlot Select which gains are applied to the primary controller
136 * by selecting the slot. Use the configuration api to set
137 * the gain values for the selected slot before enabling
138 * this feature. Slot must be within [0,2].
139 * \param DifferentialSlot Select which gains are applied to the differential
140 * controller by selecting the slot. Use the
141 * configuration api to set the gain values for the
142 * selected slot before enabling this feature. Slot
143 * must be within [0,2].
144 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
145 * output is zero (or within deadband). Set
146 * to false to use the NeutralMode
147 * configuration setting (default). This flag
148 * exists to provide the fundamental behavior
149 * of this control when output is zero, which
150 * is to provide 0V to the motor.
151 * \param LimitForwardMotion Set to true to force forward limiting. This
152 * allows users to use other limit switch sensors
153 * connected to robot controller. This also allows
154 * use of active sensors that require external
155 * power.
156 * \param LimitReverseMotion Set to true to force reverse limiting. This
157 * allows users to use other limit switch sensors
158 * connected to robot controller. This also allows
159 * use of active sensors that require external
160 * power.
161 */
162 DifferentialPositionVoltage(units::angle::turn_t TargetPosition, units::angle::turn_t DifferentialPosition, bool EnableFOC = true, int TargetSlot = 0, int DifferentialSlot = 1, bool OverrideBrakeDurNeutral = false, bool LimitForwardMotion = false, bool LimitReverseMotion = false) : ControlRequest{"DifferentialPositionVoltage"},
163 TargetPosition{std::move(TargetPosition)},
165 EnableFOC{std::move(EnableFOC)},
166 TargetSlot{std::move(TargetSlot)},
171 {}
172
173 /**
174 * \brief Modifies this Control Request's TargetPosition parameter and returns itself for
175 * method-chaining and easier to use request API.
176 * \param newTargetPosition Parameter to modify
177 * \returns Itself
178 */
179 DifferentialPositionVoltage& WithTargetPosition(units::angle::turn_t newTargetPosition)
180 {
181 TargetPosition = std::move(newTargetPosition);
182 return *this;
183 }
184
185 /**
186 * \brief Modifies this Control Request's DifferentialPosition parameter and returns itself for
187 * method-chaining and easier to use request API.
188 * \param newDifferentialPosition Parameter to modify
189 * \returns Itself
190 */
191 DifferentialPositionVoltage& WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
192 {
193 DifferentialPosition = std::move(newDifferentialPosition);
194 return *this;
195 }
196
197 /**
198 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
199 * method-chaining and easier to use request API.
200 * \param newEnableFOC Parameter to modify
201 * \returns Itself
202 */
204 {
205 EnableFOC = std::move(newEnableFOC);
206 return *this;
207 }
208
209 /**
210 * \brief Modifies this Control Request's TargetSlot parameter and returns itself for
211 * method-chaining and easier to use request API.
212 * \param newTargetSlot Parameter to modify
213 * \returns Itself
214 */
216 {
217 TargetSlot = std::move(newTargetSlot);
218 return *this;
219 }
220
221 /**
222 * \brief Modifies this Control Request's DifferentialSlot parameter and returns itself for
223 * method-chaining and easier to use request API.
224 * \param newDifferentialSlot Parameter to modify
225 * \returns Itself
226 */
228 {
229 DifferentialSlot = std::move(newDifferentialSlot);
230 return *this;
231 }
232
233 /**
234 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
235 * method-chaining and easier to use request API.
236 * \param newOverrideBrakeDurNeutral Parameter to modify
237 * \returns Itself
238 */
240 {
241 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
242 return *this;
243 }
244
245 /**
246 * \brief Modifies this Control Request's LimitForwardMotion parameter and returns itself for
247 * method-chaining and easier to use request API.
248 * \param newLimitForwardMotion Parameter to modify
249 * \returns Itself
250 */
252 {
253 LimitForwardMotion = std::move(newLimitForwardMotion);
254 return *this;
255 }
256
257 /**
258 * \brief Modifies this Control Request's LimitReverseMotion parameter and returns itself for
259 * method-chaining and easier to use request API.
260 * \param newLimitReverseMotion Parameter to modify
261 * \returns Itself
262 */
264 {
265 LimitReverseMotion = std::move(newLimitReverseMotion);
266 return *this;
267 }
268 /**
269 * \brief Sets the period at which this control will update at.
270 * This is designated in Hertz, with a minimum of 20 Hz
271 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
272 *
273 * If this field is set to 0 Hz, the control request will
274 * be sent immediately as a one-shot frame. This may be useful
275 * for advanced applications that require outputs to be
276 * synchronized with data acquisition. In this case, we
277 * recommend not exceeding 50 ms between control calls.
278 *
279 * \param newUpdateFreqHz Parameter to modify
280 * \returns Itself
281 */
282 DifferentialPositionVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
283 {
284 UpdateFreqHz = newUpdateFreqHz;
285 return *this;
286 }
287 /**
288 * Returns a string representation of the object.
289 *
290 * \returns a string representation of the object.
291 */
292 std::string ToString() const override
293 {
294 std::stringstream ss;
295 ss << "class: DifferentialPositionVoltage" << std::endl;
296 ss << "TargetPosition: " << TargetPosition.to<double>() << std::endl;
297 ss << "DifferentialPosition: " << DifferentialPosition.to<double>() << std::endl;
298 ss << "EnableFOC: " << EnableFOC << std::endl;
299 ss << "TargetSlot: " << TargetSlot << std::endl;
300 ss << "DifferentialSlot: " << DifferentialSlot << std::endl;
301 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
302 ss << "LimitForwardMotion: " << LimitForwardMotion << std::endl;
303 ss << "LimitReverseMotion: " << LimitReverseMotion << std::endl;
304 return ss.str();
305 }
306
307 /**
308 * \brief Gets information about this control request.
309 *
310 * \returns Map of control parameter names and corresponding applied values
311 */
312 std::map<std::string, std::string> GetControlInfo() const override
313 {
314 std::map<std::string, std::string> controlInfo;
315 std::stringstream ss;
316 controlInfo["Name"] = GetName();
317 ss << TargetPosition.to<double>(); controlInfo["TargetPosition"] = ss.str(); ss.str(std::string{});
318 ss << DifferentialPosition.to<double>(); controlInfo["DifferentialPosition"] = ss.str(); ss.str(std::string{});
319 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
320 ss << TargetSlot; controlInfo["TargetSlot"] = ss.str(); ss.str(std::string{});
321 ss << DifferentialSlot; controlInfo["DifferentialSlot"] = ss.str(); ss.str(std::string{});
322 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
323 ss << LimitForwardMotion; controlInfo["LimitForwardMotion"] = ss.str(); ss.str(std::string{});
324 ss << LimitReverseMotion; controlInfo["LimitReverseMotion"] = ss.str(); ss.str(std::string{});
325 return controlInfo;
326 }
327};
328
329}
330}
331}
332
CTREXPORT int c_ctre_phoenix6_RequestControlDifferentialPositionVoltage(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double TargetPosition, double DifferentialPosition, bool EnableFOC, int TargetSlot, int DifferentialSlot, bool OverrideBrakeDurNeutral, bool LimitForwardMotion, bool LimitReverseMotion)
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
std::string const & GetName() const
Definition: ControlRequest.hpp:51
Request PID to target position with a differential position setpoint.
Definition: DifferentialPositionVoltage.hpp:29
DifferentialPositionVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: DifferentialPositionVoltage.hpp:239
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: DifferentialPositionVoltage.hpp:67
DifferentialPositionVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: DifferentialPositionVoltage.hpp:203
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: DifferentialPositionVoltage.hpp:86
int DifferentialSlot
Select which gains are applied to the differential controller by selecting the slot.
Definition: DifferentialPositionVoltage.hpp:79
DifferentialPositionVoltage & WithLimitForwardMotion(bool newLimitForwardMotion)
Modifies this Control Request's LimitForwardMotion parameter and returns itself for method-chaining a...
Definition: DifferentialPositionVoltage.hpp:251
bool LimitForwardMotion
Set to true to force forward limiting.
Definition: DifferentialPositionVoltage.hpp:92
DifferentialPositionVoltage & WithTargetSlot(int newTargetSlot)
Modifies this Control Request's TargetSlot parameter and returns itself for method-chaining and easie...
Definition: DifferentialPositionVoltage.hpp:215
int TargetSlot
Select which gains are applied to the primary controller by selecting the slot.
Definition: DifferentialPositionVoltage.hpp:73
DifferentialPositionVoltage & WithTargetPosition(units::angle::turn_t newTargetPosition)
Modifies this Control Request's TargetPosition parameter and returns itself for method-chaining and e...
Definition: DifferentialPositionVoltage.hpp:179
units::angle::turn_t TargetPosition
Average position to drive toward in rotations.
Definition: DifferentialPositionVoltage.hpp:52
DifferentialPositionVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: DifferentialPositionVoltage.hpp:282
units::angle::turn_t DifferentialPosition
Differential position to drive toward in rotations.
Definition: DifferentialPositionVoltage.hpp:56
bool LimitReverseMotion
Set to true to force reverse limiting.
Definition: DifferentialPositionVoltage.hpp:98
DifferentialPositionVoltage & WithLimitReverseMotion(bool newLimitReverseMotion)
Modifies this Control Request's LimitReverseMotion parameter and returns itself for method-chaining a...
Definition: DifferentialPositionVoltage.hpp:263
DifferentialPositionVoltage & WithDifferentialSlot(int newDifferentialSlot)
Modifies this Control Request's DifferentialSlot parameter and returns itself for method-chaining and...
Definition: DifferentialPositionVoltage.hpp:227
DifferentialPositionVoltage & WithDifferentialPosition(units::angle::turn_t newDifferentialPosition)
Modifies this Control Request's DifferentialPosition parameter and returns itself for method-chaining...
Definition: DifferentialPositionVoltage.hpp:191
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: DifferentialPositionVoltage.hpp:312
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: DifferentialPositionVoltage.hpp:111
std::string ToString() const override
Returns a string representation of the object.
Definition: DifferentialPositionVoltage.hpp:292
DifferentialPositionVoltage(units::angle::turn_t TargetPosition, units::angle::turn_t DifferentialPosition, bool EnableFOC=true, int TargetSlot=0, int DifferentialSlot=1, bool OverrideBrakeDurNeutral=false, bool LimitForwardMotion=false, bool LimitReverseMotion=false)
Request PID to target position with a differential position setpoint.
Definition: DifferentialPositionVoltage.hpp:162
Definition: RcManualEvent.hpp:12