-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest.js
75 lines (59 loc) · 2.05 KB
/
test.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
var util = require('util')
var nodeimu = require('./index.js');
var IMU = new nodeimu.IMU();
var num = 0;
var numStop = 100;
console.time("async");
var print_vector3 = function(name, data) {
var sx = data.x >= 0 ? ' ' : '';
var sy = data.y >= 0 ? ' ' : '';
var sz = data.z >= 0 ? ' ' : '';
return util.format('%s: %s%s %s%s %s%s ', name, sx, data.x.toFixed(4), sy, data.y.toFixed(4), sz, data.z.toFixed(4));
}
var headingCorrection = function(heading, offset) {
if (typeof offset ==='undefined')
offset = 0;
// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
// Find yours here: http://www.magnetic-declination.com/
var declinationAngle = 0.03106686;
heading += declinationAngle + offset;
// Correct for when signs are reversed.
if (heading < 0)
heading += 2 * Math.PI;
// Check for wrap due to addition of declination.
if (heading > 2 * Math.PI)
heading -= 2 * Math.PI;
return heading;
}
var headingToDegree = function(heading) {
// Convert radians to degrees for readability.
return heading * 180 / Math.PI;
}
var tic = new Date();
var callb = function (e, data) {
var toc = new Date();
if (e) {
console.log(e);
return;
}
var str = data.timestamp.toISOString() + " ";
str += print_vector3('Accel', data.accel)
// str += print_vector3('Gyro', data.gyro)
// str += print_vector3('Compass', data.compass)
// str += print_vector3('Fusion', data.fusionPose)
str += util.format('TiltHeading: %s ', headingToDegree(headingCorrection(data.tiltHeading, Math.PI / 2)).toFixed(0));
var str2 = "";
if (data.temperature && data.pressure && data.humidity) {
var str2 = util.format('%s %s %s', data.temperature.toFixed(4), data.pressure.toFixed(4), data.humidity.toFixed(4));
}
if ((num % 10) == 0) {
console.log(str + str2);
}
num++;
if (num == numStop) {
console.timeEnd("async");
} else {
setTimeout(function() { tic = new Date(); IMU.getValue(callb); } , 20 - (toc - tic));
}
}
IMU.getValue(callb);