$search
00001 /* 00002 * Copyright (C) 2010 by Moritz Tenorth 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 package edu.tum.cs.ias.knowrob.tutorial; 00019 00020 import ros.*; 00021 import ros.pkg.geometry_msgs.msg.Point; 00022 import ros.pkg.geometry_msgs.msg.Quaternion; 00023 import ros.pkg.household_objects_database_msgs.msg.DatabaseModelPose; 00024 import ros.pkg.tabletop_object_detector.msg.*; 00025 import ros.pkg.tabletop_object_detector.srv.*; 00026 00027 00028 public class ROSClientTabletopObjectDetector { 00029 00030 static Boolean rosInitialized = false; 00031 static Ros ros; 00032 static NodeHandle n; 00033 00034 00040 public ROSClientTabletopObjectDetector(String node_name) { 00041 initRos(node_name); 00042 } 00043 00044 00050 protected static void initRos(String node_name) { 00051 00052 ros = Ros.getInstance(); 00053 00054 if(!Ros.getInstance().isInitialized()) { 00055 ros.init(node_name); 00056 } 00057 n = ros.createNodeHandle(); 00058 00059 } 00060 00061 00066 public TabletopDetectionResult callTabletopObjDetection() { 00067 00068 TabletopDetectionResult r=null; 00069 try { 00070 00071 // call the tabletop_object_detector service 00072 TabletopDetection.Request req = new TabletopDetection.Request(); 00073 req.return_clusters=false; 00074 req.return_models=true; 00075 00076 ServiceClient<TabletopDetection.Request, TabletopDetection.Response, TabletopDetection> cl = 00077 n.serviceClient("/object_detection", new TabletopDetection()); 00078 00079 r = cl.call(req).detection; 00080 cl.shutdown(); 00081 00082 } catch (RosException e) { 00083 ros.logError("ROSClient: Call to service /object_detection failed"); 00084 } 00085 00086 return r; 00087 } 00088 00094 public static double[] objectPose(DatabaseModelPose p) { 00095 00096 return quaternionToMatrix(p.pose.pose.position, p.pose.pose.orientation); 00097 } 00098 00106 protected static double[] quaternionToMatrix(Point p, Quaternion q) { 00107 00108 double[] m = new double[16]; 00109 00110 double xx = q.x * q.x; 00111 double xy = q.x * q.y; 00112 double xz = q.x * q.z; 00113 double xw = q.x * q.w; 00114 00115 double yy = q.y * q.y; 00116 double yz = q.y * q.z; 00117 double yw = q.y * q.w; 00118 00119 double zz = q.z * q.z; 00120 double zw = q.z * q.w; 00121 00122 m[0] = 1 - 2 * ( yy + zz ); 00123 m[1] = 2 * ( xy - zw ); 00124 m[2] = 2 * ( xz + yw ); 00125 m[3] = p.x; 00126 00127 m[4] = 2 * ( xy + zw ); 00128 m[5] = 1 - 2 * ( xx + zz ); 00129 m[6] = 2 * ( yz - xw ); 00130 m[7] = p.y; 00131 00132 m[8] = 2 * ( xz - yw ); 00133 m[9] = 2 * ( yz + xw ); 00134 m[10] = 1 - 2 * ( xx + yy ); 00135 m[11]=p.z; 00136 00137 m[12]=0; 00138 m[13]=0; 00139 m[14]=0; 00140 m[15]=1; 00141 return m; 00142 } 00143 00144 00145 00146 /* 00147 * Test method: call the tabletop_object_detector and print results 00148 */ 00149 public static void main(String[] args) { 00150 00151 ROSClientTabletopObjectDetector r = new ROSClientTabletopObjectDetector("knowrob_tabletop_test_123"); 00152 00153 TabletopDetectionResult res = r.callTabletopObjDetection(); 00154 00155 for(int i=0;i<res.models.size();i++) { 00156 for(int j=0;j<res.models.get(i).model_list.size();j++) { 00157 System.out.println(res.models.get(i).model_list.get(j).model_id); 00158 System.out.println("Pos: "+res.models.get(i).model_list.get(j).pose.pose.position.x+","+res.models.get(i).model_list.get(j).pose.pose.position.y+","+res.models.get(i).model_list.get(j).pose.pose.position.z); 00159 } 00160 } 00161 } 00162 00163 }