wam_cartesian_planning_alg.cpp
Go to the documentation of this file.
00001 #include "wam_cartesian_planning_alg.h"
00002 namespace ob = ompl::base;
00003 namespace og = ompl::geometric;
00004 WamCartesianPlanningAlgorithm::WamCartesianPlanningAlgorithm(void)
00005 {
00006  // construct the state space we are planning in
00007  space.reset(new ob::SE3StateSpace());
00008  // set the bounds for the R^3 part of SE(3)
00009  ob::RealVectorBounds bounds(3);
00010  bounds.setLow(-2);
00011  bounds.setHigh(2);
00012  space->as<ob::SE3StateSpace>()->setBounds(bounds);
00013 }
00014 
00015 WamCartesianPlanningAlgorithm::~WamCartesianPlanningAlgorithm(void)
00016 {
00017 }
00018 
00019 void WamCartesianPlanningAlgorithm::config_update(Config& new_cfg, uint32_t level)
00020 {
00021   this->lock();
00022 
00023   // save the current configuration
00024   this->config_=new_cfg;
00025   
00026   this->unlock();
00027 }
00028 
00029 bool WamCartesianPlanningAlgorithm::isStateValid(const ob::State *state)
00030 {
00031         // cast the abstract state type to the type we expect
00032         const ob::SE3StateSpace::StateType *se3state = state->as<ob::SE3StateSpace::StateType>();
00033 
00034         // extract the first component of the state and cast it to what we expect
00035         const ob::RealVectorStateSpace::StateType *pos = se3state->as<ob::RealVectorStateSpace::StateType>(0);
00036 
00037         // extract the second component of the state and cast it to what we expect
00038         const ob::SO3StateSpace::StateType *rot = se3state->as<ob::SO3StateSpace::StateType>(1);
00039 
00040         // check validity of state defined by pos & rot
00041 
00042 
00043         // return a value that is always true but uses the two variables we define, so we avoid compiler warnings
00044         return (void*)rot != (void*)pos;
00045 }
00046 
00047 void WamCartesianPlanningAlgorithm::planWithSimpleSetup(ob::ScopedState<ompl::base::SE3StateSpace>& start,ob::ScopedState<ompl::base::SE3StateSpace>& goal, const int& st,std::vector<geometry_msgs::PoseStamped>& vector)
00048 {
00049         // define a simple setup class
00050         og::SimpleSetup ss(space);
00051         // set state validity checking for this space
00052         ss.setStateValidityChecker(boost::bind(&isStateValid, _1));
00053         // create a random start state
00054         std::vector<geometry_msgs::PoseStamped> poses;
00055         // set the start and goal states
00056         ss.setStartAndGoalStates(start, goal);
00057         // this call is optional, but we put it in to get more output information
00058         ss.setup();
00059         ss.print();
00060         // attempt to solve the problem within one second of planning time
00061         bool solved = ss.solve(1.0);
00062         if (solved)
00063         {
00064                 std::cout << "Found solution:" << std::endl;
00065                 // print the path to screen
00066                 ss.simplifySolution();
00067                 og::PathGeometric path=ss.getSolutionPath();
00068                 path.interpolate(st);
00069                 omplToRos(path,poses);
00070 /*              std::cout<<"SIZE "<<path.states.size()<<std::endl;
00071                 path.print(std::cout);
00072                 std::string pt="/home/irojas/Desktop/trajectories.txt";
00073                 writeFile(path,pt);
00074 */ 
00075         }
00076 
00077         else std::cout << "No solution found" << std::endl;
00078 }
00079 
00080 void WamCartesianPlanningAlgorithm::omplToRos(og::PathGeometric& path, std::vector<geometry_msgs::PoseStamped>& vect)
00081 {       
00082                 geometry_msgs::PoseStamped msg;
00083                 for(size_t i=0; i <path.getStates().size(); ++i)
00084                 {
00085                         msg.pose.position.x=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->getX();
00086                         msg.pose.position.y=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->getY();
00087                         msg.pose.position.z=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->getZ();
00088                         msg.pose.orientation.x=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->rotation().x;
00089                         msg.pose.orientation.y=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->rotation().y;
00090                         msg.pose.orientation.z=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->rotation().z;
00091                         msg.pose.orientation.w=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->rotation().w;
00092                         vect.push_back(msg);
00093                 }
00094 }
00095 void WamCartesianPlanningAlgorithm::writeFile(og::PathGeometric& path,std::string& pathDir)
00096 {
00097         std::ofstream traj;
00098         traj.open(pathDir.c_str(), std::ofstream::out);
00099         float tx,ty,tz,qx,qy,qz,qw;
00100         for(size_t i=0; i <path.getStates().size(); ++i)
00101         {
00102                 tx=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->getX();
00103                 ty=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->getY();
00104                 tz=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->getZ();
00105                 qx=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->rotation().x;
00106                 qy=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->rotation().y;
00107                 qz=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->rotation().z;
00108                 qw=path.getStates()[i]->as<ob::SE3StateSpace::StateType>()->rotation().w;
00109                 
00110                 traj<<tx<<" "<<ty<<" "<<tz<<" "<<qx<<" "<<qy<<" "<<qz<<" "<<qw<<"\n";
00111                 std::cout<<tx<<" "<<ty<<" "<<tz<<" "<<qx<<" "<<qy<<" "<<qz<<" "<<qw<<std::endl;
00112         }
00113         traj.close();
00114 }


iri_wam_cartesian_planning
Author(s): IRI Robotics Lab, Ivan Rojas (ivan.rojas.j@gmail.com)
autogenerated on Fri Dec 6 2013 23:03:21