-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sensors.ino
187 lines (163 loc) · 4.92 KB
/
Sensors.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
/* This file is part of the Razor AHRS Firmware */
// 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 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
void 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();
if (i == 6) // All bytes received?
{
// No multiply by -1 for coordinate system transformation here, because of double negation:
// We want the gravity vector, which is negated acceleration vector.
accel[0] = (((int) buff[3]) << 8) | buff[2]; // X axis (internal sensor y axis)
accel[1] = (((int) buff[1]) << 8) | buff[0]; // Y axis (internal sensor x axis)
accel[2] = (((int) buff[5]) << 8) | buff[4]; // Z axis (internal sensor z axis)
}
else
{
num_accel_errors++;
if (output_errors) Serial.println("!ERR: reading accelerometer");
}
}
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);
}
void 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();
if (i == 6) // All bytes received?
{
// MSB byte first, then LSB; Y and Z reversed: X, Z, Y
magnetom[0] = (((int) buff[0]) << 8) | buff[1]; // X axis (internal sensor x axis)
magnetom[1] = -1 * ((((int) buff[4]) << 8) | buff[5]); // Y axis (internal sensor -y axis)
magnetom[2] = -1 * ((((int) buff[2]) << 8) | buff[3]); // Z axis (internal sensor -z axis)
}
else
{
num_magn_errors++;
if (output_errors) Serial.println("!ERR: reading magnetometer");
}
}
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");
}
}