This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutonomousRed.java
226 lines (192 loc) · 8.01 KB
/
AutonomousRed.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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import java.util.logging.Level;
import com.qualcomm.robotcore.hardware.Servo;
import java.util.Locale;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.Range;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.hardware.bosch.BNO055IMU;
import org.firstinspires.ftc.robotcore.external.Func;
import org.firstinspires.ftc.robotcore.external.navigation.Acceleration;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
import org.firstinspires.ftc.robotcore.external.navigation.Orientation;
import org.firstinspires.ftc.robotcore.external.navigation.Position;
import org.firstinspires.ftc.robotcore.external.navigation.Velocity;
@Autonomous
public class AutonomousRed extends LinearOpMode {
private ElapsedTime runtime = new ElapsedTime();
BotConfig robot = new BotConfig();
final double TOLERANCE = 0.1;
// The IMU sensor object
BNO055IMU imu;
Orientation angles;
@Override
public void runOpMode() {
// initialize hardware variables
robot.init(hardwareMap);
Object lock = new Object();
double straif;
double forward;
double turnLeft;
double turnRight;
LiftLevel lift = LiftLevel.CARRY;
// Set up the parameters with which we will use our IMU. Note that integration
// algorithm here just reports accelerations to the logcat log; it doesn't actually
// provide positional information.
BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
parameters.angleUnit = BNO055IMU.AngleUnit.DEGREES;
parameters.accelUnit = BNO055IMU.AccelUnit.METERS_PERSEC_PERSEC;
parameters.calibrationDataFile = "BNO055IMUCalibration.json"; // see the calibration sample opmode
parameters.loggingEnabled = true;
parameters.loggingTag = "IMU";
// Retrieve and initialize the IMU. We expect the IMU to be attached to an I2C port
// on a Core Device Interface Module, configured to be a sensor of type "AdaFruit IMU",
// and named "imu".
imu = hardwareMap.get(BNO055IMU.class, "imu");
imu.initialize(parameters);
angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
// Send telemetry message to signify robot waiting;
telemetry.addData("Say", "Hello Driver"); //
telemetry.update();
// Wait for the game to start (driver presses PLAY)
waitForStart();
runtime.reset();
//reset encoder
robot.winchMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
robot.winchMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
telemetry.addData("Winch motor ", "Starting at %7d",
robot.winchMotor.getCurrentPosition());
telemetry.update();
DcMotor lf = robot.leftFront;
DcMotor lb = robot.leftBack;
DcMotor rf = robot.rightFront;
DcMotor rb = robot.rightBack;
final double pwr = 0.3;
final int rotTime = 2300;
final int duckyTime = 1500;
final int f1 = 1000;
final int s1 = 2000;
final int f2 = 800;
robot.basket.setPosition(0.81);
while (opModeIsActive()) {
robot.winchMotor.setPower(0.4);
//go forward to drop thing
setMotors(lf,lb,rf,rb,-pwr,-pwr,-pwr,-pwr);
sleep(f1);
stopMotors(lf,lb,rf,rb);
//lift and drop
/*
synchronized(lock) {
moveLiftUp(lift, robot.basket, robot.winchMotor);
lift = LiftLevel.upLevel(lift);
moveLiftUp(lift, robot.basket, robot.winchMotor);
lift = LiftLevel.upLevel(lift);
sleep(600);
robot.basket.setPosition(0);
sleep(300);
robot.basket.setPosition(0.55);
}
//go back to start position
setMotors(lf,lb,rf,rb,pwr,pwr,pwr,pwr);
sleep(f1);
stopMotors(lf,lb,rf,rb);
//rotate 1pi radians
setMotors(lf,lb,rf,rb,pwr,pwr,-pwr,-pwr); //spin around 1pi radians
sleep(rotTime);
stopMotors(lf,lb,rf,rb);
sleep(100);
//go to duckies
setMotors(lf,lb,rf,rb,pwr,-pwr,-pwr,pwr);
sleep(s1);
stopMotors(lf,lb,rf, rb);
//spin abductor
robot.abductor.setPosition(0); //TODO FIX
sleep(duckyTime);
robot.abductor.setPosition(0.5); //stop abductor
stopMotors(lf,lb,rf,rb);
//go to storage thing
setMotors(lf,lb,rf,rb,pwr,pwr,pwr,pwr);
sleep(f2);
stopMotors(lf,lb,rf,rb);
return;
*/
return;
}
}
void setMotors(DcMotor leftF, DcMotor leftB, DcMotor rightF, DcMotor rightB,
double m1, double m2, double m3, double m4) {
leftF .setPower(m1);
leftB .setPower(m2);
rightF.setPower(m3);
rightB.setPower(m4);
}
void stopMotors(DcMotor leftF, DcMotor leftB,
DcMotor rightF, DcMotor rightB) {
leftF .setPower(0);
leftB .setPower(0);
rightF.setPower(0);
rightB.setPower(0);
}
synchronized void moveLiftDown(LiftLevel level, Servo servo, DcMotor motor) {
// double servPos = LiftLevel.level2Val(level);
//get the values we need to go to from the current level
LiftLevel downPos = LiftLevel.downLevel(level);
double toPosS = LiftLevel.level2Servo(downPos);
int toPosM = (int)LiftLevel.level2Motor(downPos);
double motorPos = motor.getCurrentPosition();
//getCurrentPosition() gives a value in encoder ticks.
//there are 537.7 encoder ticks per revolution
switch (level) { //PICKUP, CARRY, DROP_1, DROP_3
case PICKUP:
break;
case CARRY:
motor.setTargetPosition(toPosM-10);
sleep(100);
servo.setPosition(toPosS);
break;
case DROP_1:
servo.setPosition(toPosS);
motor.setTargetPosition(toPosM); //1.75 * 537.7
break;
case DROP_3:
servo.setPosition(toPosS);
motor.setTargetPosition(toPosM);
break;
}
}
synchronized void moveLiftUp(LiftLevel level, Servo servo, DcMotor motor) {
// double servPos = LiftLevel.level2Val(level);
//get the values we need to go to from the current level
LiftLevel downPos = LiftLevel.upLevel(level, gamepad1);
double toPosS = LiftLevel.level2Servo(downPos);
int toPosM = (int)LiftLevel.level2Motor(downPos);
double motorPos = motor.getCurrentPosition();
//getCurrentPosition() gives a value in encoder ticks.
//there are 537.7 encoder ticks per revolution
switch (level) { //PICKUP, CARRY, DROP_1, DROP_3
case PICKUP:
servo.setPosition(toPosS);
motor.setTargetPosition(toPosM);
break;
case CARRY:
motor.setTargetPosition(toPosM);
servo.setPosition(toPosS);
break;
case DROP_1:
motor.setTargetPosition(toPosM); //1.75 * 537.7
servo.setPosition(toPosS);
break;
case DROP_3:
motor.setTargetPosition(toPosM);
servo.setPosition(toPosS);
break;
}
while(motor.isBusy()) {}
}
}