a/tracking: Move dead reckoning to public header
Also rewrites the code to be in C Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2651>
4 unmodified lines56789101112131458 unmodified lines4 unmodified lines
add_library( aux_tracking STATIC t_data_utils.c t_dead_reckoning.h t_dead_reckoning.c t_imu_fusion.hpp t_imu.cpp t_imu.h t_openvr_tracker.cpp58 unmodified lines123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124// Copyright 2021-2024, Collabora, Ltd.// Copyright 2025, Beyley Cardellio// SPDX-License-Identifier: BSL-1.0/*! * @file * @brief SLAM tracking code. * @author Jakob Bornecrantz <jakob@collabora.com> * @author Mateo de Mayo <mateo.demayo@collabora.com> * @author Beyley Cardellio <ep1cm1n10n123@gmail.com> * @ingroup aux_tracking */
#include "xrt/xrt_defines.h"
#include "util/u_logging.h"#include "util/u_time.h"
#include "math/m_filter_fifo.h"#include "math/m_vec3.h"#include "math/m_predict.h"#include "math/m_api.h"
#include "t_dead_reckoning.h"
voidt_apply_dead_reckoning(struct m_ff_vec3_f32 *gyro_ff, struct m_ff_vec3_f32 *accel_ff, const struct xrt_vec3 *gravity_correction, timepoint_ns when_ns, const struct xrt_space_relation *base_rel, timepoint_ns base_rel_ts, struct xrt_space_relation *out_relation){ bool using_accel = accel_ff != NULL;
// @todo Make this determine the correct starting sample for gyro and accelerometer separately // Find oldest imu index i that is newer than latest SLAM pose (or -1) int i = 0; uint64_t imu_ts = UINT64_MAX; while (m_ff_vec3_f32_get_timestamp(gyro_ff, i, &imu_ts)) { if ((int64_t)imu_ts < base_rel_ts) { i--; // Back to the oldest newer-than-SLAM IMU index (or -1) break; } i++; }
if (i == -1) { U_LOG_W("No IMU samples received after latest SLAM pose (and frame)"); }
struct xrt_space_relation integ_rel = *base_rel; timepoint_ns integ_rel_ts = base_rel_ts; struct xrt_quat *orient = &integ_rel.pose.orientation; struct xrt_vec3 *pos = &integ_rel.pose.position; struct xrt_vec3 *ang_vel = &integ_rel.angular_velocity; struct xrt_vec3 *lin_vel = &integ_rel.linear_velocity; bool clamped = false; // If when_ns is older than the latest IMU ts
while (i >= 0) { // Decreasing i increases timestamp // Get samples struct xrt_vec3 gyro = XRT_VEC3_ZERO; struct xrt_vec3 accel = XRT_VEC3_ZERO; uint64_t gyro_ts = 0; uint64_t accel_ts = 0; bool got = true; got &= m_ff_vec3_f32_get(gyro_ff, i, &gyro, &gyro_ts); if (using_accel) { got &= m_ff_vec3_f32_get(accel_ff, i, &accel, &accel_ts); } timepoint_ns ts = gyro_ts;
// Checks if (ts > when_ns) { clamped = true; //! @todo Instead of using same a and g values, do an interpolated sample like this: // a = prev_a + ((when_ns - prev_ts) / (ts - prev_ts)) * (a - prev_a); // g = prev_g + ((when_ns - prev_ts) / (ts - prev_ts)) * (g - prev_g); ts = when_ns; // clamp ts to when_ns } if (using_accel) { assert(got && gyro_ts == accel_ts && "Failure getting synced gyro and accel samples"); } assert(ts >= base_rel_ts && "Accessing imu sample that is older than latest SLAM pose");
// Update time float dt = (float)time_ns_to_s(ts - integ_rel_ts); integ_rel_ts = ts;
// Integrate gyroscope struct xrt_quat angvel_delta = XRT_QUAT_IDENTITY; struct xrt_vec3 scaled_half_g = m_vec3_mul_scalar(gyro, dt * 0.5f); math_quat_exp(&scaled_half_g, &angvel_delta); // Same as using math_quat_from_angle_vector(g/dt) math_quat_rotate(orient, &angvel_delta, orient); // Orientation math_quat_rotate_derivative(orient, &gyro, ang_vel); // Angular velocity
if (using_accel) { // Integrate accelerometer struct xrt_vec3 world_accel = XRT_VEC3_ZERO; math_quat_rotate_vec3(orient, &accel, &world_accel); world_accel = m_vec3_add(world_accel, *gravity_correction); *lin_vel = m_vec3_add(*lin_vel, m_vec3_mul_scalar(world_accel, dt)); // Linear velocity const struct xrt_vec3 accumulated_position_change = m_vec3_add( // m_vec3_mul_scalar(*lin_vel, dt), // m_vec3_mul_scalar(world_accel, dt * dt * 0.5f)); //
math_vec3_accum(&accumulated_position_change, pos); }
if (clamped) { break; } i--; }
// Do the prediction based on the updated relation double last_imu_to_now_dt = time_ns_to_s(when_ns - integ_rel_ts); struct xrt_space_relation predicted_relation = XRT_SPACE_RELATION_ZERO; m_predict_relation(&integ_rel, last_imu_to_now_dt, &predicted_relation);
*out_relation = predicted_relation;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748// Copyright 2021-2024, Collabora, Ltd.// Copyright 2025, Beyley Cardellio// SPDX-License-Identifier: BSL-1.0/*! * @file * @brief SLAM tracking code. * @author Mateo de Mayo <mateo.demayo@collabora.com> * @author Beyley Cardellio <ep1cm1n10n123@gmail.com> * @ingroup aux_tracking */
#include "xrt/xrt_defines.h"
#include "util/u_logging.h"#include "util/u_time.h"
#include "math/m_filter_fifo.h"
#ifdef __cplusplusextern "C" {#endif
/*! * Apply dead reckoning to a base relation using IMU data from filter fifos. The filter fifos should be exclusively * locked and unmodified during the runtime of this function. * * @param gyro_ff The gyro filter fifo. * @param accel_ff The accelerometer filter fifo. Can be NULL to only use gyro. * @param gravity_correction Gravity correction to apply to accelerometer data. Can be NULL if * accel_ff is NULL. * @param when_ns The timestamp to predict to. * @param base_rel The base relation to start dead reckoning from. * @param base_rel_ts The timestamp of the base relation. * @param out_relation The predicted relation output. */voidt_apply_dead_reckoning(struct m_ff_vec3_f32 *gyro_ff, struct m_ff_vec3_f32 *accel_ff, const struct xrt_vec3 *gravity_correction, timepoint_ns when_ns, const struct xrt_space_relation *base_rel, timepoint_ns base_rel_ts, struct xrt_space_relation *out_relation);
#ifdef __cplusplus}#endif809 unmodified linesExpand all81081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390481481581681727 unmodified lines845846847848940849850851852853854855856857858859860861862863864700 unmodified linesExpand all809 unmodified linesExpand all
return true;}
//! Integrates IMU samples on top of a base pose and predicts from thatstatic voidpredict_pose_from_imu(TrackerSlam &t, timepoint_ns when_ns, xrt_space_relation base_rel, // Pose to integrate IMUs on top of timepoint_ns base_rel_ts, struct xrt_space_relation *out_relation){ os_mutex_lock(&t.lock_ff);
// Find oldest imu index i that is newer than latest SLAM pose (or -1) int i = 0; uint64_t imu_ts = UINT64_MAX; xrt_vec3 _; while (m_ff_vec3_f32_get(t.gyro_ff, i, &_, &imu_ts)) { if ((int64_t)imu_ts < base_rel_ts) { i--; // Back to the oldest newer-than-SLAM IMU index (or -1) break; } i++; }
if (i == -1) { SLAM_WARN("No IMU samples received after latest SLAM pose (and frame)"); }
xrt_space_relation integ_rel = base_rel; timepoint_ns integ_rel_ts = base_rel_ts; xrt_quat &o = integ_rel.pose.orientation; xrt_vec3 &p = integ_rel.pose.position; xrt_vec3 &w = integ_rel.angular_velocity; xrt_vec3 &v = integ_rel.linear_velocity; bool clamped = false; // If when_ns is older than the latest IMU ts
while (i >= 0) { // Decreasing i increases timestamp // Get samples xrt_vec3 g{}; xrt_vec3 a{}; uint64_t g_ts{}; uint64_t a_ts{}; bool got = true; got &= m_ff_vec3_f32_get(t.gyro_ff, i, &g, &g_ts); got &= m_ff_vec3_f32_get(t.accel_ff, i, &a, &a_ts); timepoint_ns ts = g_ts;
// Checks if (ts > when_ns) { clamped = true; //! @todo Instead of using same a and g values, do an interpolated sample like this: // a = prev_a + ((when_ns - prev_ts) / (ts - prev_ts)) * (a - prev_a); // g = prev_g + ((when_ns - prev_ts) / (ts - prev_ts)) * (g - prev_g); ts = when_ns; // clamp ts to when_ns } SLAM_DASSERT(got && g_ts == a_ts, "Failure getting synced gyro and accel samples"); SLAM_DASSERT(ts >= base_rel_ts, "Accessing imu sample that is older than latest SLAM pose");
// Update time float dt = (float)time_ns_to_s(ts - integ_rel_ts); integ_rel_ts = ts;
// Integrate gyroscope xrt_quat angvel_delta{}; xrt_vec3 scaled_half_g = g * dt * 0.5f; math_quat_exp(&scaled_half_g, &angvel_delta); // Same as using math_quat_from_angle_vector(g/dt) math_quat_rotate(&o, &angvel_delta, &o); // Orientation math_quat_rotate_derivative(&o, &g, &w); // Angular velocity
// Integrate accelerometer xrt_vec3 world_accel{}; math_quat_rotate_vec3(&o, &a, &world_accel); world_accel += t.gravity_correction; v += world_accel * dt; // Linear velocity p += v * dt + world_accel * (dt * dt * 0.5f); // Position
if (clamped) { break; } i--; }
os_mutex_unlock(&t.lock_ff);
// Do the prediction based on the updated relation double last_imu_to_now_dt = time_ns_to_s(when_ns - integ_rel_ts); xrt_space_relation predicted_relation{}; m_predict_relation(&integ_rel, last_imu_to_now_dt, &predicted_relation);
*out_relation = predicted_relation;}
//! Return our best guess of the relation at time @p when_ns using all the data the tracker has.static voidpredict_pose(TrackerSlam &t, timepoint_ns when_ns, struct xrt_space_relation *out_relation){27 unmodified lines }
if (t.pred_type == SLAM_PRED_DEAD_RECKONING) { predict_pose_from_imu(t, when_ns, rel, (int64_t)rel_ts, out_relation); os_mutex_lock(&t.lock_ff);
t_apply_dead_reckoning( // t.gyro_ff, // t.accel_ff, // &t.gravity_correction, // when_ns, // &rel, // (int64_t)rel_ts, // out_relation); //
os_mutex_unlock(&t.lock_ff); return; }
os_mutex_lock(&t.lock_ff);700 unmodified linesExpand all