ECE 4760 Final Project
 All Files Functions Variables Enumerations Enumerator Macros
Enumerations | Functions | Variables
imu.h File Reference
#include <stdio.h>

Go to the source code of this file.

Enumerations

enum  GYRO_CONTROL {
  WHO_AM_I_G =0x0F, CTRL_REG1_G =0x20, CTRL_REG2_G =0x21, CTRL_REG3_G =0x22,
  CTRL_REG4_G =0x23, CTRL_REG5_G =0x24, REFERENCE_G =0x25, STATUS_REG_G =0x27,
  OUT_X_L_G =0x28, OUT_X_H_G =0x29, OUT_Y_L_G =0x2A, OUT_Y_H_G =0x2B,
  OUT_Z_L_G =0x2C, OUT_Z_H_G =0x2D, FIFO_CTRL_REG_G =0x2E, FIFO_SRC_REG_G =0x2F,
  INT1_CFG_G =0x30, INT1_SRC_G =0x31, INT1_THS_XH_G =0x32, INT1_THS_XL_G =0x33,
  INT1_THS_YH_G =0x34, INT1_THS_YL_G =0x35, INT1_THS_ZH_G =0x36, INT1_THS_ZL_G =0x37,
  INT1_DURATION_G =0x38
}
 Register to address mapping for the Gyroscope. More...
 
enum  XM_CONTROL {
  OUT_TEMP_L_XM =0x05, OUT_TEMP_H_XM =0x06, STATUS_REG_M =0x07, OUT_X_L_M =0x08,
  OUT_X_H_M =0x09, OUT_Y_L_M =0x0A, OUT_Y_H_M =0x0B, OUT_Z_L_M =0x0C,
  OUT_Z_H_M =0x0D, WHO_AM_I_XM =0x0F, INT_CTRL_REG_M =0x12, INT_SRC_REG_M =0x13,
  INT_THS_L_M =0x14, INT_THS_H_M =0x15, OFFSET_X_L_M =0x16, OFFSET_X_H_M =0x17,
  OFFSET_Y_L_M =0x18, OFFSET_Y_H_M =0x19, OFFSET_Z_L_M =0x1A, OFFSET_Z_H_M =0x1B,
  REFERENCE_X =0x1C, REFERENCE_Y =0x1D, REFERENCE_Z =0x1E, CTRL_REG0_XM =0x1F,
  CTRL_REG1_XM =0x20, CTRL_REG2_XM =0x21, CTRL_REG3_XM =0x22, CTRL_REG4_XM =0x23,
  CTRL_REG5_XM =0x24, CTRL_REG6_XM =0x25, CTRL_REG7_XM =0x26, STATUS_REG_A =0x27,
  OUT_X_L_A =0x28, OUT_X_H_A =0x29, OUT_Y_L_A =0x2A, OUT_Y_H_A =0x2B,
  OUT_Z_L_A =0x2C, OUT_Z_H_A =0x2D, FIFO_CTRL_REG =0x2E, FIFO_SRC_REG =0x2F,
  INT_GEN_1_REG =0x30, INT_GEN_1_SRC =0x31, INT_GEN_1_THS =0x32, INT_GEN_1_DURATION =0x33,
  INT_GEN_2_REG =0x34, INT_GEN_2_SRC =0x35, INT_GEN_2_THS =0x36, INT_GEN_2_DURATION =0x37,
  CLICK_CFG =0x38, CLICK_SRC =0x39, CLICK_THS =0x3A, TIME_LIMIT =0x3B,
  TIME_LATENCY =0x3C, TIME_WINDOW =0x3D, ACT_THS =0x3E, ACT_DUR =0x3F
}
 Register to address mapping for the accelometer / magnemeter. More...
 
enum  gyro_scale { G_SCALE_245DPS, G_SCALE_500DPS, G_SCALE_2000DPS }
 
enum  accel_scale {
  A_SCALE_2G, A_SCALE_4G, A_SCALE_6G, A_SCALE_8G,
  A_SCALE_16G
}
 
enum  mag_scale { M_SCALE_2GS, M_SCALE_4GS, M_SCALE_8GS, M_SCALE_12GS }
 
