/
IMU9DOF_Logger.ino
214 lines (190 loc) · 5.48 KB
/
IMU9DOF_Logger.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
// I2C code to read the sensors
// Sensor I2C addresses
#define ACCEL_ADDRESS ((int) 0x53) // 0x53 = 0xA6 / 2
#define MAGN_ADDRESS ((int) 0x1E) // 0x1E = 0x3C / 2
#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
void setup()
{
Serial.begin(9600);
I2C_Init();
Accel_Init();
Magn_Init();
Gyro_Init();
}
void loop()
{
tmElements_t tm;
RTC.read(tm);
Serial.print(tm.Day);
Serial.print("/");
Serial.print(tm.Month);
Serial.print("/");
Serial.print(tmYearToCalendar(tm.Year));
Serial.print(" ");
Serial.print(tm.Hour);
Serial.print(":");
Serial.print(tm.Minute);
Serial.print(":");
Serial.print(tm.Second);
Serial.print(",");
Serial.print(Read_Accel());
Serial.print(",");
Serial.print(Read_Magn());
Serial.print(",");
Serial.println(Read_Gyro());
}
void I2C_Init()
{
Wire.begin();
}
void Accel_Init()
{
Wire.beginTransmission(ACCEL_ADDRESS);
WIRE_SEND(0x2D); // Power register
WIRE_SEND(0x08); // Measurement mode
Wire.endTransmission();
delay(5);
Wire.beginTransmission(ACCEL_ADDRESS);
WIRE_SEND(0x31); // Data format register
WIRE_SEND(0x08); // Set to full resolution
Wire.endTransmission();
delay(5);
// Because our main loop runs at 50Hz we adjust the output data rate to 50Hz (25Hz bandwidth)
Wire.beginTransmission(ACCEL_ADDRESS);
WIRE_SEND(0x2C); // Rate
WIRE_SEND(0x09); // Set to 50Hz, normal operation
Wire.endTransmission();
delay(5);
}
// Reads x, y and z accelerometer registers
String Read_Accel()
{
int i = 0;
byte buff[6];
Wire.beginTransmission(ACCEL_ADDRESS);
WIRE_SEND(0x32); // Send address to read from
Wire.endTransmission();
Wire.beginTransmission(ACCEL_ADDRESS);
Wire.requestFrom(ACCEL_ADDRESS, 6); // Request 6 bytes
while(Wire.available()) // ((Wire.available())&&(i<6))
{
buff[i] = WIRE_RECEIVE(); // Read one byte
i++;
}
Wire.endTransmission();
// No multiply by -1 for coordinate system transformation here, because of double negation:
// We want the gravity vector, which is negated acceleration vector.
int accelX = (((int) buff[3]) << 8) | buff[2]; // X axis (internal sensor y axis)
int accelY = (((int) buff[1]) << 8) | buff[0]; // Y axis (internal sensor x axis)
int accelZ = (((int) buff[5]) << 8) | buff[4]; // Z axis (internal sensor z axis)
String accel=String(accelX);
accel+=",";
accel+=String(accelY);
accel+=",";
accel+=String(accelZ);
return(accel);
}
void Magn_Init()
{
Wire.beginTransmission(MAGN_ADDRESS);
WIRE_SEND(0x02);
WIRE_SEND(0x00); // Set continuous mode (default 10Hz)
Wire.endTransmission();
delay(5);
Wire.beginTransmission(MAGN_ADDRESS);
WIRE_SEND(0x00);
WIRE_SEND(0b00011000); // Set 50Hz
Wire.endTransmission();
delay(5);
}
String Read_Magn()
{
int i = 0;
byte buff[6];
Wire.beginTransmission(MAGN_ADDRESS);
WIRE_SEND(0x03); // Send address to read from
Wire.endTransmission();
Wire.beginTransmission(MAGN_ADDRESS);
Wire.requestFrom(MAGN_ADDRESS, 6); // Request 6 bytes
while(Wire.available()) // ((Wire.available())&&(i<6))
{
buff[i] = WIRE_RECEIVE(); // Read one byte
i++;
}
Wire.endTransmission();
// MSB byte first, then LSB; Y and Z reversed: X, Z, Y
int magnetomX = (((int) buff[0]) << 8) | buff[1]; // X axis (internal sensor x axis)
int magnetomY = -1 * ((((int) buff[4]) << 8) | buff[5]); // Y axis (internal sensor -y axis)
int magnetomZ = -1 * ((((int) buff[2]) << 8) | buff[3]); // Z axis (internal sensor -z axis)
String magnetom=String(magnetomX);
magnetom+=",";
magnetom+=String(magnetomY);
magnetom+=",";
magnetom+=String(magnetomZ);
return(magnetom);
}
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
String 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();
int gyroX = -1 * ((((int) buff[2]) << 8) | buff[3]); // X axis (internal sensor -y axis)
int gyroY = -1 * ((((int) buff[0]) << 8) | buff[1]); // Y axis (internal sensor -x axis)
int gyroZ = -1 * ((((int) buff[4]) << 8) | buff[5]); // Z axis (internal sensor -z axis)
String gyro=String(gyroX);
gyro+=",";
gyro+=String(gyroY);
gyro+=",";
gyro+=String(gyroZ);
return(gyro);
}