12#include "xrt/xrt_config_build.h"
25#include <ceres/context.h>
35namespace xrt::tracking::constellation {
39struct ConstellationTracker;
43namespace xrt::tracking::constellation::optimizer::sensor_fusion {
45using namespace xrt::auxiliary;
46using namespace xrt::tracking;
47using namespace xrt::tracking::constellation::optimizer;
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");
92 std::array<xrt_vec2, XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE>
points2d;
94 std::array<xrt_vec3, XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE>
points3d;
111 std::array<DeviceObservation, XRT_TRACKING_MAX_FRAME_SOURCES> observations;
112 uint32_t num_observations;
127 static_assert(std::is_trivially_copyable_v<xrt_imu_sample>);
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");
133 static constexpr size_t kNumWords =
sizeof(
xrt_imu_sample) /
sizeof(uint64_t);
138 std::array<std::atomic<uint64_t>, kNumWords> words;
143 std::array<uint64_t, kNumWords> raw;
144 std::memcpy(raw.data(), &sample,
sizeof(sample));
146 for (
size_t i = 0; i < kNumWords; i++) {
147 this->words[i].store(raw[i], std::memory_order_relaxed);
152 load() const noexcept
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);
160 std::memcpy(&sample, raw.data(),
sizeof(sample));
168 const uint64_t raw = this->words[
kTimestampWord].load(std::memory_order_relaxed);
171 std::memcpy(×tamp, &raw,
sizeof(timestamp));
198 return this->state == DevicePreintegrationState::Dirty;
205 .imu_preintegration = PreintegratedImuSamples::Identity(),
253 const t_constellation_device_id_t id;
302 std::atomic<timepoint_ns> latest_imu_ts_ns;
308 Device(t_constellation_device_id_t
id,
315 operator=(
const Device &) =
delete;
318 getLatestImuCalibration()
const;
398 std::array<xrt_vec2, XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE> points2d;
399 std::array<xrt_vec3, XRT_CONSTELLATION_MAX_LEDS_PER_DEVICE> points3d;
408 uint32_t num_observations;
409 std::array<SnapshotObservation, XRT_TRACKING_MAX_FRAME_SOURCES> observations;
416 t_constellation_device_id_t id;
436 uint32_t num_keyframes;
438 std::array<SnapshotKeyframe, kSlidingWindowSize>
keyframes;
445 uint64_t num_late_samples_dropped{0};
447 int num_residuals{0};
448 int num_parameter_blocks{0};
449 uint32_t num_iterations{0};
453 double final_rms_sigma{0.0};
455 double solve_time_s{0.0};
467 ceres::Context *context;
500 std::array<std::array<CameraDescription, XRT_TRACKING_MAX_FRAME_SOURCES>,
501 XRT_CONSTELLATION_MAX_TRACKING_MOSAICS>
529#ifdef XRT_FEATURE_RERUN
533 std::unique_ptr<struct RerunContext> rerun_context{
nullptr};
547 markDirtyFusionLocked();
563 sealAgedKeyframesLocked();
577 rebuildForEvictionLocked();
587 preintegrateDeviceLocked(
bool &work_remaining);
606 t_constellation_device_id_t
id,
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.
@ Dirty
Needs to be recomputed.
@ 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 t_constellation_tracker_internal.hpp:120
Definition t_constellation_tracker_internal.hpp:417
Definition imu_preintegration.hpp:251
Definition sensor_fusion.hpp:331
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
Definition sensor_fusion.hpp:251
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
Definition sensor_fusion.hpp:81
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
Definition sensor_fusion.hpp:230
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
Definition sensor_fusion.hpp:100
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
Definition sensor_fusion.hpp:465
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
Definition sensor_fusion.hpp:415
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
Definition sensor_fusion.hpp:403
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.