/
app.js
327 lines (281 loc) · 8.1 KB
/
app.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
const Quaternion = require('quaternion')
const ws = require('ws')
const dgram = require('node:dgram')
const CryptoJS = require("crypto-js");
const DaydreamControllerNode = require('./daydream_node.js')
/*
first 4 bytes - message type
( 0 = heartbeat
1 = rotation
2 = gyro
3 = handshake
4 = accelerometer)
next 8 bytes - packet id (+1 every time)
rotation:
(floats)
next 4 bytes - rotation quat x
next 4 bytes - rotation quat y
next 4 bytes - rotation quat z
next 4 bytes - rotation quat w
gyro:
(floats)
next 4 bytes - gyro rate x
next 4 bytes - gyro rate y
next 4 bytes - gyro rate z
64 byte packets
*/
var packetSeq = 0n
var handShaken = false
var connectionCloser = 0
var owoIsAlive = false
const owoConn = dgram.createSocket('udp4')
const dayDreamID = process.argv[2]
const dayDreamMac = hashStringToMAC(dayDreamID)
let trackerId = parseInt(process.argv[4]);
if (isNaN(trackerId)) trackerId = 0
function hashStringToMAC(str) {
var hash = CryptoJS.SHA1(str);
var hexString = hash.toString(CryptoJS.enc.Hex);
var macAddress = hexString.substr(0, 12);
macAddress = macAddress.match(/.{1,2}/g).join(":");
macAddress = macAddress.toUpperCase()
return macAddress;
}
function closeWhenTimeout(){
clearTimeout(connectionCloser)
connectionCloser = setTimeout(()=>{
console.log("Connection Timed Out, Retrying with Handshake..")
handShaken = false
packetSeq = 0n
owoIsAlive = false
sendHandshake()
}, 10000)
}
function connectToOwo(){
closeWhenTimeout()
let address = 'localhost'
if (process.argv[3]) {
address = process.argv[3]
}
console.log(`Connectin using address ${address}`)
owoConn.connect(6969, address)
}
function getPacketSequence(){
packetSeq = packetSeq + 1n
return packetSeq++
}
function sendHandshake(){
//const buffer = Buffer.allocUnsafe(12) // 12
//buffer.writeUInt32BE(3,0)
//buffer.writeBigInt64BE(getPacketSequence(),4)
const firmware_name = "DayDreamOwo\0"
const packetBuffer = Buffer.allocUnsafe(
4 + // packet_type
4 + // board_type
4 + // imu_type
4 + // mcu_type
4 * 3 + // imu_info
4 + // firmware_build
1 + // firmware_name_length
Buffer.byteLength(firmware_name) + // firmware_name
6 +// mac
1
);
// packet_type
packetBuffer.writeUInt32BE(3, 0);
// board_type
packetBuffer.writeUInt32BE(0, 4);
// imu_type
packetBuffer.writeUInt32BE(0, 8);
// mcu_type
packetBuffer.writeUInt32BE(0, 12);
// imu_info
packetBuffer.writeUInt32BE(0, 16);
packetBuffer.writeUInt32BE(0, 20);
packetBuffer.writeUInt32BE(0, 24);
// firmware_build
packetBuffer.writeUInt32BE(2, 28);
// firmware_name_length
//packetBuffer.writeUInt8(firmware_name.length, 32);
// firmware_name
packetBuffer.write(firmware_name, 32);
packetBuffer.writeUInt8(0, firmware_name.length);
// mac
let mac = dayDreamMac.split(':');
for (let i = 0; i < mac.length/2; i++) {
packetBuffer.writeUInt8(parseInt(mac.slice(i*2, i*2+2), 16),33+firmware_name.length+i);
}
packetBuffer.writeUInt8(0, 33 + 6);
handShaken = true
owoConn.send(packetBuffer, (err) => {
console.log("Sending Handshake")
sendSensorInfo();
closeWhenTimeout()
})
}
function sendSensorInfo(){
const buffer = Buffer.allocUnsafe(4 + 8 + 1 + 1 + 1)
buffer.writeUInt32BE(15,0)
buffer.writeBigInt64BE(getPacketSequence(),4)
buffer.writeUInt8(trackerId,12)
buffer.writeUInt8(1,13)
buffer.writeUInt8(0,14)
owoConn.send(buffer, (err) => {
console.log(`Sent Sensor Info ${trackerId}`)
sendHeartBeat()
owoIsAlive = true
})
}
function sendxSensorInfo(){
const buffer = Buffer.allocUnsafe(16)
buffer.writeUInt32BE(15,0)
buffer.writeUInt32BE(trackerId,4)
buffer.writeUInt32BE(1,8)
buffer.writeUInt32BE(0,12)
owoConn.send(buffer, (err) => {
console.log("Sent Sensor Info")
sendHeartBeat()
owoIsAlive = true
})
}
function sendHeartBeat(){
const buffer = Buffer.allocUnsafe(16)
buffer.writeUInt32BE(0,0)
buffer.writeBigInt64BE(getPacketSequence(),4)
owoConn.send(buffer, (err) => {})
}
function sendPong(ping){
const buffer = Buffer.allocUnsafe(16)
buffer.writeUInt32BE(10,0)
buffer.writeBigInt64BE(getPacketSequence(),4)
buffer.writeInt32BE(ping.readInt32BE(12), 12)
owoConn.send(buffer, (err) => {})
}
// next 4 bytes - gyro rate x
// next 4 bytes - gyro rate y
// next 4 bytes - gyro rate z
function sendAccelerometer(daydreamData){
const buffer = Buffer.allocUnsafe(24)
buffer.writeUInt32BE(4,0)
buffer.writeBigInt64BE(getPacketSequence(),4)
buffer.writeFloatBE(daydreamData.xAcc,12)
buffer.writeFloatBE(daydreamData.yAcc,16)
buffer.writeFloatBE(daydreamData.zAcc,20)
owoConn.send(buffer, (err) => {
//console.log("Acceleration Sent")
})
}
// next 4 bytes - gyro rate x
// next 4 bytes - gyro rate y
// next 4 bytes - gyro rate z
function sendGyro(daydreamData){
// try{
const buffer = Buffer.allocUnsafe(24)
buffer.writeUInt32BE(2,0)
buffer.writeBigInt64BE(getPacketSequence(),4)
buffer.writeFloatBE(daydreamData.xGyro,12)
buffer.writeFloatBE(daydreamData.yGyro,16)
buffer.writeFloatBE(daydreamData.zGyro,20)
owoConn.send(buffer, (err) => {
//console.log("Gyro Sent", buffer)
})
// }catch(e){
// console.log("invalid json", daydreamData)
// }
}
// next 4 bytes - gyro rate x
// next 4 bytes - gyro rate y
// next 4 bytes - gyro rate z
// next 4 bytes - gyro rate w
function sendRotation(daydreamData){
// try{
const buffer = Buffer.allocUnsafe(4 + 8 + 1 + 1 + 4 + 4 + 4 + 4 + 1);
buffer.writeUInt32BE(17,0) //type
buffer.writeBigInt64BE(getPacketSequence(),4) //seq
buffer.writeUintBE(trackerId, 12, 1) // trackerid
buffer.writeUintBE(1, 13, 1); // datattype
// using Quaternion node library, maths :/
let q = new Quaternion([daydreamData.xOri,-daydreamData.zOri,daydreamData.yOri]) // z and y is flipped for some reason
let sqrMagnitude = q.normSq()//(state.xOri ^2) + (state.yOri ^2) + (state.zOri ^2)
if (sqrMagnitude > 0){
let angle = Math.sqrt(sqrMagnitude)
q = q.scale(angle)
q = Quaternion.fromAxisAngle([q.x,q.y,q.z],angle)
// q.w = sqrMagnitude
} else {
q = new Quaternion(0,[0,0,0])
}
buffer.writeFloatBE(q.x,14)
buffer.writeFloatBE(q.y,18)
buffer.writeFloatBE(q.z,22)
buffer.writeFloatBE(q.w,26)
buffer.writeUintBE(255, 30, 1);
owoConn.send(buffer, (err) => {
//console.log(`Tracker ${trackerId}:Rotation Sent`)
})
// }catch(e){
// console.log("invalid json", daydreamData)
// }
}
// next 4 bytes - gyro rate x
// next 4 bytes - gyro rate y
// next 4 bytes - gyro rate z
// next 4 bytes - gyro rate w
function sendxRotation(daydreamData){
// try{
const buffer = Buffer.allocUnsafe(29)
buffer.writeUInt32BE(1,0)
buffer.writeBigInt64BE(getPacketSequence(),4)
// using Quaternion node library, maths :/
let q = new Quaternion([daydreamData.xOri,-daydreamData.zOri,daydreamData.yOri]) // z and y is flipped for some reason
let sqrMagnitude = q.normSq()//(state.xOri ^2) + (state.yOri ^2) + (state.zOri ^2)
if (sqrMagnitude > 0){
let angle = Math.sqrt(sqrMagnitude)
q = q.scale(angle)
q = Quaternion.fromAxisAngle([q.x,q.y,q.z],angle)
// q.w = sqrMagnitude
} else {
q = new Quaternion(0,[0,0,0])
}
buffer.writeFloatBE(q.x,12)
buffer.writeFloatBE(q.y,16)
buffer.writeFloatBE(q.z,20)
buffer.writeFloatBE(q.w,24)
buffer.writeUintBE(trackerId, 12, 1) // trackerid
owoConn.send(buffer, (err) => {
//console.log("Rotation Sent")
})
// }catch(e){
// console.log("invalid json", daydreamData)
// }
}
owoConn.on('message', (msg, rinfo)=>{
let msgType = msg.readUInt32BE(0)
if (msgType > 100) {
msgType = msg.readUInt32LE(0)
}
switch (msgType){
case 1:
sendHeartBeat()
break
case 10:
sendPong(msg)
break
}
owoIsAlive = true
closeWhenTimeout()
})
daydream = new DaydreamControllerNode()
daydream.onStateChange(function(data){
if (owoIsAlive){
//sendGyro(data)
//sendAccelerometer(data)
sendRotation(data)
}
})
if (dayDreamID === 'scan') {
daydream.scan()
}else{
connectToOwo()
daydream.connect(dayDreamID)
}