Skip to content

Commit

Permalink
Added proximity sensor to MazeFollowingRobot class
Browse files Browse the repository at this point in the history
  • Loading branch information
Imesh-Isuranga committed Oct 26, 2023
1 parent 36344ae commit 8bd0a75
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 29 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
98 changes: 70 additions & 28 deletions src/main/java/Robots/samples/MazeFollowingRobot.java
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);

}
}
}
}}
5 changes: 5 additions & 0 deletions src/resources/config/sample_mqtt.properties
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"

0 comments on commit 8bd0a75

Please sign in to comment.