CARLA
Loading...
Searching...
No Matches
CarlaRadarPublisher.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 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
8
11
12#include <cstring>
13
14namespace carla {
15namespace ros2 {
16
17namespace {
18
19// Cartesian projection prefixed to each RadarDetection so subscribers see
20// (x, y, z, velocity, azimuth, altitude, depth) per point. Memory layout
21// must match kRadarFields: 3 leading floats + the raw RadarDetection
22// payload (velocity, azimuth, altitude, depth).
23struct RadarDetectionWithPosition {
24 float x;
25 float y;
26 float z;
27 sensor::data::RadarDetection detection;
28};
29
30static_assert(
31 sizeof(RadarDetectionWithPosition)
32 == 3u * sizeof(float) + sizeof(sensor::data::RadarDetection),
33 "RadarDetectionWithPosition must be tightly packed for the wire layout");
34
35} // namespace
36
38 return sizeof(RadarDetectionWithPosition);
39}
40
44
46 return kRadarFields.size();
47}
48
50 std::uint32_t height, std::uint32_t width, const std::uint8_t *data) const {
51 const std::size_t total_points =
52 static_cast<std::size_t>(height) * static_cast<std::size_t>(width);
53 const auto *detections =
54 reinterpret_cast<const sensor::data::RadarDetection *>(data);
55
56 std::vector<RadarDetectionWithPosition> radar_points(total_points);
57 for (std::size_t i = 0; i < total_points; ++i) {
58 const auto &detection = detections[i];
59 const auto cartesian = RadarPolarToCartesian(
60 detection.depth, detection.azimuth, detection.altitude);
61 radar_points[i].x = cartesian[0];
62 radar_points[i].y = cartesian[1];
63 radar_points[i].z = cartesian[2];
64 radar_points[i].detection = detection;
65 }
66
67 const auto *byte_ptr = reinterpret_cast<const std::uint8_t *>(radar_points.data());
68 return std::vector<std::uint8_t>(
69 byte_ptr, byte_ptr + radar_points.size() * sizeof(RadarDetectionWithPosition));
70}
71
72} // namespace ros2
73} // namespace carla
float y
float x
float z
sensor::data::RadarDetection detection
std::vector< std::uint8_t > ComputePointCloud(std::uint32_t height, std::uint32_t width, const std::uint8_t *data) const override
std::size_t GetFieldDescriptorCount() const override
std::size_t GetPointSize() const override
const PointFieldDescriptor * GetFieldDescriptors() const override
constexpr std::array< PointFieldDescriptor, 7 > kRadarFields
std::array< float, 3 > RadarPolarToCartesian(float depth, float azimuth, float altitude) noexcept
This file contains definitions of common data structures used in traffic manager.