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 14 extern "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 74 struct 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 106 struct 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 118 struct 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 127 struct 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 142 typedef enum { 143 RD_NO_DEBUG, 144 RD_RECORD, 145 RD_PLAYBACK 146 } rd_dbg_mode; 147 148 typedef 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 175 struct 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 197 struct 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 203 struct 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 213 void inv_turn_on_data_logging(FILE *file); 214 void inv_turn_off_data_logging(); 215 #endif 216 217 void inv_set_gyro_orientation_and_scale(int orientation, long sensitivity); 218 void inv_set_accel_orientation_and_scale(int orientation, 219 long sensitivity); 220 void inv_set_compass_orientation_and_scale(int orientation, 221 long sensitivity); 222 void inv_set_gyro_sample_rate(long sample_rate_us); 223 void inv_set_compass_sample_rate(long sample_rate_us); 224 void inv_set_quat_sample_rate(long sample_rate_us); 225 void inv_set_accel_sample_rate(long sample_rate_us); 226 void inv_set_gyro_bandwidth(int bandwidth_hz); 227 void inv_set_accel_bandwidth(int bandwidth_hz); 228 void inv_set_compass_bandwidth(int bandwidth_hz); 229 230 void inv_get_gyro_sample_rate_ms(long *sample_rate_ms); 231 void inv_get_accel_sample_rate_ms(long *sample_rate_ms); 232 void inv_get_compass_sample_rate_ms(long *sample_rate_ms); 233 234 inv_error_t inv_register_data_cb(inv_error_t (*func) 235 (struct inv_sensor_cal_t * data), int priority, 236 int sensor_type); 237 inv_error_t inv_unregister_data_cb(inv_error_t (*func) 238 (struct inv_sensor_cal_t * data)); 239 240 inv_error_t inv_build_gyro(const short *gyro, inv_time_t timestamp); 241 inv_error_t inv_build_compass(const long *compass, int status, 242 inv_time_t timestamp); 243 inv_error_t inv_build_accel(const long *accel, int status, 244 inv_time_t timestamp); 245 inv_error_t inv_build_temp(const long temp, inv_time_t timestamp); 246 inv_error_t inv_build_quat(const long *quat, int status, inv_time_t timestamp); 247 inv_error_t inv_build_pressure(const long pressure, int status, inv_time_t timestamp); 248 inv_error_t inv_execute_on_data(void); 249 250 void inv_get_compass_bias(long *bias); 251 252 void inv_set_compass_bias(const long *bias, int accuracy); 253 void inv_set_compass_disturbance(int dist); 254 void inv_set_gyro_bias(const long *bias); 255 void inv_set_mpl_gyro_bias(const long *bias, int accuracy); 256 void inv_set_accel_bias(const long *bias); 257 void inv_set_mpl_accel_bias(const long *bias, int accuracy); 258 void inv_set_accel_accuracy(int accuracy); 259 void inv_set_accel_bias_mask(const long *bias, int accuracy, int mask); 260 261 void inv_get_compass_soft_iron_matrix_d(long *matrix); 262 void inv_set_compass_soft_iron_matrix_d(long *matrix); 263 264 void inv_get_compass_soft_iron_matrix_f(float *matrix); 265 void inv_set_compass_soft_iron_matrix_f(float *matrix); 266 267 void inv_get_compass_soft_iron_output_data(long *data); 268 void inv_get_compass_soft_iron_input_data(long *data); 269 void inv_set_compass_soft_iron_input_data(const long *data); 270 271 void inv_reset_compass_soft_iron_matrix(void); 272 void inv_enable_compass_soft_iron_matrix(void); 273 void inv_disable_compass_soft_iron_matrix(void); 274 275 void inv_get_mpl_gyro_bias(long *bias, long *temp); 276 void inv_get_gyro_bias(long *bias); 277 void inv_get_gyro_bias_dmp_units(long *bias); 278 int inv_get_factory_accel_bias_mask(); 279 void inv_get_mpl_accel_bias(long *bias, long *temp); 280 void inv_get_accel_bias(long *bias); 281 282 void inv_gyro_was_turned_off(void); 283 void inv_accel_was_turned_off(void); 284 void inv_compass_was_turned_off(void); 285 void inv_quaternion_sensor_was_turned_off(void); 286 inv_error_t inv_init_data_builder(void); 287 long inv_get_gyro_sensitivity(void); 288 long inv_get_accel_sensitivity(void); 289 long inv_get_compass_sensitivity(void); 290 291 void inv_get_accel_set(long *data, int8_t *accuracy, inv_time_t * timestamp); 292 void inv_get_gyro_set(long *data, int8_t *accuracy, inv_time_t * timestamp); 293 void inv_get_gyro_set_raw(long *data, int8_t *accuracy, inv_time_t * timestamp); 294 void inv_get_compass_set(long *data, int8_t *accuracy, inv_time_t * timestamp); 295 void inv_get_compass_set_raw(long *data, int8_t *accuracy, inv_time_t * timestamp); 296 297 void inv_get_gyro(long *gyro); 298 299 int inv_get_gyro_accuracy(void); 300 int inv_get_accel_accuracy(void); 301 int inv_get_mag_accuracy(void); 302 void inv_get_raw_compass(short *raw); 303 304 int inv_get_compass_on(void); 305 int inv_get_gyro_on(void); 306 int inv_get_accel_on(void); 307 308 inv_time_t inv_get_last_timestamp(void); 309 int inv_get_compass_disturbance(void); 310 311 // new DMP cal functions 312 inv_error_t inv_get_gyro_orient(int *orient); 313 inv_error_t inv_get_accel_orient(int *orient); 314 315 // internal 316 int inv_get_gyro_bias_tc_set(void); 317 318 #ifdef __cplusplus 319 } 320 #endif 321 322 #endif /* INV_DATA_BUILDER_H__ */ 323