publishers.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2018, 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 #include <locomotor/publishers.h>
00036 #include <nav_2d_utils/path_ops.h>
00037 #include <nav_2d_utils/conversions.h>
00038 #include <nav_msgs/Path.h>
00039 #include <geometry_msgs/Twist.h>
00040 #include <string>
00041 
00042 namespace locomotor
00043 {
00044 PathPublisher::PathPublisher(ros::NodeHandle& nh)
00045 {
00046   std::string topic, publish_type;
00047   nh.param("global_plan_topic", topic, std::string("global_plan"));
00048   nh.param("global_plan_type", publish_type, std::string("Path3D"));
00049   nh.param("global_plan_epsilon", global_plan_epsilon_, 0.1);
00050 
00051   if (publish_type == "Path2D")
00052   {
00053     path_type_ = PathType::PATH_2D;
00054     pub_ = nh.advertise<nav_2d_msgs::Path2D>(topic, 1);
00055   }
00056   else if (publish_type == "")
00057   {
00058     path_type_ = PathType::NO_PATH;
00059   }
00060   else
00061   {
00062     if (publish_type != "Path3D")
00063     {
00064       ROS_ERROR_NAMED("Locomotor", "Unknown global_plan_type \"%s\". Using Path3D instead.", publish_type.c_str());
00065     }
00066     path_type_ = PathType::PATH_3D;
00067     pub_ = nh.advertise<nav_msgs::Path>(topic, 1);
00068   }
00069 }
00070 
00071 void PathPublisher::publishPath(const nav_2d_msgs::Path2D& global_plan)
00072 {
00073   if (pub_.getNumSubscribers() == 0) return;
00074 
00075   nav_2d_msgs::Path2D to_publish = global_plan;
00076   if (global_plan_epsilon_ >= 0.0)
00077   {
00078     to_publish = nav_2d_utils::compressPlan(global_plan, global_plan_epsilon_);
00079   }
00080   switch (path_type_)
00081   {
00082   case PathType::PATH_2D:
00083     pub_.publish(to_publish);
00084     break;
00085   case PathType::PATH_3D:
00086     pub_.publish(nav_2d_utils::pathToPath(to_publish));
00087     break;
00088   case PathType::NO_PATH:
00089     break;
00090   };
00091 }
00092 
00093 TwistPublisher::TwistPublisher(ros::NodeHandle& nh)
00094 {
00095   std::string topic, publish_type;
00096   nh.param("twist_topic", topic, std::string("cmd_vel"));
00097   nh.param("twist_type", publish_type, std::string("Twist3D"));
00098 
00099   ros::NodeHandle global_nh;
00100   if (publish_type == "Twist2D")
00101   {
00102     twist_type_ = TwistType::TWIST_2D;
00103     pub_ = global_nh.advertise<nav_2d_msgs::Twist2D>(topic, 1);
00104   }
00105   else if (publish_type == "Twist2DStamped")
00106   {
00107     twist_type_ = TwistType::TWIST_2D_STAMPED;
00108     pub_ = global_nh.advertise<nav_2d_msgs::Twist2DStamped>(topic, 1);
00109   }
00110   else if (publish_type == "")
00111   {
00112     twist_type_ = TwistType::NO_TWIST;
00113   }
00114   else
00115   {
00116     if (publish_type != "Twist3D")
00117     {
00118       ROS_ERROR_NAMED("Locomotor", "Unknown twist_type \"%s\". Using Twist3D instead.", publish_type.c_str());
00119     }
00120     twist_type_ = TwistType::TWIST_3D;
00121     pub_ = global_nh.advertise<geometry_msgs::Twist>(topic, 1);
00122   }
00123 }
00124 
00125 void TwistPublisher::publishTwist(const nav_2d_msgs::Twist2DStamped& command)
00126 {
00127   if (pub_.getNumSubscribers() == 0) return;
00128   switch (twist_type_)
00129   {
00130   case TwistType::TWIST_2D:
00131     pub_.publish(command.velocity);
00132     break;
00133   case TwistType::TWIST_2D_STAMPED:
00134     pub_.publish(command);
00135     break;
00136   case TwistType::TWIST_3D:
00137     pub_.publish(nav_2d_utils::twist2Dto3D(command.velocity));
00138     break;
00139   case TwistType::NO_TWIST:
00140     break;
00141   };
00142 }
00143 
00144 }  // namespace locomotor


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