CARLA
Loading...
Searching...
No Matches
CarlaIMUPublisher.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"
13
14namespace carla {
15namespace ros2 {
16
19};
20
22 std::string base_topic_name, std::string frame_id)
23 : BasePublisher(std::move(base_topic_name), std::move(frame_id)),
24 _impl(std::make_shared<PublisherImpl<CarlaImuMsgTraits>>()) {
25 if (!_impl->Init(GetBaseTopicName())) {
26 log_error("CarlaIMUPublisher: failed to initialise writer for", GetBaseTopicName());
27 }
28}
29
31
33 return _impl->Publish();
34}
35
37 std::int32_t seconds,
38 std::uint32_t nanoseconds,
39 float accel_x, float accel_y, float accel_z,
40 float gyro_x, float gyro_y, float gyro_z,
41 float compass) {
42 auto *message = _impl->GetMessage();
43 message->header.stamp.sec = seconds;
44 message->header.stamp.nanosec = nanoseconds;
45 message->header.frame_id = GetFrameId();
46
47 // Input samples arrive in CARLA's left-handed sensor frame; convert to the
48 // REP-103 right-handed frame before publishing (see ImuMath.h for the
49 // pseudovector rationale on the gyro axes).
50 const auto accel = AccelToRos(accel_x, accel_y, accel_z);
51 message->linear_acceleration.x = accel[0];
52 message->linear_acceleration.y = accel[1];
53 message->linear_acceleration.z = accel[2];
54
55 const auto gyro = GyroToRos(gyro_x, gyro_y, gyro_z);
56 message->angular_velocity.x = gyro[0];
57 message->angular_velocity.y = gyro[1];
58 message->angular_velocity.z = gyro[2];
59
60 // Yaw-only quaternion from compass heading; math lives in ImuMath.h.
61 const auto q = OrientationFromCompass(compass);
62 message->orientation.w = q[0];
63 message->orientation.x = q[1];
64 message->orientation.y = q[2];
65 message->orientation.z = q[3];
66
67 return true;
68}
69
70} // namespace ros2
71} // namespace carla
const std::string & GetBaseTopicName() const noexcept
const std::string & GetFrameId() const noexcept
std::shared_ptr< PublisherImpl< CarlaImuMsgTraits > > _impl
bool Write(std::int32_t seconds, std::uint32_t nanoseconds, float accel_x, float accel_y, float accel_z, float gyro_x, float gyro_y, float gyro_z, float compass)
CarlaIMUPublisher(std::string base_topic_name, std::string frame_id)
std::array< float, 3 > AccelToRos(float ax, float ay, float az)
Definition ImuMath.h:34
std::array< float, 3 > GyroToRos(float gx, float gy, float gz)
Definition ImuMath.h:51
std::array< float, 4 > OrientationFromCompass(float compass)
Definition ImuMath.h:23
This file contains definitions of common data structures used in traffic manager.
static void log_error(Args &&... args)
Definition Logging.h:110