enum  gyro_odr {
  G_ODR_95_BW_125 = 0x0, G_ODR_95_BW_25 = 0x1, G_ODR_190_BW_125 = 0x4, G_ODR_190_BW_25 = 0x5,
  G_ODR_190_BW_50 = 0x6, G_ODR_190_BW_70 = 0x7, G_ODR_380_BW_20 = 0x8, G_ODR_380_BW_25 = 0x9,
  G_ODR_380_BW_50 = 0xA, G_ODR_380_BW_100 = 0xB, G_ODR_760_BW_30 = 0xC, G_ODR_760_BW_35 = 0xD,
  G_ODR_760_BW_50 = 0xE, G_ODR_760_BW_100 = 0xF
}
 
enum  accel_odr {
  A_POWER_DOWN, A_ODR_3125, A_ODR_625, A_ODR_125,
  A_ODR_25, A_ODR_50, A_ODR_100, A_ODR_200,
  A_ODR_400, A_ODR_800, A_ODR_1600
}
 
enum  accel_abw { A_ABW_773, A_ABW_194, A_ABW_362, A_ABW_50 }
 
enum  mag_odr {
  M_ODR_3125, M_ODR_625, M_ODR_125, M_ODR_25,
  M_ODR_50, M_ODR_100
}
 
enum  i2c_address { LSM9DS0_XM =(0x1D<<1), LSM9DS0_G =(0x6B<<1) }
 

Functions

void calcaRes ()
 sets the aRes variable to the correct resolution of the accelometer readings More...
 
void calcmRes ()
 sets the mRes variable to the correct resolution of the magenmeter readings More...
 
void calcgRes ()
 sets the gRes variable to the correct resolution of the gyroscope readings More...
 
void initGyro ()
 initialize the gyroscope, must be done before reading from the gyroscope More...
 
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. More...
 
void setGyroScale (enum gyro_scale gScl)
 set the scale of the gyroscope see enum gyro_Scale to see what values this can take. More...
 
void initAccel (void)
 initilize the accelometer, this must be called before using any accelometer fucntions More...
 
void setAccelODR (enum accel_odr aRate)
 see setGyroODR function More...
 
void setAccelScale (enum accel_scale aScl)
 see the setGyroScle function More...
 
void initMag (void)
 
void setMagODR (enum mag_odr mRate)
 see the setGyroODR function More...
 
void setMagScale (enum mag_scale mScl)
 see the setGyroScale function More...
 
float calcGyro (int16_t gyro)
 takes a raw gyro reading and returns the actual value More...
 
float calcAccel (int16_t accel)
 
float calcMag (int16_t mag)
 
void imu_rbs (uint8_t address, uint8_t subAddress, uint8_t *dest, uint8_t count)
 read multiple bytes from the imu More...
 
uint8_t imu_rb (uint8_t address, uint8_t subAddress)
 Read one byte from the device. More...
 
void imu_wb (uint8_t address, uint8_t subAddress, uint8_t data)
 Writes a byte to the register at the address specified by subAddress. More...
 
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. More...
 
void read_gyro (float *x, float *y, float *z)
 Reads the gyroscope and calculates the real value from the raw reading. More...
 
void read_accel (float *x, float *y, float *z)
 Reads the accelometer and calculates the real value from the raw reading. More...
 
void read_mag (float *x, float *y, float *z)
 Reads the magnemeter and calculates the real value from the raw reading. More...
 
int16_t readTemp ()
 Returns the temperature in Celcius. Will be 25 degrees, which is the optimal temparture for the gyroscope. Is used internally for self-calibration. More...
 

Variables

enum gyro_scale gScale
 
enum accel_scale aScale
 
enum mag_scale mScale
 
float aRes
 
float gRes
 
float mRes
 

Enumeration Type Documentation

enum accel_abw
Enumerator
A_ABW_773 
A_ABW_194 
A_ABW_362 
A_ABW_50 

Definition at line 167 of file imu.h.

enum accel_odr
Enumerator
A_POWER_DOWN 
A_ODR_3125 
A_ODR_625 
A_ODR_125 
A_ODR_25 
A_ODR_50 
A_ODR_100 
A_ODR_200 
A_ODR_400 
A_ODR_800 
A_ODR_1600 

Definition at line 151 of file imu.h.

Enumerator
A_SCALE_2G 
A_SCALE_4G 
A_SCALE_6G 
A_SCALE_8G 
A_SCALE_16G 

