Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

Grasper.cc

Go to the documentation of this file.
00001 #include "Shared/RobotInfo.h"
00002 #if (defined(TGT_HAS_ARMS) || !defined(STRICT_TGT_MODEL)) && !defined(TGT_IS_AIBO)
00003 
00004 #include <Crew/Grasper.h>
00005 #include <DualCoding/VRmixin.h>
00006 #include <DualCoding/ShapeCylinder.h>
00007 #include <Motion/IKSolver.h>
00008 
00009 #include <fstream>
00010 
00011 using namespace DualCoding;
00012 
00013 //**** These constants are for the Chiara chess gripper.
00014 float openLeftGripperVal = -0.10f;
00015 float closedLeftGripperVal = -0.25f;
00016 float openRightGripperVal = 0.07f;
00017 float closedRightGripperVal = 0.25f;
00018 
00019 MotionManager::MC_ID motionNodesWalkMC = MotionManager::invalid_MC_ID;
00020 
00021 GrasperRequest* Grasper::curReq = NULL;
00022 Grasper::GrasperVerbosity_t Grasper::verbosity = -1U;
00023 
00024 void Grasper::ForwardRequestType::doStart() {
00025   curReq->moveConstrainedPath.clear();
00026   curReq->moveFreePath.clear();
00027   curReq->disengagePath.clear();
00028   postStateSignal<GrasperRequest::GrasperRequestType_t>(curReq->requestType);
00029 }
00030 
00031 void Grasper::PathPlanUnconstrained::doStart() {
00032   std::vector<NodeValue_t> endStates;
00033   
00034   fmat::Column<3> offset = fmat::pack(0.0f, 0.0f, 0.0f);
00035   
00036   Point toPt = curReq->object->getCentroid();
00037   VRmixin::grasper->computeGoalStates(toPt, curReq->gripperAngleRangesX, curReq->gripperAngleRangesY, curReq->gripperAngleRangesZ, curReq->angleResolution, endStates, offset);
00038   if (endStates.empty()) {
00039     std::cout << "PLAN - pickUpUnreachable" << std::endl;
00040     postStateSignal<GraspError>(GrasperRequest::pickUpUnreachable);
00041     return;
00042   }
00043   
00044   NodeValue_t startSt;
00045   Grasper::getCurrentState(startSt);
00046   
00047   VRmixin::grasper->appendObjectToObstacles();
00048   
00049   GET_SHAPE(worldBounds, PolygonData, VRmixin::worldShS);
00050   Grasper::Planner planner(VRmixin::worldShS, worldBounds, curReq->rrtInflation, curReq->effectorOffset);
00051   
00052   std::vector<NodeType_t> *treeStartResult = curReq->displayTree ? new std::vector<NodeType_t> : NULL;
00053   std::vector<NodeType_t> *treeEndResult = curReq->displayTree ? new std::vector<NodeType_t> : NULL;
00054   fmat::Transform t;
00055   DualCoding::Point c = VRmixin::theAgent->getCentroid();
00056   t.translation()[0] = c.coordX();
00057   t.translation()[1] = c.coordY();
00058   t.rotation() = fmat::Matrix<3,3>(fmat::rotation2D(VRmixin::theAgent->getOrientation()));
00059   Grasper::Planner::PlannerResult result =
00060   planner.planPath(startSt, endStates.front(), curReq->rrtInterpolationStep,
00061                    t, curReq->rrtMaxIterations,
00062                    &(curReq->moveFreePath), treeStartResult, treeEndResult);
00063   VRmixin::grasper->removeTargetFromObstacles();
00064   
00065   switch ( result.code ) {
00066     case GenericRRTBase::SUCCESS:
00067       std::cout << "Plan Unconstrained succeeded" << std::endl;
00068       break;
00069     case GenericRRTBase::START_COLLIDES:
00070       std::cout << "Navigation path planning failed: start state is in collision.\n";
00071       break;
00072     case GenericRRTBase::END_COLLIDES:
00073       std::cout << "Navigation path planning failed: end state is in collision.\n";
00074       break;
00075     case GenericRRTBase::MAX_ITER:
00076       std::cout << "Navigation path planning failed: too many iterations.\n";
00077       break;
00078   }
00079   
00080   if (result.code == GenericRRTBase::START_COLLIDES || result.code == GenericRRTBase::END_COLLIDES) {
00081     std::cout << "Obstacle: " << std::endl << "=================" << std::endl << result.movingObstacle->toString() << std::endl;
00082     std::cout << "collided with: " <<std::endl << "=================" << std::endl << result.collidingObstacle->toString() << std::endl;
00083     std::cout << "=================" << std::endl;
00084   }
00085   
00086   // Display the tree if requested
00087   if ( curReq->displayTree && result.code != GenericRRTBase::START_COLLIDES && GenericRRTBase::END_COLLIDES ) {
00088     NEW_SHAPE(plannerTrees, GraphicsData, new GraphicsData(VRmixin::worldShS));
00089     planner.plotTree(*treeStartResult, plannerTrees, rgb(0,0,0));
00090     planner.plotTree(*treeEndResult, plannerTrees, rgb(0,192,0));
00091     delete treeStartResult;
00092     delete treeEndResult;
00093   }
00094   
00095   // Display the path if requested
00096   if ( curReq->displayPath && result.code == GenericRRTBase::SUCCESS ) {
00097     NEW_SHAPE(plannedPath, GraphicsData, new GraphicsData(VRmixin::worldShS));
00098     planner.plotPath(curReq->moveFreePath, plannedPath, rgb(0,0,255));
00099   } 
00100   
00101   if (result.code == GenericRRTBase::SUCCESS)
00102     postStateCompletion();
00103   else {
00104     std::cout << "PLANNER FAILURE - UNCONSTRAINED" << std::endl;
00105     postStateSignal<GraspError>(GrasperRequest::noGraspPath);
00106   }
00107 }
00108 
00109 void Grasper::PathPlanConstrained::doStart() {
00110   std::vector<NodeValue_t> endStates;
00111   
00112   fmat::Column<3> offset = fmat::pack(0.0f, 0.0f, 0.0f);
00113   
00114   if (!curReq->targetLocation.isValid()) {
00115     postStateSignal<GraspError>(GrasperRequest::someError);
00116     return;
00117   }
00118   
00119   Point toPt = curReq->object->getCentroid();
00120   VRmixin::grasper->computeGoalStates(toPt, curReq->gripperAngleRangesX, curReq->gripperAngleRangesY, curReq->gripperAngleRangesZ,
00121                                       curReq->angleResolution, endStates, offset);
00122   if (endStates.empty()) {
00123     std::cout << "PLAN - pickUpUnreachable" << std::endl;
00124     postStateSignal<GraspError>(GrasperRequest::pickUpUnreachable);
00125     return;
00126   }
00127   
00128   NodeValue_t startSt;
00129   if (curReq->moveFreePath.empty())
00130     Grasper::getCurrentState(startSt);
00131   else
00132     startSt = curReq->moveFreePath.back();
00133   
00134   if (curReq->verbosity)
00135     std::cout << "Planning on "<< curReq->plannerObstacles.size() << " obstacles" << std::endl;
00136   
00137   GET_SHAPE(worldBounds, PolygonData, VRmixin::worldShS);
00138   Planner planner(VRmixin::worldShS, worldBounds, curReq->rrtInflation,
00139                                              curReq->effectorOffset, curReq->predicate);
00140   
00141   std::vector<NodeType_t> *treeStartResult = NULL;
00142   std::vector<NodeType_t> *treeEndResult = NULL;
00143   fmat::Transform t;
00144   DualCoding::Point c = VRmixin::theAgent->getCentroid();
00145   t.translation()[0] = c.coordX();
00146   t.translation()[1] = c.coordY();
00147   t.rotation() = fmat::Matrix<3,3>(fmat::rotation2D(VRmixin::theAgent->getOrientation()));
00148   Planner::PlannerResult result =
00149   planner.planPath(startSt, endStates.front(), curReq->rrtInterpolationStep,
00150                    t, curReq->rrtMaxIterations,
00151                    &(curReq->moveConstrainedPath), treeStartResult, treeEndResult);
00152   
00153   switch ( result.code ) {
00154     case GenericRRTBase::SUCCESS:
00155       std::cout << "Plan Constrained succeeded" << std::endl;
00156       postStateCompletion();
00157       return;
00158     case GenericRRTBase::START_COLLIDES:
00159       std::cout << "Navigation path planning failed: start state is in collision.\n";
00160       break;
00161     case GenericRRTBase::END_COLLIDES:
00162       std::cout << "Navigation path planning failed: end state is in collision.\n";
00163       break;
00164     case GenericRRTBase::MAX_ITER:
00165       std::cout << "Navigation path planning failed: too many iterations.\n";
00166       break;
00167   }
00168   
00169   if (result.code == GenericRRTBase::START_COLLIDES || result.code == GenericRRTBase::END_COLLIDES) {
00170     std::cout << "Obstacle: " << std::endl << "=================" << std::endl << result.movingObstacle->toString() << std::endl;
00171     std::cout << "collided with: " <<std::endl << "=================" << std::endl << result.collidingObstacle->toString() << std::endl;
00172     std::cout << "=================" << std::endl;
00173   }
00174   
00175   // Display the tree if requested
00176   if ( curReq->displayTree && result.code != GenericRRTBase::START_COLLIDES && GenericRRTBase::END_COLLIDES ) {
00177     NEW_SHAPE(plannerTrees, GraphicsData, new GraphicsData(VRmixin::worldShS));
00178     planner.plotTree(*treeStartResult, plannerTrees, rgb(0,0,0));
00179     planner.plotTree(*treeEndResult, plannerTrees, rgb(0,192,0));
00180     delete treeStartResult;
00181     delete treeEndResult;
00182   }
00183   
00184   // Display the path if requested
00185   if ( curReq->displayPath && result.code == GenericRRTBase::SUCCESS ) {
00186     NEW_SHAPE(plannedPath, GraphicsData, new GraphicsData(VRmixin::worldShS));
00187     planner.plotPath(curReq->moveConstrainedPath, plannedPath, rgb(0,0,255));
00188   } 
00189   
00190   if (result.code == GenericRRTBase::SUCCESS)
00191     postStateCompletion();
00192   else {
00193     std::cout << "PLANNER FAILURE - CONSTRAINED" << std::endl;
00194     postStateSignal<GraspError>(GrasperRequest::noGraspPath);
00195   }
00196 }
00197 
00198 void Grasper::PathPlanToRest::doStart() {
00199   NodeValue_t startSt;
00200   NodeValue_t endSt;
00201   
00202   switch(curReq->restType) {
00203     case GrasperRequest::stationary:
00204       postStateCompletion();
00205       return;
00206     case GrasperRequest::settleArm:
00207     case GrasperRequest::settleBodyAndArm:
00208       for(unsigned i = 0; i < NumArmJoints; i++)
00209         endSt[i] = curReq->armRestState[i];
00210       break;
00211   }
00212   
00213   VRmixin::grasper->appendObjectToObstacles();
00214   
00215   if (!curReq->moveConstrainedPath.empty())
00216     startSt = curReq->moveConstrainedPath.back();
00217   else if (!curReq->moveFreePath.empty())
00218     startSt = curReq->moveFreePath.back();
00219   else
00220     Grasper::getCurrentState(startSt);
00221   
00222   if (curReq->verbosity)
00223     std::cout << "Planning on "<< curReq->plannerObstacles.size() << " obstacles" << std::endl;
00224   
00225   GET_SHAPE(worldBounds, PolygonData, VRmixin::worldShS);
00226   Planner planner(VRmixin::worldShS, worldBounds, curReq->rrtInflation, curReq->effectorOffset, NULL);
00227   
00228   std::vector<NodeType_t> *treeStartResult = NULL;
00229   std::vector<NodeType_t> *treeEndResult = NULL;
00230   fmat::Transform t;
00231   DualCoding::Point c = VRmixin::theAgent->getCentroid();
00232   t.translation()[0] = c.coordX();
00233   t.translation()[1] = c.coordY();
00234   t.rotation() = fmat::Matrix<3,3>(fmat::rotation2D(VRmixin::theAgent->getOrientation()));
00235   Planner::PlannerResult result =
00236   planner.planPath(startSt, endSt, curReq->rrtInterpolationStep,
00237                    t, curReq->rrtMaxIterations,
00238                    &(curReq->disengagePath), treeStartResult, treeEndResult);
00239   VRmixin::grasper->removeTargetFromObstacles();
00240   
00241   switch ( result.code ) {
00242     case GenericRRTBase::SUCCESS:
00243       std::cout << "Plan disengage succeeded" << std::endl;
00244       postStateCompletion();
00245       return;
00246     case GenericRRTBase::START_COLLIDES:
00247       std::cout << "Navigation path planning failed: start state is in collision.\n";
00248       break;
00249     case GenericRRTBase::END_COLLIDES:
00250       std::cout << "Navigation path planning failed: end state is in collision.\n";
00251       break;
00252     case GenericRRTBase::MAX_ITER:
00253       std::cout << "Navigation path planning failed: too many iterations.\n";
00254       break;
00255   }
00256   
00257   if (result.code == GenericRRTBase::START_COLLIDES || result.code == GenericRRTBase::END_COLLIDES) {
00258     std::cout << "Obstacle: " << std::endl << "=================" << std::endl << result.movingObstacle->toString() << std::endl;
00259     std::cout << "collided with: " <<std::endl << "=================" << std::endl << result.collidingObstacle->toString() << std::endl;
00260     std::cout << "=================" << std::endl;
00261   }
00262   
00263   // Display the tree if requested
00264   if ( curReq->displayTree && result.code != GenericRRTBase::START_COLLIDES && GenericRRTBase::END_COLLIDES ) {
00265     NEW_SHAPE(plannerTrees, GraphicsData, new GraphicsData(VRmixin::worldShS));
00266     planner.plotTree(*treeStartResult, plannerTrees, rgb(0,0,0));
00267     planner.plotTree(*treeEndResult, plannerTrees, rgb(0,192,0));
00268     delete treeStartResult;
00269     delete treeEndResult;
00270   }
00271   
00272   // Display the path if requested
00273   if ( curReq->displayPath && result.code == GenericRRTBase::SUCCESS ) {
00274     NEW_SHAPE(plannedPath, GraphicsData, new GraphicsData(VRmixin::worldShS));
00275     planner.plotPath(curReq->disengagePath, plannedPath, rgb(0,0,255));
00276   } 
00277   
00278   if (result.code == GenericRRTBase::SUCCESS)
00279     postStateCompletion();
00280   else {
00281     std::cout << "PLANNER FAILURE - ARM_REST" << std::endl;
00282     postStateSignal<GraspError>(GrasperRequest::noGraspPath);
00283   }
00284 }
00285 
00286 void Grasper::MoveArm::doStart() {
00287   switch(pa) {
00288     case GrasperRequest::moveFree:
00289       if ( curReq->verbosity & GVexecutePath )
00290         std::cout << "Grasper: moving along unconstrained path" << std::endl;
00291       executeMove(curReq->moveFreePath);
00292       break;
00293     case GrasperRequest::moveConstrained:
00294       if ( curReq->verbosity & GVexecutePath )
00295         std::cout << "Grasper: moving along constrained path" << std::endl;
00296       executeMove(curReq->moveConstrainedPath);
00297       break;
00298     case GrasperRequest::disengage:
00299       if ( curReq->verbosity & GVexecutePath )
00300         std::cout << "Grasper: moving along disengage path" << std::endl;
00301       executeMove(curReq->disengagePath);
00302       break;
00303     case GrasperRequest::noPath:
00304       break;
00305   }
00306 }
00307 
00308 void Grasper::MoveArm::executeMove(const std::vector<NodeValue_t>& path) {
00309   MMAccessor<DynamicMotionSequence> move_acc = getMC();
00310   move_acc->clear();
00311   move_acc->setTime(10);
00312   for(unsigned int p = 0; p < path.size(); p++) {
00313     unsigned int j = 0;
00314     for(unsigned int i = 0; j < NumArmJoints; i++) {
00315 #ifdef TGT_HAS_ARMS
00316       if (kine->getKinematicJoint(ArmOffset+i)->isMobile() == false)
00317         continue;
00318       move_acc->setOutputCmd(ArmOffset+i, (float)path[p][j]);
00319 #endif
00320       j++;
00321     }
00322     if (p < path.size()-1) {
00323       NodeValue_t confDif;
00324       for(unsigned i = 0; i < NumArmJoints; i++)
00325         confDif[i] = path[p][i] - path[p+1][i];
00326       move_acc->advanceTime( advTime(confDif) );
00327     }
00328   }
00329   
00330   move_acc->play();
00331 }
00332 
00333 void Grasper::convertObstacles() {
00334   // First delete any obstacles left around by the last planning operation
00335   for(unsigned i = 0; i < curReq->plannerObstacles.size(); i++)
00336     delete curReq->plannerObstacles[i];
00337   curReq->plannerObstacles.clear();
00338   
00339   for(std::vector<ShapeRoot>::iterator it = curReq->envObstacles.begin();
00340       it != curReq->envObstacles.end(); it++)
00341 #ifdef TGT_CALLIOPE
00342     PlannerObstacle3D::convertShapeToPlannerObstacle(*it, 0, curReq->plannerObstacles);
00343 #else
00344     PlannerObstacle2D::convertShapeToPlannerObstacle(*it, 0, curReq->plannerObstacles);
00345 #endif
00346   
00347   // Prevent any self collisions
00348   /*
00349    TODO: body?
00350    Make this cleaner. Have hardcoded leg values in here
00351    */
00352   
00353 #ifdef TGT_HAS_LEGS
00354   //Need to prevent the arm from running into any legs
00355   vector<string> legNames, sweepNames;
00356   legNames.push_back("RFrFootFrame"); sweepNames.push_back("RFr:sweep");
00357   //  legNames.push_back("LFrFootFrame"); sweepNames.push_back("LFr:sweep");
00358   //  legNames.push_back("RMdFootFrame"); sweepNames.push_back("RMd:sweep");
00359   //  legNames.push_back("LMdFootFrame"); sweepNames.push_back("LMd:sweep");
00360   //  legNames.push_back("RBkFootFrame"); sweepNames.push_back("RBk:sweep");
00361   //  legNames.push_back("LBkFootFrame"); sweepNames.push_back("LBk:sweep");
00362   
00363   std::cout << "Grasper HACK: Leg obstacles" << std::endl;
00364   
00365   for (unsigned i = 0; i < legNames.size(); i++) {
00366     unsigned int jInd = capabilities.findFrameOffset(legNames[i]);
00367     unsigned int sInd = capabilities.findFrameOffset(sweepNames[i]);
00368     if (jInd > NumReferenceFrames || sInd > NumReferenceFrames)
00369       continue;
00370     const KinematicJoint* k = kine->getKinematicJoint(jInd);
00371     const KinematicJoint* s = kine->getKinematicJoint(sInd);
00372     if (k == NULL || s == NULL)
00373       continue;
00374     fmat::Column<3> legPos = k->getWorldPosition(), sweepPos = s->getWorldPosition();
00375     legPos[2] = sweepPos[2] = 0;
00376     float radius = 10;
00377     bool notRest = !(curReq->requestType == GrasperRequest::rest);
00378     //    std::cout << "isStanding=" << isStanding << " notRest=" << notRest << std::endl;
00379     if (notRest) {
00380       fmat::Column<3> dif = legPos - sweepPos;
00381       dif /= dif.norm();
00382       dif *= 40;
00383       //      std::cout << "for "<< legNames[i] << " legPos=" << legPos << " dif=" << dif;
00384       legPos += dif;
00385       //      std::cout << " updated=" << legPos << std::endl;
00386       radius = 30;
00387     }
00388     Shape<CylinderData> leg(new CylinderData(VRmixin::localShS, Point(legPos),  300, radius));
00389     PlannerObstacle::convertShapeToPlannerObstacle(leg, 0, curReq->plannerObstacles);
00390   }
00391 #endif
00392   
00393 }
00394 
00395 void Grasper::getCurrentState(NodeValue_t &current, KinematicJoint* endEffector) {
00396   KinematicJoint* joint = endEffector == NULL ? kine->getKinematicJoint(curReq->effectorOffset)->cloneBranch() : endEffector;
00397   for (unsigned int j = NumArmJoints; j > 0; j--) {
00398     while (joint->isMobile() == false) joint = joint->getParent();
00399     current[j-1] = joint->getQ();
00400     joint = joint->getParent();
00401   }
00402 }
00403 
00404 void Grasper::computeGoalStates(Point& toPt, std::vector<std::pair<float, float> >& rangesX,
00405                                 std::vector<std::pair<float, float> >& rangesY, std::vector<std::pair<float, float> >& rangesZ,
00406                                 float res, std::vector<NodeValue_t>& goals, fmat::Column<3> offset) {
00407   KinematicJoint* effector = kine->getKinematicJoint(curReq->effectorOffset)->cloneBranch();
00408   IKSolver& solver = effector->getIK();
00409   goals.clear();
00410   if (res == 0)
00411     res = 1;
00412   
00413   if (rangesX.empty()) {
00414     rangesX.push_back(pair<float, float>(0, 2*M_PI));
00415   }
00416   
00417   if (rangesY.empty()) {
00418     rangesY.push_back(pair<float, float>(0, 2*M_PI));
00419   }
00420   
00421   if (rangesZ.empty()) {
00422     rangesZ.push_back(pair<float, float>(0, 2*M_PI));
00423   }
00424   
00425   int factorX = 0;
00426   bool keepGoing = true;
00427   do {
00428     keepGoing = false;
00429     for(unsigned rX = 0; rX < rangesX.size(); rX++) {
00430       
00431       float thetaX = res * factorX;
00432       float midX = (rangesX[rX].first + rangesX[rX].second)/2;
00433       if (thetaX > rangesX[rX].second - midX)
00434         continue;
00435       int factorY = 0;
00436       
00437       for (unsigned rY = 0; rY < rangesY.size(); rY++) {
00438         
00439         float thetaY = res * factorY;
00440         float midY = (rangesY[rY].first + rangesY[rY].second)/2;
00441         if (thetaY > rangesY[rY].second - midY)
00442           continue;
00443         int factorZ = 0;
00444         
00445         for (unsigned rZ = 0; rZ < rangesZ.size(); rZ++) {
00446           
00447           float thetaZ = res * factorZ;
00448           float midZ = (rangesZ[rZ].first + rangesZ[rZ].second)/2;
00449           if (thetaZ > rangesZ[rZ].second - midZ)
00450             continue;
00451           
00452           keepGoing = true;
00453           fmat::Quaternion ori = fmat::Quaternion::fromMatrix(fmat::rotationZ(midZ + thetaZ) * fmat::rotationY(midY + thetaY) * fmat::rotationX(midX + thetaX));
00454           bool reached;
00455           reached = solver.solve(offset, IKSolver::Rotation(), *effector, IKSolver::Point(toPt.coords[0], toPt.coords[1], toPt.coords[2]), 1, IKSolver::Rotation(ori), 0);
00456           if (reached) {
00457             NodeValue_t endSt;
00458             KinematicJoint* joint = effector;
00459             for(unsigned int j = NumArmJoints; j > 0; j--) {
00460               while (joint->isMobile() == false) joint = joint->getParent();
00461               endSt[j-1] = joint->getQ();
00462               joint = joint->getParent();
00463             }
00464             goals.push_back(endSt);
00465           }
00466           
00467           ori = fmat::Quaternion::fromMatrix(fmat::rotationZ(midZ - thetaZ) * fmat::rotationY(midY - thetaY) * fmat::rotationX(midX - thetaX));
00468           reached = solver.solve(offset, IKSolver::Rotation(), *effector, IKSolver::Point(toPt.coords[0], toPt.coords[1], toPt.coords[2]), 1, IKSolver::Rotation(ori), 0);
00469           if (reached) {
00470             NodeValue_t endSt;
00471             KinematicJoint* joint = effector;
00472             while (joint->isMobile() == false) joint = joint->getParent();
00473             for(unsigned int j = NumArmJoints; j > 0; j--) {
00474               while (joint->isMobile() == false) joint = joint->getParent();
00475               endSt[j-1] = joint->getQ();
00476               joint = joint->getParent();
00477             }
00478             goals.push_back(endSt);
00479           }
00480           if ( curReq->maxNumberOfAngles <= (signed)goals.size() ) {
00481             keepGoing = false;
00482           }
00483           factorZ++;
00484         } // Z
00485         factorY++;
00486       } // Y
00487       factorX++;
00488     } // X
00489   } while (keepGoing);
00490   if ( curReq->verbosity & GVcomputeGoals )
00491     std::cout << "Grasper found " << goals.size() << " potential goal states." << std::endl;
00492 }
00493 
00494 unsigned int Grasper::executeRequest(const GrasperRequest& req, BehaviorBase* requestingBehavior) {
00495   GrasperRequest *newReq = new GrasperRequest(req);
00496   newReq->requestingBehavior = requestingBehavior;
00497   unsigned int reqID = ++idCounter;
00498   newReq->requestID = reqID;
00499   GraspError e = newReq->validateRequest();
00500   if ( e != GrasperRequest::noError ) {
00501     std::cout << "*** Grasper received invalid grasp request, id=" << reqID << std::endl;
00502     GrasperEvent myEvent(false, newReq->requestType, e, (size_t)newReq->requestingBehavior);
00503     erouter->postEvent(myEvent);
00504   }
00505   else {
00506     requests.push(newReq);
00507     executeRequest();
00508   }
00509   return reqID;
00510 }
00511 
00512 void Grasper::executeRequest() {
00513   if ( curReq != NULL )
00514     return;
00515   else if ( requests.empty() )
00516     return;
00517   curReq = requests.front();
00518   requests.pop();
00519   
00520   curReq->verbosity = (verbosity & ~curReq->clearVerbosity) | curReq->setVerbosity;
00521   if ( curReq->verbosity & GVexecuteRequest )
00522     std::cout << "Grasper now executing request " << curReq->requestID << std::endl;
00523   
00524   if (curReq->object.isValid() && curReq->object->getSpace().getRefFrameType() == VRmixin::worldShS.getRefFrameType())
00525     curReq->object = VRmixin::mapBuilder->importWorldToLocal(curReq->object);
00526   
00527   if (curReq->targetLocation.isValid() && curReq->targetLocation->getSpace().getRefFrameType() == VRmixin::worldShS.getRefFrameType())
00528     curReq->targetLocation = VRmixin::mapBuilder->importWorldToLocal(curReq->targetLocation);
00529   convertObstacles();
00530   dispatch();
00531 }
00532 
00533 void Grasper::dispatch() {
00534   switch( curReq->requestType ) {
00535     case GrasperRequest::reach:
00536       reachRoot->start();
00537       break;
00538     case GrasperRequest::computeReach:
00539       if ( curReq->populateEventPathWith == GrasperRequest::noPath )
00540         curReq->populateEventPathWith = GrasperRequest::moveFree;
00541       computeReachRoot->start();
00542       break;
00543     case GrasperRequest::grasp:
00544       graspRoot->start();
00545       break;
00546     case GrasperRequest::checkGraspable:
00547       checkRoot->start();
00548       break;
00549     case GrasperRequest::release:
00550       releaseRoot->start();
00551       break;
00552     case GrasperRequest::touch:
00553       touchRoot->start();
00554       break;
00555     case GrasperRequest::moveTo:
00556       moveRoot->start();
00557       break;
00558     case GrasperRequest::checkMovable:
00559       checkRoot->start();
00560       break;
00561     case GrasperRequest::rest:
00562       restRoot->start();
00563       break;
00564     case GrasperRequest::checkRestable:
00565       checkRoot->start();
00566       break;
00567     case GrasperRequest::sweep:
00568       std::cout << "Grasper functionality " << curReq->requestType << " not implemented"<< std::endl;
00569       break;
00570     default:
00571       std::cout << "Unidentified requestType" << std::endl;
00572       break;
00573   }
00574 } 
00575 
00576 #endif // TGT_HAS_ARMS

Tekkotsu v5.1CVS
Generated Fri Mar 16 05:26:41 2012 by Doxygen 1.6.3