@@ -13,22 +13,25 @@ AutonomousDriving::AutonomousDriving(
1313 operation_mode_state_(),
1414 vehicle_motion_state_(MotionState::UNKNOWN),
1515 route_state_(RouteState::UNKNOWN),
16+ change_operation_mode_response_received_(false ),
1617 driving_start_time_(rclcpp::Time(0 )),
1718 halt_start_time_(rclcpp::Time(0 ))
1819{
1920 // Initialize autonomous client
20- auto_drive_engage_client = node_->create_client <autoware_adapi_v1_msgs::srv::ChangeOperationMode>(
21+ auto_drive_engage_client = node_->create_client <autoware_adapi_v1_msgs::srv::ChangeOperationMode>(
2122 " /api/operation_mode/change_to_autonomous" );
22- clear_route_client =node_->create_client <autoware_adapi_v1_msgs::srv::ClearRoute>(" /api/routing/clear_route" );
23+ auto_drive_disengage_client = node_->create_client <autoware_adapi_v1_msgs::srv::ChangeOperationMode>(
24+ " /api/operation_mode/change_to_stop" );
25+ clear_route_client = node_->create_client <autoware_adapi_v1_msgs::srv::ClearRoute>(" /api/routing/clear_route" );
2326 // Initialize subscribers
2427 operation_mode_state_sub_ = node_->create_subscription <OperationModeState>(
25- " /api/operation_mode/state" , 10 ,
28+ " /api/operation_mode/state" , rclcpp::QoS ( 1 ). transient_local () ,
2629 std::bind (&AutonomousDriving::operationModeStateCallback, this , std::placeholders::_1));
2730 vehicle_motion_state_sub_ = node_->create_subscription <MotionState>(
28- " /api/motion/state" , 10 ,
31+ " /api/motion/state" , rclcpp::QoS ( 1 ). transient_local () ,
2932 std::bind (&AutonomousDriving::vehicleMotionStateCallback, this , std::placeholders::_1));
3033 route_state_sub_ = node_->create_subscription <RouteState>(
31- " /api/routing/state" , 10 ,
34+ " /api/routing/state" , rclcpp::QoS ( 1 ). transient_local () ,
3235 std::bind (&AutonomousDriving::routeStateCallback, this , std::placeholders::_1));
3336}
3437
@@ -46,8 +49,10 @@ void AutonomousDriving::execute(
4649 std::lock_guard<std::mutex> lock (task_mutex_);
4750
4851 if (is_cancel_requested_) {
49- // CANCEL
5052 autoware_bridge_util_->updateTaskStatus (task_id, " CANCELLED" );
53+ // Changed OperationMode to STOP
54+ disengageAutoDrive ();
55+ // CANCEL
5156 cancelCurrentRoute ();
5257 RCLCPP_INFO (node_->get_logger (), " Driving task %s cancelled." , task_id.c_str ());
5358 break ;
@@ -75,7 +80,6 @@ void AutonomousDriving::execute(
7580
7681 if (retry_counter <= MAX_DRIVE_RETRIES) {
7782 engageAutoDrive ();
78- driving_start_time_ = node_->get_clock ()->now ();
7983 state_ = AutonomousDrivingTaskState::WAIT_AUTO_DRIVE_READY;
8084 autoware_bridge_util_->updateTaskRetries (task_id, retry_counter);
8185 retry_counter++;
@@ -85,14 +89,24 @@ void AutonomousDriving::execute(
8589 break ;
8690
8791 case AutonomousDrivingTaskState::WAIT_AUTO_DRIVE_READY:
88- if (operation_mode_state_.mode == OperationModeState::AUTONOMOUS) {
92+ if (change_operation_mode_response_received_ &&
93+ operation_mode_state_.mode == OperationModeState::AUTONOMOUS) {
94+ RCLCPP_INFO (node_->get_logger (), " Autonomous driving triggered successfully. OperationModeState:%s" ,
95+ operation_mode_state_.mode == OperationModeState::UNKNOWN ? " UNKNOWN" :
96+ operation_mode_state_.mode == OperationModeState::STOP ? " STOP" :
97+ operation_mode_state_.mode == OperationModeState::AUTONOMOUS ? " AUTONOMOUS" :
98+ operation_mode_state_.mode == OperationModeState::LOCAL ? " LOCAL" :
99+ operation_mode_state_.mode == OperationModeState::REMOTE ? " REMOTE" : " (undefined)" );
89100 state_ = AutonomousDrivingTaskState::DRIVING;
90101 }
91- // Timer for 10 seconds and retry if the mode is not autonomous
92- else if (
93- node_->get_clock ()->now ().seconds () - driving_start_time_.seconds () >
94- DRIVE_WAIT_TIMEOUT_S) {
95- RCLCPP_ERROR_THROTTLE (node_->get_logger (), *node_->get_clock (), 1000 , " Driving error, timeout expired" );
102+ else if (node_->get_clock ()->now ().seconds () - driving_start_time_.seconds () > DRIVE_WAIT_TIMEOUT_S) {
103+ // Timer for 10 seconds and retry if the mode is not autonomous
104+ RCLCPP_ERROR (node_->get_logger (), " Driving error, timeout expired. OperationModeState:%s" ,
105+ operation_mode_state_.mode == OperationModeState::UNKNOWN ? " UNKNOWN" :
106+ operation_mode_state_.mode == OperationModeState::STOP ? " STOP" :
107+ operation_mode_state_.mode == OperationModeState::AUTONOMOUS ? " AUTONOMOUS" :
108+ operation_mode_state_.mode == OperationModeState::LOCAL ? " LOCAL" :
109+ operation_mode_state_.mode == OperationModeState::REMOTE ? " REMOTE" : " (undefined)" );
96110 state_ = AutonomousDrivingTaskState::ENGAGE_AUTO_DRIVE;
97111 }
98112 break ;
@@ -133,10 +147,13 @@ void AutonomousDriving::cancel()
133147
134148void AutonomousDriving::engageAutoDrive ()
135149{
150+ change_operation_mode_response_received_ = false ; // atomic
151+
136152 /* auto_drive_engage_client is a ROS2 service client
137153 Its purpose is to send a request to a service(in this case,
138154 /api/operation_mode/change_to_autonomous) that instructs the vehicle or control system to
139155 switch to autonomous mode. service server is responsible for processing the request*/
156+
140157 while (!auto_drive_engage_client->wait_for_service (1s)) {
141158 if (!rclcpp::ok ()) {
142159 RCLCPP_ERROR (
@@ -146,14 +163,31 @@ void AutonomousDriving::engageAutoDrive()
146163 RCLCPP_INFO (node_->get_logger (), " auto_drive_engage service not available, waiting again..." );
147164 }
148165 auto request = std::make_shared<ChangeOperationMode::Request>();
149- auto future_result = auto_drive_engage_client->async_send_request (request);
166+ auto future = auto_drive_engage_client->async_send_request (request);
167+ driving_start_time_ = node_->get_clock ()->now ();
168+
169+ RCLCPP_INFO (node_->get_logger (), " send request to change_to_autonomous service." );
170+
171+ if (future.wait_for (std::chrono::duration<double >(DRIVE_WAIT_TIMEOUT_S)) == std::future_status::ready) {
172+ change_operation_mode_response_received_ = true ; // atomic
173+ RCLCPP_INFO (node_->get_logger (), " received response from change_to_autonomous service." );
174+ RCLCPP_INFO (node_->get_logger (), " auto_drive_engage service server responded." );
175+ } else {
176+ RCLCPP_INFO (node_->get_logger (), " timeout for response from change_to_autonomous service." );
177+ }
150178
151179 // It's good practice to add a maximum retry limit in case the service is not available.
152180}
153181
154182void AutonomousDriving::operationModeStateCallback (const OperationModeState msg)
155183{
156184 operation_mode_state_ = msg;
185+ RCLCPP_INFO (node_->get_logger (), " received /api/operation_mode/state: operation mode:%s" ,
186+ operation_mode_state_.mode == OperationModeState::UNKNOWN ? " UNKNOWN" :
187+ operation_mode_state_.mode == OperationModeState::STOP ? " STOP" :
188+ operation_mode_state_.mode == OperationModeState::AUTONOMOUS ? " AUTONOMOUS" :
189+ operation_mode_state_.mode == OperationModeState::LOCAL ? " LOCAL" :
190+ operation_mode_state_.mode == OperationModeState::REMOTE ? " REMOTE" : " (undefined)" );
157191}
158192
159193void AutonomousDriving::vehicleMotionStateCallback (const MotionState msg)
@@ -179,5 +213,23 @@ void AutonomousDriving::cancelCurrentRoute()
179213 auto request = std::make_shared<ClearRoute::Request>();
180214
181215 auto future_result = clear_route_client->async_send_request (request);
216+ future_result.wait_for (1s); // you may resurrect this line for graceful cancelling
217+ }
218+
219+ void AutonomousDriving::disengageAutoDrive ()
220+ {
221+ while (!auto_drive_disengage_client->wait_for_service (1s)) {
222+ if (!rclcpp::ok ()) {
223+ RCLCPP_ERROR (
224+ node_->get_logger (), " Interrupted while waiting for change_to_stop service. Exiting." );
225+ return ;
226+ }
227+ RCLCPP_INFO (node_->get_logger (), " change_to_stop service not available, waiting again..." );
228+ }
229+ auto request = std::make_shared<ChangeOperationMode::Request>();
230+ auto future_result = auto_drive_disengage_client->async_send_request (request);
231+ RCLCPP_INFO (node_->get_logger (), " send request to change_to_stop service." );
232+ future_result.wait_for (1s);
233+ RCLCPP_INFO (node_->get_logger (), " received response from change_to_stop service." );
234+ }
182235
183- }
0 commit comments