-
Notifications
You must be signed in to change notification settings - Fork 7
/
ProfileFollower.java
206 lines (180 loc) · 7.59 KB
/
ProfileFollower.java
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
package com.team254.lib.motion;
import com.team254.lib.motion.MotionProfileGoal.CompletionBehavior;
import com.team254.lib.util.Util;
/**
* A controller for tracking a profile generated to attain a MotionProfileGoal. The controller uses feedforward on
* acceleration, velocity, and position; proportional feedback on velocity and position; and integral feedback on
* position.
*/
public class ProfileFollower {
protected double mKp;
protected double mKi;
protected double mKv;
protected double mKffv;
protected double mKffa;
protected double mKs;
protected double mMinOutput = Double.NEGATIVE_INFINITY;
protected double mMaxOutput = Double.POSITIVE_INFINITY;
protected MotionState mLatestActualState;
protected MotionState mInitialState;
protected double mLatestPosError;
protected double mLatestVelError;
protected double mTotalError;
protected MotionProfileGoal mGoal = null;
protected MotionProfileConstraints mConstraints = null;
protected SetpointGenerator mSetpointGenerator = new SetpointGenerator();
protected SetpointGenerator.Setpoint mLatestSetpoint = null;
/**
* Create a new ProfileFollower.
*
* @param kp The proportional gain on position error.
* @param ki The integral gain on position error.
* @param kv The proportional gain on velocity error (or derivative gain on position error).
* @param kffv The feedforward gain on velocity. Should be 1.0 if the units of the profile match the units of the
* output.
* @param kffa The feedforward gain on acceleration.
* @param ks The throttle required to break static friction.
*/
public ProfileFollower(double kp, double ki, double kv, double kffv, double kffa,
double ks) {
resetProfile();
setGains(kp, ki, kv, kffv, kffa, ks);
}
public void setGains(double kp, double ki, double kv, double kffv, double kffa, double ks) {
mKp = kp;
mKi = ki;
mKv = kv;
mKffv = kffv;
mKffa = kffa;
mKs = ks;
}
/**
* Completely clear all state related to the current profile (min and max outputs are maintained).
*/
public void resetProfile() {
mTotalError = 0.0;
mInitialState = MotionState.kInvalidState;
mLatestActualState = MotionState.kInvalidState;
mLatestPosError = Double.NaN;
mLatestVelError = Double.NaN;
mSetpointGenerator.reset();
mGoal = null;
mConstraints = null;
resetSetpoint();
}
/**
* Specify a goal and constraints for achieving the goal.
*/
public void setGoalAndConstraints(MotionProfileGoal goal, MotionProfileConstraints constraints) {
if (mGoal != null && !mGoal.equals(goal) && mLatestSetpoint != null) {
// Clear the final state bit since the goal has changed.
mLatestSetpoint.final_setpoint = false;
}
mGoal = goal;
mConstraints = constraints;
}
public void setGoal(MotionProfileGoal goal) {
setGoalAndConstraints(goal, mConstraints);
}
/**
* @return The current goal (null if no goal has been set since the latest call to reset()).
*/
public MotionProfileGoal getGoal() {
return mGoal;
}
public void setConstraints(MotionProfileConstraints constraints) {
setGoalAndConstraints(mGoal, constraints);
}
public MotionState getSetpoint() {
return (mLatestSetpoint == null ? MotionState.kInvalidState : mLatestSetpoint.motion_state);
}
/**
* Reset just the setpoint. This means that the latest_state provided to update() will be used rather than feeding
* forward the previous setpoint the next time update() is called. This almost always forces a MotionProfile update,
* and may be warranted if tracking error gets very large.
*/
public void resetSetpoint() {
mLatestSetpoint = null;
}
public void resetIntegral() {
mTotalError = 0.0;
}
/**
* Update the setpoint and apply the control gains to generate a control output.
*
* @param latest_state The latest *actual* state, used only for feedback purposes (unless this is the first iteration or
* reset()/resetSetpoint() was just called, in which case this is the new start state for the profile).
* @param t The timestamp for which the setpoint is desired.
* @return An output that reflects the control output to apply to achieve the new setpoint.
*/
public synchronized double update(MotionState latest_state, double t) {
mLatestActualState = latest_state;
MotionState prev_state = latest_state;
if (mLatestSetpoint != null) {
prev_state = mLatestSetpoint.motion_state;
} else {
mInitialState = prev_state;
}
final double dt = Math.max(0.0, t - prev_state.t());
mLatestSetpoint = mSetpointGenerator.getSetpoint(mConstraints, mGoal, prev_state, t);
// Update error.
mLatestPosError = mLatestSetpoint.motion_state.pos() - latest_state.pos();
mLatestVelError = mLatestSetpoint.motion_state.vel() - latest_state.vel();
// Calculate the feedforward and proportional terms.
double output = mKp * mLatestPosError + mKv * mLatestVelError + mKffv * mLatestSetpoint.motion_state.vel()
+ (Double.isNaN(mLatestSetpoint.motion_state.acc()) ? 0.0 : mKffa * mLatestSetpoint.motion_state.acc());
if (!Util.epsilonEquals(output, 0.0)) {
output += mKs * Math.signum(output);
}
if (output >= mMinOutput && output <= mMaxOutput) {
// Update integral.
mTotalError += mLatestPosError * dt;
output += mKi * mTotalError;
} else {
// Reset integral windup.
mTotalError = 0.0;
}
// Clamp to limits.
output = Math.max(mMinOutput, Math.min(mMaxOutput, output));
return output;
}
public void setMinOutput(double min_output) {
mMinOutput = min_output;
}
public void setMaxOutput(double max_output) {
mMaxOutput = max_output;
}
public double getPosError() {
return mLatestPosError;
}
public double getVelError() {
return mLatestVelError;
}
/**
* We are finished the profile when the final setpoint has been generated. Note that this does not check whether we
* are anywhere close to the final setpoint, however.
*
* @return True if the final setpoint has been generated for the current goal.
*/
public boolean isFinishedProfile() {
return mGoal != null && mLatestSetpoint != null && mLatestSetpoint.final_setpoint;
}
/**
* We are on target if our actual state achieves the goal (where the definition of achievement depends on the goal's
* completion behavior).
*
* @return True if we have actually achieved the current goal.
*/
public boolean onTarget() {
if (mGoal == null || mLatestSetpoint == null) {
return false;
}
// For the options that don't achieve the goal velocity exactly, also count any instance where we have passed
// the finish line.
final double goal_to_start = mGoal.pos() - mInitialState.pos();
final double goal_to_actual = mGoal.pos() - mLatestActualState.pos();
final boolean passed_goal_state = Math.signum(goal_to_start) * Math.signum(goal_to_actual) < 0.0;
return mGoal.atGoalState(mLatestActualState)
|| (mGoal.completion_behavior() != CompletionBehavior.OVERSHOOT && passed_goal_state);
}
}