1/*
2 $License:
3    Copyright (C) 2011-2012 InvenSense Corporation, All Rights Reserved.
4    See included License.txt for License information.
5 $
6 */
7#ifndef INV_DATA_BUILDER_H__
8#define INV_DATA_BUILDER_H__
9
10#include <stdio.h>
11#include "mltypes.h"
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
17// Uncomment this flag to enable playback debug and record or playback scenarios
18//#define INV_PLAYBACK_DBG
19
20/** This is a new sample of accel data */
21#define INV_ACCEL_NEW 1
22/** This is a new sample of gyro data */
23#define INV_GYRO_NEW 2
24/** This is a new sample of compass data */
25#define INV_MAG_NEW 4
26/** This is a new sample of temperature data */
27#define INV_TEMP_NEW 8
28/** This is a new sample of quaternion data */
29#define INV_QUAT_NEW 16
30/** This is a new sample of pressure data */
31#define INV_PRESSURE_NEW 32
32
33/** Set if the data is contiguous. Typically not set if a sample was skipped */
34#define INV_CONTIGUOUS 16
35/** Set if the calibrated data has been solved for */
36#define INV_CALIBRATED 32
37/** INV_NEW_DATA set for a new set of data, cleared if not available. */
38#define INV_NEW_DATA 64
39/** Set if raw data exists */
40#define INV_RAW_DATA 128
41/** Set if the sensor is on */
42#define INV_SENSOR_ON 256
43/** Set if quaternion has bias correction applied */
44#define INV_BIAS_APPLIED 512
45/** Set if quaternion is 6-axis */
46#define INV_QUAT_6AXIS 1024
47/** Set if quaternion is 9 axis */
48#define INV_QUAT_9AXIS 2048
49/** Set if quaternion is 3-axis, 3 elements (android only) */
50#define INV_QUAT_3AXIS 4096
51/** Set if DMP has applied bias */
52#define INV_DMP_BIAS_APPLIED 8192
53
54#define INV_PRIORITY_MOTION_NO_MOTION          100
55#define INV_PRIORITY_GYRO_TC                   150
56#define INV_PRIORITY_QUATERNION_GYRO_ACCEL     200
57#define INV_PRIORITY_QUATERNION_NO_GYRO        250
58#define INV_PRIORITY_MAGNETIC_DISTURBANCE      300
59#define INV_PRIORITY_HEADING_FROM_GYRO         350
60#define INV_PRIORITY_COMPASS_BIAS_W_GYRO       375
61#define INV_PRIORITY_COMPASS_VECTOR_CAL        400
62#define INV_PRIORITY_COMPASS_ADV_BIAS          500
63#define INV_PRIORITY_9_AXIS_FUSION             600
64#define INV_PRIORITY_9_AXIS_FUSION_LIGHT       650
65#define INV_PRIORITY_QUATERNION_ADJUST_9_AXIS  700
66#define INV_PRIORITY_QUATERNION_ACCURACY       750
67#define INV_PRIORITY_RESULTS_HOLDER            800
68#define INV_PRIORITY_INUSE_AUTO_CALIBRATION    850
69#define INV_PRIORITY_HAL_OUTPUTS               900
70#define INV_PRIORITY_GLYPH                     950
71#define INV_PRIORITY_SHAKE                     975
72#define INV_PRIORITY_SM                        1000
73
74struct inv_single_sensor_t {
75    /** Orientation Descriptor. Describes how to go from the mounting frame to the body frame when
76    * the rotation matrix could be thought of only having elements of 0,1,-1.
77    * 2 bits are used to describe the column of the 1 or -1 and the 3rd bit is used for the sign.
78    * Bit 8 is sign of +/- 1 in third row. Bit 6-7 is column of +/-1 in third row.
79    * Bit 5 is sign of +/- 1 in second row. Bit 3-4 is column of +/-1 in second row.
80    * Bit 2 is sign of +/- 1 in first row. Bit 0-1 is column of +/-1 in first row.
81    */
82    int orientation;
83    /** The raw data in raw data units in the mounting frame */
84    short raw[3];
85    /** Raw data in body frame */
86    long raw_scaled[3];
87    /** Calibrated data */
88    long calibrated[3];
89    long sensitivity;
90    /** Sample rate in microseconds */
91    long sample_rate_us;
92    long sample_rate_ms;
93    /** INV_CONTIGUOUS is set for contiguous data. Will not be set if there was a sample
94    * skipped due to power savings turning off this sensor.
95    * INV_NEW_DATA set for a new set of data, cleared if not available.
96    * INV_CALIBRATED_SET if calibrated data has been solved for */
97    int status;
98    /** 0 to 3 for how well sensor data and biases are known. 3 is most accurate. */
99    int accuracy;
100    inv_time_t timestamp;
101    inv_time_t timestamp_prev;
102    /** Bandwidth in Hz */
103    int bandwidth;
104};
105
106struct inv_quat_sensor_t {
107    long raw[4];
108    /** INV_CONTIGUOUS is set for contiguous data. Will not be set if there was a sample
109    * skipped due to power savings turning off this sensor.
110    * INV_NEW_DATA set for a new set of data, cleared if not available.
111    * INV_CALIBRATED_SET if calibrated data has been solved for */
112    int status;
113    inv_time_t timestamp;
114    long sample_rate_us;
115    long sample_rate_ms;
116};
117
118struct inv_soft_iron_t {
119    long raw[3];
120    long trans[3];
121    long matrix_d[9];  // Q30 format fixed point. The dynamic range is (-2.0 to 2.0);
122    float matrix_f[9];
123
124    int enable;
125};
126
127struct inv_sensor_cal_t {
128    struct inv_single_sensor_t gyro;
129    struct inv_single_sensor_t accel;
130    struct inv_single_sensor_t compass;
131    struct inv_single_sensor_t temp;
132    struct inv_quat_sensor_t quat;
133    struct inv_single_sensor_t pressure;
134    struct inv_soft_iron_t soft_iron;
135    /** Combinations of INV_GYRO_NEW, INV_ACCEL_NEW, INV_MAG_NEW to indicate
136    * which data is a new sample as these data points may have different sample rates.
137    */
138    int status;
139};
140
141// Useful for debug record and playback
142typedef enum {
143    RD_NO_DEBUG,
144    RD_RECORD,
145    RD_PLAYBACK
146} rd_dbg_mode;
147
148typedef enum {
149    PLAYBACK_DBG_TYPE_GYRO,
150    PLAYBACK_DBG_TYPE_ACCEL,
151    PLAYBACK_DBG_TYPE_COMPASS,
152    PLAYBACK_DBG_TYPE_TEMPERATURE,
153    PLAYBACK_DBG_TYPE_EXECUTE,
154    PLAYBACK_DBG_TYPE_A_ORIENT,
155    PLAYBACK_DBG_TYPE_G_ORIENT,
156    PLAYBACK_DBG_TYPE_C_ORIENT,
157    PLAYBACK_DBG_TYPE_A_SAMPLE_RATE,
158    PLAYBACK_DBG_TYPE_C_SAMPLE_RATE,
159    PLAYBACK_DBG_TYPE_G_SAMPLE_RATE,
160    PLAYBACK_DBG_TYPE_GYRO_OFF,
161    PLAYBACK_DBG_TYPE_ACCEL_OFF,
162    PLAYBACK_DBG_TYPE_COMPASS_OFF,
163    PLAYBACK_DBG_TYPE_Q_SAMPLE_RATE,
164    PLAYBACK_DBG_TYPE_QUAT,
165    PLAYBACK_DBG_TYPE_QUAT_OFF
166} inv_rd_dbg_states;
167
168/** Change this key if the definition of the struct inv_db_save_t changes.
169    Previous keys: 53394, 53395, 53396 */
170#define INV_DB_SAVE_KEY (53397)
171
172#define INV_DB_SAVE_MPL_KEY (50001)
173#define INV_DB_SAVE_ACCEL_MPL_KEY (50002)
174
175struct inv_db_save_t {
176    /** compass Bias in chip frame, hardware units scaled by 2^16. */
177    long compass_bias[3];
178    /** gyro factory bias in chip frame, hardware units scaled by 2^16,
179        +/- 2000 dps full scale. */
180    long factory_gyro_bias[3];
181    /** accel factory bias in chip frame, hardware units scaled by 2^16,
182        +/- 2 gee full scale. */
183    long factory_accel_bias[3];
184    /** temperature when factory_gyro_bias was stored. */
185    long gyro_temp;
186    /** flag to indicate temperature compensation that biases where stored. */
187    int gyro_bias_tc_set;
188    /** temperature when accel bias was stored. */
189    long accel_temp;
190    long gyro_temp_slope[3];
191    /** sensor accuracies */
192    int gyro_accuracy;
193    int accel_accuracy;
194    int compass_accuracy;
195};
196
197struct inv_db_save_mpl_t {
198    /** gyro bias in chip frame, hardware units scaled by 2^16, +/- 2000 dps
199        full scale */
200    long gyro_bias[3];
201};
202
203struct inv_db_save_accel_mpl_t {
204    /** accel bias in chip frame, hardware units scaled by 2^16, +/- 2 gee
205        full scale */
206    long accel_bias[3];
207};
208
209/** Maximum number of data callbacks that are supported. Safe to increase if needed.*/
210#define INV_MAX_DATA_CB 20
211
212#ifdef INV_PLAYBACK_DBG
213void inv_turn_on_data_logging(FILE *file);
214void inv_turn_off_data_logging();
215#endif
216
217void inv_set_gyro_orientation_and_scale(int orientation, long sensitivity);
218void inv_set_accel_orientation_and_scale(int orientation,
219        long sensitivity);
220void inv_set_compass_orientation_and_scale(int orientation,
221        long sensitivity);
222void inv_set_gyro_sample_rate(long sample_rate_us);
223void inv_set_compass_sample_rate(long sample_rate_us);
224void inv_set_quat_sample_rate(long sample_rate_us);
225void inv_set_accel_sample_rate(long sample_rate_us);
226void inv_set_gyro_bandwidth(int bandwidth_hz);
227void inv_set_accel_bandwidth(int bandwidth_hz);
228void inv_set_compass_bandwidth(int bandwidth_hz);
229
230void inv_get_gyro_sample_rate_ms(long *sample_rate_ms);
231void inv_get_accel_sample_rate_ms(long *sample_rate_ms);
232void inv_get_compass_sample_rate_ms(long *sample_rate_ms);
233
234inv_error_t inv_register_data_cb(inv_error_t (*func)
235                                 (struct inv_sensor_cal_t * data), int priority,
236                                 int sensor_type);
237inv_error_t inv_unregister_data_cb(inv_error_t (*func)
238                                   (struct inv_sensor_cal_t * data));
239
240inv_error_t inv_build_gyro(const short *gyro, inv_time_t timestamp);
241inv_error_t inv_build_compass(const long *compass, int status,
242                                  inv_time_t timestamp);
243inv_error_t inv_build_accel(const long *accel, int status,
244                            inv_time_t timestamp);
245inv_error_t inv_build_temp(const long temp, inv_time_t timestamp);
246inv_error_t inv_build_quat(const long *quat, int status, inv_time_t timestamp);
247inv_error_t inv_build_pressure(const long pressure, int status, inv_time_t timestamp);
248inv_error_t inv_execute_on_data(void);
249
250void inv_get_compass_bias(long *bias);
251
252void inv_set_compass_bias(const long *bias, int accuracy);
253void inv_set_compass_disturbance(int dist);
254void inv_set_gyro_bias(const long *bias);
255void inv_set_mpl_gyro_bias(const long *bias, int accuracy);
256void inv_set_accel_bias(const long *bias);
257void inv_set_mpl_accel_bias(const long *bias, int accuracy);
258void inv_set_accel_accuracy(int accuracy);
259void inv_set_accel_bias_mask(const long *bias, int accuracy, int mask);
260
261void inv_get_compass_soft_iron_matrix_d(long *matrix);
262void inv_set_compass_soft_iron_matrix_d(long *matrix);
263
264void inv_get_compass_soft_iron_matrix_f(float *matrix);
265void inv_set_compass_soft_iron_matrix_f(float *matrix);
266
267void inv_get_compass_soft_iron_output_data(long *data);
268void inv_get_compass_soft_iron_input_data(long *data);
269void inv_set_compass_soft_iron_input_data(const long *data);
270
271void inv_reset_compass_soft_iron_matrix(void);
272void inv_enable_compass_soft_iron_matrix(void);
273void inv_disable_compass_soft_iron_matrix(void);
274
275void inv_get_mpl_gyro_bias(long *bias, long *temp);
276void inv_get_gyro_bias(long *bias);
277void inv_get_gyro_bias_dmp_units(long *bias);
278int inv_get_factory_accel_bias_mask();
279void inv_get_mpl_accel_bias(long *bias, long *temp);
280void inv_get_accel_bias(long *bias);
281
282void inv_gyro_was_turned_off(void);
283void inv_accel_was_turned_off(void);
284void inv_compass_was_turned_off(void);
285void inv_quaternion_sensor_was_turned_off(void);
286inv_error_t inv_init_data_builder(void);
287long inv_get_gyro_sensitivity(void);
288long inv_get_accel_sensitivity(void);
289long inv_get_compass_sensitivity(void);
290
291void inv_get_accel_set(long *data, int8_t *accuracy, inv_time_t * timestamp);
292void inv_get_gyro_set(long *data, int8_t *accuracy, inv_time_t * timestamp);
293void inv_get_gyro_set_raw(long *data, int8_t *accuracy, inv_time_t * timestamp);
294void inv_get_compass_set(long *data, int8_t *accuracy, inv_time_t * timestamp);
295void inv_get_compass_set_raw(long *data, int8_t *accuracy, inv_time_t * timestamp);
296
297void inv_get_gyro(long *gyro);
298
299int inv_get_gyro_accuracy(void);
300int inv_get_accel_accuracy(void);
301int inv_get_mag_accuracy(void);
302void inv_get_raw_compass(short *raw);
303
304int inv_get_compass_on(void);
305int inv_get_gyro_on(void);
306int inv_get_accel_on(void);
307
308inv_time_t inv_get_last_timestamp(void);
309int inv_get_compass_disturbance(void);
310
311// new DMP cal functions
312inv_error_t inv_get_gyro_orient(int *orient);
313inv_error_t inv_get_accel_orient(int *orient);
314
315// internal
316int inv_get_gyro_bias_tc_set(void);
317
318#ifdef __cplusplus
319}
320#endif
321
322#endif  /* INV_DATA_BUILDER_H__ */
323