library.cpp
Go to the documentation of this file.
00001 
00006 #include <limits>
00007 #include "yocs_safety_controller/safety_controller.hpp"
00008 
00009 namespace yocs_safety_controller
00010 {
00011 
00012 SafetyController::SafetyController(ros::NodeHandle& nh_priv, std::string& name) :
00013     Controller(),
00014     nh_priv_(nh_priv),
00015     name_(name),
00016     obstacle_detected_(false),
00017     reverse_(false),
00018     reversing_(false),
00019     reversing_start_(ros::Time::now()),
00020     reversing_distance_(0.02),
00021     reversing_velocity_(0.02),
00022     reversing_duration_(0.0)
00023 {};
00024 
00025 
00026 bool SafetyController::init()
00027 {
00028   if (!nh_priv_.getParam("reverse", reverse_))
00029   {
00030     ROS_WARN_STREAM("Safety Controller : no parameter on server for reverse, using default '"
00031                     << reverse_ << "'");
00032   }
00033   if (reverse_)
00034   {
00035     if (!nh_priv_.getParam("reversing_distance", reversing_distance_))
00036     {
00037       ROS_WARN_STREAM("Safety Controller : no parameter on server for reversing_distance, using default '"
00038                       << reversing_distance_ << "'");
00039     }
00040     if (!nh_priv_.getParam("reversing_velocity", reversing_velocity_))
00041     {
00042       ROS_WARN_STREAM("Safety Controller : no parameter on server for reversing_velocity, using default '"
00043                       << reversing_distance_ << "'");
00044     }
00045   }
00046 
00047   // calculate reversing duration
00048   reversing_duration_ = ros::Duration(reversing_distance_/reversing_velocity_);
00049 
00050   enable_controller_subscriber_ = nh_priv_.subscribe("enable", 10, &SafetyController::enableCB, this);
00051   disable_controller_subscriber_ = nh_priv_.subscribe("disable", 10, &SafetyController::disableCB, this);
00052   ranger_subscriber_ = nh_priv_.subscribe("rangers", 10, &SafetyController::rangerCB, this);
00053   velocity_command_publisher_ = nh_priv_.advertise< geometry_msgs::Twist >("cmd_vel", 10);
00054   return true;
00055 };
00056 
00057 void SafetyController::enableCB(const std_msgs::EmptyConstPtr msg)
00058 {
00059   if (this->enable())
00060   {
00061     ROS_INFO_STREAM("Controller has been enabled. [" << name_ << "]");
00062   }
00063   else
00064   {
00065     ROS_INFO_STREAM("Controller was already enabled. [" << name_ <<"]");
00066   }
00067 }
00068 
00069 void SafetyController::disableCB(const std_msgs::EmptyConstPtr msg)
00070 {
00071   if (this->disable())
00072   {
00073     ROS_INFO_STREAM("Controller has been disabled. [" << name_ <<"]");
00074   }
00075   else
00076   {
00077     ROS_INFO_STREAM("Controller was already disabled. [" << name_ <<"]");
00078   }
00079 }
00080 
00081 void SafetyController::rangerCB(const sensor_msgs::RangeConstPtr msg)
00082 {
00083   if (this->getState())
00084   {
00085     // readings from a fixed-distance ranger
00086     if (msg->range == -std::numeric_limits<double>::infinity()) // -Inf means obstacle detected
00087     {
00088       obstacle_detected_ = true;
00089     }
00090   }
00091   return;
00092 }
00093 
00094 void SafetyController::spinOnce()
00095 {
00096   if (reversing_)
00097   {
00098     if ((ros::Time::now() - reversing_start_) > reversing_duration_)
00099     {
00100       reversing_ = false;
00101     }
00102     else
00103     {
00104       // reverse
00105       cmd_vel_msg_.reset(new geometry_msgs::Twist());
00106       cmd_vel_msg_->linear.x = -reversing_velocity_;
00107       cmd_vel_msg_->linear.y = 0.0;
00108       cmd_vel_msg_->linear.z = 0.0;
00109       cmd_vel_msg_->angular.x = 0.0;
00110       cmd_vel_msg_->angular.y = 0.0;
00111       cmd_vel_msg_->angular.z = 0.0;
00112       velocity_command_publisher_.publish(cmd_vel_msg_);
00113     }
00114   }
00115   else
00116   {
00117     if (obstacle_detected_)
00118     {
00119       // stop
00120       cmd_vel_msg_.reset(new geometry_msgs::Twist());
00121       cmd_vel_msg_->linear.x = 0.0;
00122       cmd_vel_msg_->linear.y = 0.0;
00123       cmd_vel_msg_->linear.z = 0.0;
00124       cmd_vel_msg_->angular.x = 0.0;
00125       cmd_vel_msg_->angular.y = 0.0;
00126       cmd_vel_msg_->angular.z = 0.0;
00127       velocity_command_publisher_.publish(cmd_vel_msg_);
00128       obstacle_detected_ = false;
00129 
00130       // change to reversing, if activated
00131       if (reverse_)
00132       {
00133         reversing_ = true;
00134         reversing_start_ = ros::Time::now();
00135       }
00136     }
00137   }
00138   return;
00139 }
00140 
00141 } // namespace yocs_safety_controller


yocs_safety_controller
Author(s): Marcus Liebhardt
autogenerated on Thu Jun 6 2019 21:47:38