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
00013 loop_rate_ = 20;
00014
00015
00016 cmd_vel_safe_publisher_ = public_node_handle_.advertise<geometry_msgs::Twist>("cmd_vel_safe", 100);
00017
00018
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
00024
00025
00026
00027
00028
00029
00030 }
00031
00032 SafeCmdAlgNode::~SafeCmdAlgNode(void)
00033 {
00034
00035 }
00036
00037 void SafeCmdAlgNode::mainNodeThread(void)
00038 {
00039
00040
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
00057
00058
00059
00060
00061 cmd_vel_safe_publisher_.publish(Twist_msg_);
00062
00063 }
00064
00065
00066 void SafeCmdAlgNode::cmd_vel_callback(const geometry_msgs::Twist::ConstPtr& msg)
00067 {
00068
00069
00070
00071
00072
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
00092
00093
00094 }
00095 void SafeCmdAlgNode::rear_laser_callback(const sensor_msgs::LaserScan::ConstPtr& msg)
00096 {
00097
00098
00099
00100
00101
00102
00103 max_vel_rear_ = std::min(compute_max_velocity_(msg),limit_vel_rear_);
00104
00105
00106
00107
00108
00109 }
00110 void SafeCmdAlgNode::front_laser_callback(const sensor_msgs::LaserScan::ConstPtr& msg)
00111 {
00112
00113
00114
00115
00116
00117
00118 max_vel_front_ = std::min(compute_max_velocity_(msg),limit_vel_front_);
00119
00120
00121
00122
00123
00124 }
00125
00126
00127
00128
00129
00130
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
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 }