Skip to content

Commit 5b05433

Browse files
committed
rnav: More flexible callback behavior
Added: - new INI file parameter - callback can now return a bool to reflect whether to cancel navigation or not
1 parent 86cb068 commit 5b05433

File tree

7 files changed

+48
-18
lines changed

7 files changed

+48
-18
lines changed

libs/nav/include/mrpt/nav/reactive/CAbstractNavigator.h

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ namespace mrpt
145145
double dist_to_target_for_sending_event; //!< Default value=0, means use the "targetAllowedDistance" passed by the user in the navigation request.
146146
double alarm_seems_not_approaching_target_timeout; //!< navigator timeout (seconds) [Default=30 sec]
147147
double dist_check_target_is_blocked; //!< (Default value=0.6) When closer than this distance, check if the target is blocked to abort navigation with an error.
148+
int hysteresis_check_target_is_blocked; // (Default=3) How many steps should the condition for dist_check_target_is_blocked be fulfilled to raise an event
148149

149150
virtual void loadFromConfigFile(const mrpt::utils::CConfigFileBase &c, const std::string &s) MRPT_OVERRIDE;
150151
virtual void saveToConfigFile(mrpt::utils::CConfigFileBase &c, const std::string &s) const MRPT_OVERRIDE;
@@ -159,6 +160,7 @@ namespace mrpt
159160
private:
160161
TState m_lastNavigationState; //!< Last internal state of navigator:
161162
bool m_navigationEndEventSent; //!< Will be false until the navigation end is sent, and it is reset with each new command
163+
int m_counter_check_target_is_blocked;
162164

163165
/** Called before starting a new navigation. Internally, it calls to child-implemented onStartNewNavigation() */
164166
void internal_onStartNewNavigation();

libs/nav/include/mrpt/nav/reactive/CRobot2NavInterface.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,11 @@ namespace mrpt
132132
/** Callback: Apparent collision event (i.e. there is at least one obstacle point inside the robot shape) */
133133
virtual void sendApparentCollisionEvent();
134134

135-
/** Callback: Target seems to be blocked by an obstacle. This event is invoked before ending navigation with an ERROR state and another call to sendWaySeemsBlockedEvent(). */
136-
virtual void sendCannotGetCloserToBlockedTargetEvent();
135+
/** Callback: Target seems to be blocked by an obstacle. If user
136+
* sets `do_abort_nav` to `true` (default is `false`), after this
137+
* callback returns, navigation will end with an ERROR state and
138+
* another call to sendWaySeemsBlockedEvent() will be done. */
139+
virtual void sendCannotGetCloserToBlockedTargetEvent(bool &do_abort_nav);
137140

138141
/** @} */
139142

libs/nav/src/reactive/CAbstractNavigator.cpp

