Read accelerometer and gyroscope measurements from the MPU-6050. The MPU-60X0 is the world’s first integrated 6-axis MotionTracking device that combines a 3-axis gyroscope, 3-axis accelerometer, and a Digital Motion Processor™ (DMP) all in a small 4x4x0.9mm package. With its dedicated I2C sensor bus, it directly accepts inputs from an external 3-axis compass to provide a complete 9-axis MotionFusion™ output.
Revision 0:1221112820f7, committed 2018-10-18
- Comitter:
- 213468891
- Date:
- Thu Oct 18 17:04:36 2018 +0000
- Commit message:
- Demonstrates how to read gyroscope and accelerometer values from the MPU-6050 module.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gitignore Thu Oct 18 17:04:36 2018 +0000 @@ -0,0 +1,4 @@ +.build +.mbed +projectfiles +*.py*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPU6050.lib Thu Oct 18 17:04:36 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/213468891/code/MPU6050/#f4905c7ef2ce
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Thu Oct 18 17:04:36 2018 +0000 @@ -0,0 +1,87 @@ +# Getting started with Blinky on mbed OS + +This guide reviews the steps required to get Blinky working on an mbed OS platform. + +Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli). + +## Import the example application + +From the command-line, import the example: + +``` +mbed import mbed-os-example-blinky +cd mbed-os-example-blinky +``` + +### Now compile + +Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5: + +``` +mbed compile -m K64F -t ARM +``` + +Your PC may take a few minutes to compile your code. At the end, you see the following result: + +``` +[snip] ++----------------------------+-------+-------+------+ +| Module | .text | .data | .bss | ++----------------------------+-------+-------+------+ +| Misc | 13939 | 24 | 1372 | +| core/hal | 16993 | 96 | 296 | +| core/rtos | 7384 | 92 | 4204 | +| features/FEATURE_IPV4 | 80 | 0 | 176 | +| frameworks/greentea-client | 1830 | 60 | 44 | +| frameworks/utest | 2392 | 512 | 292 | +| Subtotals | 42618 | 784 | 6384 | ++----------------------------+-------+-------+------+ +Allocated Heap: unknown +Allocated Stack: unknown +Total Static RAM memory (data + bss): 7168 bytes +Total RAM memory (data + bss + heap + stack): 7168 bytes +Total Flash memory (text + data + misc): 43402 bytes +Image: .\.build\K64F\ARM\mbed-os-example-blinky.bin +``` + +### Program your board + +1. Connect your mbed device to the computer over USB. +1. Copy the binary file to the mbed device. +1. Press the reset button to start the program. + +The LED on your platform turns on and off. + +## Export the project to Keil MDK, and debug your application + +From the command-line, run the following command: + +``` +mbed export -m K64F -i uvision +``` + +To debug the application: + +1. Start uVision. +1. Import the uVision project generated earlier. +1. Compile your application, and generate an `.axf` file. +1. Make sure uVision is configured to debug over CMSIS-DAP (From the Project menu > Options for Target '...' > Debug tab > Use CMSIS-DAP Debugger). +1. Set breakpoints, and start a debug session. + +![Image of uVision](img/uvision.png) + +## Troubleshooting + +1. Make sure `mbed-cli` is working correctly and its version is `>1.0.0` + + ``` + mbed --version + ``` + + If not, you can update it: + + ``` + pip install mbed-cli --upgrade + ``` + +2. If using Keil MDK, make sure you have a license installed. [MDK-Lite](http://www.keil.com/arm/mdk.asp) has a 32 KB restriction on code size. \ No newline at end of file
Binary file img/uvision.png has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Oct 18 17:04:36 2018 +0000 @@ -0,0 +1,127 @@ +/*################################################################################# + +Program Name : MPU6050 Hello World +Author : Crispin Mukalay +Date Modified : 17/10/2018 +Compiler : ARMmbed +Tested On : NUCLEO-F446RE + +Description : Demonstrates the use of the MPU6050 gryroscope/accelerometer/temperature + sensor to read gyroscope 3-axis angular velocities(°/s) and accelerometer + 3-axis accelerations (°). + +Requirements : * NUCLEO-F446RE Board + * MPU6050 Module + +Circuit : * The MPU6050 module is connected as follows: + VCC - 3.3V + GND - GND + SCL - PB10 (I2C2_SCL pin) + SDA - PB3 (I2C2_SDA pin) + +####################################################################################*/ + +#include "mbed.h" +#include "MPU6050.h" +#include <math.h> +#define pi 3.141592654 + +MPU6050 AccGyro(PB_3, PB_10); // Create an MPU object called AccGyro + +Serial pc(SERIAL_TX, SERIAL_RX); //To use the PC as a console (display output) + +int16_t Ax, Ay, Az, Gx, Gy, Gz; +float Ax_f, Ay_f, Az_f; +double Gx_f, Gy_f, Gz_f; +float Ax_f_sum, Ay_f_sum, Az_f_sum, Gx_f_sum, Gy_f_sum, Gz_f_sum; +float roll, pitch, yaw; + +int main() { + + uint16_t AccelReadings[3] = {0, 0, 0}; + uint16_t GyroReadings[3] = {0, 0, 0}; + uint8_t DevId; + + pc.printf("Starting MPU6050 test...\n"); + DevId = AccGyro.getWhoAmI(); + + if(DevId == 0x68){ + pc.printf("\n"); + pc.printf("MPU6050 detected...\n"); + pc.printf("Device ID is: 0x%02x\n", DevId); + pc.printf("\n"); + }else{ + pc.printf("\n"); + pc.printf("MPU6050 not found...\n"); + while(1); + } + + // The device will come up in sleep mode upon power-up. + AccGyro.setPowerCtl_1(0x00, 0x00, 0x00, 0x00, INT_8MHz_OSC); // Disable sleep mode + wait(.001); + + // Full scale, +/-2000°/s, 16.4LSB°/s. + AccGyro.setGyroConfig(GYRO_ST_OFF, GFS_2000dps); // Accelerometer elf-test trigger off. + wait(.001); + + // Full scale, +/-16g, 2048LSB/g. + AccGyro.setAccelConfig(ACC_ST_OFF, AFS_16g); // Gyroscope self-test trigger off. + wait(.001); + + while (true) { + + wait(0.2); + + Ax_f_sum = 0; + Ay_f_sum = 0; + Az_f_sum = 0; + Gx_f_sum = 0; + Gy_f_sum = 0; + Gz_f_sum = 0; + + for(int i = 0; i < 10; i = i + 1) // Take ten analog input readings + { + AccGyro.readAccel(AccelReadings); // Extract accelerometer measurements + AccGyro.readGyro(GyroReadings); // Extract gyroscope measurements + + // 2s complement acclerometer and gyroscope values + Ax = AccelReadings[0]; + Ay = AccelReadings[1]; + Az = AccelReadings[2]; + Gx = GyroReadings[0]; + Gy = GyroReadings[1]; + Gz = GyroReadings[2]; + + // Add every reading to the sum variables + Ax_f_sum = Ax_f_sum + (float)Ax; + Ay_f_sum = Ay_f_sum + (float)Ay; + Az_f_sum = Az_f_sum + (float)Az; + Gx_f_sum = Gx_f_sum + (float)Gx; + Gy_f_sum = Gy_f_sum + (float)Gy; + Gz_f_sum = Gz_f_sum + (float)Gz; + } + + // Divide by 10 to get the averaged value + Ax_f = Ax_f_sum / 10; + Ay_f = Ay_f_sum / 10; + Az_f = Az_f_sum / 10; + Gx_f = Gx_f_sum / 10; + Gy_f = Gy_f_sum / 10; + Gz_f = Gz_f_sum / 10; + + // 1. Calculate actual roll, pitch and yaw angles in degrees + // 2. Calibrate readings by adding or substracting the off-set + roll = (180/pi)*(atan(Ax_f/(sqrt((Ay_f*Ay_f)+(Az_f*Az_f))))) - 4.36; + pitch = (180/pi)*(atan(Ay_f/(sqrt((Ax_f*Ax_f)+(Az_f*Az_f))))) - 0.063; + yaw = (180/pi)*(atan((sqrt((Ax_f*Ax_f)+(Ay_f*Ay_f)))/Az_f)) - 3.93; + + // Convert gyroscope readings into degrees/s + Gx_f = Gx_f / 131.0; + Gy_f = Gy_f / 131.0; + Gz_f = Gz_f / 131.0; + + pc.printf("Gyro(deg/s) X: %.3f Y: %.3f Z: %.3f || Accel(deg) Roll: %.3f, Pitch: %.3f, Yaw: %.3f \n", Gx_f, Gy_f, Gz_f, roll, pitch, yaw); + + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Thu Oct 18 17:04:36 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#c9e63f14085f5751ff5ead79a7c0382d50a813a2