CARLA
Loading...
Searching...
No Matches
CarlaTransformPublisher.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
9#include "carla/Logging.h"
14
15#include <cmath>
16
17namespace carla {
18namespace ros2 {
19
23
24namespace {
25
26constexpr float kEpsilon = 1e-4f;
27
28bool IsSameTransform(
30 float tx, float ty, float tz,
31 float pitch, float yaw, float roll) noexcept {
32 return std::abs(cached.tx - tx) < kEpsilon
33 && std::abs(cached.ty - ty) < kEpsilon
34 && std::abs(cached.tz - tz) < kEpsilon
35 && std::abs(cached.pitch - pitch) < kEpsilon
36 && std::abs(cached.yaw - yaw) < kEpsilon
37 && std::abs(cached.roll - roll) < kEpsilon;
38}
39
40} // namespace
41
43 : BasePublisher("rt/tf"),
44 _impl(std::make_shared<PublisherImpl<CarlaTransformMsgTraits>>()) {
45 if (!_impl->Init(GetBaseTopicName())) {
46 log_error("CarlaTransformPublisher: failed to initialise writer for rt/tf");
47 }
48}
49
51
53 return _impl->Publish();
54}
55
57 std::int32_t seconds,
58 std::uint32_t nanoseconds,
59 const std::string &parent_frame_id,
60 const std::string &child_frame_id,
61 float tx, float ty, float tz,
62 float pitch_deg, float yaw_deg, float roll_deg) {
63 std::array<float, 3> translation;
64 std::array<float, 4> rotation;
65
66 auto it = _last_transforms.find(child_frame_id);
67 if (it != _last_transforms.end()
68 && IsSameTransform(it->second, tx, ty, tz, pitch_deg, yaw_deg, roll_deg)) {
69 translation = it->second.translation;
70 rotation = it->second.rotation;
71 } else {
72 // Quaternion conversion lives in TransformQuaternion.h (header-only
73 // seam, pinned by test_transform_quaternion.cpp).
74 const auto computed =
75 TransformFromCarlaRotation(tx, ty, tz, pitch_deg, yaw_deg, roll_deg);
76 translation = computed.translation;
77 rotation = computed.rotation;
78 _last_transforms.insert_or_assign(
79 child_frame_id,
81 tx, ty, tz, pitch_deg, yaw_deg, roll_deg, translation, rotation});
82 }
83
85 ts.header.stamp.sec = seconds;
86 ts.header.stamp.nanosec = nanoseconds;
87 ts.header.frame_id = parent_frame_id;
88 ts.child_frame_id = child_frame_id;
89
90 ts.transform.translation.x = translation[0];
91 ts.transform.translation.y = translation[1];
92 ts.transform.translation.z = translation[2];
93 ts.transform.rotation.w = rotation[0];
94 ts.transform.rotation.x = rotation[1];
95 ts.transform.rotation.y = rotation[2];
96 ts.transform.rotation.z = rotation[3];
97
98 _impl->GetMessage()->transforms = {ts};
99 return true;
100}
101
102} // namespace ros2
103} // namespace carla
const std::string & GetBaseTopicName() const noexcept
std::unordered_map< std::string, CachedTransform > _last_transforms
std::shared_ptr< PublisherImpl< CarlaTransformMsgTraits > > _impl
bool Write(std::int32_t seconds, std::uint32_t nanoseconds, const std::string &parent_frame_id, const std::string &child_frame_id, float tx, float ty, float tz, float pitch_deg, float yaw_deg, float roll_deg)
TransformQuaternion TransformFromCarlaRotation(float tx, float ty, float tz, float pitch_deg, float yaw_deg, float roll_deg) noexcept
This file contains definitions of common data structures used in traffic manager.
static void log_error(Args &&... args)
Definition Logging.h:110