CTRE Phoenix Pro C++ 23.0.12
MotionMagicVoltage.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/voltage.h>
15#include <units/frequency.h>
16#include <units/time.h>
17
18
19namespace ctre {
20namespace phoenixpro {
21namespace controls {
22
23/**
24 * Requests Motion Magic® to target a final position using a motion profile.
25 * Users can optionally provide a voltage feedforward.
26 *
27 * Motion Magic® produces a motion profile in real-time while attempting to honor the Cruise Velocity,
28 * Acceleration, and Jerk value specified via the Motion Magic® configuration values. Target position can be
29 * changed on-the-fly and Motion Magic® will do its best to adjust the profile. This control mode is
30 * voltage-based, so relevant closed-loop gains will use Volts for the numerator.
31 */
33{
34 bool ApplyConfigsOnRequest;
35
36 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req)
37 {
38 std::stringstream ss;
39 auto& ref = requestReference.GetNameValues();
40 ss << Position.to<double>(); ref["Position"] = ss.str(); ss.str(std::string());
41 ss << EnableFOC; ref["EnableFOC"] = ss.str(); ss.str(std::string());
42 ss << FeedForward.to<double>(); ref["FeedForward"] = ss.str(); ss.str(std::string());
43 ss << Slot; ref["Slot"] = ss.str(); ss.str(std::string());
44 ss << OverrideBrakeDurNeutral; ref["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string());
45
46 if (req.get() != this)
47 {
48 auto const reqCast = dynamic_cast<MotionMagicVoltage *>(req.get());
49 if (reqCast != nullptr)
50 {
51 *reqCast = *this;
52 }
53 else
54 {
55 req = std::make_shared<MotionMagicVoltage>(*this);
56 }
57 }
58
59
60 std::string strs{ss.str()};
61 c_ctre_phoenixpro_requestConfigApply(network, deviceHash, ConfigTimeout.to<double>(), strs.c_str(), strs.length(), ApplyConfigsOnRequest);
62 ApplyConfigsOnRequest = false;
63 return c_ctre_phoenixpro_RequestControlMotionMagicVoltage(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Position.to<double>(), EnableFOC, FeedForward.to<double>(), Slot, OverrideBrakeDurNeutral);
64 }
65
66public:
67 /**
68 * Position to drive toward in rotations.
69 */
70 units::angle::turn_t Position;
71 /**
72 * Set to true to use FOC commutation, which increases peak power by ~15%. Set
73 * to false to use trapezoidal commutation. FOC improves motor performance by
74 * leveraging torque (current) control. However, this may be inconvenient for
75 * applications that require specifying duty cycle or voltage. CTR-Electronics
76 * has developed a hybrid method that combines the performances gains of FOC
77 * while still allowing applications to provide duty cycle or voltage demand.
78 * This not to be confused with simple sinusoidal control or phase voltage
79 * control which lacks the performance gains.
80 */
82 /**
83 * Feedforward to apply in volts
84 */
85 units::voltage::volt_t FeedForward;
86 /**
87 * Select which gains are applied by selecting the slot. Use the configuration
88 * api to set the gain values for the selected slot before enabling this
89 * feature. Slot must be within [0,2].
90 */
91 int Slot;
92 /**
93 * Set to true to static-brake the rotor when output is zero (or within
94 * deadband). Set to false to use the NeutralMode configuration setting
95 * (default). This flag exists to provide the fundamental behavior of this
96 * control when output is zero, which is to provide 0V to the motor.
97 */
99
100
101
102 /**
103 * \brief The timeout when sending configs associated with this control
104 */
105 units::time::second_t ConfigTimeout{0.1_s};
106
107 /**
108 * \brief The period at which this control will update at.
109 * This is designated in Hertz, with a minimum of 20 Hz
110 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
111 *
112 * If this field is set to 0 Hz, the control request will
113 * be sent immediately as a one-shot frame. This may be useful
114 * for advanced applications that require outputs to be
115 * synchronized with data acquisition. In this case, we
116 * recommend not exceeding 50 ms between control calls.
117 */
118 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
119
120 /**
121 *\brief Requests Motion Magic® to target a final position using a motion
122 * profile. Users can optionally provide a voltage feedforward.
123 *
124 *\details Motion Magic® produces a motion profile in real-time while
125 * attempting to honor the Cruise Velocity, Acceleration, and Jerk
126 * value specified via the Motion Magic® configuration values.
127 * Target position can be changed on-the-fly and Motion Magic® will
128 * do its best to adjust the profile. This control mode is
129 * voltage-based, so relevant closed-loop gains will use Volts for
130 * the numerator.
131 *
132 * \param Position Position to drive toward in rotations.
133 * \param EnableFOC Set to true to use FOC commutation, which increases
134 * peak power by ~15%. Set to false to use trapezoidal
135 * commutation. FOC improves motor performance by
136 * leveraging torque (current) control. However, this
137 * may be inconvenient for applications that require
138 * specifying duty cycle or voltage. CTR-Electronics
139 * has developed a hybrid method that combines the
140 * performances gains of FOC while still allowing
141 * applications to provide duty cycle or voltage
142 * demand. This not to be confused with simple
143 * sinusoidal control or phase voltage control which
144 * lacks the performance gains.
145 * \param FeedForward Feedforward to apply in volts
146 * \param Slot Select which gains are applied by selecting the slot.
147 * Use the configuration api to set the gain values for the
148 * selected slot before enabling this feature. Slot must be
149 * within [0,2].
150 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor
151 * when output is zero (or within
152 * deadband). Set to false to use the
153 * NeutralMode configuration setting
154 * (default). This flag exists to provide
155 * the fundamental behavior of this
156 * control when output is zero, which is
157 * to provide 0V to the motor.
158 */
159 MotionMagicVoltage(units::angle::turn_t Position, bool EnableFOC, units::voltage::volt_t FeedForward, int Slot, bool OverrideBrakeDurNeutral) : ControlRequest{"MotionMagicVoltage"}, ApplyConfigsOnRequest{false}
160 {
161 this->Position = Position;
162 this->EnableFOC = EnableFOC;
163 this->FeedForward = FeedForward;
164 this->Slot = Slot;
165 this->OverrideBrakeDurNeutral = OverrideBrakeDurNeutral;
166 }
167
168 /**
169 *\brief Requests Motion Magic® to target a final position using a motion
170 * profile. Users can optionally provide a voltage feedforward.
171 *
172 *\details Motion Magic® produces a motion profile in real-time while
173 * attempting to honor the Cruise Velocity, Acceleration, and Jerk
174 * value specified via the Motion Magic® configuration values.
175 * Target position can be changed on-the-fly and Motion Magic® will
176 * do its best to adjust the profile. This control mode is
177 * voltage-based, so relevant closed-loop gains will use Volts for
178 * the numerator.
179 *
180 * \param Position Position to drive toward in rotations.
181 */
182 MotionMagicVoltage(units::angle::turn_t Position) : MotionMagicVoltage{Position, true, 0.0_V, 0, false}
183 {}
184
185 /**
186 * \brief Modifies this Control Request's Position parameter and returns itself for
187 * method-chaining and easier to use request API.
188 * \param newPosition Parameter to modify
189 * \returns Itself
190 */
191 MotionMagicVoltage& WithPosition(units::angle::turn_t newPosition)
192 {
193 Position = newPosition;
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 = newEnableFOC;
206 return *this;
207 }
208
209 /**
210 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
211 * method-chaining and easier to use request API.
212 * \param newFeedForward Parameter to modify
213 * \returns Itself
214 */
215 MotionMagicVoltage& WithFeedForward(units::voltage::volt_t newFeedForward)
216 {
217 FeedForward = newFeedForward;
218 return *this;
219 }
220
221 /**
222 * \brief Modifies this Control Request's Slot parameter and returns itself for
223 * method-chaining and easier to use request API.
224 * \param newSlot Parameter to modify
225 * \returns Itself
226 */
228 {
229 Slot = newSlot;
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 */
239 MotionMagicVoltage& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
240 {
241 OverrideBrakeDurNeutral = newOverrideBrakeDurNeutral;
242 return *this;
243 }
244 /**
245 * \brief Sets the period at which this control will update at.
246 * This is designated in Hertz, with a minimum of 20 Hz
247 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
248 *
249 * If this field is set to 0 Hz, the control request will
250 * be sent immediately as a one-shot frame. This may be useful
251 * for advanced applications that require outputs to be
252 * synchronized with data acquisition. In this case, we
253 * recommend not exceeding 50 ms between control calls.
254 *
255 * \param newUpdateFreqHz Parameter to modify
256 * \returns Itself
257 */
258 MotionMagicVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
259 {
260 UpdateFreqHz = newUpdateFreqHz;
261 return *this;
262 }
263 /**
264 * Returns a string representation of the object.
265 *
266 * \returns a string representation of the object.
267 */
268 std::string ToString() const
269 {
270 std::stringstream ss;
271 ss << "class: MotionMagicVoltage" << std::endl;
272 ss << "Position: " << Position.to<double>() << std::endl;
273 ss << "EnableFOC: " << EnableFOC << std::endl;
274 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
275 ss << "Slot: " << Slot << std::endl;
276 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
277 return ss.str();
278 }
279
280 /**
281 * \brief Forces configs to be applied the next time this is used in a setControl.
282 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
283 * properly set when they are not already set
284 */
285 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
286};
287
288}
289}
290}
291
CTREXPORT int c_ctre_phoenixpro_requestConfigApply(const char *canbus, uint32_t ecuEncoding, double timeoutSeconds, const char *str, uint32_t strlen, bool forceApply)
CTREXPORT int c_ctre_phoenixpro_RequestControlMotionMagicVoltage(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Position, bool EnableFOC, double FeedForward, int Slot, bool OverrideBrakeDurNeutral)
std::map< std::string, std::string > & GetNameValues()
Gets the map of control parameter names to the corresponding applied value.
Definition: ControlRequest.hpp:52
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:65
ControlInfo requestReference
Definition: ControlRequest.hpp:70
Requests Motion Magic® to target a final position using a motion profile.
Definition: MotionMagicVoltage.hpp:33
MotionMagicVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MotionMagicVoltage.hpp:258
bool EnableFOC
Set to true to use FOC commutation, which increases peak power by ~15%.
Definition: MotionMagicVoltage.hpp:81
std::string ToString() const
Returns a string representation of the object.
Definition: MotionMagicVoltage.hpp:268
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: MotionMagicVoltage.hpp:285
MotionMagicVoltage & WithFeedForward(units::voltage::volt_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition: MotionMagicVoltage.hpp:215
MotionMagicVoltage & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: MotionMagicVoltage.hpp:227
MotionMagicVoltage & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition: MotionMagicVoltage.hpp:191
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: MotionMagicVoltage.hpp:98
MotionMagicVoltage(units::angle::turn_t Position, bool EnableFOC, units::voltage::volt_t FeedForward, int Slot, bool OverrideBrakeDurNeutral)
Requests Motion Magic® to target a final position using a motion profile.
Definition: MotionMagicVoltage.hpp:159
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: MotionMagicVoltage.hpp:118
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: MotionMagicVoltage.hpp:105
units::voltage::volt_t FeedForward
Feedforward to apply in volts.
Definition: MotionMagicVoltage.hpp:85
MotionMagicVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: MotionMagicVoltage.hpp:239
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: MotionMagicVoltage.hpp:70
MotionMagicVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: MotionMagicVoltage.hpp:203
MotionMagicVoltage(units::angle::turn_t Position)
Requests Motion Magic® to target a final position using a motion profile.
Definition: MotionMagicVoltage.hpp:182
int Slot
Select which gains are applied by selecting the slot.
Definition: MotionMagicVoltage.hpp:91
Definition: string_util.hpp:14