Skip to content

Commit e1b3eea

Browse files
Corrected QoS for transient local topiocs, also added log
1 parent f9693b2 commit e1b3eea

File tree

3 files changed

+78
-16
lines changed

3 files changed

+78
-16
lines changed

include/autoware_bridge/autonomous_driving.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class AutonomousDriving : public BaseTask
4848
OperationModeState operation_mode_state_;
4949
uint16_t vehicle_motion_state_;
5050
uint16_t route_state_;
51+
std::atomic<bool> change_operation_mode_response_received_;
5152

5253
rclcpp::Time driving_start_time_;
5354
rclcpp::Time halt_start_time_;
@@ -60,6 +61,7 @@ class AutonomousDriving : public BaseTask
6061

6162
// Client
6263
rclcpp::Client<ChangeOperationMode>::SharedPtr auto_drive_engage_client;
64+
rclcpp::Client<ChangeOperationMode>::SharedPtr auto_drive_disengage_client;
6365
rclcpp::Client<ClearRoute>::SharedPtr clear_route_client;
6466

6567
// callbacks
@@ -69,6 +71,7 @@ class AutonomousDriving : public BaseTask
6971

7072
// Helper methods
7173
void engageAutoDrive();
74+
void disengageAutoDrive();
7275
void cancelCurrentRoute();
7376
};
7477

src/autonomous_driving.cpp

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

134148
void 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

154182
void 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

159193
void 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-
}

src/route_planning.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,14 @@ void RoutePlanning::routeStateCallback(const RouteState & msg)
180180
void RoutePlanning::operationModeStateCallback(const OperationModeState & msg)
181181
{
182182
operation_mode_state_ = msg;
183-
RCLCPP_INFO(node_->get_logger(), "Operation mode state: %d", operation_mode_state_.is_autonomous_mode_available);
183+
RCLCPP_INFO(node_->get_logger(), "received /api/operation_mode/state: operation mode:%s",
184+
operation_mode_state_.mode == OperationModeState::UNKNOWN ? "UNKNOWN" :
185+
operation_mode_state_.mode == OperationModeState::STOP ? "STOP" :
186+
operation_mode_state_.mode == OperationModeState::AUTONOMOUS ? "AUTONOMOUS" :
187+
operation_mode_state_.mode == OperationModeState::LOCAL ? "LOCAL" :
188+
operation_mode_state_.mode == OperationModeState::REMOTE ? "REMOTE" : "(undefined)");
189+
RCLCPP_INFO(node_->get_logger(), "AUTONOMOUS MODE %s available.", operation_mode_state_.is_autonomous_mode_available ? "is" : "is not");
190+
//RCLCPP_INFO(node_->get_logger(), "Operation mode state (AUTONOMOUS mode avaiable?): %d", operation_mode_state_.is_autonomous_mode_available);
184191
}
185192

186193
void RoutePlanning::cancelCurrentRoute()

0 commit comments

Comments
 (0)