1/*
2 $License:
3    Copyright (C) 2011-2012 InvenSense Corporation, All Rights Reserved.
4    See included License.txt for License information.
5 $
6 */
7#include "mltypes.h"
8
9#ifndef INV_DATA_BUILDER_H__
10#define INV_DATA_BUILDER_H__
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16// Uncomment this flag to enable playback debug and record or playback scenarios
17//#define INV_PLAYBACK_DBG
18
19/** This is a new sample of accel data */
20#define INV_ACCEL_NEW 1
21/** This is a new sample of gyro data */
22#define INV_GYRO_NEW 2
23/** This is a new sample of compass data */
24#define INV_MAG_NEW 4
25/** This is a new sample of temperature data */
26#define INV_TEMP_NEW 8
27/** This is a new sample of quaternion data */
28#define INV_QUAT_NEW 16
29/** Set if quaternion is 6-axis from DMP */
30#define INV_QUAT_6AXIS 1024
31/** Set if quaternion is 3-axis from DMP */
32#define INV_QUAT_3AXIS 4096
33
34/** Set if the data is contiguous. Typically not set if a sample was skipped */
35#define INV_CONTIGUOUS 16
36/** Set if the calibrated data has been solved for */
37#define INV_CALIBRATED 32
38/* INV_NEW_DATA set for a new set of data, cleared if not available. */
39#define INV_NEW_DATA 64
40/* Set if raw data exists */
41#define INV_RAW_DATA 128
42/* Set if the sensor is on */
43#define INV_SENSOR_ON 256
44/* Set if quaternion has bias correction applied */
45#define INV_BIAS_APPLIED 512
46
47#define INV_PRIORITY_MOTION_NO_MOTION          100
48#define INV_PRIORITY_GYRO_TC                   150
49#define INV_PRIORITY_QUATERNION_GYRO_ACCEL     200
50#define INV_PRIORITY_QUATERNION_NO_GYRO        250
51#define INV_PRIORITY_MAGNETIC_DISTURBANCE      300
52#define INV_PRIORITY_HEADING_FROM_GYRO         350
53#define INV_PRIORITY_COMPASS_BIAS_W_GYRO       375
54#define INV_PRIORITY_COMPASS_VECTOR_CAL        400
55#define INV_PRIORITY_COMPASS_ADV_BIAS          500
56#define INV_PRIORITY_9_AXIS_FUSION             600
57#define INV_PRIORITY_QUATERNION_ADJUST_9_AXIS  700
58#define INV_PRIORITY_QUATERNION_ACCURACY       750
59#define INV_PRIORITY_RESULTS_HOLDER            800
60#define INV_PRIORITY_INUSE_AUTO_CALIBRATION    850
61#define INV_PRIORITY_HAL_OUTPUTS               900
62#define INV_PRIORITY_GLYPH                     950
63#define INV_PRIORITY_SHAKE                     975
64#define INV_PRIORITY_SM                        1000
65
66struct inv_single_sensor_t {
67    /** Orientation Descriptor. Describes how to go from the mounting frame to the body frame when
68    * the rotation matrix could be thought of only having elements of 0,1,-1.
69    * 2 bits are used to describe the column of the 1 or -1 and the 3rd bit is used for the sign.
70    * Bit 8 is sign of +/- 1 in third row. Bit 6-7 is column of +/-1 in third row.
71    * Bit 5 is sign of +/- 1 in second row. Bit 3-4 is column of +/-1 in second row.
72    * Bit 2 is sign of +/- 1 in first row. Bit 0-1 is column of +/-1 in first row.
73    */
74    int orientation;
75    /** The raw data in raw data units in the mounting frame */
76    short raw[3];
77    /** Raw data in body frame */
78    long raw_scaled[3];
79    /** Calibrated data */
80    long calibrated[3];
81    long sensitivity;
82    /** Sample rate in microseconds */
83    long sample_rate_us;
84    long sample_rate_ms;
85    /** INV_CONTIGUOUS is set for contiguous data. Will not be set if there was a sample
86    * skipped due to power savings turning off this sensor.
87    * INV_NEW_DATA set for a new set of data, cleared if not available.
88    * INV_CALIBRATED_SET if calibrated data has been solved for */
89    int status;
90    /** 0 to 3 for how well sensor data and biases are known. 3 is most accurate. */
91    int accuracy;
92    inv_time_t timestamp;
93    inv_time_t timestamp_prev;
94    /** Bandwidth in Hz */
95    int bandwidth;
96};
97struct inv_quat_sensor_t {
98    long raw[4];
99    /** INV_CONTIGUOUS is set for contiguous data. Will not be set if there was a sample
100    * skipped due to power savings turning off this sensor.
101    * INV_NEW_DATA set for a new set of data, cleared if not available.
102    * INV_CALIBRATED_SET if calibrated data has been solved for */
103    int status;
104    inv_time_t timestamp;
105    inv_time_t timestamp_prev;
106    long sample_rate_us;
107    long sample_rate_ms;
108};
109
110struct inv_sensor_cal_t {
111    struct inv_single_sensor_t gyro;
112    struct inv_single_sensor_t accel;
113    struct inv_single_sensor_t compass;
114    struct inv_single_sensor_t temp;
115    struct inv_quat_sensor_t quat;
116    /** Combinations of INV_GYRO_NEW, INV_ACCEL_NEW, INV_MAG_NEW to indicate
117    * which data is a new sample as these data points may have different sample rates.
118    */
119    int status;
120};
121
122// Useful for debug record and playback
123typedef enum {
124    RD_NO_DEBUG,
125    RD_RECORD,
126    RD_PLAYBACK
127} rd_dbg_mode;
128
129typedef enum {
130    PLAYBACK_DBG_TYPE_GYRO,
131    PLAYBACK_DBG_TYPE_ACCEL,
132    PLAYBACK_DBG_TYPE_COMPASS,
133    PLAYBACK_DBG_TYPE_TEMPERATURE,
134    PLAYBACK_DBG_TYPE_EXECUTE,
135    PLAYBACK_DBG_TYPE_A_ORIENT,
136    PLAYBACK_DBG_TYPE_G_ORIENT,
137    PLAYBACK_DBG_TYPE_C_ORIENT,
138    PLAYBACK_DBG_TYPE_A_SAMPLE_RATE,
139    PLAYBACK_DBG_TYPE_C_SAMPLE_RATE,
140    PLAYBACK_DBG_TYPE_G_SAMPLE_RATE,
141    PLAYBACK_DBG_TYPE_GYRO_OFF,
142    PLAYBACK_DBG_TYPE_ACCEL_OFF,
143    PLAYBACK_DBG_TYPE_COMPASS_OFF,
144    PLAYBACK_DBG_TYPE_Q_SAMPLE_RATE,
145    PLAYBACK_DBG_TYPE_QUAT
146
147} inv_rd_dbg_states;
148
149/** Maximum number of data callbacks that are supported. Safe to increase if needed.*/
150#define INV_MAX_DATA_CB 20
151
152#ifdef INV_PLAYBACK_DBG
153#include <stdio.h>
154void inv_turn_on_data_logging(FILE *file);
155void inv_turn_off_data_logging();
156#endif
157
158void inv_set_gyro_orientation_and_scale(int orientation, long sensitivity);
159void inv_set_accel_orientation_and_scale(int orientation,
160        long sensitivity);
161void inv_set_compass_orientation_and_scale(int orientation,
162        long sensitivity);
163void inv_set_gyro_sample_rate(long sample_rate_us);
164void inv_set_compass_sample_rate(long sample_rate_us);
165void inv_set_quat_sample_rate(long sample_rate_us);
166void inv_set_accel_sample_rate(long sample_rate_us);
167void inv_set_gyro_bandwidth(int bandwidth_hz);
168void inv_set_accel_bandwidth(int bandwidth_hz);
169void inv_set_compass_bandwidth(int bandwidth_hz);
170
171void inv_get_gyro_sample_rate_ms(long *sample_rate_ms);
172void inv_get_accel_sample_rate_ms(long *sample_rate_ms);
173void inv_get_compass_sample_rate_ms(long *sample_rate_ms);
174
175inv_error_t inv_register_data_cb(inv_error_t (*func)
176                                 (struct inv_sensor_cal_t * data), int priority,
177                                 int sensor_type);
178inv_error_t inv_unregister_data_cb(inv_error_t (*func)
179                                   (struct inv_sensor_cal_t * data));
180
181inv_error_t inv_build_gyro(const short *gyro, inv_time_t timestamp);
182inv_error_t inv_build_compass(const long *compass, int status,
183                                  inv_time_t timestamp);
184inv_error_t inv_build_accel(const long *accel, int status,
185                            inv_time_t timestamp);
186inv_error_t inv_build_temp(const long temp, inv_time_t timestamp);
187inv_error_t inv_build_quat(const long *quat, int status, inv_time_t timestamp);
188inv_error_t inv_execute_on_data(void);
189
190void inv_get_compass_bias(long *bias);
191
192void inv_set_compass_bias(const long *bias, int accuracy);
193void inv_set_compass_disturbance(int dist);
194void inv_set_gyro_bias(const long *bias, int accuracy);
195void inv_set_accel_bias(const long *bias, int accuracy);
196void inv_set_accel_accuracy(int accuracy);
197void inv_set_accel_bias_mask(const long *bias, int accuracy, int mask);
198
199void inv_get_gyro_bias(long *bias, long *temp);
200void inv_get_accel_bias(long *bias, long *temp);
201
202void inv_gyro_was_turned_off(void);
203void inv_accel_was_turned_off(void);
204void inv_compass_was_turned_off(void);
205void inv_quaternion_sensor_was_turned_off(void);
206inv_error_t inv_init_data_builder(void);
207long inv_get_gyro_sensitivity(void);
208long inv_get_accel_sensitivity(void);
209long inv_get_compass_sensitivity(void);
210
211void inv_get_accel_set(long *data, int8_t *accuracy, inv_time_t * timestamp);
212void inv_get_gyro_set(long *data, int8_t *accuracy, inv_time_t * timestamp);
213void inv_get_gyro_set_raw(long *data, int8_t *accuracy, inv_time_t * timestamp);
214void inv_get_compass_set(long *data, int8_t *accuracy, inv_time_t * timestamp);
215
216void inv_get_gyro(long *gyro);
217
218int inv_get_gyro_accuracy(void);
219int inv_get_accel_accuracy(void);
220int inv_get_mag_accuracy(void);
221
222int inv_get_compass_on(void);
223int inv_get_gyro_on(void);
224int inv_get_accel_on(void);
225
226inv_time_t inv_get_last_timestamp(void);
227int inv_get_compass_disturbance(void);
228
229//New DMP Cal Functions
230inv_error_t inv_get_gyro_orient(int *orient);
231inv_error_t inv_get_accel_orient(int *orient);
232
233// internal
234int inv_get_6_axis_gyro_accel_timestamp(long sample_rate_us, inv_time_t *ts);
235
236#ifdef __cplusplus
237}
238#endif
239
240#endif  /* INV_DATA_BUILDER_H__ */
241