jros1client has a modular nature. It can be used in one of the following ways:
See starter project as an example for Gradle but any other build system (Maven etc) can be setup similarly.
Manual installation is useful when you don't want to create project for build systems like Gradle/Maven/... and instead would like to experiment with jros1client from the Java scripts or inside jshell.
It also should be used for trying prerelease versions of jros1client (as they are not published to Maven Central).
Download jros1client package (release version or prerelease version) and unzip it to the folder of your choice:
Inside you will find:
To use jros1client the entire libs folder needs to be added to the classpath/module-path of the application.
Examples below expect ROS to run locally. When running ROS remotely, make sure that ROS master node reports proper address (non localhost) and that it can be accessed from the host where examples run (see Name resolution in ROS1). It is also better to specify master IP address explicitly when starting master node and executing any ROS command:
For simplicity all the examples are given as Java scripts only, without any Gradle/Maven/... projects.
To run these examples all jros1client dependencies need to be present on the classpath.
To create classpath users can either:
Let`s write an application which will create a topic called helloRos and will publish a new message there every second.
Application code PublisherApp.java:
import id.jros1client.JRos1ClientConfiguration; import id.jros1client.JRos1ClientFactory; import id.jrosclient.TopicSubmissionPublisher; import id.jrosmessages.std_msgs.StringMessage; /** Creates a new topic and publishes messages to it. */ public class PublisherApp { public static void main(String[] args) throws Exception { var config = new JRos1ClientConfiguration(); // host address where jrosclient is running and to which other ROS nodes // will communicate config.setHostAddress("localhost"); // specify URL of the master node var client = new JRos1ClientFactory().createClient("http://localhost:11311/", config); String topicName = "helloRos"; var publisher = new TopicSubmissionPublisher<>(StringMessage.class, topicName); // register a new publisher for a new topic with ROS client.publish(publisher); while (true) { publisher.submit(new StringMessage().withData("Hello ROS")); System.out.println("Published"); Thread.sleep(1000); } // usually we need to close client once we are done // but here we keep it open so that subscriber will keep // printing messages indefinitely } }
Now start the script depending on the setup:
Irrespective of the command the output should be:
Published Published Published Published ...
Now in other terminal lets use rostopic command to subscribe to helloRos topic and see the messages:
data: "Hello ROS" --- data: "Hello ROS" --- data: "Hello ROS" --- data: "Hello ROS" --- data: "Hello ROS"
Having publisher running from previous example we can use jros1client to subscribe to helloRos topic to receive all messages from it.
Application code SubscriberApp.java:
import id.jros1client.JRos1ClientConfiguration; import id.jros1client.JRos1ClientFactory; import id.jrosclient.TopicSubscriber; import id.jrosmessages.std_msgs.StringMessage; /** Subscribes to ROS topic */ public class SubscriberApp { public static void main(String[] args) throws Exception { var config = new JRos1ClientConfiguration(); // host address where jrosclient is running and to which other ROS nodes // will communicate config.setHostAddress("localhost"); // specify URL of the master node var client = new JRos1ClientFactory().createClient("http://localhost:11311/"); var topicName = "/helloRos"; // register a new subscriber client.subscribe( new TopicSubscriber<>(StringMessage.class, topicName) { @Override public void onNext(StringMessage item) { System.out.println(item); // request next message getSubscription().get().request(1); } }); // usually we need to close client once we are done // but here we keep it open so that subscriber will keep // printing messages indefinitely } }
Now start the script depending on the setup:
Irrespective of the command the output should be:
{ "data": "Hello ROS" } { "data": "Hello ROS" } { "data": "Hello ROS" }
Now in other terminal lets start additional publisher using rostopic pub command. It will publish to same helloRos topic but it will be different message:
After restarting our subscriber, it should be showing both messages:
{ "data": "Hello ROS" } { "data": "hello there" }
jros1droid is an application which demonstrates jros1client usage under Android.
jros1droid apk is included in jros1client release packages. Additionally it can be built from the sources.
Example:
jros1droid can be used inside Android emulator.
Create a new Android VM with the following specs:
Available Android Virtual Devices: Name: vm-android-14 Target: Default Android System Image Based on: Android API 34 Tag/ABI: default/x86_64 Sdcard: 512 MB
Start:
Install jros1droid apk:
To subscribe to ROS1 topics from Android emulator no additional steps are required.
To publish messages port forwarding needs to be setup.
More Java examples including those provided above can be found in <JROSCLIENT_INSTALL_DIR>/examples folder or in Git repository.