Running multiple subscribers in a ROS-android App
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