Quick Start (jros1client)
CONTENT

Usage

jros1client has a modular nature. It can be used in one of the following ways:

With Java build systems

See starter project as an example for Gradle but any other build system (Maven etc) can be setup similarly.

Installing manually

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:

unzip -d <JROSCLIENT_INSTALL_DIR> jros1client.zip

Inside you will find:

To use jros1client the entire libs folder needs to be added to the classpath/module-path of the application.

Examples

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:

ROS_IP=<IP> roscore ROS_IP=<IP> rostopic ...

Setup

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:

Publishing to the topic

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:

rostopic echo helloRos
data: "Hello ROS"
---
data: "Hello ROS"
---
data: "Hello ROS"
---
data: "Hello ROS"
---
data: "Hello ROS"

Subscribing to the topic

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:

rostopic pub -r 10 helloRos std_msgs/String "hello there"

After restarting our subscriber, it should be showing both messages:

{ "data": "Hello ROS" }
{ "data": "hello there" }

Android examples

jros1droid is an application which demonstrates jros1client usage under Android.

Run

jros1droid apk is included in jros1client release packages. Additionally it can be built from the sources.

Example:


Android emulator

jros1droid can be used inside Android emulator.

Create a new Android VM with the following specs:

avdmanager list avd
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:

emulator -avd vm-android-14

Install jros1droid apk:

adb install id.jros1droid-v<VERSION>-debug.apk

To subscribe to ROS1 topics from Android emulator no additional steps are required.

To publish messages port forwarding needs to be setup.

More examples

More Java examples including those provided above can be found in <JROSCLIENT_INSTALL_DIR>/examples folder or in Git repository.