Ask Your Question
0

android tutorial pubsub does't subscribe from a pc node

asked 2013-08-11 09:33:28 -0500

mrpiccolo gravatar image

updated 2014-01-28 17:17:37 -0500

ngrennan gravatar image

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<t> 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<t> subscriber = connectedNode.newSubscriber(topicName, messageType);
subscriber.addMessageListener(new MessageListener<t>() {
@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) { } }

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-10-17 01:29:00 -0500

grzebyk gravatar image

Check your ROS variables by typing env | grep ROS into terminal. ROS_IP and ROS_MASTER_URI must be set to the actual pc's ip address.

edit flag offensive delete link more
0

answered 2013-09-09 01:12:11 -0500

C.J. gravatar image

I have the same problem ,and I still haven't solved ...

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

Stats

Asked: 2013-08-11 09:33:28 -0500

Seen: 1,553 times

Last updated: Oct 17 '13