forked from Xinyuan-LilyGO/T-Wristband
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.h
222 lines (193 loc) · 8.67 KB
/
sensor.h
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
215
216
217
218
219
220
221
222
#ifndef _SENSOR_H_
#define _SENSOR_H_
#include "MPU9250.h"
#include "config.h"
#include "quaternionFilters.h"
#define AHRS true // Set to false for basic data read
#define SerialDebugMPU9250 0 // Set to true to get Serial output for debugging
#define SerialDebugCalibrate false // Set to true to get Serial output for debugging
MPU9250 IMU;
void setupMPU9250() {
byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX);
Serial.print(" I should be "); Serial.println(0x71, HEX);
if (c == 0x71) {
Serial.println("MPU9250 is online...");
// Start by performing self test and reporting values
IMU.MPU9250SelfTest(IMU.SelfTest);
if (SerialDebugCalibrate) {
Serial.print("x-axis self test: acceleration trim within : ");
Serial.print(IMU.SelfTest[0], 1); Serial.println("% of factory value");
Serial.print("y-axis self test: acceleration trim within : ");
Serial.print(IMU.SelfTest[1], 1); Serial.println("% of factory value");
Serial.print("z-axis self test: acceleration trim within : ");
Serial.print(IMU.SelfTest[2], 1); Serial.println("% of factory value");
Serial.print("x-axis self test: gyration trim within : ");
Serial.print(IMU.SelfTest[3], 1); Serial.println("% of factory value");
Serial.print("y-axis self test: gyration trim within : ");
Serial.print(IMU.SelfTest[4], 1); Serial.println("% of factory value");
Serial.print("z-axis self test: gyration trim within : ");
Serial.print(IMU.SelfTest[5], 1); Serial.println("% of factory value");
}
Serial.println("MPU9250 acceleration and gyration self test done!");
// Calibrate gyro and accelerometers, load biases in bias registers
IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
IMU.initMPU9250();
// Initialize device for active mode read of acclerometer, gyroscope, and
// temperature
Serial.println("MPU9250 initialized for active data mode....");
// Read the WHO_AM_I register of the magnetometer, this is a good test of
// communication
byte d = IMU.readByte(AK8963_ADDRESS, WHO_AM_I_AK8963);
Serial.print("AK8963 "); Serial.print("I AM "); Serial.print(d, HEX);
Serial.print(" I should be "); Serial.println(0x48, HEX);
// Get magnetometer calibration from AK8963 ROM
IMU.initAK8963(IMU.magCalibration);
// Initialize device for active mode read of magnetometer
Serial.println("AK8963 initialized for active data mode....");
if (SerialDebugCalibrate) {
Serial.println("Calibration values: ");
Serial.print("X-Axis sensitivity adjustment value ");
Serial.println(IMU.magCalibration[0], 2);
Serial.print("Y-Axis sensitivity adjustment value ");
Serial.println(IMU.magCalibration[1], 2);
Serial.print("Z-Axis sensitivity adjustment value ");
Serial.println(IMU.magCalibration[2], 2);
}
Serial.println("AK8963 magCalibration done!");
}
}
void readMPU9250() {
// If intPin goes high, all data registers have new data
// On interrupt, check if data ready interrupt
if (IMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
{
IMU.readAccelData(IMU.accelCount); // Read the x/y/z adc values
IMU.getAres();
// Now we'll calculate the accleration value into actual g's
// This depends on scale being set
IMU.ax = (float)IMU.accelCount[0] * IMU.aRes; // - accelBias[0];
IMU.ay = (float)IMU.accelCount[1] * IMU.aRes; // - accelBias[1];
IMU.az = (float)IMU.accelCount[2] * IMU.aRes; // - accelBias[2];
IMU.readGyroData(IMU.gyroCount); // Read the x/y/z adc values
IMU.getGres();
// Calculate the gyro value into actual degrees per second
// This depends on scale being set
IMU.gx = (float)IMU.gyroCount[0] * IMU.gRes;
IMU.gy = (float)IMU.gyroCount[1] * IMU.gRes;
IMU.gz = (float)IMU.gyroCount[2] * IMU.gRes;
IMU.readMagData(IMU.magCount); // Read the x/y/z adc values
IMU.getMres();
// User environmental x-axis correction in milliGauss, should be
// automatically calculated
IMU.magbias[0] = +470.;
// User environmental x-axis correction in milliGauss TODO axis??
IMU.magbias[1] = +120.;
// User environmental x-axis correction in milliGauss
IMU.magbias[2] = +125.;
// Calculate the magnetometer values in milliGauss
// Include factory calibration per data sheet and user environmental
// corrections
// Get actual magnetometer value, this depends on scale being set
IMU.mx = (float)IMU.magCount[0] * IMU.mRes * IMU.magCalibration[0] -
IMU.magbias[0];
IMU.my = (float)IMU.magCount[1] * IMU.mRes * IMU.magCalibration[1] -
IMU.magbias[1];
IMU.mz = (float)IMU.magCount[2] * IMU.mRes * IMU.magCalibration[2] -
IMU.magbias[2];
} // if (readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
// Must be called before updating quaternions!
IMU.updateTime();
// Sensors x (y)-axis of the accelerometer is aligned with the y (x)-axis of
// the magnetometer; the magnetometer z-axis (+ down) is opposite to z-axis
// (+ up) of accelerometer and gyro! We have to make some allowance for this
// orientationmismatch in feeding the output to the quaternion filter. For the
// MPU-9250, we have chosen a magnetic rotation that keeps the sensor forward
// along the x-axis just like in the LSM9DS0 sensor. This rotation can be
// modified to allow any convenient orientation convention. This is ok by
// aircraft orientation standards! Pass gyro rate as rad/s
// MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
MahonyQuaternionUpdate(IMU.ax, IMU.ay, IMU.az, IMU.gx * DEG_TO_RAD,
IMU.gy * DEG_TO_RAD, IMU.gz * DEG_TO_RAD, IMU.my,
IMU.mx, IMU.mz, IMU.deltat);
// Serial print and/or display at 0.5 s rate independent of data rates
IMU.delt_t = millis() - IMU.count;
if (IMU.delt_t > 20) {
if (SerialDebugMPU9250) {
Serial.print("ax = ");
Serial.print((int)1000 * IMU.ax);
Serial.print(" ay = ");
Serial.print((int)1000 * IMU.ay);
Serial.print(" az = ");
Serial.print((int)1000 * IMU.az);
Serial.println(" mg");
Serial.print("gx = ");
Serial.print( IMU.gx, 2);
Serial.print(" gy = ");
Serial.print( IMU.gy, 2);
Serial.print(" gz = ");
Serial.print( IMU.gz, 2);
Serial.println(" deg/s");
Serial.print("mx = ");
Serial.print( (int)IMU.mx );
Serial.print(" my = ");
Serial.print( (int)IMU.my );
Serial.print(" mz = ");
Serial.print( (int)IMU.mz );
Serial.println(" mG");
//Serial.print("q0 = ");
Serial.print(*getQ());
// Serial.print(" qx = ");
Serial.print(*(getQ() + 1));
// Serial.print(" qy = ");
Serial.print(*(getQ() + 2));
// Serial.print(" qz = ");
Serial.println(*(getQ() + 3));
}
IMU.yaw = atan2(2.0f * (*(getQ() + 1) * *(getQ() + 2) + *getQ() *
*(getQ() + 3)), *getQ() * *getQ() + * (getQ() + 1) * *(getQ() + 1)
- * (getQ() + 2) * *(getQ() + 2) - * (getQ() + 3) * *(getQ() + 3));
IMU.pitch = -asin(2.0f * (*(getQ() + 1) * *(getQ() + 3) - *getQ() *
*(getQ() + 2)));
IMU.roll = atan2(2.0f * (*getQ() * *(getQ() + 1) + * (getQ() + 2) *
*(getQ() + 3)), *getQ() * *getQ() - * (getQ() + 1) * *(getQ() + 1)
- * (getQ() + 2) * *(getQ() + 2) + * (getQ() + 3) * *(getQ() + 3));
IMU.pitch *= RAD_TO_DEG;
IMU.yaw *= RAD_TO_DEG;
// Declination of SparkFun Electronics (40°05'26.6"N 105°11'05.9"W) is
// 8° 30' E ± 0° 21' (or 8.5°) on 2016-07-19
// - http://www.ngdc.noaa.gov/geomag-web/#declination
IMU.yaw -= 8.5;
IMU.roll *= RAD_TO_DEG;
if (SerialDebugMPU9250) {
// Serial.println("Yaw, Pitch, Roll: ");
Serial.print(IMU.yaw, 2);
Serial.print(", ");
Serial.print(IMU.pitch, 2);
Serial.print(", ");
Serial.println(IMU.roll, 2);
}
IMU.count = millis();
IMU.sumCount = 0;
IMU.sum = 0;
} // if (IMU.delt_t > 20)
}
// void get_cursor_position(uint8_t *x, uint8_t *y) {
// int cursor_x = SCREEN_WIDTH / 2 + (int)(SCREEN_WIDTH / 2 * (IMU.roll / MAX_CURSOR_ACC));
// int cursor_y = SCREEN_HEIGHT / 2 - (int)(SCREEN_HEIGHT / 2 * (IMU.pitch / MAX_CURSOR_ACC));
// if (cursor_x < 0 ) {
// cursor_x = 0;
// }
// if (cursor_x >= SCREEN_WIDTH) {
// cursor_x = SCREEN_WIDTH - 1;
// }
// if (cursor_y < 0) {
// cursor_y = 0;
// }
// if (cursor_y >= SCREEN_HEIGHT - 1) {
// cursor_y = SCREEN_HEIGHT - 2;
// }
// *x = cursor_x;
// *y = cursor_y;
// }
#endif // _SENSOR_H_