-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
94 lines (87 loc) · 2.61 KB
/
main.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
const util = require('util');
const UdpHandler = require("./UdpHandler.js");
const argparse = require("argparse");
const fs = require('fs');
const parser = new argparse.ArgumentParser({
version: "0.0.1",
addHelp:true,
description: "webxr server"
});
parser.addArgument(
[ "-p", "--port" ],
{
help: "port to listen on",
defaultValue: 12345
}
);
parser.addArgument(
[ "--host" ],
{
help: "host to connect to",
defaultValue: "172.24.71.214"
}
);
const args = parser.parseArgs();
const udpReceiver = new UdpHandler(args.host, args.port, null, false);
function AndroidIMUReceiver(){
const latest = UdpHandler.udpMsgQueue.pop();
if(latest){
var i = 0;
console.log("gyro:");
console.log(latest.readFloatBE(i));
i+=4;
console.log(latest.readFloatBE(i));
i+=4;
console.log(latest.readFloatBE(i));
i+=4;
console.log("rotation vec:");
console.log(latest.readFloatBE(i));
i+=4;
console.log(latest.readFloatBE(i));
i+=4;
console.log(latest.readFloatBE(i));
i+=4;
console.log(latest.readFloatBE(i));
i+=4;
console.log("time:");
console.log(latest.readFloatBE(i)/1000.0);
i+=4;
udpReceiver.clearQueue();
}
}
var msgCount = 0;
function AndroidIMUStringReceiver(){
if(msgCount != UdpHandler.udpMsgQueue.length){
msgCount = UdpHandler.udpMsgQueue.length;
if(msgCount > 0){
var item = UdpHandler.udpMsgQueue[msgCount-1];
var str = new Buffer(item).toString('ascii');
var obj = JSON.parse(str);
// console.log("receive:" + util.inspect(obj, {showHidden: false, depth: null}));
}
}else if(msgCount>0){
const datestr = new Date().toISOString().replace(/:/, '-').replace(/:/, '-');
var filename = datestr + ".csv";
var stream = fs.createWriteStream(filename, {flags:'a'});
UdpHandler.udpMsgQueue.forEach( function (item,index) {
var str = new Buffer(item).toString('ascii');
var obj = JSON.parse(str);
//console.log("receive:" + util.inspect(obj, {showHidden: false, depth: null}));
var newstr = obj["timestamp"]+","+obj["acc"][0] + ","+obj["acc"][1] + ","+obj["acc"][2] + ","
+obj["gyro"][0] + ","+obj["gyro"][1] + ","+obj["gyro"][2] + ","
// +obj["pos"][0] + ","+obj["pos"][1] + ","+obj["pos"][2] + ","
// +obj["rotation"][0] + ","+obj["rotation"][1] + ","+obj["rotation"][2] + ","
// +obj["rotation"][3] + ","+obj["rotation"][4] + ","+obj["rotation"][5] + ","
// +obj["rotation"][6] + ","+obj["rotation"][7] + ","+obj["rotation"][8]
+ "\n";
// console.log(obj["acc"]);
stream.write(newstr);
});
udpReceiver.clearQueue();
console.log("finished.");
stream.end();
}
}
setInterval(() => {
AndroidIMUStringReceiver();
}, 100);