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