Definition at line 115 of file imu.h.

Register to address mapping for the Gyroscope.

Enumerator
WHO_AM_I_G 
CTRL_REG1_G 
CTRL_REG2_G 
CTRL_REG3_G 
CTRL_REG4_G 
CTRL_REG5_G 
REFERENCE_G 
STATUS_REG_G 
OUT_X_L_G 
OUT_X_H_G 
OUT_Y_L_G 
OUT_Y_H_G 
OUT_Z_L_G 
OUT_Z_H_G 
FIFO_CTRL_REG_G 
FIFO_SRC_REG_G 
INT1_CFG_G 
INT1_SRC_G 
INT1_THS_XH_G 
INT1_THS_XL_G 
INT1_THS_YH_G 
INT1_THS_YL_G 
INT1_THS_ZH_G 
INT1_THS_ZL_G 
INT1_DURATION_G 

Definition at line 16 of file imu.h.

enum gyro_odr
Enumerator
G_ODR_95_BW_125 
G_ODR_95_BW_25 
G_ODR_190_BW_125 
G_ODR_190_BW_25 
G_ODR_190_BW_50 
G_ODR_190_BW_70 
G_ODR_380_BW_20 
G_ODR_380_BW_25 
G_ODR_380_BW_50 
G_ODR_380_BW_100 
G_ODR_760_BW_30 
G_ODR_760_BW_35 
G_ODR_760_BW_50 
G_ODR_760_BW_100 

Definition at line 132 of file imu.h.

enum gyro_scale
Enumerator
G_SCALE_245DPS 
G_SCALE_500DPS 
G_SCALE_2000DPS 

Definition at line 108 of file imu.h.

Enumerator
LSM9DS0_XM 
LSM9DS0_G 

Definition at line 314 of file imu.h.

enum mag_odr
Enumerator
M_ODR_3125 
M_ODR_625 
M_ODR_125 
M_ODR_25 
M_ODR_50 
M_ODR_100 

Definition at line 177 of file imu.h.

enum mag_scale
Enumerator
M_SCALE_2GS 
M_SCALE_4GS 
M_SCALE_8GS 
M_SCALE_12GS 

Definition at line 124 of file imu.h.

enum XM_CONTROL

Register to address mapping for the accelometer / magnemeter.

Enumerator
OUT_TEMP_L_XM 
OUT_TEMP_H_XM 
STATUS_REG_M 
OUT_X_L_M 
OUT_X_H_M 
OUT_Y_L_M 
OUT_Y_H_M 
OUT_Z_L_M 
OUT_Z_H_M 
WHO_AM_I_XM 
INT_CTRL_REG_M 
INT_SRC_REG_M 
INT_THS_L_M 
INT_THS_H_M 
OFFSET_X_L_M 
OFFSET_X_H_M 
OFFSET_Y_L_M 
OFFSET_Y_H_M 
OFFSET_Z_L_M 
OFFSET_Z_H_M 
REFERENCE_X 
REFERENCE_Y 
REFERENCE_Z 
CTRL_REG0_XM 
CTRL_REG1_XM 
CTRL_REG2_XM 
CTRL_REG3_XM 
CTRL_REG4_XM 
CTRL_REG5_XM 
CTRL_REG6_XM 
CTRL_REG7_XM 
STATUS_REG_A 
OUT_X_L_A 
OUT_X_H_A 
OUT_Y_L_A 
OUT_Y_H_A 
OUT_Z_L_A 
OUT_Z_H_A 
FIFO_CTRL_REG 
FIFO_SRC_REG 
INT_GEN_1_REG 
INT_GEN_1_SRC 
INT_GEN_1_THS 
INT_GEN_1_DURATION 
INT_GEN_2_REG 
INT_GEN_2_SRC 
INT_GEN_2_THS 
INT_GEN_2_DURATION 
CLICK_CFG 
CLICK_SRC 
CLICK_THS 
TIME_LIMIT 
TIME_LATENCY 
TIME_WINDOW 
ACT_THS 
ACT_DUR 

Definition at line 48 of file imu.h.

Function Documentation

float calcAccel ( int16_t  accel)
Parameters
accel
Returns
The adjusted value

Definition at line 265 of file imu.c.

void calcaRes ( )

sets the aRes variable to the correct resolution of the accelometer readings

Definition at line 397 of file imu.c.

void calcgRes ( )

sets the gRes variable to the correct resolution of the gyroscope readings

Definition at line 378 of file imu.c.

float calcGyro ( int16_t  gyro)

takes a raw gyro reading and returns the actual value

Parameters
gyroraw reading form gyroscope
Returns
The adjusted value

Definition at line 259 of file imu.c.

float calcMag ( int16_t  mag)
Parameters
mag
Returns
The adjusted value

Definition at line 271 of file imu.c.

void calcmRes ( )

sets the mRes variable to the correct resolution of the magenmeter readings

Definition at line 406 of file imu.c.

uint8_t imu_rb ( uint8_t  address,
uint8_t  subAddress 
)

Read one byte from the device.

Parameters
addressi2c device address
subAddressAddress of desired register
Returns
The byte read from the device

Definition at line 426 of file imu.c.

void imu_rbs ( uint8_t  address,
uint8_t  subAddress,
uint8_t *  dest,
uint8_t  count 
)

read multiple bytes from the imu

Parameters
addressaddress of the twi device to read from
subAddressregister to read from
destdestination buffer must be of size >= count
countNumber of bytes to read

Definition at line 437 of file imu.c.

void imu_wb ( uint8_t  address,
uint8_t  subAddress,
uint8_t  data 
)

Writes a byte to the register at the address specified by subAddress.

Parameters
addressi2c/twi address of the accelometer / gyroscope
subAddressThe address of the register that will be written to
dataThe singular data byte that is to be written to the register specified

Definition at line 416 of file imu.c.

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.

Parameters
gSclGyro Scale
a_sclAccelometer Scale
m_sclMagnemeter Scale
g_odrGyro data rate
a_odraccelometer data rate
m_odrmagnemeter data rate

Definition at line 32 of file imu.c.

void initAccel ( void  )

initilize the accelometer, this must be called before using any accelometer fucntions

Definition at line 139 of file imu.c.

void initGyro ( )

initialize the gyroscope, must be done before reading from the gyroscope

Definition at line 75 of file imu.c.

void initMag ( void  )

Definition at line 152 of file imu.c.

void read_accel ( float *  x,
float *  y,
float *  z 
)

Reads the accelometer and calculates the real value from the raw reading.

Parameters
xx-axis
yy-axis
zz-axis

Definition at line 241 of file imu.c.

void read_gyro ( float *  x,
float *  y,
float *  z 
)

Reads the gyroscope and calculates the real value from the raw reading.

Parameters
xx-axis
yy-axis
zz-axis

Definition at line 233 of file imu.c.

void read_mag ( float *  x,
float *  y,
float *  z 
)

Reads the magnemeter and calculates the real value from the raw reading.

Parameters
xx-axis
yy-axis
zz-axis

Definition at line 249 of file imu.c.

int16_t readTemp ( )

Returns the temperature in Celcius. Will be 25 degrees, which is the optimal temparture for the gyroscope. Is used internally for self-calibration.

Returns
The temperature in celcius,

Definition at line 24 of file imu.c.

void setAccelODR ( enum accel_odr  aRate)

see setGyroODR function

Parameters
aRate

Definition at line 343 of file imu.c.

void setAccelScale ( enum accel_scale  aScl)

see the setGyroScle function

Parameters
aScl

Definition at line 295 of file imu.c.

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.

Parameters
gRate

Definition at line 332 of file imu.c.

void setGyroScale ( enum gyro_scale  gScl)

set the scale of the gyroscope see enum gyro_Scale to see what values this can take.

Parameters
gScl

Definition at line 277 of file imu.c.

void setMagODR ( enum mag_odr  mRate)

see the setGyroODR function

Parameters
mRate

Definition at line 366 of file imu.c.

void setMagScale ( enum mag_scale  mScl)

see the setGyroScale function

Parameters
mScl

Definition at line 314 of file imu.c.

Variable Documentation

float aRes

Definition at line 310 of file imu.h.

enum accel_scale aScale

Definition at line 189 of file imu.h.

float gRes

Definition at line 311 of file imu.h.

enum gyro_scale gScale

Definition at line 188 of file imu.h.

float mRes

Definition at line 312 of file imu.h.

enum mag_scale mScale

Definition at line 190 of file imu.h.