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