Skip to content

Commit

Permalink
Maze Following Robot (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Imesh-Isuranga authored Oct 28, 2023
1 parent fb0bc8a commit 5b2473d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependency>
<groupId>pera.swarm</groupId>
<artifactId>java-robot</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</dependency>
</dependencies>

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.io.IOException;
import java.util.Properties;

import Robots.samples.MazeFollowingRobot;
import Robots.samples.ObstacleAvoidRobot;
import Robots.samples.SampleRobot;

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

// Start a single robot
Robot robot = new SampleRobot(10, 0, 0, 90);
Robot robot = new MazeFollowingRobot(10, 9, 9, 90);
new Thread(robot).start();

// // Start a swarm of robots
Expand Down
84 changes: 84 additions & 0 deletions src/main/java/Robots/samples/MazeFollowingRobot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package Robots.samples;

import swarm.robot.VirtualRobot;

/*
* This robot will move in a maze environment freely
*
* @author: Imesh Isuranga
*/
public class MazeFollowingRobot extends VirtualRobot {

// Size of a grid cell
private final double GRID_SPACE = 18.000;

// The default movement speed
private final int defaultMoveSpeed = 100;

// The default rotate speed
private final int defaultRotateSpeed = 100;

// Proximity Sensor options
// Angles for left,front and right side rotating
private int[] proximityAngles = { -90, 0, 90 };

// Index to get left proximity angle
public static int PROXIMITY_LEFT = 0;

// Index to get front proximity angle
public static int PROXIMITY_FRONT = 1;

// Index to get right proximity angle
public static int PROXIMITY_RIGHT = 2;

public MazeFollowingRobot(int id, double x, double y, double heading) {
super(id, x, y, heading);
}

public void setup() {
System.out.println("My Test Robot Started");

super.setup();

// Setup proximity sensor with 3 angles
proximitySensor.setAngles(proximityAngles);

// Start immediately after the setup
state = robotState.RUN;

}

@Override
public void loop() throws Exception {
super.loop();

if (state == robotState.RUN) {
// Get present distances from robot's left,front and right
int[] d = proximitySensor.getProximity().distances();

// Robot rotating way :- if distance from (any side +6) > GRID_SPACE then robot
// will rotate that side.

if (d[PROXIMITY_RIGHT] + 6 > GRID_SPACE) {
// Right
motion.rotateDegree(defaultRotateSpeed, 90);

} else if (d[PROXIMITY_FRONT] + 6 > GRID_SPACE) {
// Front

} else if (d[PROXIMITY_LEFT] + 6 > GRID_SPACE) {
// Turn Left
motion.rotateDegree(defaultRotateSpeed, -90);

} else {
// If robot can't go left,right and front then robot will rotate to back.
motion.rotateDegree(defaultRotateSpeed, 180);
}

// Robot move
motion.moveDistance(defaultMoveSpeed, GRID_SPACE);
delay(1000);

}
}
}
1 change: 1 addition & 0 deletions src/main/java/Robots/samples/ObstacleAvoidRobot.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ public void loop() throws Exception {
motion.move(defaultMoveSpeed, defaultMoveSpeed, 1000);
}
}

}
}

0 comments on commit 5b2473d

Please sign in to comment.