planning_rigid_body.cpp
Go to the documentation of this file.
00001 #include <ompl/base/SpaceInformation.h>
00002 #include <ompl/base/spaces/SE3StateSpace.h>
00003 #include <ompl/geometric/planners/rrt/RRTConnect.h>
00004 #include <ompl/geometric/SimpleSetup.h>
00005 
00006 #include <ompl/config.h>
00007 #include <iostream>
00008 #include <fstream>
00009 #include <stdio.h>
00010 namespace ob = ompl::base;
00011 namespace og = ompl::geometric;
00012 
00013 class PlanRigidBody
00014 {
00015         public:
00016         PlanRigidBody()
00017         {
00018         // construct the state space we are planning in
00019         space.reset(new ob::SE3StateSpace());
00020 
00021         // set the bounds for the R^3 part of SE(3)
00022         ob::RealVectorBounds bounds(3);
00023         bounds.setLow(-2);
00024         bounds.setHigh(2);
00025 
00026         space->as<ob::SE3StateSpace>()->setBounds(bounds);
00027  int real_vector_index = space->as<ompl::base::CompoundStateSpace>()->getSubSpaceCount();
00028 std::cout<<"real vector:= "<<real_vector_index<<std::endl;
00029         }
00030         
00031         ~PlanRigidBody(){}
00032         
00033         static bool isStateValid(const ob::State *state)
00034         {
00035         // cast the abstract state type to the type we expect
00036         const ob::SE3StateSpace::StateType *se3state = state->as<ob::SE3StateSpace::StateType>();
00037 
00038         // extract the first component of the state and cast it to what we expect
00039         const ob::RealVectorStateSpace::StateType *pos = se3state->as<ob::RealVectorStateSpace::StateType>(0);
00040 
00041         // extract the second component of the state and cast it to what we expect
00042         const ob::SO3StateSpace::StateType *rot = se3state->as<ob::SO3StateSpace::StateType>(1);
00043 
00044         // check validity of state defined by pos & rot
00045 
00046 
00047         // return a value that is always true but uses the two variables we define, so we avoid compiler warnings
00048         return (void*)rot != (void*)pos;
00049         }
00050 
00051         void planWithSimpleSetup(ob::ScopedState<ompl::base::SE3StateSpace>& start,ob::ScopedState<ompl::base::SE3StateSpace>& goal, int st)
00052         {
00053         // define a simple setup class
00054         og::SimpleSetup ss(space);
00055         // set state validity checking for this space
00056         ss.setStateValidityChecker(boost::bind(&isStateValid, _1));
00057         // create a random start state
00058 
00059         // set the start and goal states
00060         ss.setStartAndGoalStates(start, goal);
00061         // this call is optional, but we put it in to get more output information
00062         ss.setup();
00063         ss.print();
00064         // attempt to solve the problem within one second of planning time
00065         bool solved = ss.solve(1.0);
00066         if (solved)
00067         {
00068                 std::cout << "Found solution:" << std::endl;
00069                 // print the path to screen
00070                 ss.simplifySolution();
00071                 //ss.getSolutionPath().print(std::cout);
00072                 og::PathGeometric path=ss.getSolutionPath();
00073                 //std::cout<<"SIZE "<<path.states_<<std::endl;
00074                 path.interpolate(st);
00075                 std::cout<<"SIZE "<<path.states.size()<<std::endl;
00076                 path.print(std::cout);
00077                 writeFile(path);
00078         }
00079         else
00080                 std::cout << "No solution found" << std::endl;
00081         }
00082 
00083 
00084         void getKeyboard(std::string estado,ob::ScopedState<ompl::base::SE3StateSpace>& state)
00085         {
00086                 std::cout<<"Ingresa el "<<estado<<std::endl;
00087                 float tx,ty,tz,qx,qy,qz,qw;
00088                 std::cout<<"Ingresa tx (traslacion en X) "<<std::endl;
00089                 std::cin>>tx;
00090                 std::cout<<"tx ingresado "<<tx<<std::endl;
00091                 std::cout<<"Ingresa ty (traslacion en Y) "<<std::endl;
00092                 std::cin>>ty;
00093                 std::cout<<"ty ingresado "<<ty<<std::endl;
00094                 std::cout<<"Ingresa tz (traslacion en Z) "<<std::endl;
00095                 std::cin>>tz;
00096                 std::cout<<"tz ingresado "<<tz<<std::endl;
00097                 std::cout<<"Ingresa qx (Rotacion en X,Quaternion) "<<std::endl;
00098                 std::cin>>qx;
00099                 std::cout<<"qx ingresado "<<qx<<std::endl;
00100                 std::cout<<"Ingresa qy (Rotacion en Y,Quaternion) "<<std::endl;
00101                 std::cin>>qy;
00102                 std::cout<<"qy ingresado "<<qy<<std::endl;
00103                 std::cout<<"Ingresa qz (Rotacion en Z,Quaternion) "<<std::endl;
00104                 std::cin>>qz;
00105                 std::cout<<"qz ingresado "<<qz<<std::endl;
00106                 std::cout<<"Ingresa qw (Angulo del Quaternion) "<<std::endl;
00107                 std::cin>>qw;
00108                 std::cout<<"qw ingresado "<<qw<<std::endl;
00109                 state->setXYZ(tx,ty,tz);
00110                 state->rotation().x=qx;
00111                 state->rotation().y=qy;
00112                 state->rotation().z=qz;
00113                 state->rotation().w=qw;
00114                 state.print();          
00115         }
00116         void writeFile(og::PathGeometric& path)
00117         {
00118                 std::ofstream traj;
00119                 traj.open("/home/irojas/Desktop/trajectories.txt", std::ofstream::out);
00120                 float tx,ty,tz,qx,qy,qz,qw;
00121                 for(size_t i=0; i <path.states.size(); ++i)
00122                 {
00123                         tx=path.states[i]->as<ob::SE3StateSpace::StateType>()->getX();
00124                         ty=path.states[i]->as<ob::SE3StateSpace::StateType>()->getY();
00125                         tz=path.states[i]->as<ob::SE3StateSpace::StateType>()->getZ();
00126                         qx=path.states[i]->as<ob::SE3StateSpace::StateType>()->rotation().x;
00127                         qy=path.states[i]->as<ob::SE3StateSpace::StateType>()->rotation().y;
00128                         qz=path.states[i]->as<ob::SE3StateSpace::StateType>()->rotation().z;
00129                         qw=path.states[i]->as<ob::SE3StateSpace::StateType>()->rotation().w;
00130                         
00131                         traj<<tx<<" "<<ty<<" "<<tz<<" "<<qx<<" "<<qy<<" "<<qz<<" "<<qw<<"\n";
00132                         std::cout<<tx<<" "<<ty<<" "<<tz<<" "<<qx<<" "<<qy<<" "<<qz<<" "<<qw<<std::endl;
00133                 }
00134                 traj.close();
00135         }
00136         ob::StateSpacePtr space;
00137 };
00138 
00139 int main(int argc, char ** argv)
00140 {
00141         PlanRigidBody plan;
00142         int st=0;
00143     std::cout << "OMPL version: " << OMPL_VERSION << std::endl;
00144     ob::ScopedState<ompl::base::SE3StateSpace> goal(plan.space);
00145     ob::ScopedState<ompl::base::SE3StateSpace> start(plan.space);
00146         //plan.getTrajectory(argc,argv,goal);
00147         plan.getKeyboard("Estado Inicial",start);
00148 getchar();
00149    // std::cout << std::endl << std::endl;
00150         plan.getKeyboard("Estado Final",goal);
00151 getchar();
00152                 std::cout<<"Ingresa Cantidad de estados deseados(puntos de la trayectorias) "<<std::endl;
00153                 std::cin>>st;
00154                 std::cout<<"cantidad de estados ingresado "<<st<<std::endl;             
00155     plan.planWithSimpleSetup(start,goal,st);
00156 
00157     return 0;
00158 }


iri_wam_arm_navigation
Author(s): IRI Robotics Lab, Ivan Rojas (ivan.rojas.j@gmail.com)
autogenerated on Fri Dec 6 2013 22:32:00