CTRE Phoenix Pro C++ 23.0.12
MotionMagicTorqueCurrentFOC.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/dimensionless.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 torque current 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 based on
30 * torque current, so relevant closed-loop gains will use Amperes 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 << FeedForward.to<double>(); ref["FeedForward"] = ss.str(); ss.str(std::string());
42 ss << Slot; ref["Slot"] = ss.str(); ss.str(std::string());
43 ss << OverrideCoastDurNeutral; ref["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string());
44
45 if (req.get() != this)
46 {
47 auto const reqCast = dynamic_cast<MotionMagicTorqueCurrentFOC *>(req.get());
48 if (reqCast != nullptr)
49 {
50 *reqCast = *this;
51 }
52 else
53 {
54 req = std::make_shared<MotionMagicTorqueCurrentFOC>(*this);
55 }
56 }
57
58
59 std::string strs{ss.str()};
60 c_ctre_phoenixpro_requestConfigApply(network, deviceHash, ConfigTimeout.to<double>(), strs.c_str(), strs.length(), ApplyConfigsOnRequest);
61 ApplyConfigsOnRequest = false;
62 return c_ctre_phoenixpro_RequestControlMotionMagicTorqueCurrentFOC(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Position.to<double>(), FeedForward.to<double>(), Slot, OverrideCoastDurNeutral);
63 }
64
65public:
66 /**
67 * Position to drive toward in rotations.
68 */
69 units::angle::turn_t Position;
70 /**
71 * Feedforward to apply in torque current in Amperes. User can use motor's kT
72 * to scale Newton-meter to Amperes.
73 */
74 units::dimensionless::scalar_t FeedForward;
75 /**
76 * Select which gains are applied by selecting the slot. Use the configuration
77 * api to set the gain values for the selected slot before enabling this
78 * feature. Slot must be within [0,2].
79 */
80 int Slot;
81 /**
82 * Set to true to coast the rotor when output is zero (or within deadband). Set
83 * to false to use the NeutralMode configuration setting (default). This flag
84 * exists to provide the fundamental behavior of this control when output is
85 * zero, which is to provide 0A (zero torque).
86 */
88
89
90
91 /**
92 * \brief The timeout when sending configs associated with this control
93 */
94 units::time::second_t ConfigTimeout{0.1_s};
95
96 /**
97 * \brief The period at which this control will update at.
98 * This is designated in Hertz, with a minimum of 20 Hz
99 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
100 *
101 * If this field is set to 0 Hz, the control request will
102 * be sent immediately as a one-shot frame. This may be useful
103 * for advanced applications that require outputs to be
104 * synchronized with data acquisition. In this case, we
105 * recommend not exceeding 50 ms between control calls.
106 */
107 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
108
109 /**
110 *\brief Requests Motion Magic® to target a final position using a motion
111 * profile. Users can optionally provide a torque current
112 * feedforward.
113 *
114 *\details Motion Magic® produces a motion profile in real-time while
115 * attempting to honor the Cruise Velocity, Acceleration, and Jerk
116 * value specified via the Motion Magic® configuration values.
117 * Target position can be changed on-the-fly and Motion Magic® will
118 * do its best to adjust the profile. This control mode is based
119 * on torque current, so relevant closed-loop gains will use
120 * Amperes for the numerator.
121 *
122 * \param Position Position to drive toward in rotations.
123 * \param FeedForward Feedforward to apply in torque current in Amperes.
124 * User can use motor's kT to scale Newton-meter to
125 * Amperes.
126 * \param Slot Select which gains are applied by selecting the slot.
127 * Use the configuration api to set the gain values for the
128 * selected slot before enabling this feature. Slot must be
129 * within [0,2].
130 * \param OverrideCoastDurNeutral Set to true to coast the rotor when
131 * output is zero (or within deadband).
132 * Set to false to use the NeutralMode
133 * configuration setting (default). This
134 * flag exists to provide the fundamental
135 * behavior of this control when output
136 * is zero, which is to provide 0A (zero
137 * torque).
138 */
139 MotionMagicTorqueCurrentFOC(units::angle::turn_t Position, units::dimensionless::scalar_t FeedForward, int Slot, bool OverrideCoastDurNeutral) : ControlRequest{"MotionMagicTorqueCurrentFOC"}, ApplyConfigsOnRequest{false}
140 {
141 this->Position = Position;
142 this->FeedForward = FeedForward;
143 this->Slot = Slot;
144 this->OverrideCoastDurNeutral = OverrideCoastDurNeutral;
145 }
146
147 /**
148 *\brief Requests Motion Magic® to target a final position using a motion
149 * profile. Users can optionally provide a torque current
150 * feedforward.
151 *
152 *\details Motion Magic® produces a motion profile in real-time while
153 * attempting to honor the Cruise Velocity, Acceleration, and Jerk
154 * value specified via the Motion Magic® configuration values.
155 * Target position can be changed on-the-fly and Motion Magic® will
156 * do its best to adjust the profile. This control mode is based
157 * on torque current, so relevant closed-loop gains will use
158 * Amperes for the numerator.
159 *
160 * \param Position Position to drive toward in rotations.
161 */
163 {}
164
165 /**
166 * \brief Modifies this Control Request's Position parameter and returns itself for
167 * method-chaining and easier to use request API.
168 * \param newPosition Parameter to modify
169 * \returns Itself
170 */
171 MotionMagicTorqueCurrentFOC& WithPosition(units::angle::turn_t newPosition)
172 {
173 Position = newPosition;
174 return *this;
175 }
176
177 /**
178 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
179 * method-chaining and easier to use request API.
180 * \param newFeedForward Parameter to modify
181 * \returns Itself
182 */
183 MotionMagicTorqueCurrentFOC& WithFeedForward(units::dimensionless::scalar_t newFeedForward)
184 {
185 FeedForward = newFeedForward;
186 return *this;
187 }
188
189 /**
190 * \brief Modifies this Control Request's Slot parameter and returns itself for
191 * method-chaining and easier to use request API.
192 * \param newSlot Parameter to modify
193 * \returns Itself
194 */
196 {
197 Slot = newSlot;
198 return *this;
199 }
200
201 /**
202 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
203 * method-chaining and easier to use request API.
204 * \param newOverrideCoastDurNeutral Parameter to modify
205 * \returns Itself
206 */
208 {
209 OverrideCoastDurNeutral = newOverrideCoastDurNeutral;
210 return *this;
211 }
212 /**
213 * \brief Sets the period at which this control will update at.
214 * This is designated in Hertz, with a minimum of 20 Hz
215 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
216 *
217 * If this field is set to 0 Hz, the control request will
218 * be sent immediately as a one-shot frame. This may be useful
219 * for advanced applications that require outputs to be
220 * synchronized with data acquisition. In this case, we
221 * recommend not exceeding 50 ms between control calls.
222 *
223 * \param newUpdateFreqHz Parameter to modify
224 * \returns Itself
225 */
226 MotionMagicTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
227 {
228 UpdateFreqHz = newUpdateFreqHz;
229 return *this;
230 }
231 /**
232 * Returns a string representation of the object.
233 *
234 * \returns a string representation of the object.
235 */
236 std::string ToString() const
237 {
238 std::stringstream ss;
239 ss << "class: MotionMagicTorqueCurrentFOC" << std::endl;
240 ss << "Position: " << Position.to<double>() << std::endl;
241 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
242 ss << "Slot: " << Slot << std::endl;
243 ss << "OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
244 return ss.str();
245 }
246
247 /**
248 * \brief Forces configs to be applied the next time this is used in a setControl.
249 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
250 * properly set when they are not already set
251 */
252 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
253};
254
255}
256}
257}
258
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_RequestControlMotionMagicTorqueCurrentFOC(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Position, double FeedForward, int Slot, bool OverrideCoastDurNeutral)
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: MotionMagicTorqueCurrentFOC.hpp:33
MotionMagicTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: MotionMagicTorqueCurrentFOC.hpp:226
MotionMagicTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: MotionMagicTorqueCurrentFOC.hpp:195
MotionMagicTorqueCurrentFOC & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition: MotionMagicTorqueCurrentFOC.hpp:171
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition: MotionMagicTorqueCurrentFOC.hpp:87
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: MotionMagicTorqueCurrentFOC.hpp:94
MotionMagicTorqueCurrentFOC(units::angle::turn_t Position)
Requests Motion Magic® to target a final position using a motion profile.
Definition: MotionMagicTorqueCurrentFOC.hpp:162
MotionMagicTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition: MotionMagicTorqueCurrentFOC.hpp:207
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: MotionMagicTorqueCurrentFOC.hpp:107
std::string ToString() const
Returns a string representation of the object.
Definition: MotionMagicTorqueCurrentFOC.hpp:236
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: MotionMagicTorqueCurrentFOC.hpp:69
MotionMagicTorqueCurrentFOC & WithFeedForward(units::dimensionless::scalar_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition: MotionMagicTorqueCurrentFOC.hpp:183
units::dimensionless::scalar_t FeedForward
Feedforward to apply in torque current in Amperes.
Definition: MotionMagicTorqueCurrentFOC.hpp:74
int Slot
Select which gains are applied by selecting the slot.
Definition: MotionMagicTorqueCurrentFOC.hpp:80
MotionMagicTorqueCurrentFOC(units::angle::turn_t Position, units::dimensionless::scalar_t FeedForward, int Slot, bool OverrideCoastDurNeutral)
Requests Motion Magic® to target a final position using a motion profile.
Definition: MotionMagicTorqueCurrentFOC.hpp:139
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: MotionMagicTorqueCurrentFOC.hpp:252
Definition: string_util.hpp:14