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/ApplyBodyWrench.h>
00006
00007 typedef actionlib::SimpleActionClient< pr2_controllers_msgs::JointTrajectoryAction > TrajClient;
00008
00009 class RobotArm
00010 {
00011 private:
00012
00013
00014 TrajClient* traj_client_;
00015
00016 public:
00018 RobotArm()
00019 {
00020
00021 traj_client_ = new TrajClient("r_arm_controller/joint_trajectory_action", true);
00022
00023
00024 while(!traj_client_->waitForServer(ros::Duration(5.0))){
00025 ROS_INFO("Waiting for the joint_trajectory_action server");
00026 }
00027 }
00028
00030 ~RobotArm()
00031 {
00032 delete traj_client_;
00033 }
00034
00036 void startTrajectory(pr2_controllers_msgs::JointTrajectoryGoal goal)
00037 {
00038
00039
00040 ros::Duration wait_time(20.0);
00041 actionlib::SimpleClientGoalState state = traj_client_->sendGoalAndWait(goal,wait_time,wait_time);
00042 if (state == actionlib::SimpleClientGoalState::SUCCEEDED)
00043 ROS_INFO("The arm traj finished with state [%s]",state.toString().c_str());
00044 else
00045 ROS_INFO("The arm traj failed with state [%s]",state.toString().c_str());
00046 }
00047
00049
00054 pr2_controllers_msgs::JointTrajectoryGoal armExtensionTrajectory()
00055 {
00056
00057 pr2_controllers_msgs::JointTrajectoryGoal goal;
00058
00059
00060 goal.trajectory.joint_names.push_back("r_shoulder_pan_joint");
00061 goal.trajectory.joint_names.push_back("r_shoulder_lift_joint");
00062 goal.trajectory.joint_names.push_back("r_upper_arm_roll_joint");
00063 goal.trajectory.joint_names.push_back("r_elbow_flex_joint");
00064 goal.trajectory.joint_names.push_back("r_forearm_roll_joint");
00065 goal.trajectory.joint_names.push_back("r_wrist_flex_joint");
00066 goal.trajectory.joint_names.push_back("r_wrist_roll_joint");
00067
00068
00069 goal.trajectory.points.resize(2);
00070
00071
00072
00073 int ind = 0;
00074 goal.trajectory.points[ind].positions.resize(7);
00075 goal.trajectory.points[ind].positions[0] = 0.0;
00076 goal.trajectory.points[ind].positions[1] = 0.0;
00077 goal.trajectory.points[ind].positions[2] = 0.0;
00078 goal.trajectory.points[ind].positions[3] = 0.0;
00079 goal.trajectory.points[ind].positions[4] = 0.0;
00080 goal.trajectory.points[ind].positions[5] = 0.0;
00081 goal.trajectory.points[ind].positions[6] = 0.0;
00082
00083 goal.trajectory.points[ind].time_from_start = ros::Duration(3.0);
00084
00085
00086
00087 ind += 1;
00088 goal.trajectory.points[ind].positions.resize(7);
00089 goal.trajectory.points[ind].positions[0] = 0.0;
00090 goal.trajectory.points[ind].positions[1] = 0.25;
00091 goal.trajectory.points[ind].positions[2] = 0.0;
00092 goal.trajectory.points[ind].positions[3] = 0.0;
00093 goal.trajectory.points[ind].positions[4] = 0.0;
00094 goal.trajectory.points[ind].positions[5] = 0.0;
00095 goal.trajectory.points[ind].positions[6] = 0.0;
00096
00097 goal.trajectory.points[ind].time_from_start = ros::Duration(6.0);
00098
00099
00100 return goal;
00101 }
00102
00104 actionlib::SimpleClientGoalState getState()
00105 {
00106 return traj_client_->getState();
00107 }
00108
00109 };
00110
00111
00112 typedef actionlib::SimpleActionClient<pr2_controllers_msgs::Pr2GripperCommandAction> GripperClient;
00113
00114 class Gripper{
00115 private:
00116 GripperClient* gripper_client_;
00117
00118 public:
00119
00120 Gripper(){
00121
00122
00123
00124 gripper_client_ = new GripperClient("r_gripper_controller/gripper_action", true);
00125
00126
00127 while(!gripper_client_->waitForServer(ros::Duration(30.0))){
00128 ROS_INFO("Waiting for the r_gripper_controller/gripper_action action server to come up");
00129 }
00130 }
00131
00132 ~Gripper(){
00133 delete gripper_client_;
00134 }
00135
00136
00137 void open(){
00138 pr2_controllers_msgs::Pr2GripperCommandGoal open;
00139 open.command.position = 0.08;
00140 open.command.max_effort = 100.0;
00141
00142 ROS_INFO("Sending open goal");
00143 if (gripper_client_->sendGoalAndWait(open,ros::Duration(10.0),ros::Duration(5.0)) == actionlib::SimpleClientGoalState::SUCCEEDED)
00144 ROS_INFO("The gripper opened!");
00145 else
00146 ROS_INFO("The gripper failed to open.");
00147 }
00148
00149
00150 void close(){
00151 pr2_controllers_msgs::Pr2GripperCommandGoal squeeze;
00152 squeeze.command.position = 0.0;
00153 squeeze.command.max_effort = 200.0;
00154
00155 ROS_INFO("Sending squeeze goal");
00156 if (gripper_client_->sendGoalAndWait(squeeze,ros::Duration(10.0),ros::Duration(5.0)) == actionlib::SimpleClientGoalState::SUCCEEDED)
00157 ROS_INFO("The gripper closed!");
00158 else
00159 ROS_INFO("The gripper failed to close.");
00160 gripper_client_->sendGoal(squeeze);
00161 }
00162 };
00163
00164 int main(int argc, char** argv)
00165 {
00166
00167 ros::init(argc, argv, "robot_driver");
00168 ros::NodeHandle rh("");
00169 ros::Duration(1e-9).sleep();
00170
00171 Gripper gripper;
00172 RobotArm arm;
00173
00174
00175 gripper.open();
00176
00177
00178 arm.startTrajectory(arm.armExtensionTrajectory());
00179
00180
00181
00182
00183 gripper.close();
00184
00185
00186 ros::ServiceClient client = rh.serviceClient<gazebo::ApplyBodyWrench>("/gazebo/apply_body_wrench");
00187 gazebo::ApplyBodyWrench wrench;
00188
00189
00190 wrench.request.body_name = "object_1::coke_can";
00191 wrench.request.reference_frame = "world";
00192 wrench.request.wrench.force.z = -20.0;
00193 wrench.request.start_time = ros::Time(0.0);
00194 wrench.request.duration = ros::Duration(-1.0);
00195 client.call(wrench);
00196
00197
00198
00199 }
00200