$search
00001 /**************************************************************** 00002 * 00003 * Copyright (c) 2011, 2012 00004 * 00005 * School of Engineering, Cardiff University, UK 00006 * 00007 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00008 * 00009 * Project name: srs EU FP7 (www.srs-project.eu) 00010 * ROS stack name: srs 00011 * ROS package name: srs_knowledge 00012 * Description: 00013 * 00014 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00015 * 00016 * @author Ze Ji, email: jiz1@cf.ac.uk 00017 * 00018 * Date of creation: Oct 2011: 00019 * ToDo: 00020 * 00021 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00022 * 00023 * Redistribution and use in source and binary forms, with or without 00024 * modification, are permitted provided that the following conditions are met: 00025 * 00026 * * Redistributions of source code must retain the above copyright 00027 * notice, this list of conditions and the following disclaimer. 00028 * * Redistributions in binary form must reproduce the above copyright 00029 * notice, this list of conditions and the following disclaimer in the 00030 * documentation and/or other materials provided with the distribution. 00031 * * Neither the name of the school of Engineering, Cardiff University nor 00032 * the names of its contributors may be used to endorse or promote products 00033 * derived from this software without specific prior written permission. 00034 * 00035 * This program is free software: you can redistribute it and/or modify 00036 * it under the terms of the GNU Lesser General Public License LGPL as 00037 * published by the Free Software Foundation, either version 3 of the 00038 * License, or (at your option) any later version. 00039 * 00040 * This program is distributed in the hope that it will be useful, 00041 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00042 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00043 * GNU Lesser General Public License LGPL for more details. 00044 * 00045 * You should have received a copy of the GNU Lesser General Public 00046 * License LGPL along with this program. 00047 * If not, see <http://www.gnu.org/licenses/>. 00048 * 00049 ****************************************************************/ 00050 00051 package org.srs.srs_knowledge.task; 00052 00053 import java.io.*; 00054 import java.util.StringTokenizer; 00055 import java.util.ArrayList; 00056 import java.util.Iterator; 00057 import ros.pkg.srs_knowledge.msg.*; 00058 import ros.pkg.geometry_msgs.msg.Pose2D; 00059 import ros.pkg.geometry_msgs.msg.Pose; 00060 import org.srs.srs_knowledge.knowledge_engine.*; 00061 //import org.srs.srs_knowledge.utils.*; 00062 import com.hp.hpl.jena.rdf.model.*; 00063 import com.hp.hpl.jena.query.QueryExecutionFactory; 00064 import com.hp.hpl.jena.query.ResultSet; 00065 import com.hp.hpl.jena.query.QueryExecution; 00066 import com.hp.hpl.jena.query.QuerySolution; 00067 import com.hp.hpl.jena.ontology.Individual; 00068 import org.srs.srs_knowledge.task.*; 00069 00070 import ros.pkg.srs_symbolic_grounding.srv.*; 00071 import ros.pkg.srs_symbolic_grounding.msg.*; 00072 00073 import ros.*; 00074 import ros.communication.*; 00075 00076 import org.json.simple.JSONArray; 00077 import org.json.simple.JSONObject; 00078 import org.json.simple.JSONValue; 00079 import org.json.simple.parser.ParseException; 00080 import org.json.simple.parser.JSONParser; 00081 00082 import org.srs.srs_knowledge.utils.*; 00083 00084 public class GetObjectTask extends org.srs.srs_knowledge.task.Task 00085 { 00086 /* 00087 public static enum GraspType { 00088 JUST_GRASP, MOVE_AND_GRASP 00089 } 00090 */ 00091 00092 private ConfigInfo.GraspType graspType = ConfigInfo.GraspType.MOVE_AND_GRASP; 00093 00094 public GetObjectTask(String targetContent) 00095 { 00096 // this.init(taskType, targetContent, userPose); 00097 this.initTask(targetContent); 00098 System.out.println("MOVE_AND_GRASP"); 00099 } 00100 00101 public GetObjectTask(String targetContent, ConfigInfo.GraspType graspMode) 00102 { 00103 this.graspType = graspMode; 00104 // this.init(taskType, targetContent, userPose); 00105 if (this.initTask(targetContent)) { 00106 if(graspMode == ConfigInfo.GraspType.MOVE_AND_GRASP) { 00107 System.out.println("MOVE_AND_GRASP " + targetContent); 00108 } 00109 else { 00110 System.out.println("JUST_GRASP " + targetContent); 00111 } 00112 } 00113 else { 00114 System.out.println("Task not constructed successfully"); 00115 } 00116 } 00117 00118 public void setGraspMode(ConfigInfo.GraspType graspMode) { 00119 this.graspType = graspMode; 00120 if(graspMode == ConfigInfo.GraspType.MOVE_AND_GRASP) { 00121 System.out.println("MOVE_AND_GRASP "); 00122 } 00123 else { 00124 System.out.println("JUST_GRASP "); 00125 } 00126 } 00127 00128 @Override 00129 protected boolean constructTask() { 00130 return createGetObjectTask(); 00131 } 00132 00133 private boolean createGetObjectTask() { 00134 /* 00135 // query for tables 00136 // move to tables (near -- use grounding) 00137 // detect milk 00138 // grap milk 00139 // back to user 00140 */ 00141 System.out.println("Create New GET OBJECT Task --- "); 00142 00143 try { 00144 //ArrayList<String> workspaces = OntoQueryUtil.getWorkspaceNamesOfObject(this.targetContent, this.ontoQueryUtil.getObjectNameSpace(), this.ontoQueryUtil.getGlobalNameSpace(), this.ontoDB); 00145 // workspaces = OntoQueryUtil.getWorkspaceOfObject(this.targetContent, OntoQueryUtilthis.ontoQueryUtil.getObjectNameSpace(), this.ontoQueryUtil.getGlobalNameSpace(), KnowledgeEngine.ontoDB); 00146 workspaces = OntoQueryUtil.getWorkspaceOfObject(this.targetContent, OntoQueryUtil.ObjectNameSpace, OntoQueryUtil.GlobalNameSpace, KnowledgeEngine.ontoDB); 00147 } 00148 catch(Exception e) { 00149 System.out.println("--> " + e.getMessage() + "\n" + e.toString()); 00150 return false; 00151 } 00152 00153 for(Individual u : workspaces) { 00154 //System.out.println(u.getLocalName()); 00155 try{ 00156 //System.out.println("Created HLActionSeq "); 00157 HighLevelActionSequence subSeq = createSubSequenceForSingleWorkspace(u); 00158 allSubSeqs.add(subSeq); 00159 } 00160 catch(RosException e) { 00161 System.out.println("ROSEXCEPTION -- when calling symbolic grounding for scanning positions. \t" + e.getMessage() + "\t" + e.toString()); 00162 } 00163 catch(Exception e) { 00164 System.out.println(e.getMessage()); 00165 System.out.println(e.toString()); 00166 } 00167 } 00168 00169 if(allSubSeqs.size() > 0) { 00170 currentSubAction = 0; 00171 return true; 00172 } 00173 else { 00174 currentSubAction = -1; // no sub_highlevel_action in list 00175 return false; 00176 } 00177 } 00178 00179 00180 private HighLevelActionSequence createSubSequenceForSingleWorkspace(Individual workspace) throws RosException, Exception { 00181 HighLevelActionSequence actionList = new HighLevelActionSequence(); 00182 00183 // create MoveAndDetectionActionUnit 00184 //SRSFurnitureGeometry spatialInfo = new SRSFurnitureGeometry(); 00185 ros.pkg.srs_msgs.msg.SRSSpatialInfo spatialInfo = new ros.pkg.srs_msgs.msg.SRSSpatialInfo(); 00186 00187 com.hp.hpl.jena.rdf.model.Statement stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "xCoord", workspace); 00188 00189 spatialInfo.pose.position.x = stm.getFloat(); 00190 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "yCoord", workspace); 00191 spatialInfo.pose.position.y = stm.getFloat(); 00192 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "zCoord", workspace); 00193 spatialInfo.pose.position.z = stm.getFloat(); 00194 00195 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "widthOfObject", workspace); 00196 spatialInfo.w = stm.getFloat(); 00197 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "heightOfObject", workspace); 00198 spatialInfo.h = stm.getFloat(); 00199 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "lengthOfObject", workspace); 00200 spatialInfo.l = stm.getFloat(); 00201 00202 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qu", workspace); 00203 spatialInfo.pose.orientation.w = stm.getFloat(); 00204 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qx", workspace); 00205 spatialInfo.pose.orientation.x = stm.getFloat(); 00206 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qy", workspace); 00207 spatialInfo.pose.orientation.y = stm.getFloat(); 00208 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qz", workspace); 00209 spatialInfo.pose.orientation.z = stm.getFloat(); 00210 00211 // call symbolic grounding service for target pose 00212 ArrayList<Pose2D> posList; 00213 try { 00214 posList = calculateScanPositions(spatialInfo); 00215 System.out.println(posList.size()); 00216 } 00217 catch(RosException e) { 00218 System.out.println(e.toString()); 00219 System.out.println(e.getMessage()); 00220 throw e; 00221 } 00222 00223 Iterator<Individual> itInd = KnowledgeEngine.ontoDB.getInstancesOfClass(OntoQueryUtil.GlobalNameSpace + this.targetContent); 00224 int hhid = -1000; 00225 if(itInd.hasNext()) { 00226 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "houseHoldObjectID", (Individual)itInd.next()); 00227 hhid = OntoQueryUtil.getIntOfStatement(stm); 00228 } 00229 00230 // TODO: 00231 MoveAndDetectionActionUnit mdAction = new MoveAndDetectionActionUnit(posList, targetContent, hhid, workspace.asResource().getLocalName()); 00232 00233 // create MoveAndGraspActionUnit 00234 HighLevelActionUnit mgAction = null; 00235 if(this.graspType == ConfigInfo.GraspType.MOVE_AND_GRASP) { 00236 mgAction = new MoveAndGraspActionUnit(null, targetContent, hhid, "side", workspace.asResource().getLocalName()); 00237 } 00238 else if(this.graspType == ConfigInfo.GraspType.JUST_GRASP) { 00239 mgAction = new JustGraspActionUnit(targetContent, hhid, "side", workspace.asResource().getLocalName()); 00240 } 00241 else { 00242 mgAction = new MoveAndGraspActionUnit(null, targetContent, hhid, "side", workspace.asResource().getLocalName()); 00243 } 00244 00245 // create PutOnTrayActionUnit 00246 PutOnTrayActionUnit trayAction = new PutOnTrayActionUnit("side"); 00247 00248 // FoldArmActionUnit 00249 //FoldArmActionUnit foldArmAction = new FoldArmActionUnit(); 00250 00251 //create BackToUserActionUnit 00252 00253 //Pose2D posUser = OntoQueryUtil.parsePose2D(userPose); 00254 //MoveActionUnit mau = new MoveActionUnit(posUser); 00255 00256 // create FinishActionUnit 00257 FinishActionUnit fau = new FinishActionUnit(true); 00258 //MoveActionUnit mau1 = new MoveActionUnit(OntoQueryUtil.parsePose2D("home")); 00259 //actionList.appendHighLevelAction(mau1); 00260 00261 actionList.appendHighLevelAction(mdAction); 00262 actionList.appendHighLevelAction(mgAction); 00263 actionList.appendHighLevelAction(trayAction); 00264 //actionList.appendHighLevelAction(foldArmAction); 00265 //actionList.appendHighLevelAction(mau); 00266 actionList.appendHighLevelAction(fau); 00267 00268 System.out.println("ActionList size is " + actionList.getSizeOfHighLevelActionList()); 00269 00270 // set parameters for the workspace information 00271 actionList.setParameter("workspaceURI", workspace.asResource().getURI()); 00272 actionList.setParameter("workspace", workspace.asResource().getLocalName()); 00273 actionList.setParameter("workspaceNameSpace", workspace.asResource().getNameSpace()); 00274 00275 return actionList; 00276 } 00277 00278 // TODO: NOT COMPLETED... 00279 @Override 00280 public CUAction getNextCUActionNew(boolean stateLastAction, String jsonFeedback) { 00281 System.out.println("===> Get Next CUACTION -- from GetObjectTask.java"); 00282 CUAction ca = new CUAction(); 00283 if(allSubSeqs.size() == 0 ) { 00284 System.out.println("Sequence size is zero"); 00285 return null; // ??? 00286 } 00287 if(currentSubAction >= 0 && currentSubAction < allSubSeqs.size()) { 00288 // get the current SubActionSequence item 00289 System.out.println("Sequence size is " + allSubSeqs.size()); 00290 HighLevelActionSequence subActSeq = allSubSeqs.get(currentSubAction); 00291 00292 HighLevelActionUnit highAct = subActSeq.getCurrentHighLevelActionUnit(); 00293 // decide if the current SubActionSequence is finished or stuck somewhere? 00294 // if successfully finished, then finished 00295 // if stuck (fail), move to the next subActionSequence 00296 if(highAct != null) { 00297 00298 // TODO: 00299 updateDBObjectPose(); 00300 00301 int ni = highAct.getNextCUActionIndex(stateLastAction); 00302 switch(ni) { 00303 case HighLevelActionUnit.COMPLETED_SUCCESS: 00304 System.out.println("COMPLETED_SUCCESS"); 00305 lastStepActUnit = highAct; 00306 00307 CUAction retact = null; 00308 try { 00309 retact = handleSuccessMessageNew(new ActionFeedback(jsonFeedback)); 00310 } 00311 catch(ParseException pe) { 00312 System.out.println(pe.toString()); 00313 return null; 00314 } 00315 00316 return retact; 00317 case HighLevelActionUnit.COMPLETED_FAIL: 00318 // The whole task finished (failure). 00319 lastStepActUnit = null; 00320 System.out.println("COMPLETED_FAIL"); 00321 return handleFailedMessage(); 00322 case HighLevelActionUnit.INVALID_INDEX: 00323 // The whole task finished failure. Should move to a HighLevelActionUnit in subActSeq of finsihing 00324 lastStepActUnit = null; 00325 System.out.println("INVALID_INDEX"); 00326 //currentSubAction++; 00327 return handleFailedMessage(); 00328 default: 00329 System.out.println(highAct.getActionType()); 00330 if(!highAct.ifParametersSet()) { 00331 System.out.println("Parameters not set"); 00332 lastStepActUnit = null; 00333 return handleFailedMessage(); 00334 } 00335 ca = highAct.getCUActionAt(ni); 00336 // since it is going to use String list to represent action info. So cation type is always assumed to be generic, hence the first item in the list actionInfo should contain the action type information... 00337 // WARNING: No error checking here 00338 //lastActionType = ca.generic.actionInfo.get(0); 00339 lastActionType = (String)(SRSJSONParser.decodeJsonActionInfo(ca.generic.jsonActionInfo).get("action")); 00340 return ca; 00341 } 00342 } 00343 else { 00344 return null; 00345 } 00346 // or if still pending CUAction is available, return CUAction 00347 } 00348 else if (currentSubAction == -1) { 00349 } 00350 00351 return ca; 00352 } 00353 00354 private CUAction handleFailedMessage() { 00355 00356 // 00357 updateTargetOfFailedAct(); 00358 00359 currentSubAction++; 00360 00361 //System.out.println("HANDLE FAILED MESSAGE.... CURRENTSUBACTION IS AT: " + currentSubAction); 00362 00363 if(currentSubAction >= allSubSeqs.size()) { 00364 return null; 00365 } 00366 HighLevelActionSequence nextHLActSeq = allSubSeqs.get(currentSubAction); 00367 00368 // if(nextHLActSeq.hasNextHighLevelActionUnit()) { 00369 HighLevelActionUnit nextHighActUnit = nextHLActSeq.getCurrentHighLevelActionUnit(); 00370 if(nextHighActUnit != null) { 00371 int tempI = nextHighActUnit.getNextCUActionIndex(true); 00372 // TODO: COULD BE DONE RECURSIVELY. BUT TOO COMPLEX UNNECESSARY AND DIFFICULT TO DEBUG. 00373 // SO STUPID CODE HERE 00374 00375 if(tempI == HighLevelActionUnit.COMPLETED_SUCCESS || tempI == HighLevelActionUnit.COMPLETED_FAIL || tempI == HighLevelActionUnit.INVALID_INDEX) { 00376 CUAction ca = new CUAction(); 00377 ca.status = -1; 00378 return ca; 00379 } 00380 else { 00381 //System.out.println("GET NEXT CU ACTION AT: " + tempI); 00382 CUAction ca = nextHighActUnit.getCUActionAt(tempI); 00383 if(ca == null) { 00384 System.out.println("CUACTION IS NULL......."); 00385 } 00386 return ca; 00387 } 00388 } 00389 00390 return null; 00391 } 00392 00396 private void updateTargetOfSucceededActNew(ActionFeedback fb) { 00397 HighLevelActionSequence currentHLActSeq = allSubSeqs.get(currentSubAction); 00398 HighLevelActionUnit currentActUnit = currentHLActSeq.getCurrentHighLevelActionUnit(); 00399 if(currentActUnit.getActionType().equals("MoveAndDetection")) { 00400 //this.recentDetectedObject = ActionFeedback.toPose(fb); 00401 this.recentDetectedObject = fb.getDetectedObjectPose(); 00402 BoundingBoxDim bbDim = InformationRetrieval.retrieveBoundingBoxInfo(OntoQueryUtil.GlobalNameSpace + this.targetContent); 00403 ros.pkg.srs_msgs.msg.SRSSpatialInfo spaObj = new ros.pkg.srs_msgs.msg.SRSSpatialInfo(); 00404 spaObj.l = bbDim.l; 00405 spaObj.h = bbDim.h; 00406 spaObj.w = bbDim.w; 00407 00408 spaObj.pose = this.recentDetectedObject; 00409 00410 // update the knowledge (post-processing) 00411 00412 // if there exists one same object on the same workspace, update it --- simple solution 00413 //String ws = SpatialCalculator.workspaceHolding(spaObj); 00414 00415 String neighbour = SpatialCalculator.nearestObject(spaObj.pose, OntoQueryUtil.GlobalNameSpace + this.targetContent); 00416 if(!neighbour.trim().equals("")) { 00417 //System.out.println("Found neighbour of " + neighbour); 00418 // update its pose 00419 try{ 00420 OntoQueryUtil.updatePoseOfObject(spaObj.pose, OntoQueryUtil.GlobalNameSpace, neighbour.trim()); 00421 OntoQueryUtil.computeOnSpatialRelation(); 00422 } 00423 catch(Exception e) { 00424 System.out.println(e.getMessage()); 00425 } 00426 } 00427 00428 // if there does not exist such an object, then insert a new one 00429 // bounding box can be obtained from HHDB 00430 // TODO 00431 00432 } 00433 else if(currentActUnit.getActionType().equals("MoveAndGrasp")) { 00434 // look for the object at the pose 00435 // update its relationship with the Robot, and remove its pose information 00436 // Individual rob = KnowledgeEngine.ontoDB.getIndividual(OntoQueryUtil.ObjectNameSpace + OntoQueryUtil.RobotName); 00437 String mapNameSpace = OntoQueryUtil.ObjectNameSpace; 00438 String prefix = "PREFIX srs: <http://www.srs-project.eu/ontologies/srs.owl#>\n" 00439 + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" 00440 + "PREFIX mapNamespacePrefix: <" + mapNameSpace + ">\n"; 00441 String queryString = "SELECT DISTINCT ?gripper WHERE { " 00442 + "<" + mapNameSpace + OntoQueryUtil.RobotName + ">" 00443 + " srs:hasPart ?gripper . " 00444 + " ?gripper a srs:RobotGripper . " 00445 + "}"; 00446 00447 try { 00448 String targetObj = SpatialCalculator.nearestObject(this.recentDetectedObject, OntoQueryUtil.GlobalNameSpace + this.targetContent); 00449 //System.out.println("TARGET OBJECT IS ::: " + targetObj); 00450 if(!targetObj.trim().equals("")) { 00451 00452 Pose tmpPose = new Pose(); 00453 tmpPose.position.x = -1000; 00454 tmpPose.position.y = -1000; 00455 tmpPose.position.z = -1000; 00456 tmpPose.orientation.x = -1000; 00457 tmpPose.orientation.y = -1000; 00458 tmpPose.orientation.z = -1000; 00459 tmpPose.orientation.w = -1000; 00460 00461 // update its pose 00462 try{ 00463 OntoQueryUtil.updatePoseOfObject(tmpPose, OntoQueryUtil.GlobalNameSpace, targetObj.trim()); 00464 OntoQueryUtil.computeOnSpatialRelation(); 00465 } 00466 catch(Exception e) { 00467 System.out.println(e.getMessage()); 00468 } 00469 } 00470 00471 00472 // OntoQueryUtil.updatePoseOfObject(tmpPose, OntoQueryUtil.GlobalNameSpace, OntoQueryUtil.ObjectNameSpace, this.targetContent); 00473 ArrayList<QuerySolution> rset = KnowledgeEngine.ontoDB.executeQueryRaw(prefix + queryString); 00474 if(rset.size() == 0) { 00475 //System.out.println("<<<< NO GRIPPER INSTANCE FOUND >>>"); 00476 } 00477 else { 00478 //System.out.println("<<<< FOUND GRIPPER INSTANCE >>>"); 00479 Individual targetInd = KnowledgeEngine.ontoDB.getIndividual(targetObj); 00480 00481 OntoQueryUtil.removeAllSubPropertiesOf(targetObj, OntoQueryUtil.GlobalNameSpace + "spatiallyRelated"); 00482 QuerySolution qs = rset.get(0); 00483 00484 String gripper = qs.get("gripper").toString(); 00485 //System.out.println("<<<<< " + gripper + " >>>>>"); 00486 00487 Individual gripInd = KnowledgeEngine.ontoDB.getIndividual(gripper); 00488 Property proExist = KnowledgeEngine.ontoDB.getProperty(OntoQueryUtil.GlobalNameSpace + "spatiallyRelated"); 00489 00490 Property pro = KnowledgeEngine.ontoDB.getProperty(OntoQueryUtil.GlobalNameSpace + "grippedBy"); 00491 00492 targetInd.setPropertyValue(pro, gripInd); 00493 } 00494 } 00495 catch (Exception e) { 00496 //System.out.println(" ================== " + e.getMessage() + " " + e.toString()); 00497 } 00498 } 00499 else if(currentActUnit.getActionType().equals("PutOnTray")) { 00500 // look for the object at the pose 00501 00502 // update its relationship with the Robot, and remove its pose information 00503 String mapNameSpace = OntoQueryUtil.ObjectNameSpace; 00504 String prefix = "PREFIX srs: <http://www.srs-project.eu/ontologies/srs.owl#>\n" 00505 + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" 00506 + "PREFIX mapNamespacePrefix: <" + mapNameSpace + ">\n"; 00507 String queryString = "SELECT DISTINCT ?tray WHERE { " 00508 + "<" + mapNameSpace + OntoQueryUtil.RobotName + ">" 00509 + " srs:hasPart ?tray . " 00510 + " ?tray a srs:COBTray . " 00511 + "}"; 00512 00513 try { 00514 String targetObj = SpatialCalculator.nearestObject(this.recentDetectedObject, OntoQueryUtil.GlobalNameSpace + this.targetContent); 00515 // System.out.println("TARGET OBJECT IS ::: " + targetObj); 00516 if(!targetObj.trim().equals("")) { 00517 00518 Pose tmpPose = new Pose(); 00519 tmpPose.position.x = -1000; 00520 tmpPose.position.y = -1000; 00521 tmpPose.position.z = -1000; 00522 tmpPose.orientation.x = -1000; 00523 tmpPose.orientation.y = -1000; 00524 tmpPose.orientation.z = -1000; 00525 tmpPose.orientation.w = -1000; 00526 00527 // update its pose 00528 try{ 00529 OntoQueryUtil.updatePoseOfObject(tmpPose, OntoQueryUtil.GlobalNameSpace, targetObj.trim()); 00530 OntoQueryUtil.computeOnSpatialRelation(); 00531 } 00532 catch(Exception e) { 00533 //System.out.println(e.getMessage()); 00534 } 00535 } 00536 00537 00538 // OntoQueryUtil.updatePoseOfObject(tmpPose, OntoQueryUtil.GlobalNameSpace, OntoQueryUtil.ObjectNameSpace, this.targetContent); 00539 ArrayList<QuerySolution> rset = KnowledgeEngine.ontoDB.executeQueryRaw(prefix + queryString); 00540 if(rset.size() == 0) { 00541 //System.out.println("<<<< NO TRAY INSTANCE FOUND >>>"); 00542 } 00543 else { 00544 //System.out.println("<<<< FOUND TRAY INSTANCE >>>"); 00545 Individual targetInd = KnowledgeEngine.ontoDB.getIndividual(targetObj); 00546 00547 OntoQueryUtil.removeAllSubPropertiesOf(targetObj, OntoQueryUtil.GlobalNameSpace + "spatiallyRelated"); 00548 QuerySolution qs = rset.get(0); 00549 00550 String gripper = qs.get("tray").toString(); 00551 //System.out.println("<<<<< " + gripper + " >>>>>"); 00552 00553 Individual gripInd = KnowledgeEngine.ontoDB.getIndividual(gripper); 00554 Property proExist = KnowledgeEngine.ontoDB.getProperty(OntoQueryUtil.GlobalNameSpace + "spatiallyRelated"); 00555 00556 Property pro = KnowledgeEngine.ontoDB.getProperty(OntoQueryUtil.GlobalNameSpace + "aboveOf"); 00557 00558 targetInd.setPropertyValue(pro, gripInd); 00559 } 00560 } 00561 catch (Exception e) { 00562 System.out.println(" ================== " + e.getMessage() + " ---- " + e.toString()); 00563 } 00564 00565 } 00566 } 00567 00568 private void updateTargetOfFailedAct() { 00569 HighLevelActionSequence currentHLActSeq = allSubSeqs.get(currentSubAction); 00570 HighLevelActionUnit currentActUnit = currentHLActSeq.getCurrentHighLevelActionUnit(); 00571 if(currentActUnit.getActionType().equals("MoveAndDetection")) { 00572 //this.recentDetectedObject = ActionFeedback.toPose(fb); 00573 00574 // update the knowledge (post-processing) 00575 String currentWorkSpace = (String)currentHLActSeq.getParameter("workspaceURI"); 00576 String mapNameSpace = OntoQueryUtil.ObjectNameSpace; 00577 String prefix = "PREFIX srs: <http://www.srs-project.eu/ontologies/srs.owl#>\n" 00578 + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" 00579 + "PREFIX mapNamespacePrefix: <" + mapNameSpace + ">\n"; 00580 String queryString = "SELECT DISTINCT ?obj WHERE { \n" 00581 + "?obj srs:spatiallyRelated <" + currentWorkSpace + "> . \n" 00582 + " ?obj a srs:" + this.targetContent + " . \n" 00583 + "}"; 00584 // + " <" + currentWorkSpace + "> a srs:FurniturePiece . \n" 00585 00586 ArrayList<QuerySolution> rset = KnowledgeEngine.ontoDB.executeQueryRaw(prefix + queryString); 00587 if(rset.size() == 0) { 00588 //System.out.println("<<<< NO OBJECT INSTANCE FOUND >>>"); 00589 } 00590 else { 00591 //System.out.println("<<<< FOUND OBJECT INSTANCES ON WORKSPACE " + this.targetContent + " >>>"); 00592 //Individual targetInd = KnowledgeEngine.ontoDB.getIndividual(target); 00593 //Iterator targetIt = KnowledgeEngine.ontoDB.getInstancesOfClass(OntoQueryUtil.GlobalNameSpace + this.targetContent); 00594 for (QuerySolution qs : rset) { 00595 String temp = qs.get("obj").toString(); 00596 //System.out.println("<<<<< " + temp + " >>>>>"); 00597 00598 //System.out.println( temp.getNameSpace() + " " + temp.getLocalName()); 00599 OntoQueryUtil.removeAllSubPropertiesOf(temp, OntoQueryUtil.GlobalNameSpace + "spatiallyRelated"); 00600 Pose tmpPose = new Pose(); 00601 tmpPose.position.x = -1000; 00602 tmpPose.position.y = -1000; 00603 tmpPose.position.z = -1000; 00604 tmpPose.orientation.x = -1000; 00605 tmpPose.orientation.y = -1000; 00606 tmpPose.orientation.z = -1000; 00607 tmpPose.orientation.w = -1000; 00608 00609 // update its pose 00610 try{ 00611 OntoQueryUtil.updatePoseOfObject(tmpPose, OntoQueryUtil.GlobalNameSpace, temp.trim()); 00612 OntoQueryUtil.computeOnSpatialRelation(); 00613 } 00614 catch(Exception e) { 00615 //System.out.println(e.getMessage()); 00616 } 00617 00618 } 00619 } 00620 } 00621 else if(currentActUnit.getActionType().equals("MoveAndGrasp")) { 00622 // do nothing 00623 00624 } 00625 else if(currentActUnit.getActionType().equals("PutOnTray")) { 00626 // do nothing 00627 00628 } 00629 00630 } 00631 00632 private CUAction handleSuccessMessageNew(ActionFeedback fb) { 00633 // TODO: 00634 updateTargetOfSucceededActNew(fb); 00635 00636 HighLevelActionSequence currentHLActSeq = allSubSeqs.get(currentSubAction); 00637 00638 if(currentHLActSeq.hasNextHighLevelActionUnit()) { 00639 HighLevelActionUnit nextHighActUnit = currentHLActSeq.getNextHighLevelActionUnit(); 00640 // set feedback? 00641 if(nextHighActUnit.getActionType().equals("MoveAndGrasp") && !nextHighActUnit.ifParametersSet()) { 00642 00643 Pose2D posBase = calculateGraspPosFromFBNew(fb); 00644 if(posBase == null) { 00645 return handleFailedMessage(); 00646 } 00647 00648 String jsonMove = SRSJSONParser.encodeMoveAction("move", posBase.x, posBase.y, posBase.theta); 00649 if(!nextHighActUnit.setParameters("move", jsonMove, "")) { 00650 //currentSubAction++; 00651 return handleFailedMessage(); 00652 } 00653 00654 } 00655 00656 if(nextHighActUnit != null) { 00657 int tempI = nextHighActUnit.getNextCUActionIndex(true); 00658 // TODO: COULD BE DONE RECURSIVELY. BUT TOO COMPLEX UNNECESSARY AND DIFFICULT TO DEBUG. 00659 // SO STUPID CODE HERE 00660 00661 if(tempI == HighLevelActionUnit.COMPLETED_SUCCESS) { 00662 CUAction ca = new CUAction(); 00663 ca.status = 1; 00664 return ca; 00665 } 00666 else if(tempI == HighLevelActionUnit.COMPLETED_FAIL || tempI == HighLevelActionUnit.INVALID_INDEX) { 00667 CUAction ca = new CUAction(); 00668 ca.status = -1; 00669 return ca; 00670 } 00671 else { 00672 return nextHighActUnit.getCUActionAt(tempI); 00673 } 00674 } 00675 } 00676 return null; 00677 } 00678 00679 private boolean updateDBObjectPose() { 00680 00681 return true; 00682 } 00683 00684 private Pose2D calculateGraspPosFromFBNew(ActionFeedback fb) { 00685 //calculateGraspPosition(SRSFurnitureGeometry furnitureInfo, Pose targetPose) 00686 // call symbol grounding to get parameters for the MoveAndGrasp action 00687 try { 00688 ros.pkg.srs_msgs.msg.SRSSpatialInfo furGeo = getFurnitureGeometryOf(workspaces.get(currentSubAction)); 00689 //ros.pkg.srs_symbolic_grounding.msg.SRSSpatialInfo furGeo = newGetFurnitureGeometryOf(workspaces.get(currentSubAction)); 00690 // TODO: recentDetectedObject should be updated accordingly when the MoveAndDetection action finished successfully 00691 //recentDetectedObject = ActionFeedback.toPose(fb); 00692 recentDetectedObject = fb.getDetectedObjectPose(); 00693 if(recentDetectedObject == null) { 00694 return null; 00695 } 00696 00697 Pose2D pos = calculateGraspPosition(furGeo, recentDetectedObject); 00698 //Pose2D pos = newCalculateGraspPosition(furGeo, recentDetectedObject); 00699 return pos; 00700 } 00701 catch (Exception e) { 00702 System.out.println(e.getMessage() + " ++ " + e.toString()); 00703 return null; 00704 } 00705 00706 } 00707 00708 private ArrayList<Pose2D> calculateScanPositions(ros.pkg.srs_msgs.msg.SRSSpatialInfo furnitureInfo) throws RosException { 00709 ArrayList<Pose2D> posList = new ArrayList<Pose2D>(); 00710 ServiceClient<SymbolGroundingScanBasePose.Request, SymbolGroundingScanBasePose.Response, SymbolGroundingScanBasePose> sc = 00711 KnowledgeEngine.nodeHandle.serviceClient("symbol_grounding_scan_base_pose" , new SymbolGroundingScanBasePose(), false); 00712 00713 SymbolGroundingScanBasePose.Request rq = new SymbolGroundingScanBasePose.Request(); 00714 rq.parent_obj_geometry = furnitureInfo; 00715 00716 SymbolGroundingScanBasePose.Response res = sc.call(rq); 00717 posList = res.scan_base_pose_list; 00718 sc.shutdown(); 00719 return posList; 00720 } 00721 00722 private Pose2D calculateGraspPosition(ros.pkg.srs_msgs.msg.SRSSpatialInfo furnitureInfo, Pose targetPose) throws RosException { 00723 Pose2D pos = new Pose2D(); 00724 00725 ServiceClient<SymbolGroundingGraspBasePoseExperimental.Request, SymbolGroundingGraspBasePoseExperimental.Response, SymbolGroundingGraspBasePoseExperimental> sc = KnowledgeEngine.nodeHandle.serviceClient("symbol_grounding_grasp_base_pose_experimental" , new SymbolGroundingGraspBasePoseExperimental(), false); 00726 00727 SymbolGroundingGraspBasePoseExperimental.Request rq = new SymbolGroundingGraspBasePoseExperimental.Request(); 00728 rq.parent_obj_geometry = furnitureInfo; 00729 rq.target_obj_pose = targetPose; 00730 00731 SymbolGroundingGraspBasePoseExperimental.Response res = sc.call(rq); 00732 boolean obstacleCheck = res.obstacle_check; 00733 double reach = res.reach; 00734 pos = res.grasp_base_pose; 00735 00736 sc.shutdown(); 00737 if(obstacleCheck) { 00738 return null; 00739 } 00740 return pos; 00741 } 00742 00743 private Pose2D newCalculateGraspPosition(ros.pkg.srs_msgs.msg.SRSSpatialInfo furnitureInfo, Pose targetPose) throws RosException { 00744 //Pose2D pos = new Pose2D(); 00745 //ArrayList<Pose2D> poses = new ArrayList<Pose2D>(); 00746 Pose2D pose = new Pose2D(); 00747 ServiceClient<SymbolGroundingGraspBasePose.Request, SymbolGroundingGraspBasePose.Response, SymbolGroundingGraspBasePose> sc = KnowledgeEngine.nodeHandle.serviceClient("symbol_grounding_grasp_base_pose" , new SymbolGroundingGraspBasePose(), false); 00748 00749 SymbolGroundingGraspBasePose.Request rq = new SymbolGroundingGraspBasePose.Request(); 00750 rq.parent_obj_geometry = furnitureInfo; 00751 rq.target_obj_pose = targetPose; 00752 00753 SymbolGroundingGraspBasePose.Response res = sc.call(rq); 00754 boolean obstacleCheck = res.obstacle_check; 00755 double reach = res.reach; 00756 pose = res.grasp_base_pose; 00757 00758 sc.shutdown(); 00759 if(obstacleCheck) { 00760 return null; 00761 } 00762 return pose; 00763 } 00764 00765 private ros.pkg.srs_msgs.msg.SRSSpatialInfo getFurnitureGeometryOf(Individual workspace) { 00766 ros.pkg.srs_msgs.msg.SRSSpatialInfo spatialInfo = new ros.pkg.srs_msgs.msg.SRSSpatialInfo(); 00767 //SRSFurnitureGeometry spatialInfo = new SRSFurnitureGeometry(); 00768 com.hp.hpl.jena.rdf.model.Statement stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "xCoord", workspace); 00769 spatialInfo.pose.position.x = stm.getFloat(); 00770 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "yCoord", workspace); 00771 spatialInfo.pose.position.y = stm.getFloat(); 00772 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "zCoord", workspace); 00773 spatialInfo.pose.position.z = stm.getFloat(); 00774 00775 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "widthOfObject", workspace); 00776 spatialInfo.w = stm.getFloat(); 00777 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "heightOfObject", workspace); 00778 spatialInfo.h = stm.getFloat(); 00779 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "lengthOfObject", workspace); 00780 spatialInfo.l = stm.getFloat(); 00781 00782 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qu", workspace); 00783 spatialInfo.pose.orientation.w = stm.getFloat(); 00784 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qx", workspace); 00785 spatialInfo.pose.orientation.x = stm.getFloat(); 00786 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qy", workspace); 00787 spatialInfo.pose.orientation.y = stm.getFloat(); 00788 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qz", workspace); 00789 spatialInfo.pose.orientation.z = stm.getFloat(); 00790 return spatialInfo; 00791 } 00792 00793 private ros.pkg.srs_msgs.msg.SRSSpatialInfo newGetFurnitureGeometryOf(Individual workspace) { 00794 ros.pkg.srs_msgs.msg.SRSSpatialInfo spatialInfo = new ros.pkg.srs_msgs.msg.SRSSpatialInfo(); 00795 //SRSFurnitureGeometry spatialInfo = new SRSFurnitureGeometry(); 00796 com.hp.hpl.jena.rdf.model.Statement stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "xCoord", workspace); 00797 spatialInfo.pose.position.x = stm.getFloat(); 00798 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "yCoord", workspace); 00799 spatialInfo.pose.position.y = stm.getFloat(); 00800 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "zCoord", workspace); 00801 spatialInfo.pose.position.z = stm.getFloat(); 00802 00803 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "widthOfObject", workspace); 00804 spatialInfo.w = stm.getFloat(); 00805 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "heightOfObject", workspace); 00806 spatialInfo.h = stm.getFloat(); 00807 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "lengthOfObject", workspace); 00808 spatialInfo.l = stm.getFloat(); 00809 00810 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qu", workspace); 00811 spatialInfo.pose.orientation.w = stm.getFloat(); 00812 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qx", workspace); 00813 spatialInfo.pose.orientation.x = stm.getFloat(); 00814 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qy", workspace); 00815 spatialInfo.pose.orientation.y = stm.getFloat(); 00816 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qz", workspace); 00817 spatialInfo.pose.orientation.z = stm.getFloat(); 00818 return spatialInfo; 00819 } 00820 00821 00822 private boolean initTask(String targetContent) { 00823 //acts = new ArrayList<ActionTuple>(); 00824 00825 setTaskTarget(targetContent); 00826 System.out.println("TASK.JAVA: Created CurrentTask " + "get " 00827 + targetContent); 00828 return constructTask(); 00829 } 00830 00831 public boolean replan(OntologyDB onto, OntoQueryUtil ontoQuery) { 00832 return false; 00833 } 00834 00835 public boolean isEmpty() { 00836 try { 00837 if(allSubSeqs.size() == 0) { 00838 return true; 00839 } 00840 } 00841 catch(NullPointerException e) { 00842 System.out.println(e.getMessage() + "\n" + e.toString()); 00843 return true; 00844 } 00845 return false; 00846 } 00847 00848 private ArrayList<Individual> workspaces = new ArrayList<Individual>(); 00849 private int currentSubAction; 00850 private Pose recentDetectedObject; // required by MoveAndGraspActionUnit 00851 private String lastActionType; // used to handle feedback from last action executed 00852 //private String userPose; 00853 private HighLevelActionUnit lastStepActUnit; 00854 }