-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBumper2.java
More file actions
79 lines (63 loc) · 1.57 KB
/
Bumper2.java
File metadata and controls
79 lines (63 loc) · 1.57 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
import ch.aplu.robotsim.*;
import java.awt.*;
/**
* Solution to the exercise 4.2
*
* The robot count how many objects are on the workspace
*
* @version 13.09.2015
* @author Lukin Petr
*
*/
class Bumper2 {
LegoRobot robot;
Gear gear;
UltrasonicSensor us;
int NumberOfTargets = 0;
Bumper2() {
robot = new LegoRobot();
gear = new Gear();
robot.addPart(gear);
gear.setSpeed(20);
us = new UltrasonicSensor(SensorPort.S1);
robot.addPart(us);
us.setBeamAreaColor(Color.green);
us.setProximityCircleColor(Color.lightGray);
for (int j = 0; j < 4; j++) {
searchTarget();
NumberOfTargets = 0;
}
}
void searchTarget() {
int prevDist = -1;
int i = 0;
while (i < 150) {
gear.right(50);
int distance = us.getDistance();
if (distance != -1) {
if (Math.abs(prevDist - distance) > 30) {
NumberOfTargets++;
gear.right(100);
}
}
prevDist = distance;
i++;
}
System.out.println(NumberOfTargets + " objects were founded.");
gear.right(4500);
gear.forward(5000);
}
public static void main(String[] args) {
new Bumper2();
}
// ------------------ Environment --------------------------
static {
Point[] mesh = { new Point(50, 0), new Point(25, 42),
new Point(-25, 42), new Point(-50, 0), new Point(-25, -42),
new Point(25, -42) };
RobotContext.useTarget("sprites/bumper.png", mesh, 380, 420);
RobotContext.useTarget("sprites/bumper.png", mesh, 150, 300);
RobotContext.useTarget("sprites/bumper.png", mesh, 450, 210);
RobotContext.useTarget("sprites/bumper.png", mesh, 50, 100);
}
}