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 #include <pr2_mechanism_msgs/ListControllers.h>
00011
00012 typedef actionlib::SimpleActionClient< pr2_controllers_msgs::JointTrajectoryAction > TrajClient;
00013
00014 class RobotArm
00015 {
00016 private:
00017
00018
00019 TrajClient* traj_client_;
00020
00021 public:
00023 RobotArm()
00024 {
00025
00026 traj_client_ = new TrajClient("r_arm_controller/joint_trajectory_action", true);
00027
00028
00029 while(!traj_client_->waitForServer(ros::Duration(5.0))){
00030 ROS_INFO("Waiting for the joint_trajectory_action server");
00031 }
00032 }
00033
00035 ~RobotArm()
00036 {
00037 delete traj_client_;
00038 }
00039
00041 void startTrajectory(pr2_controllers_msgs::JointTrajectoryGoal goal)
00042 {
00043
00044
00045 ros::Duration wait_time(20.0);
00046 actionlib::SimpleClientGoalState state = traj_client_->sendGoalAndWait(goal,wait_time,wait_time);
00047 if (state == actionlib::SimpleClientGoalState::SUCCEEDED)
00048 ROS_INFO("The arm traj finished with state [%s]",state.toString().c_str());
00049 else
00050 ROS_INFO("The arm traj failed with state [%s]",state.toString().c_str());
00051 }
00052
00054
00059 pr2_controllers_msgs::JointTrajectoryGoal armExtensionTrajectory()
00060 {
00061
00062 pr2_controllers_msgs::JointTrajectoryGoal goal;
00063
00064
00065 goal.trajectory.joint_names.push_back("r_shoulder_pan_joint");
00066 goal.trajectory.joint_names.push_back("r_shoulder_lift_joint");
00067 goal.trajectory.joint_names.push_back("r_upper_arm_roll_joint");
00068 goal.trajectory.joint_names.push_back("r_elbow_flex_joint");
00069 goal.trajectory.joint_names.push_back("r_forearm_roll_joint");
00070 goal.trajectory.joint_names.push_back("r_wrist_flex_joint");
00071 goal.trajectory.joint_names.push_back("r_wrist_roll_joint");
00072
00073
00074 goal.trajectory.points.resize(2);
00075
00076
00077
00078 int ind = 0;
00079 goal.trajectory.points[ind].positions.resize(7);
00080 goal.trajectory.points[ind].positions[0] = 0.0;
00081 goal.trajectory.points[ind].positions[1] = 0.0;
00082 goal.trajectory.points[ind].positions[2] = 0.0;
00083 goal.trajectory.points[ind].positions[3] = 0.0;
00084 goal.trajectory.points[ind].positions[4] = 0.0;
00085 goal.trajectory.points[ind].positions[5] = 0.0;
00086 goal.trajectory.points[ind].positions[6] = 0.0;
00087
00088 goal.trajectory.points[ind].time_from_start = ros::Duration(3.0);
00089
00090
00091
00092 ind += 1;
00093 goal.trajectory.points[ind].positions.resize(7);
00094 goal.trajectory.points[ind].positions[0] = 0.0;
00095 goal.trajectory.points[ind].positions[1] = 0.25;
00096 goal.trajectory.points[ind].positions[2] = 0.0;
00097 goal.trajectory.points[ind].positions[3] = 0.0;
00098 goal.trajectory.points[ind].positions[4] = 0.0;
00099 goal.trajectory.points[ind].positions[5] = 0.0;
00100 goal.trajectory.points[ind].positions[6] = 0.0;
00101
00102 goal.trajectory.points[ind].time_from_start = ros::Duration(6.0);
00103
00104
00105 return goal;
00106 }
00107
00109 actionlib::SimpleClientGoalState getState()
00110 {
00111 return traj_client_->getState();
00112 }
00113
00114 };
00115
00116 class PR2Controllers
00117 {
00118
00119 PR2Controllers()
00120 {
00121 }
00122
00123 ~PR2Controllers()
00124 {
00125 }
00126
00127 void waitForControllers()
00128 {
00129 }
00130
00131 void stopControllers()
00132 {
00133 }
00134
00135 void startControllers()
00136 {
00137 }
00138
00139
00140 };
00141
00142
00143
00144
00145 typedef actionlib::SimpleActionClient<pr2_controllers_msgs::Pr2GripperCommandAction> GripperClient;
00146
00147 class Gripper{
00148 private:
00149 GripperClient* gripper_client_;
00150
00151 public:
00152
00153 Gripper(std::string action_topic){
00154
00155
00156
00157 gripper_client_ = new GripperClient(action_topic, true);
00158
00159
00160 while(!gripper_client_->waitForServer(ros::Duration(60.0))){
00161 ROS_INFO("Waiting for the r_gripper_controller/gripper_action action server to come up");
00162 }
00163 }
00164
00165 ~Gripper(){
00166 delete gripper_client_;
00167 }
00168
00169
00170 void open(){
00171 pr2_controllers_msgs::Pr2GripperCommandGoal open;
00172 open.command.position = 0.08;
00173 open.command.max_effort = 150.0;
00174
00175 ROS_INFO("Sending open goal");
00176
00177 ROS_WARN("cancelling all gripper actionclient goals");
00178 gripper_client_->cancelAllGoals();
00179
00180 bool success = false;
00181 while (!success)
00182 {
00183 actionlib::SimpleClientGoalState state =
00184 gripper_client_->sendGoalAndWait(open,ros::Duration(50.0),ros::Duration(30.0));
00185 bool finished_before_timeout = gripper_client_->waitForResult(ros::Duration(50.0));
00186
00187 if (finished_before_timeout)
00188 {
00189 ROS_INFO("The gripper opened.");
00190 success = true;
00191 }
00192 else
00193 {
00194 ROS_WARN("The gripper failed to open [%s].",gripper_client_->getState().toString().c_str());
00195 }
00196
00197
00198
00199
00200 }
00201 }
00202
00203
00204 void close(){
00205 pr2_controllers_msgs::Pr2GripperCommandGoal squeeze;
00206 squeeze.command.position = 0.0;
00207 squeeze.command.max_effort = 50.0;
00208
00209 ROS_INFO("Sending squeeze goal");
00210 if (gripper_client_->sendGoalAndWait(squeeze,ros::Duration(5.0),ros::Duration(3.0))
00211 == actionlib::SimpleClientGoalState::SUCCEEDED)
00212 ROS_INFO("The gripper closed!");
00213 else
00214 ROS_INFO("The gripper failed to close.");
00215 gripper_client_->sendGoal(squeeze);
00216 }
00217 };
00218
00219 int main(int argc, char** argv)
00220 {
00221
00222 ros::init(argc, argv, "robot_driver");
00223 ros::NodeHandle rh("");
00224 ros::Duration(1e-9).sleep();
00225
00226
00227 std::string pause_srv_name = "/gazebo/pause_physics";
00228 ros::service::waitForService(pause_srv_name);
00229
00230 std::string unpause_srv_name = "/gazebo/unpause_physics";
00231 ros::service::waitForService(unpause_srv_name);
00232
00233 std::string clear_body_wrenches_srv_name = "/gazebo/clear_body_wrenches";
00234 ros::service::waitForService(clear_body_wrenches_srv_name);
00235
00236 Gripper r_gripper("r_gripper_controller/gripper_action");
00237 Gripper l_gripper("l_gripper_controller/gripper_action");
00238
00239 std::string switch_controller_srv_name = "/pr2_controller_manager/switch_controller";
00240 ros::service::waitForService(switch_controller_srv_name);
00241
00242 std::string list_controllers_srv_name = "/pr2_controller_manager/list_controllers";
00243 ros::service::waitForService(list_controllers_srv_name);
00244
00245
00246 ros::ServiceClient list_controller_client = rh.serviceClient<pr2_mechanism_msgs::ListControllers>(list_controllers_srv_name);
00247 pr2_mechanism_msgs::ListControllers list_controller;
00248 int controllers_count = 0;
00249 while (controllers_count != 8)
00250 {
00251 controllers_count = 0;
00252 list_controller_client.call(list_controller);
00253 for (unsigned int i = 0; i < list_controller.response.controllers.size() ; ++i)
00254 {
00255 std::string name = list_controller.response.controllers[i];
00256 std::string state = list_controller.response.state[i];
00257 if ((name == "r_gripper_controller" || name == "l_gripper_controller" ||
00258 name == "laser_tilt_controller" || name == "head_traj_controller" ||
00259 name == "r_arm_controller" || name == "l_arm_controller" ||
00260 name == "base_controller" || name == "torso_controller") && state == "running")
00261 controllers_count++;
00262 ROS_INFO("controller [%s] state [%s] count[%d]",name.c_str(), state.c_str(),controllers_count);
00263 }
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 r_gripper.open();
00276 r_gripper.open();
00277
00278
00279 ROS_INFO("stopping controllers");
00280
00281 ros::ServiceClient switch_controller_client = rh.serviceClient<pr2_mechanism_msgs::SwitchController>(switch_controller_srv_name);
00282 pr2_mechanism_msgs::SwitchController switch_controller;
00283 switch_controller.request.start_controllers.clear();
00284 switch_controller.request.stop_controllers.clear();
00285 switch_controller.request.stop_controllers.push_back("r_gripper_controller");
00286 switch_controller.request.stop_controllers.push_back("l_gripper_controller");
00287 switch_controller.request.stop_controllers.push_back("laser_tilt_controller");
00288 switch_controller.request.stop_controllers.push_back("head_traj_controller");
00289 switch_controller.request.stop_controllers.push_back("r_arm_controller");
00290 switch_controller.request.stop_controllers.push_back("l_arm_controller");
00291 switch_controller.request.stop_controllers.push_back("base_controller");
00292 switch_controller.request.stop_controllers.push_back("torso_controller");
00293 switch_controller.request.strictness = pr2_mechanism_msgs::SwitchControllerRequest::STRICT;
00294 switch_controller_client.call(switch_controller);
00295
00296 ROS_INFO("pausing physics");
00297
00298 ros::ServiceClient ps_client = rh.serviceClient<std_srvs::Empty>(pause_srv_name);
00299 std_srvs::Empty ps;
00300 ps_client.call(ps);
00301
00302 ROS_INFO("setting arm pose");
00303
00304 std::string smcn = "/gazebo/set_model_configuration";
00305 ros::service::waitForService(smcn);
00306 ros::ServiceClient smc_client = rh.serviceClient<gazebo_msgs::SetModelConfiguration>(smcn);
00307 gazebo_msgs::SetModelConfiguration smc;
00308 smc.request.model_name = "pr2";
00309 smc.request.urdf_param_name = "robot_description";
00310
00311 smc.request.joint_names.push_back("head_pan_joint");
00312 smc.request.joint_names.push_back("head_tilt_joint");
00313 smc.request.joint_names.push_back("laser_tilt_mount_joint");
00314
00315 smc.request.joint_positions.push_back(-0.04695624787873154);
00316 smc.request.joint_positions.push_back(0.9571995901909744);
00317 smc.request.joint_positions.push_back(1.0137128597170113);
00318
00319
00320
00321 smc.request.joint_names.push_back("r_upper_arm_roll_joint");
00322 smc.request.joint_names.push_back("r_shoulder_pan_joint");
00323 smc.request.joint_names.push_back("r_shoulder_lift_joint");
00324 smc.request.joint_names.push_back("r_forearm_roll_joint");
00325 smc.request.joint_names.push_back("r_elbow_flex_joint");
00326 smc.request.joint_names.push_back("r_wrist_flex_joint");
00327 smc.request.joint_names.push_back("r_wrist_roll_joint");
00328
00329 smc.request.joint_positions.push_back(-1.6399960594910876);
00330 smc.request.joint_positions.push_back(-0.3689082990354322);
00331 smc.request.joint_positions.push_back(0.3212309310513026);
00332 smc.request.joint_positions.push_back(-3.5471901137256694);
00333 smc.request.joint_positions.push_back(-1.459007345767927);
00334 smc.request.joint_positions.push_back(-0.6873569547257024);
00335 smc.request.joint_positions.push_back(-1.4355428273114548);
00336
00337
00338 smc.request.joint_names.push_back("l_upper_arm_roll_joint");
00339 smc.request.joint_names.push_back("l_shoulder_pan_joint");
00340 smc.request.joint_names.push_back("l_shoulder_lift_joint");
00341 smc.request.joint_names.push_back("l_forearm_roll_joint");
00342 smc.request.joint_names.push_back("l_elbow_flex_joint");
00343 smc.request.joint_names.push_back("l_wrist_flex_joint");
00344 smc.request.joint_names.push_back("l_wrist_roll_joint");
00345
00346 smc.request.joint_positions.push_back(1.6399982206352233);
00347 smc.request.joint_positions.push_back(2.1350018853307198);
00348 smc.request.joint_positions.push_back(-0.02000609892474703);
00349 smc.request.joint_positions.push_back(1.6399955487793614);
00350 smc.request.joint_positions.push_back(-2.070018727445361);
00351 smc.request.joint_positions.push_back(-1.6800004066137673);
00352 smc.request.joint_positions.push_back(1.3980012258720143);
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 smc_client.call(smc);
00379
00380
00381 ros::ServiceClient ups_client = rh.serviceClient<std_srvs::Empty>(unpause_srv_name);
00382 std_srvs::Empty ups;
00383 ups_client.call(ups);
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 ROS_INFO("restarting controllers");
00408
00409 switch_controller.request.start_controllers.clear();
00410 switch_controller.request.start_controllers.push_back("r_gripper_controller");
00411 switch_controller.request.start_controllers.push_back("l_gripper_controller");
00412 switch_controller.request.start_controllers.push_back("laser_tilt_controller");
00413 switch_controller.request.start_controllers.push_back("head_traj_controller");
00414 switch_controller.request.start_controllers.push_back("r_arm_controller");
00415 switch_controller.request.start_controllers.push_back("l_arm_controller");
00416 switch_controller.request.start_controllers.push_back("base_controller");
00417 switch_controller.request.start_controllers.push_back("torso_controller");
00418 switch_controller.request.stop_controllers.clear();
00419 switch_controller_client.call(switch_controller);
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492 }
00493