-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlStream.cs
More file actions
418 lines (357 loc) · 14.9 KB
/
PlStream.cs
File metadata and controls
418 lines (357 loc) · 14.9 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System;
using System.Text;
using System.Net;
using System.Threading;
using System.IO;
public enum PlTracker
{
Liberty,
Patriot,
G4,
Fastrak,
};
public class PlStream : MonoBehaviour
{
// port used for our UDP connection
public int port = 5123;
// tracker descriptors
public PlTracker tracker_type = PlTracker.Liberty;
public int max_systems = 1;
public int max_sensors = 1;
// slots used to store tracker output data
public bool[] active;
public uint[] digio;
public Vector3[] positions;
public Vector4[] orientations;
// internal state
private int max_slots;
private UdpClient udpClient;
private Thread conThread;
private bool stopListening;
// Use this for initialization
void Awake()
{
try
{
// there are some constraints between tracking systems
switch (tracker_type)
{
case PlTracker.Liberty:
// liberty is a single tracker system
max_systems = (max_systems > 1) ? 1 : max_systems;
max_sensors = (max_sensors > 16) ? 16 : max_sensors;
break;
case PlTracker.Patriot:
max_systems = (max_systems > 1) ? 1 : max_systems;
max_sensors = (max_sensors > 2) ? 2 : max_sensors;
break;
case PlTracker.G4:
// all G4 hubs (systems) have a maximum of 3 sensors
max_sensors = (max_sensors > 3) ? 3 : max_sensors;
break;
case PlTracker.Fastrak:
max_systems = (max_systems > 1) ? 1 : max_systems;
max_sensors = (max_sensors > 4) ? 4 : max_sensors;
break;
default:
throw new Exception("[polhemus] Unknown Tracker selected in PlStream::Awake().");
}
// set the number of slots
max_slots = max_sensors * max_systems;
// allocate resources for those slots
active = new bool[max_slots];
digio = new uint[max_slots];
positions = new Vector3[max_slots];
orientations = new Vector4[max_slots];
// initialize the slots
for (int i = 0; i < max_slots; ++i)
{
active[i] = false;
digio[i] = 0;
positions[i] = Vector3.zero;
orientations[i] = Vector4.zero;
}
switch (tracker_type)
{
case PlTracker.Liberty:
case PlTracker.Patriot:
conThread = new Thread(new ThreadStart(read_liberty));
break;
case PlTracker.G4:
conThread = new Thread(new ThreadStart(read_g4));
break;
case PlTracker.Fastrak:
conThread = new Thread(new ThreadStart(read_fastrak));
break;
default:
throw new Exception("[polhemus] Unknown Tracker selected in PlStream::Awake().");
}
// start the read thread
conThread.Start();
}
catch (Exception e)
{
Debug.Log(e);
Debug.Log("[polhemus] PlStream terminated in PlStream::Awake().");
Console.WriteLine("[polhemus] PlStream terminated in PlStream::Awake().");
}
}
// Update is called once per frame
void Update()
{
}
// read thread
private void read_g4()
{
stopListening = false;
udpClient = new UdpClient(port);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, port);
try
{
// create temp_active to mark slots
bool[] temp_active = new bool[max_slots];
// using G4 frame configuration
while (!stopListening)
{
byte[] receiveBytes = udpClient.Receive(ref groupEP);
//Debug.LogFormat("receiveBytes {0}", new object[]{ receiveBytes.Length });
// set slots to inactive
for (var i = 0; i < max_slots; ++i)
temp_active[i] = false;
// offset into buffer
int offset = 0;
// process body (32*3 = 96 bytes)
while (offset + 112 <= receiveBytes.Length)
{
//Debug.LogFormat("offset {0}", new object[] { offset });
// process header (16 bytes)
uint nHubID = BitConverter.ToUInt32(receiveBytes, offset + 0);
//int nFrame = BitConverter.ToInt32(receiveBytes, offset + 4);
uint dwSMap = BitConverter.ToUInt32(receiveBytes, offset + 8);
uint dwDgIO = BitConverter.ToUInt32(receiveBytes, offset + 12);
//Debug.LogFormat("nHubID = {0}, dwSMap = 0x{1:X}, dwDgIO = 0x{2:X}", new object[] { nHubID, dwSMap, dwDgIO });
// adjust offset
offset += 16;
Debug.LogFormat("offset {0}", new object[] { offset });
for (int i = 0, mask = 0x01; i < 3; ++i, mask <<= 1)
{
Debug.LogFormat("mask {0}", new object[] { mask });
// only read sensor results when valid
if ((dwSMap & mask) != 0)
{
// process sensor (32 bytes)
int nSenID = BitConverter.ToInt32(receiveBytes, offset);
// hub nums are 0-based.
uint slot = (nHubID) * 3 + (uint)nSenID;
//Debug.LogFormat("nSenID = {0}, slot = nHub*3+nSen = 0x{1:X}, max_slots = 0x{2:X}", new object[] { nSenID, slot, max_slots });
// test that we can actually store these results
if (slot > max_slots)
throw new Exception("[polhemus] HubID * 3 + SenID is greater than max_slots in PlStream::read().");
// here we have the positions
float t = BitConverter.ToSingle(receiveBytes, offset + 4);
float u = BitConverter.ToSingle(receiveBytes, offset + 8);
float v = BitConverter.ToSingle(receiveBytes, offset + 12);
// and quaternions
float w = BitConverter.ToSingle(receiveBytes, offset + 16);
float x = BitConverter.ToSingle(receiveBytes, offset + 20);
float y = BitConverter.ToSingle(receiveBytes, offset + 24);
float z = BitConverter.ToSingle(receiveBytes, offset + 28);
// store results
var index = (nHubID) * 3 + nSenID;
temp_active[index] = true;
digio[index] = dwDgIO;
positions[index] = new Vector3(t, u, v);
orientations[index] = new Vector4(w, x, y, z);
}
// always adjust offset
offset += 32;
}
}
// mark active slots
for (var i = 0; i < max_slots; ++i)
active[i] = temp_active[i];
}
}
catch (Exception e)
{
Debug.Log(e);
Debug.Log("[polhemus] PlStream terminated in PlStream::read_g4()");
Console.WriteLine("[polhemus] PlStream terminated in PlStream::read_g4().");
}
finally
{
udpClient.Close();
udpClient = null;
}
}
// read thread
private void read_liberty()
{
stopListening = false;
udpClient = new UdpClient(port);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, port);
var fileName = "Data" + DateTime.Now + ".txt";
StreamWriter sr = File.CreateText (fileName);
try
{
// create temp_active to mark slots
bool[] temp_active = new bool[max_slots];
// using hdr + pos + qtrn frame configuration for now
while (!stopListening)
{
byte[] receiveBytes = udpClient.Receive(ref groupEP);
// set slots to inactive
for (var i = 0; i < max_slots; ++i)
temp_active[i] = false;
// offset into buffer
int offset = 0;
while (offset + 40 <= receiveBytes.Length)
{
// process header (8 bytes)
int nSenID = System.Convert.ToInt32(receiveBytes[offset + 2]) - 1;
offset += 8;
if (nSenID > max_slots)
{
Console.WriteLine("[polhemus] SenID is greater than" + max_sensors.ToString() + ".");
throw new Exception("[polhemus] SenID is greater than" + max_sensors.ToString() + ".");
}
// process stylus (4 bytes)
uint bfStylus = BitConverter.ToUInt32(receiveBytes, offset);
offset += 4;
// process position (12 bytes)
float t = BitConverter.ToSingle(receiveBytes, offset);
float u = BitConverter.ToSingle(receiveBytes, offset + 4);
float v = BitConverter.ToSingle(receiveBytes, offset + 8);
offset += 12;
// process orientation (16 bytes)
float w = BitConverter.ToSingle(receiveBytes, offset);
float x = BitConverter.ToSingle(receiveBytes, offset + 4);
float y = BitConverter.ToSingle(receiveBytes, offset + 8);
float z = BitConverter.ToSingle(receiveBytes, offset + 12);
offset += 16;
// store results
temp_active[nSenID] = true;
digio[nSenID] = bfStylus;
positions[nSenID] = new Vector3(t, u, v);
orientations[nSenID] = new Vector4(w, x, y, z);
sr.WriteLine(receiveBytes);
}
sr.Close();
// mark active slots
for (var i = 0; i < max_slots; ++i)
active[i] = temp_active[i];
}
}
catch (Exception e)
{
Debug.Log(e);
Debug.Log("[polhemus] PlStream terminated in PlStream::read_liberty()");
Console.WriteLine("[polhemus] PlStream terminated in PlStream::read_liberty().");
}
finally
{
udpClient.Close();
udpClient = null;
}
}
// read thread
private void read_fastrak()
{
stopListening = false;
udpClient = new UdpClient(port);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, port);
try
{
// create temp_active to mark slots
bool[] temp_active = new bool[max_slots];
// using hdr + pos + qtrn frame configuration for now
var update = 0;
while (!stopListening)
{
++update;
byte[] receiveBytes = udpClient.Receive(ref groupEP);
// set slots to inactive
// disabled active tracking for fastrak because of the way it sends out its data:
// a frame per sensor. Fastrak is a wired system, so all sensors will always be
// active anyways
/*for (var i = 0; i < max_slots; ++i)
temp_active[i] = false;*/
// offset into buffer
int offset = 0;
while (offset + 33 <= receiveBytes.Length)
{
// process header (3 bytes)
uint nSenID = System.Convert.ToUInt32(receiveBytes[offset + 1]) - System.Convert.ToUInt32('1');
offset += 3;
if (nSenID > max_slots)
{
Console.WriteLine("[polhemus] SenID is greater than" + max_sensors.ToString() + ".");
throw new Exception("[polhemus] SenID is greater than" + max_sensors.ToString() + ".");
}
// process stylus (2 bytes: space and '0' or '1')
uint bfStylus = System.Convert.ToUInt32(receiveBytes[offset + 1]) - System.Convert.ToUInt32('0'); ;
offset += 2;
// process position (12 bytes)
float t = BitConverter.ToSingle(receiveBytes, offset);
float u = BitConverter.ToSingle(receiveBytes, offset + 4);
float v = BitConverter.ToSingle(receiveBytes, offset + 8);
offset += 12;
// process orientation (16 bytes)
float w = BitConverter.ToSingle(receiveBytes, offset);
float x = BitConverter.ToSingle(receiveBytes, offset + 4);
float y = BitConverter.ToSingle(receiveBytes, offset + 8);
float z = BitConverter.ToSingle(receiveBytes, offset + 12);
offset += 16;
// store results
temp_active[nSenID] = true;
digio[nSenID] = bfStylus;
positions[nSenID] = new Vector3(t, u, v);
orientations[nSenID] = new Vector4(w, x, y, z);
}
// mark active slots (always marking as active)
for (var i = 0; i < max_slots; ++i)
active[i] = temp_active[i];
}
}
catch (Exception e)
{
Debug.Log(e);
Debug.Log("[polhemus] PlStream terminated in PlStream::read_liberty()");
Console.WriteLine("[polhemus] PlStream terminated in PlStream::read_liberty().");
}
finally
{
udpClient.Close();
udpClient = null;
}
}
// cleanup
private void OnApplicationQuit()
{
try
{
// signal shutdown
stopListening = true;
// attempt to join for 500ms
if (!conThread.Join(500))
{
// force shutdown
conThread.Abort();
if (udpClient != null)
{
udpClient.Close();
udpClient = null;
}
}
}
catch (Exception e)
{
Debug.Log(e);
Debug.Log("[polhemus] PlStream was unable to close the connection thread upon application exit. This is not a critical exception.");
}
}
}