CARLA
ROS2.h
Go to the documentation of this file.
1 // Copyright (c) 2023 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include "carla/Buffer.h"
10 #include "carla/BufferView.h"
11 #include "carla/geom/Transform.h"
14 
15 #include <unordered_set>
16 #include <unordered_map>
17 #include <memory>
18 #include <vector>
19 
20 // forward declarations
21 class AActor;
22 namespace carla {
23  namespace geom {
24  class GeoLocation;
25  class Vector3D;
26  }
27  namespace sensor {
28  namespace data {
29  struct DVSEvent;
30  class LidarData;
31  class SemanticLidarData;
32  class RadarData;
33  }
34  }
35 }
36 
37 namespace carla {
38 namespace ros2 {
39 
40  class CarlaPublisher;
41  class CarlaTransformPublisher;
42  class CarlaClockPublisher;
43  class CarlaEgoVehicleControlSubscriber;
44 
45 class ROS2
46 {
47  public:
48 
49  // deleting copy constructor for singleton
50  ROS2(const ROS2& obj) = delete;
51  static std::shared_ptr<ROS2> GetInstance() {
52  if (!_instance)
53  _instance = std::shared_ptr<ROS2>(new ROS2);
54  return _instance;
55  }
56 
57  // general
58  void Enable(bool enable);
59  void Shutdown();
60  bool IsEnabled() { return _enabled; }
61  void SetFrame(uint64_t frame);
62  void SetTimestamp(double timestamp);
63 
64  // ros_name managing
65  void AddActorRosName(void *actor, std::string ros_name);
66  void AddActorParentRosName(void *actor, void* parent);
67  void RemoveActorRosName(void *actor);
68  void UpdateActorRosName(void *actor, std::string ros_name);
69  std::string GetActorRosName(void *actor);
70  std::string GetActorParentRosName(void *actor);
71 
72  // callbacks
73  void AddActorCallback(void* actor, std::string ros_name, ActorCallback callback);
74  void RemoveActorCallback(void* actor);
75 
76  // enabling streams to publish
77  void EnableStream(carla::streaming::detail::stream_id_type id) { _publish_stream.insert(id); }
78  bool IsStreamEnabled(carla::streaming::detail::stream_id_type id) { return _publish_stream.count(id) > 0; }
79  void ResetStreams() { _publish_stream.clear(); }
80 
81  // receiving data to publish
82  void ProcessDataFromCamera(
83  uint64_t sensor_type,
85  const carla::geom::Transform sensor_transform,
86  int W, int H, float Fov,
87  const carla::SharedBufferView buffer,
88  void *actor = nullptr);
89  void ProcessDataFromGNSS(
90  uint64_t sensor_type,
92  const carla::geom::Transform sensor_transform,
93  const carla::geom::GeoLocation &data,
94  void *actor = nullptr);
95  void ProcessDataFromIMU(
96  uint64_t sensor_type,
98  const carla::geom::Transform sensor_transform,
99  carla::geom::Vector3D accelerometer,
100  carla::geom::Vector3D gyroscope,
101  float compass,
102  void *actor = nullptr);
103  void ProcessDataFromDVS(
104  uint64_t sensor_type,
106  const carla::geom::Transform sensor_transform,
107  const carla::SharedBufferView buffer,
108  int W, int H, float Fov,
109  void *actor = nullptr);
110  void ProcessDataFromLidar(
111  uint64_t sensor_type,
113  const carla::geom::Transform sensor_transform,
115  void *actor = nullptr);
116  void ProcessDataFromSemanticLidar(
117  uint64_t sensor_type,
119  const carla::geom::Transform sensor_transform,
121  void *actor = nullptr);
122  void ProcessDataFromRadar(
123  uint64_t sensor_type,
125  const carla::geom::Transform sensor_transform,
126  const carla::sensor::data::RadarData &data,
127  void *actor = nullptr);
128  void ProcessDataFromObstacleDetection(
129  uint64_t sensor_type,
131  const carla::geom::Transform sensor_transform,
132  AActor *first_actor,
133  AActor *second_actor,
134  float distance,
135  void *actor = nullptr);
136 void ProcessDataFromCollisionSensor(
137  uint64_t sensor_type,
139  const carla::geom::Transform sensor_transform,
140  uint32_t other_actor,
141  carla::geom::Vector3D impulse,
142  void* actor);
143 
144  private:
145  std::pair<std::shared_ptr<CarlaPublisher>, std::shared_ptr<CarlaTransformPublisher>> GetOrCreateSensor(int type, carla::streaming::detail::stream_id_type id, void* actor);
146 
147  // sigleton
148  ROS2() {};
149 
150  static std::shared_ptr<ROS2> _instance;
151 
152  bool _enabled { false };
153  uint64_t _frame { 0 };
154  int32_t _seconds { 0 };
155  uint32_t _nanoseconds { 0 };
156  std::unordered_map<void *, std::string> _actor_ros_name;
157  std::unordered_map<void *, std::vector<void*> > _actor_parent_ros_name;
158  std::shared_ptr<CarlaEgoVehicleControlSubscriber> _controller;
159  std::shared_ptr<CarlaClockPublisher> _clock_publisher;
160  std::unordered_map<void *, std::shared_ptr<CarlaPublisher>> _publishers;
161  std::unordered_map<void *, std::shared_ptr<CarlaTransformPublisher>> _transforms;
162  std::unordered_set<carla::streaming::detail::stream_id_type> _publish_stream;
163  std::unordered_map<void *, ActorCallback> _actor_callbacks;
164 };
165 
166 } // namespace ros2
167 } // namespace carla
std::unordered_map< void *, std::string > _actor_ros_name
Definition: ROS2.h:156
std::unordered_map< void *, std::shared_ptr< CarlaPublisher > > _publishers
Definition: ROS2.h:160
std::unordered_set< carla::streaming::detail::stream_id_type > _publish_stream
Definition: ROS2.h:162
void ResetStreams()
Definition: ROS2.h:79
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
uint32_t stream_id_type
Definition: Types.h:18
std::unordered_map< void *, std::vector< void * > > _actor_parent_ros_name
Definition: ROS2.h:157
geom::Vector3D Vector3D
Definition: rpc/Vector3D.h:14
std::shared_ptr< CarlaEgoVehicleControlSubscriber > _controller
Definition: ROS2.h:158
std::shared_ptr< CarlaClockPublisher > _clock_publisher
Definition: ROS2.h:159
carla::streaming::detail::stream_id_type stream_id
bool IsEnabled()
Definition: ROS2.h:60
void EnableStream(carla::streaming::detail::stream_id_type id)
Definition: ROS2.h:77
std::function< void(void *actor, ROS2CallbackData data)> ActorCallback
std::unordered_map< void *, std::shared_ptr< CarlaTransformPublisher > > _transforms
Definition: ROS2.h:161
std::shared_ptr< BufferView > SharedBufferView
Definition: BufferView.h:151
static std::shared_ptr< ROS2 > GetInstance()
Definition: ROS2.h:51
std::unordered_map< void *, ActorCallback > _actor_callbacks
Definition: ROS2.h:163
bool IsStreamEnabled(carla::streaming::detail::stream_id_type id)
Definition: ROS2.h:78