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
/*
* Copyright (c) 2018-2025 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
#ifdef _MSC_VER
// Disable deprecation warnings for this file when using VS compiler
#pragma warning(disable : 4996)
#endif
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include "rev/ClosedLoopTypes.h"
#include "rev/REVLibError.h"
#include "rev/SparkLowLevel.h"
namespace rev::spark {
class SparkBase;
class SparkClosedLoopController {
friend class SparkBase;
public:
/**
* Units for arbitrary feed-forward
*/
enum class ArbFFUnits { kVoltage = 0, kPercentOut = 1 };
SparkClosedLoopController(SparkClosedLoopController&&) = default;
SparkClosedLoopController& operator=(SparkClosedLoopController&&) = default;
SparkClosedLoopController(const SparkClosedLoopController& rhs) = default;
~SparkClosedLoopController() = default;
/**
* Set the controller setpoint based on the selected control mode.
*
* @param setpoint The setpoint to set depending on the control mode. For:
* - basic duty cycle control this should be a value between -1 and 1
* - Voltage Control: Voltage (volts)
* - Velocity Control: Velocity (RPM)
* - Position Control: Position (Rotations)
* - Current Control: Current (Amps).
* The units can be changed for position and velocity by a scale
* factor using AlternateEncoderConfig::PositionConversionFactor(), or
* ExternalEncoderConfig::PositionConversionFactor(), or
* EncoderConfig::PositionConversionFactor() or
* AlternateEncoderConfig::VelocityConversionFactor(), or
* ExternalEncoderConfig::VelocityConversionFactor(), or
* EncoderConfig::VelocityConversionFactor().
*
* @param ctrl Is the control type
*
* @param slot The ClosedLoopSlot to use
*
* @param arbFeedforward A value from -32.0 to 32.0 which is a voltage
* applied to the motor after the result of the specified control mode. The
* units for the parameter is Volts. This value is set after the control
* mode, but before any current limits or ramp rates.
*
* @param arbFFUnits the units for arbitrary feed-forward
* @return REVLibError::kOk if successful
*/
REVLibError SetSetpoint(double setpoint, SparkLowLevel::ControlType ctrl,
ClosedLoopSlot slot = ClosedLoopSlot::kSlot0,
double arbFeedforward = 0,
SparkClosedLoopController::ArbFFUnits arbFFUnits =
ArbFFUnits::kVoltage);
/**
* Set the controller setpoint based on the selected control mode.
*
* @param setpoint The setpoint to set depending on the control mode. For:
* - basic duty cycle control this should be a value between -1 and 1
* - Voltage Control: Voltage (volts)
* - Velocity Control: Velocity (RPM)
* - Position Control: Position (Rotations)
* - Current Control: Current (Amps).
* The units can be changed for position and velocity by a scale
* factor using AlternateEncoderConfig::PositionConversionFactor(), or
* ExternalEncoderConfig::PositionConversionFactor(), or
* EncoderConfig::PositionConversionFactor() or
* AlternateEncoderConfig::VelocityConversionFactor(), or
* ExternalEncoderConfig::VelocityConversionFactor(), or
* EncoderConfig::VelocityConversionFactor().
*
* @param ctrl Is the control type
*
* @param slot The ClosedLoopSlot to use
*
* @param arbFeedforward A value from -32.0 to 32.0 which is a voltage
* applied to the motor after the result of the specified control mode. The
* units for the parameter is Volts. This value is set after the control
* mode, but before any current limits or ramp rates.
*
* @param arbFFUnits the units for arbitrary feed-forward
* @return REVLibError::kOk if successful
* @deprecated Use SetSetpoint instead.
*/
[[deprecated]] REVLibError SetReference(
double setpoint, SparkLowLevel::ControlType ctrl,
ClosedLoopSlot slot = ClosedLoopSlot::kSlot0, double arbFeedforward = 0,
SparkClosedLoopController::ArbFFUnits arbFFUnits =
ArbFFUnits::kVoltage);
/**
* Get the selected control type used when SetReference() was called.
*
* @return The selected control type
*/
SparkLowLevel::ControlType GetControlType() const { return m_controlType; }
/**
* Set the I accumulator of the closed loop controller. This is useful when
* wishing to force a reset on the I accumulator of the Closed Loop
* Controller. You can also preset values to see how it will respond to
* certain I characteristics
*
* To use this function, the controller must be in a closed loop control
* mode by calling setReference()
*
* @param iAccum The value to set the I accumulator to
*
* @return REVLibError::kOk if successful
*/
REVLibError SetIAccum(double iAccum);
/**
* Get the I accumulator of the closed loop controller. This is useful when
* wishing to see what the I accumulator value is to help with PID tuning
*
* @return The value of the I accumulator
*/
double GetIAccum() const;
/**
* Get the internal setpoint of the closed loop controller.
*
* @return The internal setpoint
*/
double GetSetpoint() const;
/**
* Determine if the setpoint has been reached.
*
* @return true if the setpoint is reached; false otherwise
*/
bool IsAtSetpoint() const;
/**
* Get the selected closed loop PID slot.
*
* @return The selected closed loop PID slot
*/
ClosedLoopSlot GetSelectedSlot() const;
/**
* Get the MAXMotion internal setpoint position.
*
* <p>This will be 0 if the controller is not in a MAXMotion control mode.
*
* @return The MAXMotion internal setpoint position in rotations or units
* specified by the conversion factor.
*/
double GetMAXMotionSetpointPosition() const;
/**
* Get the MAXMotion internal setpoint velocity.
*
* <p>This will be 0 if the controller is not in a MAXMotion control mode.
*
* @return The MAXMotion internal setpoint velocity in rotations per minute
* or units specified by the conversion factor.
*/
double GetMAXMotionSetpointVelocity() const;
private:
SparkBase* m_device;
SparkLowLevel::ControlType m_controlType{
SparkLowLevel::ControlType::kDutyCycle};
explicit SparkClosedLoopController(SparkBase& device);
};
} // namespace rev::spark
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif