diff --git a/pom.xml b/pom.xml index 9e9b5bb..bdff9b5 100644 --- a/pom.xml +++ b/pom.xml @@ -25,7 +25,7 @@ pera.swarm java-robot - 1.0.2 + 1.0.3 diff --git a/src/main/java/App.java b/src/main/java/App.java index 90ba390..654447b 100644 --- a/src/main/java/App.java +++ b/src/main/java/App.java @@ -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 { @@ -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 diff --git a/src/main/java/Robots/samples/MazeFollowingRobot.java b/src/main/java/Robots/samples/MazeFollowingRobot.java new file mode 100644 index 0000000..97fe403 --- /dev/null +++ b/src/main/java/Robots/samples/MazeFollowingRobot.java @@ -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); + + } + } +} diff --git a/src/main/java/Robots/samples/ObstacleAvoidRobot.java b/src/main/java/Robots/samples/ObstacleAvoidRobot.java index 7621f26..445f20c 100644 --- a/src/main/java/Robots/samples/ObstacleAvoidRobot.java +++ b/src/main/java/Robots/samples/ObstacleAvoidRobot.java @@ -55,5 +55,6 @@ public void loop() throws Exception { motion.move(defaultMoveSpeed, defaultMoveSpeed, 1000); } } + } }