-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e6d40fa
Showing
6 changed files
with
953 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
MPU6050 Arduino Library W.I.P. | ||
====================================================================== | ||
|
||
* W.I.P. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
MPU6050.cpp - Class file for the MPU6050 Triple Axis Gyroscope & Accelerometer Arduino Library. | ||
Version: W.I.P. | ||
(c) 2014 Korneliusz Jarzebski | ||
www.jarzebski.pl | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the version 3 GNU General Public License as | ||
published by the Free Software Foundation. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#if ARDUINO >= 100 | ||
#include "Arduino.h" | ||
#else | ||
#include "WProgram.h" | ||
#endif | ||
|
||
#include <Wire.h> | ||
#include <math.h> | ||
|
||
#include <MPU6050.h> | ||
|
||
bool MPU6050::begin(mpu6050_dps_t scale, mpu6050_range_t range) | ||
{ | ||
Wire.begin(); | ||
|
||
// Check MPU6050 Who Am I Register | ||
if (fastRegister8(MPU6050_REG_WHO_AM_I) != 0x68) | ||
{ | ||
return false; | ||
} | ||
|
||
// Set Clock Source | ||
setClockSource(MPU6050_CLOCK_PLL_XGYRO); | ||
|
||
return true; | ||
} | ||
|
||
void MPU6050::setClockSource(mpu6050_clockSource_t source) | ||
{ | ||
uint8_t value; | ||
value = readRegister8(MPU6050_REG_PWR_MGMT_1); | ||
value |= source; | ||
writeRegister8(MPU6050_REG_PWR_MGMT_1, value); | ||
} | ||
|
||
mpu6050_clockSource_t MPU6050::getClockSource(void) | ||
{ | ||
uint8_t value; | ||
value = readRegister8(MPU6050_REG_PWR_MGMT_1); | ||
value &= 0b00000111; | ||
return (mpu6050_clockSource_t)value; | ||
} | ||
|
||
// Fast read 8-bit from register | ||
uint8_t MPU6050::fastRegister8(uint8_t reg) | ||
{ | ||
uint8_t value; | ||
|
||
Wire.beginTransmission(MPU6050_ADDRESS); | ||
#if ARDUINO >= 100 | ||
Wire.write(reg); | ||
#else | ||
Wire.send(reg); | ||
#endif | ||
Wire.endTransmission(); | ||
|
||
Wire.beginTransmission(MPU6050_ADDRESS); | ||
Wire.requestFrom(MPU6050_ADDRESS, 1); | ||
#if ARDUINO >= 100 | ||
value = Wire.read(); | ||
#else | ||
value = Wire.receive(); | ||
#endif; | ||
Wire.endTransmission(); | ||
|
||
return value; | ||
} | ||
|
||
// Read 8-bit from register | ||
uint8_t MPU6050::readRegister8(uint8_t reg) | ||
{ | ||
uint8_t value; | ||
|
||
Wire.beginTransmission(MPU6050_ADDRESS); | ||
#if ARDUINO >= 100 | ||
Wire.write(reg); | ||
#else | ||
Wire.send(reg); | ||
#endif | ||
Wire.endTransmission(); | ||
|
||
Wire.beginTransmission(MPU6050_ADDRESS); | ||
Wire.requestFrom(MPU6050_ADDRESS, 1); | ||
while(!Wire.available()) {}; | ||
#if ARDUINO >= 100 | ||
value = Wire.read(); | ||
#else | ||
value = Wire.receive(); | ||
#endif; | ||
Wire.endTransmission(); | ||
|
||
return value; | ||
} | ||
|
||
// Write 8-bit to register | ||
void MPU6050::writeRegister8(uint8_t reg, uint8_t value) | ||
{ | ||
Wire.beginTransmission(MPU6050_ADDRESS); | ||
|
||
#if ARDUINO >= 100 | ||
Wire.write(reg); | ||
Wire.write(value); | ||
#else | ||
Wire.send(reg); | ||
Wire.send(value); | ||
#endif | ||
Wire.endTransmission(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
MPU6050.h - Header file for the MPU6050 Triple Axis Gyroscope & Accelerometer Arduino Library. | ||
Version: W.I.P | ||
(c) 2014 Korneliusz Jarzebski | ||
www.jarzebski.pl | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the version 3 GNU General Public License as | ||
published by the Free Software Foundation. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef MPU6050_h | ||
#define MPU6050_h | ||
|
||
#if ARDUINO >= 100 | ||
#include "Arduino.h" | ||
#else | ||
#include "WProgram.h" | ||
#endif | ||
|
||
#define MPU6050_ADDRESS (0x68) // 0x69 when AD0 pin to Vcc | ||
|
||
#define MPU6050_REG_PWR_MGMT_1 (0x6B) // Power Management 1 | ||
#define MPU6050_REG_WHO_AM_I (0x75) // Who Am I | ||
|
||
#ifndef VECTOR_STRUCT_H | ||
#define VECTOR_STRUCT_H | ||
struct Vector | ||
{ | ||
float XAxis; | ||
float YAxis; | ||
float ZAxis; | ||
}; | ||
#endif | ||
|
||
typedef enum | ||
{ | ||
MPU6050_CLOCK_KEEP_RESET = 0b111, | ||
MPU6050_CLOCK_EXTERNAL_19MHZ = 0b101, | ||
MPU6050_CLOCK_EXTERNAL_32KHZ = 0b100, | ||
MPU6050_CLOCK_PLL_ZGYRO = 0b011, | ||
MPU6050_CLOCK_PLL_YGYRO = 0b010, | ||
MPU6050_CLOCK_PLL_XGYRO = 0b001, | ||
MPU6050_CLOCK_INTERNAL_8MHZ = 0b000 | ||
} mpu6050_clockSource_t; | ||
|
||
typedef enum | ||
{ | ||
MPU6050_SCALE_2000DPS = 0b11, | ||
MPU6050_SCALE_1000DPS = 0b10, | ||
MPU6050_SCALE_500DPS = 0b01, | ||
MPU6050_SCALE_250DPS = 0b00 | ||
} mpu6050_dps_t; | ||
|
||
typedef enum | ||
{ | ||
MPU6050_RANGE_16G = 0b11, | ||
MPU6050_RANGE_8G = 0b10, | ||
MPU6050_RANGE_4G = 0b01, | ||
MPU6050_RANGE_2G = 0b00, | ||
} mpu6050_range_t; | ||
|
||
class MPU6050 | ||
{ | ||
public: | ||
|
||
bool begin(mpu6050_dps_t scale = MPU6050_SCALE_2000DPS, mpu6050_range_t = MPU6050_RANGE_2G); | ||
|
||
void setClockSource(mpu6050_clockSource_t source); | ||
mpu6050_clockSource_t getClockSource(void); | ||
|
||
private: | ||
|
||
void writeRegister8(uint8_t reg, uint8_t value); | ||
uint8_t readRegister8(uint8_t reg); | ||
uint8_t fastRegister8(uint8_t reg); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
MPU6050 Triple Axis Gyroscope & Accelerometer. Simple Example. | ||
Read more: TODO | ||
GIT: https://github.com/jarzebski/Arduino-MPU6050 | ||
Web: http://www.jarzebski.pl | ||
(c) 2014 by Korneliusz Jarzebski | ||
*/ | ||
|
||
#include <Wire.h> | ||
#include <MPU6050.h> | ||
|
||
MPU6050 mpu; | ||
|
||
void setup() | ||
{ | ||
Serial.begin(9600); | ||
|
||
Serial.println("Initialize MPU6050"); | ||
|
||
while(!mpu.begin(MPU6050_SCALE_2000DPS)) | ||
{ | ||
Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); | ||
delay(500); | ||
} | ||
|
||
checkSettings(); | ||
} | ||
|
||
void checkSettings() | ||
{ | ||
Serial.println(); | ||
|
||
Serial.print(" * Clock Source: "); | ||
|
||
switch(mpu.getClockSource()) | ||
{ | ||
case MPU6050_CLOCK_KEEP_RESET: Serial.println("Stops the clock and keeps the timing generator in reset"); break; | ||
case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break; | ||
case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break; | ||
case MPU6050_CLOCK_PLL_ZGYRO: Serial.println("PLL with Z axis gyroscope reference"); break; | ||
case MPU6050_CLOCK_PLL_YGYRO: Serial.println("PLL with Y axis gyroscope reference"); break; | ||
case MPU6050_CLOCK_PLL_XGYRO: Serial.println("PLL with X axis gyroscope reference"); break; | ||
case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println("Internal 8MHz oscillator"); break; | ||
} | ||
} | ||
|
||
void loop() | ||
{ | ||
delay(100); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Arduino-MPU6050 | ||
================ | ||
|
||
MPU6050 Triple Axis Gyroscope & Accelerometer Arduino Library. | ||
|
||
Tutorials: TODO | ||
|
||
This library use I2C to communicate, 2 pins are required to interface | ||
|