Ask Your Question
1

Rosbridge 2.0 in Java

asked 2014-04-14 06:31:09 -0500

Ivano Malavolta gravatar image

updated 2016-10-24 08:36:50 -0500

ngrennan gravatar image

Hi all, does anyone know a Java library which is compliant with Rosbridge protocol v 2.0?

Thanks,

Ivano

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2014-04-14 07:30:45 -0500

rtoris288 gravatar image

Check out https://github.com/WPI-RAIL/jrosbridge/ . Documentation is still a work in progress but the API itself is pretty easy to follow! Tested on Ubuntu and Windows with OpenJDK and Oracle's JDK.

edit flag offensive delete link more
0

answered 2014-04-14 10:35:42 -0500

Ivano Malavolta gravatar image

Great! I am looking at the pom now, and it has a couple of dependencies to some server side stuff like tyrus-server, etc. and now I wondering whether jrosbridge could be used as a simple client running on a standard Java setup (with only the JDK installed), is it possible to do it?

edit flag offensive delete link more

Comments

The server-side dependencies should be a test dependency only (not sure how that got removed!). It is only used to create a mock-server for the JUnit tests. This should run on a "simple Java setup", I am unsure what you mean by only 1 JDK -- you can run with any Java7 setup.

rtoris288 gravatar image rtoris288  ( 2014-04-14 11:24:03 -0500 )edit

Nice, thank you very much, by "simple Java setup" I meant a fresh Ubuntu installation with only the JDK installed, without any other library or application server environment running on it. Thanks again!

Ivano Malavolta gravatar image Ivano Malavolta  ( 2014-04-14 12:38:44 -0500 )edit

Just a final question, is it possible to provide services from a Java classs using jrosbridge?

Ivano Malavolta gravatar image Ivano Malavolta  ( 2014-04-15 00:41:47 -0500 )edit

What do you mean by provide services from a Java class? Do you mean send a service call? If so, an example is in the README. If you mean define your own service, check out https://github.com/WPI-RAIL/jrosbridge/blob/develop/src/main/java/edu/wpi/rail/jrosbridge/services/std/Empty.java as an example

rtoris288 gravatar image rtoris288  ( 2014-04-15 00:52:19 -0500 )edit

Thanks for the link, indeed I am working on a Java class which is able to respond to service calls (coming from other ROS nodes) which adhere to a specific custom service message (.srv file in one of my ROS packages).

Ivano Malavolta gravatar image Ivano Malavolta  ( 2014-04-15 06:25:26 -0500 )edit
-1

answered 2014-04-14 16:31:49 -0500

Mehdi. gravatar image

All you need to do is include any websocket library into your Java project. I use the Tyrus implementation. https://java.net/projects/tyrus

as an example I implemented a small programm that promt for text input and send it to a topic in ros as std_msgs/String

package myPack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;

import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.EncodeException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

import org.json.JSONException;

public class App {

    public Session session;
    public static MyClient myClient = new MyClient();

    protected void start()
             {

            WebSocketContainer container = ContainerProvider.getWebSocketContainer();

            String uri = "ws://localhost:9090";
            System.out.println("Connecting to " + uri);
            try {
                session = container.connectToServer(myClient, URI.create(uri));
            } catch (DeploymentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }            

    }
    public static void main(String args[]) throws JSONException, EncodeException, IOException{
        App client = new App();
        client.start();
        //myClient.subscribe("/face_detection","std_msgs/String");
        //myClient.subscribe("/face_detection","std_msgs/String");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = "";
        try {
            do{
                input = br.readLine();
                if(!input.equals("exit")){
                    myClient.publish("/speech",input);
                }
            }while(!input.equals("exit"));

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

And the class myclient

package myPack;

import java.io.IOException;

import javax.websocket.ClientEndpoint;
import javax.websocket.EncodeException;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import org.json.JSONException;
import org.json.JSONObject;

@ClientEndpoint
public class MyClient {

    Session _session;
    JSONObject _message = new JSONObject();
    JSONObject _messageData = new JSONObject();

    @OnOpen
    public void onOpen(Session session) throws JSONException, IOException, EncodeException {
        _session = session;
        System.out.println("Connected to endpoint: " + session.getBasicRemote());
    }

    @OnMessage
    public void onMessage(String message) throws JSONException, IOException, EncodeException {
        _message = new JSONObject(message);
        System.out.println((_message.getJSONObject("msg")).getString("data"));
        switch((_message.getJSONObject("msg")).getString("data")){
        case "FaceAppeared":
            publish("/speech","Hello");
            break;
        case "FaceDisappeared":
            publish("/speech","See you");
            break;                  
        }               
    }

    @OnError
    public void onError(Throwable t) {
        t.printStackTrace();
    }


    public void subscribe(String topic,String type) throws JSONException, IOException, EncodeException {
        _message = new JSONObject();
        _message.put("op", "subscribe");
        _message.put("topic", topic);
        _message.put("type", type);
        _session.getBasicRemote().sendObject(_message);     
    }
    public void publish(String topic,String messageData) throws JSONException, IOException, EncodeException {
        _message = new JSONObject();
        _messageData = new JSONObject();
        _message.put("op", "publish");
        _messageData.put("linear",messageData);
        _message.put("msg",_messageData);
        _message.put("topic", topic);
        _session.getBasicRemote().sendObject(_message);     
        System.out.println("Send Message "+_message);
    }

}

The method onMessage is always called when a topic that your java programm subscribed for through rosbridge is sending something. You can then check first from what topic it came and what to do (in my case I just checked the content of the message, sent when a face appears or disappears from the webcam field of view)

To publish something from your java programm you just use the publish function that I added to the class.

Of course don't forget to start the rosbridge server first.

edit flag offensive delete link more

Comments

You have basically just posted how to re-implement jrosbridge (https://github.com/WPI-RAIL/jrosbridge) THere is no need to create a client from scratch when a client library exists which does the above as well as much more.

rtoris288 gravatar image rtoris288  ( 2014-04-15 00:50:35 -0500 )edit

Looks interesting, but no need to down vote. Is it possible to send binaries in a straight-forward way with your implementation?

Mehdi. gravatar image Mehdi.  ( 2014-04-16 14:44:30 -0500 )edit

Binary message data? I'm not sure rosbridge itself can handle binary data since it is based on JSON strings.

rtoris288 gravatar image rtoris288  ( 2014-04-17 01:49:25 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-04-14 06:31:09 -0500

Seen: 1,736 times

Last updated: Apr 14 '14