+31-9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ CAbstractNavigator::CAbstractNavigator(CRobot2NavInterface &react_iterf_impl) :
8787
mrpt::utils::COutputLogger("MRPT_navigator"),
8888
m_lastNavigationState ( IDLE ),
8989
m_navigationEndEventSent(false),
90+
m_counter_check_target_is_blocked(0),
9091
m_navigationState ( IDLE ),
9192
m_navigationParams ( nullptr ),
9293
m_robot ( react_iterf_impl ),
@@ -354,20 +355,23 @@ bool CAbstractNavigator::stop(bool isEmergencyStop)
354355
CAbstractNavigator::TAbstractNavigatorParams::TAbstractNavigatorParams() :
355356
dist_to_target_for_sending_event(0),
356357
alarm_seems_not_approaching_target_timeout(30),
357-
dist_check_target_is_blocked(0.6)
358+
dist_check_target_is_blocked(0.6),
359+
hysteresis_check_target_is_blocked(3)
358360
{
359361
}
360362
void CAbstractNavigator::TAbstractNavigatorParams::loadFromConfigFile(const mrpt::utils::CConfigFileBase &c, const std::string &s)
361363
{
362364
MRPT_LOAD_CONFIG_VAR_CS(dist_to_target_for_sending_event, double);
363365
MRPT_LOAD_CONFIG_VAR_CS(alarm_seems_not_approaching_target_timeout, double);
364-
MRPT_LOAD_CONFIG_VAR_CS(dist_check_target_is_blocked, double);
366+
MRPT_LOAD_CONFIG_VAR_CS(dist_check_target_is_blocked, double);
367+
MRPT_LOAD_CONFIG_VAR_CS(hysteresis_check_target_is_blocked, int);
365368
}
366369
void CAbstractNavigator::TAbstractNavigatorParams::saveToConfigFile(mrpt::utils::CConfigFileBase &c, const std::string &s) const
367370
{
368371
MRPT_SAVE_CONFIG_VAR_COMMENT(dist_to_target_for_sending_event, "Default value=0, means use the `targetAllowedDistance` passed by the user in the navigation request.");
369372
MRPT_SAVE_CONFIG_VAR_COMMENT(alarm_seems_not_approaching_target_timeout, "navigator timeout (seconds) [Default=30 sec]");
370373
MRPT_SAVE_CONFIG_VAR_COMMENT(dist_check_target_is_blocked, "When closer than this distance, check if the target is blocked to abort navigation with an error. [Default=0.6 m]");
374+
MRPT_SAVE_CONFIG_VAR_COMMENT(hysteresis_check_target_is_blocked, "How many steps should the condition for dist_check_target_is_blocked be fulfilled to raise an event");
371375
}
372376

373377
bool mrpt::nav::operator==(const CAbstractNavigator::TNavigationParamsBase& a, const CAbstractNavigator::TNavigationParamsBase&b)
@@ -473,18 +477,36 @@ void CAbstractNavigator::performNavigationStepNavigating(bool call_virtual_nav_m
473477
}
474478

475479
// Check if the target seems to be at reach, but it's clearly occupied by obstacles:
476-
if ( (!m_navigationParams->target.targetIsIntermediaryWaypoint || m_navigationParams->target.targetDesiredRelSpeed < 1 ) &&
477-
targetDist < params_abstract_navigator.dist_check_target_is_blocked)
480+
if (targetDist < params_abstract_navigator.dist_check_target_is_blocked)
478481
{
479482
const auto rel_trg = m_navigationParams->target.target_coords - m_curPoseVel.pose;
480483
const bool is_col = checkCollisionWithLatestObstacles(rel_trg);
481484
if (is_col)
482485
{
483-
MRPT_LOG_WARN("Target seems to be blocked by obstacles. Aborting navigation.");
484-
m_navigationState = NAV_ERROR;
485-
m_robot.sendCannotGetCloserToBlockedTargetEvent();
486-
m_robot.sendWaySeemsBlockedEvent();
487-
return;
486+
const bool send_event = (++m_counter_check_target_is_blocked >= params_abstract_navigator.hysteresis_check_target_is_blocked);
487+
488+
if (send_event)
489+
{
490+
MRPT_LOG_THROTTLE_WARN(5.0,"Target seems to be blocked by obstacles. Invoking sendCannotGetCloserToBlockedTargetEvent().");
491+
bool do_abort_nav = false;
492+
m_robot.sendCannotGetCloserToBlockedTargetEvent(do_abort_nav);
493+
494+
if (do_abort_nav)
495+
{
496+
MRPT_LOG_WARN("sendCannotGetCloserToBlockedTargetEvent() told me to abort navigation.");
497+
m_navigationState = NAV_ERROR;
498+
m_robot.sendWaySeemsBlockedEvent();
499+
return;
500+
}
501+
else
502+
{
503+
m_counter_check_target_is_blocked = 0;
504+
}
505+
}
506+
}
507+
else
508+
{
509+
m_counter_check_target_is_blocked = 0;
488510
}
489511
}
490512
}

