From f13be80e06e498264a9071080c1d85f6aace7685 Mon Sep 17 00:00:00 2001 From: Nuwan Jaliyagoda Date: Wed, 2 Aug 2023 18:25:19 +0530 Subject: [PATCH] Changes of the initial setup (#5) * Obstacle Avoiding Robot Example * Entry point changes * Add instructions for cmd use * Add launch file for easy debugging with VS Code * Sample Robots and Runtime configurations * Readme updates --- .gitignore | 5 +- .vscode/launch.json | 15 +++++ README.md | 32 ++++++---- pom.xml | 33 ++++++++++- src/main/java/{Swarm.java => App.java} | 25 ++++++-- .../Robots/samples/ObstacleAvoidRobot.java | 59 +++++++++++++++++++ .../SampleRobot.java} | 16 +++-- 7 files changed, 162 insertions(+), 23 deletions(-) create mode 100644 .vscode/launch.json rename src/main/java/{Swarm.java => App.java} (66%) create mode 100644 src/main/java/Robots/samples/ObstacleAvoidRobot.java rename src/main/java/Robots/{MyTestRobot.java => samples/SampleRobot.java} (56%) diff --git a/.gitignore b/.gitignore index b2b3354..ba42a53 100644 --- a/.gitignore +++ b/.gitignore @@ -24,9 +24,8 @@ hs_err_pid* /src/resources/config/mqtt.properties /.settings/ - +/target .idea .target - -/target settings.xml +.DS_Store diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..1e55839 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "App", + "request": "launch", + "mainClass": "App", + "projectName": "java-robot" + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 3fab9b2..a346bad 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,12 @@ -[![Java CI with Maven](https://github.com/Pera-Swarm/java-robot/actions/workflows/java-ci.yml/badge.svg)](https://github.com/Pera-Swarm/java-robot/actions/workflows/java-ci.yml) +[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0) [![Java CI with Maven](https://github.com/Pera-Swarm/java-robot/actions/workflows/java-ci.yml/badge.svg)](https://github.com/Pera-Swarm/java-robot/actions/workflows/java-ci.yml) # Java Robot -This is a boilerplate template for java virtual robotst, *Pera-Swarm* - -More details will be updated soon. - ---- +This is a boilerplate template for java virtual robots, under *Pera-Swarm* ## 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. @@ -39,4 +33,22 @@ channel="v1" ```bash mvn -s ./settings.xml -B install --file pom.xml -``` \ No newline at end of file +``` + +## Run with commandline + +- Compile the packages and assembly with all the dependencies, using `mvn compile assembly:single` + +```bash +mvn clean compile assembly:single -s settings.xml +``` + +- Run the code implementation using `java -jar [jar_file_path] ` + +```bash +java -jar target/java-robot-node-1.0-SNAPSHOT-jar-with-dependencies.jar +``` + +## Detailed Guide + +- [pera-swarm.ce.pdn.ac.lk/docs/robots/virtual/v1/java/](https://pera-swarm.ce.pdn.ac.lk/docs/robots/virtual/v1/java/) \ No newline at end of file diff --git a/pom.xml b/pom.xml index 1f019e5..c656807 100644 --- a/pom.xml +++ b/pom.xml @@ -29,4 +29,35 @@ - \ No newline at end of file + + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.0 + + + + true + App + + + + + + + maven-assembly-plugin + + + + App + + + + jar-with-dependencies + + + + + + diff --git a/src/main/java/Swarm.java b/src/main/java/App.java similarity index 66% rename from src/main/java/Swarm.java rename to src/main/java/App.java index 9518ae8..90ba390 100644 --- a/src/main/java/Swarm.java +++ b/src/main/java/App.java @@ -1,7 +1,6 @@ import swarm.configs.MQTTSettings; -import swarm.robot.VirtualRobot; -import Robots.*; +import swarm.robot.Robot; import java.io.File; import java.io.FileNotFoundException; @@ -9,7 +8,9 @@ import java.io.IOException; import java.util.Properties; -public class Swarm { +import Robots.samples.SampleRobot; + +public class App { public static void main(String[] args) { @@ -31,9 +32,25 @@ public static void main(String[] args) { MQTTSettings.channel = props.getProperty("channel", "v1"); reader.close(); - VirtualRobot robot = new MyTestRobot(10, -52, 32, 45); + // Start a single robot + Robot robot = new SampleRobot(10, 0, 0, 90); new Thread(robot).start(); + // // Start a swarm of robots + // int[] robotList = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + + // int startX = 0; + // int startY = 0; + // int startHeading = 90; + + // Robot[] vr = new VirtualRobot[robotList.length]; + + // for (int i = 0; i < robotList.length; i++) { + // vr[i] = new SampleRobot(robotList[i], startX + 40 * i, startY + 50 * i, + // startHeading + 10 * i); + // new Thread(vr[i]).start(); + // } + } catch (FileNotFoundException ex) { // file does not exist System.out.println("File Not Found !!!"); diff --git a/src/main/java/Robots/samples/ObstacleAvoidRobot.java b/src/main/java/Robots/samples/ObstacleAvoidRobot.java new file mode 100644 index 0000000..7621f26 --- /dev/null +++ b/src/main/java/Robots/samples/ObstacleAvoidRobot.java @@ -0,0 +1,59 @@ +// This robot will move freely, avoiding obstacles +// Written by Nuwan Jaliyagoda + +package Robots.samples; + +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); + + } else { + motion.move(defaultMoveSpeed, defaultMoveSpeed, 1000); + } + } + } +} diff --git a/src/main/java/Robots/MyTestRobot.java b/src/main/java/Robots/samples/SampleRobot.java similarity index 56% rename from src/main/java/Robots/MyTestRobot.java rename to src/main/java/Robots/samples/SampleRobot.java index b273efa..adbb9ef 100644 --- a/src/main/java/Robots/MyTestRobot.java +++ b/src/main/java/Robots/samples/SampleRobot.java @@ -1,10 +1,10 @@ -package Robots; +package Robots.samples; import swarm.robot.VirtualRobot; -public class MyTestRobot extends VirtualRobot { +public class SampleRobot extends VirtualRobot { - public MyTestRobot(int id, double x, double y, double heading) { + public SampleRobot(int id, double x, double y, double heading) { super(id, x, y, heading); } @@ -17,8 +17,14 @@ public void loop() throws Exception { super.loop(); if (state == robotState.RUN) { - System.out.println("Test"); - delay(1000); + System.out.println("Run"); + + } else if (state == robotState.WAIT) { + System.out.println("Waiting"); + + } else if (state == robotState.BEGIN) { + System.out.println("Begin"); + } }