-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovement.java
196 lines (141 loc) · 4.23 KB
/
Movement.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
package Lab5;
import lejos.hardware.lcd.LCD;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3GyroSensor;
import lejos.hardware.sensor.EV3UltrasonicSensor;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;
public class Movement {
private final float CALIBRATION_DIST = 0.15f;
private final float COLOR_IDENTIFICATION_RANGE = 0.04f;
final float TILE_LENGTH = 0.35f;
final int ROBOT_ROTATION_RIGHT = -90;
final int ROBOT_ROTATION_LEFT = 90;
private EV3GyroSensor gyroSensor = new EV3GyroSensor(SensorPort.S3);
private SampleProvider angle = gyroSensor.getAngleMode();
private float angleSample[] = new float[angle.sampleSize()];
private EV3LargeRegulatedMotor rightMotor = new EV3LargeRegulatedMotor(
MotorPort.C);
private EV3LargeRegulatedMotor leftMotor = new EV3LargeRegulatedMotor(
MotorPort.B);
EV3UltrasonicSensor distanceSensor = new EV3UltrasonicSensor(SensorPort.S4);
SampleProvider distance = distanceSensor.getDistanceMode();
float distanceSample[] = new float[distance.sampleSize()];
public void calibration() {
distance.fetchSample(distanceSample, 0);
if (distanceSample[0] < TILE_LENGTH)
{
Delay.msDelay(150);
if (distanceSample[0] < CALIBRATION_DIST) {
moveBackward();
while (distanceSample[0] <= CALIBRATION_DIST) {
distance.fetchSample(distanceSample, 0);
}
stop();
} else {
{
moveForward();
while (distanceSample[0] >= CALIBRATION_DIST) {
distance.fetchSample(distanceSample, 0);
}
stop();
}
}
}
}
public void turn(int degrees) {
final int ANGLE_LOCATION_ROW = 2;
final int ANGLE_LOACTION_COLUMN = 1;
angleSample[0] = 0;
gyroSensor.reset();
Delay.msDelay(150);
while (Math.abs(angleSample[0]) < Math.abs(degrees)) {
angle.fetchSample(angleSample, 0);
int varyingSpeed = (int) (10 * (Math.abs(degrees) - Math
.abs(angleSample[0])));
rightMotor.setSpeed(varyingSpeed);
leftMotor.setSpeed(varyingSpeed);
if (degrees > 0) {
rightMotor.backward();
leftMotor.forward();
//Delay.msDelay(110);
//stop();
}
if (degrees < 0) {
rightMotor.forward();
leftMotor.backward();
//Delay.msDelay(110);
//stop();
}
angle.fetchSample(angleSample, 0);
LCD.drawString("Angle " + angleSample[0], ANGLE_LOACTION_COLUMN,
ANGLE_LOCATION_ROW);
}
stop();
calibration();
// gyroSensor.close();
}
public void moveForward35cm() {
int speed = 200;
final int DEGREES = -2188; // motor should be rotated 2188 degrees so
// the robot can move 35cm (a tile).
Delay.msDelay(150);
rightMotor.setSpeed(speed);
leftMotor.setSpeed(speed);
rightMotor.rotate(DEGREES, true);
leftMotor.rotate(DEGREES, false);
calibration();
}
public void moveBackward() {
Delay.msDelay(150);
rightMotor.setSpeed(200);
leftMotor.setSpeed(200);
rightMotor.forward();
leftMotor.forward();
}
public void moveForward() {
Delay.msDelay(150);
rightMotor.setSpeed(200);
leftMotor.setSpeed(200);
rightMotor.backward();
leftMotor.backward();
}
public void stop() {
rightMotor.stop(true);
leftMotor.stop(false);
}
public void initialCalibration() {
boolean frontNotCalibrated = true;
boolean rightNotCalibrated = true;
distance.fetchSample(distanceSample, 0);
if (distanceSample[0] < TILE_LENGTH) {
calibration();
frontNotCalibrated = false;
}
turn(ROBOT_ROTATION_RIGHT);
distance.fetchSample(distanceSample, 0);
if (distanceSample[0] < TILE_LENGTH) {
calibration();
rightNotCalibrated = false;
}
turn(ROBOT_ROTATION_RIGHT);
if (frontNotCalibrated) {
calibration();
}
turn(ROBOT_ROTATION_RIGHT);
if (rightNotCalibrated) {
calibration();
}
turn(ROBOT_ROTATION_RIGHT);
}
public void moveToDetectColor() {
distance.fetchSample(distanceSample, 0);
moveForward();
while (distanceSample[0] >= COLOR_IDENTIFICATION_RANGE) {
distance.fetchSample(distanceSample, 0);
}
stop();
}
}