Hi
i'm writing an Android app which must publish in a topic and subscriber from an another topic. The app which publish works fine (the listener node receive the correct string), while the app which subscriber does't work if the talker node is executed by the PC, the phone does't dispay anything, (but by the phone works).
What is the issue?
if i write rostopic info /chatter :
Type: std_msgs/String
Publishers:
- /talker (http://nicolo-laptop:38914/)
Subscribers: - /android_gingerbread/ros_text_view (http://192.168.1.5:38617/)
This is the code of MainActivity.java
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.address.InetAddressFactory;
public class MainActivity extends RosActivity {
private RosTextView<std_msgs.String> rosTextView;
public MainActivity() {
super("Pubsub Tutorial", "Pubsub Tutorial");
}
@SuppressWarnings("unchecked")
@Override
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();
}
});
}
@Override
protected void init(NodeMainExecutor nodeMainExecutor) {
NodeConfiguration nodeConfiguration = NodeConfiguration.newPublic(InetAddressFactory.newNonLoopback().getHostAddress());
nodeConfiguration.setMasterUri(getMasterUri());
nodeMainExecutor.execute(rosTextView, nodeConfiguration);
}
}
This is the code of RosTextView.java:
package org.ros.android.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import org.ros.android.MessageCallable;
import org.ros.message.MessageListener;
import org.ros.namespace.GraphName;
import org.ros.node.ConnectedNode;
import org.ros.node.Node;
import org.ros.node.NodeMain;
import org.ros.node.topic.Subscriber;
public class RosTextView extends TextView implements NodeMain {
private String topicName;
private String messageType;
private MessageCallable<String, T> callable;
public RosTextView(Context context) { super(context); }
public RosTextView(Context context, AttributeSet attrs) { super(context, attrs); }
public RosTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
public void setTopicName(String topicName) { this.topicName = topicName; }
public void setMessageType(String messageType) { this.messageType = messageType; }
public void setMessageToStringCallable(MessageCallable<String, T> callable) { this.callable = callable; }
@Override public GraphName getDefaultNodeName() { return GraphName.of("android_gingerbread/ros_text_view"); }
@Override
public void onStart(ConnectedNode connectedNode) {
Subscriber subscriber = connectedNode.newSubscriber(topicName, messageType);
subscriber.addMessageListener(new MessageListener() {
@Override
public void onNewMessage(final T message) {
if (callable != null) {
post(new Runnable() {
@Override
public void run() {
setText(callable.call(message));
}
});
} else {
post(new Runnable() {
@Override
public void run() {
setText(message.toString());
}
});
}
postInvalidate();
}
});
}
@Override public void onShutdown(Node node) { }
@Override public void onShutdownComplete(Node node) { }
@Override public void onError(Node node, Throwable throwable) { } }
Originally posted by mrpiccolo on ROS Answers with karma: 36 on 2013-08-11
Post score: 0