Code walkthrough

Let's check out the Android-ROS application code for the basic publisher-subscriber app. You can get it from ~/android_core/android_tutorial_pubsub/src. You'll see a file called MainActiviy.java, and now I'll explain the code.

In the beginning of the code, you can see the package name and required Android modules for this application. The important modules are RosActivity and NodeConfiguration. These will help us create a new ROS node in an Android activity (https://developer.android.com/guide/components/activities.html).

    package org.ros.android.android_tutorial_pubsub; 
 
    import android.os.Bundle; 
    import org.ros.android.MessageCallable; 
    import org.ros.android.RosActivity; 
    import org.ros.android.view.RosTextView; 
    import org.ros.node.NodeConfiguration; 
    import org.ros.node.NodeMainExecutor; 
    import org.ros.rosjava_tutorial_pubsub.Talker; 

Here is where the Android MainActivity starts, which is inherited from RosActivity. It is also creates a Talker object for publishing topics.

    public class MainActivity extends RosActivity { 
 
      private RosTextView<std_msgs.String> rosTextView; 
      private Talker talker; 
 
      public MainActivity() { 
         // The RosActivity constructor configures the notification 
     title and ticker 
        // messages. 
        super("Pubsub Tutorial", "Pubsub Tutorial"); 
      } 

This is one of the important callback functions whenever an activity is initialized. We have to define the essential components of activities inside this function.

In this code, we are creating a rosTextView for ROS_MASTER_URI and also performing topic creation and creating a callback for sending the ROS message through the topic:

      public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        rosTextView = (RosTextView<std_msgs.String>) 
     findViewById(R.id.text); 
         rosTextView.setTopicName("chatter"); 
         rosTextView.setMessageType(std_msgs.String._TYPE); 
         rosTextView.setMessageToStringCallable(new 
     MessageCallable<String, std_msgs.String>() { 
          @Override 
          public String call(std_msgs.String message) { 
             return message.getData(); 
          } 
        }); 
      } 

The following function is inherited from RosActivity, and what it does is when the MainActivity gets initialized, it will run as a thread and query for ROS_MASTER_URI. If it gets the URI, it will start a ROS node itself.

     protected void init(NodeMainExecutor nodeMainExecutor) { 
         talker = new Talker(); 
 
        // At this point, the user has already been prompted to either 
    enter the URI 
        // of a master to use or to start a master locally. 
 
        // The user can easily use the selected ROS Hostname in the 
    master chooser 
        // activity. 
        NodeConfiguration nodeConfiguration = 
    NodeConfiguration.newPublic(getRosHostname()); 
         nodeConfiguration.setMasterUri(getMasterUri()); 
        nodeMainExecutor.execute(talker, nodeConfiguration); 
        // The RosTextView is also a NodeMain that must be executed in 
    order to 
        // start displaying incoming messages. 
        nodeMainExecutor.execute(rosTextView, nodeConfiguration); 
      } 
    } 

You can see more code of ROS-Android applications from the android_core package.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.150.80