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

Committer:
st17099ng
Date:
Thu Mar 12 15:55:31 2020 +0000
Revision:
0:c7160959f043
Initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
st17099ng 0:c7160959f043 1 #include "mbed.h"
st17099ng 0:c7160959f043 2 #include "MPU6050.h"
st17099ng 0:c7160959f043 3 #include "Adafruit_SSD1306.h"
st17099ng 0:c7160959f043 4 #include "Adafruit_GFX_Config.h"
st17099ng 0:c7160959f043 5 #include <string>
st17099ng 0:c7160959f043 6
st17099ng 0:c7160959f043 7 #define NUM 10
st17099ng 0:c7160959f043 8
st17099ng 0:c7160959f043 9 DigitalOut myled(LED1);
st17099ng 0:c7160959f043 10 Serial pc(USBTX, USBRX,115200);
st17099ng 0:c7160959f043 11
st17099ng 0:c7160959f043 12 MPU6050 mpu;
st17099ng 0:c7160959f043 13
st17099ng 0:c7160959f043 14 DigitalIn Slides[] = {
st17099ng 0:c7160959f043 15 DigitalIn( D1 ),
st17099ng 0:c7160959f043 16 DigitalIn( D0 ),
st17099ng 0:c7160959f043 17 DigitalIn( D3 ),
st17099ng 0:c7160959f043 18 DigitalIn( D6 )
st17099ng 0:c7160959f043 19 };
st17099ng 0:c7160959f043 20
st17099ng 0:c7160959f043 21 DigitalIn Tacts[] = {
st17099ng 0:c7160959f043 22 DigitalIn( D8 ),
st17099ng 0:c7160959f043 23 DigitalIn( D9 ),
st17099ng 0:c7160959f043 24 DigitalIn( D11 ),
st17099ng 0:c7160959f043 25 DigitalIn( D12 )
st17099ng 0:c7160959f043 26 };
st17099ng 0:c7160959f043 27
st17099ng 0:c7160959f043 28 DigitalOut leds[] = {
st17099ng 0:c7160959f043 29 DigitalOut( A3 ),
st17099ng 0:c7160959f043 30 DigitalOut( A0 ),
st17099ng 0:c7160959f043 31 DigitalOut( D13 ),
st17099ng 0:c7160959f043 32 DigitalOut( A2 )
st17099ng 0:c7160959f043 33 };
st17099ng 0:c7160959f043 34
st17099ng 0:c7160959f043 35 // OLEDとの通信に使用するI2Cオブジェクトを生成
st17099ng 0:c7160959f043 36 I2C i2c(PB_7,PB_6);
st17099ng 0:c7160959f043 37 // OLED制御クラスのインスタンス化
st17099ng 0:c7160959f043 38 uint8_t i2cAddress = SSD_I2C_ADDRESS;
st17099ng 0:c7160959f043 39 int rawHeight = 32;
st17099ng 0:c7160959f043 40 int rawWidth = 128;
st17099ng 0:c7160959f043 41 Adafruit_SSD1306_I2c oled(i2c, D10, i2cAddress);
st17099ng 0:c7160959f043 42
st17099ng 0:c7160959f043 43 void print(std::string str);
st17099ng 0:c7160959f043 44
st17099ng 0:c7160959f043 45 int16_t ax, ay, az;
st17099ng 0:c7160959f043 46 int16_t gx, gy, gz;
st17099ng 0:c7160959f043 47
st17099ng 0:c7160959f043 48 int sum_ax,sum_ay,sum_az;
st17099ng 0:c7160959f043 49 int sum_gx,sum_gy,sum_gz;
st17099ng 0:c7160959f043 50 char str_ax[15],str_ay[15],str_az[15];
st17099ng 0:c7160959f043 51 char str_gx[15],str_gy[15],str_gz[15];
st17099ng 0:c7160959f043 52
st17099ng 0:c7160959f043 53 int main()
st17099ng 0:c7160959f043 54 {
st17099ng 0:c7160959f043 55 // char a[]= {"HELLO"};
st17099ng 0:c7160959f043 56 // char b[]= {" WORLD"};
st17099ng 0:c7160959f043 57 // char c[]= {"\n"};
st17099ng 0:c7160959f043 58 char title1[] = {"AC[m/ss]"};
st17099ng 0:c7160959f043 59 char title2[] = {"GY[rad/s]"};
st17099ng 0:c7160959f043 60 char but=0x00;
st17099ng 0:c7160959f043 61 for(int i=0; i<4; i++) {
st17099ng 0:c7160959f043 62 Slides[i].mode(PullUp);
st17099ng 0:c7160959f043 63 Tacts[i].mode(PullUp);
st17099ng 0:c7160959f043 64 }
st17099ng 0:c7160959f043 65 pc.printf("\033[2J");
st17099ng 0:c7160959f043 66 pc.printf("MPU6050 test\n\n");
st17099ng 0:c7160959f043 67 pc.printf("MPU6050 initialize \n");
st17099ng 0:c7160959f043 68
st17099ng 0:c7160959f043 69 mpu.initialize();
st17099ng 0:c7160959f043 70 pc.printf("MPU6050 testConnection \n");
st17099ng 0:c7160959f043 71
st17099ng 0:c7160959f043 72 bool mpu6050TestResult = mpu.testConnection();
st17099ng 0:c7160959f043 73 if(mpu6050TestResult) {
st17099ng 0:c7160959f043 74 pc.printf("MPU6050 test passed \n");
st17099ng 0:c7160959f043 75 } else {
st17099ng 0:c7160959f043 76 pc.printf("MPU6050 test failed \n");
st17099ng 0:c7160959f043 77 }
st17099ng 0:c7160959f043 78
st17099ng 0:c7160959f043 79 pc.printf("\033[2J");
st17099ng 0:c7160959f043 80 pc.printf("\033[1;1HACCEL_x: 0.0000[m/s^2]");
st17099ng 0:c7160959f043 81 pc.printf("\033[2;1HACCEL_y: 0.0000[m/s^2]");
st17099ng 0:c7160959f043 82 pc.printf("\033[3;1HACCEL_z: 0.0000[m/s^2]");
st17099ng 0:c7160959f043 83
st17099ng 0:c7160959f043 84 pc.printf("\033[5;1HGYRO_x : 0.0000[rad/s]");
st17099ng 0:c7160959f043 85 pc.printf("\033[6;1HGYRO_y : 0.0000[rad/s]");
st17099ng 0:c7160959f043 86 pc.printf("\033[7;1HGYRO_z : 0.0000[rad/s]");
st17099ng 0:c7160959f043 87 oled.clearDisplay();
st17099ng 0:c7160959f043 88 oled.setTextCursor(0,0);
st17099ng 0:c7160959f043 89 print(title1);
st17099ng 0:c7160959f043 90 oled.setTextCursor(64,0);
st17099ng 0:c7160959f043 91 print(title2);
st17099ng 0:c7160959f043 92 while(1) {
st17099ng 0:c7160959f043 93 myled = !myled;
st17099ng 0:c7160959f043 94 for(int i=0; i < NUM; i++) {
st17099ng 0:c7160959f043 95 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
st17099ng 0:c7160959f043 96 sum_ax += ax;
st17099ng 0:c7160959f043 97 sum_ay += ay;
st17099ng 0:c7160959f043 98 sum_az += az;
st17099ng 0:c7160959f043 99
st17099ng 0:c7160959f043 100 sum_gx += gx;
st17099ng 0:c7160959f043 101 sum_gy += gy;
st17099ng 0:c7160959f043 102 sum_gz += gz;
st17099ng 0:c7160959f043 103 }
st17099ng 0:c7160959f043 104
st17099ng 0:c7160959f043 105 for(int i=0; i<4; i++) {
st17099ng 0:c7160959f043 106 if ( !(Slides[i]) ) but |= (0x01<<i);
st17099ng 0:c7160959f043 107 if ( !(Tacts[i]) ) but |= (0x10<<i);
st17099ng 0:c7160959f043 108 if( Slides[i] ^ Tacts[i] ) leds[i] = 0;
st17099ng 0:c7160959f043 109 else leds[i] = 1;
st17099ng 0:c7160959f043 110 }
st17099ng 0:c7160959f043 111
st17099ng 0:c7160959f043 112 sprintf(str_ax, "%+08.5g", float(sum_ax)/(2048*NUM));
st17099ng 0:c7160959f043 113 sprintf(str_ay, "%+08.5g", float(sum_ay)/(2048*NUM));
st17099ng 0:c7160959f043 114 sprintf(str_az, "%+08.5g", float(sum_az)/(2048*NUM));
st17099ng 0:c7160959f043 115
st17099ng 0:c7160959f043 116 sprintf(str_gx, "%+08.5g", float(sum_gx)/(16.4*NUM));
st17099ng 0:c7160959f043 117 sprintf(str_gy, "%+08.5g", float(sum_gy)/(16.4*NUM));
st17099ng 0:c7160959f043 118 sprintf(str_gz, "%+08.5g", float(sum_gz)/(16.4*NUM));
st17099ng 0:c7160959f043 119
st17099ng 0:c7160959f043 120 oled.setTextCursor(0,8);
st17099ng 0:c7160959f043 121 print(str_ax);
st17099ng 0:c7160959f043 122 oled.setTextCursor(0,16);
st17099ng 0:c7160959f043 123 print(str_ay);
st17099ng 0:c7160959f043 124 oled.setTextCursor(0,24);
st17099ng 0:c7160959f043 125 print(str_az);
st17099ng 0:c7160959f043 126
st17099ng 0:c7160959f043 127 oled.setTextCursor(64,8);
st17099ng 0:c7160959f043 128 print(str_gx);
st17099ng 0:c7160959f043 129 oled.setTextCursor(64,16);
st17099ng 0:c7160959f043 130 print(str_gy);
st17099ng 0:c7160959f043 131 oled.setTextCursor(64,24);
st17099ng 0:c7160959f043 132 print(str_gz);
st17099ng 0:c7160959f043 133
st17099ng 0:c7160959f043 134 // pc.printf("\033[1;10H%s",str_ax);
st17099ng 0:c7160959f043 135 // pc.printf("\033[2;10H%s",str_ay);
st17099ng 0:c7160959f043 136 // pc.printf("\033[3;10H%s",str_az);
st17099ng 0:c7160959f043 137 //
st17099ng 0:c7160959f043 138 // pc.printf("\033[5;9H%s",str_gx);
st17099ng 0:c7160959f043 139 // pc.printf("\033[6;9H%s",str_gy);
st17099ng 0:c7160959f043 140 // pc.printf("\033[7;9H%s",str_gz);
st17099ng 0:c7160959f043 141 // pc.printf("\033[8;1Hbut:0x%x",but);
st17099ng 0:c7160959f043 142
st17099ng 0:c7160959f043 143 sum_ax = 0;
st17099ng 0:c7160959f043 144 sum_ay = 0;
st17099ng 0:c7160959f043 145 sum_az = 0;
st17099ng 0:c7160959f043 146
st17099ng 0:c7160959f043 147 sum_gx = 0;
st17099ng 0:c7160959f043 148 sum_gy = 0;
st17099ng 0:c7160959f043 149 sum_gz = 0;
st17099ng 0:c7160959f043 150
st17099ng 0:c7160959f043 151 but=0x00;
st17099ng 0:c7160959f043 152 }
st17099ng 0:c7160959f043 153 }
st17099ng 0:c7160959f043 154
st17099ng 0:c7160959f043 155 void print(std::string str)
st17099ng 0:c7160959f043 156 {
st17099ng 0:c7160959f043 157 for(size_t i=0; i<str.size() ; i++) {
st17099ng 0:c7160959f043 158 oled._putc(str[i]);
st17099ng 0:c7160959f043 159 }
st17099ng 0:c7160959f043 160 // 表示を更新
st17099ng 0:c7160959f043 161 oled.display();
st17099ng 0:c7160959f043 162 }