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
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’
Implement NodeMain:
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:
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.
1 private float[] acceleration = new float[]{0f, 0f, 0f};
Listen for accelerometer changes
Create a method to access the current value.
Create methods to register the accelerometer sensor
Store a reference to the context:
Register and unregister:
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.
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:
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)