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