Skip to content

Imu handling in component - #3517

Merged
Nir-Az merged 4 commits into
realsenseai:ros2-developmentfrom
Gilaadb:imu_handling_in_component
May 17, 2026
Merged

Nir-Az merged 4 commits into
realsenseai:ros2-developmentfrom
Gilaadb:imu_handling_in_component

Conversation

@Gilaadb

@Gilaadb Gilaadb commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

When multiple camera nodes are loaded into the same composable node container they share a process, which exposes several bugs caused by static local variables behaving as process-wide singletons instead of per-instance state.

Commit 1: IMU message pollution
FillImuData_LinearInterpolation and FillImuData_Copy each used a static local variable (_imu_history, _accel_data) to carry state between frames. These were shared across all instances, causing IMU data from one camera to corrupt the output of another.
Replaced both with a single deque member variable.

Commit 2: Shared IMU callback mutex
imu_callback_sync used a static std::mutex to serialize access to IMU state. In a composable container all instances shared the same mutex, meaning cameras serialized against each other rather than independently. Promoted to a member mutex and switched to std::lock_guard for RAII safety.

Commit 3: Race on time base initialization
The hardware-clock time base (_ros_time_base, _camera_time_base, _previous_frame_time) was initialized via atomic_bool::compare_exchange_strong from three different callbacks (frame_callback, imu_callback, imu_callback_sync). This left the non-atomic members unprotected against concurrent writes. Initialization is now consolidated into frameSystemTimeSec under a dedicated _time_base_mutex, and _is_initialized_time_base is a plain bool (protected by the same mutex).

Commit 4: _imu_history corruption on unite_imu_method change
_imu_history is shared between FillImuData_Copy and FillImuData_LinearInterpolation. When unite_imu_method is changed at runtime, stale entries from the previous mode remain in the deque. In the worst case a GYRO sample is used as accel data in COPY mode. The parameter callback now clears _imu_history under _imu_callback_mutex on every mode change.

Testing
Validated with two RealSense D436 cameras in the same composable node container with unite_imu_method set to COPY

Gilaadb added 4 commits May 14, 2026 14:52
When using components, different realsense cameras are part of the same
process. When using a static variable like was done in both unite
methods, these variables are shared between the different camera
components and all of them write to the same structure. It caused the
published IMU messages to have the wrong data because it came from
different camera sources.

Moved the buffer to an instance member so each component has its own.
When using components, different realsense cameras are part of the same
process. The mutex guarding imu_callback_sync was a function-local
static, so it was shared between all camera components and serialized
unrelated callbacks across cameras.

Moved the mutex to an instance member so each component has its own,
and switched to std::lock_guard which is cleaner and better if an
exception is ever thrown while the lock is held.
When using components, different sensor threads (video, IMU) can call
frameSystemTimeSec concurrently on the same instance. The time base
fields (_ros_time_base, _camera_time_base, _previous_frame_time) had
no protection, so concurrent init or hardware clock reset could produce
torn reads and inconsistent timestamp pairs.

Folded the time base init and reset logic into frameSystemTimeSec
behind a per-instance mutex, making it the single place that reads and
writes the time base fields. Removed setBaseTime and the CAS-based
init blocks in the callbacks, which had a publication window where one
thread could read uninitialized values after another won the CAS but
before it had finished writing. Converted _is_initialized_time_base
from std::atomic_bool to bool since all accesses are now under the
mutex and the atomic is no longer needed.
When switching between COPY and LINEAR_INTERPOLATION at runtime via the
'unite_imu_method' parameter, _imu_history may contain stale entries from
the previous mode. Clear it under _imu_callback_mutex to prevent corrupt
IMU messages (e.g. a GYRO sample being used as accel data in COPY mode).
if (ACCEL == type)
{
_accel_data = imu_data;
_imu_history.clear();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we clear on each frame? so why do we need a deque?

@Nir-Az Nir-Az left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great enhancement!

@Nir-Az
Nir-Az merged commit 8134115 into realsenseai:ros2-development May 17, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants