Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MPU9150_raw.cpp
00001 #include "MPU9150_raw.h" 00002 00003 // I2Cdev and MPU9150 must be installed as libraries, or else the .cpp/.h files 00004 // for both classes must be in the include path of your project 00005 #include "I2Cdev.h" 00006 #include "MPU9150.h" 00007 #include "helper_3dmath.h" 00008 #include "ArduinoSerial.h" 00009 00010 namespace MPU9150raw { 00011 // class default I2C address is 0x68 00012 // specific I2C addresses may be passed as a parameter here 00013 // AD0 low = 0x68 (default for InvenSense evaluation board) 00014 // AD0 high = 0x69 00015 MPU9150 accelGyroMag; 00016 00017 int16_t ax, ay, az; 00018 int16_t gx, gy, gz; 00019 int16_t mx, my, mz; 00020 00021 DigitalOut led1(LED1); 00022 ArduinoSerial arduinoSerial; 00023 00024 void setup() { 00025 // initialize Serial communication 00026 // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but 00027 // it's really up to you depending on your project) 00028 arduinoSerial.begin(115200); 00029 00030 // initialize device 00031 arduinoSerial.println("Initializing I2C devices..."); 00032 accelGyroMag.initialize(); 00033 00034 // verify connection 00035 arduinoSerial.println("Testing device connections..."); 00036 arduinoSerial.println(accelGyroMag.testConnection() ? "MPU9150 connection successful" : "MPU9150 connection failed"); 00037 } 00038 00039 void loop() { 00040 // read raw accel/gyro/mag measurements from device 00041 accelGyroMag.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz); 00042 00043 // these methods (and a few others) are also available 00044 //accelGyroMag.getAcceleration(&ax, &ay, &az); 00045 //accelGyroMag.getRotation(&gx, &gy, &gz); 00046 00047 // display tab-separated accel/gyro/mag x/y/z values 00048 arduinoSerial.print("a/g/m:\t"); 00049 arduinoSerial.print(ax); arduinoSerial.print("\t"); 00050 arduinoSerial.print(ay); arduinoSerial.print("\t"); 00051 arduinoSerial.print(az); arduinoSerial.print("\t"); 00052 arduinoSerial.print(gx); arduinoSerial.print("\t"); 00053 arduinoSerial.print(gy); arduinoSerial.print("\t"); 00054 arduinoSerial.print(gz); arduinoSerial.print("\t"); 00055 arduinoSerial.print(int(mx)*int(mx)); arduinoSerial.print("\t"); 00056 arduinoSerial.print(int(my)*int(my)); arduinoSerial.print("\t"); 00057 arduinoSerial.print(int(mz)*int(mz)); arduinoSerial.print("\t | "); 00058 00059 const float N = 256; 00060 float mag = mx*mx/N + my*my/N + mz*mz/N; 00061 00062 arduinoSerial.print(mag); arduinoSerial.print("\t"); 00063 for (int i=0; i<mag; i+=100000) 00064 arduinoSerial.print("*"); 00065 arduinoSerial.print("\r\n"); 00066 00067 // blink LED to indicate activity 00068 if( led1 == 0 ) led1 = 0; 00069 else led1 = 1; 00070 wait_ms(50); 00071 } 00072 00073 };
Generated on Sat Jul 16 2022 15:36:19 by
1.7.2