Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compass Sensor Implementation V2 #15

Open
wants to merge 7 commits into
base: 12-implement-compass-emulator
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/java/Robots/ObstacleAvoidRobot.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class ObstacleAvoidRobot extends VirtualRobot {
// The default movement speed
private int defaultMoveSpeed = 100;

// private int[] restrictedAngles = {90, 180};

public ObstacleAvoidRobot(int id, double x, double y, double heading) {
super(id, x, y, heading);
}
Expand All @@ -34,6 +36,7 @@ public void loop() throws Exception {
// number
int random = -1000 + ((int) ((Math.random() * 2000)));
int sign = (random % 2 == 0) ? 1 : -1;
// int angle = restrictedAngles[random % restrictedAngles.length];

System.out.println("\t Wall detected, go back and rotate " + ((sign > 0) ? "CW" : "CCW"));

Expand All @@ -43,14 +46,15 @@ public void loop() throws Exception {
// rotate
int loopCount = 0;
while (distSensor.getDistance() < distanceThreshold && loopCount < 5) {
// Maximum 5 tries to rotate and find a obstale free path
// Maximum 5 tries to rotate and find a obstacle free path
motion.rotate((int) (defaultMoveSpeed * 0.75 * sign), 1000);
// motion.rotateDegree((int) (defaultMoveSpeed * 0.75 * sign), angle);
loopCount++;
}

// rotate a little more
motion.rotate((int) (defaultMoveSpeed * 0.75 * sign), 500);

// motion.rotateDegree((int) (defaultMoveSpeed * 0.75 * sign), angle);

System.out.println("\t\t Compass reading: " + compassSensor.readCompass());

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/swarm/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void main(String[] args) {

// Start a single robot
// Robot robot = new MyTestRobot(4, 18, 6, 180);
Robot robot = new ObstacleAvoidRobot(4, 18, 6, 180);
Robot robot = new ObstacleAvoidRobot(10, 45, -45, 180);
new Thread(robot).start();

// // Start a swarm of robots
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/swarm/robot/sensors/CompassSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ private enum mqttTopic {
}

private HashMap<CompassSensor.mqttTopic, String> topicsSub = new HashMap<>();
private double heading;
private double reading;

// private static final double TOLERANCE = 0.000001; // Value should be closer to zero

private static final double NORTH = 0.0; // Should measure relative to a reference (here, to the horizontal)
dhmudalige marked this conversation as resolved.
Show resolved Hide resolved

public CompassSensor(Robot robot, RobotMqttClient mqttClient) {
super(robot, mqttClient);
Expand Down Expand Up @@ -65,15 +69,18 @@ public void handleSubscription(Robot robot, MqttMsg m) throws RuntimeException {
public double readCompass() {
try {
if (robot instanceof VirtualRobot) {
heading = robot.coordinates.getHeading();

double robotHead = robot.coordinates.getHeading();
reading = convertToNorth(robotHead);

} else {
robot.handleSubscribeQueue();
}
} catch (ParseException e) {
e.printStackTrace();
}

return heading;
return reading;
}

/**
Expand All @@ -90,4 +97,12 @@ public void sendCompass(double compass) {
robotMqttClient.publish("sensor/compass/", obj.toJSONString());
}

}
private double convertToNorth(double measuredValue) {
double bearing = NORTH - measuredValue;

// Normalize the result to ensure it's within [0, 360)
bearing = (bearing + 360) % 360;

return bearing;
}
}