ECE 4760 Final Project
 All Files Functions Variables Enumerations Enumerator Macros
imu.h
Go to the documentation of this file.
1 /*
2 from https://github.com/sparkfun/LSM9DS0_Breakout/blob/master/Libraries/Arduino/SFE_LSM9DS0/SFE_LSM9DS0.h
3 
4 This code is beerware; if you see me (or any other SparkFun employee) at the
5 local, and you've found our code helpful, please buy us a round!
6 Distributed as-is; no warranty is given.
7 ******************************************************************************/
8 
9 #ifndef _IMU_H_
10 #define _IMU_H_
11 
12 #include <stdio.h>
13 /**
14  * @brief Register to address mapping for the Gyroscope
15  */
17  WHO_AM_I_G=0x0F,
25  OUT_X_L_G=0x28,
26  OUT_X_H_G=0x29,
27  OUT_Y_L_G=0x2A,
28  OUT_Y_H_G=0x2B,
29  OUT_Z_L_G=0x2C,
30  OUT_Z_H_G=0x2D,
33  INT1_CFG_G=0x30,
34  INT1_SRC_G=0x31,
42 };
43 
44 
45 /**
46  * @brief Register to address mapping for the accelometer / magnemeter
47  */
48 enum XM_CONTROL {
52  OUT_X_L_M=0x08,
53  OUT_X_H_M=0x09,
54  OUT_Y_L_M=0x0A,
55  OUT_Y_H_M=0x0B,
56  OUT_Z_L_M=0x0C,
57  OUT_Z_H_M=0x0D,
81  OUT_X_L_A=0x28,
82  OUT_X_H_A=0x29,
83  OUT_Y_L_A=0x2A,
84  OUT_Y_H_A=0x2B,
85  OUT_Z_L_A=0x2C,
86  OUT_Z_H_A=0x2D,
97  CLICK_CFG=0x38,
98  CLICK_SRC=0x39,
99  CLICK_THS=0x3A,
103  ACT_THS=0x3E,
104  ACT_DUR=0x3F,
105 };
106 
107 // gyro_scale defines the possible full-scale ranges of the gyroscope:
109 {
110  G_SCALE_245DPS, // 00: 245 degrees per second
111  G_SCALE_500DPS, // 01: 500 dps
112  G_SCALE_2000DPS, // 10: 2000 dps
113 };
114 // accel_scale defines all possible FSR's of the accelerometer:
116 {
117  A_SCALE_2G, // 000: 2g
118  A_SCALE_4G, // 001: 4g
119  A_SCALE_6G, // 010: 6g
120  A_SCALE_8G, // 011: 8g
121  A_SCALE_16G // 100: 16g
122 };
123 // mag_scale defines all possible FSR's of the magnetometer:
125 {
126  M_SCALE_2GS, // 00: 2Gs
127  M_SCALE_4GS, // 01: 4Gs
128  M_SCALE_8GS, // 10: 8Gs
129  M_SCALE_12GS, // 11: 12Gs
130 };
131 // gyro_odr defines all possible data rate/bandwidth combos of the gyro:
133 { // ODR (Hz) --- Cutoff
134  G_ODR_95_BW_125 = 0x0, // 95 12.5
135  G_ODR_95_BW_25 = 0x1, // 95 25
136  // 0x2 and 0x3 define the same data rate and bandwidth
137  G_ODR_190_BW_125 = 0x4, // 190 12.5
138  G_ODR_190_BW_25 = 0x5, // 190 25
139  G_ODR_190_BW_50 = 0x6, // 190 50
140  G_ODR_190_BW_70 = 0x7, // 190 70
141  G_ODR_380_BW_20 = 0x8, // 380 20
142  G_ODR_380_BW_25 = 0x9, // 380 25
143  G_ODR_380_BW_50 = 0xA, // 380 50
144  G_ODR_380_BW_100 = 0xB, // 380 100
145  G_ODR_760_BW_30 = 0xC, // 760 30
146  G_ODR_760_BW_35 = 0xD, // 760 35
147  G_ODR_760_BW_50 = 0xE, // 760 50
148  G_ODR_760_BW_100 = 0xF, // 760 100
149 };
150 // accel_oder defines all possible output data rates of the accelerometer:
152 {
153  A_POWER_DOWN, // Power-down mode (0x0)
154  A_ODR_3125, // 3.125 Hz (0x1)
155  A_ODR_625, // 6.25 Hz (0x2)
156  A_ODR_125, // 12.5 Hz (0x3)
157  A_ODR_25, // 25 Hz (0x4)
158  A_ODR_50, // 50 Hz (0x5)
159  A_ODR_100, // 100 Hz (0x6)
160  A_ODR_200, // 200 Hz (0x7)
161  A_ODR_400, // 400 Hz (0x8)
162  A_ODR_800, // 800 Hz (9)
163  A_ODR_1600 // 1600 Hz (0xA)
164 };
165 
166 // accel_abw defines all possible anti-aliasing filter rates of the accelerometer:
168 {
169  A_ABW_773, // 773 Hz (0x0)
170  A_ABW_194, // 194 Hz (0x1)
171  A_ABW_362, // 362 Hz (0x2)
172  A_ABW_50, // 50 Hz (0x3)
173 };
174 
175 
176 // mag_oder defines all possible output data rates of the magnetometer:
178 {
179  M_ODR_3125, // 3.125 Hz (0x00)
180  M_ODR_625, // 6.25 Hz (0x01)
181  M_ODR_125, // 12.5 Hz (0x02)
182  M_ODR_25, // 25 Hz (0x03)
183  M_ODR_50, // 50 (0x04)
184  M_ODR_100, // 100 Hz (0x05)
185 };
186 
187 
191 
192 /**
193  * @brief sets the aRes variable to the correct resolution of the accelometer readings
194  */
195 void calcaRes();
196 /**
197  * @brief sets the mRes variable to the correct resolution of the magenmeter readings
198  */
199 void calcmRes();
200 /**
201  * @brief sets the gRes variable to the correct resolution of the gyroscope readings
202  */
203 void calcgRes();
204 /**
205  * @brief initialize the gyroscope, must be done before reading from the gyroscope
206  */
207 void initGyro();
208 
209 /**
210  * @brief set the data capture rate of the gyroscope see the enum gyro_odr to see what values this can take.
211  *
212  * @param gRate
213  */
214 void setGyroODR(enum gyro_odr gRate);
215 /**
216  * @brief set the scale of the gyroscope see enum gyro_Scale to see what values this can take.
217  *
218  * @param gScl
219  */
220 void setGyroScale(enum gyro_scale gScl);
221 
222 /**
223  * @brief initilize the accelometer, this must be called before using any accelometer fucntions
224  */
225 void initAccel(void);
226 /**
227  * @brief see setGyroODR function
228  *
229  * @param aRate
230  */
231 void setAccelODR(enum accel_odr aRate);
232 /**
233  * @brief see the setGyroScle function
234  *
235  * @param aScl
236  */
237 void setAccelScale(enum accel_scale aScl);
238 
239 /**
240  * @brief
241  */
242 void initMag(void);
243 /**
244  * @brief see the setGyroODR function
245  *
246  * @param mRate
247  */
248 void setMagODR(enum mag_odr mRate);
249 /**
250  * @brief see the setGyroScale function
251  *
252  * @param mScl
253  */
254 void setMagScale(enum mag_scale mScl);
255 
256 /**
257  * @brief takes a raw gyro reading and returns the actual value
258  *
259  * @param gyro raw reading form gyroscope
260  *
261  * @return The adjusted value
262  */
263 float calcGyro(int16_t gyro);
264 /**
265  * @brief
266  *
267  * @param accel
268  *
269  * @return The adjusted value
270  */
271 float calcAccel(int16_t accel);
272 /**
273  * @brief
274  *
275  * @param mag
276  *
277  * @return The adjusted value
278  *
279  */
280 float calcMag(int16_t mag);
281 
282 
283 /**
284  * @brief read multiple bytes from the imu
285  *
286  * @param address address of the twi device to read from
287  * @param subAddress register to read from
288  * @param dest destination buffer must be of size >= count
289  * @param count Number of bytes to read
290  */
291 void imu_rbs(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count);
292 /**
293  * @brief Read one byte from the device
294  *
295  * @param address i2c device address
296  * @param subAddress Address of desired register
297  *
298  * @return The byte read from the device
299  */
300 uint8_t imu_rb(uint8_t address, uint8_t subAddress);
301 /**
302  * @brief Writes a byte to the register at the address specified by subAddress
303  *
304  * @param address i2c/twi address of the accelometer / gyroscope
305  * @param subAddress The address of the register that will be written to
306  * @param data The singular data byte that is to be written to the register specified
307  */
308 void imu_wb(uint8_t address, uint8_t subAddress, uint8_t data);
309 
310 float aRes;
311 float gRes;
312 float mRes;
313 
315  LSM9DS0_XM=(0x1D<<1),
316  LSM9DS0_G=(0x6B<<1)
317 };
318 
319 
320 /**
321  * @brief Initilizes the IMU and enables all 9 axes and set the respective scales and data rates
322  *
323  * @param gScl Gyro Scale
324  * @param a_scl Accelometer Scale
325  * @param m_scl Magnemeter Scale
326  * @param g_odr Gyro data rate
327  * @param a_odr accelometer data rate
328  * @param m_odr magnemeter data rate
329  *
330  */
331 uint16_t
332 init_imu(enum gyro_scale gScl, enum accel_scale a_scl,enum mag_scale m_scl,
333  enum gyro_odr g_odr, enum accel_odr a_odr,enum mag_odr m_odr);
334 
335 /**
336  * @brief Reads the gyroscope and calculates the real value from the raw reading.
337  *
338  * @param x x-axis
339  * @param y y-axis
340  * @param z z-axis
341  */
342 void read_gyro(float * x, float * y, float * z);
343 /**
344  * @brief Reads the accelometer and calculates the real value from the raw reading.
345  *
346  * @param x x-axis
347  * @param y y-axis
348  * @param z z-axis
349  */
350 void read_accel(float * x, float * y, float * z);
351 /**
352  * @brief Reads the magnemeter and calculates the real value from the raw reading.
353  *
354  * @param x x-axis
355  * @param y y-axis
356  * @param z z-axis
357  */
358 void read_mag(float * x, float * y, float * z);
359 
360 
361 /**
362  * @brief Returns the temperature in Celcius. Will be 25 degrees, which is the optimal temparture for the gyroscope. Is used internally for self-calibration.
363 
364  *
365  * @return The temperature in celcius,*/
366 int16_t readTemp();
367 #endif
GYRO_CONTROL
Register to address mapping for the Gyroscope.
Definition: imu.h:16
float calcMag(int16_t mag)
Definition: imu.c:271
float gRes
Definition: imu.h:311
void read_mag(float *x, float *y, float *z)
Reads the magnemeter and calculates the real value from the raw reading.
Definition: imu.c:249
void read_accel(float *x, float *y, float *z)
Reads the accelometer and calculates the real value from the raw reading.
Definition: imu.c:241
accel_odr
Definition: imu.h:151
Definition: imu.h:162
void setAccelODR(enum accel_odr aRate)
see setGyroODR function
Definition: imu.c:343
Definition: imu.h:17
Definition: imu.h:34
Definition: imu.h:29
void initAccel(void)
initilize the accelometer, this must be called before using any accelometer fucntions ...
Definition: imu.c:139
Definition: imu.h:184
Definition: imu.h:97
Definition: imu.h:157
gyro_odr
Definition: imu.h:132
Definition: imu.h:56
XM_CONTROL
Register to address mapping for the accelometer / magnemeter.
Definition: imu.h:48
Definition: imu.h:316
Definition: imu.h:85
Definition: imu.h:25
Definition: imu.h:84
float mRes
Definition: imu.h:312
enum gyro_scale gScale
Definition: imu.h:188
Definition: imu.h:159
void read_gyro(float *x, float *y, float *z)
Reads the gyroscope and calculates the real value from the raw reading.
Definition: imu.c:233
uint16_t init_imu(enum gyro_scale gScl, enum accel_scale a_scl, enum mag_scale m_scl, enum gyro_odr g_odr, enum accel_odr a_odr, enum mag_odr m_odr)
Initilizes the IMU and enables all 9 axes and set the respective scales and data rates.
Definition: imu.c:32
Definition: imu.h:54
Definition: imu.h:57
mag_scale
Definition: imu.h:124
Definition: imu.h:156
Definition: imu.h:183
Definition: imu.h:30
Definition: imu.h:86
Definition: imu.h:28
mag_odr
Definition: imu.h:177
Definition: imu.h:169
Definition: imu.h:104
enum accel_scale aScale
Definition: imu.h:189
Definition: imu.h:27
enum mag_scale mScale
Definition: imu.h:190
void setMagODR(enum mag_odr mRate)
see the setGyroODR function
Definition: imu.c:366
i2c_address
Definition: imu.h:314
void calcmRes()
sets the mRes variable to the correct resolution of the magenmeter readings
Definition: imu.c:406
void setMagScale(enum mag_scale mScl)
see the setGyroScale function
Definition: imu.c:314
Definition: imu.h:52
void imu_rbs(uint8_t address, uint8_t subAddress, uint8_t *dest, uint8_t count)
read multiple bytes from the imu
Definition: imu.c:437
gyro_scale
Definition: imu.h:108
Definition: imu.h:55
Definition: imu.h:182
void calcaRes()
sets the aRes variable to the correct resolution of the accelometer readings
Definition: imu.c:397
Definition: imu.h:171
Definition: imu.h:53
uint8_t imu_rb(uint8_t address, uint8_t subAddress)
Read one byte from the device.
Definition: imu.c:426
Definition: imu.h:103
Definition: imu.h:155
accel_scale
Definition: imu.h:115
void initGyro()
initialize the gyroscope, must be done before reading from the gyroscope
Definition: imu.c:75
Definition: imu.h:26
Definition: imu.h:181
Definition: imu.h:170
float calcGyro(int16_t gyro)
takes a raw gyro reading and returns the actual value
Definition: imu.c:259
Definition: imu.h:83
Definition: imu.h:158
void setAccelScale(enum accel_scale aScl)
see the setGyroScle function
Definition: imu.c:295
float calcAccel(int16_t accel)
Definition: imu.c:265
int16_t readTemp()
Returns the temperature in Celcius. Will be 25 degrees, which is the optimal temparture for the gyros...
Definition: imu.c:24
Definition: imu.h:81
Definition: imu.h:99
float aRes
Definition: imu.h:310
void setGyroScale(enum gyro_scale gScl)
set the scale of the gyroscope see enum gyro_Scale to see what values this can take.
Definition: imu.c:277
accel_abw
Definition: imu.h:167
Definition: imu.h:172
void imu_wb(uint8_t address, uint8_t subAddress, uint8_t data)
Writes a byte to the register at the address specified by subAddress.
Definition: imu.c:416
void initMag(void)
Definition: imu.c:152
Definition: imu.h:98
Definition: imu.h:82
Definition: imu.h:180
void calcgRes()
sets the gRes variable to the correct resolution of the gyroscope readings
Definition: imu.c:378
Definition: imu.h:33
Definition: imu.h:161
Definition: imu.h:160
void setGyroODR(enum gyro_odr gRate)
set the data capture rate of the gyroscope see the enum gyro_odr to see what values this can take...
Definition: imu.c:332