IZU2020 / PQMPU9250

Dependents:   IZU2020_AVIONICS IZU2020_AVIONICS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PQMPU9250.h Source File

PQMPU9250.h

00001 #ifndef PQMPU9250_H
00002 #define MPU9250_PQMPU9250_H
00003 
00004 #define MPU9250_ADDR_HIGH 0b1101001<<1
00005 #define MPU9250_ADDR_LOW 0b1101000<<1
00006 #define MPU9250_AK8963_ADDR 0b0001100<<1
00007 #define MPU9250_WIA 0x00
00008 #define MPU9250_ST1 0x02
00009 #define MPU9250_HXL 0x03
00010 #define MPU9250_CNTL1 0x0A
00011 #define MPU9250_GYRO_CONFIG 0x1B
00012 #define MPU9250_ACCEL_CONFIG 0x1C
00013 #define MPU9250_INT_PIN_CFG 0x37
00014 #define MPU9250_ACCEL_XOUT_H 0x3B
00015 #define MPU9250_GYRO_XOUT_H 0x43
00016 #define MPU9250_PWR_MGMT_1 0x6B
00017 #define MPU9250_WHO_AM_I 0x75
00018 #define MPU9250_ACCEL_LSB 0.0000610
00019 #define MPU9250_GYRO_LSB 0.00763
00020 #define MPU9250_MAG_LSB 0.150
00021 
00022 /**
00023  * 9軸センサMPU9250のライブラリ
00024  * @code
00025 #include "mbed.h"
00026 #include "PQMPU9250.h"
00027 
00028 Serial pc(USBTX, USBRX, 115200);
00029 I2C i2c(p9, p10);
00030 
00031 MPU9250 mpu(i2c, MPU9250::AD0_HIGH);
00032 
00033 float accel_offset[] = {0, 0, 0};
00034 float gyro_offset[] = {0, 0, 0};
00035 float mag_offset[] = {0, 0, 0};
00036 float accel[3];
00037 float gyro[3];
00038 float mag[3];
00039 
00040 int main() {
00041     mpu.begin();
00042     mpu.set(_2G, _250DPS, _16B_8HZ);
00043     mpu.offset(accel_offset, gyro_offset, mag_offset);
00044     if(!mpu.test()){
00045         pc.printf("[  FAIL  ] MPU9250 cannot be reached.\r\n");
00046     }
00047     if(!mpu.test_AK8963()){
00048         pc.printf("[  FAIL  ] AK8963 cannot be reached.\r\n");
00049     }
00050     while(1) {
00051         mpu.read(accel, gyro, mag);
00052         pc.printf("%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\r\n", accel[0], accel[1], accel[2], gyro[0], gyro[1], gyro[2], mag[0], mag[1], mag[2]);
00053     }
00054 }
00055  * @endcode
00056  */
00057 class MPU9250
00058 {
00059 public:
00060     typedef enum {
00061         AD0_HIGH = MPU9250_ADDR_HIGH,
00062         AD0_LOW  = MPU9250_ADDR_LOW
00063     } AD0_t;
00064 
00065     typedef enum {
00066         _2G  = 0b00000000,
00067         _4G  = 0b00001000,
00068         _8G  = 0b00010000,
00069         _16G = 0b00011000
00070     } AccelConfig_t;
00071 
00072     typedef enum {
00073         _250DPS  = 0b00000000,
00074         _500DPS  = 0b00001000,
00075         _1000DPS = 0b00010000,
00076         _2000DPS = 0b00011000
00077     } GyroConfig_t;
00078 
00079     typedef enum {
00080         _16B_8HZ   = 0b00010010,
00081         _16B_100HZ = 0b00010110,
00082         _14B_8HZ   = 0b00000010,
00083         _14B_100HZ = 0b00000100
00084     } MagConfig_t;
00085 
00086 private:
00087     I2C *_i2c;
00088     int _addr;
00089     char cmd[2];
00090     char buff[8];
00091     float accel_LSB;
00092     float gyro_LSB;
00093     float mag_LSB;
00094     float accel_offset[3];
00095     float gyro_offset[3];
00096     float mag_offset[3];
00097 public:
00098 
00099     /**
00100      * @param i2c I2Cのインスタンスへの参照
00101      * @param AD0 AD0ピンのH/Lレベル
00102      */
00103     MPU9250 (I2C &i2c, AD0_t AD0);
00104 
00105     /**
00106      * センサ動作開始
00107      */
00108     void begin();
00109 
00110     /**
00111      * センサ通信テスト
00112      * @retval true 通信成功
00113      * @retval false 通信失敗
00114      */
00115     bool test();
00116 
00117     /**
00118      * 磁気センサAK8963通信テスト
00119      * @retval true 通信成功
00120      * @retval false 通信失敗
00121      */
00122     bool test_AK8963();
00123 
00124     /**
00125      * センサ設定
00126      * @param accel_config 加速度の測定レンジ
00127      * @param gyro_config 角速度の測定レンジ
00128      * @param mag_config 磁気センサの分解能/データレート
00129      */
00130     void set(AccelConfig_t accel_config, GyroConfig_t gyro_config, MagConfig_t mag_config);
00131 
00132     /**
00133      * 加速度センサ設定
00134      * @param accel_config 角速度センサの測定レンジ
00135      */
00136     void set_accel(AccelConfig_t accel_config);
00137 
00138     /**
00139      * 角速度センサ設定
00140      * @param gyro_config 角速度の測定レンジ
00141      */
00142     void set_gyro(GyroConfig_t gyro_config);
00143 
00144     /**
00145      * 磁気センサ設定
00146      * @param mag_config 磁気センサの分解能/データレート
00147      */
00148     void set_mag(MagConfig_t mag_config);
00149 
00150     /**
00151      * ゼロ点補正
00152      * @param accel 加速度のオフセット配列
00153      * @param gyro 角速度のオフセット配列
00154      * @param mag 磁気のオフセット配列
00155      */
00156     void offset(float *accel, float *gyro, float *mag);
00157 
00158     /**
00159      * 加速度のゼロ点補正
00160      * @param accel 加速度のオフセット配列
00161      */
00162     void offset_accel(float *accel);
00163 
00164     /**
00165      * 角速度のゼロ点補正
00166      * @param gyro 角速度のオフセット配列
00167      */
00168     void offset_gyro(float *gyro);
00169 
00170     /**
00171      * 磁気のゼロ点補正
00172      * @param mag 磁気のオフセット配列
00173      */
00174     void offset_mag(float *mag);
00175 
00176     /**
00177      * 測定値の読み取り
00178      * @param accel 加速度を格納する配列
00179      * @param gyro 角速度を格納する配列
00180      * @param mag 磁気を格納する配列
00181      */
00182     void read(float *accel, float *gyro, float *mag);
00183 
00184     /**
00185      * 加速度測定値の読み取り
00186      * @param accel 加速度を格納する配列
00187      */
00188     void read_accel(float *accel);
00189 
00190     /**
00191      * 角速度測定値の読み取り
00192      * @param gyro 角速度を格納する配列
00193      */
00194     void read_gyro(float *gyro);
00195 
00196     /**
00197      * 磁気測定値の読み取り
00198      * @param mag 磁気を格納する配列
00199      */
00200     void read_mag(float *mag);
00201 };
00202 
00203 #endif