scenario_simulator_v2 C++ API
transition_assertion.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__TRANSITION_ASSERTION_HPP_
16 #define CONCEALER__TRANSITION_ASSERTION_HPP_
17 
18 #include <chrono>
19 #include <rclcpp/node.hpp>
20 #include <rclcpp/rate.hpp>
22 
23 namespace concealer
24 {
25 template <typename Autoware>
27 {
28  const std::chrono::steady_clock::time_point start;
29 
30  const std::chrono::seconds initialize_duration;
31 
33 
35  : start(std::chrono::steady_clock::now()), initialize_duration([]() {
36  auto node = rclcpp::Node("get_parameter", "simulation");
37  node.declare_parameter("initialize_duration", 0);
38  return node.get_parameter("initialize_duration").as_int();
39  }())
40  {
41  }
42 
43 #define DEFINE_WAIT_FOR_AUTOWARE_STATE_TO_BE(STATE) \
44  template <typename Thunk = void (*)(), typename Interval = std::chrono::seconds> \
45  auto waitForAutowareStateToBe##STATE( \
46  Thunk && thunk = [] {}, Interval interval = std::chrono::seconds(1)) \
47  { \
48  for (thunk(); not static_cast<const Autoware &>(*this).isStopRequested() and \
49  not static_cast<const Autoware &>(*this).is##STATE(); \
50  rclcpp::GenericRate<std::chrono::steady_clock>(interval).sleep()) { \
51  if ( \
52  have_never_been_engaged and \
53  start + initialize_duration <= std::chrono::steady_clock::now()) { \
54  const auto state = static_cast<const Autoware &>(*this).getAutowareStateName(); \
55  throw common::AutowareError( \
56  "Simulator waited for the Autoware state to transition to " #STATE \
57  ", but time is up. The current Autoware state is ", \
58  (state.empty() ? "not published yet" : state)); \
59  } else { \
60  thunk(); \
61  } \
62  } \
63  if constexpr (std::string_view(#STATE) == std::string_view("Driving")) { \
64  have_never_been_engaged = false; \
65  } \
66  } \
67  static_assert(true)
68 
76 
77 #undef DEFINE_WAIT_FOR_AUTOWARE_STATE_TO_BE
78 };
79 } // namespace concealer
80 
81 #endif // CONCEALER__TRANSITION_ASSERTION_HPP_
Definition: autoware.hpp:30
Definition: cache.hpp:27
Definition: transition_assertion.hpp:27
DEFINE_WAIT_FOR_AUTOWARE_STATE_TO_BE(Planning)
DEFINE_WAIT_FOR_AUTOWARE_STATE_TO_BE(Finalizing)
DEFINE_WAIT_FOR_AUTOWARE_STATE_TO_BE(ArrivedGoal)
DEFINE_WAIT_FOR_AUTOWARE_STATE_TO_BE(Initializing)
TransitionAssertion()
Definition: transition_assertion.hpp:34
DEFINE_WAIT_FOR_AUTOWARE_STATE_TO_BE(WaitingForRoute)
DEFINE_WAIT_FOR_AUTOWARE_STATE_TO_BE(WaitingForEngage)
bool have_never_been_engaged
Definition: transition_assertion.hpp:32
const std::chrono::seconds initialize_duration
Definition: transition_assertion.hpp:30
const std::chrono::steady_clock::time_point start
Definition: transition_assertion.hpp:28