-
Notifications
You must be signed in to change notification settings - Fork 1
/
testNavigationPID.java
264 lines (217 loc) · 9.67 KB
/
testNavigationPID.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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.Range;
public final class testNavigationPID {
private AdafruitIMU AdafruitGyro; //Instance of AdafruitIMU
private double yawAngle; //Array to store IMU's output
private DcMotor rightFront;
private DcMotor rightBack;
private DcMotor leftFront;
private DcMotor leftBack;
private double[][] movementArray;
private int movementArrayStep = 0;
//Variables for PID loop
private double setPoint = 0;
private double turnKp;
private double turnKi;
private double turnKd;
private double strafeKp;
private double strafeKi;
private double strafeKd;
private double forwardsKp;
private double forwardsKi;
private double forwardsKd;
private double integral = 0;
private double preError;
private double encoderTicksPerInch;//= encoderTicksPerRev / (Math.PI * 3); //Variable to convert encoder ticks to inches
private int encoderPositionReference; //Variable to use as reference for moveForward and moveBackward
private OpMode opmode;
private double completedHeading;
//Constructor
public testNavigationPID(double[][] movementCommandArray, OpMode opmode, String configuredIMUname, DcMotor leftFront, DcMotor rightFront, DcMotor leftBack, DcMotor rightBack, int encoderTicksPerRev, double wheelDiameter) {
this.opmode = opmode;
AdafruitGyro = new AdafruitIMU(opmode, configuredIMUname);
//Assign the DcMotor instances to those of the main class
this.rightFront = rightFront;
this.rightBack = rightBack;
this.leftFront = leftFront;
this.leftBack = leftBack;
movementArray = movementCommandArray;
encoderTicksPerInch = encoderTicksPerRev / (Math.PI * wheelDiameter);
}
//**********Public methods***********
public void initialize() {
AdafruitGyro.startIMU(); //Prepare the IMU for I2C communication
encoderPositionReference = (leftFront.getCurrentPosition() + rightBack.getCurrentPosition()) / 2; //Set encoderPositionReference to the mean value of the current motor positions
}
public void tuneTurnGains(double newKp, double newKi, double newKd) {
//Set the gains for the PID loop
turnKp = newKp;
turnKi = newKi;
turnKd = newKd;
}
public void tuneStrafeGains(double newKp, double newKi, double newKd) {
//Set the gains for the PID loop
strafeKp = newKp;
strafeKi = newKi;
strafeKd = newKd;
}
public void tuneForwardsGains(double newKp, double newKi, double newKd) {
//Set the gains for the PID loop
forwardsKp = newKp;
forwardsKi = newKi;
forwardsKd = newKd;
}
public void loopNavigation() {
yawAngle = AdafruitGyro.getYaw();
opmode.telemetry.addData("Heading, Goal", "%1$s, %2$s", currentHeading(), movementArray[movementArrayStep][2]);
opmode.telemetry.addData("Completed Heading", completedHeading);
opmode.telemetry.addData("Encoder values", "\nRF: %1$s LF: %2$s\nRB: %3$s LB: %4$s", rightFront.getCurrentPosition(), leftFront.getCurrentPosition(), rightBack.getCurrentPosition(), leftBack.getCurrentPosition());
opmode.telemetry.addData("Motor powers", "\nRF: %1$s LF: %2$s\nRB: %3$s LB: %4$s", rightFront.getPower(), leftFront.getPower(), rightBack.getPower(), leftBack.getPower());
//debugLogFile.write("Heading, Goal", currentHeading() + ", " + movementArray[movementArrayStep][2]);
//debugLogFile.write("Completed Heading", completedHeading);
//debugLogFile.write("Encoder values", "RF: " + rightFront.getCurrentPosition() + "LF: " + leftFront.getCurrentPosition() + "RB: " + rightBack.getCurrentPosition() + "LB: " + leftBack.getCurrentPosition());
//debugLogFile.write("Motor powers", "RF: " + rightFront.getPower() + "LF: " + leftFront.getPower() + "RB: " + rightBack.getPower() + "LB: " + leftBack.getPower());
move((int) movementArray[movementArrayStep][0], movementArray[movementArrayStep][1], movementArray[movementArrayStep][2]);
}
public int navigationStep() {
return movementArrayStep;
}
public int navigationType() {
return (int) movementArray[movementArrayStep][0];
}
public double currentHeading() {
return yawAngle;
}
public void forceNextMovement() {
nextMovement();
}
public void moveNoStop(double TpForwards, double TpSlide) {
yawAngle = AdafruitGyro.getYaw();
if (Math.abs(TpForwards) > Math.abs(TpSlide)) {
loopPID(TpForwards, TpSlide, forwardsKp, forwardsKi, forwardsKd);
} else {
loopPID(TpForwards, TpSlide, strafeKp, strafeKi, strafeKd);
}
}
public boolean rotateToAngle(double angle, double Kp, double Ki, double Kd) {
yawAngle = AdafruitGyro.getYaw();
if (yawAngle + 0.5 >= angle && yawAngle - 0.5 <= angle) {
return true;
} else {
double error = yawAngle - setPoint;
integral += error;
double derivative = error - preError;
double output = Range.clip((Kp * error) + (Ki * integral) + (Kd * derivative), -1, 1);
rightFront.setPower(-output);
rightBack.setPower(-output);
leftFront.setPower(output);
leftBack.setPower(output);
preError = error;
return false;
}
}
//**********Private methods**********
private void move(int movementType, double Tp, double goalValue) {
switch (movementType) {
case 1:
moveForward(goalValue, Tp);
break;
case 2:
moveBackward(goalValue, Tp);
break;
case 3:
rotateCW(goalValue);
break;
case 4:
rotateCCW(goalValue);
break;
case 5:
moveLeft(goalValue, Tp);
break;
case 6:
moveRight(goalValue, Tp);
break;
case 10:
fullStop();
break;
default:
break;
}
}
private void moveForward(double goalDistanceInch, double Tp) { //Movement val == 1
int distanceTraveled = (leftFront.getCurrentPosition() + rightBack.getCurrentPosition()) / 2;
if (distanceTraveled < convertInchesToEncoderTicks(goalDistanceInch) + encoderPositionReference) {
loopPID(Tp, 0, forwardsKp, forwardsKi, forwardsKd);
} else {
nextMovement();
}
}
private void moveBackward(double goalDistanceInch, double Tp) { //Movement val == 2
int distanceTraveled = (leftFront.getCurrentPosition() + rightBack.getCurrentPosition()) / 2;
if (distanceTraveled > convertInchesToEncoderTicks(goalDistanceInch) + encoderPositionReference) {
loopPID(Tp, 0, forwardsKp, forwardsKi, forwardsKd);
} else {
nextMovement();
}
}
private void rotateCW(double goalAngle) { //Movement val == 3
setPoint = goalAngle;
if (yawAngle < setPoint) {
loopPID(0, 0, turnKp, turnKi, turnKd);
} else {
nextMovement();
}
}
private void rotateCCW(double goalAngle) { //Movement val == 4
setPoint = goalAngle;
if (yawAngle > setPoint) {
loopPID(0, 0, turnKp, turnKi, turnKd);
} else {
nextMovement();
}
}
private void moveLeft(double goalDistanceInch, double Tp) { //Movement val == 2
int distanceTraveled = (leftFront.getCurrentPosition() + rightBack.getCurrentPosition()) / 2;
if (distanceTraveled < convertInchesToEncoderTicks(goalDistanceInch) + encoderPositionReference) {
loopPID(0, Tp, strafeKp, strafeKi, strafeKd);
} else {
nextMovement();
}
}
private void moveRight(double goalDistanceInch, double Tp) { //Movement val == 2
int distanceTraveled = (leftFront.getCurrentPosition() + rightBack.getCurrentPosition()) / 2;
if (distanceTraveled > convertInchesToEncoderTicks(goalDistanceInch) + encoderPositionReference) {
loopPID(0, Tp, strafeKp, strafeKi, strafeKd);
} else {
nextMovement();
}
}
private double convertInchesToEncoderTicks(double inches) {
return encoderTicksPerInch * inches;
}
private void fullStop() {
rightFront.setPower(0);
rightBack.setPower(0);
leftFront.setPower(0);
leftBack.setPower(0);
}
private void nextMovement() {
fullStop();
completedHeading = yawAngle;
encoderPositionReference = (leftFront.getCurrentPosition() + rightBack.getCurrentPosition()) / 2;
movementArrayStep++;
}
private void loopPID(double TpForwards, double TpSlide, double Kp, double Ki, double Kd) {
double error = yawAngle - setPoint;
integral += error;
double derivative = error - preError;
double output = Range.clip((Kp * error) + (Ki * integral) + (Kd * derivative), -1, 1);
rightFront.setPower(TpForwards - output - TpSlide);
rightBack.setPower(TpForwards - output + TpSlide);
leftFront.setPower(TpForwards + output + TpSlide);
leftBack.setPower(TpForwards + output - TpSlide);
preError = error;
}
}