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

Dependents:   library

Committer:
Gaku0606
Date:
Sat Jan 28 20:50:23 2017 +0000
Revision:
8:96cb569b6349
Parent:
7:22f5519ab43a
Child:
9:424ce8140bd1
fff

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