forked from autowarefoundation/autoware.universe
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathrtc_interface.cpp
470 lines (407 loc) · 15.1 KB
/
rtc_interface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Copyright 2022 Tier IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "autoware/rtc_interface/rtc_interface.hpp"
#include <chrono>
namespace
{
using tier4_rtc_msgs::msg::Module;
std::string uuid_to_string(const unique_identifier_msgs::msg::UUID & uuid)
{
std::stringstream ss;
for (auto i = 0; i < 16; ++i) {
ss << std::hex << std::setfill('0') << std::setw(2) << +uuid.uuid[i];
}
return ss.str();
}
std::string command_to_string(const uint8_t type)
{
if (type == tier4_rtc_msgs::msg::Command::ACTIVATE) {
return "ACTIVATE";
} else if (type == tier4_rtc_msgs::msg::Command::DEACTIVATE) {
return "DEACTIVATE";
}
throw std::domain_error("invalid rtc command.");
}
std::string state_to_string(const uint8_t type)
{
if (type == tier4_rtc_msgs::msg::State::WAITING_FOR_EXECUTION) {
return "WAITING_FOR_EXECUTION";
} else if (type == tier4_rtc_msgs::msg::State::RUNNING) {
return "RUNNING";
} else if (type == tier4_rtc_msgs::msg::State::ABORTING) {
return "ABORTING";
} else if (type == tier4_rtc_msgs::msg::State::SUCCEEDED) {
return "SUCCEEDED";
} else if (type == tier4_rtc_msgs::msg::State::FAILED) {
return "FAILED";
}
throw std::domain_error("invalid rtc state.");
}
Module getModuleType(const std::string & module_name)
{
Module module;
if (module_name == "blind_spot") {
module.type = Module::BLIND_SPOT;
} else if (module_name == "crosswalk") {
module.type = Module::CROSSWALK;
} else if (module_name == "detection_area") {
module.type = Module::DETECTION_AREA;
} else if (module_name == "intersection") {
module.type = Module::INTERSECTION;
} else if (module_name == "no_stopping_area") {
module.type = Module::NO_STOPPING_AREA;
} else if (module_name == "occlusion_spot") {
module.type = Module::OCCLUSION_SPOT;
} else if (module_name == "stop_line") {
module.type = Module::NONE;
} else if (module_name == "traffic_light") {
module.type = Module::TRAFFIC_LIGHT;
} else if (module_name == "virtual_traffic_light") {
module.type = Module::TRAFFIC_LIGHT;
} else if (module_name == "lane_change_left") {
module.type = Module::LANE_CHANGE_LEFT;
} else if (module_name == "lane_change_right") {
module.type = Module::LANE_CHANGE_RIGHT;
} else if (module_name == "external_request_lane_change_left") {
module.type = Module::EXT_REQUEST_LANE_CHANGE_LEFT;
} else if (module_name == "external_request_lane_change_right") {
module.type = Module::EXT_REQUEST_LANE_CHANGE_RIGHT;
} else if (module_name == "avoidance_by_lane_change_left") {
module.type = Module::AVOIDANCE_BY_LC_LEFT;
} else if (module_name == "avoidance_by_lane_change_right") {
module.type = Module::AVOIDANCE_BY_LC_RIGHT;
} else if (module_name == "static_obstacle_avoidance_left") {
module.type = Module::AVOIDANCE_LEFT;
} else if (module_name == "static_obstacle_avoidance_right") {
module.type = Module::AVOIDANCE_RIGHT;
} else if (module_name == "goal_planner") {
module.type = Module::GOAL_PLANNER;
} else if (module_name == "start_planner") {
module.type = Module::START_PLANNER;
} else if (module_name == "intersection_occlusion") {
module.type = Module::INTERSECTION_OCCLUSION;
}
return module;
}
} // namespace
namespace autoware::rtc_interface
{
RTCInterface::RTCInterface(rclcpp::Node * node, const std::string & name, const bool enable_rtc)
: clock_{node->get_clock()},
logger_{node->get_logger().get_child("RTCInterface[" + name + "]")},
is_auto_mode_enabled_{!enable_rtc},
is_locked_{false}
{
using std::placeholders::_1;
using std::placeholders::_2;
constexpr double update_rate = 10.0;
const auto period_ns = rclcpp::Rate(update_rate).period();
timer_ = rclcpp::create_timer(
node, node->get_clock(), period_ns, std::bind(&RTCInterface::onTimer, this));
// Publisher
pub_statuses_ =
node->create_publisher<CooperateStatusArray>(cooperate_status_namespace_ + "/" + name, 1);
pub_auto_mode_status_ =
node->create_publisher<AutoModeStatus>(auto_mode_status_namespace_ + "/" + name, 1);
// Service
callback_group_ = node->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
srv_commands_ = node->create_service<CooperateCommands>(
cooperate_commands_namespace_ + "/" + name,
std::bind(&RTCInterface::onCooperateCommandService, this, _1, _2),
rmw_qos_profile_services_default, callback_group_);
srv_auto_mode_ = node->create_service<AutoMode>(
enable_auto_mode_namespace_ + "/" + name,
std::bind(&RTCInterface::onAutoModeService, this, _1, _2), rmw_qos_profile_services_default,
callback_group_);
// Module
module_ = getModuleType(name);
}
void RTCInterface::publishCooperateStatus(const rclcpp::Time & stamp)
{
std::lock_guard<std::mutex> lock(mutex_);
registered_status_.stamp = stamp;
pub_statuses_->publish(registered_status_);
}
void RTCInterface::onCooperateCommandService(
const CooperateCommands::Request::SharedPtr request,
const CooperateCommands::Response::SharedPtr responses)
{
std::lock_guard<std::mutex> lock(mutex_);
responses->responses = validateCooperateCommands(request->commands);
if (isLocked()) {
stored_commands_ = request->commands;
return;
}
updateCooperateCommandStatus(request->commands);
}
std::vector<CooperateResponse> RTCInterface::validateCooperateCommands(
const std::vector<CooperateCommand> & commands)
{
std::vector<CooperateResponse> responses;
for (const auto & command : commands) {
CooperateResponse response;
response.uuid = command.uuid;
response.module = command.module;
const auto itr = std::find_if(
registered_status_.statuses.begin(), registered_status_.statuses.end(),
[command](auto & s) { return s.uuid == command.uuid; });
if (itr != registered_status_.statuses.end()) {
if (itr->state.type == State::WAITING_FOR_EXECUTION || itr->state.type == State::RUNNING) {
response.success = true;
} else {
RCLCPP_WARN_STREAM(
getLogger(), "[validateCooperateCommands] uuid : "
<< uuid_to_string(command.uuid)
<< " state is not WAITING_FOR_EXECUTION or RUNNING. state : "
<< itr->state.type << std::endl);
response.success = false;
}
} else {
RCLCPP_WARN_STREAM(
getLogger(), "[validateCooperateCommands] uuid : " << uuid_to_string(command.uuid)
<< " is not found." << std::endl);
response.success = false;
}
responses.push_back(response);
}
return responses;
}
void RTCInterface::updateCooperateCommandStatus(const std::vector<CooperateCommand> & commands)
{
for (const auto & command : commands) {
const auto itr = std::find_if(
registered_status_.statuses.begin(), registered_status_.statuses.end(),
[command](auto & s) { return s.uuid == command.uuid; });
// Update command if the command has been already received
if (itr != registered_status_.statuses.end()) {
if (itr->state.type == State::WAITING_FOR_EXECUTION || itr->state.type == State::RUNNING) {
itr->command_status = command.command;
itr->auto_mode = false;
}
}
}
}
void RTCInterface::onAutoModeService(
const AutoMode::Request::SharedPtr request, const AutoMode::Response::SharedPtr response)
{
std::lock_guard<std::mutex> lock(mutex_);
is_auto_mode_enabled_ = request->enable;
for (auto & status : registered_status_.statuses) {
status.auto_mode = request->enable;
}
response->success = true;
}
void RTCInterface::onTimer()
{
AutoModeStatus auto_mode_status;
auto_mode_status.module = module_;
auto_mode_status.is_auto_mode = is_auto_mode_enabled_;
pub_auto_mode_status_->publish(auto_mode_status);
}
void RTCInterface::updateCooperateStatus(
const UUID & uuid, const bool safe, const uint8_t state, const double start_distance,
const double finish_distance, const rclcpp::Time & stamp)
{
std::lock_guard<std::mutex> lock(mutex_);
// Find registered status which has same uuid
auto itr = std::find_if(
registered_status_.statuses.begin(), registered_status_.statuses.end(),
[uuid](auto & s) { return s.uuid == uuid; });
// If there is no registered status, add it
if (itr == registered_status_.statuses.end()) {
CooperateStatus status;
status.stamp = stamp;
status.uuid = uuid;
status.module = module_;
status.safe = safe;
status.command_status.type = Command::DEACTIVATE;
status.state.type = state;
status.start_distance = start_distance;
status.finish_distance = finish_distance;
status.auto_mode = is_auto_mode_enabled_;
registered_status_.statuses.push_back(status);
return;
}
// If the registered status is found, update status
itr->stamp = stamp;
itr->safe = safe;
itr->state.type = state;
itr->start_distance = start_distance;
itr->finish_distance = finish_distance;
}
void RTCInterface::removeCooperateStatus(const UUID & uuid)
{
std::lock_guard<std::mutex> lock(mutex_);
removeStoredCommand(uuid);
// Find registered status which has same uuid and erase it
const auto itr = std::find_if(
registered_status_.statuses.begin(), registered_status_.statuses.end(),
[uuid](auto & s) { return s.uuid == uuid; });
if (itr != registered_status_.statuses.end()) {
registered_status_.statuses.erase(itr);
return;
}
RCLCPP_WARN_STREAM(
getLogger(),
"[removeCooperateStatus] uuid : " << uuid_to_string(uuid) << " is not found." << std::endl);
}
void RTCInterface::removeStoredCommand(const UUID & uuid)
{
// Find stored command which has same uuid and erase it
const auto itr = std::find_if(
stored_commands_.begin(), stored_commands_.end(), [uuid](auto & s) { return s.uuid == uuid; });
if (itr != stored_commands_.end()) {
stored_commands_.erase(itr);
return;
}
}
void RTCInterface::removeExpiredCooperateStatus()
{
std::lock_guard<std::mutex> lock(mutex_);
const auto itr = std::remove_if(
registered_status_.statuses.begin(), registered_status_.statuses.end(),
[this](const auto & status) { return (clock_->now() - status.stamp).seconds() > 10.0; });
registered_status_.statuses.erase(itr, registered_status_.statuses.end());
}
void RTCInterface::clearCooperateStatus()
{
std::lock_guard<std::mutex> lock(mutex_);
registered_status_.statuses.clear();
stored_commands_.clear();
}
bool RTCInterface::isActivated(const UUID & uuid) const
{
std::lock_guard<std::mutex> lock(mutex_);
const auto itr = std::find_if(
registered_status_.statuses.begin(), registered_status_.statuses.end(),
[uuid](auto & s) { return s.uuid == uuid; });
if (itr != registered_status_.statuses.end()) {
if (itr->state.type == State::FAILED || itr->state.type == State::SUCCEEDED) {
return false;
}
if (itr->auto_mode) {
return itr->safe;
} else {
return itr->command_status.type == Command::ACTIVATE;
}
}
RCLCPP_WARN_STREAM(
getLogger(), "[isActivated] uuid : " << uuid_to_string(uuid) << " is not found." << std::endl);
return false;
}
bool RTCInterface::isForceActivated(const UUID & uuid) const
{
std::lock_guard<std::mutex> lock(mutex_);
const auto itr = std::find_if(
registered_status_.statuses.begin(), registered_status_.statuses.end(),
[uuid](const auto & s) { return s.uuid == uuid; });
if (itr != registered_status_.statuses.end()) {
if (itr->state.type != State::WAITING_FOR_EXECUTION && itr->state.type != State::RUNNING) {
return false;
}
if (itr->command_status.type == Command::ACTIVATE && !itr->safe) {
return true;
} else {
return false;
}
}
RCLCPP_WARN_STREAM(
getLogger(),
"[isForceActivated] uuid : " << uuid_to_string(uuid) << " is not found" << std::endl);
return false;
}
bool RTCInterface::isForceDeactivated(const UUID & uuid) const
{
std::lock_guard<std::mutex> lock(mutex_);
const auto itr = std::find_if(
registered_status_.statuses.begin(), registered_status_.statuses.end(),
[uuid](const auto & s) { return s.uuid == uuid; });
if (itr != registered_status_.statuses.end()) {
if (itr->state.type != State::RUNNING) {
return false;
}
if (itr->command_status.type == Command::DEACTIVATE && itr->safe && !itr->auto_mode) {
return true;
} else {
return false;
}
}
RCLCPP_WARN_STREAM(
getLogger(),
"[isForceDeactivated] uuid : " << uuid_to_string(uuid) << " is not found" << std::endl);
return false;
}
bool RTCInterface::isRegistered(const UUID & uuid) const
{
std::lock_guard<std::mutex> lock(mutex_);
const auto itr = std::find_if(
registered_status_.statuses.begin(), registered_status_.statuses.end(),
[uuid](auto & s) { return s.uuid == uuid; });
return itr != registered_status_.statuses.end();
}
bool RTCInterface::isRTCEnabled(const UUID & uuid) const
{
std::lock_guard<std::mutex> lock(mutex_);
const auto itr = std::find_if(
registered_status_.statuses.begin(), registered_status_.statuses.end(),
[uuid](auto & s) { return s.uuid == uuid; });
if (itr != registered_status_.statuses.end()) {
return !itr->auto_mode;
}
RCLCPP_WARN_STREAM(
getLogger(), "[isRTCEnabled] uuid : " << uuid_to_string(uuid) << " is not found." << std::endl);
return is_auto_mode_enabled_;
}
bool RTCInterface::isTerminated(const UUID & uuid) const
{
std::lock_guard<std::mutex> lock(mutex_);
const auto itr = std::find_if(
registered_status_.statuses.begin(), registered_status_.statuses.end(),
[uuid](auto & s) { return s.uuid == uuid; });
if (itr != registered_status_.statuses.end()) {
return itr->state.type == State::SUCCEEDED || itr->state.type == State::FAILED;
}
RCLCPP_WARN_STREAM(
getLogger(), "[isTerminated] uuid : " << uuid_to_string(uuid) << " is not found." << std::endl);
return is_auto_mode_enabled_;
}
void RTCInterface::lockCommandUpdate()
{
is_locked_ = true;
}
void RTCInterface::unlockCommandUpdate()
{
is_locked_ = false;
updateCooperateCommandStatus(stored_commands_);
}
rclcpp::Logger RTCInterface::getLogger() const
{
return logger_;
}
bool RTCInterface::isLocked() const
{
return is_locked_;
}
void RTCInterface::print() const
{
RCLCPP_INFO_STREAM(getLogger(), "---print rtc cooperate statuses---" << std::endl);
for (const auto status : registered_status_.statuses) {
RCLCPP_INFO_STREAM(
getLogger(), "uuid:" << uuid_to_string(status.uuid)
<< " command:" << command_to_string(status.command_status.type)
<< std::boolalpha << " safe:" << status.safe
<< " state:" << state_to_string(status.state.type) << std::endl);
}
}
} // namespace autoware::rtc_interface