move_pr2.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <pr2_controllers_msgs/JointTrajectoryAction.h>
00003 #include <actionlib/client/simple_action_client.h>
00004 #include <pr2_controllers_msgs/Pr2GripperCommandAction.h>
00005 #include <gazebo_msgs/BodyRequest.h>
00006 #include <gazebo_msgs/ApplyBodyWrench.h>
00007 #include <gazebo_msgs/SetModelConfiguration.h>
00008 #include <std_srvs/Empty.h>
00009 #include <pr2_mechanism_msgs/SwitchController.h>
00010 
00011 typedef actionlib::SimpleActionClient< pr2_controllers_msgs::JointTrajectoryAction > TrajClient;
00012 
00013 class RobotArm
00014 {
00015 private:
00016   // Action client for the joint trajectory action 
00017   // used to trigger the arm movement action
00018   TrajClient* traj_client_;
00019 
00020 public:
00022   RobotArm() 
00023   {
00024     // tell the action client that we want to spin a thread by default
00025     traj_client_ = new TrajClient("r_arm_controller/joint_trajectory_action", true);
00026 
00027     // wait for action server to come up
00028     while(!traj_client_->waitForServer(ros::Duration(5.0))){
00029       ROS_INFO("Waiting for the joint_trajectory_action server");
00030     }
00031   }
00032 
00034   ~RobotArm()
00035   {
00036     delete traj_client_;
00037   }
00038 
00040   void startTrajectory(pr2_controllers_msgs::JointTrajectoryGoal goal)
00041   {
00042     // When to start the trajectory: 1s from now
00043     //goal.trajectory.header.stamp = ros::Time::now() + ros::Duration(3.0);
00044     ros::Duration wait_time(20.0);
00045     actionlib::SimpleClientGoalState state = traj_client_->sendGoalAndWait(goal,wait_time,wait_time);
00046     if (state == actionlib::SimpleClientGoalState::SUCCEEDED)
00047       ROS_INFO("The arm traj finished with state [%s]",state.toString().c_str());
00048     else
00049       ROS_INFO("The arm traj failed with state [%s]",state.toString().c_str());
00050   }
00051 
00053 
00058   pr2_controllers_msgs::JointTrajectoryGoal armExtensionTrajectory()
00059   {
00060     //our goal variable
00061     pr2_controllers_msgs::JointTrajectoryGoal goal;
00062 
00063     // First, the joint names, which apply to all waypoints
00064     goal.trajectory.joint_names.push_back("r_shoulder_pan_joint");
00065     goal.trajectory.joint_names.push_back("r_shoulder_lift_joint");
00066     goal.trajectory.joint_names.push_back("r_upper_arm_roll_joint");
00067     goal.trajectory.joint_names.push_back("r_elbow_flex_joint");
00068     goal.trajectory.joint_names.push_back("r_forearm_roll_joint");
00069     goal.trajectory.joint_names.push_back("r_wrist_flex_joint");
00070     goal.trajectory.joint_names.push_back("r_wrist_roll_joint");
00071 
00072     // We will have two waypoints in this goal trajectory
00073     goal.trajectory.points.resize(2);
00074 
00075     // First trajectory point
00076     // Positions
00077     int ind = 0;
00078     goal.trajectory.points[ind].positions.resize(7);
00079     goal.trajectory.points[ind].positions[0] = 0.0;
00080     goal.trajectory.points[ind].positions[1] = 0.0;
00081     goal.trajectory.points[ind].positions[2] = 0.0;
00082     goal.trajectory.points[ind].positions[3] = 0.0;
00083     goal.trajectory.points[ind].positions[4] = 0.0;
00084     goal.trajectory.points[ind].positions[5] = 0.0;
00085     goal.trajectory.points[ind].positions[6] = 0.0;
00086     // To be reached 1 second after starting along the trajectory
00087     goal.trajectory.points[ind].time_from_start = ros::Duration(3.0);
00088 
00089     // Second trajectory point
00090     // Positions
00091     ind += 1;
00092     goal.trajectory.points[ind].positions.resize(7);
00093     goal.trajectory.points[ind].positions[0] = 0.0;
00094     goal.trajectory.points[ind].positions[1] = 0.25;
00095     goal.trajectory.points[ind].positions[2] = 0.0;
00096     goal.trajectory.points[ind].positions[3] = 0.0;
00097     goal.trajectory.points[ind].positions[4] = 0.0;
00098     goal.trajectory.points[ind].positions[5] = 0.0;
00099     goal.trajectory.points[ind].positions[6] = 0.0;
00100     // To be reached 2 seconds after starting along the trajectory
00101     goal.trajectory.points[ind].time_from_start = ros::Duration(6.0);
00102 
00103     //we are done; return the goal
00104     return goal;
00105   }
00106 
00108   actionlib::SimpleClientGoalState getState()
00109   {
00110     return traj_client_->getState();
00111   }
00112  
00113 };
00114 
00115 // Our Action interface type, provided as a typedef for convenience
00116 typedef actionlib::SimpleActionClient<pr2_controllers_msgs::Pr2GripperCommandAction> GripperClient;
00117 
00118 class Gripper{
00119 private:
00120   GripperClient* gripper_client_;  
00121 
00122 public:
00123   //Action client initialization
00124   Gripper(std::string action_topic){
00125 
00126     //Initialize the client for the Action interface to the gripper controller
00127     //and tell the action client that we want to spin a thread by default
00128     gripper_client_ = new GripperClient(action_topic, true);
00129     //gripper_client_ = new GripperClient("r_gripper_controller/gripper_action", true);
00130     
00131     //wait for the gripper action server to come up 
00132     while(!gripper_client_->waitForServer(ros::Duration(30.0))){
00133       ROS_INFO("Waiting for the r_gripper_controller/gripper_action action server to come up");
00134     }
00135   }
00136 
00137   ~Gripper(){
00138     delete gripper_client_;
00139   }
00140 
00141   //Open the gripper
00142   void open(){
00143     pr2_controllers_msgs::Pr2GripperCommandGoal open;
00144     open.command.position = 0.08;
00145     open.command.max_effort = 100.0;  // use -1 to not limit effort (negative)
00146     
00147     ROS_INFO("Sending open goal");
00148     if (gripper_client_->sendGoalAndWait(open,ros::Duration(10.0),ros::Duration(5.0)) == actionlib::SimpleClientGoalState::SUCCEEDED)
00149       ROS_INFO("The gripper opened!");
00150     else
00151       ROS_INFO("The gripper failed to open.");
00152   }
00153 
00154   //Close the gripper
00155   void close(){
00156     pr2_controllers_msgs::Pr2GripperCommandGoal squeeze;
00157     squeeze.command.position = 0.0;
00158     squeeze.command.max_effort = 200.0;  // Close gently
00159     
00160     ROS_INFO("Sending squeeze goal");
00161     if (gripper_client_->sendGoalAndWait(squeeze,ros::Duration(5.0),ros::Duration(3.0)) == actionlib::SimpleClientGoalState::SUCCEEDED)
00162       ROS_INFO("The gripper closed!");
00163     else
00164       ROS_INFO("The gripper failed to close.");
00165     gripper_client_->sendGoal(squeeze);
00166   }
00167 };
00168 
00169 int main(int argc, char** argv)
00170 {
00171   // Init the ROS node
00172   ros::init(argc, argv, "robot_driver");
00173   ros::NodeHandle rh("");
00174   ros::Duration(1e-9).sleep();
00175 
00176   // clear object wrench
00177   ros::service::waitForService("/gazebo/clear_body_wrenches");
00178   ros::ServiceClient clear_client = rh.serviceClient<gazebo_msgs::BodyRequest>("/gazebo/clear_body_wrenches");
00179   gazebo_msgs::BodyRequest body_request;
00180   body_request.request.body_name = "object_1::coke_can";
00181   clear_client.call(body_request);
00182 
00183   // open gripper
00184   Gripper r_gripper("r_gripper_controller/gripper_action");
00185   Gripper l_gripper("l_gripper_controller/gripper_action");
00186   r_gripper.open();
00187   l_gripper.open();
00188 
00189   // stop arm controller
00190   std::string switch_controller_srv_name = "/pr2_controller_manager/switch_controller";
00191   ros::service::waitForService(switch_controller_srv_name);
00192   ros::ServiceClient switch_controller_client = rh.serviceClient<pr2_mechanism_msgs::SwitchController>(switch_controller_srv_name);
00193   pr2_mechanism_msgs::SwitchController switch_controller;
00194   switch_controller.request.start_controllers.clear();
00195   switch_controller.request.stop_controllers.push_back("r_arm_controller");
00196   switch_controller.request.stop_controllers.push_back("l_arm_controller");
00197   switch_controller.request.strictness = pr2_mechanism_msgs::SwitchControllerRequest::STRICT;
00198   switch_controller_client.call(switch_controller);
00199   
00200   sleep(3); // for debug viewing
00201 
00202   // pause simulation
00203   std::string psn = "/gazebo/pause_physics";
00204   ros::service::waitForService(psn);
00205   ros::ServiceClient ps_client = rh.serviceClient<std_srvs::Empty>(psn);
00206   std_srvs::Empty ps;
00207   ps_client.call(ps);
00208 
00209   // set arm model configuration
00210   /*********************************************************************************/
00211   /* be sure to Stay away from joint's soft limits, or you will get a jumpy robot! */
00212   /*********************************************************************************/
00213   std::string smcn = "/gazebo/set_model_configuration";
00214   ros::service::waitForService(smcn);
00215   ros::ServiceClient smc_client = rh.serviceClient<gazebo_msgs::SetModelConfiguration>(smcn);
00216   gazebo_msgs::SetModelConfiguration smc;
00217   smc.request.model_name = "pr2";
00218   smc.request.urdf_param_name = "robot_description";
00219   smc.request.joint_names.push_back("l_upper_arm_roll_joint");
00220   smc.request.joint_names.push_back("l_shoulder_pan_joint");
00221   smc.request.joint_names.push_back("l_shoulder_lift_joint");
00222   smc.request.joint_names.push_back("l_forearm_roll_joint");
00223   smc.request.joint_names.push_back("l_elbow_flex_joint");
00224   smc.request.joint_names.push_back("l_wrist_flex_joint");
00225   smc.request.joint_names.push_back("l_wrist_roll_joint");
00226   smc.request.joint_positions.push_back(3.1);
00227   smc.request.joint_positions.push_back(1.0);
00228   smc.request.joint_positions.push_back(-0.2);
00229   smc.request.joint_positions.push_back(0);
00230   smc.request.joint_positions.push_back(-0.4);
00231   smc.request.joint_positions.push_back(-1.0);
00232   smc.request.joint_positions.push_back(0);
00233   smc.request.joint_names.push_back("r_upper_arm_roll_joint");
00234   smc.request.joint_names.push_back("r_shoulder_pan_joint");
00235   smc.request.joint_names.push_back("r_shoulder_lift_joint");
00236   smc.request.joint_names.push_back("r_forearm_roll_joint");
00237   smc.request.joint_names.push_back("r_elbow_flex_joint");
00238   smc.request.joint_names.push_back("r_wrist_flex_joint");
00239   smc.request.joint_names.push_back("r_wrist_roll_joint");
00240   smc.request.joint_positions.push_back(0.1);
00241   smc.request.joint_positions.push_back(-1.0);
00242   smc.request.joint_positions.push_back(-0.2);
00243   smc.request.joint_positions.push_back(0.0);
00244   smc.request.joint_positions.push_back(-0.2);
00245   smc.request.joint_positions.push_back(-0.2);
00246   smc.request.joint_positions.push_back(0.0);
00247   smc_client.call(smc);
00248   
00249   sleep(3); // for debug viewing
00250 
00251 /* arm waving test */
00252   // pause simulation
00253   std::string upsn = "/gazebo/unpause_physics";
00254   ros::service::waitForService(upsn);
00255   ros::ServiceClient ups_client = rh.serviceClient<std_srvs::Empty>(upsn);
00256   std_srvs::Empty ups;
00257   ups_client.call(ups);
00258 
00259   /*********************************************************************************/
00260   /* be sure to Stay away from joint's soft limits, or you will get a jumpy robot! */
00261   /*********************************************************************************/
00262   gazebo_msgs::SetModelConfiguration smc1;
00263   smc1.request.model_name = "pr2";
00264   smc1.request.urdf_param_name = "robot_description";
00265   smc1.request.joint_names.clear();
00266   smc1.request.joint_positions.clear();
00267 
00268   smc1.request.joint_names.push_back("l_shoulder_pan_joint");
00269   smc1.request.joint_names.push_back("l_shoulder_lift_joint");
00270   smc1.request.joint_names.push_back("l_upper_arm_roll_joint");
00271   smc1.request.joint_names.push_back("l_forearm_roll_joint");
00272   smc1.request.joint_names.push_back("l_elbow_flex_joint");
00273   smc1.request.joint_names.push_back("l_wrist_flex_joint");
00274   smc1.request.joint_names.push_back("l_wrist_roll_joint");
00275   smc1.request.joint_positions.push_back(1.0);
00276   smc1.request.joint_positions.push_back(-0.2);
00277   smc1.request.joint_positions.push_back(3.1);
00278   smc1.request.joint_positions.push_back(0.0);
00279   smc1.request.joint_positions.push_back(-1.7);
00280   smc1.request.joint_positions.push_back(-0.2);
00281   smc1.request.joint_positions.push_back(0.0);
00282   smc1.request.joint_names.push_back("r_shoulder_pan_joint");
00283   smc1.request.joint_names.push_back("r_shoulder_lift_joint");
00284   smc1.request.joint_names.push_back("r_upper_arm_roll_joint");
00285   smc1.request.joint_names.push_back("r_forearm_roll_joint");
00286   smc1.request.joint_names.push_back("r_elbow_flex_joint");
00287   smc1.request.joint_names.push_back("r_wrist_flex_joint");
00288   smc1.request.joint_names.push_back("r_wrist_roll_joint");
00289   smc1.request.joint_positions.push_back(-1.0);
00290   smc1.request.joint_positions.push_back(-0.2);
00291   smc1.request.joint_positions.push_back(-3.0);
00292   smc1.request.joint_positions.push_back(0.0);
00293   smc1.request.joint_positions.push_back(-1.7);
00294   smc1.request.joint_positions.push_back(-0.2);
00295   smc1.request.joint_positions.push_back(0.0);
00296   smc_client.call(smc1);
00297 
00298   sleep(3); // for debug viewing
00299 
00300   // start arm controller
00301   switch_controller.request.start_controllers.push_back("r_arm_controller");
00302   switch_controller.request.start_controllers.push_back("l_arm_controller");
00303   switch_controller.request.stop_controllers.clear();
00304   switch_controller_client.call(switch_controller);
00305 
00306   //sleep(3); // for debug viewing
00307 
00308   // reset simulation
00309   std::string rsn = "/gazebo/reset_simulation";
00310   ros::service::waitForService(rsn);
00311   ros::ServiceClient rs_client = rh.serviceClient<std_srvs::Empty>(rsn);
00312   std_srvs::Empty rs;
00313   rs_client.call(rs);
00314 
00315   r_gripper.open();
00316   l_gripper.open();
00317 
00318 /*
00319   RobotArm arm;
00320 
00321   // Start the trajectory
00322   arm.startTrajectory(arm.armExtensionTrajectory());
00323 
00324   //sleep(5); // FIXME: seems startTrajectory finishes without waiting?
00325 
00326   // close gripper
00327   gripper.close();
00328 */
00329 
00330 /*
00331   // pull object down
00332   ros::ServiceClient wrench_client = rh.serviceClient<gazebo_msgs::ApplyBodyWrench>("/gazebo/apply_body_wrench");
00333   gazebo_msgs::ApplyBodyWrench wrench;
00334   //wrench.request.body_name       = "object_1::object_1_base_link";
00335   //wrench.request.reference_frame = "object_1::object_1_base_link";
00336   wrench.request.body_name       = "object_1::coke_can";
00337   wrench.request.reference_frame = "object_1::coke_can";
00338   wrench.request.wrench.force.z = -25.0;
00339   wrench.request.start_time = ros::Time(0.0);
00340   wrench.request.duration = ros::Duration(-1.0);
00341   wrench_client.call(wrench);
00342 */
00343 
00344 
00345 
00346 }
00347 


pr2_arm_gazebo
Author(s): John Hsu
autogenerated on Mon Jan 6 2014 12:02:20