scenario_simulator_v2 C++ API
simulator_core.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 OPENSCENARIO_INTERPRETER__SIMULATOR_CORE_HPP_
16 #define OPENSCENARIO_INTERPRETER__SIMULATOR_CORE_HPP_
17 
30 
32 {
34 
36 
38 
40 
42 {
43  static inline std::unique_ptr<traffic_simulator::API> core = nullptr;
44 
45 public:
46  template <typename Node, typename... Ts>
47  static auto activate(
48  const Node & node, const traffic_simulator::Configuration & configuration, Ts &&... xs) -> void
49  {
50  if (not active()) {
51  core = std::make_unique<traffic_simulator::API>(
52  node, configuration, std::forward<decltype(xs)>(xs)...);
53  } else {
54  throw Error("The simulator core has already been instantiated.");
55  }
56  }
57 
58  static auto active() { return static_cast<bool>(core); }
59 
60  static auto deactivate() -> void
61  {
62  if (active()) {
63  core->despawnEntities();
64  core->closeZMQConnection();
65  core.reset();
66  }
67  }
68 
69  static auto update() -> void { core->updateFrame(); }
70 
72  {
73  protected:
74  static auto canonicalize(const traffic_simulator::LaneletPose & non_canonicalized)
76  {
77  return NativeLanePosition(non_canonicalized);
78  }
79 
80  template <typename T, typename std::enable_if_t<std::is_same_v<T, NativeLanePosition>, int> = 0>
81  static auto convert(const NativeWorldPosition & pose) -> NativeLanePosition
82  {
83  constexpr bool include_crosswalk{false};
84  if (
85  const auto result =
86  traffic_simulator::pose::toCanonicalizedLaneletPose(pose, include_crosswalk)) {
87  return result.value();
88  } else {
89  throw Error(
90  "The specified WorldPosition = [", pose.position.x, ", ", pose.position.y, ", ",
91  pose.position.z,
92  "] could not be approximated to the proper Lane. Perhaps the "
93  "WorldPosition points to a location where multiple lanes overlap, and "
94  "there are at least two or more candidates for a LanePosition that "
95  "can be approximated to that WorldPosition. This issue can be "
96  "resolved by strictly specifying the location using LanePosition "
97  "instead of WorldPosition");
98  }
99  }
100 
101  template <
102  typename T, typename std::enable_if_t<std::is_same_v<T, NativeWorldPosition>, int> = 0>
103  static auto convert(const NativeLanePosition & native_lane_position) -> NativeWorldPosition
104  {
105  return traffic_simulator::pose::toMapPose(native_lane_position);
106  }
107 
109  const std::string & from_entity_name, const std::string & to_entity_name)
110  {
111  if (const auto from_entity = core->getEntityPointer(from_entity_name)) {
112  if (const auto to_entity = core->getEntityPointer(to_entity_name)) {
113  if (
114  const auto relative_pose = traffic_simulator::pose::relativePose(
115  from_entity->getMapPose(), to_entity->getMapPose()))
116  return relative_pose.value();
117  }
118  }
120  }
121 
123  const std::string & from_entity_name, const NativeWorldPosition & to_map_pose)
124  {
125  if (const auto from_entity = core->getEntityPointer(from_entity_name)) {
126  if (
127  const auto relative_pose =
128  traffic_simulator::pose::relativePose(from_entity->getMapPose(), to_map_pose)) {
129  return relative_pose.value();
130  }
131  }
133  }
134 
136  const NativeWorldPosition & from_map_pose, const std::string & to_entity_name)
137  {
138  if (const auto to_entity = core->getEntityPointer(to_entity_name)) {
139  if (
140  const auto relative_pose =
141  traffic_simulator::pose::relativePose(from_map_pose, to_entity->getMapPose())) {
142  return relative_pose.value();
143  }
144  }
146  }
147 
149  const std::string & from_entity_name, const std::string & to_entity_name,
150  const RoutingAlgorithm::value_type routing_algorithm = RoutingAlgorithm::undefined)
152  {
153  if (const auto to_entity = core->getEntityPointer(to_entity_name)) {
154  if (const auto to_lanelet_pose = to_entity->getCanonicalizedLaneletPose()) {
156  from_entity_name, to_lanelet_pose.value(), routing_algorithm);
157  }
158  }
160  }
161 
163  const std::string & from_entity_name, const NativeLanePosition & to_lanelet_pose,
164  const RoutingAlgorithm::value_type routing_algorithm = RoutingAlgorithm::undefined)
166  {
167  if (const auto from_entity = core->getEntityPointer(from_entity_name)) {
168  if (const auto from_lanelet_pose = from_entity->getCanonicalizedLaneletPose()) {
170  from_lanelet_pose.value(), to_lanelet_pose, routing_algorithm);
171  }
172  }
174  }
175 
177  const NativeLanePosition & from_lanelet_pose, const NativeLanePosition & to_lanelet_pose,
178  const RoutingAlgorithm::value_type routing_algorithm = RoutingAlgorithm::undefined)
180  {
181  traffic_simulator::RoutingConfiguration routing_configuration;
182  routing_configuration.allow_lane_change =
183  (routing_algorithm == RoutingAlgorithm::value_type::shortest);
185  from_lanelet_pose, to_lanelet_pose, routing_configuration, core->getHdmapUtils());
186  }
187 
189  const std::string & from_entity_name, const std::string & to_entity_name,
190  const RoutingAlgorithm::value_type routing_algorithm = RoutingAlgorithm::undefined)
191  {
192  if (const auto from_entity = core->getEntityPointer(from_entity_name)) {
193  if (const auto to_entity = core->getEntityPointer(to_entity_name)) {
194  if (const auto from_lanelet_pose = from_entity->getCanonicalizedLaneletPose()) {
195  if (const auto to_lanelet_pose = to_entity->getCanonicalizedLaneletPose()) {
197  from_lanelet_pose.value(), from_entity->getBoundingBox(), to_lanelet_pose.value(),
198  to_entity->getBoundingBox(), routing_algorithm);
199  }
200  }
201  }
202  }
204  }
205 
207  const std::string & from_entity_name, const NativeLanePosition & to_lanelet_pose,
208  const RoutingAlgorithm::value_type routing_algorithm = RoutingAlgorithm::undefined)
209  {
210  if (const auto from_entity = core->getEntityPointer(from_entity_name)) {
211  if (const auto from_lanelet_pose = from_entity->getCanonicalizedLaneletPose()) {
213  from_lanelet_pose.value(), from_entity->getBoundingBox(), to_lanelet_pose,
214  traffic_simulator_msgs::msg::BoundingBox(), routing_algorithm);
215  }
216  }
218  }
219 
221  const NativeLanePosition & from_lanelet_pose,
222  const traffic_simulator_msgs::msg::BoundingBox & from_bounding_box,
223  const NativeLanePosition & to_lanelet_pose,
224  const traffic_simulator_msgs::msg::BoundingBox & to_bounding_box,
225  const RoutingAlgorithm::value_type routing_algorithm = RoutingAlgorithm::undefined)
227  {
228  traffic_simulator::RoutingConfiguration routing_configuration;
229  routing_configuration.allow_lane_change =
230  (routing_algorithm == RoutingAlgorithm::value_type::shortest);
232  from_lanelet_pose, from_bounding_box, to_lanelet_pose, to_bounding_box,
233  routing_configuration, core->getHdmapUtils());
234  }
235 
237  const std::string & from_entity_name, const std::string & to_entity_name)
238  {
239  if (const auto from_entity = core->getEntityPointer(from_entity_name)) {
240  if (const auto to_entity = core->getEntityPointer(to_entity_name)) {
241  if (
242  const auto relative_pose = traffic_simulator::pose::boundingBoxRelativePose(
243  from_entity->getMapPose(), from_entity->getBoundingBox(), to_entity->getMapPose(),
244  to_entity->getBoundingBox())) {
245  return relative_pose.value();
246  }
247  }
248  }
250  }
251 
253  const std::string & from_entity_name, const NativeWorldPosition & to_map_pose)
254  {
255  if (const auto from_entity = core->getEntityPointer(from_entity_name)) {
256  if (
257  const auto relative_pose = traffic_simulator::pose::boundingBoxRelativePose(
258  from_entity->getMapPose(), from_entity->getBoundingBox(), to_map_pose,
260  return relative_pose.value();
261  }
262  }
264  }
265 
267  const std::string & from_entity_name, const std::string & to_entity_name,
268  const RoutingAlgorithm::value_type routing_algorithm = RoutingAlgorithm::undefined) -> int
269  {
270  if (
271  const auto from_lanelet_pose =
272  core->getEntity(from_entity_name).getCanonicalizedLaneletPose()) {
273  if (
274  const auto to_lanelet_pose =
275  core->getEntity(to_entity_name).getCanonicalizedLaneletPose()) {
276  traffic_simulator::RoutingConfiguration routing_configuration;
277  routing_configuration.allow_lane_change =
278  (routing_algorithm == RoutingAlgorithm::value_type::shortest);
279  if (
280  const auto lane_changes = traffic_simulator::distance::countLaneChanges(
281  from_lanelet_pose.value(), to_lanelet_pose.value(), routing_configuration,
282  core->getHdmapUtils())) {
283  return lane_changes.value().first - lane_changes.value().second;
284  }
285  }
286  }
287  throw common::Error(
288  "Failed to evaluate lateral relative lanes between ", from_entity_name, " and ",
289  to_entity_name);
290  }
291  };
292 
293  class ActionApplication // OpenSCENARIO 1.1.1 Section 3.1.5
294  {
295  protected:
296  template <typename... Ts>
297  static auto applyAcquirePositionAction(const std::string & entity_ref, Ts &&... xs)
298  {
299  return core->getEntity(entity_ref).requestAcquirePosition(std::forward<decltype(xs)>(xs)...);
300  }
301 
302  template <typename... Ts>
303  static auto applyAddEntityAction(Ts &&... xs)
304  {
305  return core->spawn(std::forward<decltype(xs)>(xs)...);
306  }
307 
308  template <typename EntityRef, typename DynamicConstraints>
309  static auto applyProfileAction(
310  const EntityRef & entity_ref, const DynamicConstraints & dynamic_constraints) -> void
311  {
312  auto & entity = core->getEntity(entity_ref);
313  entity.setBehaviorParameter([&]() {
314  auto behavior_parameter = entity.getBehaviorParameter();
315 
316  // clang-format off
317  const auto setIfNotInf = [](auto & target, const auto & value) {
318  if (not std::isinf(value)) {
319  target = value;
320  }
321  };
322  setIfNotInf(behavior_parameter.dynamic_constraints.max_speed, dynamic_constraints.max_speed);
323  setIfNotInf(behavior_parameter.dynamic_constraints.max_acceleration, dynamic_constraints.max_acceleration);
324  setIfNotInf(behavior_parameter.dynamic_constraints.max_acceleration_rate, dynamic_constraints.max_acceleration_rate);
325  setIfNotInf(behavior_parameter.dynamic_constraints.max_deceleration, dynamic_constraints.max_deceleration);
326  setIfNotInf(behavior_parameter.dynamic_constraints.max_deceleration_rate, dynamic_constraints.max_deceleration_rate);
327  // clang-format on
328 
329  return behavior_parameter;
330  }());
331  }
332 
333  template <typename Controller>
335  const std::string & entity_ref, Controller && controller) -> void
336  {
337  auto & entity = core->getEntity(entity_ref);
338  entity.setVelocityLimit(controller.properties.template get<Double>(
339  "maxSpeed", std::numeric_limits<Double::value_type>::max()));
340 
341  entity.setBehaviorParameter([&]() {
342  auto message = entity.getBehaviorParameter();
343  message.see_around = not controller.properties.template get<Boolean>("isBlind");
345  message.dynamic_constraints.max_acceleration =
346  controller.properties.template get<Double>("maxAcceleration", 10.0);
347  message.dynamic_constraints.max_acceleration_rate =
348  controller.properties.template get<Double>("maxAccelerationRate", 3.0);
349  message.dynamic_constraints.max_deceleration =
350  controller.properties.template get<Double>("maxDeceleration", 10.0);
351  message.dynamic_constraints.max_deceleration_rate =
352  controller.properties.template get<Double>("maxDecelerationRate", 3.0);
353  message.dynamic_constraints.max_speed =
354  controller.properties.template get<Double>("maxSpeed", 50.0);
355  return message;
356  }());
357 
358  if (controller.isAutoware()) {
359  core->attachImuSensor(entity_ref, [&]() {
360  simulation_api_schema::ImuSensorConfiguration configuration;
361  configuration.set_entity(entity_ref);
362  configuration.set_frame_id("base_link");
363  configuration.set_add_gravity(true);
364  configuration.set_use_seed(true);
365  configuration.set_seed(0);
366  configuration.set_noise_standard_deviation_orientation(0.01);
367  configuration.set_noise_standard_deviation_twist(0.01);
368  configuration.set_noise_standard_deviation_acceleration(0.01);
369  return configuration;
370  }());
371 
372  core->attachLidarSensor([&]() {
373  simulation_api_schema::LidarConfiguration configuration;
374 
375  auto degree_to_radian = [](auto degree) {
376  return degree / 180.0 * boost::math::constants::pi<double>();
377  };
378 
379  // clang-format off
380  configuration.set_architecture_type(core->getROS2Parameter<std::string>("architecture_type", "awf/universe/20240605"));
381  configuration.set_entity(entity_ref);
382  configuration.set_horizontal_resolution(degree_to_radian(controller.properties.template get<Double>("pointcloudHorizontalResolution", 1.0)));
383  configuration.set_lidar_sensor_delay(controller.properties.template get<Double>("pointcloudPublishingDelay"));
384  configuration.set_scan_duration(0.1);
385  // clang-format on
386 
387  const auto vertical_field_of_view = degree_to_radian(
388  controller.properties.template get<Double>("pointcloudVerticalFieldOfView", 30.0));
389 
390  const auto channels =
391  controller.properties.template get<UnsignedInteger>("pointcloudChannels", 16);
392 
393  for (std::size_t i = 0; i < channels; ++i) {
394  configuration.add_vertical_angles(
395  vertical_field_of_view / 2 - vertical_field_of_view / channels * i);
396  }
397 
398  return configuration;
399  }());
400 
401  core->attachDetectionSensor([&]() {
402  simulation_api_schema::DetectionSensorConfiguration configuration;
403  // clang-format off
404  configuration.set_architecture_type(core->getROS2Parameter<std::string>("architecture_type", "awf/universe/20240605"));
405  configuration.set_entity(entity_ref);
406  configuration.set_detect_all_objects_in_range(controller.properties.template get<Boolean>("isClairvoyant"));
407  configuration.set_object_recognition_delay(controller.properties.template get<Double>("detectedObjectPublishingDelay"));
408  configuration.set_pos_noise_stddev(controller.properties.template get<Double>("detectedObjectPositionStandardDeviation"));
409  configuration.set_probability_of_lost(controller.properties.template get<Double>("detectedObjectMissingProbability"));
410  configuration.set_random_seed(controller.properties.template get<UnsignedInteger>("randomSeed"));
411  configuration.set_range(controller.properties.template get<Double>("detectionSensorRange",300.0));
412  configuration.set_object_recognition_ground_truth_delay(controller.properties.template get<Double>("detectedObjectGroundTruthPublishingDelay"));
413  configuration.set_update_duration(0.1);
414  // clang-format on
415  return configuration;
416  }());
417 
418  core->attachOccupancyGridSensor([&]() {
419  simulation_api_schema::OccupancyGridSensorConfiguration configuration;
420  // clang-format off
421  configuration.set_architecture_type(core->getROS2Parameter<std::string>("architecture_type", "awf/universe/20240605"));
422  configuration.set_entity(entity_ref);
423  configuration.set_filter_by_range(controller.properties.template get<Boolean>("isClairvoyant"));
424  configuration.set_height(200);
425  configuration.set_range(300);
426  configuration.set_resolution(0.5);
427  configuration.set_update_duration(0.1);
428  configuration.set_width(200);
429  // clang-format on
430  return configuration;
431  }());
432 
433  core->attachPseudoTrafficLightDetector([&]() {
434  simulation_api_schema::PseudoTrafficLightDetectorConfiguration configuration;
435  configuration.set_architecture_type(
436  core->getROS2Parameter<std::string>("architecture_type", "awf/universe/20240605"));
437  return configuration;
438  }());
439 
440  auto & ego_entity = core->getEgoEntity(entity_ref);
441 
442  ego_entity.setParameter<bool>(
443  "allow_goal_modification",
444  controller.properties.template get<Boolean>("allowGoalModification"));
445 
446  for (const auto & module :
447  [](std::string manual_modules_string) {
448  manual_modules_string.erase(
449  std::remove_if(
450  manual_modules_string.begin(), manual_modules_string.end(),
451  [](const auto & c) { return std::isspace(c); }),
452  manual_modules_string.end());
453 
454  std::vector<std::string> modules;
455  std::string buffer;
456  std::istringstream modules_stream(manual_modules_string);
457  while (std::getline(modules_stream, buffer, ',')) {
458  modules.push_back(buffer);
459  }
460  return modules;
461  }(controller.properties.template get<String>(
462  "featureIdentifiersRequiringExternalPermissionForAutonomousDecisions"))) {
463  try {
464  ego_entity.requestAutoModeForCooperation(module, false);
465  } catch (const Error & error) {
466  throw Error(
467  "featureIdentifiersRequiringExternalPermissionForAutonomousDecisions is not "
468  "supported in this environment: ",
469  error.what());
470  }
471  }
472  }
473  }
474 
475  template <typename... Ts>
476  static auto applyAssignRouteAction(const std::string & entity_ref, Ts &&... xs)
477  {
478  return core->getEntity(entity_ref).requestAssignRoute(std::forward<decltype(xs)>(xs)...);
479  }
480 
481  template <typename... Ts>
482  static auto applyDeleteEntityAction(Ts &&... xs)
483  {
484  return core->despawn(std::forward<decltype(xs)>(xs)...);
485  }
486 
487  template <typename... Ts>
488  static auto applyFollowTrajectoryAction(const std::string & entity_ref, Ts &&... xs)
489  {
490  return core->getEntity(entity_ref).requestFollowTrajectory(std::forward<decltype(xs)>(xs)...);
491  }
492 
493  template <typename... Ts>
494  static auto applyLaneChangeAction(const std::string & entity_ref, Ts &&... xs)
495  {
496  return core->getEntity(entity_ref).requestLaneChange(std::forward<decltype(xs)>(xs)...);
497  }
498 
499  template <typename... Ts>
500  static auto applySpeedAction(const std::string & entity_ref, Ts &&... xs)
501  {
502  return core->getEntity(entity_ref).requestSpeedChange(std::forward<decltype(xs)>(xs)...);
503  }
504 
505  template <
506  typename PoseType, typename... Ts,
507  typename = std::enable_if_t<
508  std::is_same_v<std::decay_t<PoseType>, geometry_msgs::msg::Pose> ||
509  std::is_same_v<std::decay_t<PoseType>, traffic_simulator::LaneletPose> ||
510  std::is_same_v<std::decay_t<PoseType>, traffic_simulator::CanonicalizedLaneletPose>>>
511  static auto applyTeleportAction(const std::string & name, const PoseType & pose, Ts &&... xs)
512  {
513  return core->getEntity(name).setStatus(pose, std::forward<decltype(xs)>(xs)...);
514  }
515 
516  template <typename... Ts>
517  static auto applyTeleportAction(
518  const std::string & name, const std::string & reference_entity_name, Ts &&... xs)
519  {
520  return core->getEntity(name).setStatus(
521  core->getEntity(reference_entity_name).getMapPose(), std::forward<decltype(xs)>(xs)...);
522  }
523 
524  template <typename... Ts>
525  static auto applyWalkStraightAction(const std::string & entity_ref, Ts &&... xs)
526  {
527  return core->getEntity(entity_ref).requestWalkStraight(std::forward<decltype(xs)>(xs)...);
528  }
529  };
530 
531  // OpenSCENARIO 1.1.1 Section 3.1.5
533  {
534  protected:
535  static auto evaluateAcceleration(const std::string & name)
536  {
537  return core->getEntity(name).getCurrentAccel().linear.x;
538  }
539 
540  template <typename... Ts>
541  static auto evaluateCollisionCondition(Ts &&... xs) -> bool
542  {
543  return core->checkCollision(std::forward<decltype(xs)>(xs)...);
544  }
545 
547  const std::string & from_entity_name,
548  const std::string & to_entity_name) // for RelativeDistanceCondition
549  {
550  if (const auto from_entity = core->getEntityPointer(from_entity_name)) {
551  if (const auto to_entity = core->getEntityPointer(to_entity_name)) {
552  if (
554  from_entity->getMapPose(), from_entity->getBoundingBox(), to_entity->getMapPose(),
555  to_entity->getBoundingBox())) {
556  return distance.value();
557  }
558  }
559  }
560  return std::numeric_limits<double>::quiet_NaN();
561  }
562 
563  static auto evaluateRelativeSpeed(const Entity & from, const Entity & to) -> Eigen::Vector3d
564  {
565  if (const auto observer = core->getEntityPointer(from.name())) {
566  if (const auto observed = core->getEntityPointer(to.name())) {
567  auto velocity = [](const auto & entity) -> Eigen::Vector3d {
568  auto direction = [](const auto & q) -> Eigen::Vector3d {
569  return Eigen::Quaternion(q.w, q.x, q.y, q.z) * Eigen::Vector3d::UnitX();
570  };
571  return direction(entity->getMapPose().orientation) * entity->getCurrentTwist().linear.x;
572  };
573 
574  const Eigen::Matrix3d rotation =
575  math::geometry::getRotationMatrix(observer->getMapPose().orientation);
576 
577  return rotation.transpose() * velocity(observed) -
578  rotation.transpose() * velocity(observer);
579  }
580  }
581 
582  return Eigen::Vector3d(Double::nan(), Double::nan(), Double::nan());
583  }
584 
585  template <typename... Ts>
586  static auto evaluateSimulationTime(Ts &&... xs) -> double
587  {
588  if (SimulatorCore::active()) {
589  return core->getCurrentTime(std::forward<decltype(xs)>(xs)...);
590  } else {
591  return std::numeric_limits<double>::quiet_NaN();
592  }
593  }
594 
595  static auto evaluateSpeed(const std::string & name)
596  {
597  /*
598  The function name "evaluateSpeed" stands for "evaluate SpeedCondition"
599  and is a part used to implement `SpeedCondition::evaluate`.
600  SpeedCondition can be evaluated in three directions: longitudinal,
601  lateral, and vertical, based on the attribute direction. Therefore,
602  please note that this function returns velocity, that is, a vector,
603  rather than speed, contrary to the name "evaluateSpeed".
604  */
605  const auto linear = core->getEntity(name).getCurrentTwist().linear;
606  return Eigen::Vector3d(linear.x, linear.y, linear.z);
607  }
608 
609  static auto evaluateStandStill(const std::string & name)
610  {
611  return core->getEntity(name).getStandStillDuration();
612  }
613 
614  static auto evaluateTimeHeadway(
615  const std::string & from_entity_name, const std::string & to_entity_name)
616  {
617  if (const auto from_entity = core->getEntityPointer(from_entity_name)) {
618  if (const auto to_entity = core->getEntityPointer(to_entity_name)) {
619  if (const auto relative_pose = traffic_simulator::pose::relativePose(
620  from_entity->getMapPose(), to_entity->getMapPose());
621  relative_pose && relative_pose->position.x <= 0) {
622  const double time_headway =
623  (relative_pose->position.x * -1) / to_entity->getCurrentTwist().linear.x;
624  return std::isnan(time_headway) ? std::numeric_limits<double>::infinity()
625  : time_headway;
626  }
627  }
628  }
629  return std::numeric_limits<double>::quiet_NaN();
630  }
631  };
632 
634  {
635  protected:
636  template <typename Performance, typename Properties>
638  const std::string & entity_ref, const Performance & performance,
639  const Properties & properties)
640  {
641  core->getEntity(entity_ref)
642  .activateOutOfRangeJob(
643  -performance.max_speed, +performance.max_speed, -performance.max_deceleration,
644  +performance.max_acceleration,
645  properties.template get<Double>("minJerk", Double::lowest()),
646  properties.template get<Double>("maxJerk", Double::max()));
647  }
648 
649  static auto activateNonUserDefinedControllers() -> decltype(auto)
650  {
651  return core->startNpcLogic();
652  }
653 
654  template <typename... Ts>
655  static auto evaluateCurrentState(const std::string & entity_ref, Ts &&... xs) -> decltype(auto)
656  {
657  return core->getEntity(entity_ref).getCurrentAction(std::forward<decltype(xs)>(xs)...);
658  }
659 
660  template <typename EntityRef, typename OSCLanePosition>
662  const EntityRef & entity_ref, const OSCLanePosition & osc_lane_position)
663  {
664  if (const auto entity = core->getEntityPointer(entity_ref)) {
665  const auto from_map_pose = entity->getMapPose();
666  const auto to_map_pose = static_cast<NativeWorldPosition>(osc_lane_position);
667  if (
668  const auto relative_pose =
669  traffic_simulator::pose::relativePose(from_map_pose, to_map_pose)) {
670  return static_cast<Double>(std::abs(
671  math::geometry::convertQuaternionToEulerAngle(relative_pose.value().orientation).z));
672  }
673  }
674  return Double::nan();
675  }
676 
677  template <typename EntityRef>
678  static auto evaluateRelativeHeading(const EntityRef & entity_ref)
679  {
680  if (const auto entity = core->getEntityPointer(entity_ref)) {
681  if (const auto canonicalized_lanelet_pose = entity->getCanonicalizedLaneletPose()) {
682  return static_cast<Double>(std::abs(
683  static_cast<traffic_simulator::LaneletPose>(canonicalized_lanelet_pose.value()).rpy.z));
684  }
685  }
686  return Double::nan();
687  }
688 
689  static auto engage(const std::string & ego_ref) -> decltype(auto)
690  {
691  return core->getEgoEntity(ego_ref).engage();
692  }
693 
694  static auto isEngageable(const std::string & ego_ref) -> decltype(auto)
695  {
696  return core->getEgoEntity(ego_ref).isEngageable();
697  }
698 
699  static auto isEngaged(const std::string & ego_ref) -> decltype(auto)
700  {
701  return core->getEgoEntity(ego_ref).isEngaged();
702  }
703 
704  template <typename... Ts>
705  static auto sendCooperateCommand(Ts &&... xs) -> decltype(auto)
706  {
708  if (const auto ego_name = core->getFirstEgoName()) {
709  return core->getEgoEntity(ego_name.value())
710  .sendCooperateCommand(std::forward<decltype(xs)>(xs)...);
711  } else {
712  throw common::Error("No ego entity exists.");
713  }
714  }
715 
716  static auto getMinimumRiskManeuverBehaviorName(const std::string & ego_ref) -> decltype(auto)
717  {
718  return core->getEgoEntity(ego_ref).getMinimumRiskManeuverBehaviorName();
719  }
720 
721  static auto getMinimumRiskManeuverStateName(const std::string & ego_ref) -> decltype(auto)
722  {
723  return core->getEgoEntity(ego_ref).getMinimumRiskManeuverStateName();
724  }
725 
726  static auto getEmergencyStateName(const std::string & ego_ref) -> decltype(auto)
727  {
728  return core->getEgoEntity(ego_ref).getEmergencyStateName();
729  }
730 
731  static auto getTurnIndicatorsCommandName(const std::string & ego_ref) -> decltype(auto)
732  {
733  return core->getEgoEntity(ego_ref).getTurnIndicatorsCommandName();
734  }
735 
736  // TrafficLights - Conventional and V2I
737  template <typename... Ts>
738  static auto setConventionalTrafficLightsState(Ts &&... xs) -> decltype(auto)
739  {
740  return core->getConventionalTrafficLights()->setTrafficLightsState(
741  std::forward<decltype(xs)>(xs)...);
742  }
743 
744  template <typename... Ts>
745  static auto setConventionalTrafficLightConfidence(Ts &&... xs) -> decltype(auto)
746  {
747  return core->getConventionalTrafficLights()->setTrafficLightsConfidence(
748  std::forward<decltype(xs)>(xs)...);
749  }
750 
751  template <typename... Ts>
752  static auto getConventionalTrafficLightsComposedState(Ts &&... xs) -> decltype(auto)
753  {
754  return core->getConventionalTrafficLights()->getTrafficLightsComposedState(
755  std::forward<decltype(xs)>(xs)...);
756  }
757 
758  template <typename... Ts>
759  static auto compareConventionalTrafficLightsState(Ts &&... xs) -> decltype(auto)
760  {
761  return core->getConventionalTrafficLights()->compareTrafficLightsState(
762  std::forward<decltype(xs)>(xs)...);
763  }
764 
765  template <typename... Ts>
766  static auto resetConventionalTrafficLightPublishRate(Ts &&... xs) -> decltype(auto)
767  {
768  return core->getConventionalTrafficLights()->resetUpdate(std::forward<decltype(xs)>(xs)...);
769  }
770 
771  template <typename... Ts>
772  static auto setV2ITrafficLightsState(Ts &&... xs) -> decltype(auto)
773  {
774  return core->getV2ITrafficLights()->setTrafficLightsState(std::forward<decltype(xs)>(xs)...);
775  }
776 
777  template <typename... Ts>
778  static auto resetV2ITrafficLightPublishRate(Ts &&... xs) -> decltype(auto)
779  {
780  return core->getV2ITrafficLights()->resetUpdate(std::forward<decltype(xs)>(xs)...);
781  }
782  };
783 };
784 } // namespace openscenario_interpreter
785 
786 #endif // OPENSCENARIO_INTERPRETER__SIMULATOR_CORE_HPP_
static auto applyWalkStraightAction(const std::string &entity_ref, Ts &&... xs)
Definition: simulator_core.hpp:525
static auto applyFollowTrajectoryAction(const std::string &entity_ref, Ts &&... xs)
Definition: simulator_core.hpp:488
static auto applyAcquirePositionAction(const std::string &entity_ref, Ts &&... xs)
Definition: simulator_core.hpp:297
static auto applyAssignRouteAction(const std::string &entity_ref, Ts &&... xs)
Definition: simulator_core.hpp:476
static auto applyAssignControllerAction(const std::string &entity_ref, Controller &&controller) -> void
Definition: simulator_core.hpp:334
static auto applyLaneChangeAction(const std::string &entity_ref, Ts &&... xs)
Definition: simulator_core.hpp:494
static auto applyProfileAction(const EntityRef &entity_ref, const DynamicConstraints &dynamic_constraints) -> void
Definition: simulator_core.hpp:309
static auto applyDeleteEntityAction(Ts &&... xs)
Definition: simulator_core.hpp:482
static auto applyTeleportAction(const std::string &name, const PoseType &pose, Ts &&... xs)
Definition: simulator_core.hpp:511
static auto applySpeedAction(const std::string &entity_ref, Ts &&... xs)
Definition: simulator_core.hpp:500
static auto applyAddEntityAction(Ts &&... xs)
Definition: simulator_core.hpp:303
static auto applyTeleportAction(const std::string &name, const std::string &reference_entity_name, Ts &&... xs)
Definition: simulator_core.hpp:517
static auto evaluateStandStill(const std::string &name)
Definition: simulator_core.hpp:609
static auto evaluateCollisionCondition(Ts &&... xs) -> bool
Definition: simulator_core.hpp:541
static auto evaluateAcceleration(const std::string &name)
Definition: simulator_core.hpp:535
static auto evaluateSimulationTime(Ts &&... xs) -> double
Definition: simulator_core.hpp:586
static auto evaluateBoundingBoxEuclideanDistance(const std::string &from_entity_name, const std::string &to_entity_name)
Definition: simulator_core.hpp:546
static auto evaluateRelativeSpeed(const Entity &from, const Entity &to) -> Eigen::Vector3d
Definition: simulator_core.hpp:563
static auto evaluateSpeed(const std::string &name)
Definition: simulator_core.hpp:595
static auto evaluateTimeHeadway(const std::string &from_entity_name, const std::string &to_entity_name)
Definition: simulator_core.hpp:614
static auto makeNativeRelativeWorldPosition(const std::string &from_entity_name, const std::string &to_entity_name)
Definition: simulator_core.hpp:108
static auto makeNativeBoundingBoxRelativeWorldPosition(const std::string &from_entity_name, const std::string &to_entity_name)
Definition: simulator_core.hpp:236
static auto makeNativeRelativeLanePosition(const std::string &from_entity_name, const NativeLanePosition &to_lanelet_pose, const RoutingAlgorithm::value_type routing_algorithm=RoutingAlgorithm::undefined) -> traffic_simulator::LaneletPose
Definition: simulator_core.hpp:162
static auto makeNativeBoundingBoxRelativeLanePosition(const std::string &from_entity_name, const std::string &to_entity_name, const RoutingAlgorithm::value_type routing_algorithm=RoutingAlgorithm::undefined)
Definition: simulator_core.hpp:188
static auto makeNativeBoundingBoxRelativeLanePosition(const NativeLanePosition &from_lanelet_pose, const traffic_simulator_msgs::msg::BoundingBox &from_bounding_box, const NativeLanePosition &to_lanelet_pose, const traffic_simulator_msgs::msg::BoundingBox &to_bounding_box, const RoutingAlgorithm::value_type routing_algorithm=RoutingAlgorithm::undefined) -> traffic_simulator::LaneletPose
Definition: simulator_core.hpp:220
static auto convert(const NativeLanePosition &native_lane_position) -> NativeWorldPosition
Definition: simulator_core.hpp:103
static auto convert(const NativeWorldPosition &pose) -> NativeLanePosition
Definition: simulator_core.hpp:81
static auto evaluateLateralRelativeLanes(const std::string &from_entity_name, const std::string &to_entity_name, const RoutingAlgorithm::value_type routing_algorithm=RoutingAlgorithm::undefined) -> int
Definition: simulator_core.hpp:266
static auto makeNativeRelativeLanePosition(const NativeLanePosition &from_lanelet_pose, const NativeLanePosition &to_lanelet_pose, const RoutingAlgorithm::value_type routing_algorithm=RoutingAlgorithm::undefined) -> traffic_simulator::LaneletPose
Definition: simulator_core.hpp:176
static auto makeNativeBoundingBoxRelativeWorldPosition(const std::string &from_entity_name, const NativeWorldPosition &to_map_pose)
Definition: simulator_core.hpp:252
static auto makeNativeRelativeLanePosition(const std::string &from_entity_name, const std::string &to_entity_name, const RoutingAlgorithm::value_type routing_algorithm=RoutingAlgorithm::undefined) -> traffic_simulator::LaneletPose
Definition: simulator_core.hpp:148
static auto canonicalize(const traffic_simulator::LaneletPose &non_canonicalized) -> NativeLanePosition
Definition: simulator_core.hpp:74
static auto makeNativeRelativeWorldPosition(const std::string &from_entity_name, const NativeWorldPosition &to_map_pose)
Definition: simulator_core.hpp:122
static auto makeNativeRelativeWorldPosition(const NativeWorldPosition &from_map_pose, const std::string &to_entity_name)
Definition: simulator_core.hpp:135
static auto makeNativeBoundingBoxRelativeLanePosition(const std::string &from_entity_name, const NativeLanePosition &to_lanelet_pose, const RoutingAlgorithm::value_type routing_algorithm=RoutingAlgorithm::undefined)
Definition: simulator_core.hpp:206
static auto isEngageable(const std::string &ego_ref) -> decltype(auto)
Definition: simulator_core.hpp:694
static auto evaluateRelativeHeading(const EntityRef &entity_ref)
Definition: simulator_core.hpp:678
static auto getMinimumRiskManeuverStateName(const std::string &ego_ref) -> decltype(auto)
Definition: simulator_core.hpp:721
static auto setV2ITrafficLightsState(Ts &&... xs) -> decltype(auto)
Definition: simulator_core.hpp:772
static auto resetV2ITrafficLightPublishRate(Ts &&... xs) -> decltype(auto)
Definition: simulator_core.hpp:778
static auto setConventionalTrafficLightsState(Ts &&... xs) -> decltype(auto)
Definition: simulator_core.hpp:738
static auto compareConventionalTrafficLightsState(Ts &&... xs) -> decltype(auto)
Definition: simulator_core.hpp:759
static auto resetConventionalTrafficLightPublishRate(Ts &&... xs) -> decltype(auto)
Definition: simulator_core.hpp:766
static auto getMinimumRiskManeuverBehaviorName(const std::string &ego_ref) -> decltype(auto)
Definition: simulator_core.hpp:716
static auto evaluateRelativeHeading(const EntityRef &entity_ref, const OSCLanePosition &osc_lane_position)
Definition: simulator_core.hpp:661
static auto getConventionalTrafficLightsComposedState(Ts &&... xs) -> decltype(auto)
Definition: simulator_core.hpp:752
static auto getTurnIndicatorsCommandName(const std::string &ego_ref) -> decltype(auto)
Definition: simulator_core.hpp:731
static auto sendCooperateCommand(Ts &&... xs) -> decltype(auto)
Definition: simulator_core.hpp:705
static auto activatePerformanceAssertion(const std::string &entity_ref, const Performance &performance, const Properties &properties)
Definition: simulator_core.hpp:637
static auto engage(const std::string &ego_ref) -> decltype(auto)
Definition: simulator_core.hpp:689
static auto evaluateCurrentState(const std::string &entity_ref, Ts &&... xs) -> decltype(auto)
Definition: simulator_core.hpp:655
static auto setConventionalTrafficLightConfidence(Ts &&... xs) -> decltype(auto)
Definition: simulator_core.hpp:745
static auto activateNonUserDefinedControllers() -> decltype(auto)
Definition: simulator_core.hpp:649
static auto isEngaged(const std::string &ego_ref) -> decltype(auto)
Definition: simulator_core.hpp:699
static auto getEmergencyStateName(const std::string &ego_ref) -> decltype(auto)
Definition: simulator_core.hpp:726
Definition: simulator_core.hpp:42
static auto deactivate() -> void
Definition: simulator_core.hpp:60
static auto active()
Definition: simulator_core.hpp:58
static auto activate(const Node &node, const traffic_simulator::Configuration &configuration, Ts &&... xs) -> void
Definition: simulator_core.hpp:47
static auto update() -> void
Definition: simulator_core.hpp:69
Definition: lanelet_pose.hpp:35
auto getRotationMatrix(T quat) -> Eigen::Matrix3d
Definition: get_rotation_matrix.hpp:33
auto convertQuaternionToEulerAngle(const T &q)
Definition: quaternion_to_euler.hpp:30
Definition: hypot.hpp:22
traffic_simulator::CanonicalizedLaneletPose NativeLanePosition
Definition: simulator_core.hpp:37
geometry_msgs::msg::Pose NativeWorldPosition
Definition: simulator_core.hpp:33
traffic_simulator::LaneletPose NativeRelativeLanePosition
Definition: simulator_core.hpp:39
NativeWorldPosition NativeRelativeWorldPosition
Definition: simulator_core.hpp:35
auto distance(const geometry_msgs::Pose &pose1, const geometry_msgs::Pose &pose2)
Definition: detection_sensor.cpp:36
auto countLaneChanges(const CanonicalizedLaneletPose &from, const CanonicalizedLaneletPose &to, const traffic_simulator::RoutingConfiguration &routing_configuration, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils_ptr) -> std::optional< std::pair< int, int >>
Definition: distance.cpp:49
auto boundingBoxDistance(const geometry_msgs::msg::Pose &from, const traffic_simulator_msgs::msg::BoundingBox &from_bounding_box, const geometry_msgs::msg::Pose &to, const traffic_simulator_msgs::msg::BoundingBox &to_bounding_box) -> std::optional< double >
Definition: distance.cpp:133
geometry_msgs::msg::Pose Pose
Definition: lanelet_wrapper.hpp:65
traffic_simulator_msgs::msg::BoundingBox BoundingBox
Definition: lanelet_wrapper.hpp:61
auto quietNaNPose() -> geometry_msgs::msg::Pose
Definition: pose.cpp:34
auto quietNaNLaneletPose() -> LaneletPose
Definition: pose.cpp:44
auto relativeLaneletPose(const CanonicalizedLaneletPose &from, const CanonicalizedLaneletPose &to, const RoutingConfiguration &routing_configuration, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils_ptr) -> LaneletPose
Definition: pose.cpp:285
auto boundingBoxRelativePose(const geometry_msgs::msg::Pose &from, const traffic_simulator_msgs::msg::BoundingBox &from_bounding_box, const geometry_msgs::msg::Pose &to, const traffic_simulator_msgs::msg::BoundingBox &to_bounding_box) -> std::optional< geometry_msgs::msg::Pose >
Definition: pose.cpp:263
auto relativePose(const geometry_msgs::msg::Pose &from, const geometry_msgs::msg::Pose &to) -> std::optional< geometry_msgs::msg::Pose >
Definition: pose.cpp:241
auto boundingBoxRelativeLaneletPose(const CanonicalizedLaneletPose &from, const traffic_simulator_msgs::msg::BoundingBox &from_bounding_box, const CanonicalizedLaneletPose &to, const traffic_simulator_msgs::msg::BoundingBox &to_bounding_box, const RoutingConfiguration &routing_configuration, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils_ptr) -> LaneletPose
Definition: pose.cpp:311
auto toCanonicalizedLaneletPose(const LaneletPose &lanelet_pose) -> std::optional< CanonicalizedLaneletPose >
Definition: pose.cpp:104
auto toMapPose(const CanonicalizedLaneletPose &lanelet_pose) -> geometry_msgs::msg::Pose
Definition: pose.cpp:87
traffic_simulator_msgs::msg::LaneletPose LaneletPose
Definition: lanelet_pose.hpp:30
Definition: junit5.hpp:25
std::string string
Definition: junit5.hpp:26
Definition: exception.hpp:27
Definition: controller.hpp:46
Definition: double.hpp:25
Definition: dynamic_constraints.hpp:44
Definition: entity_ref.hpp:35
Definition: entity.hpp:46
Definition: performance.hpp:39
const Double max_deceleration
Definition: performance.hpp:44
const Double max_acceleration
Definition: performance.hpp:40
const Double max_speed
Definition: performance.hpp:48
Definition: properties.hpp:42
Definition: configuration.hpp:31
Definition: routing_configuration.hpp:24
bool allow_lane_change
Definition: routing_configuration.hpp:25
traffic_simulator::lanelet_pose::CanonicalizedLaneletPose CanonicalizedLaneletPose
Definition: test_lanelet_pose.cpp:21