i2c library - http://www.i2cdevlib.com/usage
Pitch and Roll from accel - https://theccontinuum.com/2012/09/24/arduino-imu-pitch-roll-from-accelerometer/
Complementary filter - http://www.pieter-jan.com/node/11
i2c library - http://www.i2cdevlib.com/usage
Pitch and Roll from accel - https://theccontinuum.com/2012/09/24/arduino-imu-pitch-roll-from-accelerometer/
Complementary filter - http://www.pieter-jan.com/node/11
#include <Wire.h> | |
#include <I2Cdev.h> | |
#include <ADXL345.h> | |
ADXL345 acc; | |
void setup() { | |
Serial.begin(9600); | |
Wire.begin(); | |
acc.initialize(); | |
delay(100); | |
} | |
void loop() { | |
int16_t aX, aY, aZ; | |
acc.getAcceleration(&aX, &aY, &aZ); | |
float gX = (float) aX / 1023.0 * 5; | |
float gY = (float) aY / 1023.0 * 5; | |
float gZ = (float) aZ / 1023.0 * 5; | |
//Roll & Pitch Equations | |
double roll = (atan2(-gY, gZ)*180.0)/M_PI; | |
double pitch = (atan2(gX, sqrt(gY*gY + gZ*gZ))*180.0)/M_PI; | |
Serial.print(pitch); | |
Serial.print(":"); | |
Serial.println(roll); | |
delay(100); | |
} |
#include <Wire.h> | |
#include <I2Cdev.h> | |
#include <ITG3200.h> | |
// Set a default time of 100 ms | |
#define TIME_STEP 100 | |
ITG3200 gyro; | |
float roll = 0; | |
float pitch = 0; | |
void setup() { | |
Serial.begin(9600); | |
Wire.begin(); | |
gyro.initialize(); | |
delay(100); | |
} | |
void loop() { | |
unsigned long timer = millis(); | |
int16_t gx, gy, gz; | |
gyro.getRotation(&gx, &gy, &gz); | |
roll = roll + (float)gx/14.375 * TIME_STEP / 1000.0; | |
pitch = pitch + (float)gy/14.375 * TIME_STEP / 1000.0; | |
while (millis() - timer < TIME_STEP) {} | |
Serial.print(pitch); | |
Serial.print(":"); | |
Serial.println(roll); | |
} |