diff --git a/.github/workflows/java-ci.yml b/.github/workflows/java-ci.yml new file mode 100644 index 0000000..f7ca86c --- /dev/null +++ b/.github/workflows/java-ci.yml @@ -0,0 +1,18 @@ +name: Java CI + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'adopt' + + - name: Build with Maven + run: mvn -B package --file pom.xml diff --git a/.github/workflows/maven-publish.yml b/.github/workflows/maven-publish.yml index 60ba20e..3ed1552 100644 --- a/.github/workflows/maven-publish.yml +++ b/.github/workflows/maven-publish.yml @@ -1,7 +1,7 @@ # This workflow will build a package using Maven and then publish it to GitHub packages when a release is created # For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path -name: Maven Package +name: Maven Publish on: release: @@ -21,7 +21,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Set up JDK 11 - uses: actions/setup-java@v2 + uses: actions/setup-java@v3 with: java-version: '11' distribution: 'adopt' diff --git a/.vscode/settings.json b/.vscode/settings.json index c5f3f6b..e0f15db 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "java.configuration.updateBuildConfiguration": "interactive" + "java.configuration.updateBuildConfiguration": "automatic" } \ No newline at end of file diff --git a/README.md b/README.md index 3749856..a7ecf02 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0) +[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0) [![Java CI](https://github.com/Pera-Swarm/robot-library-java/actions/workflows/java-ci.yml/badge.svg)](https://github.com/Pera-Swarm/robot-library-java/actions/workflows/java-ci.yml) [![Maven Package](https://github.com/Pera-Swarm/robot-library-java/actions/workflows/maven-publish.yml/badge.svg)](https://github.com/Pera-Swarm/robot-library-java/actions/workflows/maven-publish.yml) -# java-swarm-node +# Robot Library (Java) -This is the Java Swarm Node Implementation for the project, *Pera-Swarm* +This is the Java Library Implementation of the Virtual Robots under, *Pera-Swarm* More details will be updated soon. @@ -10,13 +10,11 @@ More details will be updated soon. ## Local Environment Setup -If you need to run this repository on your local environment, -please create a file named *'mqtt.properties'* in path, *'./src/resources/config/'* -as follows and provide your MQTT broker's configurations. +If you need to run this repository on your local environment, please create a file named *'mqtt.properties'* in path, *'./src/resources/config/'* as follows and provide your MQTT Broker's configurations. -You can select any channel, as same as your simulation server runs on. +You can select any channel, as same as your [Simulation Server](https://github.com/Pera-Swarm/swarm-simulator) runs on. -``` +```bash server=127.0.0.1 port=1883 username=user diff --git a/node.iml b/node.iml index 0b9ffe9..82c5871 100644 --- a/node.iml +++ b/node.iml @@ -1,5 +1,6 @@ - + @@ -10,8 +11,10 @@ - - + + diff --git a/pom.xml b/pom.xml index cd7f2d4..eadc099 100644 --- a/pom.xml +++ b/pom.xml @@ -1,15 +1,14 @@ - + 4.0.0 - swarm.node - swarm-node - 1.0.0-SNAPSHOT + pera.swarm + java-robot + 0.9.1-SNAPSHOT - Pera-swarm - http://pera-swarm.github.io/ + Pera-Swarm Java Node + https://github.com/Pera-Swarm/robot-library-java UTF-8 @@ -17,24 +16,19 @@ 1.7 - - - - - github - GitHub OWNER Apache Maven Packages - https://maven.pkg.github.com/Pera-Swarm/java-swarm-node - + + scm:git:https://github.com/Pera-Swarm/robot-library-java.git + HEAD + + Eclipse Paho Repo @@ -66,14 +60,17 @@ - + - + maven-clean-plugin 3.1.0 - + maven-resources-plugin 3.0.2 @@ -95,7 +92,8 @@ maven-deploy-plugin 2.8.2 - + maven-site-plugin 3.7.1 @@ -107,4 +105,4 @@ - + \ No newline at end of file diff --git a/release.properties b/release.properties new file mode 100644 index 0000000..cbbb9a3 --- /dev/null +++ b/release.properties @@ -0,0 +1,22 @@ +#release configuration +#Fri Jul 28 22:04:29 IST 2023 +projectVersionPolicyId=default +scm.rollbackCommitComment=@{prefix} rollback the release of @{releaseLabel} +remoteTagging=true +scm.commentPrefix=[maven-release-plugin] +releaseStrategyId=default +scm.branchCommitComment=@{prefix} prepare branch @{releaseLabel} +project.scm.pera.swarm\:java-robot.tag=HEAD +pinExternals=false +completedPhase=check-poms +scm.url=scm\:git\:https\://github.com/Pera-Swarm/robot-library-java.git +scm.developmentCommitComment=@{prefix} prepare for next development iteration +projectVersionPolicyConfig=${projectVersionPolicyConfig}\n +scm.tagNameFormat=@{project.artifactId}-@{project.version} +exec.activateProfiles=github +pushChanges=true +project.scm.pera.swarm\:java-robot.developerConnection=scm\:git\:https\://github.com/Pera-Swarm/robot-library-java.git +exec.snapshotReleasePluginAllowed=false +preparationGoals=clean verify +scm.releaseCommitComment=@{prefix} prepare release @{releaseLabel} +exec.pomFileName=pom.xml diff --git a/settings.xml b/settings.xml index 12fe912..fefca9a 100644 --- a/settings.xml +++ b/settings.xml @@ -1,7 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> github diff --git a/src/main/java/Robots/MyTestRobot.java b/src/main/java/Robots/MyTestRobot.java new file mode 100644 index 0000000..d4b249a --- /dev/null +++ b/src/main/java/Robots/MyTestRobot.java @@ -0,0 +1,30 @@ +package Robots; + +import swarm.robot.VirtualRobot; + +public class MyTestRobot extends VirtualRobot { + + public MyTestRobot(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(); + } + + public void loop() throws Exception { + super.loop(); + + if (state == robotState.RUN) { + System.out.println("Test"); + delay(1000); + } + } + + @Override + public void communicationInterrupt(String msg) { + System.out.println("communicationInterrupt on " + id + " with msg:" + msg); + } + +} \ No newline at end of file diff --git a/src/main/java/robotImplementations/ColorRippleRobot.java b/src/main/java/robotImplementations/ColorRippleRobot.java deleted file mode 100644 index 5304c97..0000000 --- a/src/main/java/robotImplementations/ColorRippleRobot.java +++ /dev/null @@ -1,75 +0,0 @@ -package robotImplementations; - -import swarm.robot.VirtualRobot; - -public class ColorRippleRobot extends VirtualRobot { - - private int currentHopId; - private boolean colorUpdated; - - public ColorRippleRobot(int id, double x, double y, double heading) { - super(id, x, y, heading); - } - - public void setup() { - super.setup(); - - neoPixel.changeColor(0, 0, 0); - colorUpdated = false; - currentHopId = -1; - coordinates.publishCoordinate(); - } - - @Override - public void loop() throws Exception { - super.loop(); - // Anything specially check in continuously - } - - @Override - public void interrupt() { - - } - - @Override - public void communicationInterrupt(String msg) { - System.out.println("communicationInterrupt on " + id + " with msg:" + msg); - - String[] s = msg.split(" "); - - if (s.length == 4) { - int hopId = Integer.parseInt(s[0]); - - if (colorUpdated) { - // a returning message, don't forward - neoPixel.changeColor(0, 0, 0); - - } else if (hopId > currentHopId) { - int hopR = Integer.parseInt(s[1]); - int hopG = Integer.parseInt(s[2]); - int hopB = Integer.parseInt(s[3]); - neoPixel.changeColor(hopR, hopG, hopB); - currentHopId = hopId; - colorUpdated = true; - - delay(2000); - - // Send it to the next robot - simpleComm.sendMessage((hopId + 1) + " " + hopR + " " + hopG + " " + hopB); - neoPixel.changeColor(0, 0, 0); - } - } else { - System.out.println("Invalid msg received"); - } - } - - public void start() { - super.start(); - - // Things to do when start action - neoPixel.changeColor(0, 0, 0); - colorUpdated = false; - currentHopId = -1; - } - -} diff --git a/src/main/java/robotImplementations/DiscoverColorRobot.java b/src/main/java/robotImplementations/DiscoverColorRobot.java deleted file mode 100644 index 51576f2..0000000 --- a/src/main/java/robotImplementations/DiscoverColorRobot.java +++ /dev/null @@ -1,152 +0,0 @@ -package robotImplementations; - -import swarm.robot.VirtualRobot; -import swarm.robot.types.RGBColorType; - -public class DiscoverColorRobot extends VirtualRobot { - - private int currentHopId; - private boolean colorUpdated; - private final RGBColorType obstacleColor; - private boolean searching; - - public DiscoverColorRobot(int id, double x, double y, double heading, RGBColorType obstacleColor) { - super(id, x, y, heading); - this.obstacleColor = obstacleColor; - } - - public void setup() { - super.setup(); - neoPixel.changeColor(0, 0, 0); - searching = true; - } - - @Override - public void loop() throws Exception { - super.loop(); -// System.out.println(state); - - if (searching && state == robotState.RUN) { - double dist = distSensor.getDistance(); - - if (dist < 15) { - RGBColorType color = colorSensor.getColor(); - System.out.println("\t An obstacle detected, stop"); - System.out.println("observed:" + color.toString() + " must be:" + obstacleColor.toString()); - - if (obstacleColor.compareTo(color)) { - // Color is matching - System.out.println("\t Matching obstacle detected"); - String id = Integer.toString(getId()); - simpleComm.sendMessage("1 " + obstacleColor.toStringColor(), 70); - neoPixel.changeColor(obstacleColor.getR(), obstacleColor.getG(), obstacleColor.getB()); - state = robotState.WAIT; - searching = false; - - } else { - // 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 Not the obstacle we are looking for, go back and rotate " + ((sign > 0) ? "CW" : "CCW")); - - // Go back a little - motion.move(-100, -100, 900); - - // rotate - int loopCount = 0; // to avoid infinity loop - while (distSensor.getDistance() < 35 && loopCount < 5) { - motion.rotate(50 * sign, 1000); - loopCount++; - } - // TODO: This is a temp update to restrict the robot into arena - // if (coordinates.getX() >= 90) coordinates.setX(85); - // if (coordinates.getX() <= -90) coordinates.setX(-85); - // if (coordinates.getY() >= 90) coordinates.setY(85); - // if (coordinates.getY() <= -90) coordinates.setY(-85); - - // rotate a little - motion.rotate(50 * sign, 500); - - } - } else { - motion.move(100, 100, 1000); - delay(1000); - } - } else { - // Task is completed, waiting - delay(150); - } - } - - @Override - public void sensorInterrupt(String sensor, String value) { - - switch (sensor) { - case "distance": - System.out.println("Distance sensor interrupt on " + id); - break; - - case "color": - System.out.println("Color sensor interrupt on " + id); - break; - - case "proximity": - System.out.println("Proximity sensor interrupt on " + id); - break; - - default: - System.out.println("Unknown sensor type"); - } - } - - @Override - public void communicationInterrupt(String msg) { - - // split the message - String[] s = msg.split(" "); - System.out.println("communicationInterrupt on " + id + " with msg:" + msg); - - if (s.length == 4) { - int hopId = Integer.parseInt(s[0]); - - if (hopId > currentHopId) { - int hopR = Integer.parseInt(s[1]); - int hopG = Integer.parseInt(s[2]); - int hopB = Integer.parseInt(s[3]); - neoPixel.changeColor(hopR, hopG, hopB); - currentHopId = hopId; - colorUpdated = true; - - delay(2000); - - // Send it to the next robot - simpleComm.sendMessage((hopId + 1) + " " + hopR + " " + hopG + " " + hopB, 120); - - searching = false; - System.out.println("Send the received message..."); - } - } else { - System.out.println("Invalid msg received"); - } - } - - public void reset() { - neoPixel.changeColor(0, 0, 0); - colorUpdated = false; - currentHopId = -1; - searching = true; - } - - public void start() { - super.start(); - - // Things to do when start action - neoPixel.changeColor(0, 0, 0); - colorUpdated = false; - currentHopId = -1; - searching = true; - } - -} diff --git a/src/main/java/robotImplementations/ObstacleAvoidRobot.java b/src/main/java/robotImplementations/ObstacleAvoidRobot.java deleted file mode 100644 index d37eef9..0000000 --- a/src/main/java/robotImplementations/ObstacleAvoidRobot.java +++ /dev/null @@ -1,53 +0,0 @@ -package robotImplementations; - -import swarm.robot.VirtualRobot; - -public class ObstacleAvoidRobot extends VirtualRobot { - - public ObstacleAvoidRobot(int id, double x, double y, double heading) { - super(id, x, y, heading); - } - - public void setup() { - super.setup(); - } - - public void loop() throws Exception { - super.loop(); - - if(state==robotState.RUN) { - double dist = distSensor.getDistance(); - - if (dist < 15) { - - // 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; // to avoid infinity loop - while (distSensor.getDistance() < 35 && loopCount < 5) { - motion.rotate(50 * sign, 1000); - loopCount++; - } - // TODO: This is a temp update to restrict the robot into arena - // if (coordinates.getX() >= 90) coordinates.setX(85); - // if (coordinates.getX() <= -90) coordinates.setX(-85); - // if (coordinates.getY() >= 90) coordinates.setY(85); - // if (coordinates.getY() <= -90) coordinates.setY(-85); - - // rotate a little - motion.rotate(50 * sign, 500); - - } else { - motion.move(100, 100, 1000); - } - } - } -} diff --git a/src/main/java/swarm/Interfaces/IMqttHandler.java b/src/main/java/swarm/Interfaces/IMqttHandler.java index be12194..aa1aa66 100644 --- a/src/main/java/swarm/Interfaces/IMqttHandler.java +++ b/src/main/java/swarm/Interfaces/IMqttHandler.java @@ -1,9 +1,7 @@ package swarm.Interfaces; + import swarm.mqtt.MqttMsg; import swarm.robot.Robot; -import swarm.robot.exception.RGBColorException; - -import java.util.HashMap; public interface IMqttHandler { diff --git a/src/main/java/swarm/Interfaces/IRobotState.java b/src/main/java/swarm/Interfaces/IRobotState.java index d5426b2..f02c522 100644 --- a/src/main/java/swarm/Interfaces/IRobotState.java +++ b/src/main/java/swarm/Interfaces/IRobotState.java @@ -1,18 +1,21 @@ package swarm.Interfaces; -public interface IRobotState -{ - enum robotState {WAIT, RUN, BEGIN} +public interface IRobotState { + enum robotState { + WAIT, RUN, BEGIN + } public void loop() throws Exception; - // public void execute(); public void interrupt(); public void sensorInterrupt(String sensor, String value); + public void communicationInterrupt(String msg); public void start(); + public void stop(); + public void reset(); } diff --git a/src/main/java/swarm/Swarm.java b/src/main/java/swarm/Swarm.java index 087b053..d90d351 100644 --- a/src/main/java/swarm/Swarm.java +++ b/src/main/java/swarm/Swarm.java @@ -2,20 +2,14 @@ import swarm.configs.MQTTSettings; import swarm.robot.Robot; -import swarm.robot.VirtualRobot; -import swarm.robot.exception.RGBColorException; -import swarm.robot.types.RGBColorType; - -import robotImplementations.ColorRippleRobot; -import robotImplementations.DiscoverColorRobot; -import robotImplementations.ObstacleAvoidRobot; - import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Properties; +import Robots.*; + public class Swarm extends Thread { public static void main(String[] args) { @@ -23,7 +17,8 @@ public static void main(String[] args) { try { // COMPLETE THIS BEFORE RUN // Read config properties from the file, src/resources/config/mqtt.properties - // If it isn't there, please make one, as given sample in the 'sample_mqtt.properties' file + // If it isn't there, please make one, as given sample in the + // 'sample_mqtt.properties' file File configFile = new File("src/resources/config/mqtt.properties"); FileReader reader = new FileReader(configFile); @@ -37,9 +32,10 @@ public static void main(String[] args) { MQTTSettings.channel = props.getProperty("channel", "v1"); reader.close(); - obstacleAvoidingExperiment(); - // colorRippleExperiment(); - // discoverColorExperiment(); + // TODO: Add the robot formation in here + + Robot robot = new MyTestRobot(10, 0, 0, 90); + new Thread(robot).start(); } catch (FileNotFoundException ex) { // file does not exist @@ -50,103 +46,4 @@ public static void main(String[] args) { System.out.println("IO Error !!!"); } } - - private static void obstacleAvoidingExperiment() { - - Robot[] vr = new VirtualRobot[5]; - - vr[0] = new ObstacleAvoidRobot(10, -52, 32, 45); - vr[1] = new ObstacleAvoidRobot(11, -32, -12, -20); - vr[2] = new ObstacleAvoidRobot(12, 55, -23, 3); - vr[3] = new ObstacleAvoidRobot(13, 54, 65, -70); - vr[4] = new ObstacleAvoidRobot(14, -30, -40, 105); - - for (Robot robot : vr) { -// vr[i] = new ObstacleAvoidRobot(robotList[i], -50 + 25 * i, 0, 90); - new Thread(robot).start(); - } - - } - - private static void colorRippleExperiment() { - // Note: 0,1,2,6 and 7 are ignored; hardware robots - int[] robotList = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - circularFormation(robotList, 0, 0, 0, 60, 0, 36); - } - - private static void discoverColorExperiment() { - - Robot[] vr = new VirtualRobot[10]; - RGBColorType obstacleColor = null; - try { - obstacleColor = new RGBColorType(255, 0, 0); - } catch (RGBColorException e) { - e.printStackTrace(); - } - - vr[0] = new DiscoverColorRobot(10, -52, 32, 45, obstacleColor); - vr[1] = new DiscoverColorRobot(11, -32, -12, -20, obstacleColor); - vr[2] = new DiscoverColorRobot(12, 49, -23, 3, obstacleColor); - vr[3] = new DiscoverColorRobot(13, 54, 65, -70, obstacleColor); - vr[4] = new DiscoverColorRobot(14, 0, -40, 105, obstacleColor); - -// vr[5] = new DiscoverColorRobot(0, 80, 20, 90, obstacleColor); -// vr[6] = new DiscoverColorRobot(1, 40, -80, -90, obstacleColor); -// vr[7] = new DiscoverColorRobot(2, -40, -70, 45, obstacleColor); -// vr[8] = new DiscoverColorRobot(6, -50, -50, -90, obstacleColor); -// vr[9] = new DiscoverColorRobot(7, -70, 0, -45, obstacleColor); - - //robots start to move - for (Robot robot : vr) { - new Thread(robot).start(); - } - } - - private static void lineFormation(int[] robotList, int startX, int startY, int heading, int incX, int incY) { - Robot[] vr = new VirtualRobot[robotList.length]; - - for (int i = 0; i < robotList.length; i++) { - vr[i] = new ObstacleAvoidRobot(robotList[i], startX + incX * i, startY + incY * i, heading); - new Thread(vr[i]).start(); - } - } - - private static void circularFormation(int[] robotList, int centerX, int centerY, int headingOffset, int radius, int startAngle, int deltaAngle) { - Robot[] vr = new VirtualRobot[robotList.length]; - - int x, y, robotHeading; - - for (int i = 0; i < robotList.length; i++) { - double a = (startAngle + i * deltaAngle); - x = (int) (radius * Math.cos(a * Math.PI / 180)); - y = (int) (radius * Math.sin(a * Math.PI / 180)); - robotHeading = (int) (a + headingOffset); - - if (i == 0 || i == 1 || i == 2 || i == 6 | i == 7) { - System.out.println(i + "> x:" + x + " y:" + y + " heading:" + robotHeading); - } else { - vr[i] = new ColorRippleRobot(robotList[i], x, y, robotHeading); - // vr[i] = new ObstacleAvoidRobot(robotList[i], x, y, robotHeading); - new Thread(vr[i]).start(); - } - - } - } - - private static void spiralFormation(int[] robotList, int centerX, int centerY, int headingOffset, int startRadius, int deltaRadius, int startAngle, int deltaAngle) { - Robot[] vr = new VirtualRobot[robotList.length]; - - int x, y, robotHeading; - - for (int i = 0; i < robotList.length; i++) { - double a = (startAngle + i * deltaAngle); - double r = startRadius + (deltaRadius * i); - x = (int) (r * Math.cos(a * Math.PI / 180)); - y = (int) (r * Math.sin(a * Math.PI / 180)); - robotHeading = (int) (a + headingOffset); - - vr[i] = new ColorRippleRobot(robotList[i], x, y, robotHeading); - new Thread(vr[i]).start(); - } - } } diff --git a/src/main/java/swarm/configs/MQTTSettings.java b/src/main/java/swarm/configs/MQTTSettings.java index 399d3fc..d2a65ce 100644 --- a/src/main/java/swarm/configs/MQTTSettings.java +++ b/src/main/java/swarm/configs/MQTTSettings.java @@ -8,12 +8,11 @@ public class MQTTSettings { public static String password; public static String channel; - public static void print(){ + public static void print() { System.out.println("server: " + server); System.out.println("port: " + port); System.out.println("username: " + userName); System.out.println("password: " + password); System.out.println("channel: " + channel); - } } diff --git a/src/main/java/swarm/mqtt/RobotMqttClient.java b/src/main/java/swarm/mqtt/RobotMqttClient.java index 8543e07..b8cc793 100644 --- a/src/main/java/swarm/mqtt/RobotMqttClient.java +++ b/src/main/java/swarm/mqtt/RobotMqttClient.java @@ -25,7 +25,6 @@ public class RobotMqttClient implements MqttCallback { public Queue outQueue = new PriorityQueue(); // Documentation: https://www.eclipse.org/paho/files/javadoc/index.html - public RobotMqttClient(String server, int port, String userName, String password, String channel) { this.server = server; @@ -76,7 +75,7 @@ public void publish(String topic, String body, int qos) { if (isConnected && topic.length() > 0 && body.length() > 0) { // Connected, non empty topic and body - String t = channel + "/" + topic; // prepare topic with message channel + String t = channel + "/" + topic; // prepare topic with message channel MqttMessage m = new MqttMessage(body.getBytes()); m.setQos(qos); @@ -100,7 +99,7 @@ public void subscribe(String topic) { if (isConnected && topic.length() > 0) { // connected and non empty topic try { - String t = channel + "/" + topic; // prepare topic with message channel + String t = channel + "/" + topic; // prepare topic with message channel client.subscribe(t); System.out.println("Subscribed to " + t); @@ -141,7 +140,8 @@ public void messageArrived(String topic, MqttMessage message) throws Exception { String msg = new String(message.getPayload()); if (msg.length() > 0) { - //System.out.println("Received " + topic + " >> " + new String(message.getPayload())); + // System.out.println("Received " + topic + " >> " + new + // String(message.getPayload())); inQueue.add(new MqttMsg(t, msg)); } } @@ -154,5 +154,4 @@ public MqttMsg outQueue() { return this.outQueue.poll(); } - } \ No newline at end of file diff --git a/src/main/java/swarm/patterns/patterns.plantuml b/src/main/java/swarm/patterns/patterns.plantuml deleted file mode 100644 index d0964df..0000000 --- a/src/main/java/swarm/patterns/patterns.plantuml +++ /dev/null @@ -1,62 +0,0 @@ -@startuml - -title __PATTERNS's Class Diagram__\n - - namespace swarm { - namespace patterns { - class swarm.patterns.CircularMove { - + CircularMove() - + loop() - + setup() - } - } - } - - - namespace swarm { - namespace patterns { - class swarm.patterns.GoToGoal { - + GoToGoal() - + loop() - + setup() - } - } - } - - - namespace swarm { - namespace patterns { - class swarm.patterns.LinearMove { - + LinearMove() - + loop() - + setup() - } - } - } - - - namespace swarm { - namespace patterns { - abstract class swarm.patterns.Pattern { - + Pattern() - + loop() - + setup() - } - } - } - - - swarm.patterns.CircularMove -up-|> swarm.patterns.Pattern - swarm.patterns.GoToGoal -up-|> swarm.patterns.Pattern - swarm.patterns.LinearMove -up-|> swarm.patterns.Pattern - swarm.patterns.Pattern o-- swarm.robot.Robot : r - - -right footer - - -PlantUML diagram generated by SketchIt! (https://bitbucket.org/pmesmeur/sketch.it) -For more information about this tool, please contact philippe.mesmeur@gmail.com -endfooter - -@enduml diff --git a/src/main/java/swarm/robot/Robot.java b/src/main/java/swarm/robot/Robot.java index 19e4e42..b5310d0 100644 --- a/src/main/java/swarm/robot/Robot.java +++ b/src/main/java/swarm/robot/Robot.java @@ -45,7 +45,8 @@ public Robot(int id, double x, double y, double heading, char reality) { this.id = id; this.reality = reality; - robotMqttClient = new RobotMqttClient(MQTTSettings.server, MQTTSettings.port, MQTTSettings.userName, MQTTSettings.password, MQTTSettings.channel); + robotMqttClient = new RobotMqttClient(MQTTSettings.server, MQTTSettings.port, MQTTSettings.userName, + MQTTSettings.password, MQTTSettings.channel); coordinates = new Coordinate(id, x, y, heading, robotMqttClient); robotMQTT = new RobotMQTT(id, robotMqttClient, reality); @@ -83,7 +84,7 @@ public int getId() { public void run() { setup(); - //noinspection InfiniteLoopStatement + // noinspection InfiniteLoopStatement while (true) { try { loop(); @@ -107,7 +108,7 @@ public void run() { public void handleSubscribeQueue() throws ParseException { // Handle the messages in incoming queue - while(!robotMqttClient.inQueue.isEmpty()){ + while (!robotMqttClient.inQueue.isEmpty()) { MqttMsg m = robotMqttClient.inQueue.poll(); if (m != null) { @@ -165,7 +166,7 @@ public void handleSubscribeQueue() throws ParseException { private void handlePublishQueue() { // Publish messages which are collected in the outgoing queue - while(!robotMqttClient.outQueue.isEmpty()){ + while (!robotMqttClient.outQueue.isEmpty()) { MqttMsg m = robotMqttClient.outQueue.poll(); assert m != null; robotMqttClient.publish(m.topic, m.message, m.QoS); diff --git a/src/main/java/swarm/robot/VirtualRobot.java b/src/main/java/swarm/robot/VirtualRobot.java index 65f0a74..6b10afb 100644 --- a/src/main/java/swarm/robot/VirtualRobot.java +++ b/src/main/java/swarm/robot/VirtualRobot.java @@ -18,23 +18,23 @@ public void interrupt() { @Override public void sensorInterrupt(String sensor, String value) { -// switch (sensor) { -// case "distance": -// System.out.println("Distance sensor interrupt on " + id); -// break; -// -// case "color": -// System.out.println("Color sensor interrupt on " + id); -// break; -// -// case "proximity": -// System.out.println("Proximity sensor interrupt on " + id); -// break; -// -// default: -// // TODO: make an exception other than println -// System.out.println("Unknown sensor type"); -// } + switch (sensor) { + case "distance": + System.out.println("Distance sensor interrupt on " + id + "with value" + value); + break; + + case "color": + System.out.println("Color sensor interrupt on " + id + "with value" + value); + break; + + case "proximity": + System.out.println("Proximity sensor interrupt on " + id + "with value" + value); + break; + + default: + // TODO: make an exception other than println + System.out.println("Unknown sensor type"); + } } @Override diff --git a/src/main/java/swarm/robot/communication/DirectedCommunication.java b/src/main/java/swarm/robot/communication/DirectedCommunication.java index 17743ba..7d5e5f0 100644 --- a/src/main/java/swarm/robot/communication/DirectedCommunication.java +++ b/src/main/java/swarm/robot/communication/DirectedCommunication.java @@ -9,7 +9,9 @@ public class DirectedCommunication extends Communication { - enum mqttTopic {COMMUNICATION_IN_DIR} + enum mqttTopic { + COMMUNICATION_IN_DIR + } private final HashMap topicsSub = new HashMap(); @@ -19,8 +21,8 @@ public DirectedCommunication(int robotId, RobotMqttClient m) { } private void subscribe(mqttTopic key, String topic) { - topicsSub.put(key, topic); // Put to the queue - robotMqttClient.subscribe(topic); // Subscribe through MqttHandler + topicsSub.put(key, topic); // Put to the queue + robotMqttClient.subscribe(topic); // Subscribe through MqttHandler } public void sendMessage(String msg) { diff --git a/src/main/java/swarm/robot/communication/SimpleCommunication.java b/src/main/java/swarm/robot/communication/SimpleCommunication.java index e2b86ae..65c55ad 100644 --- a/src/main/java/swarm/robot/communication/SimpleCommunication.java +++ b/src/main/java/swarm/robot/communication/SimpleCommunication.java @@ -9,7 +9,9 @@ public class SimpleCommunication extends Communication { - enum mqttTopic {COMMUNICATION_IN_SIMP} + enum mqttTopic { + COMMUNICATION_IN_SIMP + } private final HashMap topicsSub = new HashMap(); @@ -19,8 +21,8 @@ public SimpleCommunication(int robotId, RobotMqttClient m) { } private void subscribe(mqttTopic key, String topic) { - topicsSub.put(key, topic); // Put to the queue - robotMqttClient.subscribe(topic); // Subscribe through MqttHandler + topicsSub.put(key, topic); // Put to the queue + robotMqttClient.subscribe(topic); // Subscribe through MqttHandler } public void sendMessage(String msg) { @@ -53,4 +55,3 @@ public void handleSubscription(Robot robot, MqttMsg m) { } } } - diff --git a/src/main/java/swarm/robot/exception/MqttClientException.java b/src/main/java/swarm/robot/exception/MqttClientException.java index 108c029..efba17a 100644 --- a/src/main/java/swarm/robot/exception/MqttClientException.java +++ b/src/main/java/swarm/robot/exception/MqttClientException.java @@ -1,7 +1,7 @@ package swarm.robot.exception; -public class MqttClientException extends Exception{ - public MqttClientException(String s){ +public class MqttClientException extends Exception { + public MqttClientException(String s) { System.out.println("MQTT Error: " + s); } } \ No newline at end of file diff --git a/src/main/java/swarm/robot/exception/ProximityException.java b/src/main/java/swarm/robot/exception/ProximityException.java index f9539e0..0096f7c 100644 --- a/src/main/java/swarm/robot/exception/ProximityException.java +++ b/src/main/java/swarm/robot/exception/ProximityException.java @@ -2,7 +2,6 @@ public class ProximityException extends Exception { public ProximityException(String msg) { - // TODO: what is the best test ? System.out.println("Proximity reading error: " + msg); } } diff --git a/src/main/java/swarm/robot/exception/SensorException.java b/src/main/java/swarm/robot/exception/SensorException.java index d060161..21150fc 100644 --- a/src/main/java/swarm/robot/exception/SensorException.java +++ b/src/main/java/swarm/robot/exception/SensorException.java @@ -1,8 +1,7 @@ package swarm.robot.exception; -public class SensorException extends Exception -{ - public SensorException(String s){ +public class SensorException extends Exception { + public SensorException(String s) { System.out.println("Sensor Error: " + s); } } diff --git a/src/main/java/swarm/robot/helpers/Coordinate.java b/src/main/java/swarm/robot/helpers/Coordinate.java index 11ed8fd..4513219 100644 --- a/src/main/java/swarm/robot/helpers/Coordinate.java +++ b/src/main/java/swarm/robot/helpers/Coordinate.java @@ -18,7 +18,9 @@ public class Coordinate implements IMqttHandler { protected RobotMqttClient robotMqttClient; private final int robotId; - private enum mqttTopic {ROBOT_LOCALIZATION} + private enum mqttTopic { + ROBOT_LOCALIZATION + } private final HashMap topicsSub = new HashMap(); @@ -33,8 +35,8 @@ public Coordinate(int robotId, double x, double y, double heading, RobotMqttClie } private void subscribe(mqttTopic key, String topic) { - topicsSub.put(key, topic); // Put to the queue - robotMqttClient.subscribe(topic); // Subscribe through MqttHandler + topicsSub.put(key, topic); // Put to the queue + robotMqttClient.subscribe(topic); // Subscribe through MqttHandler } public void handleSubscription(Robot r, MqttMsg m) { diff --git a/src/main/java/swarm/robot/helpers/MotionController.java b/src/main/java/swarm/robot/helpers/MotionController.java index 3a2de28..df754a6 100644 --- a/src/main/java/swarm/robot/helpers/MotionController.java +++ b/src/main/java/swarm/robot/helpers/MotionController.java @@ -3,10 +3,10 @@ import swarm.configs.RobotSettings; import swarm.robot.exception.MotionControllerException; - public class MotionController { - // This is the maximum interval allowed to coordinate calculation, smaller values increase the smoothness of the movement + // This is the maximum interval allowed to coordinate calculation, smaller + // values increase the smoothness of the movement static final private int maxInterval = 100; // REM: Obtain this by an experiment @@ -34,19 +34,19 @@ public void move(int leftSpeed, int rightSpeed) { // ----------------------------------- - public void rotateDegree(int speed, float degree){ + public void rotateDegree(int speed, float degree) { // TODO: implement this // degree is relative angle displacement from the current heading - // prepare a mathematical equation to translate speed and degree into + // prepare a mathematical equation to translate speed and degree into // the function 'public void move(int leftSpeed, int rightSpeed, int interval)' } - public void moveDistance(int speed, float distance){ + public void moveDistance(int speed, float distance) { // TODO: implement & test this // distance is relative displacement (in cm) from the current position - int interval = (int) Math.ceil((double)distance/speed); - move(speed,speed, interval); + int interval = (int) Math.ceil((double) distance / speed); + move(speed, speed, interval); } // ----------------------------------- @@ -56,7 +56,6 @@ public void move(int leftSpeed, int rightSpeed, int interval) { int steps = (int) Math.ceil((double) interval / maxInterval); int stepInterval = interval / steps; - int cumulativeInterval = 0; debug("Move using " + steps + " steps, each has " + stepInterval + " intervals"); @@ -71,11 +70,12 @@ public void move(int leftSpeed, int rightSpeed, int interval) { double y = c.getY() + d * Math.sin(h); double heading = (c.getHeadingRad() + (dR - dL) / (RobotSettings.ROBOT_WIDTH)); // in radians -// double dist = Math.sqrt(Math.pow(x - c.getX(), 2) + Math.pow(y - c.getY(), 2)); -// double speed = ((leftSpeed + rightSpeed) / 2.0) * speedFactor; -// int time = (int) (1000 * dist / speed); // time in ms -// System.out.println("dist:" + dist + " time:" + time + " speed:" + speed); -// delay(time); + // double dist = Math.sqrt(Math.pow(x - c.getX(), 2) + Math.pow(y - c.getY(), + // 2)); + // double speed = ((leftSpeed + rightSpeed) / 2.0) * speedFactor; + // int time = (int) (1000 * dist / speed); // time in ms + // System.out.println("dist:" + dist + " time:" + time + " speed:" + speed); + // delay(time); c.setCoordinate(x, y, Math.toDegrees(heading)); @@ -86,7 +86,7 @@ public void move(int leftSpeed, int rightSpeed, int interval) { if (cumulativeInterval >= 2000) { debug("Adding extra delay, " + cumulativeInterval); - //c.print(); + // c.print(); c.publishCoordinate(); delay(cumulativeInterval - 1000); cumulativeInterval -= 1000; @@ -118,18 +118,21 @@ public boolean goToGoal(double targetX, double targetY, int velocity, int interv double phiD = Math.atan2(dy, dx); double w = 0.2 * (phiD - heading); - debug("dx:" + dx + " dy:" + dy + " head:" + Math.toDegrees(heading) + " w:" + Math.toDegrees(w) + " phiD:" + phiD); + debug("dx:" + dx + " dy:" + dy + " head:" + Math.toDegrees(heading) + " w:" + Math.toDegrees(w) + " phiD:" + + phiD); c.setX(x + 5 * Math.cos(w)); c.setY(y + 5 * Math.sin(w)); c.setHeadingRad(heading + w); c.publishCoordinate(); // Publish to visualizer through MQTT - //double vR = (2 * velocity/2 + w * RobotSettings.ROBOT_WIDTH) / 2 * RobotSettings.ROBOT_WHEEL_RADIUS; - //double vL = (2 * velocity/2 - w * RobotSettings.ROBOT_WIDTH) / 2 * RobotSettings.ROBOT_WHEEL_RADIUS; + // double vR = (2 * velocity/2 + w * RobotSettings.ROBOT_WIDTH) / 2 * + // RobotSettings.ROBOT_WHEEL_RADIUS; + // double vL = (2 * velocity/2 - w * RobotSettings.ROBOT_WIDTH) / 2 * + // RobotSettings.ROBOT_WHEEL_RADIUS; - //System.out.println("vL:" + vL + " vR:" + vR + "\n" ); - //move((int) vL, (int) vR, interval); + // System.out.println("vL:" + vL + " vR:" + vR + "\n" ); + // move((int) vL, (int) vR, interval); return (c.getX() == targetX && c.getY() == targetY); } diff --git a/src/main/java/swarm/robot/helpers/RobotMQTT.java b/src/main/java/swarm/robot/helpers/RobotMQTT.java index 5736498..191a286 100644 --- a/src/main/java/swarm/robot/helpers/RobotMQTT.java +++ b/src/main/java/swarm/robot/helpers/RobotMQTT.java @@ -14,7 +14,9 @@ public class RobotMQTT { protected int robotId; protected char reality; - private enum mqttTopic {ROBOT_MSG, ROBOT_MSG_BROADCAST} + private enum mqttTopic { + ROBOT_MSG, ROBOT_MSG_BROADCAST + } private final HashMap topicsSub = new HashMap(); @@ -40,14 +42,15 @@ public void robotCreate(double x, double y, double heading) { } private void subscribe(mqttTopic key, String topic) { - topicsSub.put(key, topic); // Put to the queue - robotMqttClient.subscribe(topic); // Subscribe through MqttHandler + topicsSub.put(key, topic); // Put to the queue + robotMqttClient.subscribe(topic); // Subscribe through MqttHandler } public void handleSubscription(Robot r, MqttMsg m) { String topic = m.topic, msg = m.message; - if (topic.equals(topicsSub.get(mqttTopic.ROBOT_MSG)) || topic.equals(topicsSub.get(mqttTopic.ROBOT_MSG_BROADCAST))) { + if (topic.equals(topicsSub.get(mqttTopic.ROBOT_MSG)) + || topic.equals(topicsSub.get(mqttTopic.ROBOT_MSG_BROADCAST))) { // robot/msg/{robotId} or robot/msg/broadcast // System.out.println("Received: " + topic + "> " + msg); diff --git a/src/main/java/swarm/robot/indicator/AbstractIndicator.java b/src/main/java/swarm/robot/indicator/AbstractIndicator.java index 79968bf..2faf9b6 100644 --- a/src/main/java/swarm/robot/indicator/AbstractIndicator.java +++ b/src/main/java/swarm/robot/indicator/AbstractIndicator.java @@ -20,7 +20,7 @@ private void subscribe(String topic) { // This will subscribe to a given topic through mqttHandler } - // This will handle incoming messages with already subscribed topics + // This will handle incoming messages with already subscribed topics public abstract void handleSubscription(Robot r, MqttMsg m) throws ParseException; } diff --git a/src/main/java/swarm/robot/indicator/NeoPixel.java b/src/main/java/swarm/robot/indicator/NeoPixel.java index 13ce2d9..52ec4c9 100644 --- a/src/main/java/swarm/robot/indicator/NeoPixel.java +++ b/src/main/java/swarm/robot/indicator/NeoPixel.java @@ -1,7 +1,6 @@ package swarm.robot.indicator; import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import swarm.mqtt.RobotMqttClient; import swarm.mqtt.MqttMsg; @@ -13,7 +12,9 @@ public class NeoPixel extends AbstractIndicator { - private enum mqttTopic {NEOPIXEL_IN} + private enum mqttTopic { + NEOPIXEL_IN + } private final HashMap topicsSub = new HashMap(); @@ -25,8 +26,8 @@ public NeoPixel(Robot robot, RobotMqttClient m) { } protected void subscribe(mqttTopic key, String topic) { - topicsSub.put(key, topic); // Put to the queue - robotMqttClient.subscribe(topic); // Subscribe through MqttHandler + topicsSub.put(key, topic); // Put to the queue + robotMqttClient.subscribe(topic); // Subscribe through MqttHandler } @Override @@ -35,7 +36,7 @@ public void handleSubscription(Robot r, MqttMsg m) throws ParseException { if (topic.equals(topicsSub.get(mqttTopic.NEOPIXEL_IN))) { // output/neopixel/{id} - //System.out.println("Received: " + topic + "> " + msg); + // System.out.println("Received: " + topic + "> " + msg); String[] colors = msg.split(" "); R = Integer.parseInt(colors[0]); diff --git a/src/main/java/swarm/robot/sensors/AbstractSensor.java b/src/main/java/swarm/robot/sensors/AbstractSensor.java index ea07af4..efd70a8 100644 --- a/src/main/java/swarm/robot/sensors/AbstractSensor.java +++ b/src/main/java/swarm/robot/sensors/AbstractSensor.java @@ -18,7 +18,7 @@ public AbstractSensor(Robot robot, RobotMqttClient m) { } public void handleSubscription(Robot r, MqttMsg m) { - // This will handle incoming messages with already subscribed topics + // This will handle incoming messages with already subscribed topics } } diff --git a/src/main/java/swarm/robot/sensors/ColorSensor.java b/src/main/java/swarm/robot/sensors/ColorSensor.java index c90daf1..3ec6217 100644 --- a/src/main/java/swarm/robot/sensors/ColorSensor.java +++ b/src/main/java/swarm/robot/sensors/ColorSensor.java @@ -16,7 +16,9 @@ public class ColorSensor extends AbstractSensor { private final static int MQTT_TIMEOUT = 1000; - private enum mqttTopic {COLOR_IN, COLOR_LOOK} + private enum mqttTopic { + COLOR_IN, COLOR_LOOK + } private HashMap topicsSub = new HashMap(); private boolean col_lock = false; @@ -27,7 +29,7 @@ public ColorSensor(Robot robot, RobotMqttClient m) { super(robot, m); try { - color = new RGBColorType(0,0,0); + color = new RGBColorType(0, 0, 0); } catch (RGBColorException e) { e.printStackTrace(); } @@ -36,8 +38,8 @@ public ColorSensor(Robot robot, RobotMqttClient m) { } private void subscribe(mqttTopic key, String topic) { - topicsSub.put(key, topic); // Put to the queue - robotMqttClient.subscribe(topic); // Subscribe through MqttHandler + topicsSub.put(key, topic); // Put to the queue + robotMqttClient.subscribe(topic); // Subscribe through MqttHandler } @Override @@ -51,9 +53,9 @@ public void handleSubscription(Robot robot, MqttMsg m) { color.setColor(msg); col_lock = false; -// } else if (topic.equals(topicsSub.get(mqttTopic.COLOR_LOOK))) { -// // sensor/color/{id}/? -// System.out.println("Received: " + topic + "> " + color.toString()); + // } else if (topic.equals(topicsSub.get(mqttTopic.COLOR_LOOK))) { + // // sensor/color/{id}/? + // System.out.println("Received: " + topic + "> " + color.toString()); } else { System.out.println("Received (unknown): " + topic + "> " + color.toString()); @@ -65,9 +67,9 @@ public RGBColorType getColor() throws Exception { // Prepare the message JSONObject msg = new JSONObject(); msg.put("id", robotId); - msg.put("reality", "M"); // inform the requesting reality + msg.put("reality", "M"); // inform the requesting reality - col_lock = true; // Acquire the color sensor lock + col_lock = true; // Acquire the color sensor lock robotMqttClient.publish("sensor/color", msg.toJSONString()); robot.delay(250); diff --git a/src/main/java/swarm/robot/sensors/DistanceSensor.java b/src/main/java/swarm/robot/sensors/DistanceSensor.java index f04bc48..66d5193 100644 --- a/src/main/java/swarm/robot/sensors/DistanceSensor.java +++ b/src/main/java/swarm/robot/sensors/DistanceSensor.java @@ -12,7 +12,9 @@ public class DistanceSensor extends AbstractSensor { - private enum mqttTopic {DISTANCE_IN, DISTANCE_LOOK} + private enum mqttTopic { + DISTANCE_IN, DISTANCE_LOOK + } private HashMap topicsSub = new HashMap(); @@ -28,8 +30,8 @@ public DistanceSensor(Robot robot, RobotMqttClient m) { } private void subscribe(mqttTopic key, String topic) { - topicsSub.put(key, topic); // Put to the queue - robotMqttClient.subscribe(topic); // Subscribe through MqttHandler + topicsSub.put(key, topic); // Put to the queue + robotMqttClient.subscribe(topic); // Subscribe through MqttHandler } @Override @@ -40,7 +42,7 @@ public void handleSubscription(Robot robot, MqttMsg m) { if (topic.equals(topicsSub.get(mqttTopic.DISTANCE_IN))) { // sensor/distance/{id} - //System.out.println("Input>" + msg); + // System.out.println("Input>" + msg); if (msg.compareTo("Infinity") == 0) { // -1 will be returned as a fail-proof option. Should throw an exception @@ -50,9 +52,9 @@ public void handleSubscription(Robot robot, MqttMsg m) { } dist_lock = false; -// } else if (topic.equals(topicsSub.get(mqttTopic.DISTANCE_LOOK))) { -// // sensor/distance/{id}/? -// System.out.println("Received: " + topic + "> " + msg); + // } else if (topic.equals(topicsSub.get(mqttTopic.DISTANCE_LOOK))) { + // // sensor/distance/{id}/? + // System.out.println("Received: " + topic + "> " + msg); } else { System.out.println("Received (unknown): " + topic + "> " + msg); @@ -63,13 +65,13 @@ public double getDistance() throws Exception { // Prepare the message JSONObject msg = new JSONObject(); msg.put("id", robotId); - msg.put("reality", "M"); // inform the requesting reality + msg.put("reality", "M"); // inform the requesting reality // Start measuring the time long begin = System.currentTimeMillis(); // --------------------------- - dist_lock = true; // Acquire the distance sensor lock + dist_lock = true; // Acquire the distance sensor lock robotMqttClient.publish("sensor/distance", msg.toJSONString()); robot.delay(250); @@ -97,7 +99,7 @@ public double getDistance() throws Exception { JSONObject logMsg = new JSONObject(); logMsg.put("id", robotId); - logMsg.put("msg", (end-begin)); + logMsg.put("msg", (end - begin)); robotMqttClient.publish("robot/log", logMsg.toJSONString()); // --------------------------- diff --git a/src/main/java/swarm/robot/sensors/ProximitySensor.java b/src/main/java/swarm/robot/sensors/ProximitySensor.java index 46382e4..4e05593 100644 --- a/src/main/java/swarm/robot/sensors/ProximitySensor.java +++ b/src/main/java/swarm/robot/sensors/ProximitySensor.java @@ -16,7 +16,9 @@ public class ProximitySensor extends AbstractSensor { private final static int MQTT_TIMEOUT = 1000; - private enum mqttTopic {PROXIMITY_IN} + private enum mqttTopic { + PROXIMITY_IN + } private HashMap topicsSub = new HashMap(); @@ -29,8 +31,8 @@ public ProximitySensor(Robot robot, RobotMqttClient m) { } private void subscribe(mqttTopic key, String topic) { - topicsSub.put(key, topic); // Put to the queue - robotMqttClient.subscribe(topic); // Subscribe through MqttHandler + topicsSub.put(key, topic); // Put to the queue + robotMqttClient.subscribe(topic); // Subscribe through MqttHandler } @Override @@ -41,7 +43,7 @@ public void handleSubscription(Robot robot, MqttMsg m) { if (topic.equals(topicsSub.get(mqttTopic.PROXIMITY_IN))) { // sensor/proximity/{id} - //System.out.println("Input>" + msg); + // System.out.println("Input>" + msg); try { proximity = new ProximityReadingType(msg); } catch (ProximityException e) { @@ -59,9 +61,9 @@ public ProximityReadingType getProximity() throws Exception { // Prepare the message JSONObject msg = new JSONObject(); msg.put("id", robotId); - msg.put("reality", "M"); // inform the requesting reality + msg.put("reality", "M"); // inform the requesting reality - proximity_lock = true; // Acquire the proximity sensor lock + proximity_lock = true; // Acquire the proximity sensor lock robotMqttClient.publish("sensor/proximity", msg.toJSONString()); robot.delay(250); diff --git a/src/main/java/swarm/robot/types/RGBColorType.java b/src/main/java/swarm/robot/types/RGBColorType.java index dfcb05a..9957c8f 100644 --- a/src/main/java/swarm/robot/types/RGBColorType.java +++ b/src/main/java/swarm/robot/types/RGBColorType.java @@ -55,7 +55,7 @@ public int getG() { } public int[] getColor() { - return new int[]{this.R, this.G, this.B}; + return new int[] { this.R, this.G, this.B }; } public String toString() { @@ -67,6 +67,6 @@ public String toStringColor() { } public boolean compareTo(RGBColorType color) { - return (color.getR()==this.R) && (color.getG()==this.G) && (color.getB()==this.B); + return (color.getR() == this.R) && (color.getG() == this.G) && (color.getB() == this.B); } } diff --git a/src/main/java/swarm/robotImplementations/DiscoverColorRobot.java b/src/main/java/swarm/robotImplementations/DiscoverColorRobot.java deleted file mode 100644 index d56ba96..0000000 --- a/src/main/java/swarm/robotImplementations/DiscoverColorRobot.java +++ /dev/null @@ -1,152 +0,0 @@ -package swarm.robotImplementations; - -import swarm.robot.VirtualRobot; -import swarm.robot.types.RGBColorType; - -public class DiscoverColorRobot extends VirtualRobot { - - private int currentHopId; - private boolean colorUpdated; - private final RGBColorType obstacleColor; - private boolean searching; - - public DiscoverColorRobot(int id, double x, double y, double heading, RGBColorType obstacleColor) { - super(id, x, y, heading); - this.obstacleColor = obstacleColor; - } - - public void setup() { - super.setup(); - neoPixel.changeColor(0, 0, 0); - searching = true; - } - - @Override - public void loop() throws Exception { - super.loop(); -// System.out.println(state); - - if (searching && state == robotState.RUN) { - double dist = distSensor.getDistance(); - - if (dist < 15) { - RGBColorType color = colorSensor.getColor(); - System.out.println("\t An obstacle detected, stop"); - System.out.println("observed:" + color.toString() + " must be:" + obstacleColor.toString()); - - if (obstacleColor.compareTo(color)) { - // Color is matching - System.out.println("\t Matching obstacle detected"); - String id = Integer.toString(getId()); - simpleComm.sendMessage("1 " + obstacleColor.toStringColor(), 70); - neoPixel.changeColor(obstacleColor.getR(), obstacleColor.getG(), obstacleColor.getB()); - state = robotState.WAIT; - searching = false; - - } else { - // 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 Not the obstacle we are looking for, go back and rotate " + ((sign > 0) ? "CW" : "CCW")); - - // Go back a little - motion.move(-100, -100, 900); - - // rotate - int loopCount = 0; // to avoid infinity loop - while (distSensor.getDistance() < 35 && loopCount < 5) { - motion.rotate(50 * sign, 1000); - loopCount++; - } - // TODO: This is a temp update to restrict the robot into arena - // if (coordinates.getX() >= 90) coordinates.setX(85); - // if (coordinates.getX() <= -90) coordinates.setX(-85); - // if (coordinates.getY() >= 90) coordinates.setY(85); - // if (coordinates.getY() <= -90) coordinates.setY(-85); - - // rotate a little - motion.rotate(50 * sign, 500); - - } - } else { - motion.move(100, 100, 1000); - delay(1000); - } - } else { - // Task is completed, waiting - delay(150); - } - } - - @Override - public void sensorInterrupt(String sensor, String value) { - - switch (sensor) { - case "distance": - System.out.println("Distance sensor interrupt on " + id); - break; - - case "color": - System.out.println("Color sensor interrupt on " + id); - break; - - case "proximity": - System.out.println("Proximity sensor interrupt on " + id); - break; - - default: - System.out.println("Unknown sensor type"); - } - } - - @Override - public void communicationInterrupt(String msg) { - - // split the message - String[] s = msg.split(" "); - System.out.println("communicationInterrupt on " + id + " with msg:" + msg); - - if (s.length == 4) { - int hopId = Integer.parseInt(s[0]); - - if (hopId > currentHopId) { - int hopR = Integer.parseInt(s[1]); - int hopG = Integer.parseInt(s[2]); - int hopB = Integer.parseInt(s[3]); - neoPixel.changeColor(hopR, hopG, hopB); - currentHopId = hopId; - colorUpdated = true; - - delay(2000); - - // Send it to the next robot - simpleComm.sendMessage((hopId + 1) + " " + hopR + " " + hopG + " " + hopB, 120); - - searching = false; - System.out.println("Send the received message..."); - } - } else { - System.out.println("Invalid msg received"); - } - } - - public void reset() { - neoPixel.changeColor(0, 0, 0); - colorUpdated = false; - currentHopId = -1; - searching = true; - } - - public void start() { - super.start(); - - // Things to do when start action - neoPixel.changeColor(0, 0, 0); - colorUpdated = false; - currentHopId = -1; - searching = true; - } - -} diff --git a/src/resources/config/mqtt.properties b/src/resources/config/mqtt.properties deleted file mode 100644 index 3f646e3..0000000 --- a/src/resources/config/mqtt.properties +++ /dev/null @@ -1,5 +0,0 @@ -server=swarm-gui.tk -port=1883 -username=swarm_user -password=swarm_usere15 -channel=v1 diff --git a/swarm-node.iml b/swarm-node.iml index e41e130..a241fff 100644 --- a/swarm-node.iml +++ b/swarm-node.iml @@ -1,5 +1,6 @@ - + @@ -11,8 +12,10 @@ - - + +