|
| 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 | +} |
0 commit comments