Example to use MPU9150 library. This project explaining the way to get raw values from the sensor. I ported here from this arduino's project https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU9150/Examples. To get run this program, we need to connect pinName 27 to "SCL" pin, pinName 28 to "SDA" pin, GND to GND, VOUT to VCC.

Dependencies:   MPU9150 mbed

Committer:
syundo0730
Date:
Mon Feb 01 16:14:10 2016 +0000
Revision:
1:30fb42e3ae76
Parent:
0:0d6025d96c54
added LICENSE file to MPU9150 folder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
syundo0730 0:0d6025d96c54 1 #include "MPU9150_raw.h"
syundo0730 0:0d6025d96c54 2
syundo0730 0:0d6025d96c54 3 // I2Cdev and MPU9150 must be installed as libraries, or else the .cpp/.h files
syundo0730 0:0d6025d96c54 4 // for both classes must be in the include path of your project
syundo0730 0:0d6025d96c54 5 #include "I2Cdev.h"
syundo0730 0:0d6025d96c54 6 #include "MPU9150.h"
syundo0730 0:0d6025d96c54 7 #include "helper_3dmath.h"
syundo0730 0:0d6025d96c54 8 #include "ArduinoSerial.h"
syundo0730 0:0d6025d96c54 9
syundo0730 0:0d6025d96c54 10 namespace MPU9150raw {
syundo0730 0:0d6025d96c54 11 // class default I2C address is 0x68
syundo0730 0:0d6025d96c54 12 // specific I2C addresses may be passed as a parameter here
syundo0730 0:0d6025d96c54 13 // AD0 low = 0x68 (default for InvenSense evaluation board)
syundo0730 0:0d6025d96c54 14 // AD0 high = 0x69
syundo0730 0:0d6025d96c54 15 MPU9150 accelGyroMag;
syundo0730 0:0d6025d96c54 16
syundo0730 0:0d6025d96c54 17 int16_t ax, ay, az;
syundo0730 0:0d6025d96c54 18 int16_t gx, gy, gz;
syundo0730 0:0d6025d96c54 19 int16_t mx, my, mz;
syundo0730 0:0d6025d96c54 20
syundo0730 0:0d6025d96c54 21 DigitalOut led1(LED1);
syundo0730 0:0d6025d96c54 22 ArduinoSerial arduinoSerial;
syundo0730 0:0d6025d96c54 23
syundo0730 0:0d6025d96c54 24 void setup() {
syundo0730 0:0d6025d96c54 25 // initialize Serial communication
syundo0730 0:0d6025d96c54 26 // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
syundo0730 0:0d6025d96c54 27 // it's really up to you depending on your project)
syundo0730 0:0d6025d96c54 28 arduinoSerial.begin(115200);
syundo0730 0:0d6025d96c54 29
syundo0730 0:0d6025d96c54 30 // initialize device
syundo0730 0:0d6025d96c54 31 arduinoSerial.println("Initializing I2C devices...");
syundo0730 0:0d6025d96c54 32 accelGyroMag.initialize();
syundo0730 0:0d6025d96c54 33
syundo0730 0:0d6025d96c54 34 // verify connection
syundo0730 0:0d6025d96c54 35 arduinoSerial.println("Testing device connections...");
syundo0730 0:0d6025d96c54 36 arduinoSerial.println(accelGyroMag.testConnection() ? "MPU9150 connection successful" : "MPU9150 connection failed");
syundo0730 0:0d6025d96c54 37 }
syundo0730 0:0d6025d96c54 38
syundo0730 0:0d6025d96c54 39 void loop() {
syundo0730 0:0d6025d96c54 40 // read raw accel/gyro/mag measurements from device
syundo0730 0:0d6025d96c54 41 accelGyroMag.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
syundo0730 0:0d6025d96c54 42
syundo0730 0:0d6025d96c54 43 // these methods (and a few others) are also available
syundo0730 0:0d6025d96c54 44 //accelGyroMag.getAcceleration(&ax, &ay, &az);
syundo0730 0:0d6025d96c54 45 //accelGyroMag.getRotation(&gx, &gy, &gz);
syundo0730 0:0d6025d96c54 46
syundo0730 0:0d6025d96c54 47 // display tab-separated accel/gyro/mag x/y/z values
syundo0730 0:0d6025d96c54 48 arduinoSerial.print("a/g/m:\t");
syundo0730 0:0d6025d96c54 49 arduinoSerial.print(ax); arduinoSerial.print("\t");
syundo0730 0:0d6025d96c54 50 arduinoSerial.print(ay); arduinoSerial.print("\t");
syundo0730 0:0d6025d96c54 51 arduinoSerial.print(az); arduinoSerial.print("\t");
syundo0730 0:0d6025d96c54 52 arduinoSerial.print(gx); arduinoSerial.print("\t");
syundo0730 0:0d6025d96c54 53 arduinoSerial.print(gy); arduinoSerial.print("\t");
syundo0730 0:0d6025d96c54 54 arduinoSerial.print(gz); arduinoSerial.print("\t");
syundo0730 0:0d6025d96c54 55 arduinoSerial.print(int(mx)*int(mx)); arduinoSerial.print("\t");
syundo0730 0:0d6025d96c54 56 arduinoSerial.print(int(my)*int(my)); arduinoSerial.print("\t");
syundo0730 0:0d6025d96c54 57 arduinoSerial.print(int(mz)*int(mz)); arduinoSerial.print("\t | ");
syundo0730 0:0d6025d96c54 58
syundo0730 0:0d6025d96c54 59 const float N = 256;
syundo0730 0:0d6025d96c54 60 float mag = mx*mx/N + my*my/N + mz*mz/N;
syundo0730 0:0d6025d96c54 61
syundo0730 0:0d6025d96c54 62 arduinoSerial.print(mag); arduinoSerial.print("\t");
syundo0730 0:0d6025d96c54 63 for (int i=0; i<mag; i+=100000)
syundo0730 0:0d6025d96c54 64 arduinoSerial.print("*");
syundo0730 0:0d6025d96c54 65 arduinoSerial.print("\r\n");
syundo0730 0:0d6025d96c54 66
syundo0730 0:0d6025d96c54 67 // blink LED to indicate activity
syundo0730 0:0d6025d96c54 68 if( led1 == 0 ) led1 = 0;
syundo0730 0:0d6025d96c54 69 else led1 = 1;
syundo0730 0:0d6025d96c54 70 wait_ms(50);
syundo0730 0:0d6025d96c54 71 }
syundo0730 0:0d6025d96c54 72
syundo0730 0:0d6025d96c54 73 };