ROS /
ConnectingROSToAnExternalProgramBefore we configure accessors, we first need to establish a connection between two entities. This page documents how to setup the connection between an external java program and ROS. You will need to install ROS first.
The code and steps provided follow closely the code provided in java_rosbridge written by James MacGlashan. To do more, compile the docs and explore the API in java_rosbridge.
Set up ros with the following terminal commands: Then enter
Download the java_rosbridge library above from gitHub: Open Eclipse, select File>New Project>Java Project from Existing Antfile.
Select the We will create the following package tests; import ros.Publisher; import ros.RosBridge; import ros.RosListenDelegate; import java.util.HashMap; import java.util.Map; public class Listener { public static void main(String[] args) { if(args.length != 1){ System.out.println("Need the rosbridge websocket URI provided as argument"); System.exit(0); } RosBridge bridge = RosBridge.createConnection(args[0]); bridge.waitForConnection(); bridge.subscribe("/chatter", "std_msgs/String", new RosListenDelegate() { @Override public void receive(Map<String, Object> data, String stringRep) { System.out.println("I received: " + stringRep); } }); } } Eclipse should auto-compile, so supply the WebSocket URI as a command line argument (Right Click on Listener Java, select Run As->Run Configurations, and select the tab Arguments). This is "ws://localhost:9090" by default. Run the file, and you should see the Eclipse console log: "I received: hi there."
package tests; import ros.Publisher; import ros.RosBridge; import ros.RosListenDelegate; import java.util.HashMap; import java.util.Map; public class Talker { public static void main(String[] args) { if(args.length != 1){ System.out.println("Need the rosbridge websocket URI provided as argument"); System.exit(0); } RosBridge bridge = RosBridge.createConnection(args[0]); bridge.waitForConnection(); Publisher pub = new Publisher("/chatter", "std_msgs/String", bridge); final Map<String, String> strData = new HashMap<String, String>(); for(int i = 0; i < 100; i++) { strData.put("data", "hello from java " + i); System.out.println("sending..."); pub.publish(strData); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } }
From a terminal, enter |