Monado OpenXR Runtime
t_imu.h
Go to the documentation of this file.
1 // Copyright 2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief C interface to basic IMU fusion.
6  * @author Rylie Pavlik <rylie.pavlik@collabora.com>
7  * @ingroup aux_tracking
8  */
9 
10 #include "math/m_api.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 
17 /*!
18  * Opaque type for fusing IMU reports.
19  */
20 struct imu_fusion;
21 
22 /*!
23  * Create a struct imu_fusion.
24  *
25  * @public @memberof imu_fusion
26  * @ingroup aux_tracking
27  */
28 struct imu_fusion *
29 imu_fusion_create(void);
30 
31 
32 /*!
33  * Destroy a struct imu_fusion.
34  *
35  * Should not be called simultaneously with any other imu_fusion function.
36  *
37  * @param fusion The IMU Fusion object
38  *
39  * @public @memberof imu_fusion
40  * @ingroup aux_tracking
41  */
42 void
43 imu_fusion_destroy(struct imu_fusion *fusion);
44 
45 /*!
46  * Predict and correct fusion with a gyroscope reading.
47  *
48  * dt should not be zero: If you're receiving accel and gyro data at the same
49  * time, call imu_fusion_incorporate_gyros_and_accelerometer() instead.
50  *
51  * Should not be called simultaneously with any other imu_fusion function.
52  *
53  * Non-zero return means error.
54  *
55  * @param fusion The IMU Fusion object
56  * @param timestamp_ns The timestamp corresponding to the information being
57  * processed with this call.
58  * @param ang_vel Angular velocity vector from gyroscope: in radians per second.
59  * @param ang_vel_variance The variance of the angular velocity measurements:
60  * part of the characteristics of the IMU being used.
61  *
62  * @public @memberof imu_fusion
63  * @ingroup aux_tracking
64  */
65 int
67  uint64_t timestamp_ns,
68  struct xrt_vec3 const *ang_vel,
69  struct xrt_vec3 const *ang_vel_variance);
70 
71 /*!
72  * Predict and correct fusion with an accelerometer reading.
73  *
74  * If you're receiving accel and gyro data at the same time, call
75  * imu_fusion_incorporate_gyros_and_accelerometer() instead.
76  *
77  * Should not be called simultaneously with any other imu_fusion function.
78  *
79  * Non-zero return means error.
80  *
81  * @param fusion The IMU Fusion object
82  * @param timestamp_ns The timestamp corresponding to the information being
83  * processed with this call.
84  * @param accel Accelerometer data (in m/s/s) including the effect of gravity -
85  * assumed to be +y when aligned with the world.
86  * @param accel_variance The variance of the accelerometer measurements: part of
87  * the characteristics of the IMU being used.
88  * @param out_world_accel Optional output parameter: will contain the
89  * non-gravity acceleration in the world frame.
90  *
91  * @public @memberof imu_fusion
92  * @ingroup aux_tracking
93  */
94 int
96  uint64_t timestamp_ns,
97  struct xrt_vec3 const *accel,
98  struct xrt_vec3 const *accel_variance,
99  struct xrt_vec3 *out_world_accel);
100 
101 /*!
102  * Predict and correct fusion with a simultaneous accelerometer and gyroscope
103  * reading.
104  *
105  * Should not be called simultaneously with any other imu_fusion function.
106  *
107  * Non-zero return means error.
108  *
109  * @param fusion The IMU Fusion object
110  * @param timestamp_ns The timestamp corresponding to the information being
111  * processed with this call.
112  * @param ang_vel Angular velocity vector from gyroscope: radians/s
113  * @param ang_vel_variance The variance of the angular velocity measurements:
114  * part of the characteristics of the IMU being used.
115  * @param accel Accelerometer data (in m/s/s) including the effect of gravity -
116  * assumed to be +y when aligned with the world.
117  * @param accel_variance The variance of the accelerometer measurements: part of
118  * the characteristics of the IMU being used.
119  * @param out_world_accel Optional output parameter: will contain the
120  * non-gravity acceleration in the world frame.
121  *
122  * @public @memberof imu_fusion
123  * @ingroup aux_tracking
124  */
125 int
127  uint64_t timestamp_ns,
128  struct xrt_vec3 const *ang_vel,
129  struct xrt_vec3 const *ang_vel_variance,
130  struct xrt_vec3 const *accel,
131  struct xrt_vec3 const *accel_variance,
132  struct xrt_vec3 *out_world_accel);
133 
134 /*!
135  * Get the predicted state. Does not advance the internal state clock.
136  *
137  * Non-zero return means error.
138  *
139  * @param fusion The IMU Fusion object
140  * @param timestamp_ns The timestamp corresponding to the predicted state you
141  * want.
142  * @param out_quat The quaternion to populate with the predicted orientation.
143  * @param out_ang_vel The vector to poluate with the predicted angular velocity.
144  *
145  * @public @memberof imu_fusion
146  * @ingroup aux_tracking
147  */
148 int
149 imu_fusion_get_prediction(struct imu_fusion const *fusion,
150  uint64_t timestamp_ns,
151  struct xrt_quat *out_quat,
152  struct xrt_vec3 *out_ang_vel);
153 
154 
155 /*!
156  * Get the predicted state as a rotation vector. Does not advance the internal
157  * state clock.
158  *
159  * This is mostly for debugging: a rotation vector can be easier to visualize or
160  * understand intuitively.
161  *
162  * Non-zero return means error.
163  *
164  * @param fusion The IMU Fusion object
165  * @param timestamp_ns The timestamp corresponding to the predicted state you
166  * want.
167  * @param out_rotation_vec The vector to poluate with the predicted orientation
168  * rotation vector.
169  *
170  * @public @memberof imu_fusion
171  * @ingroup aux_tracking
172  */
173 int
175  uint64_t timestamp_ns,
176  struct xrt_vec3 *out_rotation_vec);
177 
178 
179 #ifdef __cplusplus
180 }
181 #endif
int imu_fusion_incorporate_accelerometer(struct imu_fusion *fusion, uint64_t timestamp_ns, struct xrt_vec3 const *accel, struct xrt_vec3 const *accel_variance, struct xrt_vec3 *out_world_accel)
Predict and correct fusion with an accelerometer reading.
Definition: t_imu.cpp:80
struct imu_fusion * imu_fusion_create(void)
Create a struct imu_fusion.
Definition: t_imu.cpp:40
int imu_fusion_get_prediction(struct imu_fusion const *fusion, uint64_t timestamp_ns, struct xrt_quat *out_quat, struct xrt_vec3 *out_ang_vel)
Get the predicted state.
Definition: t_imu.cpp:105
int imu_fusion_get_prediction_rotation_vec(struct imu_fusion const *fusion, uint64_t timestamp_ns, struct xrt_vec3 *out_rotation_vec)
Get the predicted state as a rotation vector.
Definition: t_imu.cpp:137
int imu_fusion_incorporate_gyros(struct imu_fusion *fusion, uint64_t timestamp_ns, struct xrt_vec3 const *ang_vel, struct xrt_vec3 const *ang_vel_variance)
Predict and correct fusion with a gyroscope reading.
Definition: t_imu.cpp:60
int imu_fusion_incorporate_gyros_and_accelerometer(struct imu_fusion *fusion, uint64_t timestamp_ns, struct xrt_vec3 const *ang_vel, struct xrt_vec3 const *ang_vel_variance, struct xrt_vec3 const *accel, struct xrt_vec3 const *accel_variance, struct xrt_vec3 *out_world_accel)
Predict and correct fusion with a simultaneous accelerometer and gyroscope reading.
Definition: t_imu.cpp:164
void imu_fusion_destroy(struct imu_fusion *fusion)
Destroy a struct imu_fusion.
Definition: t_imu.cpp:51
C interface to math library.
Definition: t_imu.cpp:25
A quaternion with single floats.
Definition: xrt_defines.h:216
A 3 element vector with single floats.
Definition: xrt_defines.h:271