2381 |
kaklik |
1 |
/* IMU Fusion Board - ADXL345 & IMU3000 |
|
|
2 |
Example Arduino Sketch to read the Gyro and Accelerometer Data |
|
|
3 |
|
|
|
4 |
Written by www.hobbytronics.co.uk |
|
|
5 |
See the latest version at www.hobbytronics.co.uk/arduino-adxl345-imu3000 |
|
|
6 |
08-Apr-2011 |
|
|
7 |
*/ |
|
|
8 |
|
|
|
9 |
#define GYRO 0x68 // gyro I2C address |
|
|
10 |
#define REG_GYRO_X 0x1D // IMU-3000 Register address for GYRO_XOUT_H |
|
|
11 |
#define ACCEL 0x53 // Accel I2c Address |
|
|
12 |
#define ADXL345_POWER_CTL 0x2D |
|
|
13 |
|
|
|
14 |
byte buffer[12]; // Array to store ADC values |
|
|
15 |
int gyro_x; |
|
|
16 |
int gyro_y; |
|
|
17 |
int gyro_z; |
|
|
18 |
int accel_x; |
|
|
19 |
int accel_y; |
|
|
20 |
int accel_z; |
|
|
21 |
int i; |
|
|
22 |
#include <Wire.h> |
|
|
23 |
|
|
|
24 |
void setup() |
|
|
25 |
{ |
|
|
26 |
Serial.begin(9600); |
|
|
27 |
|
|
|
28 |
Wire.begin(); |
|
|
29 |
// Set Gyro settings |
|
|
30 |
// Sample Rate 1kHz, Filter Bandwidth 42Hz, Gyro Range 500 d/s |
|
|
31 |
writeTo(GYRO, 0x16, 0x0B); |
|
|
32 |
//set accel register data address |
|
|
33 |
writeTo(GYRO, 0x18, 0x32); |
|
|
34 |
// set accel i2c slave address |
|
|
35 |
writeTo(GYRO, 0x14, ACCEL); |
|
|
36 |
|
|
|
37 |
// Set passthrough mode to Accel so we can turn it on |
|
|
38 |
writeTo(GYRO, 0x3D, 0x08); |
|
|
39 |
// set accel power control to 'measure' |
|
|
40 |
writeTo(ACCEL, ADXL345_POWER_CTL, 8); |
|
|
41 |
//cancel pass through to accel, gyro will now read accel for us |
|
|
42 |
writeTo(GYRO, 0x3D, 0x28); |
|
|
43 |
|
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
// Write a value to address register on device |
|
|
47 |
void writeTo(int device, byte address, byte val) { |
|
|
48 |
Wire.beginTransmission(device); // start transmission to device |
|
|
49 |
Wire.send(address); // send register address |
|
|
50 |
Wire.send(val); // send value to write |
|
|
51 |
Wire.endTransmission(); // end transmission |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
void loop() |
|
|
55 |
{ |
|
|
56 |
// Read the Gyro X, Y and Z and Accel X, Y and Z all through the gyro |
|
|
57 |
|
|
|
58 |
// First set the register start address for X on Gyro |
|
|
59 |
Wire.beginTransmission(GYRO); |
|
|
60 |
Wire.send(REG_GYRO_X); //Register Address GYRO_XOUT_H |
|
|
61 |
Wire.endTransmission(); |
|
|
62 |
|
|
|
63 |
// New read the 12 data bytes |
|
|
64 |
Wire.beginTransmission(GYRO); |
|
|
65 |
Wire.requestFrom(GYRO,12); // Read 12 bytes |
|
|
66 |
i = 0; |
|
|
67 |
while(Wire.available()) |
|
|
68 |
{ |
|
|
69 |
buffer[i] = Wire.receive(); |
|
|
70 |
i++; |
|
|
71 |
} |
|
|
72 |
Wire.endTransmission(); |
|
|
73 |
|
|
|
74 |
//Combine bytes into integers |
|
|
75 |
// Gyro format is MSB first |
|
|
76 |
gyro_x = buffer[0] << 8 | buffer[1]; |
|
|
77 |
gyro_y = buffer[2] << 8 | buffer[3]; |
|
|
78 |
gyro_z = buffer[4] << 8 | buffer[5]; |
|
|
79 |
// Accel is LSB first. Also because of orientation of chips |
|
|
80 |
// accel y output is in same orientation as gyro x |
|
|
81 |
// and accel x is gyro -y |
|
|
82 |
accel_y = buffer[7] << 8 | buffer[6]; |
|
|
83 |
accel_x = buffer[9] << 8 | buffer[8]; |
|
|
84 |
accel_z = buffer[11] << 8 | buffer[10]; |
|
|
85 |
|
|
|
86 |
// Print out what we have |
|
|
87 |
Serial.print(gyro_x); // echo the number received to screen |
2394 |
kaklik |
88 |
Serial.print(" "); |
2381 |
kaklik |
89 |
Serial.print(gyro_y); // echo the number received to screen |
2394 |
kaklik |
90 |
Serial.print(" "); |
2381 |
kaklik |
91 |
Serial.print(gyro_z); // echo the number received to screen |
2394 |
kaklik |
92 |
Serial.print(" "); |
2381 |
kaklik |
93 |
Serial.print(accel_x); // echo the number received to screen |
2394 |
kaklik |
94 |
Serial.print(" "); |
2381 |
kaklik |
95 |
Serial.print(accel_y); // echo the number received to screen |
2394 |
kaklik |
96 |
Serial.print(" "); |
|
|
97 |
Serial.println(accel_z); // prints the number and carriage return |
|
|
98 |
delay(100); // some wait before new reading |
2381 |
kaklik |
99 |
} |