1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (c) 2024-2026 REV Robotics
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of REV Robotics nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <string>
#include "rev/ClosedLoopTypes.h"
#include "rev/config/BaseConfig.h"
#include "rev/config/FeedForwardConfig.h"
#include "rev/config/MAXMotionConfig.h"
namespace rev::detached {
class DetachedEncoder;
}
namespace rev::spark {
class ClosedLoopConfig : public BaseConfig {
public:
ClosedLoopConfig() = default;
~ClosedLoopConfig() override = default;
ClosedLoopConfig(const ClosedLoopConfig&) = default;
ClosedLoopConfig& operator=(const ClosedLoopConfig&) = delete;
ClosedLoopConfig(ClosedLoopConfig&&) noexcept = delete;
ClosedLoopConfig& operator=(ClosedLoopConfig&&) noexcept = delete;
/**
* Applies settings from another ClosedLoopConfig to this one,
* including all of its nested configurations.
*
* <p>Settings in the provided config will overwrite existing values in this
* object. Settings not specified in the provided config remain unchanged.
*
* @param config The ClosedLoopConfig to copy settings from
* @return The updated ClosedLoopConfig for method chaining
*/
ClosedLoopConfig& Apply(ClosedLoopConfig& config);
/**
* Applies settings from a MAXMotionConfig to this {@link
* ClosedLoopConfig}.
*
* <p>Settings in the provided config will overwrite existing values in this
* object. Settings not specified in the provided config remain unchanged.
*
* @param config The MAXMotionConfig to copy settings from
* @return The updated ClosedLoopConfig for method chaining
*/
ClosedLoopConfig& Apply(MAXMotionConfig& config);
/**
* Applies settings from a FeedForwardConfig to this {@link
* ClosedLoopConfig}.
*
* <p>Settings in the provided config will overwrite existing values in this
* object. Settings not specified in the provided config remain unchanged.
*
* @param config The FeedForwardConfig to copy settings from
* @return The updated ClosedLoopConfig for method chaining
*/
ClosedLoopConfig& Apply(FeedForwardConfig& config);
/**
* Set the PIDF gains of the controller for a specific closed loop slot.
*
* @param p The proportional gain value
* @param i The integral gain value
* @param d The derivative gain value
* @param ff The velocity feedforward value
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
* @deprecated Use FeedForwardConfig::V(double v, ClosedLoopSlot slot) to
* set a Velocity Feedforward
*/
[[deprecated(
"Use FeedForwardConfig::V(double v, ClosedLoopSlot slot) to set a "
"Velocity Feedforward")]] ClosedLoopConfig&
Pidf(double p, double i, double d, double ff, ClosedLoopSlot slot = kSlot0);
/**
* Set the PID gains of the controller for a specific closed loop slot.
*
* @param p The proportional gain value
* @param i The integral gain value
* @param d The derivative gain value
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& Pid(double p, double i, double d,
ClosedLoopSlot slot = kSlot0);
/**
* Set the proportional gain of the controller for a specific closed loop
* slot.
*
* @param p The proportional gain value
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& P(double p, ClosedLoopSlot slot = kSlot0);
/**
* Set the integral gain of the controller for a specific closed loop slot.
*
* @param i The integral gain value
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& I(double i, ClosedLoopSlot slot = kSlot0);
/**
* Set the derivative gain of the controller for a specific closed loop
* slot.
*
* @param d The derivative gain value
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& D(double d, ClosedLoopSlot slot = kSlot0);
/**
* Set the velocity feedforward gain of the controller for a specific closed
* loop slot.
*
* @param ff The velocity feedforward gain value
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
* @deprecated Use FeedForwardConfig::V(double v, ClosedLoopSlot slot) to
* set this gain
*/
[[deprecated(
"Use FeedForwardConfig::V(double v, ClosedLoopSlot slot) to set this "
"gain")]] ClosedLoopConfig&
VelocityFF(double ff, ClosedLoopSlot slot = kSlot0);
/**
* Set the derivative filter of the controller for a specific closed loop
* slot.
*
* @param dFilter The derivative filter value
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& DFilter(double dFilter, ClosedLoopSlot slot = kSlot0);
/**
* Set the integral zone of the controller for a specific closed loop slot.
*
* @param iZone The integral zone value
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& IZone(double iZone, ClosedLoopSlot slot = kSlot0);
/**
* Set the minimum output of the controller for a specific closed loop slot.
*
* @param minOutput The minimum output value in the range [-1, 1]
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& MinOutput(double minOutput, ClosedLoopSlot slot = kSlot0);
/**
* Set the maximum output of the controller for a specific closed loop slot.
*
* @param maxOutput The maximum output value in the range [-1, 1]
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& MaxOutput(double maxOutput, ClosedLoopSlot slot = kSlot0);
/**
* Set the output range of the controller for a specific closed loop slot.
*
* @param minOutput The minimum output value in the range [-1, 1]
* @param maxOutput The maximum output value in the range [-1, 1]
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& OutputRange(double minOutput, double maxOutput,
ClosedLoopSlot slot = kSlot0);
/**
* Set the maximum I accumulator of the controller for a specific closed
* loop slot. This value is used to constrain the I accumulator to help
* manage integral wind-up.
*
* @param iMaxAccum The max value to constrain the I accumulator to
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& IMaxAccum(double iMaxAccum, ClosedLoopSlot slot = kSlot0);
/**
* Set the allowed closed loop error for the controller for a specific
* PID slot. This value is how much deviation from the setpoint is
* tolerated and is useful in preventing oscillation around the setpoint.
* Natively, the units are in rotations but will be affected by the
* position conversion factor.
*
* @param allowedError The allowed error with the position conversion factor
* applied
* @param slot The closed loop slot to set the values for
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& AllowedClosedLoopError(double allowedError,
ClosedLoopSlot slot = kSlot0);
/**
* Enable or disable PID wrapping for position closed loop control.
*
* @param enabled True to enable position PID wrapping
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& PositionWrappingEnabled(bool enabled);
/**
* Set the minimum input value for PID wrapping with position closed loop
* control.
*
* @param minInput The value of min input for the position
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& PositionWrappingMinInput(double minInput);
/**
* Set the maximum input value for PID wrapping with position closed loop
* control
*
* @param maxInput The value of max input for the position
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& PositionWrappingMaxInput(double maxInput);
/**
* Set the input range for PID wrapping with position closed loop control
*
* @param minInput The value of min input for the position
* @param maxInput The value of max input for the position
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& PositionWrappingInputRange(double minInput,
double maxInput);
/**
* Set an attached sensor as the feedback sensor of the controller.
* The controller will use this sensor as the source of feedback for its
* closed loop control.
*
* To set a detached encoder, use SetFeedbackSensor(FeedbackSensor, int) or
* SetFeedbackSensor(FeedbackSensor, rev::detached::DetachedEncoder&)
* instead. This method will ignore @c sensor set to a Detached sensor.
*
* <p>The default feedback sensor is assumed to be the primary encoder for
* either brushless or brushed mode. This can be changed to another feedback
* sensor for the controller such as an analog sensor, absolute encoder, or
* alternate/external encoder.
*
* @param sensor The attached feedback sensor
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& SetFeedbackSensor(FeedbackSensor sensor);
/**
* Set an external CAN Detached Encoder as the feedback sensor of the
* controller. The controller will use this sensor as the source of feedback
* for its closed loop control.
*
* To set an attached encoder, use the SetFeedbackSensor(FeedbackSensor)
* method instead. This method will ignore @c sensor set to an attached
* sensor.
*
* @param sensor The detached feedback sensor
* @detachedEncoderDeviceId The device ID of the detached CAN encoder to use
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& SetFeedbackSensor(FeedbackSensor sensor,
int detachedEncoderDeviceId);
/**
* Set an external CAN Detached Encoder as the feedback sensor of the
* controller. The controller will use this sensor as the source of feedback
* for its closed loop control.
*
* To set an attached encoder, use the SetFeedbackSensor(FeedbackSensor)
* method instead. This method will ignore @c sensor set to an attached
* sensor.
*
* @param sensor The detached feedback sensor
* @splineEncoder The detached CAN encoder to use
* @return The modified ClosedLoopConfig object for method chaining
*/
ClosedLoopConfig& SetFeedbackSensor(
FeedbackSensor sensor,
const rev::detached::DetachedEncoder& detachedEncoder);
std::string Flatten() override;
MAXMotionConfig maxMotion;
FeedForwardConfig feedForward;
}; // ClosedLoopConfig
} // namespace rev::spark