Skip to content

Commit

Permalink
Changes of the initial setup (#5)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
NuwanJ authored Aug 2, 2023
1 parent f3a766c commit f13be80
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 23 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ hs_err_pid*

/src/resources/config/mqtt.properties
/.settings/

/target
.idea
.target

/target
settings.xml
.DS_Store
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -39,4 +33,22 @@ channel="v1"

```bash
mvn -s ./settings.xml -B install --file pom.xml
```
```

## 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/)
33 changes: 32 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,35 @@
</dependency>
</dependencies>

</project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
25 changes: 21 additions & 4 deletions src/main/java/Swarm.java → src/main/java/App.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@

import swarm.configs.MQTTSettings;
import swarm.robot.VirtualRobot;
import Robots.*;
import swarm.robot.Robot;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class Swarm {
import Robots.samples.SampleRobot;

public class App {

public static void main(String[] args) {

Expand All @@ -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 !!!");
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/Robots/samples/ObstacleAvoidRobot.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

Expand All @@ -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");

}
}

Expand Down

0 comments on commit f13be80

Please sign in to comment.