00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "navigator_internal.h"
00011 #include "Controller.h"
00012 #include "course.h"
00013 #include "obstacle.h"
00014 #include "ntimer.h"
00015 #include "run.h"
00016
00017 #include "halt.h"
00018 #include "road.h"
00019 #if NOT_PORTED_TO_ROS
00020 #include "safety.h"
00021 #include "voronoi_zone.h"
00022 #endif
00023
00024 #include <art_nav/estimate.h>
00025 #include <art_map/ZoneOps.h>
00026
00027
00028
00029 static const char *state_name[] =
00030 {
00031 "Continue",
00032 "Escape",
00033 "Replan",
00034 };
00035
00036 Run::Run(Navigator *navptr, int _verbose):
00037 Controller(navptr, _verbose)
00038 {
00039 halt = new Halt(navptr, _verbose);
00040 road = new Road(navptr, _verbose);
00041 #if NOT_PORTED_TO_ROS
00042 safety = new Safety(navptr, _verbose);
00043 unstuck = new VoronoiZone(navptr, _verbose);
00044
00045 escape_timer = new NavTimer(cycle);
00046 #endif
00047 go_state = Continue;
00048 };
00049
00050 Run::~Run()
00051 {
00052 delete halt;
00053 delete road;
00054 #if NOT_PORTED_TO_ROS
00055 delete safety;
00056 delete unstuck;
00057
00058 delete escape_timer;
00059 #endif
00060 };
00061
00062
00063 void Run::begin_escape(void)
00064 {
00065 #if NOT_PORTED_TO_ROS
00066 unstuck->reset();
00067 blockage_pose = estimate->pos;
00068 blockage_waypt_dist =
00069 Euclidean::DistanceToWaypt(estimate->pos, order->waypt[1]);
00070 escape_timer->Start(escape_timeout_secs);
00071 set_go_state(Escape);
00072 #endif
00073 }
00074
00087 Controller::result_t Run::control(pilot_command_t &pcmd)
00088 {
00089
00090
00091
00092 if (NavBehavior(order->behavior) == NavBehavior::Run
00093 || course->polygons.empty())
00094 {
00095 ROS_DEBUG_STREAM("run controller not initialized, have "
00096 << course->polygons.size() << " polygons");
00097
00098
00099 return halt->control(pcmd);
00100 }
00101
00102
00103 pcmd.yawRate = 0.0;
00104 pcmd.velocity = fminf(order->max_speed, config_->max_speed);
00105
00106
00107
00108 Estimate::control_pose(*odom, ros::Time::now(), *estimate);
00109
00110 course->begin_run_cycle();
00111
00112 trace("begin run controller", pcmd);
00113
00114 result_t result;
00115
00116
00117 switch (order->behavior.value)
00118 {
00119 case NavBehavior::Go:
00120 result = go(pcmd);
00121 break;
00122
00123 case NavBehavior::Initialize:
00124 result = initialize(pcmd);
00125 break;
00126
00127 default:
00128 ROS_ERROR("unsupported Navigator behavior: %s (halting)",
00129 NavBehavior(order->behavior).Name());
00130 halt->control(pcmd);
00131 result = NotImplemented;
00132 }
00133
00134 #if NOT_PORTED_TO_ROS
00135 if (extra_safety_check)
00136 {
00137
00138 result_t sres = safety->control(pcmd);
00139 if (sres != OK)
00140 result = sres;
00141 }
00142 #endif
00143
00144 if (navdata->reverse)
00145 {
00146
00147 pcmd.velocity = -pcmd.velocity;
00148 }
00149
00150 course->end_run_cycle();
00151
00152 trace("run controller", pcmd, result);
00153
00154 return result;
00155 };
00156
00166 Controller::result_t Run::initialize(pilot_command_t &pcmd)
00167 {
00168 result_t result;
00169
00170 ElementID start_way = starting_waypt();
00171 if (start_way != ElementID())
00172 {
00173 course->new_waypoint_reached(start_way);
00174 ROS_INFO("starting way-point: %s", start_way.name().str);
00175 result = OK;
00176 }
00177 else
00178 {
00179
00180 ROS_WARN("no starting way-point found, run failed.");
00181 result = NotApplicable;
00182 course->no_waypoint_reached();
00183 }
00184
00185 halt->control(pcmd);
00186 return result;
00187 }
00188
00189
00219 Controller::result_t Run::go(pilot_command_t &pcmd)
00220 {
00221 if (ElementID(order->waypt[0].id) == ElementID(order->waypt[1].id))
00222 {
00223
00224 ROS_INFO("already reached all way-points in order, halting");
00225 return halt->control(pcmd);
00226 }
00227
00228 #if 0 // not doing blockage timer yet
00229
00230 obstacle->update_blockage_state();
00231
00232
00233 if (obstacle->blockage_timeout())
00234 {
00235 obstacle->unblocked();
00236 if (escape)
00237 {
00238 ART_MSG(1, "Run controller blocked, try to escape.");
00239 begin_escape();
00240 }
00241 else
00242 ART_MSG(1, "Run controller blocked, but not trying to escape.");
00243 }
00244 #endif // no blockage timeout
00245
00246 switch (go_state)
00247 {
00248 #if 0 // not doing Escape yet
00249 case Escape:
00250 {
00251 float blockage_distance =
00252 Euclidean::DistanceTo(blockage_pose, estimate->pos);
00253
00254 if (!escape_timer->Check()
00255 && blockage_distance < escape_distance)
00256 {
00257
00258 result_t result = unstuck->control(pcmd);
00259 if (result != Finished)
00260 return result;
00261
00262 ART_MSG(1, "Unstuck controller returns Finished");
00263 }
00264
00265 ART_MSG(1, "Car moved %.3fm since blocking, try to replan.",
00266 blockage_distance);
00267 escape_timer->Cancel();
00268 last_replan = order->replan_num;
00269 set_go_state(Replan);
00270
00271 }
00272 #endif // no escape yet
00273
00274 case Replan:
00275 if (order->replan_num == last_replan)
00276 {
00277 ElementID start_id = starting_waypt();
00278 navdata->replan_waypt = start_id.toMapID();
00279 if (start_id == ElementID())
00280 {
00281 ROS_WARN("Unable to replan from here, keep trying to escape.");
00282 begin_escape();
00283 }
00284 return halt->control(pcmd);
00285 }
00286
00287
00288 navdata->replan_waypt = ElementID().toMapID();
00289 road->reset();
00290 set_go_state(Continue);
00291
00292
00293 case Continue:
00294
00295 ROS_DEBUG("Running, invoking Road controller");
00296 return road->control(pcmd);
00297
00298 default:
00299
00300 return NotImplemented;
00301 }
00302 }
00303
00304
00305 void Run::reset(void)
00306 {
00307 go_state = Continue;
00308
00309 halt->reset();
00310 road->reset();
00311
00312 #if NOT_PORTED_TO_ROS
00313 safety->reset();
00314 #endif
00315 }
00316
00318 void Run::set_go_state(state_t newstate)
00319 {
00320 if (go_state != newstate)
00321 {
00322 ROS_DEBUG("Go behavior state changing from %s to %s",
00323 state_name[go_state], state_name[newstate]);
00324 go_state = newstate;
00325 }
00326 }
00327
00334 ElementID Run::starting_waypt(void)
00335 {
00336 ElementID waypt;
00337
00338 #if NOT_PORTED_TO_ROS
00339
00340 segment_id_t starting_zone =
00341 ZoneOps::containing_zone(course->zones, MapXY(estimate->pos));
00342
00343 if (verbose >= 4)
00344 ART_MSG(2, "Run::starting_waypt() containing zone is %d", starting_zone);
00345
00346 if(starting_zone > 0) {
00347 ZonePerimeter zone = ZoneOps::get_zone_by_id(course->zones, starting_zone);
00348
00349 waypt = (ZoneOps::starting_node_for_zone(zone)).id;
00350
00351 if (waypt != ElementID())
00352 {
00353 ART_MSG(2, "Starting at %s in zone %d",
00354 waypt.name().str, zone.zone_id);
00355 return waypt;
00356 }
00357 }
00358 #endif
00359
00360
00361 int index = pops->getStartingPoly(MapPose(estimate->pose.pose),
00362 course->polygons,
00363 config_->initialize_distance,
00364 config_->initialize_min_angle);
00365
00366 if (index < 0)
00367 {
00368
00369 ROS_FATAL_STREAM("getStartingPoly() failed, returning " << index);
00370 }
00371 else
00372 {
00373 waypt = course->polygons[index].start_way;
00374 ROS_DEBUG_STREAM("starting_waypt() is " << waypt.name().str
00375 << ", polygon " << course->polygons[index].poly_id);
00376 }
00377
00378 return waypt;
00379 }