CTRE Phoenix 6 C++ 26.0.0-beta-1
Loading...
Searching...
No Matches
SlotConfigs.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
13#include <unordered_map>
14
15namespace ctre {
16namespace phoenix6 {
17
18
19namespace configs {
20/**
21 * \brief Gains for the specified slot.
22 *
23 * \details If this slot is selected, these gains are used in closed
24 * loop control requests.
25 */
27 struct SlotSpns {
28 int kPSpn;
29 int kISpn;
30 int kDSpn;
31 int kSSpn;
32 int kVSpn;
33 int kASpn;
34 int kGSpn;
35 int GravityTypeSpn;
36 int StaticFeedforwardSignSpn;
37 int GravityArmPositionOffsetSpn;
38 int GainSchedBehaviorSpn;
39 };
40
41 static std::unordered_map<int, SlotSpns> const genericMap;
42
43public:
44 constexpr SlotConfigs() = default;
45
46 /**
47 * \brief Proportional gain.
48 *
49 * \details The units for this gain is dependent on the control mode.
50 * Since this gain is multiplied by error in the input, the units
51 * should be defined as units of output per unit of input error. For
52 * example, when controlling velocity using a duty cycle closed loop,
53 * the units for the proportional gain will be duty cycle per rps of
54 * error, or 1/rps.
55 *
56 * - Minimum Value: 0
57 * - Maximum Value: 3.4e+38
58 * - Default Value: 0
59 * - Units:
60 */
61 units::dimensionless::scalar_t kP = 0;
62 /**
63 * \brief Integral gain.
64 *
65 * \details The units for this gain is dependent on the control mode.
66 * Since this gain is multiplied by error in the input integrated over
67 * time (in units of seconds), the units should be defined as units of
68 * output per unit of integrated input error. For example, when
69 * controlling velocity using a duty cycle closed loop, integrating
70 * velocity over time results in rps * s = rotations. Therefore, the
71 * units for the integral gain will be duty cycle per rotation of
72 * accumulated error, or 1/rot.
73 *
74 * - Minimum Value: 0
75 * - Maximum Value: 3.4e+38
76 * - Default Value: 0
77 * - Units:
78 */
79 units::dimensionless::scalar_t kI = 0;
80 /**
81 * \brief Derivative gain.
82 *
83 * \details The units for this gain is dependent on the control mode.
84 * Since this gain is multiplied by the derivative of error in the
85 * input with respect to time (in units of seconds), the units should
86 * be defined as units of output per unit of the differentiated input
87 * error. For example, when controlling velocity using a duty cycle
88 * closed loop, the derivative of velocity with respect to time is rot
89 * per sec², which is acceleration. Therefore, the units for the
90 * derivative gain will be duty cycle per unit of acceleration error,
91 * or 1/(rot per sec²).
92 *
93 * - Minimum Value: 0
94 * - Maximum Value: 3.4e+38
95 * - Default Value: 0
96 * - Units:
97 */
98 units::dimensionless::scalar_t kD = 0;
99 /**
100 * \brief Static feedforward gain.
101 *
102 * \details This is added to the closed loop output. The unit for this
103 * constant is dependent on the control mode, typically fractional
104 * duty cycle, voltage, or torque current.
105 *
106 * The sign is typically determined by reference velocity when using
107 * position, velocity, and Motion Magic® closed loop modes. However,
108 * when using position closed loop with zero velocity reference (no
109 * motion profiling), the application can instead use the position
110 * closed loop error by setting the Static Feedforward Sign
111 * configuration parameter. When doing so, we recommend the minimal
112 * amount of kS, otherwise the motor output may dither when closed
113 * loop error is near zero.
114 *
115 * - Minimum Value: -128
116 * - Maximum Value: 127
117 * - Default Value: 0
118 * - Units:
119 */
120 units::dimensionless::scalar_t kS = 0;
121 /**
122 * \brief Velocity feedforward gain.
123 *
124 * \details The units for this gain is dependent on the control mode.
125 * Since this gain is multiplied by the requested velocity, the units
126 * should be defined as units of output per unit of requested input
127 * velocity. For example, when controlling velocity using a duty cycle
128 * closed loop, the units for the velocity feedfoward gain will be
129 * duty cycle per requested rps, or 1/rps.
130 *
131 * - Minimum Value: 0
132 * - Maximum Value: 3.4e+38
133 * - Default Value: 0
134 * - Units:
135 */
136 units::dimensionless::scalar_t kV = 0;
137 /**
138 * \brief Acceleration feedforward gain.
139 *
140 * \details The units for this gain is dependent on the control mode.
141 * Since this gain is multiplied by the requested acceleration, the
142 * units should be defined as units of output per unit of requested
143 * input acceleration. For example, when controlling velocity using a
144 * duty cycle closed loop, the units for the acceleration feedfoward
145 * gain will be duty cycle per requested rot per sec², or 1/(rot per
146 * sec²).
147 *
148 * - Minimum Value: 0
149 * - Maximum Value: 3.4e+38
150 * - Default Value: 0
151 * - Units:
152 */
153 units::dimensionless::scalar_t kA = 0;
154 /**
155 * \brief Gravity feedforward/feedback gain. The type of gravity
156 * compensation is selected by #GravityType.
157 *
158 * \details This is added to the closed loop output. The sign is
159 * determined by the gravity type. The unit for this constant is
160 * dependent on the control mode, typically fractional duty cycle,
161 * voltage, or torque current.
162 *
163 * - Minimum Value: -128
164 * - Maximum Value: 127
165 * - Default Value: 0
166 * - Units:
167 */
168 units::dimensionless::scalar_t kG = 0;
169 /**
170 * \brief Gravity feedforward/feedback type.
171 *
172 * This determines the type of the gravity feedforward/feedback.
173 *
174 * Choose Elevator_Static for systems where the gravity feedforward is
175 * constant, such as an elevator. The gravity feedforward output will
176 * always have the same sign.
177 *
178 * Choose Arm_Cosine for systems where the gravity feedback is
179 * dependent on the angular position of the mechanism, such as an arm.
180 * The gravity feedback output will vary depending on the mechanism
181 * angular position. Note that the sensor offset and ratios must be
182 * configured so that the sensor reports a position of 0 when the
183 * mechanism is horizonal (parallel to the ground), and the reported
184 * sensor position is 1:1 with the mechanism.
185 *
186 */
188 /**
189 * \brief Static feedforward sign during position closed loop.
190 *
191 * This determines the sign of the applied kS during position
192 * closed-loop modes. The default behavior uses the velocity reference
193 * sign. This works well with velocity closed loop, Motion Magic®
194 * controls, and position closed loop when velocity reference is
195 * specified (motion profiling).
196 *
197 * However, when using position closed loop with zero velocity
198 * reference (no motion profiling), the application may want to apply
199 * static feedforward based on the sign of closed loop error instead.
200 * When doing so, we recommend using the minimal amount of kS,
201 * otherwise the motor output may dither when closed loop error is
202 * near zero.
203 *
204 */
206 /**
207 * \brief Gravity feedback position offset when using the Arm/Cosine
208 * gravity type.
209 *
210 * This is an offset applied to the position of the arm, within
211 * (-0.25, 0.25) rot, before calculating the output of kG. This is
212 * useful when the center of gravity of the arm is offset from the
213 * actual zero point of the arm, such as when the arm and intake form
214 * an L shape.
215 *
216 * - Minimum Value: -0.25
217 * - Maximum Value: 0.25
218 * - Default Value: 0
219 * - Units: rotations
220 */
221 units::angle::turn_t GravityArmPositionOffset = 0_tr;
222 /**
223 * \brief The behavior of the gain scheduler on this slot. This
224 * specifies which gains to use while within the configured
225 * GainSchedErrorThreshold. The default is to continue using the
226 * specified slot.
227 *
228 * \details Gain scheduling will not take effect when running velocity
229 * closed-loop controls.
230 *
231 */
233
234 /**
235 * \brief Modifies this configuration's kP parameter and returns itself for
236 * method-chaining and easier to use config API.
237 *
238 * Proportional gain.
239 *
240 * \details The units for this gain is dependent on the control mode.
241 * Since this gain is multiplied by error in the input, the units
242 * should be defined as units of output per unit of input error. For
243 * example, when controlling velocity using a duty cycle closed loop,
244 * the units for the proportional gain will be duty cycle per rps of
245 * error, or 1/rps.
246 *
247 * - Minimum Value: 0
248 * - Maximum Value: 3.4e+38
249 * - Default Value: 0
250 * - Units:
251 *
252 * \param newKP Parameter to modify
253 * \returns Itself
254 */
255 constexpr SlotConfigs &WithKP(units::dimensionless::scalar_t newKP)
256 {
257 kP = std::move(newKP);
258 return *this;
259 }
260
261 /**
262 * \brief Modifies this configuration's kI parameter and returns itself for
263 * method-chaining and easier to use config API.
264 *
265 * Integral gain.
266 *
267 * \details The units for this gain is dependent on the control mode.
268 * Since this gain is multiplied by error in the input integrated over
269 * time (in units of seconds), the units should be defined as units of
270 * output per unit of integrated input error. For example, when
271 * controlling velocity using a duty cycle closed loop, integrating
272 * velocity over time results in rps * s = rotations. Therefore, the
273 * units for the integral gain will be duty cycle per rotation of
274 * accumulated error, or 1/rot.
275 *
276 * - Minimum Value: 0
277 * - Maximum Value: 3.4e+38
278 * - Default Value: 0
279 * - Units:
280 *
281 * \param newKI Parameter to modify
282 * \returns Itself
283 */
284 constexpr SlotConfigs &WithKI(units::dimensionless::scalar_t newKI)
285 {
286 kI = std::move(newKI);
287 return *this;
288 }
289
290 /**
291 * \brief Modifies this configuration's kD parameter and returns itself for
292 * method-chaining and easier to use config API.
293 *
294 * Derivative gain.
295 *
296 * \details The units for this gain is dependent on the control mode.
297 * Since this gain is multiplied by the derivative of error in the
298 * input with respect to time (in units of seconds), the units should
299 * be defined as units of output per unit of the differentiated input
300 * error. For example, when controlling velocity using a duty cycle
301 * closed loop, the derivative of velocity with respect to time is rot
302 * per sec², which is acceleration. Therefore, the units for the
303 * derivative gain will be duty cycle per unit of acceleration error,
304 * or 1/(rot per sec²).
305 *
306 * - Minimum Value: 0
307 * - Maximum Value: 3.4e+38
308 * - Default Value: 0
309 * - Units:
310 *
311 * \param newKD Parameter to modify
312 * \returns Itself
313 */
314 constexpr SlotConfigs &WithKD(units::dimensionless::scalar_t newKD)
315 {
316 kD = std::move(newKD);
317 return *this;
318 }
319
320 /**
321 * \brief Modifies this configuration's kS parameter and returns itself for
322 * method-chaining and easier to use config API.
323 *
324 * Static feedforward gain.
325 *
326 * \details This is added to the closed loop output. The unit for this
327 * constant is dependent on the control mode, typically fractional
328 * duty cycle, voltage, or torque current.
329 *
330 * The sign is typically determined by reference velocity when using
331 * position, velocity, and Motion Magic® closed loop modes. However,
332 * when using position closed loop with zero velocity reference (no
333 * motion profiling), the application can instead use the position
334 * closed loop error by setting the Static Feedforward Sign
335 * configuration parameter. When doing so, we recommend the minimal
336 * amount of kS, otherwise the motor output may dither when closed
337 * loop error is near zero.
338 *
339 * - Minimum Value: -128
340 * - Maximum Value: 127
341 * - Default Value: 0
342 * - Units:
343 *
344 * \param newKS Parameter to modify
345 * \returns Itself
346 */
347 constexpr SlotConfigs &WithKS(units::dimensionless::scalar_t newKS)
348 {
349 kS = std::move(newKS);
350 return *this;
351 }
352
353 /**
354 * \brief Modifies this configuration's kV parameter and returns itself for
355 * method-chaining and easier to use config API.
356 *
357 * Velocity feedforward gain.
358 *
359 * \details The units for this gain is dependent on the control mode.
360 * Since this gain is multiplied by the requested velocity, the units
361 * should be defined as units of output per unit of requested input
362 * velocity. For example, when controlling velocity using a duty cycle
363 * closed loop, the units for the velocity feedfoward gain will be
364 * duty cycle per requested rps, or 1/rps.
365 *
366 * - Minimum Value: 0
367 * - Maximum Value: 3.4e+38
368 * - Default Value: 0
369 * - Units:
370 *
371 * \param newKV Parameter to modify
372 * \returns Itself
373 */
374 constexpr SlotConfigs &WithKV(units::dimensionless::scalar_t newKV)
375 {
376 kV = std::move(newKV);
377 return *this;
378 }
379
380 /**
381 * \brief Modifies this configuration's kA parameter and returns itself for
382 * method-chaining and easier to use config API.
383 *
384 * Acceleration feedforward gain.
385 *
386 * \details The units for this gain is dependent on the control mode.
387 * Since this gain is multiplied by the requested acceleration, the
388 * units should be defined as units of output per unit of requested
389 * input acceleration. For example, when controlling velocity using a
390 * duty cycle closed loop, the units for the acceleration feedfoward
391 * gain will be duty cycle per requested rot per sec², or 1/(rot per
392 * sec²).
393 *
394 * - Minimum Value: 0
395 * - Maximum Value: 3.4e+38
396 * - Default Value: 0
397 * - Units:
398 *
399 * \param newKA Parameter to modify
400 * \returns Itself
401 */
402 constexpr SlotConfigs &WithKA(units::dimensionless::scalar_t newKA)
403 {
404 kA = std::move(newKA);
405 return *this;
406 }
407
408 /**
409 * \brief Modifies this configuration's kG parameter and returns itself for
410 * method-chaining and easier to use config API.
411 *
412 * Gravity feedforward/feedback gain. The type of gravity compensation
413 * is selected by #GravityType.
414 *
415 * \details This is added to the closed loop output. The sign is
416 * determined by the gravity type. The unit for this constant is
417 * dependent on the control mode, typically fractional duty cycle,
418 * voltage, or torque current.
419 *
420 * - Minimum Value: -128
421 * - Maximum Value: 127
422 * - Default Value: 0
423 * - Units:
424 *
425 * \param newKG Parameter to modify
426 * \returns Itself
427 */
428 constexpr SlotConfigs &WithKG(units::dimensionless::scalar_t newKG)
429 {
430 kG = std::move(newKG);
431 return *this;
432 }
433
434 /**
435 * \brief Modifies this configuration's GravityType parameter and returns itself for
436 * method-chaining and easier to use config API.
437 *
438 * Gravity feedforward/feedback type.
439 *
440 * This determines the type of the gravity feedforward/feedback.
441 *
442 * Choose Elevator_Static for systems where the gravity feedforward is
443 * constant, such as an elevator. The gravity feedforward output will
444 * always have the same sign.
445 *
446 * Choose Arm_Cosine for systems where the gravity feedback is
447 * dependent on the angular position of the mechanism, such as an arm.
448 * The gravity feedback output will vary depending on the mechanism
449 * angular position. Note that the sensor offset and ratios must be
450 * configured so that the sensor reports a position of 0 when the
451 * mechanism is horizonal (parallel to the ground), and the reported
452 * sensor position is 1:1 with the mechanism.
453 *
454 *
455 * \param newGravityType Parameter to modify
456 * \returns Itself
457 */
459 {
460 GravityType = std::move(newGravityType);
461 return *this;
462 }
463
464 /**
465 * \brief Modifies this configuration's StaticFeedforwardSign parameter and returns itself for
466 * method-chaining and easier to use config API.
467 *
468 * Static feedforward sign during position closed loop.
469 *
470 * This determines the sign of the applied kS during position
471 * closed-loop modes. The default behavior uses the velocity reference
472 * sign. This works well with velocity closed loop, Motion Magic®
473 * controls, and position closed loop when velocity reference is
474 * specified (motion profiling).
475 *
476 * However, when using position closed loop with zero velocity
477 * reference (no motion profiling), the application may want to apply
478 * static feedforward based on the sign of closed loop error instead.
479 * When doing so, we recommend using the minimal amount of kS,
480 * otherwise the motor output may dither when closed loop error is
481 * near zero.
482 *
483 *
484 * \param newStaticFeedforwardSign Parameter to modify
485 * \returns Itself
486 */
488 {
489 StaticFeedforwardSign = std::move(newStaticFeedforwardSign);
490 return *this;
491 }
492
493 /**
494 * \brief Modifies this configuration's GravityArmPositionOffset parameter and returns itself for
495 * method-chaining and easier to use config API.
496 *
497 * Gravity feedback position offset when using the Arm/Cosine gravity
498 * type.
499 *
500 * This is an offset applied to the position of the arm, within
501 * (-0.25, 0.25) rot, before calculating the output of kG. This is
502 * useful when the center of gravity of the arm is offset from the
503 * actual zero point of the arm, such as when the arm and intake form
504 * an L shape.
505 *
506 * - Minimum Value: -0.25
507 * - Maximum Value: 0.25
508 * - Default Value: 0
509 * - Units: rotations
510 *
511 * \param newGravityArmPositionOffset Parameter to modify
512 * \returns Itself
513 */
514 constexpr SlotConfigs &WithGravityArmPositionOffset(units::angle::turn_t newGravityArmPositionOffset)
515 {
516 GravityArmPositionOffset = std::move(newGravityArmPositionOffset);
517 return *this;
518 }
519
520 /**
521 * \brief Modifies this configuration's GainSchedBehavior parameter and returns itself for
522 * method-chaining and easier to use config API.
523 *
524 * The behavior of the gain scheduler on this slot. This specifies
525 * which gains to use while within the configured
526 * GainSchedErrorThreshold. The default is to continue using the
527 * specified slot.
528 *
529 * \details Gain scheduling will not take effect when running velocity
530 * closed-loop controls.
531 *
532 *
533 * \param newGainSchedBehavior Parameter to modify
534 * \returns Itself
535 */
537 {
538 GainSchedBehavior = std::move(newGainSchedBehavior);
539 return *this;
540 }
541
542
543 /**
544 * \brief Chooses which slot these configs are for.
545 */
546 int SlotNumber = 0;
547
548 /**
549 * Converts the provided value to an instance of this type.
550 *
551 * \param value The value to convert
552 * \returns Converted value
553 */
554 static SlotConfigs From(Slot0Configs const &value);
555 /**
556 * Converts the provided value to an instance of this type.
557 *
558 * \param value The value to convert
559 * \returns Converted value
560 */
561 static SlotConfigs From(Slot1Configs const &value);
562 /**
563 * Converts the provided value to an instance of this type.
564 *
565 * \param value The value to convert
566 * \returns Converted value
567 */
568 static SlotConfigs From(Slot2Configs const &value);
569
570 std::string ToString() const override;
571
572 std::string Serialize() const final;
573 ctre::phoenix::StatusCode Deserialize(std::string const &to_deserialize) final;
574};
575
576}
577}
578}
Definition Configuration.hpp:17
Gains for the specified slot.
Definition Slot0Configs.hpp:26
Gains for the specified slot.
Definition Slot1Configs.hpp:26
Gains for the specified slot.
Definition Slot2Configs.hpp:26
Gains for the specified slot.
Definition SlotConfigs.hpp:26
constexpr SlotConfigs & WithGravityType(signals::GravityTypeValue newGravityType)
Modifies this configuration's GravityType parameter and returns itself for method-chaining and easier...
Definition SlotConfigs.hpp:458
units::dimensionless::scalar_t kI
Integral gain.
Definition SlotConfigs.hpp:79
static SlotConfigs From(Slot1Configs const &value)
Converts the provided value to an instance of this type.
constexpr SlotConfigs & WithKS(units::dimensionless::scalar_t newKS)
Modifies this configuration's kS parameter and returns itself for method-chaining and easier to use c...
Definition SlotConfigs.hpp:347
constexpr SlotConfigs & WithStaticFeedforwardSign(signals::StaticFeedforwardSignValue newStaticFeedforwardSign)
Modifies this configuration's StaticFeedforwardSign parameter and returns itself for method-chaining ...
Definition SlotConfigs.hpp:487
units::angle::turn_t GravityArmPositionOffset
Gravity feedback position offset when using the Arm/Cosine gravity type.
Definition SlotConfigs.hpp:221
signals::GainSchedBehaviorValue GainSchedBehavior
The behavior of the gain scheduler on this slot.
Definition SlotConfigs.hpp:232
std::string ToString() const override
static SlotConfigs From(Slot2Configs const &value)
Converts the provided value to an instance of this type.
std::string Serialize() const final
constexpr SlotConfigs & WithKD(units::dimensionless::scalar_t newKD)
Modifies this configuration's kD parameter and returns itself for method-chaining and easier to use c...
Definition SlotConfigs.hpp:314
signals::StaticFeedforwardSignValue StaticFeedforwardSign
Static feedforward sign during position closed loop.
Definition SlotConfigs.hpp:205
units::dimensionless::scalar_t kV
Velocity feedforward gain.
Definition SlotConfigs.hpp:136
units::dimensionless::scalar_t kS
Static feedforward gain.
Definition SlotConfigs.hpp:120
constexpr SlotConfigs & WithKV(units::dimensionless::scalar_t newKV)
Modifies this configuration's kV parameter and returns itself for method-chaining and easier to use c...
Definition SlotConfigs.hpp:374
units::dimensionless::scalar_t kP
Proportional gain.
Definition SlotConfigs.hpp:61
constexpr SlotConfigs & WithGainSchedBehavior(signals::GainSchedBehaviorValue newGainSchedBehavior)
Modifies this configuration's GainSchedBehavior parameter and returns itself for method-chaining and ...
Definition SlotConfigs.hpp:536
constexpr SlotConfigs & WithGravityArmPositionOffset(units::angle::turn_t newGravityArmPositionOffset)
Modifies this configuration's GravityArmPositionOffset parameter and returns itself for method-chaini...
Definition SlotConfigs.hpp:514
constexpr SlotConfigs & WithKA(units::dimensionless::scalar_t newKA)
Modifies this configuration's kA parameter and returns itself for method-chaining and easier to use c...
Definition SlotConfigs.hpp:402
int SlotNumber
Chooses which slot these configs are for.
Definition SlotConfigs.hpp:546
units::dimensionless::scalar_t kD
Derivative gain.
Definition SlotConfigs.hpp:98
static SlotConfigs From(Slot0Configs const &value)
Converts the provided value to an instance of this type.
constexpr SlotConfigs & WithKP(units::dimensionless::scalar_t newKP)
Modifies this configuration's kP parameter and returns itself for method-chaining and easier to use c...
Definition SlotConfigs.hpp:255
units::dimensionless::scalar_t kA
Acceleration feedforward gain.
Definition SlotConfigs.hpp:153
ctre::phoenix::StatusCode Deserialize(std::string const &to_deserialize) final
constexpr SlotConfigs & WithKI(units::dimensionless::scalar_t newKI)
Modifies this configuration's kI parameter and returns itself for method-chaining and easier to use c...
Definition SlotConfigs.hpp:284
signals::GravityTypeValue GravityType
Gravity feedforward/feedback type.
Definition SlotConfigs.hpp:187
constexpr SlotConfigs & WithKG(units::dimensionless::scalar_t newKG)
Modifies this configuration's kG parameter and returns itself for method-chaining and easier to use c...
Definition SlotConfigs.hpp:428
units::dimensionless::scalar_t kG
Gravity feedforward/feedback gain.
Definition SlotConfigs.hpp:168
Definition motor_constants.h:14
The behavior of the gain scheduler on this slot.
Definition SpnEnums.hpp:5059
static constexpr int Inactive
No gain scheduling will occur.
Definition SpnEnums.hpp:5065
Gravity feedforward/feedback type.
Definition SpnEnums.hpp:1488
static constexpr int Elevator_Static
The system's gravity feedforward is constant, such as an elevator.
Definition SpnEnums.hpp:1495
Static feedforward sign during position closed loop.
Definition SpnEnums.hpp:2475
static constexpr int UseVelocitySign
Use the velocity reference sign.
Definition SpnEnums.hpp:2483