locomotor.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2017, Locus Robotics
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 the copyright holder 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 HOLDER 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 #ifndef LOCOMOTOR_LOCOMOTOR_H
00036 #define LOCOMOTOR_LOCOMOTOR_H
00037 
00038 #include <ros/ros.h>
00039 #include <locomotor/executor.h>
00040 #include <locomotor/publishers.h>
00041 #include <locomotor_msgs/NavigationState.h>
00042 #include <locomotor_msgs/ResultCode.h>
00043 #include <nav_core2/exceptions.h>
00044 #include <nav_core2/costmap.h>
00045 #include <nav_core2/global_planner.h>
00046 #include <nav_core2/local_planner.h>
00047 #include <pluginlib/class_loader.h>
00048 #include <nav_2d_utils/odom_subscriber.h>
00049 #include <nav_2d_utils/plugin_mux.h>
00050 #include <map>
00051 #include <string>
00052 #include <vector>
00053 
00054 namespace locomotor
00055 {
00056 // Callback Type Definitions
00057 using CostmapUpdateCallback = std::function<void (const ros::Duration&)>;
00058 using CostmapUpdateExceptionCallback = std::function<void (nav_core2::NavCore2ExceptionPtr, const ros::Duration&)>;
00059 using GlobalPlanCallback = std::function<void (const nav_2d_msgs::Path2D&, const ros::Duration&)>;
00060 using LocalPlanCallback = std::function<void (const nav_2d_msgs::Twist2DStamped&, const ros::Duration&)>;
00061 using PlannerExceptionCallback = std::function<void (nav_core2::NavCore2ExceptionPtr, const ros::Duration&)>;
00062 using NavigationCompleteCallback = std::function<void ()>;
00063 using NavigationFailureCallback = std::function<void (const locomotor_msgs::ResultCode)>;
00064 
00065 inline locomotor_msgs::ResultCode makeResultCode(int component = -1, int result_code = -1,
00066                                                  const std::string& message = "")
00067 {
00068   locomotor_msgs::ResultCode code;
00069   code.component = component;
00070   code.result_code = result_code;
00071   code.message = message;
00072   return code;
00073 }
00074 
00079 class Locomotor
00080 {
00081 public:
00085   explicit Locomotor(const ros::NodeHandle& private_nh);
00086 
00092   void initializeGlobalCostmap(Executor& ex);
00093   void initializeLocalCostmap(Executor& ex);
00094   void initializeGlobalPlanners(Executor& ex);
00095   void initializeLocalPlanners(Executor& ex);  // end of InitializeMethods group
00097 
00102   virtual void setGoal(nav_2d_msgs::Pose2DStamped goal);
00103 
00116   void requestGlobalCostmapUpdate(Executor& work_ex, Executor& result_ex,
00117                                   CostmapUpdateCallback cb = nullptr,
00118                                   CostmapUpdateExceptionCallback fail_cb = nullptr);
00119 
00127   void requestLocalCostmapUpdate(Executor& work_ex, Executor& result_ex,
00128                                  CostmapUpdateCallback cb = nullptr,
00129                                  CostmapUpdateExceptionCallback fail_cb = nullptr);
00130 
00138   void requestGlobalPlan(Executor& work_ex, Executor& result_ex,
00139                          GlobalPlanCallback cb = nullptr,
00140                          PlannerExceptionCallback fail_cb = nullptr);
00141 
00150   void requestLocalPlan(Executor& work_ex, Executor& result_ex,
00151                         LocalPlanCallback cb = nullptr,
00152                         PlannerExceptionCallback fail_cb = nullptr,
00153                         NavigationCompleteCallback complete_cb = nullptr);
00154 
00161   void requestNavigationFailure(Executor& result_ex, const locomotor_msgs::ResultCode& result,
00162                                 NavigationFailureCallback cb = nullptr);  // end of ActionRequests group
00164 
00165   const locomotor_msgs::NavigationState& getNavigationState() const { return state_; }
00166 
00167   // Global/Local Planner Access
00168   std::vector<std::string> getGlobalPlannerNames() const { return global_planner_mux_.getPluginNames(); }
00169   std::string getCurrentGlobalPlannerName() const { return global_planner_mux_.getCurrentPluginName(); }
00170   nav_core2::GlobalPlanner& getCurrentGlobalPlanner() { return global_planner_mux_.getCurrentPlugin(); }
00171   bool useGlobalPlanner(const std::string& name) { return global_planner_mux_.usePlugin(name); }
00172 
00173   std::vector<std::string> getLocalPlannerNames() const { return local_planner_mux_.getPluginNames(); }
00174   std::string getCurrentLocalPlannerName() const { return local_planner_mux_.getCurrentPluginName(); }
00175   nav_core2::LocalPlanner& getCurrentLocalPlanner() { return local_planner_mux_.getCurrentPlugin(); }
00176   bool useLocalPlanner(const std::string& name) { return local_planner_mux_.usePlugin(name); }
00177 
00178   // Costmap Access
00179   nav_core2::Costmap::Ptr getGlobalCostmap() const { return global_costmap_; }
00180   nav_core2::Costmap::Ptr getLocalCostmap() const { return local_costmap_; }
00181 
00182   // Additional Convenience Access
00183   TFListenerPtr getTFListener() const { return tf_; }
00184   nav_2d_msgs::Pose2DStamped getGlobalRobotPose() const { return getRobotPose(global_costmap_->getFrameId()); }
00185   nav_2d_msgs::Pose2DStamped getLocalRobotPose() const { return getRobotPose(local_costmap_->getFrameId()); }
00186 
00187   // Publisher Access
00188   void publishPath(const nav_2d_msgs::Path2D& global_plan) { path_pub_.publishPath(global_plan); }
00189   void publishTwist(const nav_2d_msgs::Twist2DStamped& command) { twist_pub_.publishTwist(command); }
00190 
00191 protected:
00196   void doCostmapUpdate(nav_core2::Costmap& costmap, Executor& result_ex,
00197                        CostmapUpdateCallback cb, CostmapUpdateExceptionCallback fail_cb);
00198   void makeGlobalPlan(Executor& result_ex, GlobalPlanCallback cb, PlannerExceptionCallback fail_cb);
00199   void makeLocalPlan(Executor& result_ex, LocalPlanCallback cb, PlannerExceptionCallback fail_cb,
00200                      NavigationCompleteCallback complete_cb);  // end of ActualActions group
00202 
00208   virtual void switchLocalPlannerCallback(const std::string& old_planner, const std::string& new_planner);
00209 
00210   // Costmap Loader
00211   pluginlib::ClassLoader<nav_core2::Costmap> costmap_loader_;
00212 
00213   // Global Planners and Costmap
00214   nav_2d_utils::PluginMux<nav_core2::GlobalPlanner> global_planner_mux_;
00215   nav_core2::Costmap::Ptr global_costmap_;
00216 
00217   // Local Planners and Costmap
00218   nav_2d_utils::PluginMux<nav_core2::LocalPlanner> local_planner_mux_;
00219   nav_core2::Costmap::Ptr local_costmap_;
00220 
00221   // Tools for getting the position and velocity of the robot
00222   nav_2d_msgs::Pose2DStamped getRobotPose(const std::string& target_frame) const;
00223   TFListenerPtr tf_;
00224   bool use_latest_pose_;
00225   std::shared_ptr<nav_2d_utils::OdomSubscriber> odom_sub_;
00226 
00227   // Core Variables
00228   ros::NodeHandle private_nh_;
00229   locomotor_msgs::NavigationState state_;
00230   std::string robot_base_frame_;
00231 
00232   // Publishers
00233   PathPublisher path_pub_;
00234   TwistPublisher twist_pub_;
00235 };
00236 }  // namespace locomotor
00237 
00238 #endif  // LOCOMOTOR_LOCOMOTOR_H


locomotor
Author(s):
autogenerated on Wed Jun 26 2019 20:09:43