FollowerActivity.java
Go to the documentation of this file.
00001 package com.ros.turtlebot.apps.follower;
00002 
00003 import org.ros.address.InetAddressFactory;
00004 import org.ros.android.BitmapFromCompressedImage;
00005 import org.ros.android.robotapp.RosAppActivity;
00006 import org.ros.android.view.RosImageView;
00007 import org.ros.exception.RemoteException;
00008 import org.ros.exception.ServiceNotFoundException;
00009 import org.ros.namespace.GraphName;
00010 import org.ros.node.AbstractNodeMain;
00011 import org.ros.node.ConnectedNode;
00012 import org.ros.node.NodeConfiguration;
00013 import org.ros.node.NodeMainExecutor;
00014 import org.ros.node.service.ServiceClient;
00015 import org.ros.node.service.ServiceResponseListener;
00016 
00017 import android.os.Bundle;
00018 import android.os.StrictMode;
00019 import android.util.Log;
00020 import android.view.Menu;
00021 import android.view.MenuItem;
00022 import android.view.View;
00023 import android.view.View.OnClickListener;
00024 import android.widget.Button;
00025 import android.widget.ImageButton;
00026 import android.widget.Toast;
00027 
00028 public class FollowerActivity extends RosAppActivity
00029 {
00030   private Toast    lastToast;
00031   private ConnectedNode node;
00032   private RosImageView<sensor_msgs.CompressedImage> cameraView;
00033   private static final String cameraTopic = "/camera/rgb/image_color/compressed_throttle";
00034 
00035 
00036   public FollowerActivity()
00037   {
00038     super("FollowerActivity", "FollowerActivity");
00039   }
00040 
00042   @Override
00043   public void onCreate(Bundle savedInstanceState)
00044   {
00045     setDefaultRobotName(getString(R.string.default_robot));
00046     setDefaultAppName(getString(R.string.default_app));
00047     setDashboardResource(R.id.top_bar);
00048     setMainWindowResource(R.layout.main);
00049 
00050     super.onCreate(savedInstanceState);
00051 
00052     cameraView = (RosImageView<sensor_msgs.CompressedImage>)findViewById(R.id.image);
00053     cameraView.setMessageType(sensor_msgs.CompressedImage._TYPE);
00054     cameraView.setMessageToBitmapCallable(new BitmapFromCompressedImage());
00055 
00056     // Register input controls callbacks
00057     Button backButton = (Button) findViewById(R.id.back_button);
00058     backButton.setOnClickListener(backButtonListener);
00059 
00060     ImageButton startButton = (ImageButton)findViewById(R.id.button_start);
00061     startButton.setOnClickListener(startButtonListener);
00062 
00063     ImageButton stopButton = (ImageButton)findViewById(R.id.button_stop);
00064     stopButton.setOnClickListener(stopButtonListener);
00065 
00066     // TODO Tricky solution to the StrictMode; the recommended way is by using AsyncTask
00067     if (android.os.Build.VERSION.SDK_INT > 9) {
00068       StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
00069       StrictMode.setThreadPolicy(policy);
00070     }
00071   }
00072 
00073 
00074   @Override
00075   protected void init(NodeMainExecutor nodeMainExecutor)
00076   {
00077     super.init(nodeMainExecutor);
00078 
00079     NodeConfiguration nodeConfiguration =
00080       NodeConfiguration.newPublic(InetAddressFactory.newNonLoopback().getHostAddress(), getMasterUri());
00081 
00082     // Execute camera view node
00083     cameraView.setTopicName(getAppNameSpace().resolve(cameraTopic).toString());
00084     nodeMainExecutor.execute(cameraView, nodeConfiguration.setNodeName("android/camera_view"));
00085 
00086     // Execute another node just to allow calling services; I suppose there will be a shortcut for this in the future
00087     nodeMainExecutor.execute(new AbstractNodeMain()
00088     {
00089       @Override
00090       public GraphName getDefaultNodeName()
00091       {
00092         return GraphName.of("android/follower");
00093       }
00094 
00095       @Override
00096       public void onStart(final ConnectedNode connectedNode)
00097       {
00098         node = connectedNode;
00099       }
00100     }, nodeConfiguration.setNodeName("android/follower"));
00101   }
00102 
00103   @Override
00104   public boolean onCreateOptionsMenu(Menu menu)
00105   {
00106     menu.add(0,0,0,R.string.stop_app);
00107     return super.onCreateOptionsMenu(menu);
00108   }
00109 
00110   @Override
00111   public boolean onOptionsItemSelected(MenuItem item)
00112   {
00113     super.onOptionsItemSelected(item);
00114     switch (item.getItemId()){
00115     case 0:
00116         onDestroy();
00117         break;
00118     }
00119     return true;
00120   }
00121 
00122   private void callService(byte newState)
00123   {
00124     if (node == null)
00125     {
00126       Log.e("FollowerActivity", "Still doesn't have a connected node");
00127       return;
00128     }
00129 
00130     ServiceClient<turtlebot_follower.ChangeStateRequest, turtlebot_follower.ChangeStateResponse> serviceClient;
00131     try
00132     {
00133       serviceClient = node.newServiceClient("/turtlebot_follower/change_state", turtlebot_follower.ChangeState._TYPE);
00134     }
00135     catch (ServiceNotFoundException e)
00136     {
00137       Log.e("FollowerActivity", "Service not found: " + e.getMessage());
00138       Toast.makeText(getBaseContext(), "Change follower state service not found", Toast.LENGTH_LONG).show();
00139       return;
00140     }
00141     final turtlebot_follower.ChangeStateRequest request = serviceClient.newMessage();
00142     request.setState(newState);
00143 
00144     serviceClient.call(request, new ServiceResponseListener<turtlebot_follower.ChangeStateResponse>() {
00145       @Override
00146       public void onSuccess(turtlebot_follower.ChangeStateResponse response) {
00147         Log.i("FollowerActivity", "Service result " + response.getResult());
00148         if (request.getState() == turtlebot_follower.ChangeStateRequest.STOPPED)
00149           showToast("Follower stopped");
00150         else
00151           showToast("Follower started");
00152       }
00153 
00154       @Override
00155       public void onFailure(RemoteException e) {
00156         Log.e("FollowerActivity", "Service result: failure (" + e.getMessage() + ")");
00157         showToast("Change follower state failed");
00158       }
00159     });
00160   }
00161 
00166   private void showToast(final String message)
00167   {
00168     runOnUiThread(new Runnable()
00169     {
00170       @Override
00171       public void run() {
00172         if (lastToast != null)
00173           lastToast.cancel();
00174 
00175         lastToast = Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG);
00176         lastToast.show();
00177       }
00178     });
00179   }
00180 
00181   private final OnClickListener backButtonListener = new OnClickListener()
00182   {
00183     @Override
00184     public void onClick(View v)
00185     {
00186       onBackPressed();
00187     }
00188   };
00189 
00190   private final OnClickListener startButtonListener = new OnClickListener()
00191   {
00192     @Override
00193     public void onClick(View v)
00194     {
00195       callService(turtlebot_follower.ChangeStateRequest.FOLLOW);
00196     }
00197   };
00198 
00199   private final OnClickListener stopButtonListener = new OnClickListener()
00200   {
00201     @Override
00202     public void onClick(View v)
00203     {
00204       callService(turtlebot_follower.ChangeStateRequest.STOPPED);
00205     }
00206   };
00207 }


turtlebot_android_follower
Author(s): Jorge Santos
autogenerated on Mon Oct 6 2014 07:58:47