action_push_door.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  * 
00004  *  Copyright (c) 2008, Willow Garage, Inc.
00005  *  All rights reserved.
00006  * 
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  * 
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  * 
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Wim Meeussen */
00036 
00037 #include <pr2_doors_common/door_functions.h>
00038 #include <geometry_msgs/PoseStamped.h>
00039 #include "pr2_doors_actions/action_push_door.h"
00040 
00041 using namespace tf;
00042 using namespace KDL;
00043 using namespace ros;
00044 using namespace std;
00045 using namespace door_handle_detector;
00046 using namespace pr2_doors_common;
00047 using namespace actionlib;
00048 
00049 static const string fixed_frame = "odom_combined";
00050 static const double push_dist = 0.65;
00051 static const double push_vel  = 10.0 * M_PI/180.0;  // 10 [deg/sec]
00052 
00053 
00054 
00055 PushDoorAction::PushDoorAction(tf::TransformListener& tf) : 
00056   tf_(tf),
00057   action_server_(ros::NodeHandle(), 
00058                  "push_door", 
00059                  boost::bind(&PushDoorAction::execute, this, _1))
00060 {
00061   pose_pub_ = node_.advertise<geometry_msgs::PoseStamped>("r_arm_constraint_cartesian_pose_controller/command",20);
00062 };
00063 
00064 
00065 PushDoorAction::~PushDoorAction()
00066 {};
00067 
00068 
00069 
00070 void PushDoorAction::execute(const door_msgs::DoorGoalConstPtr& goal)
00071 {
00072   ROS_INFO("PushDoorAction: execute");
00073  
00074   // transform door message to time fixed frame
00075   door_msgs::Door goal_tr;
00076   if (!transformTo(tf_, fixed_frame, goal->door, goal_tr, fixed_frame)){
00077     ROS_ERROR("Could not tranform door message from '%s' to '%s' at time %f",
00078               goal->door.header.frame_id.c_str(), fixed_frame.c_str(), goal->door.header.stamp.toSec());
00079     action_server_.setPreempted();
00080     return;
00081   }
00082 
00083   // start monitoring gripper pose
00084   pose_state_received_ = false;
00085   Subscriber sub = node_.subscribe("r_arm_constraint_cartesian_pose_controller/state/pose", 1,  &PushDoorAction::poseCallback, this);
00086   Duration timeout = Duration().fromSec(3.0);
00087   Duration poll = Duration().fromSec(0.1);
00088   Time start_time = ros::Time::now();
00089   while (!pose_state_received_){
00090     if (start_time + timeout < ros::Time::now()){
00091       ROS_ERROR("failed to receive pose state");
00092       action_server_.setPreempted();
00093       return;
00094     }
00095     poll.sleep();
00096   }
00097 
00098   // angle step
00099   Duration sleep_time(0.01);
00100   double angle_step = 0;
00101   StampedTransform shoulder_pose; tf_.lookupTransform(goal_tr.header.frame_id, "r_shoulder_pan_link", Time(), shoulder_pose);
00102   if (goal_tr.rot_dir == door_msgs::Door::ROT_DIR_CLOCKWISE)
00103     angle_step = -push_vel*sleep_time.toSec();
00104   else if (goal_tr.rot_dir == door_msgs::Door::ROT_DIR_COUNTERCLOCKWISE)
00105     angle_step = push_vel*sleep_time.toSec();
00106   else{
00107     ROS_ERROR("door rotation direction not specified");
00108     action_server_.setPreempted();
00109     return;
00110   }
00111 
00112   // push door
00113   Stamped<Pose> gripper_pose;
00114   geometry_msgs::PoseStamped gripper_pose_msg;
00115   double angle = getNearestDoorAngle(shoulder_pose, goal_tr, 0.75, push_dist);
00116   while (!action_server_.isPreemptRequested()){
00117     sleep_time.sleep();
00118 
00119     // define griper pose
00120     gripper_pose = getGripperPose(goal_tr, angle, push_dist);
00121     gripper_pose.stamp_ = Time::now();
00122     poseStampedTFToMsg(gripper_pose, gripper_pose_msg);
00123     pose_pub_.publish(gripper_pose_msg);
00124 
00125     // increase angle when pose error is small enough
00126     boost::mutex::scoped_lock lock(pose_mutex_);
00127     if (fabs(angle) < M_PI/2.0)// && (gripper_pose.getOrigin() - pose_state_.getOrigin()).length() < 0.5)
00128       angle += angle_step;
00129   }
00130   ROS_ERROR("PushDoorAction: preempted");
00131   action_server_.setPreempted();
00132 }
00133 
00134 
00135 void PushDoorAction::poseCallback(const PoseConstPtr& pose)
00136 {
00137   boost::mutex::scoped_lock lock(pose_mutex_);
00138   poseStampedMsgToTF(*pose, pose_state_);
00139   pose_state_received_ = true;
00140 }
00141 


pr2_doors_actions
Author(s): Wim Meeussen
autogenerated on Wed Dec 11 2013 14:17:44