forked from miniben-90/mpu9250
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
255 lines (217 loc) · 6.64 KB
/
example.js
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
'use strict';
var mpu9250 = require('./mpu9250');
// These values were generated using calibrate_mag.js - you will want to create your own.
var MAG_CALIBRATION = {
min: { x: -106.171875, y: -56.8125, z: -14.828125 },
max: { x: 71.9609375, y: 117.17578125, z: 164.25 },
offset: { x: -17.10546875, y: 30.181640625, z: 74.7109375 },
scale: {
x: 1.491020130696022,
y: 1.5265373476123123,
z: 1.483149376145188
}
};
// These values were generated using calibrate_gyro.js - you will want to create your own.
// NOTE: These are temperature dependent.
var GYRO_OFFSET = {
x: -1.068045801,
y: -0.156656488,
z: 1.3846259541
};
// These values were generated using calibrate_accel.js - you will want to create your own.
var ACCEL_CALIBRATION = {
offset: {
x: 0.00943176,
y: 0.00170817,
z: 0.05296142
},
scale: {
x: [-0.9931640, 1.0102189],
y: [-0.9981974, 1.0055884],
z: [-0.9598844, 1.0665967]
}
};
// Instantiate and initialize.
var mpu = new mpu9250({
// i2c path (default is '/dev/i2c-1')
device: '/dev/i2c-2',
// Enable/Disable debug mode (default false)
DEBUG: true,
// Set the Gyroscope sensitivity (default 0), where:
// 0 => 250 degrees / second
// 1 => 500 degrees / second
// 2 => 1000 degrees / second
// 3 => 2000 degrees / second
GYRO_FS: 0,
// Set the Accelerometer sensitivity (default 2), where:
// 0 => +/- 2 g
// 1 => +/- 4 g
// 2 => +/- 8 g
// 3 => +/- 16 g
ACCEL_FS: 0,
scaleValues: true,
UpMagneto: true,
magCalibration: MAG_CALIBRATION,
gyroBiasOffset: GYRO_OFFSET,
accelCalibration: ACCEL_CALIBRATION
});
if (mpu.initialize()) {
var ACCEL_NAME = 'Accel (g)';
var GYRO_NAME = 'Gyro (°/sec)';
var MAG_NAME = 'Mag (uT)';
var HEADING_NAME = 'Heading (°)';
var stats = new Stats([ACCEL_NAME, GYRO_NAME, MAG_NAME, HEADING_NAME], 1000);
console.log('\n Time Accel.x Accel.y Accel.z Gyro.x Gyro.y Gyro.z Mag.x Mag.y Mag.z Temp(°C) heading(°)');
var cnt = 0;
var lastMag = [0, 0, 0];
setInterval(function() {
var start = new Date().getTime();
var m9;
// Only get the magnetometer values every 100Hz
var getMag = cnt++ % 2;
if (getMag) {
m9 = mpu.getMotion6().concat(lastMag);
} else {
m9 = mpu.getMotion9();
lastMag = [m9[6], m9[7], m9[8]];
}
var end = new Date().getTime();
var t = (end - start) / 1000;
// Make the numbers pretty
var str = '';
for (var i = 0; i < m9.length; i++) {
str += p(m9[i]);
}
stats.add(ACCEL_NAME, m9[0], m9[1], m9[2]);
stats.add(GYRO_NAME, m9[3], m9[4], m9[5]);
if (getMag) {
stats.add(MAG_NAME, m9[6], m9[7], m9[8]);
stats.addValue(HEADING_NAME, calcHeading(m9[6], m9[7]));
}
process.stdout.write(p(t) + str + p(mpu.getTemperatureCelsiusDigital()) + p(calcHeading(m9[6], m9[7])) + ' \r');
}, 5);
}
function p(num) {
if (num === undefined) {
return ' ';
}
var str = num.toFixed(3);
while (str.length <= 7) {
str = ' ' + str;
}
return str + ' ';
}
function calcHeading(x, y) {
var heading = Math.atan2(y, x) * 180 / Math.PI;
if (heading < -180) {
heading += 360;
} else if (heading > 180) {
heading -= 360;
}
return heading;
}
/**
* Calculate Statistics
* @param {[string]} names The names of the vectors.
*/
function Stats(vectorNames, numStats) {
this.vectorNames = vectorNames;
this.numStats = numStats;
this.vectors = {};
this.done = false;
for (var i = 0; i < vectorNames.length; i += 1) {
var name = vectorNames[i];
this.vectors[name] = {
x: [],
y: [],
z: [],
pos: 0
};
}
function exitHandler(options, err) {
if (err) {
console.log(err.stack);
} else {
this.printStats();
}
if (options.exit) {
var exit = process.exit;
exit();
}
}
// do something when app is closing
process.on('exit', exitHandler.bind(this, {cleanup: true}));
// catches ctrl+c event
process.on('SIGINT', exitHandler.bind(this, {exit: true}));
// catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(this, {exit: true}));
}
Stats.prototype.add = function(vectorName, x, y, z) {
var v = this.vectors[vectorName];
var len = v.x.length;
if (v.pos >= this.numStats) {
v.pos = 0;
} else {
v.pos += 1;
}
v.x[v.pos] = x;
v.y[v.pos] = y;
v.z[v.pos] = z;
};
Stats.prototype.addValue = function(vectorName, x) {
var v = this.vectors[vectorName];
v.isValue = true;
if (v.pos >= this.numStats) {
v.pos = 0;
} else {
v.pos += 1;
}
v.x[v.pos] = x;
};
Stats.prototype.printStats = function () {
if (this.done) return;
this.done = true;
console.log('\n\n\nStatistics:');
console.log(' average std.dev. num.same.values');
for (var i = 0; i < this.vectorNames.length; i += 1) {
var name = this.vectorNames[i];
var v = this.vectors[name];
console.log(name + ':');
console.log(' -> x: ', average(v.x).toFixed(5), standardDeviation(v.x).toFixed(5), numSameValues(v.x));
if (!v.isValue) {
console.log(' -> y: ', average(v.y).toFixed(5), standardDeviation(v.y).toFixed(5), numSameValues(v.y));
console.log(' -> z: ', average(v.z).toFixed(5), standardDeviation(v.z).toFixed(5), numSameValues(v.z));
}
console.log(' -> num samples: ', v.x.length);
console.log();
}
function standardDeviation(values) {
var avg = average(values);
var squareDiffs = values.map(function (value) {
var diff = value - avg;
var sqrDiff = diff * diff;
return sqrDiff;
});
var avgSquareDiff = average(squareDiffs);
var stdDev = Math.sqrt(avgSquareDiff);
return stdDev;
}
function average(values) {
var sumData = values.reduce(function (sum, value) {
return sum + value;
}, 0);
var avg = sumData / values.length;
return avg;
}
function numSameValues(values) {
var same = 0;
var lastVal = NaN;
values.forEach(function(val) {
if (val === lastVal) {
same += 1;
}
lastVal = val;
});
return same;
}
};