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