00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "navigator_internal.h"
00011 #include "Controller.h"
00012 #include "obstacle.h"
00013 #include "follow_safely.h"
00014
00015 #include <art/DARPA_rules.h>
00016
00017 FollowSafely::FollowSafely(Navigator *navptr, int _verbose):
00018 Controller(navptr, _verbose) {};
00019
00020 FollowSafely::~FollowSafely() {};
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 Controller::result_t FollowSafely::control(pilot_command_t &pcmd)
00042 {
00043 bool was_blocked = navdata->lane_blocked;
00044 navdata->lane_blocked = false;
00045
00046 float location = obstacle->closest_ahead_in_plan();
00047 result_t result = OK;
00048
00049 if (location >= obstacle->maximum_range())
00050 {
00051
00052 return result;
00053 }
00054
00055 float following_time =
00056 Euclidean::DistanceToTime(location, estimate->twist.twist.linear.x);
00057
00058 ROS_DEBUG("obstacle is %.3f sec ahead at %.3f m/s",
00059 following_time, estimate->twist.twist.linear.x);
00060
00061
00062
00063
00064 if ((following_time <= config_->min_following_time)
00065 || (location <= config_->close_stopping_distance))
00066 {
00067
00068 pcmd.velocity = 0.0;
00069
00070 if (verbose >= 2)
00071 ART_MSG(8, "Obstacle avoidance requesting immediate halt");
00072
00073
00074
00075 if (navdata->stopped)
00076 {
00077 navdata->lane_blocked = true;
00078 if (!was_blocked)
00079 {
00080
00081 ART_MSG(1, "New obstacle detected!");
00082 }
00083 result = Blocked;
00084 }
00085 }
00086 else if (following_time < config_->desired_following_time)
00087 {
00088 adjust_speed(pcmd, location);
00089 }
00090 else if (nav->navdata.stopped
00091 || (following_time > config_->desired_following_time))
00092 {
00093 adjust_speed(pcmd, location);
00094 }
00095
00096
00097
00098
00099
00100 if (obstacle->car_approaching())
00101 {
00102 ART_MSG(1, "Possible collision ahead!");
00103 result = Collision;
00104 }
00105
00106 trace("follow_safely controller", pcmd, result);
00107 return result;
00108 };
00109
00110
00111
00112
00113
00114 void FollowSafely::adjust_speed(pilot_command_t &pcmd, float obs_dist)
00115 {
00116
00117
00118 float adjusted_speed = fminf(obs_dist / config_->desired_following_time,
00119 order->max_speed);
00120
00121
00122 pcmd.velocity = fminf(pcmd.velocity, adjusted_speed);
00123 }