Contents
As with all ROS nodes, rosjava nodes must connect to a roscore master in order to function. However, unlike other client library implementations, rosjava also provides its own roscore implementation. This is to allow ROS to function in a pure Java installation. For example, you can operate a complete ROS graph on a stock (i.e. non-rooted) Android phone.
Nodes are created from NodeFactory objects. Each NodeFactory takes a NodeConfiguration. NodeConfiguration objects specify things like the ROS Master URI, ROS IP address, and remappings. Unlike other implementations rosjava can have multiple nodes within a single virtual machine process. The node exists until it is shutdown or for as long as the Node object remains in scope.
1 import java.net.URI;
2 import java.net.URISyntaxException;
3 import org.ros.address.InetAddressFactory;
4 import org.ros.node.Node;
5 import org.ros.node.NodeConfiguration;
6 import org.ros.node.DefaultNodeFactory;
7
8 ...
9
10 String masterUri;
11
12 ...
13
14 URI uri = new URI(masterUri);
15 String host = InetAddressFactory.newNonLoopback().getHostName();
16 NodeConfiguration nodeConfiguration =
17 NodeConfiguration.newPublic(host, uri);
18
19 String defaultNodeName = "my_node";
20
21 ...
22
23 DefaultNodeFactory factory = new DefaultNodeFactory();
24 Node node = factory.newNode(defaultNodeName, nodeConfiguration);
Let's break that down.
NodeConfiguration
In this snippet, we are creating the configuration that our new node will use. First we specify the URI of the master. Then we specify the node's host (typically specified via ROS_IP or ROS_HOSTNAME environment variables). In this case, we specify the first non-loopback IP address we could find as the host.
Finally, we create the NodeConfiguration and specify that the node should be publicly accessible (i.e. it should not bind to the loopback address).
NodeFactory
Typically, you will use a DefaultNodeFactory to create a new node. In addition to handing in the NodeConfiguration, you also provide a default node name. This node name can be overriden by the NodeConfiguration.
The NodeFactory handles initializing the node with your NodeConfiguration and registering the node with the master. The new node will start immediately.
Shutdown
Shutting down a node also stops and unregisters all of its Publishers and Subscribers.
1 node.shutdown();






