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
PositionDutyCycle.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/dimensionless.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 duty cycle 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 duty cycle as an arbitrary feedforward value.
29 */
31{
32 bool ApplyConfigsOnRequest{false};
33
34 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) override
35 {
36 if (req.get() != this)
37 {
38 auto const reqCast = dynamic_cast<PositionDutyCycle *>(req.get());
39 if (reqCast != nullptr)
40 {
41 *reqCast = *this;
42 }
43 else
44 {
45 req = std::make_shared<PositionDutyCycle>(*this);
46 }
47 }
48
49 std::stringstream ss;
50
51 std::string strs{ss.str()};
52 c_ctre_phoenix6_requestConfigApply(network, deviceHash, ConfigTimeout.to<double>(), strs.c_str(), strs.length(), ApplyConfigsOnRequest);
53 ApplyConfigsOnRequest = false;
54 return c_ctre_phoenix6_RequestControlPositionDutyCycle(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Position.to<double>(), Velocity.to<double>(), EnableFOC, FeedForward.to<double>(), Slot, OverrideBrakeDurNeutral);
55 }
56
57public:
58 /**
59 * Position to drive toward in rotations.
60 */
61 units::angle::turn_t Position;
62 /**
63 * Velocity to drive toward in rotations per second.
64 */
65 units::angular_velocity::turns_per_second_t Velocity;
66 /**
67 * Set to true to use FOC commutation (requires Phoenix Pro), which increases
68 * peak power by ~15%. Set to false to use trapezoidal commutation. FOC
69 * improves motor performance by leveraging torque (current) control. However,
70 * this may be inconvenient for applications that require specifying duty cycle
71 * or voltage. CTR-Electronics has developed a hybrid method that combines the
72 * performances gains of FOC while still allowing applications to provide duty
73 * cycle or voltage demand. This not to be confused with simple sinusoidal
74 * control or phase voltage control which lacks the performance gains.
75 */
77 /**
78 * Feedforward to apply in fractional units between -1 and +1.
79 */
80 units::dimensionless::scalar_t FeedForward;
81 /**
82 * Select which gains are applied by selecting the slot. Use the configuration
83 * api to set the gain values for the selected slot before enabling this
84 * feature. Slot must be within [0,2].
85 */
86 int Slot;
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 duty cycle feedforward.
117 *
118 * \details This control mode will set the motor's position setpoint to the
119 * position specified by the user. In addition, it will apply an
120 * additional duty cycle as an arbitrary feedforward value.
121 *
122 * \param Position Position to drive toward in rotations.
123 * \param Velocity Velocity to drive toward in rotations per second.
124 * \param EnableFOC Set to true to use FOC commutation (requires Phoenix
125 * Pro), which increases peak power by ~15%. Set to false to
126 * use trapezoidal commutation. FOC improves motor
127 * performance by leveraging torque (current) control.
128 * However, this may be inconvenient for applications that
129 * require specifying duty cycle or voltage.
130 * CTR-Electronics has developed a hybrid method that
131 * combines the performances gains of FOC while still
132 * allowing applications to provide duty cycle or voltage
133 * demand. This not to be confused with simple sinusoidal
134 * control or phase voltage control which lacks the
135 * performance gains.
136 * \param FeedForward Feedforward to apply in fractional units between -1 and
137 * +1.
138 * \param Slot Select which gains are applied by selecting the slot. Use the
139 * configuration api to set the gain values for the selected slot
140 * before enabling this feature. Slot must be within [0,2].
141 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
142 * output is zero (or within deadband). Set
143 * to false to use the NeutralMode
144 * configuration setting (default). This flag
145 * exists to provide the fundamental behavior
146 * of this control when output is zero, which
147 * is to provide 0V to the motor.
148 */
149 PositionDutyCycle(units::angle::turn_t Position, units::angular_velocity::turns_per_second_t Velocity, bool EnableFOC, units::dimensionless::scalar_t FeedForward, int Slot, bool OverrideBrakeDurNeutral) : ControlRequest{"PositionDutyCycle"},
150 Position{std::move(Position)},
151 Velocity{std::move(Velocity)},
152 EnableFOC{std::move(EnableFOC)},
153 FeedForward{std::move(FeedForward)},
154 Slot{std::move(Slot)},
156 {}
157
158 /**
159 * \brief Request PID to target position with duty cycle feedforward.
160 *
161 * \details This control mode will set the motor's position setpoint to the
162 * position specified by the user. In addition, it will apply an
163 * additional duty cycle as an arbitrary feedforward value.
164 *
165 * \param Position Position to drive toward in rotations.
166 */
167 PositionDutyCycle(units::angle::turn_t Position) : PositionDutyCycle{Position, 0.0_tps, true, 0.0, 0, false}
168 {}
169
170 /**
171 * \brief Modifies this Control Request's Position parameter and returns itself for
172 * method-chaining and easier to use request API.
173 * \param newPosition Parameter to modify
174 * \returns Itself
175 */
176 PositionDutyCycle& WithPosition(units::angle::turn_t newPosition)
177 {
178 Position = std::move(newPosition);
179 return *this;
180 }
181
182 /**
183 * \brief Modifies this Control Request's Velocity parameter and returns itself for
184 * method-chaining and easier to use request API.
185 * \param newVelocity Parameter to modify
186 * \returns Itself
187 */
188 PositionDutyCycle& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
189 {
190 Velocity = std::move(newVelocity);
191 return *this;
192 }
193
194 /**
195 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
196 * method-chaining and easier to use request API.
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 FeedForward parameter and returns itself for
208 * method-chaining and easier to use request API.
209 * \param newFeedForward Parameter to modify
210 * \returns Itself
211 */
212 PositionDutyCycle& WithFeedForward(units::dimensionless::scalar_t newFeedForward)
213 {
214 FeedForward = std::move(newFeedForward);
215 return *this;
216 }
217
218 /**
219 * \brief Modifies this Control Request's Slot parameter and returns itself for
220 * method-chaining and easier to use request API.
221 * \param newSlot Parameter to modify
222 * \returns Itself
223 */
225 {
226 Slot = std::move(newSlot);
227 return *this;
228 }
229
230 /**
231 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
232 * method-chaining and easier to use request API.
233 * \param newOverrideBrakeDurNeutral Parameter to modify
234 * \returns Itself
235 */
236 PositionDutyCycle& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
237 {
238 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
239 return *this;
240 }
241 /**
242 * \brief Sets the period at which this control will update at.
243 * This is designated in Hertz, with a minimum of 20 Hz
244 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
245 *
246 * If this field is set to 0 Hz, the control request will
247 * be sent immediately as a one-shot frame. This may be useful
248 * for advanced applications that require outputs to be
249 * synchronized with data acquisition. In this case, we
250 * recommend not exceeding 50 ms between control calls.
251 *
252 * \param newUpdateFreqHz Parameter to modify
253 * \returns Itself
254 */
255 PositionDutyCycle &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
256 {
257 UpdateFreqHz = newUpdateFreqHz;
258 return *this;
259 }
260 /**
261 * Returns a string representation of the object.
262 *
263 * \returns a string representation of the object.
264 */
265 std::string ToString() const override
266 {
267 std::stringstream ss;
268 ss << "class: PositionDutyCycle" << std::endl;
269 ss << "Position: " << Position.to<double>() << std::endl;
270 ss << "Velocity: " << Velocity.to<double>() << std::endl;
271 ss << "EnableFOC: " << EnableFOC << std::endl;
272 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
273 ss << "Slot: " << Slot << std::endl;
274 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
275 return ss.str();
276 }
277
278 /**
279 * \brief Forces configs to be applied the next time this is used in a setControl.
280 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
281 * properly set when they are not already set
282 */
283 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
284
285 /**
286 * \brief Gets information about this control request.
287 *
288 * \returns Map of control parameter names and corresponding applied values
289 */
290 std::map<std::string, std::string> GetControlInfo() const override
291 {
292 std::map<std::string, std::string> controlInfo;
293 std::stringstream ss;
294 controlInfo["Name"] = GetName();
295 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
296 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
297 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
298 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
299 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
300 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
301 return controlInfo;
302 }
303};
304
305}
306}
307}
308
CTREXPORT int c_ctre_phoenix6_RequestControlPositionDutyCycle(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Position, double Velocity, bool EnableFOC, double FeedForward, int Slot, 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 duty cycle feedforward.
Definition: PositionDutyCycle.hpp:31
PositionDutyCycle & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: PositionDutyCycle.hpp:224
units::dimensionless::scalar_t FeedForward
Feedforward to apply in fractional units between -1 and +1.
Definition: PositionDutyCycle.hpp:80
PositionDutyCycle & 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: PositionDutyCycle.hpp:188
PositionDutyCycle & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: PositionDutyCycle.hpp:200
units::angular_velocity::turns_per_second_t Velocity
Velocity to drive toward in rotations per second.
Definition: PositionDutyCycle.hpp:65
PositionDutyCycle & WithFeedForward(units::dimensionless::scalar_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition: PositionDutyCycle.hpp:212
PositionDutyCycle(units::angle::turn_t Position, units::angular_velocity::turns_per_second_t Velocity, bool EnableFOC, units::dimensionless::scalar_t FeedForward, int Slot, bool OverrideBrakeDurNeutral)
Request PID to target position with duty cycle feedforward.
Definition: PositionDutyCycle.hpp:149
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: PositionDutyCycle.hpp:76
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: PositionDutyCycle.hpp:61
int Slot
Select which gains are applied by selecting the slot.
Definition: PositionDutyCycle.hpp:86
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: PositionDutyCycle.hpp:290
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: PositionDutyCycle.hpp:93
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: PositionDutyCycle.hpp:113
PositionDutyCycle & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: PositionDutyCycle.hpp:255
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: PositionDutyCycle.hpp:283
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: PositionDutyCycle.hpp:100
PositionDutyCycle & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: PositionDutyCycle.hpp:236
PositionDutyCycle & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition: PositionDutyCycle.hpp:176
std::string ToString() const override
Returns a string representation of the object.
Definition: PositionDutyCycle.hpp:265
PositionDutyCycle(units::angle::turn_t Position)
Request PID to target position with duty cycle feedforward.
Definition: PositionDutyCycle.hpp:167
Definition: ManualEvent.hpp:12