![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
I2C通信を使用した角速度測定、LCD表示サンプル
Dependencies: mbed mbed_mpu6050_i2c_raw_register AQM0802
main.cpp@3:ebd656a9c89f, 2018-12-13 (annotated)
- Committer:
- satoshi1204
- Date:
- Thu Dec 13 15:35:41 2018 +0000
- Revision:
- 3:ebd656a9c89f
- Parent:
- 2:f2db52a742b0
- Child:
- 4:bf507fe18cb2
fix register address.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
satoshi1204 | 0:abd3a0fd55a9 | 1 | #include "mbed.h" |
satoshi1204 | 0:abd3a0fd55a9 | 2 | #include "mpu6050.h" |
satoshi1204 | 0:abd3a0fd55a9 | 3 | |
satoshi1204 | 0:abd3a0fd55a9 | 4 | Serial pc(USBTX, USBRX); |
satoshi1204 | 1:6f743fe9a027 | 5 | DigitalOut led1(LED1); |
satoshi1204 | 1:6f743fe9a027 | 6 | DigitalOut led2(LED2); |
satoshi1204 | 1:6f743fe9a027 | 7 | DigitalOut led3(LED3); |
satoshi1204 | 1:6f743fe9a027 | 8 | DigitalOut led4(LED4); |
satoshi1204 | 0:abd3a0fd55a9 | 9 | MPU6050 mpu(p9, p10); |
satoshi1204 | 0:abd3a0fd55a9 | 10 | |
satoshi1204 | 3:ebd656a9c89f | 11 | void turnOffAllLed() |
satoshi1204 | 2:f2db52a742b0 | 12 | { |
satoshi1204 | 2:f2db52a742b0 | 13 | led1 = 0; |
satoshi1204 | 2:f2db52a742b0 | 14 | led2 = 0; |
satoshi1204 | 2:f2db52a742b0 | 15 | led3 = 0; |
satoshi1204 | 2:f2db52a742b0 | 16 | led4 = 0; |
satoshi1204 | 2:f2db52a742b0 | 17 | } |
satoshi1204 | 2:f2db52a742b0 | 18 | |
satoshi1204 | 0:abd3a0fd55a9 | 19 | void readData() |
satoshi1204 | 0:abd3a0fd55a9 | 20 | { |
satoshi1204 | 0:abd3a0fd55a9 | 21 | double ax, ay, az; |
satoshi1204 | 0:abd3a0fd55a9 | 22 | mpu.readAccelemeter(ax, ay, az); |
satoshi1204 | 0:abd3a0fd55a9 | 23 | double gx, gy, gz; |
satoshi1204 | 0:abd3a0fd55a9 | 24 | mpu.readGyroscope(gx, gy, gz); |
satoshi1204 | 0:abd3a0fd55a9 | 25 | pc.printf("%.4lf %.4lf %.4lf %.4lf %.4lf %.4lf\r\n", ax, ay, az, gx, gy, gz); |
satoshi1204 | 3:ebd656a9c89f | 26 | turnOffAllLed(); |
satoshi1204 | 2:f2db52a742b0 | 27 | if(ax > ay && ax > az) { |
satoshi1204 | 2:f2db52a742b0 | 28 | led1 = 1; |
satoshi1204 | 2:f2db52a742b0 | 29 | } else if(ay > ax && ay > az) { |
satoshi1204 | 2:f2db52a742b0 | 30 | led2 = 1; |
satoshi1204 | 2:f2db52a742b0 | 31 | } else if(az > ax && az > ay) { |
satoshi1204 | 2:f2db52a742b0 | 32 | led3 = 1; |
satoshi1204 | 2:f2db52a742b0 | 33 | } else { |
satoshi1204 | 2:f2db52a742b0 | 34 | led4 = 1; |
satoshi1204 | 2:f2db52a742b0 | 35 | } |
satoshi1204 | 0:abd3a0fd55a9 | 36 | } |
satoshi1204 | 0:abd3a0fd55a9 | 37 | |
satoshi1204 | 0:abd3a0fd55a9 | 38 | int main() { |
satoshi1204 | 3:ebd656a9c89f | 39 | pc.baud(115200); |
satoshi1204 | 0:abd3a0fd55a9 | 40 | mpu.setMaxScale(MAX_ACC_8G, MAX_GYRO_1000degpersec); |
satoshi1204 | 3:ebd656a9c89f | 41 | while(true) { |
satoshi1204 | 0:abd3a0fd55a9 | 42 | readData(); |
satoshi1204 | 0:abd3a0fd55a9 | 43 | wait(0.2); |
satoshi1204 | 0:abd3a0fd55a9 | 44 | } |
satoshi1204 | 0:abd3a0fd55a9 | 45 | } |