-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added proximity sensor to MazeFollowingRobot class
- Loading branch information
1 parent
36344ae
commit 8bd0a75
Showing
3 changed files
with
76 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,95 @@ | ||
// This robot will move in a maze environment freely | ||
// Written by Imesh Isuranga | ||
|
||
package Robots.samples; | ||
|
||
import swarm.Interfaces.IRobotState; | ||
import swarm.robot.VirtualRobot; | ||
import swarm.robot.types.ProximityReadingType; | ||
|
||
import java.util.Objects; | ||
|
||
|
||
public class MazeFollowingRobot extends VirtualRobot { | ||
|
||
private int distanceThreshold = 15; | ||
private int nearThreshold = 10; | ||
//minimum distance between grid wall and robot | ||
private final double GRID_SPACE = 18.000; | ||
|
||
// The default movement speed | ||
private int defaultMoveSpeed = 100; | ||
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 { | ||
state = IRobotState.robotState.RUN; | ||
super.loop(); | ||
|
||
|
||
if(state == IRobotState.robotState.RUN){ | ||
double dist = distSensor.getDistance(); | ||
|
||
|
||
if(dist<=nearThreshold){ | ||
if(dist<=1){ | ||
motion.rotateDegree((int) (defaultMoveSpeed * 0.75), 91); | ||
if(distSensor.getDistance()<=1){ | ||
motion.rotateDegree((int) (defaultMoveSpeed * 0.75), -91); | ||
motion.rotateDegree((int) (defaultMoveSpeed * 0.75), -91); | ||
if(distSensor.getDistance()<=1){ | ||
motion.rotateDegree((int) (defaultMoveSpeed * 0.75), -91); | ||
} | ||
} | ||
}else{ | ||
motion.moveDistance(defaultMoveSpeed,1); | ||
//motion.move(defaultMoveSpeed, defaultMoveSpeed, 100); | ||
} | ||
}else{ | ||
motion.moveDistance(defaultMoveSpeed,10); | ||
// motion.move(defaultMoveSpeed, defaultMoveSpeed, 1000); | ||
if (state == robotState.RUN) { | ||
//creating ProximityReadingType object from proximitySensor | ||
ProximityReadingType p = proximitySensor.getProximity(); | ||
|
||
//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); | ||
|
||
} | ||
} | ||
} | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
server=127.0.0.1 | ||
port=1883 | ||
username=user | ||
password=pass | ||
channel="v1" |