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
PositionTorqueCurrentFOC.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/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 position with torque current feedforward.
27 *
28 * This control mode will set the motor's position setpoint to the position 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<PositionTorqueCurrentFOC *>(req.get());
40 if (reqCast != nullptr)
41 {
42 *reqCast = *this;
43 }
44 else
45 {
46 req = std::make_shared<PositionTorqueCurrentFOC>(*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_RequestControlPositionTorqueCurrentFOC(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Position.to<double>(), Velocity.to<double>(), FeedForward.to<double>(), Slot, OverrideCoastDurNeutral);
56 }
57
58public:
59 /**
60 * Position to drive toward in rotations.
61 */
62 units::angle::turn_t Position;
63 /**
64 * Velocity to drive toward in rotations per second.
65 */
66 units::angular_velocity::turns_per_second_t Velocity;
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 position with torque
109 * current feedforward.
110 *
111 * \details This control mode will set the motor's position setpoint to the
112 * position specified by the user. In addition, it will apply an
113 * additional torque current as an arbitrary feedforward value.
114 *
115 * \param Position Position to drive toward in rotations.
116 * \param Velocity Velocity to drive toward in rotations per second.
117 * \param FeedForward Feedforward to apply in torque current in Amperes.
118 * User can use motor's kT to scale Newton-meter to
119 * Amperes.
120 * \param Slot Select which gains are applied by selecting the slot. Use the
121 * configuration api to set the gain values for the selected slot
122 * before enabling this feature. Slot must be within [0,2].
123 * \param OverrideCoastDurNeutral Set to true to coast the rotor when output
124 * is zero (or within deadband). Set to false
125 * to use the NeutralMode configuration
126 * setting (default). This flag exists to
127 * provide the fundamental behavior of this
128 * control when output is zero, which is to
129 * provide 0A (zero torque).
130 */
131 PositionTorqueCurrentFOC(units::angle::turn_t Position, units::angular_velocity::turns_per_second_t Velocity, units::current::ampere_t FeedForward, int Slot, bool OverrideCoastDurNeutral) : ControlRequest{"PositionTorqueCurrentFOC"},
132 Position{std::move(Position)},
133 Velocity{std::move(Velocity)},
134 FeedForward{std::move(FeedForward)},
135 Slot{std::move(Slot)},
137 {}
138
139 /**
140 * \brief Requires Phoenix Pro;
141 * Request PID to target position with torque
142 * current feedforward.
143 *
144 * \details This control mode will set the motor's position setpoint to the
145 * position specified by the user. In addition, it will apply an
146 * additional torque current as an arbitrary feedforward value.
147 *
148 * \param Position Position to drive toward in rotations.
149 */
150 PositionTorqueCurrentFOC(units::angle::turn_t Position) : PositionTorqueCurrentFOC{Position, 0.0_tps, 0.0_A, 0, false}
151 {}
152
153 /**
154 * \brief Modifies this Control Request's Position parameter and returns itself for
155 * method-chaining and easier to use request API.
156 * \param newPosition Parameter to modify
157 * \returns Itself
158 */
159 PositionTorqueCurrentFOC& WithPosition(units::angle::turn_t newPosition)
160 {
161 Position = std::move(newPosition);
162 return *this;
163 }
164
165 /**
166 * \brief Modifies this Control Request's Velocity parameter and returns itself for
167 * method-chaining and easier to use request API.
168 * \param newVelocity Parameter to modify
169 * \returns Itself
170 */
171 PositionTorqueCurrentFOC& WithVelocity(units::angular_velocity::turns_per_second_t newVelocity)
172 {
173 Velocity = std::move(newVelocity);
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 PositionTorqueCurrentFOC& WithFeedForward(units::current::ampere_t newFeedForward)
184 {
185 FeedForward = std::move(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 = std::move(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 = std::move(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 PositionTorqueCurrentFOC &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 override
237 {
238 std::stringstream ss;
239 ss << "class: PositionTorqueCurrentFOC" << std::endl;
240 ss << "Position: " << Position.to<double>() << std::endl;
241 ss << "Velocity: " << Velocity.to<double>() << std::endl;
242 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
243 ss << "Slot: " << Slot << std::endl;
244 ss << "OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
245 return ss.str();
246 }
247
248 /**
249 * \brief Forces configs to be applied the next time this is used in a setControl.
250 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
251 * properly set when they are not already set
252 */
253 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
254
255 /**
256 * \brief Gets information about this control request.
257 *
258 * \returns Map of control parameter names and corresponding applied values
259 */
260 std::map<std::string, std::string> GetControlInfo() const override
261 {
262 std::map<std::string, std::string> controlInfo;
263 std::stringstream ss;
264 controlInfo["Name"] = GetName();
265 ss << Position.to<double>(); controlInfo["Position"] = ss.str(); ss.str(std::string{});
266 ss << Velocity.to<double>(); controlInfo["Velocity"] = ss.str(); ss.str(std::string{});
267 ss << FeedForward.to<double>(); controlInfo["FeedForward"] = ss.str(); ss.str(std::string{});
268 ss << Slot; controlInfo["Slot"] = ss.str(); ss.str(std::string{});
269 ss << OverrideCoastDurNeutral; controlInfo["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string{});
270 return controlInfo;
271 }
272};
273
274}
275}
276}
277
CTREXPORT int c_ctre_phoenix6_RequestControlPositionTorqueCurrentFOC(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Position, double Velocity, 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; Request PID to target position with torque current feedforward.
Definition: PositionTorqueCurrentFOC.hpp:32
int Slot
Select which gains are applied by selecting the slot.
Definition: PositionTorqueCurrentFOC.hpp:77
units::angular_velocity::turns_per_second_t Velocity
Velocity to drive toward in rotations per second.
Definition: PositionTorqueCurrentFOC.hpp:66
PositionTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition: PositionTorqueCurrentFOC.hpp:207
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: PositionTorqueCurrentFOC.hpp:91
PositionTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: PositionTorqueCurrentFOC.hpp:195
std::string ToString() const override
Returns a string representation of the object.
Definition: PositionTorqueCurrentFOC.hpp:236
PositionTorqueCurrentFOC & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition: PositionTorqueCurrentFOC.hpp:159
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: PositionTorqueCurrentFOC.hpp:62
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: PositionTorqueCurrentFOC.hpp:104
units::current::ampere_t FeedForward
Feedforward to apply in torque current in Amperes.
Definition: PositionTorqueCurrentFOC.hpp:71
PositionTorqueCurrentFOC(units::angle::turn_t Position, units::angular_velocity::turns_per_second_t Velocity, units::current::ampere_t FeedForward, int Slot, bool OverrideCoastDurNeutral)
Requires Phoenix Pro; Request PID to target position with torque current feedforward.
Definition: PositionTorqueCurrentFOC.hpp:131
PositionTorqueCurrentFOC & 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: PositionTorqueCurrentFOC.hpp:171
PositionTorqueCurrentFOC(units::angle::turn_t Position)
Requires Phoenix Pro; Request PID to target position with torque current feedforward.
Definition: PositionTorqueCurrentFOC.hpp:150
PositionTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: PositionTorqueCurrentFOC.hpp:226
std::map< std::string, std::string > GetControlInfo() const override
Gets information about this control request.
Definition: PositionTorqueCurrentFOC.hpp:260
PositionTorqueCurrentFOC & WithFeedForward(units::current::ampere_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition: PositionTorqueCurrentFOC.hpp:183
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition: PositionTorqueCurrentFOC.hpp:84
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: PositionTorqueCurrentFOC.hpp:253
Definition: ManualEvent.hpp:12