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
PositionVoltage.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/voltage.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 voltage 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 voltage 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<PositionVoltage *>(req.get());
39 if (reqCast != nullptr)
40 {
41 *reqCast = *this;
42 }
43 else
44 {
45 req = std::make_shared<PositionVoltage>(*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_RequestControlPositionVoltage(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 volts
79 */
80 units::voltage::volt_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 voltage 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 voltage 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 volts
137 * \param Slot Select which gains are applied by selecting the slot. Use the
138 * configuration api to set the gain values for the selected slot
139 * before enabling this feature. Slot must be within [0,2].
140 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor when
141 * output is zero (or within deadband). Set
142 * to false to use the NeutralMode
143 * configuration setting (default). This flag
144 * exists to provide the fundamental behavior
145 * of this control when output is zero, which
146 * is to provide 0V to the motor.
147 */
148 PositionVoltage(units::angle::turn_t Position, units::angular_velocity::turns_per_second_t Velocity, bool EnableFOC, units::voltage::volt_t FeedForward, int Slot, bool OverrideBrakeDurNeutral) : ControlRequest{"PositionVoltage"},
149 Position{std::move(Position)},
150 Velocity{std::move(Velocity)},
151 EnableFOC{std::move(EnableFOC)},
152 FeedForward{std::move(FeedForward)},
153 Slot{std::move(Slot)},
155 {}
156
157 /**
158 * \brief Request PID to target position with voltage feedforward
159 *
160 * \details This control mode will set the motor's position setpoint to the
161 * position specified by the user. In addition, it will apply an
162 * additional voltage as an arbitrary feedforward value.
163 *
164 * \param Position Position to drive toward in rotations.
165 */
166 PositionVoltage(units::angle::turn_t Position) : PositionVoltage{Position, 0.0_tps, true, 0.0_V, 0, false}
167 {}
168
169 /**
170 * \brief Modifies this Control Request's Position parameter and returns itself for
171 * method-chaining and easier to use request API.
172 * \param newPosition Parameter to modify
173 * \returns Itself
174 */
175 PositionVoltage& WithPosition(units::angle::turn_t newPosition)
176 {
177 Position = std::move(newPosition);
178 return *this;
179 }
180
181 /**
182 * \brief Modifies this Control Request's Velocity parameter and returns itself for
183 * method-chaining and easier to use request API.
184 * \param newVelocity Parameter to modify
185 * \returns Itself
186 */
187 PositionVoltage& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
188 {
189 Velocity = std::move(newVelocity);
190 return *this;
191 }
192
193 /**
194 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
195 * method-chaining and easier to use request API.
196 * \param newEnableFOC Parameter to modify
197 * \returns Itself
198 */
199 PositionVoltage& WithEnableFOC(bool newEnableFOC)
200 {
201 EnableFOC = std::move(newEnableFOC);
202 return *this;
203 }
204
205 /**
206 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
207 * method-chaining and easier to use request API.
208 * \param newFeedForward Parameter to modify
209 * \returns Itself
210 */
211 PositionVoltage& WithFeedForward(units::voltage::volt_t newFeedForward)
212 {
213 FeedForward = std::move(newFeedForward);
214 return *this;
215 }
216
217 /**
218 * \brief Modifies this Control Request's Slot parameter and returns itself for
219 * method-chaining and easier to use request API.
220 * \param newSlot Parameter to modify
221 * \returns Itself
222 */
224 {
225 Slot = std::move(newSlot);
226 return *this;
227 }
228
229 /**
230 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
231 * method-chaining and easier to use request API.
232 * \param newOverrideBrakeDurNeutral Parameter to modify
233 * \returns Itself
234 */
235 PositionVoltage& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
236 {
237 OverrideBrakeDurNeutral = std::move(newOverrideBrakeDurNeutral);
238 return *this;
239 }
240 /**
241 * \brief Sets the period at which this control will update at.
242 * This is designated in Hertz, with a minimum of 20 Hz
243 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
244 *
245 * If this field is set to 0 Hz, the control request will
246 * be sent immediately as a one-shot frame. This may be useful
247 * for advanced applications that require outputs to be
248 * synchronized with data acquisition. In this case, we
249 * recommend not exceeding 50 ms between control calls.
250 *
251 * \param newUpdateFreqHz Parameter to modify
252 * \returns Itself
253 */
254 PositionVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
255 {
256 UpdateFreqHz = newUpdateFreqHz;
257 return *this;
258 }
259 /**
260 * Returns a string representation of the object.
261 *
262 * \returns a string representation of the object.
263 */
264 std::string ToString() const override
265 {
266 std::stringstream ss;
267 ss << "class: PositionVoltage" << std::endl;
268 ss << "Position: " << Position.to<double>() << std::endl;
269 ss << "Velocity: " << Velocity.to<double>() << std::endl;
270 ss << "EnableFOC: " << EnableFOC << std::endl;
271 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
272 ss << "Slot: " << Slot << std::endl;
273 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
274 return ss.str();
275 }
276
277 /**
278 * \brief Forces configs to be applied the next time this is used in a setControl.
279 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
280 * properly set when they are not already set
281 */
282 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
283
284 /**
285 * \brief Gets information about this control request.
286 *
287 * \returns Map of control parameter names and corresponding applied values
288 */
289 std::map<std::string, std::string> GetControlInfo() const override
290 {
291 std::map<std::string, std::string> controlInfo;
292 std::stringstream ss;
293 controlInfo["Name"] = GetName();
294 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
295 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
296 ss << EnableFOC; controlInfo["EnableFOC"] = ss.str(); ss.str(std::string{});
297 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
298 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
299 ss << OverrideBrakeDurNeutral; controlInfo["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string{});
300 return controlInfo;
301 }
302};
303
304}
305}
306}
307
CTREXPORT int c_ctre_phoenix6_RequestControlPositionVoltage(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 voltage feedforward.
Definition: PositionVoltage.hpp:31
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: PositionVoltage.hpp:113
PositionVoltage(units::angle::turn_t Position, units::angular_velocity::turns_per_second_t Velocity, bool EnableFOC, units::voltage::volt_t FeedForward, int Slot, bool OverrideBrakeDurNeutral)
Request PID to target position with voltage feedforward.
Definition: PositionVoltage.hpp:148
bool EnableFOC
Set to true to use FOC commutation (requires Phoenix Pro), which increases peak power by ~15%.
Definition: PositionVoltage.hpp:76
PositionVoltage & 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: PositionVoltage.hpp:187
PositionVoltage & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: PositionVoltage.hpp:223
PositionVoltage & WithFeedForward(units::voltage::volt_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition: PositionVoltage.hpp:211
PositionVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: PositionVoltage.hpp:199
std::string ToString() const override
Returns a string representation of the object.
Definition: PositionVoltage.hpp:264
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: PositionVoltage.hpp:61
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: PositionVoltage.hpp:289
units::voltage::volt_t FeedForward
Feedforward to apply in volts.
Definition: PositionVoltage.hpp:80
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: PositionVoltage.hpp:282
int Slot
Select which gains are applied by selecting the slot.
Definition: PositionVoltage.hpp:86
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: PositionVoltage.hpp:100
PositionVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: PositionVoltage.hpp:254
PositionVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: PositionVoltage.hpp:235
PositionVoltage & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition: PositionVoltage.hpp:175
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: PositionVoltage.hpp:93
units::angular_velocity::turns_per_second_t Velocity
Velocity to drive toward in rotations per second.
Definition: PositionVoltage.hpp:65
PositionVoltage(units::angle::turn_t Position)
Request PID to target position with voltage feedforward.
Definition: PositionVoltage.hpp:166
Definition: ManualEvent.hpp:12