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