-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeleop3.java
153 lines (137 loc) · 5.8 KB
/
Teleop3.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
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.Servo;
@TeleOp(name = "Teleop3" )
// motors and servos names
/* Controller Description:
Left_Joystick = robot direction controls
up - forward
down - backwards
left - left
right - right
Right_Joystick = arm and pixel control
up - lift up
down - lower down
right - pixel angle up
left - pixel angle down
RB = right pixel grab and release
LB = left pixel grab and release
A = airplane Release
B = boost increase to robot speed
Y = boost increase to lift speed
*/
public class Teleop3 extends OpMode {
public Servo pixelArmAngle;
public Servo pixelGrabberRight;
public Servo pixelGrabberLeft;
public Servo airPlaneServo;
DcMotor robotGearRight;
DcMotor robotGearLeft;
DcMotor liftMotorRight;
DcMotor liftMotorLeft;
double ticks = 288;
double newTarget;
double robotFrontBack, robotRightLeft, liftUpDown, pixelAngle = 0;
double[] speedBoost = {0.35, 0.7, 1};
int count = 0;
int liftCount = 0;
@Override
public void init() {
// Motors
robotGearRight = hardwareMap.get(DcMotor.class, "GearMotorRight"); // Port 0
robotGearLeft = hardwareMap.get(DcMotor.class, "GearMotorLeft"); // Port 1
liftMotorRight = hardwareMap.get(DcMotor.class, "LiftRight"); // Port 2
liftMotorLeft = hardwareMap.get(DcMotor.class, "LiftLeft"); // Port 3
// Servos
pixelArmAngle = hardwareMap.get(Servo.class, "PixelAngle"); // Port 0
airPlaneServo = hardwareMap.get(Servo.class, "Airplane"); // Port 1
pixelGrabberRight = hardwareMap.get(Servo.class, "PixelRight"); // Port 2
pixelGrabberLeft = hardwareMap.get(Servo.class, "PixelLeft"); // Port 3
// Set Motors Direction and Modes
robotGearRight.setDirection(DcMotorSimple.Direction.FORWARD);
robotGearLeft.setDirection(DcMotorSimple.Direction.REVERSE);
liftMotorRight.setDirection(DcMotorSimple.Direction.FORWARD);
liftMotorLeft.setDirection(DcMotorSimple.Direction.FORWARD);
liftMotorRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
liftMotorLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
airPlaneServo.setPosition(0.8);
run_encoder(0, 0.7, liftMotorLeft);
run_encoder(0, 0.7, liftMotorRight);
pixelArmAngle.setPosition(0.83);
}
public void loop() {
telemetry.addData("PushBot", "run");
if (gamepad1.b) { // *controls robot driving speed
count = (count + 1) % 3;
}
if (gamepad1.y) { // controls lift speed
liftCount = (liftCount + 1) % 3;
}
robotFrontBack = gamepad1.left_stick_y;
robotRightLeft = gamepad1.left_stick_x;
//liftUpDown = gamepad1.right_stick_y;
// Motor Operation:
robotGearRight.setPower((robotRightLeft + robotFrontBack) * speedBoost[count]);
robotGearLeft.setPower((-robotRightLeft + robotFrontBack) * speedBoost[count]);
//liftMotorRight.setPower(liftUpDown * speedBoost[liftCount]);
//liftMotorRight.setPower(liftUpDown * speedBoost[liftCount]);
if (gamepad1.dpad_up) { // Controls Pixel arm Position - UP
run_encoder(-0.5, 1, liftMotorRight);
run_encoder(-0.5, 1, liftMotorLeft);
pixelArmAngle.setPosition(0.3);//armServo position - UP
}
if (gamepad1.dpad_down) { // Controls Pixel arm Position - UP
run_encoder(-0.0001, 0.5, liftMotorRight);
run_encoder(-0.0001, 0.5, liftMotorLeft);
pixelArmAngle.setPosition(0.1);//armServo position - DOWN
}
// if (gamepad1.dpad_left) {
// run_encoder(0.8, speedBoost[liftCount],liftMotorLeft);
// run_encoder(0.8, speedBoost[liftCount],liftMotorRight);
// pixelArmAngle.setPosition(0.3);
// }
// if (gamepad1.dpad_right) {
// run_encoder(1.5, speedBoost[liftCount],liftMotorLeft);
// run_encoder(1.5, speedBoost[liftCount],liftMotorRight);
// pixelArmAngle.setPosition(0.5);
// }
telemetry.addData("Current Lift Speed (press Y to change):", speedBoost[liftCount]);
telemetry.addData("Current Robot Speed (press B to change):", speedBoost[count]);
// Airplane Servo
if (gamepad1.a) {
airPlaneServo.setPosition(1);
} else {
airPlaneServo.setPosition(0.83);
}
//Pixel Operation:
if (gamepad1.right_bumper) {
pixelGrabberRight.setPosition(0);
telemetry.addData("servo", "right");
} else {
pixelGrabberRight.setPosition(0.45);
}
if (gamepad1.left_bumper) {
pixelGrabberLeft.setPosition(0.55);
telemetry.addData("servo", "left");
} else {
pixelGrabberLeft.setPosition(0);
}
if (gamepad1.left_trigger == 1) {
liftMotorLeft.setTargetPosition((int) (-gamepad1.right_trigger * ticks * 1.2));
liftMotorRight.setTargetPosition((int) (-gamepad1.right_trigger * ticks * 1.2));
liftMotorLeft.setPower(1);
liftMotorRight.setPower(1);
liftMotorLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
liftMotorRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
}
}
public void run_encoder(double turnage, double power, DcMotor motor) {
newTarget = ticks * turnage;
motor.setTargetPosition((int) newTarget);
motor.setPower(power);
motor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
}
}