Skip to content

Commit 5b2473d

Browse files
Maze Following Robot (#10)
1 parent fb0bc8a commit 5b2473d

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<dependency>
2626
<groupId>pera.swarm</groupId>
2727
<artifactId>java-robot</artifactId>
28-
<version>1.0.2</version>
28+
<version>1.0.3</version>
2929
</dependency>
3030
</dependencies>
3131

src/main/java/App.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.io.IOException;
99
import java.util.Properties;
1010

11+
import Robots.samples.MazeFollowingRobot;
12+
import Robots.samples.ObstacleAvoidRobot;
1113
import Robots.samples.SampleRobot;
1214

1315
public class App {
@@ -33,7 +35,7 @@ public static void main(String[] args) {
3335
reader.close();
3436

3537
// Start a single robot
36-
Robot robot = new SampleRobot(10, 0, 0, 90);
38+
Robot robot = new MazeFollowingRobot(10, 9, 9, 90);
3739
new Thread(robot).start();
3840

3941
// // Start a swarm of robots
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package Robots.samples;
2+
3+
import swarm.robot.VirtualRobot;
4+
5+
/*
6+
* This robot will move in a maze environment freely
7+
*
8+
* @author: Imesh Isuranga
9+
*/
10+
public class MazeFollowingRobot extends VirtualRobot {
11+
12+
// Size of a grid cell
13+
private final double GRID_SPACE = 18.000;
14+
15+
// The default movement speed
16+
private final int defaultMoveSpeed = 100;
17+
18+
// The default rotate speed
19+
private final int defaultRotateSpeed = 100;
20+
21+
// Proximity Sensor options
22+
// Angles for left,front and right side rotating
23+
private int[] proximityAngles = { -90, 0, 90 };
24+
25+
// Index to get left proximity angle
26+
public static int PROXIMITY_LEFT = 0;
27+
28+
// Index to get front proximity angle
29+
public static int PROXIMITY_FRONT = 1;
30+
31+
// Index to get right proximity angle
32+
public static int PROXIMITY_RIGHT = 2;
33+
34+
public MazeFollowingRobot(int id, double x, double y, double heading) {
35+
super(id, x, y, heading);
36+
}
37+
38+
public void setup() {
39+
System.out.println("My Test Robot Started");
40+
41+
super.setup();
42+
43+
// Setup proximity sensor with 3 angles
44+
proximitySensor.setAngles(proximityAngles);
45+
46+
// Start immediately after the setup
47+
state = robotState.RUN;
48+
49+
}
50+
51+
@Override
52+
public void loop() throws Exception {
53+
super.loop();
54+
55+
if (state == robotState.RUN) {
56+
// Get present distances from robot's left,front and right
57+
int[] d = proximitySensor.getProximity().distances();
58+
59+
// Robot rotating way :- if distance from (any side +6) > GRID_SPACE then robot
60+
// will rotate that side.
61+
62+
if (d[PROXIMITY_RIGHT] + 6 > GRID_SPACE) {
63+
// Right
64+
motion.rotateDegree(defaultRotateSpeed, 90);
65+
66+
} else if (d[PROXIMITY_FRONT] + 6 > GRID_SPACE) {
67+
// Front
68+
69+
} else if (d[PROXIMITY_LEFT] + 6 > GRID_SPACE) {
70+
// Turn Left
71+
motion.rotateDegree(defaultRotateSpeed, -90);
72+
73+
} else {
74+
// If robot can't go left,right and front then robot will rotate to back.
75+
motion.rotateDegree(defaultRotateSpeed, 180);
76+
}
77+
78+
// Robot move
79+
motion.moveDistance(defaultMoveSpeed, GRID_SPACE);
80+
delay(1000);
81+
82+
}
83+
}
84+
}

src/main/java/Robots/samples/ObstacleAvoidRobot.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ public void loop() throws Exception {
5555
motion.move(defaultMoveSpeed, defaultMoveSpeed, 1000);
5656
}
5757
}
58+
5859
}
5960
}

0 commit comments

Comments
 (0)