-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpticalDistanceAutoDriver.java
More file actions
97 lines (81 loc) · 3.56 KB
/
OpticalDistanceAutoDriver.java
File metadata and controls
97 lines (81 loc) · 3.56 KB
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
package org.firstinspires.ftc.teamcode.RobotCoreExtensions;
/**
* Filename: OpticalDistanceAutoDriver.java
*
*
* Description:
* This class contains all of the predefined robot movements that use an optical distance sensor.
*
* Methods:
* squareUpToLine - Uses the optical distance sensors on the robot to
* align the robot perpendicular to a taped white line on the floor.
*
* Example: robot.hardwareConfiguration.opticalDistanceAutoDriver.squareUpToLine()
*
* Requirements:
* -2 optical distance sensors, created and stored in the hardware configuration
* -taped white line
*
* Changelog:
* -Edited and tested by Team 3486 on 3/6/2017.
* -Added comments 7/21/17
* -Edited file description and documentation 7/22/17
*/
public class OpticalDistanceAutoDriver extends AutoDriver
{
private double lightValue = 0.06;
public OpticalDistanceAutoDriver(HardwareConfiguration hw)
{
super(hw);
}
public double getLightValue()
{
return lightValue;
}
public void setLightValue(double lightValue)
{
this.lightValue = lightValue;
}
public void squareUpToLine() throws InterruptedException
{
setupMotion("Squaring up to a line.");
hw.drivetrain.setPowers(0.1, 0.1);
// Continue driving forwards until either sensor finds a line or the opMode ends.
while (hw.leftOpticalDistanceSensor.getLightDetected() < lightValue
&& hw.rightOpticalDistanceSensor.getLightDetected() < lightValue && hw.opMode.opModeIsActive()) {}
hw.drivetrain.haltDrive();
// Check to see which sensor found the line first. If both found it at the same time,
// the robot is already aligned to the line and will adjust no further.
// If the left sensor found the line, and the right sensor did not.
if (hw.leftOpticalDistanceSensor.getLightDetected() >= lightValue &&
hw.rightOpticalDistanceSensor.getLightDetected() < lightValue)
{
// Drive until the right sensor sees the line.
hw.drivetrain.setPowers(0, .1);
while (hw.rightOpticalDistanceSensor.getLightDetected() < lightValue) {}
hw.drivetrain.haltDrive();
hw.drivetrain.resetMotorEncoders();
// This short movement adjust the robot back straight, as the pivot turn can slightly
// adjust the position of the left side of the robot when the right side turns.
hw.drivetrain.setPowers(-0.1, 0);
while (hw.drivetrain.getLeftEncoderCount() > -50 && hw.opMode.opModeIsActive()) {}
hw.drivetrain.haltDrive();
}
// If the right sensor found the line, and the left sensor did not.
else if (hw.rightOpticalDistanceSensor.getLightDetected() >= lightValue &&
hw.leftOpticalDistanceSensor.getLightDetected() < lightValue)
{
// Drive until the left sensor sees the line.
hw.drivetrain.setPowers(0.1, 0);
while (hw.leftOpticalDistanceSensor.getLightDetected() < lightValue) {}
hw.drivetrain.haltDrive();
// This short movement adjust the robot back straight, as the pivot turn can slightly
// adjust the position of the right side of the robot when the left side turns.
hw.drivetrain.resetMotorEncoders();
hw.drivetrain.setPowers(0.0, -0.1);
while (hw.drivetrain.getRightEncoderCount() > -50 && hw.opMode.opModeIsActive()) {}
hw.drivetrain.haltDrive();
}
endMotion();
}
}