mpu9250のライブラリ、I2Cを利用。開発段階のため微妙

Dependents:   library

Committer:
Gaku0606
Date:
Mon Jan 09 06:28:04 2017 +0000
Revision:
0:d36bfb8300a2
Child:
1:6a4c2f84180b
mpu9250 library with I2C

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Gaku0606 0:d36bfb8300a2 1 #ifndef _MPU9250_I2C_H_
Gaku0606 0:d36bfb8300a2 2 #define _MPU9250_I2C_H_
Gaku0606 0:d36bfb8300a2 3
Gaku0606 0:d36bfb8300a2 4 #define SLAVE_ADDR_LOW (0b1101000 << 1)//AD0 == LOW
Gaku0606 0:d36bfb8300a2 5 #define SLAVE_ADDR_HIGH (0b1101001 << 1)//AD0 == HIGH
Gaku0606 0:d36bfb8300a2 6 #define MAG_ADDR (0b0001100 << 1)
Gaku0606 0:d36bfb8300a2 7
Gaku0606 0:d36bfb8300a2 8 #define WRITE_FLAG 0b00000000
Gaku0606 0:d36bfb8300a2 9 #define READ_FLAG 0b00000001
Gaku0606 0:d36bfb8300a2 10 #define CONFIG 0x1A
Gaku0606 0:d36bfb8300a2 11 #define GYRO_CONFIG 0x1B
Gaku0606 0:d36bfb8300a2 12 #define ACCEL_CONFIG 0x1C
Gaku0606 0:d36bfb8300a2 13 #define ACCEL_CONFIG2 0x1D
Gaku0606 0:d36bfb8300a2 14 #define LP_ACCEL_ODR 0x1E
Gaku0606 0:d36bfb8300a2 15 #define INT_PIN_CFG 0x37
Gaku0606 0:d36bfb8300a2 16 #define ACCEL_XOUT_H 0x3B
Gaku0606 0:d36bfb8300a2 17 #define ACCEL_XOUT_L 0x3C
Gaku0606 0:d36bfb8300a2 18 #define ACCEL_YOUT_H 0x3D
Gaku0606 0:d36bfb8300a2 19 #define ACCEL_YOUT_L 0x3E
Gaku0606 0:d36bfb8300a2 20 #define ACCLE_ZOUT_H 0x3F
Gaku0606 0:d36bfb8300a2 21 #define ACCEL_ZOUT_L 0x40
Gaku0606 0:d36bfb8300a2 22 #define TEMP_OUT_H 0x41
Gaku0606 0:d36bfb8300a2 23 #define TEMP_OUT_L 0x42
Gaku0606 0:d36bfb8300a2 24 #define GYRO_XOUT_H 0x43
Gaku0606 0:d36bfb8300a2 25 #define GYRO_XOUT_L 0x44
Gaku0606 0:d36bfb8300a2 26 #define GYRO_YOUT_H 0x45
Gaku0606 0:d36bfb8300a2 27 #define GYRO_YOUT_L 0x46
Gaku0606 0:d36bfb8300a2 28 #define GYRO_ZOUT_H 0x47
Gaku0606 0:d36bfb8300a2 29 #define GYRO_ZOUT_L 0x48
Gaku0606 0:d36bfb8300a2 30 #define WHO_AM_I 0x75 //0x71ならおk
Gaku0606 0:d36bfb8300a2 31 #define XG_OFFSET_H 0x13
Gaku0606 0:d36bfb8300a2 32 #define XG_OFFSET_L 0x14
Gaku0606 0:d36bfb8300a2 33 #define YG_OFFSET_H 0x15
Gaku0606 0:d36bfb8300a2 34 #define YG_OFFSET_L 0x16
Gaku0606 0:d36bfb8300a2 35 #define ZG_OFFSET_H 0x17
Gaku0606 0:d36bfb8300a2 36 #define ZG_OFFSET_L 0x18
Gaku0606 0:d36bfb8300a2 37 #define XA_OFFSET_H 0x77
Gaku0606 0:d36bfb8300a2 38 #define XA_OFFSET_L 0x78
Gaku0606 0:d36bfb8300a2 39 #define YA_OFFSET_H 0x79
Gaku0606 0:d36bfb8300a2 40 #define YA_OFFERT_L 0x80
Gaku0606 0:d36bfb8300a2 41 #define ZA_OFFSET_H 0x81
Gaku0606 0:d36bfb8300a2 42 #define ZA_OFFSET_L 0x82
Gaku0606 0:d36bfb8300a2 43
Gaku0606 0:d36bfb8300a2 44 #define WIA 0x00 //device ID
Gaku0606 0:d36bfb8300a2 45 #define INFO 0x01
Gaku0606 0:d36bfb8300a2 46 #define ST1 0x02
Gaku0606 0:d36bfb8300a2 47 #define HXL 0x03//Low -> Highの順に注意
Gaku0606 0:d36bfb8300a2 48 #define HXH 0x04
Gaku0606 0:d36bfb8300a2 49 #define HYL 0x05
Gaku0606 0:d36bfb8300a2 50 #define HYH 0x06
Gaku0606 0:d36bfb8300a2 51 #define HZL 0x07
Gaku0606 0:d36bfb8300a2 52 #define HZH 0x08
Gaku0606 0:d36bfb8300a2 53 #define ST2 0x09
Gaku0606 0:d36bfb8300a2 54 #define CNTL1 0x0A
Gaku0606 0:d36bfb8300a2 55 #define CNTL2 0x0B
Gaku0606 0:d36bfb8300a2 56
Gaku0606 0:d36bfb8300a2 57 #define ACC_LSB (0.0000610350)//[G / LSB]
Gaku0606 0:d36bfb8300a2 58 #define GYRO_LSB (0.007630) //[(degree / s) / LSB]
Gaku0606 0:d36bfb8300a2 59 #define MAG_LSB (0.150) //[uT / LSB]
Gaku0606 0:d36bfb8300a2 60
Gaku0606 0:d36bfb8300a2 61 typedef enum AD0{
Gaku0606 0:d36bfb8300a2 62 AD0_HIGH = 1,
Gaku0606 0:d36bfb8300a2 63 AD0_LOW = 0
Gaku0606 0:d36bfb8300a2 64 }ad0;
Gaku0606 0:d36bfb8300a2 65
Gaku0606 0:d36bfb8300a2 66 typedef enum ACC_RANGE{
Gaku0606 0:d36bfb8300a2 67 _2G = 1,
Gaku0606 0:d36bfb8300a2 68 _4G = 2,
Gaku0606 0:d36bfb8300a2 69 _8G = 4,
Gaku0606 0:d36bfb8300a2 70 _16G = 8
Gaku0606 0:d36bfb8300a2 71 }acc_range;
Gaku0606 0:d36bfb8300a2 72
Gaku0606 0:d36bfb8300a2 73 typedef enum GYRO_RANGE{
Gaku0606 0:d36bfb8300a2 74 _250DPS = 1,
Gaku0606 0:d36bfb8300a2 75 _500DPS = 2,
Gaku0606 0:d36bfb8300a2 76 _1000DPS = 4,
Gaku0606 0:d36bfb8300a2 77 _2000DPS = 8
Gaku0606 0:d36bfb8300a2 78 }gyro_range;
Gaku0606 0:d36bfb8300a2 79
Gaku0606 0:d36bfb8300a2 80 typedef enum MAG_RATE{
Gaku0606 0:d36bfb8300a2 81 _8HZ = 0,
Gaku0606 0:d36bfb8300a2 82 _100HZ = 1
Gaku0606 0:d36bfb8300a2 83 }mag_rate;
Gaku0606 0:d36bfb8300a2 84
Gaku0606 0:d36bfb8300a2 85 typedef enum A_BAND_WIDTH{
Gaku0606 0:d36bfb8300a2 86 NO_USE = 0b00000000,
Gaku0606 0:d36bfb8300a2 87 _460HZ = 0b00001000,
Gaku0606 0:d36bfb8300a2 88 _184HZ = 0b00001001,
Gaku0606 0:d36bfb8300a2 89 _92HZ = 0b00001010,
Gaku0606 0:d36bfb8300a2 90 _41HZ = 0b00001011,
Gaku0606 0:d36bfb8300a2 91 _20HZ = 0b00001100,
Gaku0606 0:d36bfb8300a2 92 _10HZ = 0b00001101,
Gaku0606 0:d36bfb8300a2 93 _5HZ = 0b00001110,
Gaku0606 0:d36bfb8300a2 94 }a_band_width;
Gaku0606 0:d36bfb8300a2 95
Gaku0606 0:d36bfb8300a2 96 class mpu9250{
Gaku0606 0:d36bfb8300a2 97
Gaku0606 0:d36bfb8300a2 98 public:
Gaku0606 0:d36bfb8300a2 99 mpu9250(I2C &_i2c, AD0 celect);
Gaku0606 0:d36bfb8300a2 100 I2C *_nine;
Gaku0606 0:d36bfb8300a2 101 public:
Gaku0606 0:d36bfb8300a2 102 void writeReg(char addr, char data);
Gaku0606 0:d36bfb8300a2 103 void writeReg(char addr, char reg, char data);
Gaku0606 0:d36bfb8300a2 104 char readReg(char addr, char reg);
Gaku0606 0:d36bfb8300a2 105 void readReg(char addr, char start_reg, char* buff, char num);
Gaku0606 0:d36bfb8300a2 106 bool senserTest();
Gaku0606 0:d36bfb8300a2 107 bool mag_senserTest();
Gaku0606 0:d36bfb8300a2 108 void setAcc(ACC_RANGE a_range);
Gaku0606 0:d36bfb8300a2 109 void setGyro(GYRO_RANGE g_range);
Gaku0606 0:d36bfb8300a2 110 void setMag(MAG_RATE rate);
Gaku0606 0:d36bfb8300a2 111 void init();
Gaku0606 0:d36bfb8300a2 112 void frequency(int Hz);
Gaku0606 0:d36bfb8300a2 113 void setAccLPF(A_BAND_WIDTH band);
Gaku0606 0:d36bfb8300a2 114 void setOffset(double ax, double ay, double az,
Gaku0606 0:d36bfb8300a2 115 double gx, double gy, double gz,
Gaku0606 0:d36bfb8300a2 116 double mx, double my, double mz);
Gaku0606 0:d36bfb8300a2 117
Gaku0606 0:d36bfb8300a2 118 void getAcc(double *ax, double *ay, double *az);
Gaku0606 0:d36bfb8300a2 119 void getAcc(double *acc);
Gaku0606 0:d36bfb8300a2 120
Gaku0606 0:d36bfb8300a2 121 void getGyro(double *gx, double *gy, double *gz);
Gaku0606 0:d36bfb8300a2 122 void getGyro(double *gyro);
Gaku0606 0:d36bfb8300a2 123
Gaku0606 0:d36bfb8300a2 124 void getMag(double *mx, double *my, double *mz);
Gaku0606 0:d36bfb8300a2 125 void getMag(double *mag);
Gaku0606 0:d36bfb8300a2 126
Gaku0606 0:d36bfb8300a2 127 void getGyroAcc(double *imu);//gx,gy,gz,ax,ay,az
Gaku0606 0:d36bfb8300a2 128
Gaku0606 0:d36bfb8300a2 129 static char _addr;
Gaku0606 0:d36bfb8300a2 130 static double acc_coef;//coefficient
Gaku0606 0:d36bfb8300a2 131 static double gyro_coef;
Gaku0606 0:d36bfb8300a2 132 static double mag_coef;
Gaku0606 0:d36bfb8300a2 133 static double acc_offset[3];
Gaku0606 0:d36bfb8300a2 134 static double gyro_offset[3];
Gaku0606 0:d36bfb8300a2 135 static double mag_offset[3];
Gaku0606 0:d36bfb8300a2 136 };
Gaku0606 0:d36bfb8300a2 137
Gaku0606 0:d36bfb8300a2 138
Gaku0606 0:d36bfb8300a2 139
Gaku0606 0:d36bfb8300a2 140 inline void mpu9250::writeReg(char addr, char data){
Gaku0606 0:d36bfb8300a2 141 _nine->write( addr | WRITE_FLAG, &data, 1, false);
Gaku0606 0:d36bfb8300a2 142 }
Gaku0606 0:d36bfb8300a2 143 inline void mpu9250::writeReg(char addr, char reg, char data){
Gaku0606 0:d36bfb8300a2 144 char temp[2] = { reg, data};
Gaku0606 0:d36bfb8300a2 145 _nine->write(addr | WRITE_FLAG, temp, 2, false);
Gaku0606 0:d36bfb8300a2 146 }
Gaku0606 0:d36bfb8300a2 147 inline char mpu9250::readReg(char addr, char reg){
Gaku0606 0:d36bfb8300a2 148 char buff[1];
Gaku0606 0:d36bfb8300a2 149 writeReg(addr, reg);
Gaku0606 0:d36bfb8300a2 150 _nine->read(addr | READ_FLAG, buff, 1, true);
Gaku0606 0:d36bfb8300a2 151 return buff[0];
Gaku0606 0:d36bfb8300a2 152 }
Gaku0606 0:d36bfb8300a2 153 inline void mpu9250::readReg(char addr, char start_reg, char* buff, char num){
Gaku0606 0:d36bfb8300a2 154 writeReg(addr, start_reg);
Gaku0606 0:d36bfb8300a2 155 _nine->read(addr | READ_FLAG, buff, num, true);
Gaku0606 0:d36bfb8300a2 156 }
Gaku0606 0:d36bfb8300a2 157
Gaku0606 0:d36bfb8300a2 158 #endif