buffer_server.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2009, Willow Garage, Inc.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of Willow Garage, Inc. nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Eitan Marder-Eppstein
00036 *********************************************************************/
00037 #include <tf2_ros/buffer_server.h>
00038 
00039 namespace tf2_ros
00040 {
00041   BufferServer::BufferServer(const Buffer& buffer, const std::string& ns, bool auto_start, ros::Duration check_period): 
00042     buffer_(buffer),
00043     server_(ros::NodeHandle(),
00044             ns,
00045             boost::bind(&BufferServer::goalCB, this, _1),
00046             boost::bind(&BufferServer::cancelCB, this, _1),
00047             auto_start)
00048   {
00049     ros::NodeHandle n;
00050     check_timer_ = n.createTimer(check_period, boost::bind(&BufferServer::checkTransforms, this, _1));
00051   }
00052 
00053   void BufferServer::checkTransforms(const ros::TimerEvent& e)
00054   {
00055     boost::mutex::scoped_lock l(mutex_);
00056     for(std::list<GoalInfo>::iterator it = active_goals_.begin(); it != active_goals_.end();)
00057     {
00058       GoalInfo& info = *it;
00059 
00060       //we want to lookup a transform if the time on the goal
00061       //has expired, or a transform is available
00062       if(canTransform(info.handle) || info.end_time < ros::Time::now())
00063       {
00064         tf2_msgs::LookupTransformResult result;
00065 
00066         //try to populate the result, catching exceptions if they occur
00067         try
00068         {
00069           result.transform = lookupTransform(info.handle);
00070         }
00071         catch (tf2::ConnectivityException &ex)
00072         {
00073           result.error.error = result.error.CONNECTIVITY_ERROR;
00074           result.error.error_string = ex.what();
00075         }
00076         catch (tf2::LookupException &ex)
00077         {
00078           result.error.error = result.error.LOOKUP_ERROR;
00079           result.error.error_string = ex.what();
00080         }
00081         catch (tf2::ExtrapolationException &ex)
00082         {
00083           result.error.error = result.error.EXTRAPOLATION_ERROR;
00084           result.error.error_string = ex.what();
00085         }
00086         catch (tf2::InvalidArgumentException &ex)
00087         {
00088           result.error.error = result.error.INVALID_ARGUMENT_ERROR;
00089           result.error.error_string = ex.what();
00090         }
00091         catch (tf2::TimeoutException &ex)
00092         {
00093           result.error.error = result.error.TIMEOUT_ERROR;
00094           result.error.error_string = ex.what();
00095         }
00096         catch (tf2::TransformException &ex)
00097         {
00098           result.error.error = result.error.TRANSFORM_ERROR;
00099           result.error.error_string = ex.what();
00100         }
00101 
00102         //make sure to pass the result to the client
00103         //even failed transforms are considered a success
00104         //since the request was successfully processed
00105         it = active_goals_.erase(it);
00106         info.handle.setSucceeded(result);
00107       }
00108       else
00109         ++it;
00110     }
00111   }
00112 
00113   void BufferServer::cancelCB(GoalHandle gh)
00114   {
00115     boost::mutex::scoped_lock l(mutex_);
00116     //we need to find the goal in the list and remove it... also setting it as canceled
00117     //if its not in the list, we won't do anything since it will have already been set
00118     //as completed
00119     for(std::list<GoalInfo>::iterator it = active_goals_.begin(); it != active_goals_.end();)
00120     {
00121       GoalInfo& info = *it;
00122       if(info.handle == gh)
00123       {
00124         it = active_goals_.erase(it);
00125         info.handle.setCanceled();
00126         return;
00127       }
00128       else
00129         ++it;
00130     }
00131   }
00132 
00133   void BufferServer::goalCB(GoalHandle gh)
00134   {
00135     //we'll accept all goals we get
00136     gh.setAccepted();
00137 
00138     //if the transform isn't immediately available, we'll push it onto our list to check
00139     //along with the time that the goal will end
00140     GoalInfo goal_info;
00141     goal_info.handle = gh;
00142     goal_info.end_time = ros::Time::now() + gh.getGoal()->timeout;
00143 
00144     //we can do a quick check here to see if the transform is valid
00145     //we'll also do this if the end time has been reached 
00146     if(canTransform(gh) || goal_info.end_time <= ros::Time::now())
00147     {
00148       tf2_msgs::LookupTransformResult result;
00149       try
00150       {
00151         result.transform = lookupTransform(gh);
00152       }
00153       catch (tf2::ConnectivityException &ex)
00154       {
00155         result.error.error = result.error.CONNECTIVITY_ERROR;
00156         result.error.error_string = ex.what();
00157       }
00158       catch (tf2::LookupException &ex)
00159       {
00160         result.error.error = result.error.LOOKUP_ERROR;
00161         result.error.error_string = ex.what();
00162       }
00163       catch (tf2::ExtrapolationException &ex)
00164       {
00165         result.error.error = result.error.EXTRAPOLATION_ERROR;
00166         result.error.error_string = ex.what();
00167       }
00168       catch (tf2::InvalidArgumentException &ex)
00169       {
00170         result.error.error = result.error.INVALID_ARGUMENT_ERROR;
00171         result.error.error_string = ex.what();
00172       }
00173       catch (tf2::TimeoutException &ex)
00174       {
00175         result.error.error = result.error.TIMEOUT_ERROR;
00176         result.error.error_string = ex.what();
00177       }
00178       catch (tf2::TransformException &ex)
00179       {
00180         result.error.error = result.error.TRANSFORM_ERROR;
00181         result.error.error_string = ex.what();
00182       }
00183 
00184       gh.setSucceeded(result);
00185       return;
00186     }
00187 
00188     boost::mutex::scoped_lock l(mutex_);
00189     active_goals_.push_back(goal_info);
00190   }
00191 
00192   bool BufferServer::canTransform(GoalHandle gh)
00193   {
00194     const tf2_msgs::LookupTransformGoal::ConstPtr& goal = gh.getGoal();
00195 
00196     //check whether we need to used the advanced or simple api
00197     if(!goal->advanced)
00198       return buffer_.canTransform(goal->target_frame, goal->source_frame, goal->source_time);
00199 
00200     return buffer_.canTransform(goal->target_frame, goal->target_time, 
00201         goal->source_frame, goal->source_time, goal->fixed_frame);
00202   }
00203 
00204   geometry_msgs::TransformStamped BufferServer::lookupTransform(GoalHandle gh)
00205   {
00206     const tf2_msgs::LookupTransformGoal::ConstPtr& goal = gh.getGoal();
00207 
00208     //check whether we need to used the advanced or simple api
00209     if(!goal->advanced)
00210       return buffer_.lookupTransform(goal->target_frame, goal->source_frame, goal->source_time);
00211 
00212     return buffer_.lookupTransform(goal->target_frame, goal->target_time, 
00213         goal->source_frame, goal->source_time, goal->fixed_frame);
00214   }
00215 
00216   void BufferServer::start()
00217   {
00218     server_.start();
00219   }
00220 
00221 };


tf2_ros
Author(s): Eitan Marder-Eppstein, Wim Meeussen
autogenerated on Thu Aug 27 2015 13:10:05