2020/08/26
Dependents: STM32_MR_NOT_IM_REV2 STM32_MR_NOT_IM_REV2 STM32_MR_NOT_IM_REV2_829 STM32_MR_include_IM_rev2
Revision 3:58349cbf9ed9, committed 2020-08-31
- Comitter:
- falconsyunya
- Date:
- Mon Aug 31 11:21:16 2020 +0000
- Parent:
- 2:7e049ebd96ab
- Commit message:
- 2020/08/31
Changed in this revision
BMX055.cpp | Show annotated file Show diff for this revision Revisions of this file |
BMX055.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 7e049ebd96ab -r 58349cbf9ed9 BMX055.cpp --- a/BMX055.cpp Tue Aug 25 17:28:06 2020 +0000 +++ b/BMX055.cpp Mon Aug 31 11:21:16 2020 +0000 @@ -1,43 +1,65 @@ #include "mbed.h" #include "BMX055.h" -BMX055::BMX055(PinName SDA, PinName SCL) : bmx055(SDA,SCL){ +BMX055::BMX055(PinName SDA, PinName SCL) : bmx055(SDA,SCL) +{ bmx055.frequency(100000); bmx_init(); } void BMX055::bmx_init(void) { - char buf[2]={0}; + char buf[2]= {0}; printf("accel setting\r\n"); - buf[0]=0x14; buf[1]=0xB6; - bmx055.write(ACC,buf,2); wait_ms(2); - buf[0]=0x0F; buf[1]=0x05; - buf[0]=0x10; buf[1]=0x08; + buf[0]=0x14; + buf[1]=0xB6; + bmx055.write(ACC,buf,2); + wait_ms(2); + buf[0]=0x0F; + buf[1]=0x11;//加速度の計測レンジの設定 + bmx055.write(ACC,buf,2); + buf[0]=0x10; + buf[1]=0x08; + bmx055.write(ACC,buf,2); + buf[0]=0x11; + buf[1]=0x00; bmx055.write(ACC,buf,2); - buf[0]=0x11; buf[1]=0x00; - bmx055.write(ACC,buf,2); wait_ms(2); - + wait_ms(2); + printf("gyroscope setting\r\n"); - buf[0]=0x0F; buf[1]=0x04; - bmx055.write(GYRO,buf,2); wait(0.1); - buf[0]=0x10; buf[1]=0x07; - bmx055.write(GYRO,buf,2); wait(0.1); - buf[0]=0x11; buf[1]=0x00; - bmx055.write(GYRO,buf,2); wait(0.1); + buf[0]=0x0F; + buf[1]=0x04;//角加速度の計測レンジの設定 + bmx055.write(GYRO,buf,2); + wait(0.1); + buf[0]=0x10; + buf[1]=0x07; + bmx055.write(GYRO,buf,2); + wait(0.1); + buf[0]=0x11; + buf[1]=0x00; + bmx055.write(GYRO,buf,2); + wait(0.1); printf("magnet setting\r\n"); - buf[0]=0x4B; buf[1]=0x82; - bmx055.write(MAG,buf,2); wait(0.1); - buf[0]=0x4B; buf[1]=0x01; - bmx055.write(MAG,buf,2); wait(0.1); - buf[0]=0x4C; buf[1]=0x00; + buf[0]=0x4B; + buf[1]=0x82; + bmx055.write(MAG,buf,2); + wait(0.1); + buf[0]=0x4B; + buf[1]=0x01; bmx055.write(MAG,buf,2); - buf[0]=0x4E; buf[1]=0x84; + wait(0.1); + buf[0]=0x4C; + buf[1]=0x00; bmx055.write(MAG,buf,2); - buf[0]=0x51; buf[1]=0x04; + buf[0]=0x4E; + buf[1]=0x84; bmx055.write(MAG,buf,2); - buf[0]=0x52; buf[1]=0x16; + buf[0]=0x51; + buf[1]=0x04; + bmx055.write(MAG,buf,2); + buf[0]=0x52; + buf[1]=0x16; bmx055.write(MAG,buf,2); wait(0.1); @@ -46,60 +68,39 @@ bmx055.read(MAG,buf,1); printf("read:0x%02x\r\n",buf[0]); } - - void BMX055::getAcc(void){ - uint8_t data[6]={0}; - char send[1], get[1]; - char temp; - send[0]=(char)(2); - bmx055.write(ACC,send,1,true); +void BMX055::getAcc(void) +{ + uint8_t data[6]= {0}; + char reg = 0x02; + bmx055.write(ACC,®,1,true); bmx055.read(ACC,(char*)data,6); - for(int i=0;i<3;i++){ - accel[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 4; - //if(accel[i]>2047)accel[i]-=4096; + for(int i=0; i<3; i++) { + accel[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 4;//ビット演算 } } -void BMX055::getGyro(void){ - int data[6]={0}; - char send[1],get[1]; - char temp; - - for(int i=0;i<6;i++){ - send[0]=(char)(2+i); - bmx055.write(GYRO,send,1); - bmx055.read(GYRO,get,1); - temp=get[0]; - data[i]=temp; - } +void BMX055::getGyro(void) +{ + uint8_t data[6]= {0}; + char reg = 0x02; + bmx055.write(GYRO,®,1,true); + bmx055.read(GYRO,(char*)data,6); - for(int i=0;i<3;i++){ - gyroscope[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 4; - if(gyroscope[i]>32767)gyroscope[i]-=65536; -// gyroscope[i]=gyroscope[i]*0.0038; + for(int i=0; i<3; i++) { + gyroscope[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]); } } -void BMX055::getMag(void){ - int data[8]={0}; - char send[1],get[1]; - char temp; +void BMX055::getMag(void) +{ + uint8_t data[8]= {0}; + char reg = 0x42; + bmx055.write(MAG,®,1,true); + bmx055.read(MAG,(char*)data,8); - for(int i=0;i<8;i++){ - send[0]=(char)(0x42+i); - bmx055.write(MAG,send,1); - bmx055.read(MAG,get,1); -// printf("%02X ",get[0]); - temp=get[0]; - data[i]=temp; - } - - for(int i=0;i<3;i++){ - if(i!=2)magnet[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 3; - else magnet[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 1; - if(i==2 && magnet[i]>16383)magnet[i]-=32768; - else if(i!=2 && magnet[i]>4095)magnet[i]-=8092; - } + magnet[0]=(int16_t)(((int16_t)data[1]<<8) | data[0]) >> 3; + magnet[1]=(int16_t)(((int16_t)data[3]<<8) | data[2]) >> 3; + magnet[2]=(int16_t)(((int16_t)data[5]<<8) | data[4]) >> 1; }
diff -r 7e049ebd96ab -r 58349cbf9ed9 BMX055.h --- a/BMX055.h Tue Aug 25 17:28:06 2020 +0000 +++ b/BMX055.h Mon Aug 31 11:21:16 2020 +0000 @@ -1,86 +1,17 @@ -/* -宣言方法 -BMX055 bmx(SDA_pin,SCL_pin); - -bmx.getAcc(); -bmx.getGyro(); -bmx.getMag(); で値をゲット! - -値は -bmx.accel[3]; -bmx.gyroscope[3]; -bmx.magnet[3]; に入っている! - - - -用例----------------------------------------------------------- -#include "mbed.h" -#include "BMX055.h" - - -BMX055 bmx(D14,D15); //SDA SCL -DigitalOut myled(LED1); -DigitalIn sw(PC_13); -Timer t; - -int main() { - float preAngle=0, AngGyr=0, gy=0, acc[2]={0}, angleAccel=0; - int angle=0, use_angle=0; - const double dt = 0.01; - - printf("\r\nrun\r\n"); - - while(sw==0){} - while(sw==1){} - while(1) { - -// getAcc(accel); -// printf("%2.4f, %2.4f, %2.4f\r\n",(accel[0]/512)*9.8,(accel[1]/512)*9.8,(accel[2]/512)*9.8); -// getGyro(gyroscope); -// printf("%2.4f, %2.4f, %2.4f\r\n",gyroscope[0]*125/2048,gyroscope[1]*125/2048,gyroscope[2]*125/2048); -// getMag(magnet); -// printf("%2.4f, %2.4f, %2.4f\r\n",magnet[0],magnet[1],magnet[2]); - - bmx.getGyro(); - - while(sw==0){ - angle=0; - use_angle=0; - printf("Setting now!\r\n"); - } - - gy=bmx.gyroscope[2]*125/2048; - angleAccel=bmx.gyroscope[2]*125/2560; - AngGyr = preAngle+gy*dt; - angle = (int)(0.905*AngGyr+0.09*angleAccel); //回転の検知幅小さくすると遅いのができて、大きくすると早いのができる - //回転の検知制度を調整:上と反比例 - use_angle += angle; - - printf("%3d\r\n", use_angle/48); - preAngle=angle; - acc[1]=acc[0]; - - wait(dt); - } -} ----------------------------------------------------------------------- -回転角がわかるよV -*/ - #ifndef BMX055_H_ #define BMX055_H_ -#define ACC 0x19<<1 +#define ACC 0x19<<1 #define GYRO 0x69<<1 -#define MAG 0x13<<1 +#define MAG 0x13<<1 class BMX055 { public: BMX055(PinName SDA, PinName SCL); - float accel[3]; - float gyroscope[3]; - float magnet[3]; + int accel[3]; + int gyroscope[3]; + int magnet[3]; void getAcc(void); void getGyro(void); void getMag(void);