磁気センサv4
Dependencies: mbed
Fork of mag_sensor by
main.cpp@0:ae23b58fc2d4, 2018-08-12 (annotated)
- Committer:
- ponpoko1939
- Date:
- Sun Aug 12 02:16:58 2018 +0000
- Revision:
- 0:ae23b58fc2d4
- Child:
- 1:21ba826811d6
mag??????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ponpoko1939 | 0:ae23b58fc2d4 | 1 | /*MPU9250_PROGRAM ver2.2 PROGRAMED BY RYOTARO FUNAI 2018/08/10*/ |
ponpoko1939 | 0:ae23b58fc2d4 | 2 | #include <mbed.h> |
ponpoko1939 | 0:ae23b58fc2d4 | 3 | #include <math.h> |
ponpoko1939 | 0:ae23b58fc2d4 | 4 | |
ponpoko1939 | 0:ae23b58fc2d4 | 5 | I2C i2c(p9, p10); |
ponpoko1939 | 0:ae23b58fc2d4 | 6 | Serial pc(USBTX, USBRX); //TWELITE使う予定なら13,14ピン |
ponpoko1939 | 0:ae23b58fc2d4 | 7 | |
ponpoko1939 | 0:ae23b58fc2d4 | 8 | #define MPU9250_ADDRESS 0x68<<1 //I2CでのMPU9250のスレーブアドレス |
ponpoko1939 | 0:ae23b58fc2d4 | 9 | #define AK8963_ADDRESS 0x0c<<1 //磁気センサのスレーブアドレス |
ponpoko1939 | 0:ae23b58fc2d4 | 10 | #define Whoami 0x75 //who_am_iレジスタのアドレス、0x71が返ってくる |
ponpoko1939 | 0:ae23b58fc2d4 | 11 | #define Magadd 0x00 //ak8963のWho_am_i |
ponpoko1939 | 0:ae23b58fc2d4 | 12 | #define PWR 0x6b //スリープモードをonにするためのアドレス |
ponpoko1939 | 0:ae23b58fc2d4 | 13 | #define MAG_OPN 0x37 //mpu9250から磁気センサにアクセスできるようにする |
ponpoko1939 | 0:ae23b58fc2d4 | 14 | #define ACC_CONFIG 0x1c //加速度センサ設定用のアドレス |
ponpoko1939 | 0:ae23b58fc2d4 | 15 | #define ACC_2G 0x00 //加速度センサのレンジ(2G) |
ponpoko1939 | 0:ae23b58fc2d4 | 16 | #define ACC_4G 0x08 //加速度センサのレンジ(4G) |
ponpoko1939 | 0:ae23b58fc2d4 | 17 | #define ACC_8G 0x10 //加速度センサのレンジ(8G) |
ponpoko1939 | 0:ae23b58fc2d4 | 18 | #define ACC_16G 0x18 //加速度センサのレンジ(16Gまで計測可能) |
ponpoko1939 | 0:ae23b58fc2d4 | 19 | #define MAG_CONFIG 0x0a //磁気センサ設定用のアドレス |
ponpoko1939 | 0:ae23b58fc2d4 | 20 | #define MAG_8HZ 0x12 //磁気センサの出力周期(8Hz) |
ponpoko1939 | 0:ae23b58fc2d4 | 21 | #define MAG_100HZ 0x16 //磁気センサの出力周期(100Hz) |
ponpoko1939 | 0:ae23b58fc2d4 | 22 | #define accRange 16.0 //加速度センサの測定レンジ |
ponpoko1939 | 0:ae23b58fc2d4 | 23 | #define ST2 0x02 //磁気センサのステータスが入っているアドレス |
ponpoko1939 | 0:ae23b58fc2d4 | 24 | #define Ain 35 |
ponpoko1939 | 0:ae23b58fc2d4 | 25 | #define SDA 21 |
ponpoko1939 | 0:ae23b58fc2d4 | 26 | #define SCL 22 |
ponpoko1939 | 0:ae23b58fc2d4 | 27 | #define led 2 //チェック用のLEDピン |
ponpoko1939 | 0:ae23b58fc2d4 | 28 | |
ponpoko1939 | 0:ae23b58fc2d4 | 29 | void Ac_Read(int16_t*, int16_t*, int16_t*); //9軸から加速度の値を取得 |
ponpoko1939 | 0:ae23b58fc2d4 | 30 | void Mag_Read(int16_t*, int16_t*, int16_t*); |
ponpoko1939 | 0:ae23b58fc2d4 | 31 | //addrにスレーブアドレス、regにアクセスするアドレスを入力する |
ponpoko1939 | 0:ae23b58fc2d4 | 32 | void i2cRead(uint8_t addr,uint8_t reg, uint8_t bytes,uint8_t* data); |
ponpoko1939 | 0:ae23b58fc2d4 | 33 | void i2cWrite(uint8_t addr,uint8_t reg, uint8_t data); |
ponpoko1939 | 0:ae23b58fc2d4 | 34 | uint8_t IDcheck(); |
ponpoko1939 | 0:ae23b58fc2d4 | 35 | |
ponpoko1939 | 0:ae23b58fc2d4 | 36 | uint8_t accgyrodata[14]; |
ponpoko1939 | 0:ae23b58fc2d4 | 37 | uint8_t magneticdata[7]; |
ponpoko1939 | 0:ae23b58fc2d4 | 38 | uint8_t ST2_Bit; //磁気センサのステータスを入れておく |
ponpoko1939 | 0:ae23b58fc2d4 | 39 | |
ponpoko1939 | 0:ae23b58fc2d4 | 40 | int16_t mx, my, mz; |
ponpoko1939 | 0:ae23b58fc2d4 | 41 | double magX, magY, magZ, mag; |
ponpoko1939 | 0:ae23b58fc2d4 | 42 | float rad; |
ponpoko1939 | 0:ae23b58fc2d4 | 43 | float degree; |
ponpoko1939 | 0:ae23b58fc2d4 | 44 | float ID; |
ponpoko1939 | 0:ae23b58fc2d4 | 45 | int main() { |
ponpoko1939 | 0:ae23b58fc2d4 | 46 | i2cWrite(MPU9250_ADDRESS, PWR, 0x00); //スリープモードの解除 |
ponpoko1939 | 0:ae23b58fc2d4 | 47 | i2cWrite(MPU9250_ADDRESS, MAG_OPN, 0x02); //磁気センサの起動 |
ponpoko1939 | 0:ae23b58fc2d4 | 48 | i2cWrite(AK8963_ADDRESS, MAG_CONFIG, MAG_100HZ);//磁気センサの測定レンジの設定 |
ponpoko1939 | 0:ae23b58fc2d4 | 49 | double maveX = 0, maveY = 0, maveZ = 0; |
ponpoko1939 | 0:ae23b58fc2d4 | 50 | while(1) { |
ponpoko1939 | 0:ae23b58fc2d4 | 51 | for(int i = 0;i < 100;i++){ |
ponpoko1939 | 0:ae23b58fc2d4 | 52 | Mag_Read(&mx, &my, &mz); |
ponpoko1939 | 0:ae23b58fc2d4 | 53 | maveX += mx; |
ponpoko1939 | 0:ae23b58fc2d4 | 54 | maveY += my; |
ponpoko1939 | 0:ae23b58fc2d4 | 55 | maveZ += mz; |
ponpoko1939 | 0:ae23b58fc2d4 | 56 | wait_ms(15); |
ponpoko1939 | 0:ae23b58fc2d4 | 57 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 58 | maveX /= 100; |
ponpoko1939 | 0:ae23b58fc2d4 | 59 | maveY /= 100; |
ponpoko1939 | 0:ae23b58fc2d4 | 60 | maveZ /= 100; |
ponpoko1939 | 0:ae23b58fc2d4 | 61 | magX = (maveX - 168.75) / 32768.0f * 4800.0f;//[uT]に変換 |
ponpoko1939 | 0:ae23b58fc2d4 | 62 | magY = (maveY - 18.75) / 32768.0f * 4800.0f;//[uT]に変換 |
ponpoko1939 | 0:ae23b58fc2d4 | 63 | pc.printf("%f,%f\n\r", magX, magY); |
ponpoko1939 | 0:ae23b58fc2d4 | 64 | ID = IDcheck(); |
ponpoko1939 | 0:ae23b58fc2d4 | 65 | //pc.printf("%f\n\r", ID); |
ponpoko1939 | 0:ae23b58fc2d4 | 66 | //magZ = mz / 32768.0f * 4800.0f;//[uT]に変換 |
ponpoko1939 | 0:ae23b58fc2d4 | 67 | rad = atan2(magY, magX); |
ponpoko1939 | 0:ae23b58fc2d4 | 68 | degree = (180.0 * rad / 3.14); |
ponpoko1939 | 0:ae23b58fc2d4 | 69 | if(degree < 0){ |
ponpoko1939 | 0:ae23b58fc2d4 | 70 | degree += 360; |
ponpoko1939 | 0:ae23b58fc2d4 | 71 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 72 | //pc.printf("%f\n\r",degree); |
ponpoko1939 | 0:ae23b58fc2d4 | 73 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 74 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 75 | |
ponpoko1939 | 0:ae23b58fc2d4 | 76 | //mpu9250から磁気センサのみ引っ張ってくる |
ponpoko1939 | 0:ae23b58fc2d4 | 77 | void Mag_Read(int16_t* mx, int16_t* my, int16_t* mz){ |
ponpoko1939 | 0:ae23b58fc2d4 | 78 | i2cRead(AK8963_ADDRESS, ST2, 1, &ST2_Bit);//読み出し準備ができたか確認 |
ponpoko1939 | 0:ae23b58fc2d4 | 79 | if(ST2_Bit & 0x01){ //ちゃんと読めたかをST2レジスタの値を読んで確認 |
ponpoko1939 | 0:ae23b58fc2d4 | 80 | i2cRead(AK8963_ADDRESS, 0x03, 7, magneticdata); |
ponpoko1939 | 0:ae23b58fc2d4 | 81 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 82 | else pc.printf("ERROR!!\n"); |
ponpoko1939 | 0:ae23b58fc2d4 | 83 | *mx = (magneticdata[0] << 8) | magneticdata[1]; |
ponpoko1939 | 0:ae23b58fc2d4 | 84 | *my = (magneticdata[2] << 8) | magneticdata[3]; |
ponpoko1939 | 0:ae23b58fc2d4 | 85 | *mz = (magneticdata[4] << 8) | magneticdata[5]; |
ponpoko1939 | 0:ae23b58fc2d4 | 86 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 87 | |
ponpoko1939 | 0:ae23b58fc2d4 | 88 | //mpu9250からデータを取得(bytesに受け取るデータのバイト数、dataに実際のデータを挿入していく) |
ponpoko1939 | 0:ae23b58fc2d4 | 89 | void i2cRead(uint8_t addr,uint8_t reg, uint8_t bytes,uint8_t* data){ |
ponpoko1939 | 0:ae23b58fc2d4 | 90 | char cmd[1]; |
ponpoko1939 | 0:ae23b58fc2d4 | 91 | char written_data[14]; |
ponpoko1939 | 0:ae23b58fc2d4 | 92 | cmd[0] = reg; |
ponpoko1939 | 0:ae23b58fc2d4 | 93 | i2c.write(addr, cmd, 1, 1); |
ponpoko1939 | 0:ae23b58fc2d4 | 94 | i2c.read(addr, written_data, bytes, 0); |
ponpoko1939 | 0:ae23b58fc2d4 | 95 | for(int ii = 0; ii < bytes; ii++) { |
ponpoko1939 | 0:ae23b58fc2d4 | 96 | data[ii] = written_data[ii]; |
ponpoko1939 | 0:ae23b58fc2d4 | 97 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 98 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 99 | |
ponpoko1939 | 0:ae23b58fc2d4 | 100 | //mpu9250にデータを送信(dataに送信するデータを入力する) |
ponpoko1939 | 0:ae23b58fc2d4 | 101 | void i2cWrite(uint8_t addr,uint8_t reg, uint8_t data){ |
ponpoko1939 | 0:ae23b58fc2d4 | 102 | char cmd[2]; |
ponpoko1939 | 0:ae23b58fc2d4 | 103 | cmd[0] = reg; //レジスタ指定 |
ponpoko1939 | 0:ae23b58fc2d4 | 104 | cmd[1] = data; //送信するデータ |
ponpoko1939 | 0:ae23b58fc2d4 | 105 | i2c.write(addr, cmd, 2); //レジスタ指定はどうする? |
ponpoko1939 | 0:ae23b58fc2d4 | 106 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 107 | /* |
ponpoko1939 | 0:ae23b58fc2d4 | 108 | void readMagData(int16_t * destination) |
ponpoko1939 | 0:ae23b58fc2d4 | 109 | { |
ponpoko1939 | 0:ae23b58fc2d4 | 110 | uint8_t rawData[7]; // x/y/z gyro register data, ST2 register stored here, must read ST2 at end of data acquisition |
ponpoko1939 | 0:ae23b58fc2d4 | 111 | if(readByte(AK8963_ADDRESS, AK8963_ST1) & 0x01) { // wait for magnetometer data ready bit to be set |
ponpoko1939 | 0:ae23b58fc2d4 | 112 | readBytes(AK8963_ADDRESS, AK8963_XOUT_L, 7, &rawData[0]); // Read the six raw data and ST2 registers sequentially into data array |
ponpoko1939 | 0:ae23b58fc2d4 | 113 | uint8_t c = rawData[6]; // End data read by reading ST2 register |
ponpoko1939 | 0:ae23b58fc2d4 | 114 | if(!(c & 0x08)) { // Check if magnetic sensor overflow set, if not then report data |
ponpoko1939 | 0:ae23b58fc2d4 | 115 | destination[0] = (int16_t)(((int16_t)rawData[1] << 8) | rawData[0]); // Turn the MSB and LSB into a signed 16-bit value |
ponpoko1939 | 0:ae23b58fc2d4 | 116 | destination[1] = (int16_t)(((int16_t)rawData[3] << 8) | rawData[2]) ; // Data stored as little Endian |
ponpoko1939 | 0:ae23b58fc2d4 | 117 | destination[2] = (int16_t)(((int16_t)rawData[5] << 8) | rawData[4]) ; |
ponpoko1939 | 0:ae23b58fc2d4 | 118 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 119 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 120 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 121 | |
ponpoko1939 | 0:ae23b58fc2d4 | 122 | void getMres() { |
ponpoko1939 | 0:ae23b58fc2d4 | 123 | switch (Mscale) |
ponpoko1939 | 0:ae23b58fc2d4 | 124 | { |
ponpoko1939 | 0:ae23b58fc2d4 | 125 | // Possible magnetometer scales (and their register bit settings) are: |
ponpoko1939 | 0:ae23b58fc2d4 | 126 | // 14 bit resolution (0) and 16 bit resolution (1) |
ponpoko1939 | 0:ae23b58fc2d4 | 127 | case MFS_14BITS: |
ponpoko1939 | 0:ae23b58fc2d4 | 128 | mRes = 10.0*4912.0/8190.0; // Proper scale to return milliGauss |
ponpoko1939 | 0:ae23b58fc2d4 | 129 | break; |
ponpoko1939 | 0:ae23b58fc2d4 | 130 | case MFS_16BITS: |
ponpoko1939 | 0:ae23b58fc2d4 | 131 | mRes = 10.0*4912.0/32760.0; // Proper scale to return milliGauss |
ponpoko1939 | 0:ae23b58fc2d4 | 132 | break; |
ponpoko1939 | 0:ae23b58fc2d4 | 133 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 134 | } |
ponpoko1939 | 0:ae23b58fc2d4 | 135 | */ |
ponpoko1939 | 0:ae23b58fc2d4 | 136 | |
ponpoko1939 | 0:ae23b58fc2d4 | 137 | //Who_am_Iアドレスで接続確認ができる。0x48もしくは10進数で72が返ってくればok |
ponpoko1939 | 0:ae23b58fc2d4 | 138 | uint8_t IDcheck(){ |
ponpoko1939 | 0:ae23b58fc2d4 | 139 | uint8_t address; |
ponpoko1939 | 0:ae23b58fc2d4 | 140 | i2cRead(AK8963_ADDRESS, Magadd, 1, &address); |
ponpoko1939 | 0:ae23b58fc2d4 | 141 | return address; |
ponpoko1939 | 0:ae23b58fc2d4 | 142 | } |