scenario_simulator_v2 C++ API
simple_sensor_simulator.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 SIMPLE_SENSOR_SIMULATOR__SIMPLE_SENSOR_SIMULATOR_HPP_
16 #define SIMPLE_SENSOR_SIMULATOR__SIMPLE_SENSOR_SIMULATOR_HPP_
17 
18 #include <tf2/LinearMath/Quaternion.h>
19 #include <tf2_ros/transform_broadcaster.h>
20 
21 #include <geographic_msgs/msg/geo_point.hpp>
22 #include <geometry_msgs/msg/pose_stamped.hpp>
23 #include <geometry_msgs/msg/transform_stamped.hpp>
24 #include <map>
25 #include <memory>
26 #include <rclcpp/rclcpp.hpp>
32 #include <string>
33 #include <thread>
34 #include <vector>
35 #include <visualization_msgs/msg/marker_array.hpp>
36 
37 #if __cplusplus
38 extern "C" {
39 #endif
40 
41 // The below macros are taken from https://gcc.gnu.org/wiki/Visibility and from
42 // demos/composition/include/composition/visibility_control.h at https://github.com/ros2/demos
43 #if defined _WIN32 || defined __CYGWIN__
44 #ifdef __GNUC__
45 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_EXPORT __attribute__((dllexport))
46 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_IMPORT __attribute__((dllimport))
47 #else
48 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_EXPORT __declspec(dllexport)
49 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_IMPORT __declspec(dllimport)
50 #endif
51 #ifdef SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_BUILDING_DLL
52 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_PUBLIC \
53  SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_EXPORT
54 #else
55 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_PUBLIC \
56  SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_IMPORT
57 #endif
58 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_PUBLIC_TYPE \
59  SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_PUBLIC
60 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_LOCAL
61 #else
62 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_EXPORT \
63  __attribute__((visibility("default")))
64 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_IMPORT
65 #if __GNUC__ >= 4
66 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_PUBLIC \
67  __attribute__((visibility("default")))
68 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_LOCAL \
69  __attribute__((visibility("hidden")))
70 #else
71 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_PUBLIC
72 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_LOCAL
73 #endif
74 #define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_PUBLIC_TYPE
75 #endif
76 #if __cplusplus
77 } // extern "C"
78 #endif
79 
81 {
82 class ScenarioSimulator : public rclcpp::Node
83 {
84 public:
86  explicit ScenarioSimulator(const rclcpp::NodeOptions & options);
88 
89 private:
90  SensorSimulation sensor_sim_;
91 
92  auto initialize(const simulation_api_schema::InitializeRequest &)
93  -> simulation_api_schema::InitializeResponse;
94 
95  auto updateFrame(const simulation_api_schema::UpdateFrameRequest &)
96  -> simulation_api_schema::UpdateFrameResponse;
97 
98  auto updateStepTime(const simulation_api_schema::UpdateStepTimeRequest &)
99  -> simulation_api_schema::UpdateStepTimeResponse;
100 
101  auto updateEntityStatus(const simulation_api_schema::UpdateEntityStatusRequest &)
102  -> simulation_api_schema::UpdateEntityStatusResponse;
103 
104  auto spawnVehicleEntity(const simulation_api_schema::SpawnVehicleEntityRequest &)
105  -> simulation_api_schema::SpawnVehicleEntityResponse;
106 
107  template <typename SpawnRequestType>
108  auto insertEntitySpawnedStatus(
109  const SpawnRequestType & spawn_request, const traffic_simulator_msgs::EntityType::Enum & type,
110  const traffic_simulator_msgs::EntitySubtype::Enum & subtype) -> void;
111 
112  auto spawnPedestrianEntity(const simulation_api_schema::SpawnPedestrianEntityRequest &)
113  -> simulation_api_schema::SpawnPedestrianEntityResponse;
114 
115  auto spawnMiscObjectEntity(const simulation_api_schema::SpawnMiscObjectEntityRequest &)
116  -> simulation_api_schema::SpawnMiscObjectEntityResponse;
117 
118  auto despawnEntity(const simulation_api_schema::DespawnEntityRequest &)
119  -> simulation_api_schema::DespawnEntityResponse;
120 
121  auto attachImuSensor(const simulation_api_schema::AttachImuSensorRequest &)
122  -> simulation_api_schema::AttachImuSensorResponse;
123 
124  auto attachDetectionSensor(const simulation_api_schema::AttachDetectionSensorRequest &)
125  -> simulation_api_schema::AttachDetectionSensorResponse;
126 
127  auto attachLidarSensor(const simulation_api_schema::AttachLidarSensorRequest &)
128  -> simulation_api_schema::AttachLidarSensorResponse;
129 
130  auto attachOccupancyGridSensor(const simulation_api_schema::AttachOccupancyGridSensorRequest &)
131  -> simulation_api_schema::AttachOccupancyGridSensorResponse;
132 
133  auto updateTrafficLights(const simulation_api_schema::UpdateTrafficLightsRequest &)
134  -> simulation_api_schema::UpdateTrafficLightsResponse;
135 
136  auto attachPseudoTrafficLightDetector(
137  const simulation_api_schema::AttachPseudoTrafficLightDetectorRequest &)
138  -> simulation_api_schema::AttachPseudoTrafficLightDetectorResponse;
139 
140  int getSocketPort();
141 
142  std::vector<traffic_simulator_msgs::VehicleParameters> ego_vehicles_;
143  std::vector<traffic_simulator_msgs::VehicleParameters> vehicles_;
144  std::vector<traffic_simulator_msgs::PedestrianParameters> pedestrians_;
145  std::vector<traffic_simulator_msgs::MiscObjectParameters> misc_objects_;
146  double realtime_factor_;
147  double step_time_;
148  double current_simulation_time_;
149  double current_scenario_time_;
150  rclcpp::Time current_ros_time_;
151  bool initialized_;
152  std::map<std::string, simulation_api_schema::EntityStatus> entity_status_;
153  simulation_api_schema::UpdateTrafficLightsRequest traffic_signals_states_;
154  traffic_simulator_msgs::BoundingBox getBoundingBox(const std::string & name);
155  zeromq::MultiServer server_;
156  std::shared_ptr<vehicle_simulation::EgoEntitySimulation> ego_entity_simulation_;
157 
158  bool isEgo(const std::string & name);
159  bool isEntityExists(const std::string & name);
160 };
161 } // namespace simple_sensor_simulator
162 
163 #endif // SIMPLE_SENSOR_SIMULATOR__SIMPLE_SENSOR_SIMULATOR_HPP_
Definition: simple_sensor_simulator.hpp:83
~ScenarioSimulator()
Definition: simple_sensor_simulator.cpp:54
SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_PUBLIC ScenarioSimulator(const rclcpp::NodeOptions &options)
Definition: simple_sensor_simulator.cpp:31
Definition: sensor_simulation.hpp:45
Definition: zmq_multi_server.hpp:32
Definition: constants.hpp:19
traffic_simulator_msgs::msg::BoundingBox BoundingBox
Definition: lanelet_wrapper.hpp:65
std::string string
Definition: junit5.hpp:26
#define SIMPLE_SENSOR_SIMULATOR_SIMPLE_SENSOR_SIMULATOR_COMPONENT_PUBLIC
Definition: simple_sensor_simulator.hpp:71