2020 Off season development Test of MPU6050 and OLED display on the Nucleo Board STM32 F303K8 and SSD1306. MPU6050 and OLED Display are Implemented on one circuit board.
Dependencies: mbed MPU6050 Adafruit_GFX
main.cpp
00001 #include "mbed.h" 00002 #include "MPU6050.h" 00003 #include "Adafruit_SSD1306.h" 00004 #include "Adafruit_GFX_Config.h" 00005 #include <string> 00006 00007 #define NUM 10 00008 00009 DigitalOut myled(LED1); 00010 Serial pc(USBTX, USBRX,115200); 00011 00012 MPU6050 mpu; 00013 00014 DigitalIn Slides[] = { 00015 DigitalIn( D1 ), 00016 DigitalIn( D0 ), 00017 DigitalIn( D3 ), 00018 DigitalIn( D6 ) 00019 }; 00020 00021 DigitalIn Tacts[] = { 00022 DigitalIn( D8 ), 00023 DigitalIn( D9 ), 00024 DigitalIn( D11 ), 00025 DigitalIn( D12 ) 00026 }; 00027 00028 DigitalOut leds[] = { 00029 DigitalOut( A3 ), 00030 DigitalOut( A0 ), 00031 DigitalOut( D13 ), 00032 DigitalOut( A2 ) 00033 }; 00034 00035 // OLEDとの通信に使用するI2Cオブジェクトを生成 00036 I2C i2c(PB_7,PB_6); 00037 // OLED制御クラスのインスタンス化 00038 uint8_t i2cAddress = SSD_I2C_ADDRESS; 00039 int rawHeight = 32; 00040 int rawWidth = 128; 00041 Adafruit_SSD1306_I2c oled(i2c, D10, i2cAddress); 00042 00043 void print(std::string str); 00044 00045 int16_t ax, ay, az; 00046 int16_t gx, gy, gz; 00047 00048 int sum_ax,sum_ay,sum_az; 00049 int sum_gx,sum_gy,sum_gz; 00050 char str_ax[15],str_ay[15],str_az[15]; 00051 char str_gx[15],str_gy[15],str_gz[15]; 00052 00053 int main() 00054 { 00055 // char a[]= {"HELLO"}; 00056 // char b[]= {" WORLD"}; 00057 // char c[]= {"\n"}; 00058 char title1[] = {"AC[m/ss]"}; 00059 char title2[] = {"GY[rad/s]"}; 00060 char but=0x00; 00061 for(int i=0; i<4; i++) { 00062 Slides[i].mode(PullUp); 00063 Tacts[i].mode(PullUp); 00064 } 00065 pc.printf("\033[2J"); 00066 pc.printf("MPU6050 test\n\n"); 00067 pc.printf("MPU6050 initialize \n"); 00068 00069 mpu.initialize(); 00070 pc.printf("MPU6050 testConnection \n"); 00071 00072 bool mpu6050TestResult = mpu.testConnection(); 00073 if(mpu6050TestResult) { 00074 pc.printf("MPU6050 test passed \n"); 00075 } else { 00076 pc.printf("MPU6050 test failed \n"); 00077 } 00078 00079 pc.printf("\033[2J"); 00080 pc.printf("\033[1;1HACCEL_x: 0.0000[m/s^2]"); 00081 pc.printf("\033[2;1HACCEL_y: 0.0000[m/s^2]"); 00082 pc.printf("\033[3;1HACCEL_z: 0.0000[m/s^2]"); 00083 00084 pc.printf("\033[5;1HGYRO_x : 0.0000[rad/s]"); 00085 pc.printf("\033[6;1HGYRO_y : 0.0000[rad/s]"); 00086 pc.printf("\033[7;1HGYRO_z : 0.0000[rad/s]"); 00087 oled.clearDisplay(); 00088 oled.setTextCursor(0,0); 00089 print(title1); 00090 oled.setTextCursor(64,0); 00091 print(title2); 00092 while(1) { 00093 myled = !myled; 00094 for(int i=0; i < NUM; i++) { 00095 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); 00096 sum_ax += ax; 00097 sum_ay += ay; 00098 sum_az += az; 00099 00100 sum_gx += gx; 00101 sum_gy += gy; 00102 sum_gz += gz; 00103 } 00104 00105 for(int i=0; i<4; i++) { 00106 if ( !(Slides[i]) ) but |= (0x01<<i); 00107 if ( !(Tacts[i]) ) but |= (0x10<<i); 00108 if( Slides[i] ^ Tacts[i] ) leds[i] = 0; 00109 else leds[i] = 1; 00110 } 00111 00112 sprintf(str_ax, "%+08.5g", float(sum_ax)/(2048*NUM)); 00113 sprintf(str_ay, "%+08.5g", float(sum_ay)/(2048*NUM)); 00114 sprintf(str_az, "%+08.5g", float(sum_az)/(2048*NUM)); 00115 00116 sprintf(str_gx, "%+08.5g", float(sum_gx)/(16.4*NUM)); 00117 sprintf(str_gy, "%+08.5g", float(sum_gy)/(16.4*NUM)); 00118 sprintf(str_gz, "%+08.5g", float(sum_gz)/(16.4*NUM)); 00119 00120 oled.setTextCursor(0,8); 00121 print(str_ax); 00122 oled.setTextCursor(0,16); 00123 print(str_ay); 00124 oled.setTextCursor(0,24); 00125 print(str_az); 00126 00127 oled.setTextCursor(64,8); 00128 print(str_gx); 00129 oled.setTextCursor(64,16); 00130 print(str_gy); 00131 oled.setTextCursor(64,24); 00132 print(str_gz); 00133 00134 // pc.printf("\033[1;10H%s",str_ax); 00135 // pc.printf("\033[2;10H%s",str_ay); 00136 // pc.printf("\033[3;10H%s",str_az); 00137 // 00138 // pc.printf("\033[5;9H%s",str_gx); 00139 // pc.printf("\033[6;9H%s",str_gy); 00140 // pc.printf("\033[7;9H%s",str_gz); 00141 // pc.printf("\033[8;1Hbut:0x%x",but); 00142 00143 sum_ax = 0; 00144 sum_ay = 0; 00145 sum_az = 0; 00146 00147 sum_gx = 0; 00148 sum_gy = 0; 00149 sum_gz = 0; 00150 00151 but=0x00; 00152 } 00153 } 00154 00155 void print(std::string str) 00156 { 00157 for(size_t i=0; i<str.size() ; i++) { 00158 oled._putc(str[i]); 00159 } 00160 // 表示を更新 00161 oled.display(); 00162 }
Generated on Mon Aug 8 2022 14:57:42 by
1.7.2