Go to the documentation of this file.00001 package com.github.rosjava.android_remocons.talker;
00002
00003 import android.os.Bundle;
00004 import android.util.Log;
00005 import android.view.Menu;
00006 import android.view.MenuItem;
00007 import android.widget.Toast;
00008
00009 import com.github.rosjava.android_remocons.common_tools.apps.RosAppActivity;
00010
00011 import org.ros.android.MessageCallable;
00012 import org.ros.android.view.RosTextView;
00013 import org.ros.node.ConnectedNode;
00014 import org.ros.node.NodeConfiguration;
00015 import org.ros.node.NodeMainExecutor;
00016
00017 import java.io.IOException;
00018
00019 public class Talker extends RosAppActivity
00020 {
00021 private Toast lastToast;
00022 private ConnectedNode node;
00023 private RosTextView<std_msgs.String> rosTextView;
00024
00025 public Talker()
00026 {
00027 super("Talker", "Talker");
00028 }
00029
00031 @Override
00032 public void onCreate(Bundle savedInstanceState)
00033 {
00034 setDefaultMasterName(getString(R.string.default_robot));
00035 setDashboardResource(R.id.top_bar);
00036 setMainWindowResource(R.layout.main);
00037 super.onCreate(savedInstanceState);
00038 }
00039
00040 @Override
00041 protected void init(NodeMainExecutor nodeMainExecutor) {
00042 String chatterTopic = remaps.get(getString(R.string.chatter_topic));
00043 super.init(nodeMainExecutor);
00044
00045 rosTextView = (RosTextView<std_msgs.String>) findViewById(R.id.text);
00046 rosTextView.setTopicName(getMasterNameSpace().resolve(chatterTopic).toString());
00047 rosTextView.setMessageType(std_msgs.String._TYPE);
00048 rosTextView.setMessageToStringCallable(new MessageCallable<String, std_msgs.String>() {
00049 @Override
00050 public java.lang.String call(std_msgs.String message) {
00051 Log.i("Talker", "received closed loop message [" + message.getData() + "]");
00052 return message.getData();
00053 }
00054 });
00055
00056 try {
00057
00058
00059 Thread.sleep(1000);
00060 java.net.Socket socket = new java.net.Socket(getMasterUri().getHost(), getMasterUri().getPort());
00061 java.net.InetAddress local_network_address = socket.getLocalAddress();
00062 socket.close();
00063 NodeConfiguration nodeConfiguration =
00064 NodeConfiguration.newPublic(local_network_address.getHostAddress(), getMasterUri());
00065 Log.e("Talker", "master uri [" + getMasterUri() + "]");
00066 org.ros.rosjava_tutorial_pubsub.Talker talker = new org.ros.rosjava_tutorial_pubsub.Talker(getMasterNameSpace().resolve(chatterTopic).toString());
00067 nodeMainExecutor.execute(talker, nodeConfiguration);
00068 nodeMainExecutor.execute(rosTextView, nodeConfiguration);
00069 } catch(InterruptedException e) {
00070
00071 Log.e("Talker", "sleep interrupted");
00072 } catch (IOException e) {
00073
00074 Log.e("Talker", "socket error trying to get networking information from the master uri");
00075 }
00076 }
00077 @Override
00078 public boolean onCreateOptionsMenu(Menu menu)
00079 {
00080 menu.add(0,0,0,getString(R.string.stop_app));
00081 return super.onCreateOptionsMenu(menu);
00082 }
00083
00084 @Override
00085 public boolean onOptionsItemSelected(MenuItem item)
00086 {
00087 super.onOptionsItemSelected(item);
00088 switch (item.getItemId()){
00089 case 0:
00090 finish();
00091 break;
00092 }
00093 return true;
00094 }
00095 }
00096
00097
00098