Wiki

(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Create a new ROS Android node

Description: Create a node to publish the device acceleration, similar to Talker.java.

Tutorial Level: BEGINNER

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:

Toggle line numbers
   1 public class AccelerationListener implements SensorEventListener {
   2         @Override
   3         public void onSensorChanged(SensorEvent event) {
   4             //
   5         }
   6         
   7         @Override
   8         public void onAccuracyChanged(Sensor sensor, int accuracy) {
   9             //
  10         }
  11 }

Create a variable to store the current value.

Toggle line numbers
   1 private float[] acceleration = new float[]{0f, 0f, 0f}; 

Listen for accelerometer changes

Toggle line numbers
   1         @Override
   2         public void onSensorChanged(SensorEvent event) {
   3             if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
   4                 acceleration[0] = event.values[0];
   5                 acceleration[1] = event.values[1];
   6                 acceleration[2] = event.values[2];
   7             }
   8         }

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.

Toggle line numbers
   1         private AccelerationListener aListener;
   2 
   3         public AccelerationTalker(Context c) {
   4             aListener = new AccelerationListener(c);
   5         }

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

Toggle line numbers
   1         @Override
   2         public void onStart(final Node node) {
   3              ...
   4             node.executeCancellableLoop(aLoop);
   5             aListener.onResume();
   6         }
   7 
   8         @Override
   9         public void onShutdown(Node node) {
  10             aListener.onPause();
  11         }

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

  1. Make sure the roscore is running on your host machine
  2. Check the URI of the remote machine (echo $ROS_MASTER_URI)
  3. Enter the uri into the ROS launcher activity and press OK.
  4. On the remote machine, check the node has been registered (rostopic list should have /aChatter)
  5. Subscribe to the device accelerometer updates (rostopic echo /aChatter)

Wiki: rosjava/Tutorials/Create a ROS Android Node (last edited 2015-09-28 15:00:18 by ErnestoCorbellini)