-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDriveAvoid.java
More file actions
82 lines (65 loc) · 2.41 KB
/
Copy pathDriveAvoid.java
File metadata and controls
82 lines (65 loc) · 2.41 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
package ev3.exercises;
import lejos.hardware.Button;
import lejos.hardware.Sound;
import lejos.hardware.motor.*;
import lejos.hardware.port.*;
import lejos.utility.Delay;
import ev3.exercises.library.*;
public class DriveAvoid
{
public static void main(String[] args)
{
TouchSensor touch = new TouchSensor(SensorPort.S1);
UltraSonicSensor ultra = new UltraSonicSensor(SensorPort.S4);
GyroSensor gyro = new GyroSensor(SensorPort.S2);
System.out.println("Drive and Avoid\n");
System.out.println("Press any key to start");
Button.LEDPattern(4); // flash green led and
Sound.beepSequenceUp(); // make sound when ready.
Button.waitForAnyPress();
// create two motor objects to control the motors.
UnregulatedMotor motorA = new UnregulatedMotor(MotorPort.A);
UnregulatedMotor motorB = new UnregulatedMotor(MotorPort.B);
motorA.setPower(-50);
motorB.setPower(-50);
// drive waiting for touch sensor or escape key to stop driving.
while (!touch.isTouched() && Button.ESCAPE.isUp())
{
Lcd.clear(6);
Lcd.print(6, "range=%.3f", ultra.getRange());
// watch for obstacle.
if (ultra.getRange() < .25)
{
// Set gyro angle to zero.
gyro.reset();
Lcd.clear(7);
Lcd.print(7, "angle=%d", gyro.getAngle());
Delay.msDelay(50);
// start rotation around current location.
motorA.setPower(-50);
motorB.setPower(+50);
// wait for 90 degrees of rotation
while (Math.abs(gyro.getAngle()) < 90 && !touch.isTouched() && Button.ESCAPE.isUp())
{
Lcd.clear(7);
Lcd.print(7, "angle=%d", gyro.getAngle());
Delay.msDelay(50);
}
// back to straight driving.
motorA.setPower(-50);
motorB.setPower(-50);
}
Delay.msDelay(50);
}
// stop motors with brakes on.
motorA.stop();
motorB.stop();
// free up resources.
motorA.close();
motorB.close();
touch.close();
ultra.close();
gyro.close();
Sound.beepSequence(); // we are done.
}
}