Rosbridge 2.0 in Java
Hi all, does anyone know a Java library which is compliant with Rosbridge protocol v 2.0?
Thanks,
Ivano
ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org |
Hi all, does anyone know a Java library which is compliant with Rosbridge protocol v 2.0?
Thanks,
Ivano
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.
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?
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.
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!
Just a final question, is it possible to provide services from a Java classs using jrosbridge?
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
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).
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.
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.
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 2014-04-14 06:31:09 -0500
Seen: 3,746 times
Last updated: Apr 14 '14
Sleeping thread after publishing a message into a topic
Using the Java Rosbridge Client API on Android
Does rosjava have explicit support for hot class reloading?
Exceptions not being caught using rosbridge version 1.0
how images are encoded in rosbridge?
how to use rosbridge, gscam to load video
Rosbridge subscribe: Unable to load the manifest for package