CTRE Phoenix Pro C++ 23.0.12
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/current.h>
15#include <units/frequency.h>
16#include <units/time.h>
17
18
19namespace ctre {
20namespace phoenixpro {
21namespace controls {
22
23/**
24 * Request PID to target position with torque current feedforward.
25 *
26 * This control mode will set the motor's position setpoint to the position specified by the user. In
27 * addition, it will apply an additional torque current as an arbitrary feedforward value.
28 */
30{
31 bool ApplyConfigsOnRequest;
32
33 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req)
34 {
35 std::stringstream ss;
36 auto& ref = requestReference.GetNameValues();
37 ss << Position.to<double>(); ref["Position"] = ss.str(); ss.str(std::string());
38 ss << FeedForward.to<double>(); ref["FeedForward"] = ss.str(); ss.str(std::string());
39 ss << Slot; ref["Slot"] = ss.str(); ss.str(std::string());
40 ss << OverrideCoastDurNeutral; ref["OverrideCoastDurNeutral"] = ss.str(); ss.str(std::string());
41
42 if (req.get() != this)
43 {
44 auto const reqCast = dynamic_cast<PositionTorqueCurrentFOC *>(req.get());
45 if (reqCast != nullptr)
46 {
47 *reqCast = *this;
48 }
49 else
50 {
51 req = std::make_shared<PositionTorqueCurrentFOC>(*this);
52 }
53 }
54
55
56 std::string strs{ss.str()};
57 c_ctre_phoenixpro_requestConfigApply(network, deviceHash, ConfigTimeout.to<double>(), strs.c_str(), strs.length(), ApplyConfigsOnRequest);
58 ApplyConfigsOnRequest = false;
59 return c_ctre_phoenixpro_RequestControlPositionTorqueCurrentFOC(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Position.to<double>(), FeedForward.to<double>(), Slot, OverrideCoastDurNeutral);
60 }
61
62public:
63 /**
64 * Position to drive toward in rotations.
65 */
66 units::angle::turn_t Position;
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 Request PID to target position with torque current feedforward.
108 *
109 *\details This control mode will set the motor's position setpoint to the
110 * position specified by the user. In addition, it will apply an
111 * additional torque current as an arbitrary feedforward value.
112 *
113 * \param Position Position to drive toward in rotations.
114 * \param FeedForward Feedforward to apply in torque current in Amperes.
115 * User can use motor's kT to scale Newton-meter to
116 * Amperes.
117 * \param Slot Select which gains are applied by selecting the slot.
118 * Use the configuration api to set the gain values for the
119 * selected slot before enabling this feature. Slot must be
120 * within [0,2].
121 * \param OverrideCoastDurNeutral Set to true to coast the rotor when
122 * output is zero (or within deadband).
123 * Set to false to use the NeutralMode
124 * configuration setting (default). This
125 * flag exists to provide the fundamental
126 * behavior of this control when output
127 * is zero, which is to provide 0A (zero
128 * torque).
129 */
130 PositionTorqueCurrentFOC(units::angle::turn_t Position, units::current::ampere_t FeedForward, int Slot, bool OverrideCoastDurNeutral) : ControlRequest{"PositionTorqueCurrentFOC"}, ApplyConfigsOnRequest{false}
131 {
132 this->Position = Position;
133 this->FeedForward = FeedForward;
134 this->Slot = Slot;
135 this->OverrideCoastDurNeutral = OverrideCoastDurNeutral;
136 }
137
138 /**
139 *\brief Request PID to target position with torque current feedforward.
140 *
141 *\details This control mode will set the motor's position setpoint to the
142 * position specified by the user. In addition, it will apply an
143 * additional torque current as an arbitrary feedforward value.
144 *
145 * \param Position Position to drive toward in rotations.
146 */
147 PositionTorqueCurrentFOC(units::angle::turn_t Position) : PositionTorqueCurrentFOC{Position, 0.0_A, 0, false}
148 {}
149
150 /**
151 * \brief Modifies this Control Request's Position parameter and returns itself for
152 * method-chaining and easier to use request API.
153 * \param newPosition Parameter to modify
154 * \returns Itself
155 */
156 PositionTorqueCurrentFOC& WithPosition(units::angle::turn_t newPosition)
157 {
158 Position = newPosition;
159 return *this;
160 }
161
162 /**
163 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
164 * method-chaining and easier to use request API.
165 * \param newFeedForward Parameter to modify
166 * \returns Itself
167 */
168 PositionTorqueCurrentFOC& WithFeedForward(units::current::ampere_t newFeedForward)
169 {
170 FeedForward = newFeedForward;
171 return *this;
172 }
173
174 /**
175 * \brief Modifies this Control Request's Slot parameter and returns itself for
176 * method-chaining and easier to use request API.
177 * \param newSlot Parameter to modify
178 * \returns Itself
179 */
181 {
182 Slot = newSlot;
183 return *this;
184 }
185
186 /**
187 * \brief Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for
188 * method-chaining and easier to use request API.
189 * \param newOverrideCoastDurNeutral Parameter to modify
190 * \returns Itself
191 */
193 {
194 OverrideCoastDurNeutral = newOverrideCoastDurNeutral;
195 return *this;
196 }
197 /**
198 * \brief Sets the period at which this control will update at.
199 * This is designated in Hertz, with a minimum of 20 Hz
200 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
201 *
202 * If this field is set to 0 Hz, the control request will
203 * be sent immediately as a one-shot frame. This may be useful
204 * for advanced applications that require outputs to be
205 * synchronized with data acquisition. In this case, we
206 * recommend not exceeding 50 ms between control calls.
207 *
208 * \param newUpdateFreqHz Parameter to modify
209 * \returns Itself
210 */
211 PositionTorqueCurrentFOC &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
212 {
213 UpdateFreqHz = newUpdateFreqHz;
214 return *this;
215 }
216 /**
217 * Returns a string representation of the object.
218 *
219 * \returns a string representation of the object.
220 */
221 std::string ToString() const
222 {
223 std::stringstream ss;
224 ss << "class: PositionTorqueCurrentFOC" << std::endl;
225 ss << "Position: " << Position.to<double>() << std::endl;
226 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
227 ss << "Slot: " << Slot << std::endl;
228 ss << "OverrideCoastDurNeutral: " << OverrideCoastDurNeutral << std::endl;
229 return ss.str();
230 }
231
232 /**
233 * \brief Forces configs to be applied the next time this is used in a setControl.
234 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
235 * properly set when they are not already set
236 */
237 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
238};
239
240}
241}
242}
243
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_RequestControlPositionTorqueCurrentFOC(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
Request PID to target position with torque current feedforward.
Definition: PositionTorqueCurrentFOC.hpp:30
bool OverrideCoastDurNeutral
Set to true to coast the rotor when output is zero (or within deadband).
Definition: PositionTorqueCurrentFOC.hpp:84
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:156
PositionTorqueCurrentFOC(units::angle::turn_t Position, units::current::ampere_t FeedForward, int Slot, bool OverrideCoastDurNeutral)
Request PID to target position with torque current feedforward.
Definition: PositionTorqueCurrentFOC.hpp:130
PositionTorqueCurrentFOC & WithOverrideCoastDurNeutral(bool newOverrideCoastDurNeutral)
Modifies this Control Request's OverrideCoastDurNeutral parameter and returns itself for method-chain...
Definition: PositionTorqueCurrentFOC.hpp:192
PositionTorqueCurrentFOC & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: PositionTorqueCurrentFOC.hpp:180
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: PositionTorqueCurrentFOC.hpp:91
PositionTorqueCurrentFOC & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: PositionTorqueCurrentFOC.hpp:211
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: PositionTorqueCurrentFOC.hpp:66
PositionTorqueCurrentFOC(units::angle::turn_t Position)
Request PID to target position with torque current feedforward.
Definition: PositionTorqueCurrentFOC.hpp:147
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: PositionTorqueCurrentFOC.hpp:237
units::current::ampere_t FeedForward
Feedforward to apply in torque current in Amperes.
Definition: PositionTorqueCurrentFOC.hpp:71
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: PositionTorqueCurrentFOC.hpp:104
int Slot
Select which gains are applied by selecting the slot.
Definition: PositionTorqueCurrentFOC.hpp:77
std::string ToString() const
Returns a string representation of the object.
Definition: PositionTorqueCurrentFOC.hpp:221
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:168
Definition: string_util.hpp:14