Monado OpenXR Runtime
Loading...
Searching...
No Matches
sensor_fusion.hpp
Go to the documentation of this file.
1// Copyright 2026, Beyley Cardellio
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Constellation tracker sensor fusion.
6 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
7 * @ingroup tracking
8 */
9
10#pragma once
11
12#include "xrt/xrt_config_build.h"
13
14#include "os/os_threading.h"
15
17
19
22
23#include <array>
24#include <atomic>
25#include <ceres/context.h>
26#include <cstddef>
27#include <cstring>
28#include <memory>
29#include <optional>
30#include <string>
31#include <type_traits>
32#include <vector>
33
34
35namespace xrt::tracking::constellation {
36
37// Declared here rather than included, since the tracker's internal header is the one that includes this file.
38struct CameraSample;
39struct ConstellationTracker;
40
41}; // namespace xrt::tracking::constellation
42
43namespace xrt::tracking::constellation::optimizer::sensor_fusion {
44
45using namespace xrt::auxiliary;
46using namespace xrt::tracking;
47using namespace xrt::tracking::constellation::optimizer;
48
49//! How many keyframes to keep in our sliding window.
50static constexpr uint32_t kSlidingWindowSize = 3;
51
52//! The number of IMU samples to keep in our ring buffer
53static constexpr uint32_t kImuBufferSize = 5000;
54
55/*!
56 * The most IMU samples we ever expect to find between two keyframes. At a 1KHz IMU and a keyframe every few tens of
57 * milliseconds the real count is well under a hundred, so this is generous headroom rather than a tight bound. Sizing
58 * the staging buffer off this instead of `kImuBufferSize` keeps it small enough to sit on the stack.
59 */
60static constexpr uint32_t kMaxImuSamplesPerKeyframe = 512;
61
62/*!
63 * How many newer keyframes have to pile up before a keyframe gets sealed and marginalized.
64 */
65static constexpr uint32_t kKeyframeSealLagKeyframes = 2;
66
68 "the oldest keyframe in the window has to be sealed by the time it is evicted, so that marginalization "
69 "never has to fold an open keyframe into the prior");
70
71
72/*!
73 * How many camera samples may be queued for the fusion thread before new ones start being dropped.
74 *
75 * The fusion thread drains the whole queue every pass, so if this grows past even a handful, the thread has stalled a
76 * lot and we should just warn and start dropping them to not use an ever-increasing amount of RAM.
77 */
78static constexpr uint32_t kMaxQueuedCameraSamples = 32;
79
81{
82 //! The camera that this observation is tied to
83 uint32_t camera_idx;
84
85 //! The camera pose at the timestamp
87
88 //! The seed cam->device transform
90
91 //! 2D observations
92 std::array<xrt_vec2, XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE> points2d;
93 //! 3D model correspondences
94 std::array<xrt_vec3, XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE> points3d;
95
96 uint32_t num_points;
97};
98
100{
101 //! The mosaic this keyframe corresponds to
102 uint32_t mosaic_idx;
103 //! The timestamp of this observation
105
106 /*!
107 * Set once this keyframe can no longer accept observations due to marginalization or age.
108 */
109 bool sealed;
110
111 std::array<DeviceObservation, XRT_TRACKING_MAX_FRAME_SOURCES> observations;
112 uint32_t num_observations;
113};
114
115/*!
116 * One IMU sample as it sits in @ref Device::imu_samples.
117 *
118 * The fusion thread reads these while the IMU thread is writing them, so the words are loaded and stored one at a time
119 * through relaxed atomics. This can cause torn reads on the reader end, but it will not be UB.
120 *
121 * Relaxed is the right ordering for the words themselves. All the ordering that matters lives on
122 * @ref Device::imu_write_count, whose release/acquire pair is what publishes a finished slot, plus a fence pair around
123 * the words that lets a reader detect a slot being overwritten underneath it.
124 */
126{
127 static_assert(std::is_trivially_copyable_v<xrt_imu_sample>);
128 static_assert(sizeof(xrt_imu_sample) % sizeof(uint64_t) == 0,
129 "the sample has to decompose into whole words to be stored and loaded atomically");
130 static_assert(std::atomic<uint64_t>::is_always_lock_free,
131 "a locking atomic here would put a mutex back on the IMU push path");
132
133 static constexpr size_t kNumWords = sizeof(xrt_imu_sample) / sizeof(uint64_t);
134
135 //! The word `xrt_imu_sample::timestamp_ns` lives in, which is the only one the collector's bisection needs.
136 static constexpr size_t kTimestampWord = offsetof(xrt_imu_sample, timestamp_ns) / sizeof(uint64_t);
137
138 std::array<std::atomic<uint64_t>, kNumWords> words;
139
140 void
141 store(const xrt_imu_sample &sample) noexcept
142 {
143 std::array<uint64_t, kNumWords> raw;
144 std::memcpy(raw.data(), &sample, sizeof(sample));
145
146 for (size_t i = 0; i < kNumWords; i++) {
147 this->words[i].store(raw[i], std::memory_order_relaxed);
148 }
149 }
150
152 load() const noexcept
153 {
154 std::array<uint64_t, kNumWords> raw;
155 for (size_t i = 0; i < kNumWords; i++) {
156 raw[i] = this->words[i].load(std::memory_order_relaxed);
157 }
158
159 xrt_imu_sample sample;
160 std::memcpy(&sample, raw.data(), sizeof(sample));
161 return sample;
162 }
163
164 //! Just the timestamp, so the bisection does not pull in six words it is only going to throw away.
166 loadTimestamp() const noexcept
167 {
168 const uint64_t raw = this->words[kTimestampWord].load(std::memory_order_relaxed);
169
170 timepoint_ns timestamp;
171 std::memcpy(&timestamp, &raw, sizeof(timestamp));
172 return timestamp;
173 }
174};
175
177{
178 //! Needs to be recomputed
179 Dirty,
180 //! Does not refer to any real pre-integration. This term contributes nothing and can be skipped.
181 Empty,
182 //! This preintegration is complete and will never change.
184};
185
186/*!
187 * Stores state for a single preintegration residual for a device.
188 */
190{
191 PreintegratedImuSamples imu_preintegration;
192
194
195 bool
196 dirty() const
197 {
198 return this->state == DevicePreintegrationState::Dirty;
199 }
200
202 Identity(DevicePreintegrationState state)
203 {
204 return {
205 .imu_preintegration = PreintegratedImuSamples::Identity(),
206 .state = state,
207 };
208 }
209};
210
211/*!
212 * The IMU intrinsics the rig calibration happened to solve for, as the fusion takes them in as a seed.
213 *
214 * A copy of @ref rig_calibration::ImuCalibration rather than that type itself.
215 */
217{
218 //! Accelerometer bias, in meters per second squared.
220 //! Gyroscope bias, in radians per second.
222 //! Per-axis accelerometer scale, unitless.
224
225 //! The LED model -> IMU rotation.
227};
228
230{
231 //! The time of the solve
233 //! The position component of the world -> LED model pose at the solve
235 //! The orientation component of the world -> LED model pose at the solve
237 //! The world velocity at the solve
239 //! The imu calibration at the solve
241 /*!
242 * The pose covariance at the solve, in the solver's tangent space (world position, LED model local rotation,
243 * world velocity).
244 *
245 * @see PoseCovarianceIndex
246 */
247 PoseCovarianceMatrix covariance = PoseCovarianceMatrix::Zero();
248};
249
250struct Device
251{
252public: // Fields
253 const t_constellation_device_id_t id;
254
255 const t_constellation_tracker_led_model *const led_model;
256
257 //! The noise model and bias behaviour of the device's IMU, as the driver gave it.
259
260 //! The latest fused solve.
261 std::optional<DeviceSolveState> latest_solve;
262 //! The solved state for the latest IMU dead reckoned position.
263 std::optional<DeviceSolveState> latest_imu_reckon;
264
265 //! The relation history of the device, used as a fast way to retrieve the latest dead-reckoned poses.
267 /*!
268 * The relation history containing only the exact camera keyframes, to allow access to exact fused keyframes.
269 *
270 * Since relation histories are not allowed to be pushed to out of order, if we want to allow access to exact
271 * camera keyframe poses, so that a late camera can get the full solved pose from other cameras, this is the way
272 * to go about that.
273 */
275
276 //! Pre-calibrated accelerometer bias, in meters per second squared.
278
279 /*!
280 * The preintegrated IMU samples between keyframes, as a ring running parallel to @ref
281 * SensorFusion::keyframe_storage: slot `i` holds the interval that *ends* at the keyframe in slot `i`.
282 *
283 * Sharing the keyframe ring's indexing is what makes a window shift free for these too. The slot belonging
284 * to the oldest keyframe in the window has no predecessor to integrate from and is simply never read. Index
285 * this through @ref SensorFusion::preintegrationLocked rather than directly.
286 */
287 std::array<DevicePreintegration, kSlidingWindowSize> preintegrations;
288
289 /*!
290 * The total number of IMU samples ever pushed for this device. Absolute sample `n` lives in ring slot
291 * `n % kImuBufferSize`, so this is both the write pointer and the count of samples that have ever been
292 * written. Incrementing forever means there's no wraparound behaviour to worry about when the reader gets
293 * lapped by the writer.
294 *
295 * Only the IMU push thread writes this, and it publishes it with a release store once the slot it names has
296 * been filled. Readers must load it with acquire before touching `imu_samples`, and re-load it after an acquire
297 * fence to confirm the writer did not overwrite what they just read; the writer issues the matching release
298 * fence before it starts overwriting a slot.
299 */
300 std::atomic<uint64_t> imu_write_count;
301
302 std::atomic<timepoint_ns> latest_imu_ts_ns;
303
304 //! The buffered IMU samples, indexed by absolute sample index modulo `kImuBufferSize`.
305 std::array<RingImuSample, kImuBufferSize> imu_samples;
306
307public: // Methods
308 Device(t_constellation_device_id_t id,
309 const t_constellation_tracker_led_model *led_model,
311 ~Device();
312
313 Device(const Device &) = delete;
314 Device &
315 operator=(const Device &) = delete;
316
318 getLatestImuCalibration() const;
319
320 /*!
321 * Resets the device's state to a fresh state.
322 *
323 * Can be used if all prior converged data can no longer
324 * be considered trusted (eg. large jump in clock sync).
325 */
326 void
327 resetLocked();
328};
329
331{
332public: // Fields
333 size_t mosaic_idx;
334 size_t camera_idx;
335
336 //! Intrinsics and distortion, which is what turns a LED in the device's frame into a pixel.
338};
339
340/*
341 * Solve snapshot
342 *
343 * A description of one solve that stands still while the next pass moves the window on underneath it. The fusion
344 * thread owns the window outright.
345 */
346
347//! A device's solved state at one keyframe.
349{
350 //! The positional part of the solved world -> LED model pose.
352 //! The orientational part of the solved world -> LED model pose.
354 //! Solved world-frame velocity, in meters per second.
356
357 //! Whether or not the IMU sample is valid.
359 //! Device-frame raw IMU sample that ended the preintegration.
361 //! Device-frame calibrated IMU sample that ended the preintegration.
363 //! World-frame IMU sample that ended the preintegration.
365
366 //! Solved accelerometer bias, in meters per second squared.
368 //! Solved gyroscope bias, in radians per second.
370 //! Solved per-axis accelerometer scale, unitless.
372
373 /*!
374 * The covariance matrix of this solve's keyframe parameters of the pose
375 *
376 * @see PoseCovarianceIndex
377 */
378 PoseCovarianceMatrix covariance{PoseCovarianceMatrix::Zero()};
379};
380
381/*!
382 * One camera's view of one device at one keyframe.
383 *
384 * Carries the camera intrinsics alongside the correspondences so that whatever consumes this can reproject the solved
385 * pose without having to reach back into @ref SensorFusion::cameras for them.
386 */
388{
389 uint32_t mosaic_idx;
390 uint32_t camera_idx;
391
392 xrt_pose Tcv_world_cam;
393 xrt_pose Tcv_cam_device_seed;
394
395 camera_model params;
396
397 uint32_t num_points;
398 std::array<xrt_vec2, XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE> points2d;
399 std::array<xrt_vec3, XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE> points3d;
400};
401
403{
404 timepoint_ns timestamp_ns;
405 uint32_t mosaic_idx;
406 bool sealed;
407
408 uint32_t num_observations;
409 std::array<SnapshotObservation, XRT_TRACKING_MAX_FRAME_SOURCES> observations;
410
412};
413
415{
416 t_constellation_device_id_t id;
417
418 //! A bit per keyframe age the solve held a state for.
420
421 //! Solved LED model -> IMU rotation.
423
424 //! The LED model -> IMU rotation from the rig calibration, for measuring how far the solve has walked from it.
426};
427
428/*!
429 * One solve, whole: what went in, what came out, and how the solver got there.
430 */
432{
433 //! Counts solves over the life of the fusion, and is what the logger uses as a sequence timeline.
434 uint64_t solve_index{0};
435
436 uint32_t num_keyframes;
437 //! Newest first, so index `i` is keyframe age `i`.
438 std::array<SnapshotKeyframe, kSlidingWindowSize> keyframes;
439
440 SnapshotDevice device;
441
442 //! Solved gravity magnitude, in meters per second squared.
443 double world_gravity_mag{0.0};
444
445 uint64_t num_late_samples_dropped{0};
446
447 int num_residuals{0};
448 int num_parameter_blocks{0};
449 uint32_t num_iterations{0};
450
451 //! Residual RMS in standard deviations, before and after the solve. See @ref kTargetResidualRmsSigma.
452 double initial_rms_sigma{0.0};
453 double final_rms_sigma{0.0};
454
455 double solve_time_s{0.0};
456
457 //! Time spent computing the pose covariances after the solve
459
460 //! Ceres' termination type, already stringified, since the logger has no business including Ceres headers.
461 std::string termination;
462};
463
465{
466public: // Fields
467 ceres::Context *context;
468
469 u_logging_level log_level;
470
471 //! Whether we're in single threaded mode
473
474 /*!
475 * The fusion thread, lock does not protect fusion operations, only threading operations like
476 * wakeup and inter-thread sample transfer.
477 *
478 * See @ref SensorFusion::lock for fusion operations.
479 */
481
482 //! Locks heavy fusion operations, but no threading operations (such as wakeup/sleep).
483 os::Mutex lock;
484
485 /*!
486 * Backing store for the keyframe ring. The keyframe with absolute index `n` lives in slot
487 * `n % kSlidingWindowSize`.
488 *
489 * Reach these through @ref keyframeLocked instead of indexing here, and never move the entries around.
490 */
491 std::array<Keyframe, kSlidingWindowSize> keyframe_storage{};
492
493 //! One past the absolute index of the newest keyframe, so the newest keyframe is `keyframe_head - 1`.
494 uint64_t keyframe_head{0};
495
496 //! How many slots of the ring hold real keyframes, saturating at `kSlidingWindowSize`.
497 uint32_t num_keyframes{0};
498
499 //! The cameras that are present in the system
500 std::array<std::array<CameraDescription, XRT_TRACKING_MAX_FRAME_SOURCES>,
501 XRT_CONSTELLATION_MAX_TRACKING_MOSAICS>
503
504 Device device;
505
506 /*!
507 * Camera samples handed over by @ref pushCameraSample, in arrival order, waiting to be folded into the
508 * window by the fusion thread.
509 */
510 std::vector<constellation::CameraSample> sample_queue;
511
512 /*!
513 * How many samples have been thrown away for arriving with @ref sample_queue already at
514 * @ref kMaxQueuedCameraSamples.
515 *
516 * If this grows, it indicates the fusion thread has likely stalled or is running too slowly.
517 */
519
520 //! Whether the current state is dirty and we need to run an optimization
521 bool dirty{false};
522
523 //! How many camera samples have been dropped for turning up after their keyframe was already sealed.
525
526 //! The active Ceres problem.
527 struct SensorFusionProblem *problem{nullptr};
528
529#ifdef XRT_FEATURE_RERUN
530 /*!
531 * Rerun logging, null unless enabled.
532 */
533 std::unique_ptr<struct RerunContext> rerun_context{nullptr};
534#endif
535
536 /*!
537 * The window and its solved states as of the most recent solve, filled by @ref runFusionLocked once Ceres has
538 * returned. Kept between solves since dead reckoning integrates with the solved gravity it holds.
539 *
540 * This is owned in full by the fusion thread, don't touch it outside of that.
541 */
543
544private: // Methods
545 //! Marks the fusion dirty, call with the fusion lock held.
546 void
547 markDirtyFusionLocked();
548
549 //! Wakes the fusion thread, call with the thread lock held.
550 void
551 wakeThreadLocked();
552
553 /*!
554 * Retires the keyframe falling out of the window and opens a fresh one at the head of the ring.
555 *
556 * @return The new keyframe, which is the newest in the window and starts out open.
557 */
558 Keyframe &
559 pushKeyframeLocked(const constellation::CameraSample &sample);
560
561 //! Seals every keyframe that has aged past @ref kKeyframeSealLagKeyframes.
562 void
563 sealAgedKeyframesLocked();
564
565 //! @return `true` when the built problem has something to solve.
566 bool
567 buildFusionLocked();
568
569 /*!
570 * Rebuilds the problem, without solving it, so that an eviction can marginalize against the window as it
571 * stands.
572 *
573 * Needed when an earlier eviction in the same pass has already marginalized against the current problem: the
574 * prior it produced lives outside that problem, so a second eviction against it would drop the prior.
575 */
576 void
577 rebuildForEvictionLocked();
578
579 /*!
580 * Integrates every dirty IMU interval of the device.
581 *
582 * @param[in,out] work_remaining Set when an interval could not be integrated yet and has to be retried.
583 *
584 * @returns When an interval was closed and it's worth a fusion run.
585 */
586 bool
587 preintegrateDeviceLocked(bool &work_remaining);
588
589 //! Triggers the sensor fusion to reset it's state
590 void
591 resetLocked();
592
593public: // Methods
594 /*!
595 * Constructs a new constellation tracker.
596 *
597 * @param context The Ceres context to use
598 * @param deterministic Whether to not startup a separate thread, and run the fusion on the same
599 * thread as the callee's push functions.
600 * @param id The ID of the constellation device
601 * @param led_model The LED model of the device
602 * @param sensor_calibration The noise model and bias behaviour of the device's IMU
603 */
604 SensorFusion(ceres::Context *context,
605 bool deterministic,
606 t_constellation_device_id_t id,
607 const t_constellation_tracker_led_model *led_model,
608 const xrt_imu_noise_calibration &sensor_calibration);
609
611
612 //! Returns the index of the keyframe in the ring buffer.
613 uint32_t
614 keyframeIdxLocked(uint32_t age) const;
615
616 /*!
617 * The age of the keyframe currently living in ring slot @p idx, or `false` when no keyframe in the window
618 * does.
619 */
620 bool
621 keyframeAgeForIdxLocked(size_t idx, uint32_t &out_age) const;
622
623 /*!
624 * The keyframe `age` steps back from the newest, so age 0 is the newest keyframe in the window and
625 * `num_keyframes - 1` is the oldest.
626 */
627 Keyframe &
628 keyframeLocked(uint32_t age);
629
630 const Keyframe &
631 keyframeLocked(uint32_t age) const;
632
633 //! The preintegration covering the interval that *ends* at `keyframeLocked(age)`.
635 preintegrationLocked(uint32_t age);
636
637 //! Adds a camera to the sensor fusion
638 void
639 addCamera(const CameraDescription &camera_description);
640
641 /*!
642 * Hands a device the IMU intrinsics a rig calibration solved for. This is used as a better seed start than
643 * assuming an identity calibration.
644 *
645 * Blocks until the current solve is complete.
646 */
647 void
649
650 /*!
651 * Hands a complete constellation camera sample to the fusion thread. Must be called with
652 * `tracker->device_lock` held.
653 *
654 * Does no work beyond the copy onto @ref sample_queue: everything the sample goes on to do to the window
655 * happens on the fusion thread, so a camera thread never blocks on anything more than the queue push.
656 */
657 void
659
660 /*!
661 * Pushes an IMU sample for this particular device, IMU samples should be in OpenXR coordinate space. IMU biases
662 * found during rig calibration should not be applied to these samples.
663 *
664 * May wait on large data copies during inter-thread transfer.
665 * If your thread timing is thrown off calling this, put it behind a threaded queue,
666 * but this should be fine without, the copies are fast.
667 */
668 void
669 pushImuSample(const xrt_imu_sample &sample);
670
671 //! Provides the latest predicted device pose for the requested timestamp.
672 void
673 getTrackedPose(timepoint_ns requested_time_ns, xrt_space_relation &out_relation);
674
675 /*!
676 * Provides the latest predicted device pose for the requested timestamp, also checking for exact keyframe
677 * poses, if available.
678 */
679 void
680 getKeyframeTrackedPose(timepoint_ns requested_time_ns, xrt_space_relation &out_relation);
681
682 /*!
683 * Appends any camera samples into @p pending, before sorting pending by timestamp order.
684 *
685 * @remarks Call with @ref SensorFusion::thread lock held.
686 *
687 * @return Whether any camera samples have been passed by the IMU, and so are ready for integration.
688 */
689 bool
690 pullReadyCameraSamplesLocked(timepoint_ns now_ns, std::vector<constellation::CameraSample> &pending);
691
692 void
693 writeSampleToKeyframe(Keyframe &keyframe, const DeviceObservation &observation, uint32_t camera_index);
694
695 /*!
696 * Folds one queued camera sample into the window, or drops if it's no longer able to be merged in.
697 *
698 * This is the far side of @ref sample_queue, and the only thing that grows the window.
699 */
700 void
702
703 /*!
704 * Dead reckons @p inout_solve forward through every IMU sample that has arrived since it, pushing a pose for
705 * each into the device's relation history.
706 *
707 * The history only takes poses newer than the newest it already holds, so wherever it already covers the range,
708 * the poses integrated there are dropped, and only the state carried out the end of the range is kept.
709 *
710 * @return false, leaving @p inout_solve untouched, when the samples could not be collected.
711 */
712 bool
714
715 /*!
716 * Uses the latest IMU reckoned solve and reckons it forward to the latest IMU sample
717 */
718 void
720
721 /*!
722 * Runs preintegration, and when @p have_new_input says the window has gained something to solve over,
723 * builds the problem, then solves it if need-be.
724 *
725 * @param have_new_input Whether this pass drained any camera samples, rather than being woken by an IMU
726 * sample arriving to unpark a preintegration.
727 */
728 void
729 tickLocked(bool have_new_input);
730};
731
732}; // namespace xrt::tracking::constellation::optimizer::sensor_fusion
Covariance-aware relation history.
u_logging_level
Logging level enum.
Definition u_logging.h:45
int64_t timepoint_ns
Integer timestamp type.
Definition u_time.h:77
int64_t time_duration_ns
Integer duration type in nanoseconds.
Definition u_time.h:88
Generic math helpers for IMU preintegration.
Matrix< double, kNumPoseCovIndices, kNumPoseCovIndices > PoseCovarianceMatrix
[position; rotation; velocity], the order computeImuResidual writes its residual in.
Definition imu_preintegration.hpp:197
Wrapper around OS threading native functions.
static constexpr uint32_t kKeyframeSealLagKeyframes
How many newer keyframes have to pile up before a keyframe gets sealed and marginalized.
Definition sensor_fusion.hpp:65
static constexpr uint32_t kImuBufferSize
The number of IMU samples to keep in our ring buffer.
Definition sensor_fusion.hpp:53
static constexpr uint32_t kMaxImuSamplesPerKeyframe
The most IMU samples we ever expect to find between two keyframes.
Definition sensor_fusion.hpp:60
DevicePreintegrationState
Definition sensor_fusion.hpp:177
@ Integrated
This preintegration is complete and will never change.
@ Empty
Does not refer to any real pre-integration. This term contributes nothing and can be skipped.
static constexpr uint32_t kMaxQueuedCameraSamples
How many camera samples may be queued for the fusion thread before new ones start being dropped.
Definition sensor_fusion.hpp:78
static constexpr uint32_t kSlidingWindowSize
How many keyframes to keep in our sliding window.
Definition sensor_fusion.hpp:50
Definition camera_model.h:21
All in one helper that handles locking, waiting for change and starting a thread.
Definition os_threading.h:587
The LED model is a series of points which define the real-world positions of all LEDs.
Definition t_constellation.h:313
Definition u_worker.c:38
Definition t_constellation_tracker_internal.hpp:120
Definition t_constellation_tracker_internal.hpp:417
camera_model params
Intrinsics and distortion, which is what turns a LED in the device's frame into a pixel.
Definition sensor_fusion.hpp:337
void resetLocked()
Resets the device's state to a fresh state.
Definition sensor_fusion.cpp:2349
CovarianceRelationHistory keyframe_relation_history
The relation history containing only the exact camera keyframes, to allow access to exact fused keyfr...
Definition sensor_fusion.hpp:274
std::atomic< uint64_t > imu_write_count
The total number of IMU samples ever pushed for this device.
Definition sensor_fusion.hpp:300
std::optional< DeviceSolveState > latest_imu_reckon
The solved state for the latest IMU dead reckoned position.
Definition sensor_fusion.hpp:263
std::array< RingImuSample, kImuBufferSize > imu_samples
The buffered IMU samples, indexed by absolute sample index modulo kImuBufferSize.
Definition sensor_fusion.hpp:305
const xrt_imu_noise_calibration sensor_calibration
The noise model and bias behaviour of the device's IMU, as the driver gave it.
Definition sensor_fusion.hpp:258
DeviceImuCalibration stored_imu_calibration
Pre-calibrated accelerometer bias, in meters per second squared.
Definition sensor_fusion.hpp:277
std::optional< DeviceSolveState > latest_solve
The latest fused solve.
Definition sensor_fusion.hpp:261
std::array< DevicePreintegration, kSlidingWindowSize > preintegrations
The preintegrated IMU samples between keyframes, as a ring running parallel to SensorFusion::keyframe...
Definition sensor_fusion.hpp:287
CovarianceRelationHistory imu_relation_history
The relation history of the device, used as a fast way to retrieve the latest dead-reckoned poses.
Definition sensor_fusion.hpp:266
The IMU intrinsics the rig calibration happened to solve for, as the fusion takes them in as a seed.
Definition sensor_fusion.hpp:217
xrt_vec3_f64 accel_scale XRT_VEC3_ONE
Per-axis accelerometer scale, unitless.
Definition sensor_fusion.hpp:223
xrt_vec3_f64 gyro_bias XRT_VEC3_ZERO
Gyroscope bias, in radians per second.
Definition sensor_fusion.hpp:221
xrt_vec3_f64 accel_bias XRT_VEC3_ZERO
Accelerometer bias, in meters per second squared.
Definition sensor_fusion.hpp:219
xrt_quat_f64 Qcv_model_imu XRT_QUAT_IDENTITY
The LED model -> IMU rotation.
Definition sensor_fusion.hpp:226
xrt_pose Tcv_cam_device_seed
The seed cam->device transform.
Definition sensor_fusion.hpp:89
std::array< xrt_vec3, XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE > points3d
3D model correspondences
Definition sensor_fusion.hpp:94
uint32_t camera_idx
The camera that this observation is tied to.
Definition sensor_fusion.hpp:83
std::array< xrt_vec2, XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE > points2d
2D observations
Definition sensor_fusion.hpp:92
xrt_pose Tcv_world_cam
The camera pose at the timestamp.
Definition sensor_fusion.hpp:86
Stores state for a single preintegration residual for a device.
Definition sensor_fusion.hpp:190
timepoint_ns time_ns
The time of the solve.
Definition sensor_fusion.hpp:232
xrt_vec3_f64 Tcv_world_device
The position component of the world -> LED model pose at the solve.
Definition sensor_fusion.hpp:234
DeviceImuCalibration imu_calibration
The imu calibration at the solve.
Definition sensor_fusion.hpp:240
xrt_vec3_f64 world_velocity
The world velocity at the solve.
Definition sensor_fusion.hpp:238
PoseCovarianceMatrix covariance
The pose covariance at the solve, in the solver's tangent space (world position, LED model local rota...
Definition sensor_fusion.hpp:247
xrt_quat_f64 Qcv_world_device
The orientation component of the world -> LED model pose at the solve.
Definition sensor_fusion.hpp:236
uint32_t mosaic_idx
The mosaic this keyframe corresponds to.
Definition sensor_fusion.hpp:102
bool sealed
Set once this keyframe can no longer accept observations due to marginalization or age.
Definition sensor_fusion.hpp:109
timepoint_ns timestamp_ns
The timestamp of this observation.
Definition sensor_fusion.hpp:104
One IMU sample as it sits in Device::imu_samples.
Definition sensor_fusion.hpp:126
timepoint_ns loadTimestamp() const noexcept
Just the timestamp, so the bisection does not pull in six words it is only going to throw away.
Definition sensor_fusion.hpp:166
static constexpr size_t kTimestampWord
The word xrt_imu_sample::timestamp_ns lives in, which is the only one the collector's bisection needs...
Definition sensor_fusion.hpp:136
os::Mutex lock
Locks heavy fusion operations, but no threading operations (such as wakeup/sleep).
Definition sensor_fusion.hpp:483
void pushImuSample(const xrt_imu_sample &sample)
Pushes an IMU sample for this particular device, IMU samples should be in OpenXR coordinate space.
Definition sensor_fusion.cpp:2836
uint64_t num_late_samples_dropped
How many camera samples have been dropped for turning up after their keyframe was already sealed.
Definition sensor_fusion.hpp:524
bool dirty
Whether the current state is dirty and we need to run an optimization.
Definition sensor_fusion.hpp:521
SolveSnapshot snapshot
The window and its solved states as of the most recent solve, filled by runFusionLocked once Ceres ha...
Definition sensor_fusion.hpp:542
bool pullReadyCameraSamplesLocked(timepoint_ns now_ns, std::vector< constellation::CameraSample > &pending)
Appends any camera samples into pending, before sorting pending by timestamp order.
Definition sensor_fusion.cpp:2525
void addCamera(const CameraDescription &camera_description)
Adds a camera to the sensor fusion.
Definition sensor_fusion.cpp:2610
void updateDeviceRelationHistoryLocked()
Uses the latest IMU reckoned solve and reckons it forward to the latest IMU sample.
Definition sensor_fusion.cpp:3142
std::vector< constellation::CameraSample > sample_queue
Camera samples handed over by pushCameraSample, in arrival order, waiting to be folded into the windo...
Definition sensor_fusion.hpp:510
uint32_t keyframeIdxLocked(uint32_t age) const
Returns the index of the keyframe in the ring buffer.
Definition sensor_fusion.cpp:2372
std::array< Keyframe, kSlidingWindowSize > keyframe_storage
Backing store for the keyframe ring.
Definition sensor_fusion.hpp:491
void setDeviceImuCalibration(const DeviceImuCalibration &calibration)
Hands a device the IMU intrinsics a rig calibration solved for.
Definition sensor_fusion.cpp:2619
uint32_t num_keyframes
How many slots of the ring hold real keyframes, saturating at kSlidingWindowSize.
Definition sensor_fusion.hpp:497
struct SensorFusionProblem * problem
The active Ceres problem.
Definition sensor_fusion.hpp:527
void getKeyframeTrackedPose(timepoint_ns requested_time_ns, xrt_space_relation &out_relation)
Provides the latest predicted device pose for the requested timestamp, also checking for exact keyfra...
Definition sensor_fusion.cpp:2919
bool keyframeAgeForIdxLocked(size_t idx, uint32_t &out_age) const
The age of the keyframe currently living in ring slot idx, or false when no keyframe in the window do...
Definition sensor_fusion.cpp:2378
DevicePreintegration & preintegrationLocked(uint32_t age)
The preintegration covering the interval that ends at keyframeLocked(age).
Definition sensor_fusion.cpp:2408
void pushCameraSample(constellation::ConstellationTracker *tracker, const constellation::CameraSample &sample)
Hands a complete constellation camera sample to the fusion thread.
Definition sensor_fusion.cpp:2656
bool reckonToLatestImu(DeviceSolveState &inout_solve)
Dead reckons inout_solve forward through every IMU sample that has arrived since it,...
Definition sensor_fusion.cpp:3096
bool deterministic
Whether we're in single threaded mode.
Definition sensor_fusion.hpp:472
void getTrackedPose(timepoint_ns requested_time_ns, xrt_space_relation &out_relation)
Provides the latest predicted device pose for the requested timestamp.
Definition sensor_fusion.cpp:2886
uint64_t num_queued_samples_dropped
How many samples have been thrown away for arriving with sample_queue already at kMaxQueuedCameraSamp...
Definition sensor_fusion.hpp:518
void tickLocked(bool have_new_input)
Runs preintegration, and when have_new_input says the window has gained something to solve over,...
Definition sensor_fusion.cpp:3160
Keyframe & keyframeLocked(uint32_t age)
The keyframe age steps back from the newest, so age 0 is the newest keyframe in the window and num_ke...
Definition sensor_fusion.cpp:2392
void processCameraSampleLocked(const constellation::CameraSample &sample)
Folds one queued camera sample into the window, or drops if it's no longer able to be merged in.
Definition sensor_fusion.cpp:2727
std::array< std::array< CameraDescription, XRT_TRACKING_MAX_FRAME_SOURCES >, XRT_CONSTELLATION_MAX_TRACKING_MOSAICS > cameras
The cameras that are present in the system.
Definition sensor_fusion.hpp:502
uint64_t keyframe_head
One past the absolute index of the newest keyframe, so the newest keyframe is keyframe_head - 1.
Definition sensor_fusion.hpp:494
xrt_quat_f64 calibrated_Qcv_model_imu
The LED model -> IMU rotation from the rig calibration, for measuring how far the solve has walked fr...
Definition sensor_fusion.hpp:425
uint32_t keyframe_mask
A bit per keyframe age the solve held a state for.
Definition sensor_fusion.hpp:419
xrt_quat_f64 Qcv_model_imu
Solved LED model -> IMU rotation.
Definition sensor_fusion.hpp:422
One camera's view of one device at one keyframe.
Definition sensor_fusion.hpp:388
One solve, whole: what went in, what came out, and how the solver got there.
Definition sensor_fusion.hpp:432
double world_gravity_mag
Solved gravity magnitude, in meters per second squared.
Definition sensor_fusion.hpp:443
uint64_t solve_index
Counts solves over the life of the fusion, and is what the logger uses as a sequence timeline.
Definition sensor_fusion.hpp:434
time_duration_ns covariance_time_ns
Time spent computing the pose covariances after the solve.
Definition sensor_fusion.hpp:458
std::string termination
Ceres' termination type, already stringified, since the logger has no business including Ceres header...
Definition sensor_fusion.hpp:461
double initial_rms_sigma
Residual RMS in standard deviations, before and after the solve. See kTargetResidualRmsSigma.
Definition sensor_fusion.hpp:452
std::array< SnapshotKeyframe, kSlidingWindowSize > keyframes
Newest first, so index i is keyframe age i.
Definition sensor_fusion.hpp:438
A device's solved state at one keyframe.
Definition sensor_fusion.hpp:349
PoseCovarianceMatrix covariance
The covariance matrix of this solve's keyframe parameters of the pose.
Definition sensor_fusion.hpp:378
xrt_vec3_f64 linear_velocity
Solved world-frame velocity, in meters per second.
Definition sensor_fusion.hpp:355
bool imu_sample_valid
Whether or not the IMU sample is valid.
Definition sensor_fusion.hpp:358
xrt_imu_sample world_imu_sample
World-frame IMU sample that ended the preintegration.
Definition sensor_fusion.hpp:364
xrt_vec3_f64 gyro_bias
Solved gyroscope bias, in radians per second.
Definition sensor_fusion.hpp:369
xrt_quat_f64 Qcv_world_device
The orientational part of the solved world -> LED model pose.
Definition sensor_fusion.hpp:353
xrt_vec3_f64 accel_scale
Solved per-axis accelerometer scale, unitless.
Definition sensor_fusion.hpp:371
xrt_imu_sample calibrated_imu_sample
Device-frame calibrated IMU sample that ended the preintegration.
Definition sensor_fusion.hpp:362
xrt_imu_sample raw_imu_sample
Device-frame raw IMU sample that ended the preintegration.
Definition sensor_fusion.hpp:360
xrt_vec3_f64 Tcv_world_device
The positional part of the solved world -> LED model pose.
Definition sensor_fusion.hpp:351
xrt_vec3_f64 accel_bias
Solved accelerometer bias, in meters per second squared.
Definition sensor_fusion.hpp:367
IMU noise model and bias calibration, the noise density grows with the reading on each axis.
Definition xrt_tracking.h:213
IMU Sample.
Definition xrt_tracking.h:179
A pose composed of a position and orientation.
Definition xrt_defines.h:535
A quaternion with double floats.
Definition xrt_defines.h:269
A relation with two spaces, includes velocity and acceleration.
Definition xrt_defines.h:726
A 3 element vector with single doubles.
Definition xrt_defines.h:335
Header defining the tracking system integration in Monado.
Header defining the constellation tracker parameters and functions.