1616
1717#include < smacc2/smacc.hpp>
1818#include < geometry_msgs/msg/pose_with_covariance_stamped.hpp>
19- #include < cl_nav2z/client_behaviors/cb_wait_transform .hpp>
19+ #include < cl_nav2z/client_behaviors/cb_wait_nav2_nodes .hpp>
2020
2121namespace sm_nav2_unit_test_1
2222{
@@ -25,32 +25,83 @@ using namespace cl_nav2z;
2525using namespace cl_keyboard ;
2626using namespace smacc2 ::default_transition_tags;
2727
28- // Forward declaration
28+ // Custom transition tag for retry
29+ struct RETRY : SUCCESS {};
30+
31+ // Custom event for max retries exceeded
32+ template <typename TSource, typename TOrthogonal>
33+ struct EvMaxRetriesExceeded : sc::event<EvMaxRetriesExceeded<TSource, TOrthogonal>>
34+ {
35+ };
36+
37+ // Forward declarations
2938struct StNavigateToWaypoint1 ;
39+ struct StFinalState ;
3040
3141// STATE DECLARATION
3242struct StSetInitialPose : smacc2::SmaccState<StSetInitialPose, SmNav2UnitTest1>
3343{
3444 using SmaccState::SmaccState;
3545
46+ // Retry configuration: MAX_RETRIES means number of retries after initial attempt
47+ // Total attempts = 1 (initial) + MAX_RETRIES = 4
48+ static constexpr int MAX_RETRIES = 3 ;
49+ static constexpr int MAX_ATTEMPTS = 1 + MAX_RETRIES; // 4 total attempts
50+ static constexpr const char * ATTEMPT_COUNTER_KEY = " localization_attempt_count" ;
51+
3652 // TRANSITION TABLE
3753 typedef mpl::list<
38- Transition<EvCbSuccess<CbWaitTransform, OrNavigation>, StNavigateToWaypoint1, SUCCESS>,
39- Transition<EvCbFailure<CbWaitTransform, OrNavigation>, StNavigateToWaypoint1, ABORT>
54+ // Success: proceed to navigation when Nav2 nodes are ready
55+ Transition<EvCbSuccess<CbWaitNav2Nodes, OrNavigation>, StNavigateToWaypoint1, SUCCESS>,
56+ // Failure with retries remaining: self-transition to try again
57+ Transition<EvCbFailure<CbWaitNav2Nodes, OrNavigation>, StSetInitialPose, RETRY>,
58+ // Max retries exceeded: abort to final state
59+ Transition<EvMaxRetriesExceeded<CbWaitNav2Nodes, OrNavigation>, StFinalState, ABORT>
4060 > reactions;
4161
4262 // STATE FUNCTIONS
4363 static void staticConfigure ()
4464 {
45- // Wait for map->base_link transform (10 second timeout)
46- configure_orthogonal<OrNavigation, CbWaitTransform>(" base_link" , " map" , rclcpp::Duration (10 , 0 ));
65+ // Wait for Nav2 nodes to be active (PlannerServer, ControllerServer, BtNavigator)
66+ // This ensures the navigation stack is ready before attempting to navigate
67+ configure_orthogonal<OrNavigation, CbWaitNav2Nodes>();
4768
4869 // Keyboard behavior for manual control
4970 configure_orthogonal<OrKeyboard, CbDefaultKeyboardBehavior>();
5071 }
5172
5273 void runtimeConfigure ()
5374 {
75+ // Increment attempt counter first
76+ int attemptCount = 0 ;
77+ this ->getGlobalSMData (ATTEMPT_COUNTER_KEY, attemptCount);
78+ attemptCount++;
79+ this ->setGlobalSMData (ATTEMPT_COUNTER_KEY, attemptCount);
80+
81+ // Check if max attempts exceeded (1 initial + MAX_RETRIES)
82+ if (attemptCount > MAX_ATTEMPTS)
83+ {
84+ RCLCPP_ERROR (
85+ getLogger (),
86+ " StSetInitialPose: All %d attempts failed, aborting mission" , MAX_ATTEMPTS);
87+ // Post abort event - this will be processed and transition to StFinalState
88+ this ->postEvent <EvMaxRetriesExceeded<CbWaitNav2Nodes, OrNavigation>>();
89+ return ;
90+ }
91+
92+ // Log current attempt
93+ if (attemptCount == 1 )
94+ {
95+ RCLCPP_INFO (getLogger (), " StSetInitialPose: Initial attempt (1 of %d)" , MAX_ATTEMPTS);
96+ }
97+ else
98+ {
99+ RCLCPP_WARN (
100+ getLogger (),
101+ " StSetInitialPose: Retry %d of %d (attempt %d of %d)" ,
102+ attemptCount - 1 , MAX_RETRIES, attemptCount, MAX_ATTEMPTS);
103+ }
104+
54105 RCLCPP_INFO (getLogger (), " StSetInitialPose: runtimeConfigure() - Setting initial pose for AMCL" );
55106
56107 // Set initial pose for AMCL localization
@@ -81,12 +132,25 @@ struct StSetInitialPose : smacc2::SmaccState<StSetInitialPose, SmNav2UnitTest1>
81132
82133 void onEntry ()
83134 {
84- RCLCPP_INFO (getLogger (), " StSetInitialPose: onEntry() - Waiting for map->base_link transform " );
135+ RCLCPP_INFO (getLogger (), " StSetInitialPose: onEntry() - Waiting for Nav2 nodes to be active " );
85136 }
86137
87138 void onExit ()
88139 {
89- RCLCPP_INFO (getLogger (), " StSetInitialPose: onExit() - Localization ready" );
140+ RCLCPP_INFO (getLogger (), " StSetInitialPose: onExit()" );
141+ }
142+
143+ // Reset attempt counter on successful exit to navigation
144+ void onExit (SUCCESS)
145+ {
146+ // Clear attempt counter on success
147+ this ->setGlobalSMData (ATTEMPT_COUNTER_KEY, 0 );
148+ RCLCPP_INFO (getLogger (), " StSetInitialPose: Localization successful, proceeding to navigation" );
149+ }
150+
151+ void onExit (ABORT)
152+ {
153+ RCLCPP_ERROR (getLogger (), " StSetInitialPose: Localization failed after max retries, aborting" );
90154 }
91155};
92156
0 commit comments