Ask Your Question
1

Running multiple subscribers in a ROS-android App

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

samiSH gravatar image

Hi,

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.android.MessageCallable;
import org.ros.android.RosActivity;
import org.ros.android.view.RosTextView;
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"); 
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    processesSubscribers();  // for clarity, I put it in a separate method
  }

  public void processesSubscribers(){

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

    // String subscriber   
    rtvString = (RosTextView<std_msgs.String>) findViewById(R.id.stringMessage); // 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>() {
      @Override
      public String call(std_msgs.String message) {
        return "String Listener - message received: [" + message.getData() + "]"; 
      } });
  }

  @Override
  protected void init(NodeMainExecutor nodeMainExecutor) {

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

  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
0

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

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

2 followers

Stats

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

Seen: 625 times

Last updated: Oct 11 '16