-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrive.java
292 lines (222 loc) · 7.13 KB
/
Drive.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
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
package frc.robot.subsystems.drive;
import PIDControl.PIDControl;
import frc.robot.abstraction.PositionSensor;
import frc.robot.abstraction.SwartdogSubsystem;
public abstract class Drive extends SwartdogSubsystem
{
protected PositionSensor _gyro;
protected PIDControl _translatePID;
protected PIDControl _rotatePID;
private Vector _origin;
protected SwerveModule[] _swerveModules;
private double _maxModuleDistance;
private double _rotateSetpoint;
private Vector _odometer;
private Vector _targetTranslation;
private double _translationDistanceThreshold;
private double _translationCloseMaxSpeed;
public Drive()
{
_origin = new Vector();
_maxModuleDistance = 0;
_rotateSetpoint = 0;
}
public void init()
{
resetEncoders();
setOrigin(0, 0);
resetOdometer();
}
public void drive(double drive, double strafe, double rotate)
{
drive(drive, strafe, rotate, true);
}
public void drive(double drive, double strafe, double rotate, boolean absolute)
{
Vector translateVector = new Vector(strafe, drive);
drive(translateVector, rotate, absolute);
}
public void drive(Vector translateVector, double rotate, boolean absolute)
{
if (absolute)
{
translateVector.setTheta(translateVector.getTheta() - getHeading());
}
drive(translateVector, rotate);
}
public void drive(Vector translateVector, double rotate)
{
Vector[] moduleCommands = new Vector[_swerveModules.length];
double maxSpeed = 1;
for (int i = 0; i < _swerveModules.length; i++)
{
Vector modulePosition = _swerveModules[i].subtract(_origin);
Vector rotateVector = new Vector(modulePosition.getY(), -modulePosition.getX()).multiply(rotate / _maxModuleDistance);
Vector outputVector = translateVector.add(rotateVector);
moduleCommands[i] = outputVector;
maxSpeed = Math.max(maxSpeed, moduleCommands[i].getR());
}
for (int i = 0; i < moduleCommands.length; i++)
{
moduleCommands[i] = moduleCommands[i].divide(maxSpeed);
_swerveModules[i].drive(moduleCommands[i]);
}
}
public void setOrigin(double x, double y)
{
setOrigin(new Vector(x, y));
}
public void setOrigin(Vector newOrigin)
{
_origin = newOrigin;
_maxModuleDistance = 0;
for (int i = 0; i < _swerveModules.length; i++)
{
Vector modulePosition = _swerveModules[i].clone();
modulePosition = modulePosition.subtract(_origin);
if (modulePosition.getR() > _maxModuleDistance)
{
_maxModuleDistance = modulePosition.getR();
}
}
}
public Vector getOrigin()
{
return _origin;
}
public SwerveModule getSwerveModule(int index)
{
return _swerveModules[index];
}
public void translateInit(Vector targetTranslation, double distanceThreshold, double maxSpeed, double closeMaxSpeed, double minSpeed, boolean resetEncoders)
{
_targetTranslation = targetTranslation;
_translationDistanceThreshold = distanceThreshold;
_translationCloseMaxSpeed = closeMaxSpeed;
maxSpeed = Math.abs(maxSpeed);
minSpeed = Math.abs(minSpeed);
if (resetEncoders)
{
resetEncoders();
}
Vector translateErrorVector = _targetTranslation.subtract(getOdometer());
_translatePID.setSetpoint(0, translateErrorVector.getR());
_translatePID.setOutputRange(0, maxSpeed, minSpeed);
}
public Vector translateExec()
{
Vector translateErrorVector = _targetTranslation.subtract(getOdometer());
if (translateErrorVector.getR() > _translationDistanceThreshold)
{
translateErrorVector.setR(_translatePID.calculate(-translateErrorVector.getR()));
}
else
{
translateErrorVector.setR(Math.min(_translatePID.calculate(-translateErrorVector.getR()), _translationCloseMaxSpeed));
}
return translateErrorVector;
}
public boolean translateIsFinished()
{
return _translatePID.atSetpoint();
}
public void rotateInit(double heading, double maxSpeed)
{
maxSpeed = Math.abs(maxSpeed);
_rotateSetpoint = heading;
double PIDPosition = Math.toRadians(_rotateSetpoint - getHeading());
PIDPosition /= 2;
PIDPosition = Math.sin(PIDPosition) * (Math.cos(PIDPosition) / -Math.abs(Math.cos(PIDPosition)));
_rotatePID.setSetpoint(0, PIDPosition);
_rotatePID.setOutputRange(-maxSpeed, maxSpeed);
}
public double rotateExec()
{
double PIDPosition = Math.toRadians(_rotateSetpoint - getHeading());
PIDPosition /= 2;
PIDPosition = Math.sin(PIDPosition) * (Math.cos(PIDPosition) / -Math.abs(Math.cos(PIDPosition)));
return _rotatePID.calculate(PIDPosition);
}
public boolean rotateIsFinished()
{
return _rotatePID.atSetpoint();
}
public double getHeading()
{
return normalizeAngle(_gyro.get());
}
public double getPIDHeading()
{
double heading = getHeading();
if ((_rotateSetpoint - heading) > 180)
{
heading += 360;
}
else if ((_rotateSetpoint - heading) < -180)
{
heading -= 360;
}
return heading;
}
public void setGyro(double heading)
{
_gyro.set(heading);
}
public void resetEncoders()
{
for (SwerveModule swerveModule : _swerveModules)
{
swerveModule.getDriveMotor().getPositionSensor().set(0);
swerveModule.resetDrivePosition();
}
}
private double normalizeAngle(double angle)
{
if (angle < 0)
{
angle += 360 * (((int)angle / -360) + 1);
}
angle %= 360;
if (angle > 180)
{
angle -= 360;
}
return angle;
}
@Override
public void periodic()
{
updateOdometry();
for (int i = 0; i < _swerveModules.length; i++)
{
_swerveModules[i].drive();
}
}
public void resetOdometer()
{
resetOdometer(new Vector());
}
public void resetOdometer(Vector newPosition)
{
_odometer = newPosition;
for (int i = 0; i < _swerveModules.length; i++)
{
_swerveModules[i].resetDrivePosition();
}
}
public Vector getOdometer()
{
return _odometer;
}
public void updateOdometry()
{
Vector change = new Vector();
for (int i = 0; i < _swerveModules.length; i++)
{
change = change.add(_swerveModules[i].getOffset());
}
change = change.divide(_swerveModules.length);
change.translatePolarPosition(0.0, getHeading());
_odometer = _odometer.add(change);
}
}