-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_server.js
118 lines (85 loc) · 2.81 KB
/
ws_server.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
var util = require('util')
var nodeimu = require('../nodeimu/index.js')
var IMU = new nodeimu.IMU();
var temp = "";
/*var callb1 = function(e, data1) {
temp = util.format('%s', data1.temperature.toFixed(4));
console.log(temp);
}*/
var ws = require("nodejs-websocket")
var server = ws.createServer(function (conn) {
console.log("New connection")
var count = 1;
while(count < 100000 && server) {
var d = IMU.getValueSync();
temp = util.format('%s', d.temperature.toFixed(4));
console.log(temp);
// IMU.getValue(callb);
//IMU.getValue(callb1);
// conn.send(count.toString());
conn.send(temp);
count++;
}
//conn.on("text", function() {
//conn.sendText("testing!!");
//})
conn.on("close", function(code, reason) {
console.log("Connection closed")
})
}).listen(8001)
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));
temp = util.format('%s', data.temperature.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));
}
}