safe_cmd_alg_node.cpp
Go to the documentation of this file.
00001 #include "safe_cmd_alg_node.h"
00002 
00003 SafeCmdAlgNode::SafeCmdAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<SafeCmdAlgorithm>(),
00005   collision_time_(1),
00006   min_dist_(0.4),
00007   max_vel_front_(7),
00008   max_vel_rear_(7),
00009   limit_vel_front_(7),
00010   limit_vel_rear_(7)
00011 {
00012   //init class attributes if necessary
00013   loop_rate_ = 20;//in [Hz]
00014 
00015   // [init publishers]
00016   cmd_vel_safe_publisher_ = public_node_handle_.advertise<geometry_msgs::Twist>("cmd_vel_safe", 100);
00017   
00018   // [init subscribers]
00019   cmd_vel_subscriber_ = public_node_handle_.subscribe("cmd_vel", 100, &SafeCmdAlgNode::cmd_vel_callback,this);
00020   rear_laser_subscriber_ = public_node_handle_.subscribe("rear_laser", 100, &SafeCmdAlgNode::rear_laser_callback, this);
00021   front_laser_subscriber_ = public_node_handle_.subscribe("front_laser", 100, &SafeCmdAlgNode::front_laser_callback, this);
00022   
00023   // [init services]
00024   
00025   // [init clients]
00026   
00027   // [init action servers]
00028   
00029   // [init action clients]
00030 }
00031 
00032 SafeCmdAlgNode::~SafeCmdAlgNode(void)
00033 {
00034   // [free dynamic memory]
00035 }
00036 
00037 void SafeCmdAlgNode::mainNodeThread(void)
00038 {
00039   // [fill msg structures]
00040   //this->Twist_msg_.data = my_var;
00041   
00042   if(!alg_.config_.unsafe)
00043   {
00044     if(Twist_msg_.linear.x > fabs(max_vel_front_))
00045     {
00046       Twist_msg_.linear.x = fabs(max_vel_front_);
00047       ROS_WARN("heading to front obstacle, reducing velocity");
00048     }  
00049 
00050     if(Twist_msg_.linear.x < -fabs(max_vel_rear_))
00051     {
00052       Twist_msg_.linear.x = -fabs(max_vel_rear_); 
00053       ROS_WARN("heading to rear obstacle, reducing velocity");
00054     }
00055   }
00056   // [fill srv structure and make request to the server]
00057   
00058   // [fill action structure and make request to the action server]
00059 
00060   // [publish messages]
00061   cmd_vel_safe_publisher_.publish(Twist_msg_);
00062   
00063 }
00064 
00065 /*  [subscriber callbacks] */
00066 void SafeCmdAlgNode::cmd_vel_callback(const geometry_msgs::Twist::ConstPtr& msg) 
00067 { 
00068   //ROS_INFO("SafeCmdAlgNode::cmd_vel_callback: New Message Received"); 
00069 
00070   //use appropiate mutex to shared variables if necessary 
00071   //this->alg_.lock(); 
00072   //this->cmd_vel_mutex_.enter();
00073 
00074   if(!alg_.config_.unsafe)
00075   {
00076     if (msg->linear.x == 0 && msg->angular.z ==0)
00077       Twist_msg_ = *msg;
00078     else
00079     {
00080       Twist_msg_.linear.x  += (msg->linear.x  - last_twist_.linear.x ); 
00081       Twist_msg_.linear.y  += (msg->linear.y  - last_twist_.linear.y ); 
00082       Twist_msg_.linear.z  += (msg->linear.z  - last_twist_.linear.z ); 
00083       Twist_msg_.angular.x += (msg->angular.x - last_twist_.angular.x); 
00084       Twist_msg_.angular.y += (msg->angular.y - last_twist_.angular.y); 
00085       Twist_msg_.angular.z += (msg->angular.z - last_twist_.angular.z); 
00086     }
00087     last_twist_= *msg;
00088   } else
00089     Twist_msg_ = *msg;
00090 
00091   //unlock previously blocked shared variables 
00092   //this->alg_.unlock(); 
00093   //this->cmd_vel_mutex_.exit(); 
00094 }
00095 void SafeCmdAlgNode::rear_laser_callback(const sensor_msgs::LaserScan::ConstPtr& msg) 
00096 { 
00097   //ROS_INFO("SafeCmdAlgNode::rear_laser_callback: New Message Received"); 
00098 
00099   //use appropiate mutex to shared variables if necessary 
00100   //this->alg_.lock(); 
00101   //this->rear_laser_mutex_.enter(); 
00102 
00103   max_vel_rear_ = std::min(compute_max_velocity_(msg),limit_vel_rear_);
00104   //ROS_INFO("Max vel r: %f",max_vel_rear_);
00105 
00106   //unlock previously blocked shared variables 
00107   //this->alg_.unlock(); 
00108   //this->rear_laser_mutex_.exit(); 
00109 }
00110 void SafeCmdAlgNode::front_laser_callback(const sensor_msgs::LaserScan::ConstPtr& msg) 
00111 { 
00112   //ROS_INFO("SafeCmdAlgNode::front_laser_callback: New Message Received"); 
00113 
00114   //use appropiate mutex to shared variables if necessary 
00115   //this->alg_.lock(); 
00116   //this->front_laser_mutex_.enter(); 
00117 
00118   max_vel_front_ = std::min(compute_max_velocity_(msg),limit_vel_front_);
00119   //ROS_INFO("Max vel f: %f",max_vel_front_);
00120 
00121   //unlock previously blocked shared variables 
00122   //this->alg_.unlock(); 
00123   //this->front_laser_mutex_.exit(); 
00124 }
00125 
00126 /*  [service callbacks] */
00127 
00128 /*  [action callbacks] */
00129 
00130 /*  [action requests] */
00131 
00132 void SafeCmdAlgNode::node_config_update(Config &config, uint32_t level)
00133 {
00134   alg_.lock();
00135 
00136   alg_.unlock();
00137 }
00138 
00139 void SafeCmdAlgNode::addNodeDiagnostics(void)
00140 {
00141 }
00142 
00143 /* main function */
00144 int main(int argc,char *argv[])
00145 {
00146   return algorithm_base::main<SafeCmdAlgNode>(argc, argv, "safe_cmd_alg_node");
00147 }
00148 
00149 float SafeCmdAlgNode::compute_max_velocity_(const sensor_msgs::LaserScan::ConstPtr& scan) const
00150 {
00151   float max_velocity;
00152 
00153   float min_range = *std::min_element( scan->ranges.begin(), scan->ranges.end() );
00154 
00155   if (min_range < min_dist_)
00156     max_velocity = 0;
00157   else
00158     max_velocity = min_range / collision_time_;
00159 
00160   return max_velocity;
00161 }


iri_safe_cmd
Author(s): Maintained by IRI Robotics Lab, Marti Morta
autogenerated on Fri Dec 6 2013 20:24:55