rosjava / android_core

Android libraries for rosjava
145 stars 166 forks source link

Unable to get multiple Topic follow along android_core/android_tutorial_pubsub #320

Open longduong11311 opened 4 years ago

longduong11311 commented 4 years ago

I try get 3 Topic based on android_tutorial_pubsub instead of 1 topic is "/chatter". Purpose is get multiple Topic to display monitor telephone. But I have issue this unable to get data at the same time. i only get 1 topic. Help me! My code:

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.ConnectedNode; import org.ros.node.NodeConfiguration; import org.ros.node.NodeMainExecutor; import org.ros.node.topic.Subscriber; import std_msgs.String;

/**

public class MainActivity extends RosActivity { // create private RosTextView rosPreArm; private RosTextView rosBattery; private RosTextView rosMode; protected NodeMainExecutor nodeMainExecutor;

public MainActivity() { // The RosActivity constructor configures the notification title and ticker // messages. super("Pubsub Tutorial", "Pubsub Tutorial"); }

@SuppressWarnings("unchecked") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

rosPreArm = (RosTextView<std_msgs.String>) findViewById(R.id.prearm);
rosPreArm.setTopicName("/hp/StatusPreArm");
rosPreArm.setMessageType(std_msgs.String._TYPE);
rosPreArm.setMessageToStringCallable(new MessageCallable<java.lang.String, String>() {
  @Override
  public java.lang.String call(String message) {
    return message.getData();
  }
});

rosBattery = (RosTextView<std_msgs.String>) findViewById(R.id.battery);
rosBattery.setTopicName("/hp/StatusBattery");
rosBattery.setMessageType(std_msgs.String._TYPE);
rosBattery.setMessageToStringCallable(new MessageCallable<java.lang.String, String>() {
  @Override
  public java.lang.String call(String message) {
    return message.getData();
  }
});

rosMode = (RosTextView<std_msgs.String>) findViewById(R.id.mode);
rosMode.setTopicName("/hp/StatusMode");
rosMode.setMessageType(std_msgs.String._TYPE);
rosMode.setMessageToStringCallable(new MessageCallable<java.lang.String, String>() {
  @Override
  public java.lang.String call(String message) {
    return message.getData();
  }
});

}

@Override protected void init(NodeMainExecutor nodeEx) { nodeMainExecutor = nodeEx;

NodeConfiguration nodeConfiguration = NodeConfiguration.newPublic(getRosHostname());
nodeConfiguration.setMasterUri(getMasterUri());
nodeMainExecutor.execute(rosPreArm, nodeConfiguration);

}

protected void Battery(){ NodeConfiguration nodeConfiguration1 = NodeConfiguration.newPublic(getRosHostname()); nodeConfiguration1.setMasterUri(getMasterUri()); nodeMainExecutor.execute(rosBattery, nodeConfiguration1); }

protected void Mode(){ NodeConfiguration nodeConfiguration2 = NodeConfiguration.newPublic(getRosHostname()); nodeConfiguration2.setMasterUri(getMasterUri()); nodeMainExecutor.execute(rosMode, nodeConfiguration2); } }

And, I have issue is get message like: In file msg have: StatusType.msg (string PreArm, string Battery, string Mode, string Armed) and multiple file like GPSType.msg, AttitudeType.msg, ..., created by me. I want to get message in android instead of get Topic. Help me!

arpit999 commented 3 years ago

You need to define the other two RosTextView in init. As per you code, I can only see that you define one.

add below lines in your init method

nodeMainExecutor.execute(rosPreArm, nodeConfiguration);
nodeMainExecutor.execute(rosBattery , nodeConfiguration);
nodeMainExecutor.execute(rosMode, nodeConfiguration);