Created
June 9, 2015 19:02
-
-
Save Robotto/25f405d43d62ddacf083 to your computer and use it in GitHub Desktop.
9dof
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
#include <Wire.h> | |
#define GYRO_ADDRESS ((int) 0x68) // 0x68 = 0xD0 / 2 | |
// Arduino backward compatibility macros | |
#if ARDUINO >= 100 | |
#define WIRE_SEND(b) Wire.write((byte) b) | |
#define WIRE_RECEIVE() Wire.read() | |
#else | |
#define WIRE_SEND(b) Wire.send(b) | |
#define WIRE_RECEIVE() Wire.receive() | |
#endif | |
int gyro[6]; | |
int num_gyro_errors=0; | |
boolean output_errors = false; | |
int beeppin = 10; | |
void setup() | |
{ | |
Wire.begin(); | |
Gyro_Init(); | |
TX_RX_LED_INIT; | |
pinMode(beeppin,OUTPUT); | |
} | |
//int i=0; | |
//int j=0; | |
void loop() | |
{ | |
Read_Gyro(); | |
//Serial.print("X: "); | |
//Serial.println(gyro[0]); | |
//Serial.print("Y: "); | |
Serial.println(gyro[1]); | |
//Serial.print("Z: "); | |
//Serial.println(gyro[2]); | |
if (gyro[0]<-1500) beep(); | |
} | |
void beep() | |
{ | |
TXLED1; | |
analogWrite(beeppin,128); | |
delay(100); | |
digitalWrite(beeppin,0); | |
TXLED0; | |
} | |
void Gyro_Init() | |
{ | |
// Power up reset defaults | |
Wire.beginTransmission(GYRO_ADDRESS); | |
WIRE_SEND(0x3E); | |
WIRE_SEND(0x80); | |
Wire.endTransmission(); | |
delay(5); | |
// Select full-scale range of the gyro sensors | |
// Set LP filter bandwidth to 42Hz | |
Wire.beginTransmission(GYRO_ADDRESS); | |
WIRE_SEND(0x16); | |
WIRE_SEND(0x1B); // DLPF_CFG = 3, FS_SEL = 3 | |
Wire.endTransmission(); | |
delay(5); | |
// Set sample rato to 50Hz | |
Wire.beginTransmission(GYRO_ADDRESS); | |
WIRE_SEND(0x15); | |
WIRE_SEND(0x0A); // SMPLRT_DIV = 10 (50Hz) | |
Wire.endTransmission(); | |
delay(5); | |
// Set clock to PLL with z gyro reference | |
Wire.beginTransmission(GYRO_ADDRESS); | |
WIRE_SEND(0x3E); | |
WIRE_SEND(0x00); | |
Wire.endTransmission(); | |
delay(5); | |
} | |
// Reads x, y and z gyroscope registers | |
void Read_Gyro() | |
{ | |
int i = 0; | |
byte buff[6]; | |
Wire.beginTransmission(GYRO_ADDRESS); | |
WIRE_SEND(0x1D); // Sends address to read from | |
Wire.endTransmission(); | |
Wire.beginTransmission(GYRO_ADDRESS); | |
Wire.requestFrom(GYRO_ADDRESS, 6); // Request 6 bytes | |
while(Wire.available()) // ((Wire.available())&&(i<6)) | |
{ | |
buff[i] = WIRE_RECEIVE(); // Read one byte | |
i++; | |
} | |
Wire.endTransmission(); | |
if (i == 6) // All bytes received? | |
{ | |
gyro[0] = -1 * ((((int) buff[2]) << 8) | buff[3]); // X axis (internal sensor -y axis) | |
gyro[1] = -1 * ((((int) buff[0]) << 8) | buff[1]); // Y axis (internal sensor -x axis) | |
gyro[2] = -1 * ((((int) buff[4]) << 8) | buff[5]); // Z axis (internal sensor -z axis) | |
} | |
else | |
{ | |
num_gyro_errors++; | |
if (output_errors) Serial.println("!ERR: reading gyroscope"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment