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 package org.roboearth.re_ontology;
00019
00020 import java.util.HashMap;
00021 import java.util.Hashtable;
00022 import java.util.Vector;
00023
00024 import jpl.Query;
00025 import ros.*;
00026 import ros.pkg.re_msgs.msg.*;
00027 import ros.pkg.geometry_msgs.msg.*;
00028
00029
00030 public class ReVisionROSClient {
00031
00032 static Boolean rosInitialized = false;
00033 static Ros ros;
00034 static NodeHandle n;
00035
00036 Subscriber.QueueingCallback<ros.pkg.re_msgs.msg.SeenObjectArray> callback;
00037
00038 Thread listenToObjDetections;
00039 Thread updateKnowRobObjDetections;
00040
00041
00042
00048 public ReVisionROSClient(String node_name) {
00049
00050 initRos(node_name);
00051
00052 callback = new Subscriber.QueueingCallback<ros.pkg.re_msgs.msg.SeenObjectArray>();
00053
00054 }
00055
00056
00057
00058 public void startObjDetectionsListener(String re_vision_topic) {
00059
00060
00061
00062 listenToObjDetections = new Thread( new ListenerThread(re_vision_topic) );
00063 listenToObjDetections.start();
00064
00065 updateKnowRobObjDetections = new Thread( new UpdateKnowrobThread() );
00066 updateKnowRobObjDetections.start();
00067
00068 }
00069
00070
00071
00077 protected static void initRos(String node_name) {
00078
00079 ros = Ros.getInstance();
00080
00081 if(!Ros.getInstance().isInitialized()) {
00082 ros.init(node_name);
00083 }
00084 n = ros.createNodeHandle();
00085
00086 }
00087
00088
00096 public class ListenerThread implements Runnable {
00097
00098 String topic;
00099
00100 public ListenerThread() {
00101
00102 this("/re_world_model/filtered_object_locations");
00103 }
00104
00105 public ListenerThread(String t) {
00106 topic=t;
00107 }
00108
00109 @Override public void run() {
00110
00111 try {
00112
00113 Subscriber<ros.pkg.re_msgs.msg.SeenObjectArray> sub =
00114 n.subscribe(topic, new ros.pkg.re_msgs.msg.SeenObjectArray(), callback, 10);
00115
00116 n.spin();
00117 sub.shutdown();
00118
00119 } catch(RosException e) {
00120 e.printStackTrace();
00121 }
00122 }
00123 }
00124
00125
00133 public class UpdateKnowrobThread implements Runnable {
00134
00135 public UpdateKnowrobThread() {
00136
00137 }
00138
00139 @Override public void run() {
00140
00141 try {
00142
00143 ros.pkg.re_msgs.msg.SeenObjectArray res;
00144 HashMap<String, Vector<Object>> solutions;
00145
00146 while (n.isValid()) {
00147
00148 res = callback.pop();
00149
00150
00151 for(SeenObject seen_obj : res.object) {
00152
00153 int obj_id = 1;
00154 String obj_name = seen_obj.name;
00155
00156
00157
00158
00159 solutions = executeQuery("comp_re_vision:re_create_perception_instance(Perception)");
00160 if(solutions.get("Perception").size()>1) {throw new Exception("ERROR: More than one Perception instance created.");}
00161 String perception = solutions.get("Perception").get(0).toString();
00162
00163
00164
00165 solutions = executeQuery("comp_re_vision:re_set_perception_pose("+perception+", "+doubleArrayToPlList(objectPose(seen_obj))+")");
00166
00167
00168
00169 solutions = executeQuery("comp_re_vision:re_create_object_instance(["+obj_name+"], "+obj_id+", Obj)");
00170 if(solutions.get("Obj").size()>1) {throw new Exception("ERROR: More than one Object instance created:"+objectArrayToPlList(solutions.get("Obj").toArray()));}
00171 String obj_inst = solutions.get("Obj").get(0).toString();
00172
00173 synchronized(jpl.Query.class) {
00174
00175 new Query("comp_re_vision:re_set_object_perception("+obj_inst+", "+perception+")").allSolutions();
00176 }
00177 }
00178 n.spinOnce();
00179 }
00180 } catch (Exception e) {
00181 e.printStackTrace();
00182 }
00183 }
00184 }
00185
00192 protected static String objectArrayToPlList(Object[] a) {
00193 String res="[";
00194 for(int i=0;i<a.length;i++) {
00195
00196 if(a[i].toString().startsWith("'"))
00197 res+=a[i].toString();
00198 else
00199 res+="'"+a[i].toString()+"'";
00200
00201 if(i<a.length-1)
00202 res+=",";
00203 }
00204 return res+="]";
00205 }
00206
00207 protected static String doubleArrayToPlList(double[] a) {
00208 String res="[";
00209 for(int i=0;i<a.length;i++) {
00210 res+="'"+a[i]+"'";
00211 if(i<a.length-1)
00212 res+=",";
00213 }
00214 return res+="]";
00215 }
00216
00217
00223 public static double[] objectPose(SeenObject p) {
00224
00225 return quaternionToMatrix(p.pose.position, p.pose.orientation);
00226 }
00227
00235 protected static double[] quaternionToMatrix(Point p, Quaternion q) {
00236
00237 double[] m = new double[16];
00238
00239 double xx = q.x * q.x;
00240 double xy = q.x * q.y;
00241 double xz = q.x * q.z;
00242 double xw = q.x * q.w;
00243
00244 double yy = q.y * q.y;
00245 double yz = q.y * q.z;
00246 double yw = q.y * q.w;
00247
00248 double zz = q.z * q.z;
00249 double zw = q.z * q.w;
00250
00251 m[0] = 1 - 2 * ( yy + zz );
00252 m[1] = 2 * ( xy - zw );
00253 m[2] = 2 * ( xz + yw );
00254 m[3] = p.x;
00255
00256 m[4] = 2 * ( xy + zw );
00257 m[5] = 1 - 2 * ( xx + zz );
00258 m[6] = 2 * ( yz - xw );
00259 m[7] = p.y;
00260
00261 m[8] = 2 * ( xz - yw );
00262 m[9] = 2 * ( yz + xw );
00263 m[10] = 1 - 2 * ( xx + yy );
00264 m[11]=p.z;
00265
00266 m[12]=0;
00267 m[13]=0;
00268 m[14]=0;
00269 m[15]=1;
00270 return m;
00271 }
00272
00273
00280 public static HashMap<String, Vector<Object>> executeQuery(String query) {
00281
00282 HashMap<String, Vector<Object>> result = new HashMap< String, Vector<Object> >();
00283 Hashtable[] solutions;
00284
00285 synchronized(jpl.Query.class) {
00286
00287 Query q = new Query( "expand_goal(("+query+"),_9), call(_9)" );
00288
00289 if(!q.hasSolution())
00290 return new HashMap<String, Vector<Object>>();
00291
00292
00293 solutions = q.allSolutions();
00294 for (Object key: solutions[0].keySet()) {
00295 result.put(key.toString(), new Vector<Object>());
00296 }
00297
00298
00299 for (int i=0; i<solutions.length; i++) {
00300 Hashtable solution = solutions[i];
00301 for (Object key: solution.keySet()) {
00302 String keyStr = key.toString();
00303 if (!result.containsKey( keyStr )) {
00304
00305
00306 Vector<Object> resultVector = new Vector<Object>();
00307 resultVector.add( i, solution.get( key ).toString() );
00308 result.put(keyStr, resultVector);
00309
00310 }
00311
00312 Vector<Object> resultVector = result.get( keyStr );
00313 resultVector.add( i, solution.get( key ).toString() );
00314 }
00315 }
00316 }
00317
00318 return result;
00319 }
00320
00321
00322 }