-
Notifications
You must be signed in to change notification settings - Fork 3
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 #13
Open
NuwanJ
wants to merge
5
commits into
release-1.1.0
Choose a base branch
from
12-implement-compass-emulator
base: release-1.1.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// This robot will move freely, avoiding obstacles | ||
// Written by Nuwan Jaliyagoda | ||
|
||
package Robots; | ||
|
||
import swarm.robot.VirtualRobot; | ||
|
||
public class ObstacleAvoidRobot extends VirtualRobot { | ||
|
||
// The minimum distance that robot tries to keep with the obstacles | ||
private int distanceThreshold = 15; | ||
|
||
// The default movement speed | ||
private int defaultMoveSpeed = 100; | ||
|
||
public ObstacleAvoidRobot(int id, double x, double y, double heading) { | ||
super(id, x, y, heading); | ||
} | ||
|
||
public void setup() { | ||
super.setup(); | ||
} | ||
|
||
@Override | ||
public void loop() throws Exception { | ||
super.loop(); | ||
|
||
if (state == robotState.RUN) { | ||
double dist = distSensor.getDistance(); | ||
|
||
if (dist < distanceThreshold) { | ||
// Generate a random number in [-1000,1000] range | ||
// if even, rotate CW, otherwise rotate CCW an angle depends on the random | ||
// number | ||
int random = -1000 + ((int) ((Math.random() * 2000))); | ||
int sign = (random % 2 == 0) ? 1 : -1; | ||
|
||
System.out.println("\t Wall detected, go back and rotate " + ((sign > 0) ? "CW" : "CCW")); | ||
|
||
// Go back a little | ||
motion.move(-100, -100, 900); | ||
|
||
// rotate | ||
int loopCount = 0; | ||
while (distSensor.getDistance() < distanceThreshold && loopCount < 5) { | ||
// Maximum 5 tries to rotate and find a obstale free path | ||
motion.rotate((int) (defaultMoveSpeed * 0.75 * sign), 1000); | ||
loopCount++; | ||
} | ||
|
||
// rotate a little more | ||
motion.rotate((int) (defaultMoveSpeed * 0.75 * sign), 500); | ||
|
||
|
||
System.out.println("\t\t Compass reading: " + compassSensor.readCompass()); | ||
|
||
} else { | ||
motion.move(defaultMoveSpeed, defaultMoveSpeed, 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
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package swarm.robot.sensors; | ||
|
||
import org.json.simple.JSONObject; | ||
import org.json.simple.parser.ParseException; | ||
import swarm.mqtt.MqttMsg; | ||
import swarm.mqtt.RobotMqttClient; | ||
import swarm.robot.Robot; | ||
import swarm.robot.VirtualRobot; | ||
import swarm.robot.exception.SensorException; | ||
|
||
import java.util.HashMap; | ||
|
||
/** | ||
* Compass Sensors Emulator class | ||
* | ||
* @author Dinuka Mudalige | ||
*/ | ||
public class CompassSensor extends AbstractSensor { | ||
private enum mqttTopic { | ||
COMPASS_OUT | ||
} | ||
|
||
private HashMap<CompassSensor.mqttTopic, String> topicsSub = new HashMap<>(); | ||
private double heading; | ||
|
||
public CompassSensor(Robot robot, RobotMqttClient mqttClient) { | ||
super(robot, mqttClient); | ||
subscribe(CompassSensor.mqttTopic.COMPASS_OUT, "sensor/compass/" + robotId + "/?"); | ||
} | ||
|
||
/** | ||
* Subscribe to a MQTT topic | ||
* | ||
* @param key Subscription topic key | ||
* @param topic Subscription topic value | ||
*/ | ||
private void subscribe(CompassSensor.mqttTopic key, String topic) { | ||
topicsSub.put(key, topic); | ||
robotMqttClient.subscribe(topic); | ||
} | ||
|
||
/** | ||
* Handle compassSensor related MQTT subscriptions | ||
* | ||
* @param robot Robot object | ||
* @param m Subscription topic received object | ||
*/ | ||
@Override | ||
public void handleSubscription(Robot robot, MqttMsg m) throws RuntimeException { | ||
String topic = m.topic, msg = m.message; | ||
|
||
if (topic.equals(topicsSub.get(mqttTopic.COMPASS_OUT))) { | ||
sendCompass(readCompass()); | ||
} else { | ||
System.out.println("Received (unknown): " + topic + "> " + msg); | ||
} | ||
} | ||
|
||
/** | ||
* Get the emulated compass sensor reading from the simulator | ||
* | ||
* @return heading as double | ||
* @throws SensorException | ||
*/ | ||
public double readCompass() { | ||
try { | ||
if (robot instanceof VirtualRobot) { | ||
heading = robot.coordinates.getHeading(); | ||
} else { | ||
robot.handleSubscribeQueue(); | ||
} | ||
} catch (ParseException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return heading; | ||
} | ||
|
||
/** | ||
* Send the compass reading to simulation server | ||
* | ||
* @param compass compass reading | ||
*/ | ||
public void sendCompass(double compass) { | ||
|
||
JSONObject obj = new JSONObject(); | ||
obj.put("id", robotId); | ||
obj.put("compass", compass); | ||
|
||
robotMqttClient.publish("sensor/compass/", obj.toJSONString()); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we need to do the mapping @dhmudalige
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Please refer #15