![]() |
Create a new ROS Android node
Description: Create a node to publish the device acceleration, similar to Talker.java.Tutorial Level: BEGINNER
Contents
WARNING: This documentation refers to an outdated version of rosjava and is probably incorrect. Use at your own risk.
Create the AccelerationTalker
Create the AccelerationTalker class
Create class AccelerationTalker in ‘com.newpackage’
Toggle line numbers
1 public class AccelerationTalker {
2
3 }
Implement NodeMain:
Toggle line numbers
1 public class AccelerationTalker implements NodeMain {
2 @Override
3 public GraphName getDefaultNodeName() {
4 //
5 }
6
7 @Override
8 public void onStart(final Node node) {
9 //
10 }
11
12 @Override
13 public void onShutdown(Node node) {
14 //
15 }
16
17 @Override
18 public void onShutdownComplete(Node node) {
19 //
20 }
21 }
Set the GraphName
Set the GraphName to be seen by RosCore:
Toggle line numbers
1 @Override
2 public GraphName getDefaultNodeName() {
3 return new GraphName("AccelerationTalker/aTalker");
4 }
Create the AccelerationListener
Create class AccelerationListener in ‘com.newpackage’ to listen for the acceleration changes and implement SensorEventListener:
Create a variable to store the current value.
Toggle line numbers
1 private float[] acceleration = new float[]{0f, 0f, 0f};
Listen for accelerometer changes
Create a method to access the current value.
Toggle line numbers
1 public float[] getSensorValue() {
2 return acceleration;
3 }
Create methods to register the accelerometer sensor
Store a reference to the context:
Toggle line numbers
1 public AccelerationListener(Context c) {
2 this.context = c;
3 }
Register and unregister:
Toggle line numbers
1 protected void onResume() {
2 SensorManager sm = (SensorManager)
3 context.getSystemService(Context.SENSOR_SERVICE);
4 Sensor accel = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
5 sm.registerListener(this, accel, SensorManager.SENSOR_DELAY_FASTEST);
6 }
7
8 protected void onPause() {
9 SensorManager sm = (SensorManager)
10 context.getSystemService(Context.SENSOR_SERVICE);
11 sm.unregisterListener(this);
12 }
Create an instance of the AccelerationListener in AccelerationTalker.
Update AccelerationTalker to publish the current value.
Toggle line numbers
1 @Override
2 public void onStart(final Node node) {
3 final Publisher<org.ros.message.std_msgs.String> publisher =
4 node.newPublisher("aChatter", "std_msgs/String");
5
6 final CancellableLoop aLoop = new CancellableLoop() {
7 @Override protected void loop() throws InterruptedException {
8 org.ros.message.std_msgs.String str = new
9 org.ros.message.std_msgs.String();
10
11 float[] aValue = aListener.getSensorValue();
12 str.data = String.format("(%f, %f, %f)", aValue[0], aValue[1],
13 aValue[2]);
14 publisher.publish(str);
15 Thread.sleep(1000);
16 }
17 };
18
19 // This CancellableLoop will be cancelled automatically when the Node
20 // shuts down.
21 node.executeCancellableLoop(aLoop);
22 }
Update AccelerationTalker to register and unregister the listener
Create an instance of AccelerationTalker in MainActivity
Create an instance of AccelerationTalker in MainActivity and execute on the node:
Toggle line numbers
1 @Override
2 protected void init(NodeMainExecutor nodeMainExecutor) {
3 aTalker = new AccelerationTalker(this);
4 NodeConfiguration nodeConfiguration = NodeConfiguration.newPublic(
5 InetAddressFactory.newNonLoopback().getHostAddress());
6 nodeConfiguration.setMasterUri(getMasterUri());
7 nodeMainExecutor.execute(aTalker, nodeConfiguration);
8 }
Run the project
- Make sure the roscore is running on your host machine
- Check the URI of the remote machine (echo $ROS_MASTER_URI)
- Enter the uri into the ROS launcher activity and press OK.
- On the remote machine, check the node has been registered (rostopic list should have /aChatter)
- Subscribe to the device accelerometer updates (rostopic echo /aChatter)