Ask Your Question

Running multiple subscribers in a ROS-android App

asked 2016-10-09 03:50:24 -0500

samiSH gravatar image


I am trying to create a ROS-android app that contains two subscribers, each subscribing to two different publishers, publishing on two different topics, and on two different message types. I want to capture that data from each and process it in my android app.

I can create and run each subscriber with no problems. The problem comes when attempting to run both of them at the same time. In which case only one will run, usually the one which was called first. Here is the details:

import android.os.Bundle;
import android.util.Log;

import org.ros.node.NodeConfiguration;
import org.ros.node.NodeMainExecutor;

public class MainActivity extends RosActivity {

  private String chatterTopic = "chatter";
  private String counterTopic = "turtlebotCommands"; 
  private RosTextView<std_msgs.Int8> rtvInt;            // initiating Integer subscriber       
  private RosTextView<std_msgs.String> rtvString;   // initiating String subscriber

  public MainActivity() { 
    super("Multiple Subscribers", "Multiple Subscribers"); 

  public void onCreate(Bundle savedInstanceState) {
    processesSubscribers();  // for clarity, I put it in a separate method

  public void processesSubscribers(){

    // Integer subscriber  
    rtvInt = (RosTextView<std_msgs.Int8>) findViewById(;
    rtvInt.setMessageToStringCallable(new MessageCallable<String, std_msgs.Int8>() {
      public String call(std_msgs.Int8 message) {
            return "Int Listener - message received: [" + String.valueOf(message.getData()) + "]";  
      } });

    // String subscriber   
    rtvString = (RosTextView<std_msgs.String>) findViewById(; // set view
    rtvString.setTopicName(chatterTopic);  // set topic, must match Publisher
    rtvString.setMessageType(std_msgs.String._TYPE); // set messageType, must match Publisher

    rtvString.setMessageToStringCallable(new MessageCallable<String, std_msgs.String>() {
      public String call(std_msgs.String message) {
        return "String Listener - message received: [" + message.getData() + "]"; 
      } });

  protected void init(NodeMainExecutor nodeMainExecutor) {

  NodeConfiguration nodeConfiguration = NodeConfiguration.newPublic(getRosHostname()); 

  nodeMainExecutor.execute(rtvString, nodeConfiguration);     //String Subscriber
  nodeMainExecutor.execute(rtvInt, nodeConfiguration);        //Int Subscriber

I am suspecting the last two lines is the issue .. if i leave them as shown here, only the String subscriber would be called (output shown in the TextView) . But if I change the order and put the int subscriber first, then only it would be called.

So I am guessing this has to do with threading and executing multiple executors simultaneously. My guess is that the second executor never gets called because the first is always running.

So my question now is ... what would I need to do to make both executors run simultaneously .. unfortunately this is an area which i am lacking in .. and would appreciate the help .. thank you

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-10-11 00:13:05 -0500

samiSH gravatar image

OK I already managed to solve it .... the issue comes from the fact that both subscribers (ROSTextView) are calling the same class from source. In that class, the NodeName is defined as "ros_text_view", so if multiple subscribers call it, they will both have the same NodeName, which is not allowed in ROS.

The solution to this comes from this answer

simply by creating two separate NodeConfigurations, and assign a new NodeName for each (or at least one of them) , this way, there will be no Name Conflict

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2016-10-09 03:50:24 -0500

Seen: 750 times

Last updated: Oct 11 '16