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

Dependents:   library

Committer:
Gaku0606
Date:
Sat Jan 28 20:32:54 2017 +0000
Revision:
4:c4ab5c6e45c8
Parent:
3:1dcc2a9ff958
Child:
5:92d3913dcaee
jjj

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 1:6a4c2f84180b 4 /**
Gaku0606 3:1dcc2a9ff958 5 * @file mpu9250_i2c.h
Gaku0606 2:4c7bc164cc4d 6 * @note mpu9250を比較的簡単に利用できるようにしたライブラリ
Gaku0606 2:4c7bc164cc4d 7 * @note ローパスフィルタまわりの実装がまだです.外部でよろです!
Gaku0606 2:4c7bc164cc4d 8 * @author Gaku MATSUMOTO
Gaku0606 1:6a4c2f84180b 9 */
Gaku0606 1:6a4c2f84180b 10
Gaku0606 1:6a4c2f84180b 11
Gaku0606 0:d36bfb8300a2 12 #define SLAVE_ADDR_LOW (0b1101000 << 1)//AD0 == LOW
Gaku0606 0:d36bfb8300a2 13 #define SLAVE_ADDR_HIGH (0b1101001 << 1)//AD0 == HIGH
Gaku0606 0:d36bfb8300a2 14 #define MAG_ADDR (0b0001100 << 1)
Gaku0606 0:d36bfb8300a2 15
Gaku0606 0:d36bfb8300a2 16 #define WRITE_FLAG 0b00000000
Gaku0606 0:d36bfb8300a2 17 #define READ_FLAG 0b00000001
Gaku0606 0:d36bfb8300a2 18 #define CONFIG 0x1A
Gaku0606 0:d36bfb8300a2 19 #define GYRO_CONFIG 0x1B
Gaku0606 0:d36bfb8300a2 20 #define ACCEL_CONFIG 0x1C
Gaku0606 0:d36bfb8300a2 21 #define ACCEL_CONFIG2 0x1D
Gaku0606 0:d36bfb8300a2 22 #define LP_ACCEL_ODR 0x1E
Gaku0606 0:d36bfb8300a2 23 #define INT_PIN_CFG 0x37
Gaku0606 0:d36bfb8300a2 24 #define ACCEL_XOUT_H 0x3B
Gaku0606 0:d36bfb8300a2 25 #define ACCEL_XOUT_L 0x3C
Gaku0606 0:d36bfb8300a2 26 #define ACCEL_YOUT_H 0x3D
Gaku0606 0:d36bfb8300a2 27 #define ACCEL_YOUT_L 0x3E
Gaku0606 0:d36bfb8300a2 28 #define ACCLE_ZOUT_H 0x3F
Gaku0606 0:d36bfb8300a2 29 #define ACCEL_ZOUT_L 0x40
Gaku0606 0:d36bfb8300a2 30 #define TEMP_OUT_H 0x41
Gaku0606 0:d36bfb8300a2 31 #define TEMP_OUT_L 0x42
Gaku0606 0:d36bfb8300a2 32 #define GYRO_XOUT_H 0x43
Gaku0606 0:d36bfb8300a2 33 #define GYRO_XOUT_L 0x44
Gaku0606 0:d36bfb8300a2 34 #define GYRO_YOUT_H 0x45
Gaku0606 0:d36bfb8300a2 35 #define GYRO_YOUT_L 0x46
Gaku0606 0:d36bfb8300a2 36 #define GYRO_ZOUT_H 0x47
Gaku0606 0:d36bfb8300a2 37 #define GYRO_ZOUT_L 0x48
Gaku0606 1:6a4c2f84180b 38 #define WHO_AM_I_MPU9250 0x75 //0x71ならおk
Gaku0606 0:d36bfb8300a2 39 #define XG_OFFSET_H 0x13
Gaku0606 0:d36bfb8300a2 40 #define XG_OFFSET_L 0x14
Gaku0606 0:d36bfb8300a2 41 #define YG_OFFSET_H 0x15
Gaku0606 0:d36bfb8300a2 42 #define YG_OFFSET_L 0x16
Gaku0606 0:d36bfb8300a2 43 #define ZG_OFFSET_H 0x17
Gaku0606 0:d36bfb8300a2 44 #define ZG_OFFSET_L 0x18
Gaku0606 0:d36bfb8300a2 45 #define XA_OFFSET_H 0x77
Gaku0606 0:d36bfb8300a2 46 #define XA_OFFSET_L 0x78
Gaku0606 0:d36bfb8300a2 47 #define YA_OFFSET_H 0x79
Gaku0606 0:d36bfb8300a2 48 #define YA_OFFERT_L 0x80
Gaku0606 0:d36bfb8300a2 49 #define ZA_OFFSET_H 0x81
Gaku0606 0:d36bfb8300a2 50 #define ZA_OFFSET_L 0x82
Gaku0606 0:d36bfb8300a2 51
Gaku0606 0:d36bfb8300a2 52 #define WIA 0x00 //device ID
Gaku0606 0:d36bfb8300a2 53 #define INFO 0x01
Gaku0606 0:d36bfb8300a2 54 #define ST1 0x02
Gaku0606 0:d36bfb8300a2 55 #define HXL 0x03//Low -> Highの順に注意
Gaku0606 0:d36bfb8300a2 56 #define HXH 0x04
Gaku0606 0:d36bfb8300a2 57 #define HYL 0x05
Gaku0606 0:d36bfb8300a2 58 #define HYH 0x06
Gaku0606 0:d36bfb8300a2 59 #define HZL 0x07
Gaku0606 0:d36bfb8300a2 60 #define HZH 0x08
Gaku0606 0:d36bfb8300a2 61 #define ST2 0x09
Gaku0606 0:d36bfb8300a2 62 #define CNTL1 0x0A
Gaku0606 0:d36bfb8300a2 63 #define CNTL2 0x0B
Gaku0606 0:d36bfb8300a2 64
Gaku0606 0:d36bfb8300a2 65 #define ACC_LSB (0.0000610350)//[G / LSB]
Gaku0606 0:d36bfb8300a2 66 #define GYRO_LSB (0.007630) //[(degree / s) / LSB]
Gaku0606 0:d36bfb8300a2 67 #define MAG_LSB (0.150) //[uT / LSB]
Gaku0606 0:d36bfb8300a2 68
Gaku0606 0:d36bfb8300a2 69 typedef enum AD0{
Gaku0606 0:d36bfb8300a2 70 AD0_HIGH = 1,
Gaku0606 0:d36bfb8300a2 71 AD0_LOW = 0
Gaku0606 0:d36bfb8300a2 72 }ad0;
Gaku0606 0:d36bfb8300a2 73
Gaku0606 0:d36bfb8300a2 74 typedef enum ACC_RANGE{
Gaku0606 0:d36bfb8300a2 75 _2G = 1,
Gaku0606 0:d36bfb8300a2 76 _4G = 2,
Gaku0606 0:d36bfb8300a2 77 _8G = 4,
Gaku0606 0:d36bfb8300a2 78 _16G = 8
Gaku0606 0:d36bfb8300a2 79 }acc_range;
Gaku0606 0:d36bfb8300a2 80
Gaku0606 0:d36bfb8300a2 81 typedef enum GYRO_RANGE{
Gaku0606 0:d36bfb8300a2 82 _250DPS = 1,
Gaku0606 0:d36bfb8300a2 83 _500DPS = 2,
Gaku0606 0:d36bfb8300a2 84 _1000DPS = 4,
Gaku0606 0:d36bfb8300a2 85 _2000DPS = 8
Gaku0606 0:d36bfb8300a2 86 }gyro_range;
Gaku0606 0:d36bfb8300a2 87
Gaku0606 0:d36bfb8300a2 88 typedef enum MAG_RATE{
Gaku0606 0:d36bfb8300a2 89 _8HZ = 0,
Gaku0606 0:d36bfb8300a2 90 _100HZ = 1
Gaku0606 0:d36bfb8300a2 91 }mag_rate;
Gaku0606 0:d36bfb8300a2 92
Gaku0606 0:d36bfb8300a2 93 typedef enum A_BAND_WIDTH{
Gaku0606 0:d36bfb8300a2 94 NO_USE = 0b00000000,
Gaku0606 0:d36bfb8300a2 95 _460HZ = 0b00001000,
Gaku0606 0:d36bfb8300a2 96 _184HZ = 0b00001001,
Gaku0606 0:d36bfb8300a2 97 _92HZ = 0b00001010,
Gaku0606 0:d36bfb8300a2 98 _41HZ = 0b00001011,
Gaku0606 0:d36bfb8300a2 99 _20HZ = 0b00001100,
Gaku0606 0:d36bfb8300a2 100 _10HZ = 0b00001101,
Gaku0606 0:d36bfb8300a2 101 _5HZ = 0b00001110,
Gaku0606 0:d36bfb8300a2 102 }a_band_width;
Gaku0606 0:d36bfb8300a2 103
Gaku0606 0:d36bfb8300a2 104 class mpu9250{
Gaku0606 0:d36bfb8300a2 105
Gaku0606 0:d36bfb8300a2 106 public:
Gaku0606 1:6a4c2f84180b 107
Gaku0606 4:c4ab5c6e45c8 108 /**
Gaku0606 4:c4ab5c6e45c8 109 * @brief mpu9250インスタンスを生成する
Gaku0606 4:c4ab5c6e45c8 110 * @param _i2c メインプログラムで宣言したI2Cインスタンスのアドレス
Gaku0606 4:c4ab5c6e45c8 111 * @param celect AD0ピンがHIGHならAD0_HIGH,LOWならAD0_LOW
Gaku0606 4:c4ab5c6e45c8 112 * @note 第二引数なしだとAD0_HIGHになります.
Gaku0606 1:6a4c2f84180b 113 */
Gaku0606 1:6a4c2f84180b 114 mpu9250(I2C &_i2c, AD0 celect = AD0_HIGH);
Gaku0606 2:4c7bc164cc4d 115
Gaku0606 0:d36bfb8300a2 116 I2C *_nine;
Gaku0606 0:d36bfb8300a2 117 public:
Gaku0606 0:d36bfb8300a2 118 void writeReg(char addr, char data);
Gaku0606 0:d36bfb8300a2 119 void writeReg(char addr, char reg, char data);
Gaku0606 0:d36bfb8300a2 120 char readReg(char addr, char reg);
Gaku0606 0:d36bfb8300a2 121 void readReg(char addr, char start_reg, char* buff, char num);
Gaku0606 1:6a4c2f84180b 122
Gaku0606 4:c4ab5c6e45c8 123 /**
Gaku0606 4:c4ab5c6e45c8 124 * @brief 慣性センサと通信ができているか確認する
Gaku0606 4:c4ab5c6e45c8 125 * @note trueが返ってきたら成功,falseなら...
Gaku0606 1:6a4c2f84180b 126 */
Gaku0606 2:4c7bc164cc4d 127 bool senserTest();
Gaku0606 1:6a4c2f84180b 128
Gaku0606 4:c4ab5c6e45c8 129 /**
Gaku0606 4:c4ab5c6e45c8 130 * @bref 地磁気センサと通信ができているか確認する
Gaku0606 4:c4ab5c6e45c8 131 * @note trueが返ってきたら成功,falseなら...
Gaku0606 1:6a4c2f84180b 132 */
Gaku0606 0:d36bfb8300a2 133 bool mag_senserTest();
Gaku0606 1:6a4c2f84180b 134
Gaku0606 4:c4ab5c6e45c8 135 /**
Gaku0606 4:c4ab5c6e45c8 136 * @bref 加速度センサのレンジを設定
Gaku0606 4:c4ab5c6e45c8 137 * @param a_range _2G, _4G, _8G, _16Gの中から選択
Gaku0606 4:c4ab5c6e45c8 138 * @note 引数無しで±4Gになる
Gaku0606 1:6a4c2f84180b 139 */
Gaku0606 1:6a4c2f84180b 140 void setAcc(ACC_RANGE a_range = _4G);
Gaku0606 1:6a4c2f84180b 141
Gaku0606 4:c4ab5c6e45c8 142 /**
Gaku0606 4:c4ab5c6e45c8 143 * @bref 角速度センサのレンジ設定
Gaku0606 4:c4ab5c6e45c8 144 * @param g_range _250DPS, _500DPS, _1000DPS, _2000DPSの中から選択
Gaku0606 4:c4ab5c6e45c8 145 * @note 引数無しで±500DPS
Gaku0606 1:6a4c2f84180b 146 */
Gaku0606 1:6a4c2f84180b 147 void setGyro(GYRO_RANGE g_range = _500DPS);
Gaku0606 1:6a4c2f84180b 148
Gaku0606 4:c4ab5c6e45c8 149 /**
Gaku0606 4:c4ab5c6e45c8 150 * @bref 地磁気センサのデータレート設定
Gaku0606 4:c4ab5c6e45c8 151 * @param rate _8HZ か _100HZを選択
Gaku0606 4:c4ab5c6e45c8 152 * @note あえて8Hzにする必要は無いと思います.
Gaku0606 1:6a4c2f84180b 153 */
Gaku0606 1:6a4c2f84180b 154 void setMag(MAG_RATE rate = _100HZ);
Gaku0606 2:4c7bc164cc4d 155
Gaku0606 0:d36bfb8300a2 156 void init();
Gaku0606 1:6a4c2f84180b 157
Gaku0606 4:c4ab5c6e45c8 158
Gaku0606 4:c4ab5c6e45c8 159 /**
Gaku0606 4:c4ab5c6e45c8 160 * @bref I2Cの通信速度を変更できます.余程のことがない限り使用しなくていいです・
Gaku0606 1:6a4c2f84180b 161 */
Gaku0606 0:d36bfb8300a2 162 void frequency(int Hz);
Gaku0606 1:6a4c2f84180b 163
Gaku0606 4:c4ab5c6e45c8 164 /**
Gaku0606 4:c4ab5c6e45c8 165 * @bref mpu9250のデジタルローパスフィルタの設定
Gaku0606 4:c4ab5c6e45c8 166 * @param band NO_USE, _460HZ, _184HZ, _92HZ, _41HZ, _20HZ, _10HZ, _5HZから選択
Gaku0606 4:c4ab5c6e45c8 167 * @note カットオフ周波数なのかサンプルレートなのかよく分かりません.正直効果が見られません
Gaku0606 1:6a4c2f84180b 168 */
Gaku0606 0:d36bfb8300a2 169 void setAccLPF(A_BAND_WIDTH band);
Gaku0606 1:6a4c2f84180b 170
Gaku0606 4:c4ab5c6e45c8 171 /**
Gaku0606 4:c4ab5c6e45c8 172 * @bref ゼロ点のずれを補正するオフセット値を設定する
Gaku0606 4:c4ab5c6e45c8 173 * @param ax,ay,az 加速度のオフセット
Gaku0606 4:c4ab5c6e45c8 174 * @param gx,gy,gz 角速度のオフセット
Gaku0606 4:c4ab5c6e45c8 175 * @param mx,my,mz 地磁気のオフセット
Gaku0606 4:c4ab5c6e45c8 176 * @note とても重要です.地磁気は定期的にキャリブレーションをしてください.ちなみに,これらの値は測定値より引かれています.
Gaku0606 1:6a4c2f84180b 177 */
Gaku0606 0:d36bfb8300a2 178 void setOffset(double ax, double ay, double az,
Gaku0606 0:d36bfb8300a2 179 double gx, double gy, double gz,
Gaku0606 0:d36bfb8300a2 180 double mx, double my, double mz);
Gaku0606 2:4c7bc164cc4d 181
Gaku0606 4:c4ab5c6e45c8 182 /**
Gaku0606 4:c4ab5c6e45c8 183 * @bref 加速度を取得します.
Gaku0606 4:c4ab5c6e45c8 184 * @param ax x軸方向の加速度[G]
Gaku0606 4:c4ab5c6e45c8 185 * @param ay y軸方向の加速度[G]
Gaku0606 4:c4ab5c6e45c8 186 * @param az z軸方向の加速度[G]
Gaku0606 4:c4ab5c6e45c8 187 * @note 型はfloat でも doubleでも構いません.
Gaku0606 1:6a4c2f84180b 188 */
Gaku0606 1:6a4c2f84180b 189 template<typename T>void getAcc(T *ax, T *ay, T *az);
Gaku0606 2:4c7bc164cc4d 190
Gaku0606 4:c4ab5c6e45c8 191 /**
Gaku0606 4:c4ab5c6e45c8 192 * @bref 加速度を取得します.
Gaku0606 4:c4ab5c6e45c8 193 * @param acc 各軸方向の加速度[G],x,y,zの順
Gaku0606 4:c4ab5c6e45c8 194 * @note 型はfloat でも doubleでも構いません.
Gaku0606 1:6a4c2f84180b 195 */
Gaku0606 1:6a4c2f84180b 196 template<typename T>void getAcc(T *acc);
Gaku0606 0:d36bfb8300a2 197
Gaku0606 4:c4ab5c6e45c8 198 /**
Gaku0606 4:c4ab5c6e45c8 199 * @bref 角速度を取得します.
Gaku0606 4:c4ab5c6e45c8 200 * @param gx x軸方向の角速度[degree/s]
Gaku0606 4:c4ab5c6e45c8 201 * @param gy y軸方向の角速度[degree/s]
Gaku0606 4:c4ab5c6e45c8 202 * @param gz z軸方向の角速度[degree/s]
Gaku0606 4:c4ab5c6e45c8 203 * @note 型はfloat でも doubleでも構いません.
Gaku0606 1:6a4c2f84180b 204 */
Gaku0606 1:6a4c2f84180b 205 template<typename T>void getGyro(T *gx, T *gy, T *gz);
Gaku0606 2:4c7bc164cc4d 206
Gaku0606 4:c4ab5c6e45c8 207 /**
Gaku0606 4:c4ab5c6e45c8 208 * @bref 角速度を取得します.
Gaku0606 4:c4ab5c6e45c8 209 * @param gyro 各軸方向の角速度[degree/s], x,y,zの順
Gaku0606 4:c4ab5c6e45c8 210 * @note 型はfloat でも doubleでも構いません.
Gaku0606 1:6a4c2f84180b 211 */
Gaku0606 1:6a4c2f84180b 212 template<typename T>void getGyro(T *gyro);
Gaku0606 0:d36bfb8300a2 213
Gaku0606 4:c4ab5c6e45c8 214 /**
Gaku0606 4:c4ab5c6e45c8 215 * @bref 磁束密度を取得します.
Gaku0606 4:c4ab5c6e45c8 216 * @param mx x軸方向の磁束密度[uT]
Gaku0606 4:c4ab5c6e45c8 217 * @param my y軸方向の磁束密度[uT]
Gaku0606 4:c4ab5c6e45c8 218 * @param mz z軸方向の磁束密度[uT]
Gaku0606 4:c4ab5c6e45c8 219 * @note 型はfloat でも doubleでも構いません.
Gaku0606 1:6a4c2f84180b 220 */
Gaku0606 1:6a4c2f84180b 221 template<typename T>void getMag(T *mx, T *my, T *mz);
Gaku0606 2:4c7bc164cc4d 222
Gaku0606 4:c4ab5c6e45c8 223
Gaku0606 4:c4ab5c6e45c8 224 /**
Gaku0606 4:c4ab5c6e45c8 225 * @bref 磁束密度を取得します.
Gaku0606 4:c4ab5c6e45c8 226 * @param mag 各軸方向の磁束密度[uT],x,y,zの順
Gaku0606 4:c4ab5c6e45c8 227 * @note 型はfloat でも doubleでも構いません.
Gaku0606 1:6a4c2f84180b 228 */
Gaku0606 1:6a4c2f84180b 229 template<typename T>void getMag(T *mag);
Gaku0606 0:d36bfb8300a2 230
Gaku0606 4:c4ab5c6e45c8 231 /**
Gaku0606 4:c4ab5c6e45c8 232 * @bref 角速度と加速度を同時に取得します.
Gaku0606 4:c4ab5c6e45c8 233 * @param imu データを入れる配列,角速度[degree/s],加速度[G]の順
Gaku0606 4:c4ab5c6e45c8 234 * @note 配列数は6以上で
Gaku0606 1:6a4c2f84180b 235 */
Gaku0606 1:6a4c2f84180b 236 template<typename T>void getGyroAcc(T *imu);//gx,gy,gz,ax,ay,az
Gaku0606 0:d36bfb8300a2 237
Gaku0606 1:6a4c2f84180b 238 private:
Gaku0606 0:d36bfb8300a2 239 static char _addr;
Gaku0606 0:d36bfb8300a2 240 static double acc_coef;//coefficient
Gaku0606 0:d36bfb8300a2 241 static double gyro_coef;
Gaku0606 0:d36bfb8300a2 242 static double mag_coef;
Gaku0606 0:d36bfb8300a2 243 static double acc_offset[3];
Gaku0606 0:d36bfb8300a2 244 static double gyro_offset[3];
Gaku0606 0:d36bfb8300a2 245 static double mag_offset[3];
Gaku0606 0:d36bfb8300a2 246 };
Gaku0606 0:d36bfb8300a2 247
Gaku0606 0:d36bfb8300a2 248
Gaku0606 0:d36bfb8300a2 249
Gaku0606 0:d36bfb8300a2 250 inline void mpu9250::writeReg(char addr, char data){
Gaku0606 0:d36bfb8300a2 251 _nine->write( addr | WRITE_FLAG, &data, 1, false);
Gaku0606 0:d36bfb8300a2 252 }
Gaku0606 0:d36bfb8300a2 253 inline void mpu9250::writeReg(char addr, char reg, char data){
Gaku0606 0:d36bfb8300a2 254 char temp[2] = { reg, data};
Gaku0606 0:d36bfb8300a2 255 _nine->write(addr | WRITE_FLAG, temp, 2, false);
Gaku0606 0:d36bfb8300a2 256 }
Gaku0606 0:d36bfb8300a2 257 inline char mpu9250::readReg(char addr, char reg){
Gaku0606 0:d36bfb8300a2 258 char buff[1];
Gaku0606 0:d36bfb8300a2 259 writeReg(addr, reg);
Gaku0606 0:d36bfb8300a2 260 _nine->read(addr | READ_FLAG, buff, 1, true);
Gaku0606 0:d36bfb8300a2 261 return buff[0];
Gaku0606 0:d36bfb8300a2 262 }
Gaku0606 0:d36bfb8300a2 263 inline void mpu9250::readReg(char addr, char start_reg, char* buff, char num){
Gaku0606 0:d36bfb8300a2 264 writeReg(addr, start_reg);
Gaku0606 0:d36bfb8300a2 265 _nine->read(addr | READ_FLAG, buff, num, true);
Gaku0606 0:d36bfb8300a2 266 }
Gaku0606 0:d36bfb8300a2 267
Gaku0606 0:d36bfb8300a2 268 #endif