move_in_joints.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <actionlib/client/simple_action_client.h>
00003 #include <actionlib/client/terminal_state.h>
00004 #include <arm_navigation_msgs/MoveArmAction.h>
00005 #include <arm_navigation_msgs/utils.h>
00006 #include <sstream>
00007 #include <iostream>
00008 #include <stdlib.h>
00009 
00010 typedef actionlib::SimpleActionClient<arm_navigation_msgs::MoveArmAction> TrajClient;
00011 
00012 class MoveInJoints
00013 {
00014   private:
00015         // Action client for the joint trajectory action 
00016     // used to trigger the   arm movement action
00017         TrajClient* traj_client_;
00018         ros::Duration time_move;
00019         std::vector<double> pos;
00020         std::vector<std::string> names;
00021   public:
00023         MoveInJoints() 
00024         {
00025                 // tell the action client that we want to spin a thread by default
00026                 traj_client_ = new TrajClient("/move_iri_wam", true);
00027                 
00028                 // wait for action server to come up
00029                 while(!traj_client_->waitForServer(ros::Duration(5.0))){
00030                 ROS_INFO("Waiting for the move_iri_wam server");
00031                 }
00032         }
00034         ~MoveInJoints()
00035         {
00036      delete traj_client_;
00037         }
00039         void setNamesJoint(void)
00040         {
00041                 names.push_back("j1_joint");
00042                 names.push_back("j2_joint");
00043                 names.push_back("j3_joint");
00044                 names.push_back("j4_joint");
00045                 names.push_back("j5_joint");
00046                 names.push_back("j6_joint");
00047                 names.push_back("j7_joint");
00048         }
00050     void startTrajectory(arm_navigation_msgs::MoveArmGoal& goal)
00051     {
00052         traj_client_->sendGoal(goal);
00053     }
00055     void getTrajectory(int argc, char** argv)
00056         {
00057          if(argc < 8 || argc > 9) 
00058          {
00059            ROS_FATAL("Error: The numbers of parameters out of bound "); 
00060            exit(1);
00061          }
00062          pos.resize(7);
00063          for(int i =1; i <=7; ++i) pos[i-1]=strtod(argv[i],NULL);
00064 
00065          time_move= (argc == 9)?ros::Duration(strtod(argv[8],NULL)):ros::Duration(0.0);
00066         }  
00068     actionlib::SimpleClientGoalState getState()
00069     {
00070      return traj_client_->getState();
00071     }     
00073         void setPlannerRequest(arm_navigation_msgs::MoveArmGoal& goal)
00074         {
00075                 goal.motion_plan_request.group_name="iri_wam";
00076                 goal.motion_plan_request.num_planning_attempts = 1;
00077                 goal.motion_plan_request.allowed_planning_time = ros::Duration(5.0);
00078                 goal.motion_plan_request.planner_id = std::string("");
00079                 goal.planner_service_name=std::string("ompl_planning/plan_kinematic_path");
00080                 goal.motion_plan_request.expected_path_dt = time_move;
00081         }
00083         void setPlannerRequestGoalConstraint(arm_navigation_msgs::MoveArmGoal& goal)
00084         { 
00085           goal.motion_plan_request.goal_constraints.joint_constraints.resize(names.size());
00086           for (unsigned int i = 0 ; i < names.size(); ++i)
00087           {
00088        goal.motion_plan_request.goal_constraints.joint_constraints[i].joint_name = names[i];
00089        goal.motion_plan_request.goal_constraints.joint_constraints[i].position = pos[i];
00090        goal.motion_plan_request.goal_constraints.joint_constraints[i].tolerance_below = pos[i]+ 0.0119999;
00091            goal.motion_plan_request.goal_constraints.joint_constraints[i].tolerance_above = pos[i] -0.0119999;
00092            if(goal.motion_plan_request.goal_constraints.joint_constraints[i].tolerance_below < 0.0){
00093                 goal.motion_plan_request.goal_constraints.joint_constraints[i].tolerance_below *= -1;
00094            }
00095            if(goal.motion_plan_request.goal_constraints.joint_constraints[i].tolerance_above < 0.0){
00096                 goal.motion_plan_request.goal_constraints.joint_constraints[i].tolerance_above *= -1;
00097            }
00098           }
00099         }
00100 };
00101 int main(int argc, char** argv)
00102 {
00103   // Init the ROS node
00104   ros::init(argc, argv, "move_arm_node");
00105   MoveInJoints arm;
00106   arm_navigation_msgs::MoveArmGoal move;
00107   // Start the trajectory  
00108   arm.getTrajectory(argc,argv);
00109   arm.setNamesJoint();
00110   arm.setPlannerRequest(move);
00111   arm.setPlannerRequestGoalConstraint(move);
00112   arm.startTrajectory(move);
00113   // Wait for trajectory completion
00114   while(!arm.getState().isDone() && ros::ok())
00115   {
00116           ROS_DEBUG("WAIT To END TRAJECTORY");
00117   }
00118       
00119  bool success = (arm.getState() == actionlib::SimpleClientGoalState::SUCCEEDED);
00120   if(success)
00121    ROS_INFO("Action finished: %s",arm.getState().toString().c_str());
00122   else
00123    ROS_INFO("Action failed: %s",arm.getState().toString().c_str());
00124 }


iri_wam_move_arm
Author(s): Ivan Rojas (ivan.rojas.j@gmail.com)
autogenerated on Fri Dec 6 2013 20:42:05