Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 package org.ros.android.android_sensors_driver;
00031
00032 import android.location.Location;
00033 import android.location.LocationListener;
00034 import android.location.LocationManager;
00035 import android.location.LocationProvider;
00036 import android.os.Bundle;
00037 import android.os.Looper;
00038
00039 import org.ros.namespace.GraphName;
00040 import org.ros.node.ConnectedNode;
00041 import org.ros.message.Time;
00042 import sensor_msgs.NavSatFix;
00043 import sensor_msgs.NavSatStatus;
00044 import org.ros.node.Node;
00045 import org.ros.node.NodeMain;
00046 import org.ros.node.topic.Publisher;
00047
00052 public class NavSatFixPublisher implements NodeMain {
00053
00054 private NavSatThread navSatThread;
00055 private LocationManager locationManager;
00056 private NavSatListener navSatFixListener;
00057 private Publisher<NavSatFix> publisher;
00058
00059 private class NavSatThread extends Thread {
00060 LocationManager locationManager;
00061 NavSatListener navSatListener;
00062 private Looper threadLooper;
00063
00064 private NavSatThread(LocationManager locationManager, NavSatListener navSatListener){
00065 this.locationManager = locationManager;
00066 this.navSatListener = navSatListener;
00067 }
00068
00069 public void run() {
00070 Looper.prepare();
00071 threadLooper = Looper.myLooper();
00072 this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this.navSatListener);
00073 Looper.loop();
00074 }
00075
00076 public void shutdown(){
00077 this.locationManager.removeUpdates(this.navSatListener);
00078 if(threadLooper != null){
00079 threadLooper.quit();
00080 }
00081 }
00082 }
00083
00084 private class NavSatListener implements LocationListener {
00085
00086 private Publisher<NavSatFix> publisher;
00087
00088 private volatile byte currentStatus;
00089
00090 private NavSatListener(Publisher<NavSatFix> publisher) {
00091 this.publisher = publisher;
00092 this.currentStatus = NavSatStatus.STATUS_FIX;
00093 }
00094
00095
00096 public void onLocationChanged(Location location)
00097 {
00098 NavSatFix fix = this.publisher.newMessage();
00099 fix.getHeader().setStamp(Time.fromMillis(System.currentTimeMillis()));
00100 fix.getHeader().setFrameId("/gps");
00101
00102 fix.getStatus().setStatus(currentStatus);
00103 fix.getStatus().setService(NavSatStatus.SERVICE_GPS);
00104
00105 fix.setLatitude(location.getLatitude());
00106 fix.setLongitude(location.getLongitude());
00107 fix.setAltitude(location.getAltitude());
00108 fix.setPositionCovarianceType(NavSatFix.COVARIANCE_TYPE_APPROXIMATED);
00109 double deviation = location.getAccuracy();
00110 double covariance = deviation*deviation;
00111 double[] tmpCov = {covariance,0,0, 0,covariance,0, 0,0,covariance};
00112 fix.setPositionCovariance(tmpCov);
00113 publisher.publish(fix);
00114 }
00115
00116
00117 public void onProviderDisabled(String provider) {
00118 }
00119
00120
00121 public void onProviderEnabled(String provider) {
00122 }
00123
00124
00125 public void onStatusChanged(String provider, int status, Bundle extras) {
00126 switch (status) {
00127 case LocationProvider.OUT_OF_SERVICE:
00128 currentStatus = NavSatStatus.STATUS_NO_FIX;
00129 break;
00130 case LocationProvider.TEMPORARILY_UNAVAILABLE:
00131 currentStatus = NavSatStatus.STATUS_NO_FIX;
00132 break;
00133 case LocationProvider.AVAILABLE:
00134 currentStatus = NavSatStatus.STATUS_FIX;
00135 break;
00136 }
00137 }
00138 }
00139
00140 public NavSatFixPublisher(LocationManager manager) {
00141 this.locationManager = manager;
00142 }
00143
00144
00145 public void onStart(ConnectedNode node)
00146 {
00147 try
00148 {
00149 this.publisher = node.newPublisher("android/fix", "sensor_msgs/NavSatFix");
00150 this.navSatFixListener = new NavSatListener(publisher);
00151 this.navSatThread = new NavSatThread(this.locationManager, this.navSatFixListener);
00152 this.navSatThread.start();
00153 }
00154 catch (Exception e)
00155 {
00156 if (node != null)
00157 {
00158 node.getLog().fatal(e);
00159 }
00160 else
00161 {
00162 e.printStackTrace();
00163 }
00164 }
00165 }
00166
00167
00168 public void onShutdown(Node arg0) {
00169 this.navSatThread.shutdown();
00170 try {
00171 this.navSatThread.join();
00172 } catch (InterruptedException e) {
00173 e.printStackTrace();
00174 }
00175 }
00176
00177
00178 public void onShutdownComplete(Node arg0) {
00179 }
00180
00181 public GraphName getDefaultNodeName()
00182 {
00183 return GraphName.of("android_sensors_driver/imuPublisher");
00184 }
00185
00186 public void onError(Node node, Throwable throwable)
00187 {
00188 }
00189
00190 }