scenario_simulator_v2 C++ API
legacy_autoware_state.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CONCEALER__LEGACY_AUTOWARE_STATE_HPP_
16 #define CONCEALER__LEGACY_AUTOWARE_STATE_HPP_
17 
18 #if __has_include(<autoware_system_msgs/msg/autoware_state.hpp>)
19 #include <autoware_system_msgs/msg/autoware_state.hpp>
20 #endif
21 
22 #if __has_include(<autoware_adapi_v1_msgs/msg/localization_initialization_state.hpp>)
23 #include <autoware_adapi_v1_msgs/msg/localization_initialization_state.hpp>
24 #endif
25 
26 #if __has_include(<autoware_adapi_v1_msgs/msg/operation_mode_state.hpp>)
27 #include <autoware_adapi_v1_msgs/msg/operation_mode_state.hpp>
28 #endif
29 
30 #if __has_include(<autoware_adapi_v1_msgs/msg/route_state.hpp>)
31 #include <autoware_adapi_v1_msgs/msg/route_state.hpp>
32 #endif
33 
34 namespace concealer
35 {
37 {
38  enum value_type {
47  };
48 
50 
52 
53 #if __has_include(<autoware_system_msgs/msg/autoware_state.hpp>)
54  explicit LegacyAutowareState(const autoware_system_msgs::msg::AutowareState & autoware_state)
55  : value([&]() {
56  switch (autoware_state.state) {
57  case autoware_system_msgs::msg::AutowareState::INITIALIZING:
58  return initializing;
59  case autoware_system_msgs::msg::AutowareState::WAITING_FOR_ROUTE:
60  return waiting_for_route;
61  case autoware_system_msgs::msg::AutowareState::PLANNING:
62  return planning;
63  case autoware_system_msgs::msg::AutowareState::WAITING_FOR_ENGAGE:
64  return waiting_for_engage;
65  case autoware_system_msgs::msg::AutowareState::DRIVING:
66  return driving;
67  case autoware_system_msgs::msg::AutowareState::ARRIVED_GOAL:
68  return arrived_goal;
69  case autoware_system_msgs::msg::AutowareState::FINALIZING:
70  return finalizing;
71  }
72  }())
73  {
74  }
75 #endif
76 
77 #if __has_include(<autoware_adapi_v1_msgs/msg/localization_initialization_state.hpp>) and \
78  __has_include(<autoware_adapi_v1_msgs/msg/route_state.hpp>) and \
79  __has_include(<autoware_adapi_v1_msgs/msg/operation_mode_state.hpp>)
80  explicit LegacyAutowareState(
81  const autoware_adapi_v1_msgs::msg::LocalizationInitializationState & localization_state,
82  const autoware_adapi_v1_msgs::msg::RouteState & route_state,
83  const autoware_adapi_v1_msgs::msg::OperationModeState & operation_mode_state,
84  const rclcpp::Time & now)
85  : value([&]() {
86  /*
87  See
88  - https://github.com/autowarefoundation/autoware.universe/blob/e60daf7d1c85208eaac083b90c181e224c2ac513/system/autoware_default_adapi/document/autoware-state.md
89  - https://github.com/autowarefoundation/autoware_universe/blob/e60daf7d1c85208eaac083b90c181e224c2ac513/system/autoware_default_adapi/src/compatibility/autoware_state.cpp#L80-L141
90  */
91  if (
92  localization_state.state ==
93  autoware_adapi_v1_msgs::msg::LocalizationInitializationState::UNKNOWN or
94  route_state.state == autoware_adapi_v1_msgs::msg::RouteState::UNKNOWN or
95  operation_mode_state.mode == autoware_adapi_v1_msgs::msg::OperationModeState::UNKNOWN) {
96  return initializing;
97  } else {
98  switch (localization_state.state) {
99  case autoware_adapi_v1_msgs::msg::LocalizationInitializationState::UNINITIALIZED:
100  case autoware_adapi_v1_msgs::msg::LocalizationInitializationState::INITIALIZING:
101  return initializing;
102 
103  case autoware_adapi_v1_msgs::msg::LocalizationInitializationState::INITIALIZED:
104  switch (route_state.state) {
105  case autoware_adapi_v1_msgs::msg::RouteState::ARRIVED:
106  if (const auto duration = now - rclcpp::Time(route_state.stamp);
107  duration.seconds() < 2.0) {
108  return arrived_goal;
109  }
110  [[fallthrough]];
111 
112  case autoware_adapi_v1_msgs::msg::RouteState::UNSET:
113  return waiting_for_route;
114 
115  case autoware_adapi_v1_msgs::msg::RouteState::SET:
116  case autoware_adapi_v1_msgs::msg::RouteState::CHANGING:
117  switch (operation_mode_state.mode) {
118  case autoware_adapi_v1_msgs::msg::OperationModeState::AUTONOMOUS:
119  case autoware_adapi_v1_msgs::msg::OperationModeState::LOCAL:
120  case autoware_adapi_v1_msgs::msg::OperationModeState::REMOTE:
121  if (operation_mode_state.is_autoware_control_enabled) {
122  return driving;
123  }
124  [[fallthrough]];
125 
126  case autoware_adapi_v1_msgs::msg::OperationModeState::STOP:
127  return operation_mode_state.is_autonomous_mode_available ? waiting_for_engage
128  : planning;
129 
130  default:
131  return undefined;
132  }
133 
134  default:
135  return undefined;
136  }
137 
138  default:
139  return undefined;
140  }
141  }
142  }())
143  {
144  }
145 #endif
146 
147  constexpr operator const char *() const
148  {
149  switch (value) {
150  case initializing:
151  return "INITIALIZING";
152  case waiting_for_route:
153  return "WAITING_FOR_ROUTE";
154  case planning:
155  return "PLANNING";
156  case waiting_for_engage:
157  return "WAITING_FOR_ENGAGE";
158  case driving:
159  return "DRIVING";
160  case arrived_goal:
161  return "ARRIVED_GOAL";
162  case finalizing:
163  return "FINALIZING";
164  default:
165  return "";
166  }
167  }
168 
169  operator std::string() const { return operator const char *(); }
170 
171  friend auto operator<<(std::ostream & os, const LegacyAutowareState & state) -> std::ostream &
172  {
173  return os << static_cast<const char *>(state);
174  }
175 };
176 } // namespace concealer
177 
178 #endif // CONCEALER__LEGACY_AUTOWARE_STATE_HPP_
Definition: autoware_universe.hpp:40
std::string string
Definition: junit5.hpp:26
Definition: legacy_autoware_state.hpp:37
value_type value
Definition: legacy_autoware_state.hpp:49
constexpr LegacyAutowareState(value_type value)
Definition: legacy_autoware_state.hpp:51
friend auto operator<<(std::ostream &os, const LegacyAutowareState &state) -> std::ostream &
Definition: legacy_autoware_state.hpp:171
value_type
Definition: legacy_autoware_state.hpp:38
@ initializing
Definition: legacy_autoware_state.hpp:40
@ driving
Definition: legacy_autoware_state.hpp:44
@ planning
Definition: legacy_autoware_state.hpp:42
@ waiting_for_engage
Definition: legacy_autoware_state.hpp:43
@ waiting_for_route
Definition: legacy_autoware_state.hpp:41
@ finalizing
Definition: legacy_autoware_state.hpp:46
@ arrived_goal
Definition: legacy_autoware_state.hpp:45
@ undefined
Definition: legacy_autoware_state.hpp:39