libs/nav/src/reactive/CRobot2NavInterface.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ void CRobot2NavInterface::sendNewWaypointTargetEvent(int waypoint_index)
6161
}
6262
void CRobot2NavInterface::sendNavigationEndDueToErrorEvent()
6363
{
64-
MRPT_LOG_INFO("[sendNavigationEndDueToErrorEvent] Doing nothing: not implemented in user's derived class.");
64+
MRPT_LOG_THROTTLE_INFO(1.0,"[sendNavigationEndDueToErrorEvent] Doing nothing: not implemented in user's derived class.");
6565
}
6666
void CRobot2NavInterface::sendWaySeemsBlockedEvent()
6767
{
68-
MRPT_LOG_INFO("[sendWaySeemsBlockedEvent] Doing nothing: not implemented in user's derived class.");
68+
MRPT_LOG_THROTTLE_INFO(1.0, "[sendWaySeemsBlockedEvent] Doing nothing: not implemented in user's derived class.");
6969
}
7070
void CRobot2NavInterface::sendApparentCollisionEvent()
7171
{
72-
MRPT_LOG_INFO("[sendApparentCollisionEvent] Doing nothing: not implemented in user's derived class.");
72+
MRPT_LOG_THROTTLE_INFO(1.0,"[sendApparentCollisionEvent] Doing nothing: not implemented in user's derived class.");
7373
}
74-
void CRobot2NavInterface::sendCannotGetCloserToBlockedTargetEvent()
74+
void CRobot2NavInterface::sendCannotGetCloserToBlockedTargetEvent(bool &do_abort_nav)
7575
{
76-
MRPT_LOG_INFO("[sendCannotGetCloserToBlockedTargetEvent] Doing nothing: not implemented in user's derived class.");
76+
MRPT_LOG_THROTTLE_INFO(1.0, "[sendCannotGetCloserToBlockedTargetEvent] Doing nothing: not implemented in user's derived class.");
7777
}
7878

7979
double CRobot2NavInterface::getNavigationTime() {

share/mrpt/config_files/navigation-ptgs/reactive2d_config.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
[CAbstractNavigator]
1515
dist_to_target_for_sending_event = 0.000000 // Default value=0, means use the `targetAllowedDistance` passed by the user in the navigation request.
1616
alarm_seems_not_approaching_target_timeout = 30.000000 // navigator timeout (seconds) [Default=30 sec]
17-
dist_check_target_is_blocked = 1.0 // When closer than this distance, check if the target is blocked to abort navigation with an error
17+
dist_check_target_is_blocked = 2.0 // When closer than this distance, check if the target is blocked to abort navigation with an error
18+
hysteresis_check_target_is_blocked = 3 // How many steps should the condition for dist_check_target_is_blocked be fulfilled to raise an event
1819

1920

2021
[CWaypointsNavigator]

share/mrpt/config_files/navigation-ptgs/reactive3d_config.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
[CAbstractNavigator]
1616
dist_to_target_for_sending_event = 0.000000 // Default value=0, means use the `targetAllowedDistance` passed by the user in the navigation request.
1717
alarm_seems_not_approaching_target_timeout = 30.000000 // navigator timeout (seconds) [Default=30 sec]
18-
dist_check_target_is_blocked = 1.0 // When closer than this distance, check if the target is blocked to abort navigation with an error
18+
dist_check_target_is_blocked = 2.0 // When closer than this distance, check if the target is blocked to abort navigation with an error
19+
hysteresis_check_target_is_blocked = 3 // How many steps should the condition for dist_check_target_is_blocked be fulfilled to raise an event
1920

2021

2122
[CWaypointsNavigator]

share/mrpt/config_files/navigation-ptgs/reactivenav-app-config.ini

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
dist_to_target_for_sending_event = 0.000000 // Default value=0, means use the `targetAllowedDistance` passed by the user in the navigation request.
2222
alarm_seems_not_approaching_target_timeout = 30.000000 // navigator timeout (seconds) [Default=30 sec]
2323
dist_check_target_is_blocked = 2.0 // When closer than this distance, check if the target is blocked to abort navigation with an error
24+
hysteresis_check_target_is_blocked = 3 // How many steps should the condition for dist_check_target_is_blocked be fulfilled to raise an event
2425

2526

2627
[DIFF_CWaypointsNavigator]

0 commit comments

Comments
 (0)