arm_navigation_utils.h
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  *********************************************************************/
00036 
00037 /* \author: Sachin Chitta */
00038 
00039 #include <ros/ros.h>
00040 #include <collision_environment_msgs/MakeStaticCollisionMapAction.h>
00041 #include <actionlib/client/simple_action_client.h>
00042 #include <geometric_shapes_msgs/Shape.h>
00043 #include <pr2_msgs/SetPeriodicCmd.h>
00044 #include <sensor_msgs/PointCloud.h>
00045 #include <tf/transform_datatypes.h>
00046 
00047 #include <urdf/model.h>
00048 
00049 namespace arm_navigation_utils
00050 {
00051 
00052 static const std::string MAKE_STATIC_COLLISION_MAP_ACTION_NAME = "make_static_collision_map";
00053 static const std::string TILT_LASER_PERIODIC_COMMAND_NAME = "laser_tilt_controller/set_periodic_cmd";
00054 
00055 class ArmNavigationUtils
00056 {
00057 public:
00058 
00059   ArmNavigationUtils() :
00060     root_nh_(""),
00061     make_static_collision_map_client_(root_nh_, MAKE_STATIC_COLLISION_MAP_ACTION_NAME, true)
00062   {
00063     while ( !ros::service::waitForService(TILT_LASER_PERIODIC_COMMAND_NAME, ros::Duration(2.0)) && 
00064             root_nh_.ok() ) 
00065     {
00066       ROS_INFO("Waiting for tilt laser periodic command service to come up");
00067     }
00068     tilt_laser_service_ = root_nh_.serviceClient<pr2_msgs::SetPeriodicCmd>(TILT_LASER_PERIODIC_COMMAND_NAME);
00069     
00070     while(!make_static_collision_map_client_.waitForServer(ros::Duration(2.0))
00071           && root_nh_.ok()){
00072       ROS_INFO("Waiting for the make static collision map action to come up");
00073     }
00074 
00075     //cribbed from motion planning laser settings
00076     laser_slow_period_ = 5;
00077     laser_slow_amplitude_ = 1.02;
00078     laser_slow_offset_ = .31;
00079     
00080     //cribbed from head cart tilt_laser_launch
00081     laser_fast_period_ = 2;
00082     laser_fast_amplitude_ = .6;
00083     laser_fast_offset_ = .25;       
00084   }
00085   ~ArmNavigationUtils(){}
00086 
00087   bool setLaserScan(bool fast) 
00088   {
00089     pr2_msgs::SetPeriodicCmd::Request req;
00090     pr2_msgs::SetPeriodicCmd::Response res;
00091     
00092     req.command.profile = "linear";
00093     if(fast) 
00094     {
00095       req.command.period = laser_fast_period_;
00096       req.command.amplitude = laser_fast_amplitude_;
00097       req.command.offset = laser_fast_offset_;
00098     } 
00099     else 
00100     {
00101       req.command.period = laser_slow_period_;
00102       req.command.amplitude = laser_slow_amplitude_;
00103       req.command.offset = laser_slow_offset_;
00104     }
00105     
00106     if(!tilt_laser_service_.call(req,res))
00107     {
00108       ROS_ERROR("Tilt laser service call failed.");
00109       return false;
00110     }
00111     return true;
00112   }
00113 
00114   bool takeStaticMap()
00115   {
00116     collision_environment_msgs::MakeStaticCollisionMapGoal static_map_goal;
00117     static_map_goal.cloud_source = "full_cloud_filtered";
00118     static_map_goal.number_of_clouds = 2;
00119   
00120     make_static_collision_map_client_.sendGoal(static_map_goal);
00121     if(!setLaserScan(false))
00122     {
00123       ROS_ERROR("Could not take laser scan");
00124       return false;
00125     }
00126     if ( !make_static_collision_map_client_.waitForResult(ros::Duration(30.0)) )
00127     {
00128       ROS_ERROR("Collision map was not formed in allowed time");
00129       return false;
00130     }      
00131     if(make_static_collision_map_client_.getState() != actionlib::SimpleClientGoalState::SUCCEEDED) 
00132     {
00133       ROS_ERROR("Some non-success state was reached for static collision map.  Proceed with caution");
00134       return false;
00135     }       
00136     ROS_INFO("Successful taking static map");
00137     ros::Duration(1.0).sleep();
00138     return true;
00139   }
00140 
00141   bool getModel(urdf::Model &model)
00142   {
00143     std::string robot_desc_string;
00144     ros::NodeHandle nh;
00145     nh.param("robot_description", robot_desc_string, std::string());
00146     if (!model.initString(robot_desc_string))
00147     {
00148       ROS_ERROR("Failed to parse urdf string");
00149       return false;
00150     }
00151     ROS_INFO("Successfully parsed urdf file");        
00152     return true;
00153   }
00154 
00155   bool getJointLimits(const urdf::Model &model,
00156                       const std::vector<std::string> &joint_names,
00157                       std::vector<double> &low,
00158                       std::vector<double> &high)
00159   {
00160     low.clear();
00161     high.clear();
00162     for(unsigned int i=0; i < joint_names.size(); i++)
00163     {
00164       boost::shared_ptr<const urdf::Joint> joint = model.getJoint(joint_names[i]);
00165       if(joint != NULL)
00166       {
00167         low.push_back(joint->limits->lower);
00168         high.push_back(joint->limits->upper);
00169       }
00170       else
00171       {
00172         ROS_ERROR("Joint %s not found", joint_names[i].c_str());
00173         return false;
00174       }
00175     }
00176     return true;
00177   }
00178 
00179   double gen_rand(double min, double max)
00180   {
00181     int rand_num = rand()%100+1;
00182     double result = min + (double)((max-min)*rand_num)/101.0;
00183     return result;
00184   }
00185 
00186   bool getRandomValues(const std::vector<double> &low,
00187                        const std::vector<double> &high,    
00188                        std::vector<double> &values)
00189   {
00190     if(low.size() != high.size())
00191       return false;
00192     values.clear();
00193     for(unsigned int i=0; i < low.size(); i++)
00194     {
00195       values.push_back(gen_rand(low[i],high[i]));
00196     }
00197     return true;
00198   }
00199   
00200 private:
00202   ros::NodeHandle root_nh_;
00204   ros::ServiceClient tilt_laser_service_;
00205 
00206   // Parameters for slow laser movement (high-res scanning)
00207   double laser_slow_period_;
00208   double laser_slow_amplitude_;
00209   double laser_slow_offset_;
00210 
00211   // Parameters for fast lsaer movement (low-res scanning)
00212   double laser_fast_period_;
00213   double laser_fast_amplitude_;
00214   double laser_fast_offset_;
00215 
00217   actionlib::SimpleActionClient<collision_environment_msgs::MakeStaticCollisionMapAction> 
00218   make_static_collision_map_client_;
00219 };
00220 
00221 }


arm_navigation_tests
Author(s): Sachin Chitta
autogenerated on Sat Dec 28 2013 17:24:27