CTRE Phoenix Pro C++ 23.0.12
PositionVoltage.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/voltage.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 voltage 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 voltage 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 << EnableFOC; ref["EnableFOC"] = ss.str(); ss.str(std::string());
39 ss << FeedForward.to<double>(); ref["FeedForward"] = ss.str(); ss.str(std::string());
40 ss << Slot; ref["Slot"] = ss.str(); ss.str(std::string());
41 ss << OverrideBrakeDurNeutral; ref["OverrideBrakeDurNeutral"] = ss.str(); ss.str(std::string());
42
43 if (req.get() != this)
44 {
45 auto const reqCast = dynamic_cast<PositionVoltage *>(req.get());
46 if (reqCast != nullptr)
47 {
48 *reqCast = *this;
49 }
50 else
51 {
52 req = std::make_shared<PositionVoltage>(*this);
53 }
54 }
55
56
57 std::string strs{ss.str()};
58 c_ctre_phoenixpro_requestConfigApply(network, deviceHash, ConfigTimeout.to<double>(), strs.c_str(), strs.length(), ApplyConfigsOnRequest);
59 ApplyConfigsOnRequest = false;
60 return c_ctre_phoenixpro_RequestControlPositionVoltage(network, deviceHash, UpdateFreqHz.to<double>(), cancelOtherRequests, Position.to<double>(), EnableFOC, FeedForward.to<double>(), Slot, OverrideBrakeDurNeutral);
61 }
62
63public:
64 /**
65 * Position to drive toward in rotations.
66 */
67 units::angle::turn_t Position;
68 /**
69 * Set to true to use FOC commutation, which increases peak power by ~15%. Set
70 * to false to use trapezoidal commutation. FOC improves motor performance by
71 * leveraging torque (current) control. However, this may be inconvenient for
72 * applications that require specifying duty cycle or voltage. CTR-Electronics
73 * has developed a hybrid method that combines the performances gains of FOC
74 * while still allowing applications to provide duty cycle or voltage demand.
75 * This not to be confused with simple sinusoidal control or phase voltage
76 * control which lacks the performance gains.
77 */
79 /**
80 * Feedforward to apply in volts
81 */
82 units::voltage::volt_t FeedForward;
83 /**
84 * Select which gains are applied by selecting the slot. Use the configuration
85 * api to set the gain values for the selected slot before enabling this
86 * feature. Slot must be within [0,2].
87 */
88 int Slot;
89 /**
90 * Set to true to static-brake the rotor when output is zero (or within
91 * deadband). Set to false to use the NeutralMode configuration setting
92 * (default). This flag exists to provide the fundamental behavior of this
93 * control when output is zero, which is to provide 0V to the motor.
94 */
96
97
98
99 /**
100 * \brief The timeout when sending configs associated with this control
101 */
102 units::time::second_t ConfigTimeout{0.1_s};
103
104 /**
105 * \brief The period at which this control will update at.
106 * This is designated in Hertz, with a minimum of 20 Hz
107 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
108 *
109 * If this field is set to 0 Hz, the control request will
110 * be sent immediately as a one-shot frame. This may be useful
111 * for advanced applications that require outputs to be
112 * synchronized with data acquisition. In this case, we
113 * recommend not exceeding 50 ms between control calls.
114 */
115 units::frequency::hertz_t UpdateFreqHz{100_Hz}; // Default to 100_Hz
116
117 /**
118 *\brief Request PID to target position with voltage feedforward
119 *
120 *\details This control mode will set the motor's position setpoint to the
121 * position specified by the user. In addition, it will apply an
122 * additional voltage as an arbitrary feedforward value.
123 *
124 * \param Position Position to drive toward in rotations.
125 * \param EnableFOC Set to true to use FOC commutation, which increases
126 * peak power by ~15%. Set to false to use trapezoidal
127 * commutation. FOC improves motor performance by
128 * leveraging torque (current) control. However, this
129 * may be inconvenient for applications that require
130 * specifying duty cycle or voltage. CTR-Electronics
131 * has developed a hybrid method that combines the
132 * performances gains of FOC while still allowing
133 * applications to provide duty cycle or voltage
134 * demand. This not to be confused with simple
135 * sinusoidal control or phase voltage control which
136 * lacks the performance gains.
137 * \param FeedForward Feedforward to apply in volts
138 * \param Slot Select which gains are applied by selecting the slot.
139 * Use the configuration api to set the gain values for the
140 * selected slot before enabling this feature. Slot must be
141 * within [0,2].
142 * \param OverrideBrakeDurNeutral Set to true to static-brake the rotor
143 * when output is zero (or within
144 * deadband). Set to false to use the
145 * NeutralMode configuration setting
146 * (default). This flag exists to provide
147 * the fundamental behavior of this
148 * control when output is zero, which is
149 * to provide 0V to the motor.
150 */
151 PositionVoltage(units::angle::turn_t Position, bool EnableFOC, units::voltage::volt_t FeedForward, int Slot, bool OverrideBrakeDurNeutral) : ControlRequest{"PositionVoltage"}, ApplyConfigsOnRequest{false}
152 {
153 this->Position = Position;
154 this->EnableFOC = EnableFOC;
155 this->FeedForward = FeedForward;
156 this->Slot = Slot;
157 this->OverrideBrakeDurNeutral = OverrideBrakeDurNeutral;
158 }
159
160 /**
161 *\brief Request PID to target position with voltage feedforward
162 *
163 *\details This control mode will set the motor's position setpoint to the
164 * position specified by the user. In addition, it will apply an
165 * additional voltage as an arbitrary feedforward value.
166 *
167 * \param Position Position to drive toward in rotations.
168 */
169 PositionVoltage(units::angle::turn_t Position) : PositionVoltage{Position, true, 0.0_V, 0, false}
170 {}
171
172 /**
173 * \brief Modifies this Control Request's Position parameter and returns itself for
174 * method-chaining and easier to use request API.
175 * \param newPosition Parameter to modify
176 * \returns Itself
177 */
178 PositionVoltage& WithPosition(units::angle::turn_t newPosition)
179 {
180 Position = newPosition;
181 return *this;
182 }
183
184 /**
185 * \brief Modifies this Control Request's EnableFOC parameter and returns itself for
186 * method-chaining and easier to use request API.
187 * \param newEnableFOC Parameter to modify
188 * \returns Itself
189 */
190 PositionVoltage& WithEnableFOC(bool newEnableFOC)
191 {
192 EnableFOC = newEnableFOC;
193 return *this;
194 }
195
196 /**
197 * \brief Modifies this Control Request's FeedForward parameter and returns itself for
198 * method-chaining and easier to use request API.
199 * \param newFeedForward Parameter to modify
200 * \returns Itself
201 */
202 PositionVoltage& WithFeedForward(units::voltage::volt_t newFeedForward)
203 {
204 FeedForward = newFeedForward;
205 return *this;
206 }
207
208 /**
209 * \brief Modifies this Control Request's Slot parameter and returns itself for
210 * method-chaining and easier to use request API.
211 * \param newSlot Parameter to modify
212 * \returns Itself
213 */
215 {
216 Slot = newSlot;
217 return *this;
218 }
219
220 /**
221 * \brief Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for
222 * method-chaining and easier to use request API.
223 * \param newOverrideBrakeDurNeutral Parameter to modify
224 * \returns Itself
225 */
226 PositionVoltage& WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
227 {
228 OverrideBrakeDurNeutral = newOverrideBrakeDurNeutral;
229 return *this;
230 }
231 /**
232 * \brief Sets the period at which this control will update at.
233 * This is designated in Hertz, with a minimum of 20 Hz
234 * (every 50 ms) and a maximum of 1000 Hz (every 1 ms).
235 *
236 * If this field is set to 0 Hz, the control request will
237 * be sent immediately as a one-shot frame. This may be useful
238 * for advanced applications that require outputs to be
239 * synchronized with data acquisition. In this case, we
240 * recommend not exceeding 50 ms between control calls.
241 *
242 * \param newUpdateFreqHz Parameter to modify
243 * \returns Itself
244 */
245 PositionVoltage &WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
246 {
247 UpdateFreqHz = newUpdateFreqHz;
248 return *this;
249 }
250 /**
251 * Returns a string representation of the object.
252 *
253 * \returns a string representation of the object.
254 */
255 std::string ToString() const
256 {
257 std::stringstream ss;
258 ss << "class: PositionVoltage" << std::endl;
259 ss << "Position: " << Position.to<double>() << std::endl;
260 ss << "EnableFOC: " << EnableFOC << std::endl;
261 ss << "FeedForward: " << FeedForward.to<double>() << std::endl;
262 ss << "Slot: " << Slot << std::endl;
263 ss << "OverrideBrakeDurNeutral: " << OverrideBrakeDurNeutral << std::endl;
264 return ss.str();
265 }
266
267 /**
268 * \brief Forces configs to be applied the next time this is used in a setControl.
269 * This is not necessary in the majority of cases, because Phoenix will make sure configs are
270 * properly set when they are not already set
271 */
272 void ForceApplyConfigs() { ApplyConfigsOnRequest = true; }
273};
274
275}
276}
277}
278
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_RequestControlPositionVoltage(const char *canbus, uint32_t ecuEncoding, double updateTime, bool cancelOtherRequests, double Position, bool EnableFOC, double FeedForward, int Slot, bool OverrideBrakeDurNeutral)
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 voltage feedforward.
Definition: PositionVoltage.hpp:30
PositionVoltage(units::angle::turn_t Position, bool EnableFOC, units::voltage::volt_t FeedForward, int Slot, bool OverrideBrakeDurNeutral)
Request PID to target position with voltage feedforward.
Definition: PositionVoltage.hpp:151
PositionVoltage & WithFeedForward(units::voltage::volt_t newFeedForward)
Modifies this Control Request's FeedForward parameter and returns itself for method-chaining and easi...
Definition: PositionVoltage.hpp:202
std::string ToString() const
Returns a string representation of the object.
Definition: PositionVoltage.hpp:255
units::voltage::volt_t FeedForward
Feedforward to apply in volts.
Definition: PositionVoltage.hpp:82
PositionVoltage & WithOverrideBrakeDurNeutral(bool newOverrideBrakeDurNeutral)
Modifies this Control Request's OverrideBrakeDurNeutral parameter and returns itself for method-chain...
Definition: PositionVoltage.hpp:226
PositionVoltage(units::angle::turn_t Position)
Request PID to target position with voltage feedforward.
Definition: PositionVoltage.hpp:169
PositionVoltage & WithSlot(int newSlot)
Modifies this Control Request's Slot parameter and returns itself for method-chaining and easier to u...
Definition: PositionVoltage.hpp:214
void ForceApplyConfigs()
Forces configs to be applied the next time this is used in a setControl.
Definition: PositionVoltage.hpp:272
units::time::second_t ConfigTimeout
The timeout when sending configs associated with this control.
Definition: PositionVoltage.hpp:102
bool OverrideBrakeDurNeutral
Set to true to static-brake the rotor when output is zero (or within deadband).
Definition: PositionVoltage.hpp:95
bool EnableFOC
Set to true to use FOC commutation, which increases peak power by ~15%.
Definition: PositionVoltage.hpp:78
PositionVoltage & WithUpdateFreqHz(units::frequency::hertz_t newUpdateFreqHz)
Sets the period at which this control will update at.
Definition: PositionVoltage.hpp:245
units::angle::turn_t Position
Position to drive toward in rotations.
Definition: PositionVoltage.hpp:67
int Slot
Select which gains are applied by selecting the slot.
Definition: PositionVoltage.hpp:88
PositionVoltage & WithPosition(units::angle::turn_t newPosition)
Modifies this Control Request's Position parameter and returns itself for method-chaining and easier ...
Definition: PositionVoltage.hpp:178
units::frequency::hertz_t UpdateFreqHz
The period at which this control will update at.
Definition: PositionVoltage.hpp:115
PositionVoltage & WithEnableFOC(bool newEnableFOC)
Modifies this Control Request's EnableFOC parameter and returns itself for method-chaining and easier...
Definition: PositionVoltage.hpp:190
Definition: string_util.hpp:14