Skip to content

Commit dd87f6f

Browse files
committed
Progress implementing events (cc #10)
1 parent f578e9f commit dd87f6f

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

libselfdriving/include/selfdriving/algos/NavEngine.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ class NavEngine : public mrpt::system::COutputLogger
127127
* user in the navigation request. */
128128
double dist_to_target_for_sending_event{0};
129129

130-
/** navigator timeout (seconds) [Default=30 sec] */
131-
double alarm_seems_not_approaching_target_timeout{30};
132130

133131
/** (Default value=0.6) When closer than this distance, check if the
134132
* target is blocked to abort navigation with an error. */
@@ -149,6 +147,11 @@ class NavEngine : public mrpt::system::COutputLogger
149147
double maxDistanceForTargetApproach = 1.5; // [m]
150148
double maxRelativeHeadingForTargetApproach = 180.0_deg; // [rad]
151149

150+
/** Navigation timeout (seconds) [Default=30 sec]
151+
* See description of VehicleMotionInterface::on_path_seems_blocked()
152+
*/
153+
double timeoutNotGettingCloserGoal = 30;
154+
152155
bool generateNavLogFiles = false;
153156

154157
/** Actual files will be
@@ -447,6 +450,8 @@ class NavEngine : public mrpt::system::COutputLogger
447450
activePlanPath.clear();
448451
activePlanPathEdges.clear();
449452
pathPlannerTargetWpIdx.reset();
453+
lastDistanceToGoalTimestamp.reset();
454+
lastDistanceToGoal.reset();
450455
}
451456
}
452457

@@ -487,6 +492,11 @@ class NavEngine : public mrpt::system::COutputLogger
487492
navlogDebugMessages.clear();
488493
}
489494

495+
/** Values used to check against
496+
* Configuration::timeoutNotGettingCloserGoal
497+
*/
498+
std::optional<double> lastDistanceToGoalTimestamp, lastDistanceToGoal;
499+
490500
/** @} */
491501

492502
/** For sending an alarm (error event) when it seems that we are not

libselfdriving/include/selfdriving/interfaces/VehicleMotionInterface.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ class VehicleMotionInterface : public mrpt::system::COutputLogger,
164164
{
165165
MRPT_LOG_INFO("Default stop_watchdog() called.");
166166
}
167-
virtual void start_watchdog([
168-
[maybe_unused]] const size_t periodMilliseconds)
167+
virtual void start_watchdog(
168+
[[maybe_unused]] const size_t periodMilliseconds)
169169
{
170170
MRPT_LOG_INFO("Default start_watchdog() called.");
171171
}
@@ -184,13 +184,15 @@ class VehicleMotionInterface : public mrpt::system::COutputLogger,
184184
{
185185
MRPT_LOG_WARN("Default on_nav_end_due_to_error() called.");
186186
}
187+
187188
/**
188189
* @brief Callback upon starting a new waypointsequence navigation
189190
*/
190191
virtual void on_nav_start()
191192
{
192193
MRPT_LOG_WARN("Default on_nav_start() event handler called.");
193194
}
195+
194196
/**
195197
* @brief Callback if navigation ended by an accepted trigger or reached the
196198
* last specified waypoint
@@ -199,16 +201,25 @@ class VehicleMotionInterface : public mrpt::system::COutputLogger,
199201
{
200202
MRPT_LOG_WARN("Default on_nav_end() event handler called.");
201203
}
204+
202205
/**
203-
* @brief Callback when NavEngine encounters a blocked way
204-
* Could lead to path replan
206+
* @brief Callback for when NavEngine cannot make progress to get
207+
* increasingly closer to the final target during a certain period of time.
208+
* It may indicate that the high-level path the vehicle is trying to follow
209+
* is no longer valid due to a blocked way. On your user side, you could
210+
* call NavEngine::cancel() and/or re-compute an alternative path and issue
211+
* a new set of navigation waypoints or just report the error to the user.
212+
*
213+
* \sa NavEngine::Configuration::timeoutNotGettingCloserGoal
205214
*/
206215
virtual void on_path_seems_blocked()
207216
{
208217
MRPT_LOG_WARN("Default on_path_seems_blocked() event handler called.");
209218
}
219+
210220
/**
211-
* @brief Callback when the NavEngine predicts a collision with an obstacle
221+
* @brief Callback when the NavEngine *predicts* a collision with an
222+
* obstacle and needed to issue a stop command.
212223
*/
213224
virtual void on_apparent_collision()
214225
{
@@ -228,6 +239,7 @@ class VehicleMotionInterface : public mrpt::system::COutputLogger,
228239
"handler called (event='%s').",
229240
waypoint_index, reached_skipped ? "reached" : "skipped");
230241
}
242+
231243
/**
232244
* @brief Callback when NavEngine cannot reach a specified target location
233245
* because there are obstacles at the specified target

libselfdriving/src/algos/NavEngine.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ void NavEngine::suspend()
266266
if (navigationStatus_ == NavStatus::NAVIGATING)
267267
{
268268
navigationStatus_ = NavStatus::SUSPENDED;
269+
innerState_.active_plan_reset(true);
269270

270271
if (config_.vehicleMotionInterface)
271272
{
@@ -393,7 +394,9 @@ void NavEngine::update_robot_kinematic_state()
393394
innerState_.latestPoses.begin()->first,
394395
innerState_.latestPoses.rbegin()->first) >
395396
PREVIOUS_POSES_MAX_AGE)
396-
{ innerState_.latestPoses.erase(innerState_.latestPoses.begin()); }
397+
{
398+
innerState_.latestPoses.erase(innerState_.latestPoses.begin());
399+
}
397400
while (innerState_.latestOdomPoses.size() > 1 &&
398401
mrpt::system::timeDifference(
399402
innerState_.latestOdomPoses.begin()->first,
@@ -927,7 +930,9 @@ void NavEngine::check_new_planner_output()
927930

928931
// Merge or overwrite current plan:
929932
if (result.startingFromCurrentPlanNode.has_value())
930-
{ merge_new_plan_if_better(result); }
933+
{
934+
merge_new_plan_if_better(result);
935+
}
931936
else
932937
{
933938
MRPT_LOG_INFO_STREAM("Taking new path planning result.");
@@ -1748,6 +1753,35 @@ bool NavEngine::approach_target_controller()
17481753

17491754
const auto atrw = internal_check_about_to_reach_stop_wp();
17501755

1756+
// Check for no-getting-closer timeout here, since we have the distance to
1757+
// goal:
1758+
if (!_.lastDistanceToGoal.has_value() ||
1759+
atrw.distanceToWaypoint < *_.lastDistanceToGoal)
1760+
{
1761+
// Good: we are making progress:
1762+
_.lastDistanceToGoal = atrw.distanceToWaypoint;
1763+
_.lastDistanceToGoalTimestamp =
1764+
config_.vehicleMotionInterface->robot_time();
1765+
}
1766+
else if (_.lastDistanceToGoalTimestamp.has_value())
1767+
{
1768+
// we are not making progress:
1769+
const double age = config_.vehicleMotionInterface->robot_time() -
1770+
*_.lastDistanceToGoalTimestamp;
1771+
if (age > config_.timeoutNotGettingCloserGoal)
1772+
{
1773+
MRPT_LOG_INFO_FMT(
1774+
"Triggering on_path_seems_blocked() event since distance to "
1775+
"goal could not get shorter than %f for %f seconds.",
1776+
_.lastDistanceToGoal.value(), age);
1777+
1778+
pendingEvents_.emplace_back([this]() {
1779+
config_.vehicleMotionInterface->on_path_seems_blocked();
1780+
});
1781+
}
1782+
}
1783+
1784+
// check about-to-reach-waypoint return values:
17511785
if (atrw.aboutToReach)
17521786
{
17531787
MRPT_LOG_DEBUG_STREAM(

0 commit comments

Comments
 (0)