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