-
Notifications
You must be signed in to change notification settings - Fork 0
/
DriveAvoid.java
205 lines (164 loc) · 6.34 KB
/
DriveAvoid.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
// Simple autonomous program that drives bot forward until end of period
// or touch sensor is hit. If touched, backs up a bit and turns 90 degrees
// right and keeps going. Demonstrates obstacle avoidance and use of a MR
// gyro sensor along with a touch sensor. Also uses gamepad1 buttons to
// simulate touch sensor press and supports left as well as right turn.
//
// Also uses gyro to drive in a straight line when not avoiding an obstacle.
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.GyroSensor;
import com.qualcomm.robotcore.hardware.TouchSensor;
@Autonomous(name="Drive Avoid", group="Exercises")
//@Disabled
public class DriveAvoid extends LinearOpMode
{
DcMotor leftMotor;
DcMotor rightMotor;
TouchSensor touch;
GyroSensor gyro;
double power = .30, correction;
boolean aButton, bButton, touched;
// called when init button is pressed.
@Override
public void runOpMode() throws InterruptedException
{
leftMotor = hardwareMap.dcMotor.get("left_motor");
rightMotor = hardwareMap.dcMotor.get("right_motor");
leftMotor.setDirection(DcMotor.Direction.REVERSE);
// get a reference to the touch sensor.
touch = hardwareMap.touchSensor.get("touch_sensor");
gyro = hardwareMap.gyroSensor.get("gyro");
telemetry.addData("Mode", "starting gyro calibration...please wait");
telemetry.update();
gyro.calibrate();
// make sure the gyro is calibrated before continuing
while (!isStopRequested() && gyro.isCalibrating())
{
sleep(50);
idle();
}
telemetry.addData("Mode", "gyro calibrated...waiting for start");
telemetry.update();
// wait for start button.
waitForStart();
telemetry.addData("Mode", "running");
telemetry.update();
sleep(1000);
// drive until end of period.
gyro.resetZAxisIntegrator();
while (opModeIsActive())
{
telemetry.addData("gyro heading", gyro.getHeading());
telemetry.update();
// Use gyro to drive in a straight line.
correction = checkDirection();
// set power levels.
leftMotor.setPower(power - correction);
rightMotor.setPower(power + correction);
// We record the sensor values because we will test them in more than
// one place with time passing between those places. See the lesson on
// Timing Considerations to know why.
aButton = gamepad1.a;
bButton = gamepad1.b;
touched = touch.isPressed();
if (touched || aButton || bButton)
{
// backup.
leftMotor.setPower(power);
rightMotor.setPower(power);
sleep(500);
// stop.
leftMotor.setPower(0);
rightMotor.setPower(0);
// turn 90 degrees right.
if (touched || aButton) rotate(-90, power);
// turn 90 degrees left.
if (bButton) rotate(90, power);
}
}
// turn the motors off.
rightMotor.setPower(0);
leftMotor.setPower(0);
}
/**
* See if we are moving in a straight line and if not return a power correction value.
* @return Power adjustment, + is adjust left - is adjust right.
*/
private double checkDirection()
{
// The gain value determines how sensitive the correction is to direction changes.
// You will have to experiment with your robot to get small smooth direction changes
// to stay on a straight line.
double correction, heading, gain = .10;
heading = gyro.getHeading();
if (heading == 0)
correction = 0; // no adjustment.
else if (heading > 180)
correction = 360 - heading; // adjust left.
else
correction = -heading; // adjust right.
correction = correction * gain;
return correction;
}
/**
* Rotate left or right the number of degrees. Does not support turning more than 350 degrees.
* @param degrees Degrees to turn, + is left - is right
*/
private void rotate(int degrees, double power)
{
double leftPower, rightPower;
int targetAngle;
// reset gyro to zero.
gyro.resetZAxisIntegrator();
// Gyro returns 0->359 when rotating counter clockwise (left) and 359->0 when rotating
// clockwise (right).
if (degrees < 0)
{ // turn right.
leftPower = power;
rightPower = -power;
targetAngle = 360 + degrees; // degrees is - for right turn.
}
else if (degrees > 0)
{ // turn left.
leftPower = -power;
rightPower = power;
targetAngle = degrees;
}
else return;
// set power to rotate.
leftMotor.setPower(leftPower);
rightMotor.setPower(rightPower);
// rotate until turn is completed.
if (degrees < 0)
{
// On right turn we have to get off zero first.
while (opModeIsActive() && gyro.getHeading() == 0)
{
telemetry.addData("gyro heading", gyro.getHeading());
telemetry.update();
idle();
}
while (opModeIsActive() && gyro.getHeading() > targetAngle)
{
telemetry.addData("gyro heading", gyro.getHeading());
telemetry.update();
idle();
}
}
else
while (opModeIsActive() && gyro.getHeading() < targetAngle)
{
telemetry.addData("gyro heading", gyro.getHeading());
telemetry.update();
idle();
}
// turn the motors off.
rightMotor.setPower(0);
leftMotor.setPower(0);
// Reset gyro heading to zero on new direction we are now pointing.
gyro.resetZAxisIntegrator();
}
}