00001 
00002 
00003 
00004 
00005 
00006 
00007 #include <pen_gripper/pen_gripper.h>
00008 #include <tf/transform_datatypes.h>
00009 #define PI 3.1415926536
00010 #include <string>
00011 using namespace std;
00012 
00013 bool point_sort_x(pcl::PointXYZ a, pcl::PointXYZ b) {
00014         return a.x > b.x;
00015 }
00016 bool point_sort_y(pcl::PointXYZ a, pcl::PointXYZ b) {
00017         return a.y > b.y;
00018 }
00019 bool point_sort_z(pcl::PointXYZ a, pcl::PointXYZ b) {
00020         return a.z > b.z;
00021 }
00022 
00023 PenGripper::~PenGripper() {
00024         delete head;
00025 
00026         delete arm_l;
00027         delete arm_r;
00028 
00029         delete GripperRight;
00030         delete GripperLeft;
00031 }
00032 
00033 PenGripper::PenGripper(ros::NodeHandle& nh) {
00034 
00035         n = &nh;
00036 
00037         
00038         head = new actionlib::SimpleActionClient<
00039                         pr2_controllers_msgs::PointHeadAction>(
00040                         "/head_traj_controller/point_head_action", true);
00041 
00042         
00043         arm_r = new EECartImpedArm("r_arm_cart_imped_controller");
00044         arm_l = new EECartImpedArm("l_arm_cart_imped_controller");
00045 
00046         
00047         r_start_pos.x = 0.0;
00048         r_start_pos.y = -0.5;
00049         r_start_pos.z = 0.3;
00050 
00051         
00052         l_start_pos.x = 0.0;
00053         l_start_pos.y = 0.5;
00054         l_start_pos.z = 0.3;
00055 
00056         
00057         GripperRight = new actionlib::SimpleActionClient<
00058                         pr2_controllers_msgs::Pr2GripperCommandAction>(
00059                         "r_gripper_controller/gripper_action", true);
00060         GripperLeft = new actionlib::SimpleActionClient<
00061                         pr2_controllers_msgs::Pr2GripperCommandAction>(
00062                         "l_gripper_controller/gripper_action", true);
00063 
00064         
00065         while (!GripperRight->waitForServer(ros::Duration(5.0))) {
00066                 ROS_INFO("Waiting for the right gripper action server to come up");
00067         }
00068 
00069         
00070         while (!GripperRight->waitForServer(ros::Duration(5.0))) {
00071                 ROS_INFO("Waiting for the left gripper action server to come up");
00072         }
00073 
00074         
00075         status_clt = n->serviceClient < portrait_robot_msgs::alubsc_status_msg
00076                         > ("/alubsc/status_msg");
00077         
00078         pcl_sub = n->subscribe("/narrow_stereo_textured/points2", 1,
00079                         &PenGripper::narrow_stereo_cb, this);
00080         
00081         pcl_pub = n->advertise < sensor_msgs::PointCloud2
00082                         > ("/filtered_pointcloud", 1);
00083 
00084         
00085         marker_middle_pub = n->advertise < visualization_msgs::Marker
00086                         > ("visualization_middle", 1);
00087 
00088         
00089         status.request.ID = 1;
00090 
00091         
00092         red_pen.isReady = false;
00093 
00094         
00095         success = false;
00096         
00097         abort = true;
00098 }
00099 
00100 void PenGripper::open_gripper(const string gripper) {
00101 
00102         if (gripper == "right") {
00103                 ROS_INFO("open the right gripper");
00104                 status.request.message = "open the right gripper";
00105                 status_clt.call(status);
00106 
00107                 pr2_controllers_msgs::Pr2GripperCommandGoal open;
00108                 open.command.position = 0.08;
00109                 open.command.max_effort = -1.0; 
00110 
00111                 
00112                 GripperRight->sendGoal(open);
00113                 GripperRight->waitForResult();
00114 
00115                 if (GripperRight->getState()
00116                                 == actionlib::SimpleClientGoalState::SUCCEEDED) {
00117                         ROS_INFO("The right gripper is open");
00118                         status.request.message = "The right gripper is open.";
00119                         status_clt.call(status);
00120                 } else {
00121                         ROS_INFO("The right gripper failed to open.");
00122                         status.request.message = "The right gripper failed to open.";
00123                         status_clt.call(status);
00124                 }
00125         }
00126 
00127         if (gripper == "left") {
00128                 ROS_INFO("open the left gripper");
00129                 status.request.message = "open the left gripper";
00130                 status_clt.call(status);
00131 
00132                 pr2_controllers_msgs::Pr2GripperCommandGoal open;
00133                 open.command.position = 0.08;
00134                 open.command.max_effort = -1.0; 
00135 
00136                 
00137                 GripperLeft->sendGoal(open);
00138                 GripperLeft->waitForResult();
00139 
00140                 if (GripperLeft->getState()
00141                                 == actionlib::SimpleClientGoalState::SUCCEEDED) {
00142                         ROS_INFO("The left gripper open");
00143                         status.request.message = "The left gripper is open.";
00144                         status_clt.call(status);
00145                 } else {
00146                         ROS_INFO("The left gripper failed to open.");
00147                         status.request.message = "The left gripper failed to open.";
00148                         status_clt.call(status);
00149                 }
00150         }
00151 
00152 }
00153 
00154 void PenGripper::close_gripper(const string gripper) {
00155 
00156         if (gripper == "right") {
00157 
00158                 ROS_INFO("close the right gripper");
00159 
00160                 status.request.message = "close the right gripper";
00161                 status_clt.call(status);
00162 
00163                 pr2_controllers_msgs::Pr2GripperCommandGoal squeeze;
00164                 squeeze.command.position = 0.0;
00165                 squeeze.command.max_effort = 50.0; 
00166 
00167                 
00168                 GripperRight->sendGoal(squeeze);
00169                 GripperRight->waitForResult();
00170                 if (GripperRight->getState()
00171                                 == actionlib::SimpleClientGoalState::SUCCEEDED) {
00172                         ROS_INFO("The right gripper is closed");
00173                         status.request.message = "The right gripper is closed";
00174                         status_clt.call(status);
00175                 } else {
00176                         ROS_INFO("The right gripper failed to close.");
00177                         status.request.message = "The right gripper failed to close";
00178                         status_clt.call(status);
00179                 }
00180         }
00181 
00182         if (gripper == "left") {
00183 
00184                 ROS_INFO("close the left gripper");
00185 
00186                 status.request.message = "close the left gripper";
00187                 status_clt.call(status);
00188 
00189                 pr2_controllers_msgs::Pr2GripperCommandGoal squeeze;
00190                 squeeze.command.position = 0.0;
00191                 squeeze.command.max_effort = 50.0; 
00192 
00193                 
00194                 GripperLeft->sendGoal(squeeze);
00195                 GripperLeft->waitForResult();
00196                 if (GripperLeft->getState()
00197                                 == actionlib::SimpleClientGoalState::SUCCEEDED) {
00198                         ROS_INFO("The left gripper is closed");
00199                         status.request.message = "The left gripper is closed";
00200                         status_clt.call(status);
00201                 } else {
00202                         ROS_INFO("The left gripper failed to close.");
00203                         status.request.message = "The left gripper failed to close";
00204                         status_clt.call(status);
00205                 }
00206         }
00207 
00208 }
00209 
00210 void PenGripper::take_start_position() {
00211 
00212         ROS_INFO("take start position");
00213         status.request.message = "take start position";
00214         status_clt.call(status);
00215 
00216         
00217         ROS_INFO("set the head posture");
00218         status.request.message = "set the head posture";
00219         status_clt.call(status);
00220 
00221         
00222         while (!head->waitForServer(ros::Duration(5.0))) {
00223                 ROS_INFO("Waiting for the point_head_action server to come up");
00224         }
00225 
00226         
00227         pr2_controllers_msgs::PointHeadGoal goal;
00228 
00229         
00230         geometry_msgs::PointStamped point;
00231         
00232         point.header.frame_id = "/torso_lift_link";
00233         
00234         point.point.x = 0.6;
00235         point.point.y = 0.0;
00236         point.point.z = -0.4;
00237         goal.target = point;
00238 
00239         
00240         
00241         goal.pointing_frame = "high_def_frame";
00242 
00243         
00244         goal.min_duration = ros::Duration(5.0);
00245 
00246         
00247         goal.max_velocity = 1.0;
00248 
00249         
00250         head->sendGoal(goal);
00251 
00252         
00253         head->waitForResult(ros::Duration(2));
00254 
00255 
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263         
00264         tf::Quaternion up = tf::createQuaternionFromRPY(0.0, -0.5 * PI, PI);
00265 
00266         
00267         btQuaternion down = tf::createQuaternionFromRPY(0.0, 0.5 * PI, PI);
00268 
00269         
00270         ee_cart_imped_msgs::EECartImpedGoal traj_r, traj_l;
00271 
00272         
00273         EECartImpedArm::addTrajectoryPoint(traj_l, l_start_pos.x, l_start_pos.y,
00274                         l_start_pos.z, up.x(), up.y(), up.z(), up.w(), 1000, 1000, 1000, 30,
00275                         30, 30, false, false, false, false, false, false, 6,
00276                         "/torso_lift_link");
00277 
00278         
00279         EECartImpedArm::addTrajectoryPoint(traj_r, r_start_pos.x, r_start_pos.y,
00280                         r_start_pos.z, up.x(), up.y(), up.z(), up.w(), 1000, 1000, 1000, 30,
00281                         30, 30, false, false, false, false, false, false, 6,
00282                         "/torso_lift_link");
00283 
00284         ROS_INFO("set the arm position");
00285         status.request.message = "set the arm position";
00286         status_clt.call(status);
00287 
00288         
00289         arm_l->startTrajectory(traj_l);
00290         arm_r->startTrajectory(traj_r);
00291         
00292         arm_l->stopTrajectory();
00293         arm_r->stopTrajectory();
00294 
00295 }
00296 
00297 void PenGripper::narrow_stereo_cb(
00298                 const sensor_msgs::PointCloud2ConstPtr& cloud2_msg_ptr) {
00299 
00300 
00301         ROS_INFO("the pen is ready now");
00302         status.request.message = "the pen is ready now";
00303         status_clt.call(status);
00304 
00305         red_pen.isReady = true;
00307 
00308 
00309 
00310 
00311 
00312 
00313 
00314 
00315 
00316 
00317 
00318 
00319 
00320 
00321 
00322 
00323 
00324 
00325 
00326 
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336 
00337 
00338 
00339 
00340 
00341 
00342 
00343 
00344 
00345 
00346 
00347 
00348 
00349 
00350 
00351 
00352 
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 
00379 
00380 
00381 
00382 
00383 
00384 
00385 
00386 
00387 
00388 
00389 
00390 
00391 
00392 
00393 
00394 
00395 
00396 
00397 
00398 
00399 
00400 
00401 
00402 
00403 
00404 
00405 
00406 
00407 
00408 
00409 
00410 
00411 
00412 
00413 
00414 
00415 
00416 
00417 
00418 
00419 
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 
00494 
00495 
00496 
00497 
00498 
00499 
00500 
00501 
00502 
00503 
00504 
00505 
00506 
00507 
00508 
00509 
00510 
00511 
00512 
00513 
00514 
00515 
00516 
00517 
00518 
00519 
00520 
00521 
00522 
00523 
00524 
00525 
00526 }
00527 
00528 void PenGripper::grip_pen()
00529 
00530 {
00531 
00532         success = true;
00533         
00534         if (!red_pen.isReady) {
00535                 ROS_INFO("pen is not ready");
00536                 status.request.message = "pen is not ready";
00537                 status_clt.call(status);
00538 
00539         } else {
00540                 
00541                 this->open_gripper("right");
00542 
00543                 ROS_INFO("grip the pen");
00544                 status.request.message = "grip the pen";
00545                 status_clt.call(status);
00546 
00547                 ee_cart_imped_msgs::EECartImpedGoal traj_r, end;
00548 
00549                 
00550                 tf::Quaternion grip_forward = tf::createQuaternionFromRPY(0.0, 0.0, PI);
00551                 tf::Quaternion grip_left = tf::createQuaternionFromRPY(0.5 * PI, 0.0, PI);
00552 
00553                 
00554                 EECartImpedArm::addTrajectoryPoint(traj_r, double(red_pen.middle.x),
00555                                 double(red_pen.middle.y - 0.1), double(red_pen.middle.z),
00556                                 grip_left.x(), grip_left.y(), grip_left.z(), grip_left.w(),
00557                                 1000, 1000, 1000, 30, 30, 30, false, false, false, false, false,
00558                                 false, 8, "/torso_lift_link");
00559                 
00560                 EECartImpedArm::addTrajectoryPoint(traj_r, double(red_pen.middle.x),
00561                                 double(red_pen.middle.y + 0.04),
00562                                 double(red_pen.middle.z - 0.01), grip_left.x(), grip_left.y(),
00563                                 grip_left.z(), grip_left.w(), 1000, 1000, 1000, 30, 30, 30,
00564                                 false, false, false, false, false, false, 12,
00565                                 "/torso_lift_link");
00566 
00567                 
00568                 arm_r->startTrajectory(traj_r);
00569 
00570                 
00571                 this->close_gripper("right");
00572 
00573                 
00574                 EECartImpedArm::addTrajectoryPoint(end, double(r_start_pos.x),
00575                                 double(r_start_pos.y), double(r_start_pos.z), grip_left.x(),
00576                                 grip_left.y(), grip_left.z(), grip_left.w(), 1000, 1000, 1000,
00577                                 30, 30, 30, false, false, false, false, false, false, 8,
00578                                 "/torso_lift_link");
00579                 
00580                 arm_r->startTrajectory(end);
00581 
00582                 
00583                 success = true;
00584 
00585         }
00586 
00587 }
00588 
00589 void PenGripper::reset() {
00590         
00591         red_pen.isReady = true;
00592         success = true;
00593         abort = true;
00594 
00595 
00596 
00597 
00598 }
00599 
00600 bool PenGripper::handle_req(
00601                 portrait_robot_msgs::alubsc_node_instr::Request &req,
00602                 portrait_robot_msgs::alubsc_node_instr::Response &res) {
00603 
00604         
00605         if (!req.abort) {
00606                 ROS_INFO("Incomming Request...");
00607                 status.request.message = "Incomming Request...";
00608                 status_clt.call(status);
00609 
00610                 
00611                 abort = false;
00612                 res.success = true;
00613                 return true;
00614         } else {
00615                 ROS_INFO("reset...");
00616                 status.request.message = "reset...";
00617                 status_clt.call(status);
00618 
00619                 
00620                 this->reset();
00621                 res.success = true;
00622                 return true;
00623 
00624         }
00625 }
00626