Repository navigation
Imu handling in component - #3517
Merged
Nir-Az merged 4 commits intoMay 17, 2026
Merged
Conversation
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).
Nir-Az
reviewed
May 17, 2026
| if (ACCEL == type) | ||
| { | ||
| _accel_data = imu_data; | ||
| _imu_history.clear(); |
Collaborator
There was a problem hiding this comment.
Why do we clear on each frame? so why do we need a deque?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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