Skip to content

Commit 662eca1

Browse files
committed
modify decider request status interface
Signed-off-by: Takagi, Isamu <isamu.takagi@tier4.jp>
1 parent aea4c34 commit 662eca1

21 files changed

+304
-147
lines changed

system/autoware_command_mode_decider/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ autoware_package()
66

77
ament_auto_add_library(${PROJECT_NAME} SHARED
88
"src/command_mode_conversion.cpp"
9+
"src/command_mode_status.cpp"
910
"src/command_mode_decider_base.cpp"
1011
"src/command_mode_decider.cpp"
1112
)

system/autoware_command_mode_decider/launch/decider.launch.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<remap from="~/command_mode/request" to="/system/command_mode/request"/>
77
<remap from="~/operation_mode/state" to="/system/operation_mode/state"/>
88
<remap from="~/operation_mode/change_operation_mode" to="/system/operation_mode/change_operation_mode"/>
9-
<!-- TODO(Takagi, Isamu): MRM state. -->
10-
<!-- TODO(Takagi, Isamu): MRM request. -->
9+
<remap from="~/mrm/state" to="/system/fail_safe/mrm_state"/>
10+
<remap from="~/mrm/request" to="/system/fail_safe/mrm_request"/>
1111
</node>
1212
</launch>

system/autoware_command_mode_decider/src/command_mode_decider.cpp

+10-22
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,21 @@ CommandModeDecider::CommandModeDecider(const rclcpp::NodeOptions & options)
2727
std::string CommandModeDecider::decide_command_mode()
2828
{
2929
const auto command_mode_status = get_command_mode_status();
30-
const auto target_operation_mode = get_target_operation_mode();
31-
const auto target_mrm = get_target_mrm();
30+
const auto request_mode_status = get_request_mode_status();
3231

3332
// Use the requested MRM if available.
3433
{
35-
const auto iter = command_mode_status.find(target_mrm);
36-
if (iter != command_mode_status.end()) {
37-
const auto [mode, status] = *iter;
38-
if (status.available) {
39-
return mode;
40-
}
34+
const auto status = command_mode_status.get(request_mode_status.mrm);
35+
if (status.available) {
36+
return request_mode_status.mrm;
4137
}
4238
}
4339

4440
// Use the specified operation mode if available.
4541
{
46-
const auto iter = command_mode_status.find(target_operation_mode);
47-
if (iter != command_mode_status.end()) {
48-
const auto [mode, status] = *iter;
49-
if (status.available) {
50-
return mode;
51-
}
42+
const auto status = command_mode_status.get(request_mode_status.operation_mode);
43+
if (status.available) {
44+
return request_mode_status.operation_mode;
5245
}
5346
}
5447

@@ -59,19 +52,14 @@ std::string CommandModeDecider::decide_command_mode()
5952
const auto comfortable_stop = "comfortable_stop";
6053
const auto emergency_stop = "emergency_stop";
6154

62-
const auto is_available = [](const auto & command_mode_status, const auto & mode) {
63-
const auto iter = command_mode_status.find(mode);
64-
return iter == command_mode_status.end() ? false : iter->second.available;
65-
};
66-
6755
// TODO(Takagi, Isamu): check command_modes parameter
68-
if (is_available(command_mode_status, pull_over) /*&& use_pull_over_*/) {
56+
if (command_mode_status.get(pull_over).available /*&& use_pull_over_*/) {
6957
return pull_over;
7058
}
71-
if (is_available(command_mode_status, comfortable_stop) /*&& use_comfortable_stop_*/) {
59+
if (command_mode_status.get(comfortable_stop).available /*&& use_comfortable_stop_*/) {
7260
return comfortable_stop;
7361
}
74-
if (is_available(command_mode_status, emergency_stop)) {
62+
if (command_mode_status.get(emergency_stop).available) {
7563
return emergency_stop;
7664
}
7765

system/autoware_command_mode_decider/src/command_mode_decider_base.cpp

+92-76
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ CommandModeDeciderBase::CommandModeDeciderBase(const rclcpp::NodeOptions & optio
2727
: Node("command_mode_decider", options)
2828
{
2929
is_modes_ready_ = false;
30-
target_operation_mode_ = declare_parameter<std::string>("initial_operation_mode");
31-
target_mrm_ = std::string();
32-
curr_command_mode_ = std::string();
30+
command_mode_status_.init(declare_parameter<std::vector<std::string>>("command_modes"));
3331
command_mode_request_stamp_ = std::nullopt;
3432

35-
const auto command_modes = declare_parameter<std::vector<std::string>>("command_modes");
36-
for (const auto & mode : command_modes) {
37-
// NOTE: The mode field will be used to check topic reception.
38-
command_mode_status_[mode] = CommandModeStatusItem();
39-
}
33+
request_.autoware_control = true;
34+
request_.operation_mode = declare_parameter<std::string>("initial_operation_mode");
35+
request_.mrm = std::string();
36+
37+
decided_.autoware_control = true;
38+
decided_.command_mode = std::string();
39+
40+
current_.autoware_control = true;
41+
current_.command_mode = std::string();
4042

4143
using std::placeholders::_1;
4244
using std::placeholders::_2;
@@ -54,7 +56,12 @@ CommandModeDeciderBase::CommandModeDeciderBase(const rclcpp::NodeOptions & optio
5456
srv_operation_mode_ = create_service<ChangeOperationMode>(
5557
"~/operation_mode/change_operation_mode",
5658
std::bind(&CommandModeDeciderBase::on_change_operation_mode, this, _1, _2));
57-
srv_request_mrm_ = create_service<RequestMrm>(
59+
srv_autoware_control_ = create_service<ChangeAutowareControl>(
60+
"~/operation_mode/change_autoware_control",
61+
std::bind(&CommandModeDeciderBase::on_change_autoware_control, this, _1, _2));
62+
63+
pub_mrm_state_ = create_publisher<MrmState>("~/mrm/state", rclcpp::QoS(1));
64+
srv_mrm_request_ = create_service<RequestMrm>(
5865
"~/mrm/request", std::bind(&CommandModeDeciderBase::on_request_mrm, this, _1, _2));
5966

6067
const auto period = rclcpp::Rate(declare_parameter<double>("update_rate")).period();
@@ -63,11 +70,13 @@ CommandModeDeciderBase::CommandModeDeciderBase(const rclcpp::NodeOptions & optio
6370

6471
void CommandModeDeciderBase::on_status(const CommandModeStatus & msg)
6572
{
73+
// Update command mode status.
6674
for (const auto & item : msg.items) {
67-
const auto iter = command_mode_status_.find(item.mode);
68-
if (iter == command_mode_status_.end()) continue;
69-
iter->second = item;
75+
command_mode_status_.set(item);
7076
}
77+
78+
// Check if all command mode status items are ready.
79+
is_modes_ready_ = is_modes_ready_ ? true : command_mode_status_.ready();
7180
update_command_mode();
7281
}
7382

@@ -77,8 +86,10 @@ void CommandModeDeciderBase::on_timer()
7786
return;
7887
}
7988

89+
/*
8090
const auto & status = command_mode_status_.at(curr_command_mode_);
8191
(void)status;
92+
*/
8293

8394
/*
8495
if (mode.status.activation) {
@@ -97,29 +108,88 @@ void CommandModeDeciderBase::on_timer()
97108
*/
98109
}
99110

111+
void CommandModeDeciderBase::update_command_mode()
112+
{
113+
if (!is_modes_ready_) {
114+
return;
115+
}
116+
117+
const auto stamp = now();
118+
119+
// Decide target command mode.
120+
bool is_command_mode_changed = false;
121+
{
122+
const auto next_mode = decide_command_mode();
123+
const auto curr_mode = decided_.command_mode;
124+
is_command_mode_changed = curr_mode != next_mode;
125+
126+
if (is_command_mode_changed) {
127+
const auto curr_text = "'" + curr_mode + "'";
128+
const auto next_text = "'" + next_mode + "'";
129+
RCLCPP_INFO_STREAM(get_logger(), "mode changed: " << curr_text << " -> " << next_text);
130+
}
131+
decided_.command_mode = next_mode;
132+
}
133+
134+
// Request command mode to switcher nodes.
135+
if (is_command_mode_changed) {
136+
CommandModeRequest msg;
137+
msg.stamp = stamp;
138+
msg.ctrl = decided_.autoware_control;
139+
msg.mode = decided_.command_mode;
140+
pub_command_mode_request_->publish(msg);
141+
142+
command_mode_request_stamp_ = stamp;
143+
}
144+
145+
// Update operation mode status.
146+
const auto is_available = [this](const auto & mode) {
147+
return command_mode_status_.get(mode).available;
148+
};
149+
OperationModeState state;
150+
state.stamp = stamp;
151+
state.mode = text_to_mode(decided_.command_mode);
152+
state.is_autoware_control_enabled = true; // TODO(Takagi, Isamu): subscribe
153+
state.is_in_transition = false; // TODO(Takagi, Isamu): check status is enabled
154+
state.is_stop_mode_available = is_available("stop");
155+
state.is_autonomous_mode_available = is_available("autonomous");
156+
state.is_local_mode_available = is_available("local");
157+
state.is_remote_mode_available = is_available("remote");
158+
pub_operation_mode_->publish(state);
159+
}
160+
161+
void CommandModeDeciderBase::on_change_autoware_control(
162+
ChangeAutowareControl::Request::SharedPtr req, ChangeAutowareControl::Response::SharedPtr res)
163+
{
164+
// TODO(Takagi, Isamu): Commonize on_change_operation_mode and on_request_mrm.
165+
// TODO(Takagi, Isamu): Check is_modes_ready_.
166+
(void)req;
167+
(void)res;
168+
}
169+
100170
void CommandModeDeciderBase::on_change_operation_mode(
101171
ChangeOperationMode::Request::SharedPtr req, ChangeOperationMode::Response::SharedPtr res)
102172
{
103173
// TODO(Takagi, Isamu): Commonize on_change_operation_mode and on_request_mrm.
174+
// TODO(Takagi, Isamu): Check is_modes_ready_.
104175

105176
const auto mode = mode_to_text(req->mode);
106-
const auto iter = command_mode_status_.find(mode);
107-
if (iter == command_mode_status_.end()) {
177+
const auto item = command_mode_status_.get(mode);
178+
if (item.mode.empty()) {
108179
RCLCPP_WARN_STREAM(get_logger(), "invalid mode name: " << mode);
109180
res->status.success = false;
110181
res->status.message = "invalid mode name: " + mode;
111182
return;
112183
}
113184

114-
const auto status = iter->second;
115-
if (!status.available) {
185+
if (!item.available) {
116186
RCLCPP_WARN_STREAM(get_logger(), "mode is not available: " << mode);
117187
res->status.success = false;
118188
res->status.message = "mode is not available: " + mode;
119189
return;
120190
}
121191

122-
target_operation_mode_ = mode;
192+
request_.operation_mode = mode;
123193
res->status.success = true;
124194

125195
update_command_mode();
@@ -129,82 +199,28 @@ void CommandModeDeciderBase::on_request_mrm(
129199
RequestMrm::Request::SharedPtr req, RequestMrm::Response::SharedPtr res)
130200
{
131201
// TODO(Takagi, Isamu): Commonize on_change_operation_mode and on_request_mrm.
202+
// TODO(Takagi, Isamu): Check is_modes_ready_.
132203

133204
const auto mode = req->name;
134-
const auto iter = command_mode_status_.find(mode);
135-
if (iter == command_mode_status_.end()) {
205+
const auto item = command_mode_status_.get(mode);
206+
if (item.mode.empty()) {
136207
RCLCPP_WARN_STREAM(get_logger(), "invalid mode name: " << mode);
137208
res->status.success = false;
138209
res->status.message = "invalid mode name: " + mode;
139210
return;
140211
}
141212

142-
const auto status = iter->second;
143-
if (!status.available) {
213+
if (!item.available) {
144214
RCLCPP_WARN_STREAM(get_logger(), "mode is not available: " << mode);
145215
res->status.success = false;
146216
res->status.message = "mode is not available: " + mode;
147217
return;
148218
}
149219

150-
target_mrm_ = mode;
220+
request_.mrm = mode;
151221
res->status.success = true;
152222

153223
update_command_mode();
154224
}
155225

156-
void CommandModeDeciderBase::update_command_mode()
157-
{
158-
if (!is_modes_ready_) {
159-
for (const auto & [mode, status] : command_mode_status_) {
160-
if (status.mode.empty()) {
161-
return;
162-
}
163-
}
164-
is_modes_ready_ = true;
165-
}
166-
167-
const auto stamp = now();
168-
169-
// Decide target command mode.
170-
bool is_command_mode_changed = false;
171-
{
172-
const auto next_command_mode = decide_command_mode();
173-
is_command_mode_changed = curr_command_mode_ != next_command_mode;
174-
if (is_command_mode_changed) {
175-
const auto curr_text = "'" + curr_command_mode_ + "'";
176-
const auto next_text = "'" + next_command_mode + "'";
177-
RCLCPP_INFO_STREAM(
178-
get_logger(), "command mode changed: " << curr_text << " -> " << next_text);
179-
}
180-
curr_command_mode_ = next_command_mode;
181-
}
182-
183-
// Request command mode to switcher nodes.
184-
if (is_command_mode_changed) {
185-
CommandModeRequest msg;
186-
msg.stamp = stamp;
187-
msg.mode = curr_command_mode_;
188-
pub_command_mode_request_->publish(msg);
189-
190-
command_mode_request_stamp_ = stamp;
191-
}
192-
193-
// Update operation mode status.
194-
const auto is_available = [this](const auto & mode) {
195-
const auto iter = command_mode_status_.find(mode);
196-
return iter == command_mode_status_.end() ? false : iter->second.available;
197-
};
198-
OperationModeState state;
199-
state.stamp = stamp;
200-
state.mode = text_to_mode(target_operation_mode_);
201-
state.is_autoware_control_enabled = true; // TODO(Takagi, Isamu): subscribe
202-
state.is_in_transition = false; // TODO(Takagi, Isamu): check status is enabled
203-
state.is_stop_mode_available = is_available("stop");
204-
state.is_autonomous_mode_available = is_available("autonomous");
205-
state.is_local_mode_available = is_available("local");
206-
state.is_remote_mode_available = is_available("remote");
207-
pub_operation_mode_->publish(state);
208-
}
209-
210226
} // namespace autoware::command_mode_decider

0 commit comments

Comments
 (0)