-
Notifications
You must be signed in to change notification settings - Fork 12
/
nditracker.py
535 lines (434 loc) · 19.9 KB
/
nditracker.py
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# -*- coding: utf-8 -*-
"""Class implementing communication with NDI (Northern Digital) trackers"""
import sys
import os
import contextlib
from platform import system
from subprocess import call
from time import time
from serial.tools import list_ports #pylint: disable=import-error
from six import int2byte
from numpy import full, nan, reshape
from sksurgerycore.baseclasses.tracker import SKSBaseTracker
import ndicapy
from sksurgerynditracker.serial_utils.com_ports import \
fix_com_port_greater_than_9
@contextlib.contextmanager
def _open_logging(verbose):
"""
Opens either stdout out if verbose is true,
else os.devnull if verbose is false
"""
if verbose:
fileout = sys.stdout
else:
fileout = open(os.devnull, 'w', encoding = 'utf-8') #pylint: disable=consider-using-with
try:
yield fileout
finally:
if fileout is not sys.stdout:
fileout.close()
def _get_serial_port_name(configuration):
"""
Probes the system's serial ports to
find the name of the serial port and check we can connect
:return port_name: the name of the port
:raises: IOError if port not found or port probe fails
"""
with _open_logging(configuration.get('verbose', False)) as fileout:
serial_port = configuration.get("serial port", None)
ports_to_probe = configuration.get("ports to probe", 20)
serial_ports = list_ports.comports()
result = None
name = None
if ports_to_probe > len(serial_ports):
ports_to_probe = len(serial_ports)
if serial_port is None:
for port_no in range(ports_to_probe):
name = serial_ports[port_no].device
name = fix_com_port_greater_than_9(name)
result = ndicapy.ndiProbe(name)
print("Probing port: ", port_no, " got name: ", name,
" Result: ", result, file=fileout)
if result == ndicapy.NDI_OKAY:
break
else:
if isinstance(serial_port, int):
if serial_port < len(serial_ports):
name = serial_ports[serial_port].device
result = ndicapy.ndiProbe(name)
print("Probing port: ", serial_port, " got name: ", name,
" Result: ", result, file=fileout)
if isinstance(serial_port, str):
name = serial_port
result = ndicapy.ndiProbe(name)
print("Probing port: ", name,
" Result: ", result, file=fileout)
if result != ndicapy.NDI_OKAY:
raise IOError(
'Could not find any NDI device in '
f'{ports_to_probe} serial port candidates checked. '
'Please check the following:\n'
'\t1) Is an NDI device connected to your computer?\n'
'\t2) Is the NDI device switched on?\n'
'\t3) Do you have sufficient privilege to connect to '
'the device? (e.g. on Linux are you part of the "dialout" '
'group?)')
return name
class NDITracker(SKSBaseTracker):
"""
Class for communication with NDI trackers.
Should support Polaris, Aurora,
and Vega. Currently only tested with wireless tools on Vega
"""
def __init__(self, configuration):
"""
Creates an NDI tracker devices and connects to an NDI Tracker.
:param configuration: A dictionary containing details of the tracker.
tracker type: vega polaris aurora dummy
ip address:
port:
romfiles:
serial port:
ports to probe:
use quaternions: default is false
smoothing buffer: specify a buffer over which to average the
tracking, defaults to 1
:raises Exception: IOError, KeyError, OSError
"""
self._device = None
self._tool_descriptors = []
self._tracker_type = None
self._state = None
self._get_frame = None
self._get_transform = None
self._capture_string = None
self._configure(configuration)
super().__init__(configuration, tracked_objects = None)
if self._tracker_type == "vega":
self._connect_vega(configuration)
if self._tracker_type == "aurora":
self._connect_aurora(configuration)
if self._tracker_type == "polaris":
self._connect_polaris(configuration)
if self._tracker_type == "dummy":
self._device = True
self._initialise_ports()
self._enable_tools()
self._get_firmware_version()
self._set_use_bx_transforms()
self._state = 'ready'
def _set_use_bx_transforms(self):
"""
We'd like to use BX transforms as this sends binary
tracking data, so should be faster, however for
certain devices we can't do this. Here we check the
firmware version and set _use_bx_transforms to suit.
"""
self._get_frame = getattr(ndicapy, 'ndiGetBXFrame')
self._get_transform = getattr(ndicapy, 'ndiGetBXTransform')
self._capture_string = 'BX:0801'
firmware = self._get_firmware_version()
if firmware in (' AURORA Rev 007', ' AURORA Rev 008',
' Polaris Vega 008',
' Polaris Spectra Rev 006', ' Polaris Spectra Rev 007'):
self._get_frame = getattr(ndicapy, 'ndiGetTXFrame')
self._get_transform = getattr(ndicapy, 'ndiGetTXTransform')
self._capture_string = 'TX:0801'
def _get_firmware_version(self):
"""
Gets the device's firmware version, and sets
self._device_firmware_version
"""
device_firmware_version = 'unknown 00.0'
if self._tracker_type != 'dummy':
device_info = ndicapy.ndiVER(self._device, 0).split('\n')
for line in device_info:
if line.startswith('Freeze Tag:'):
device_firmware_version = line.split(':')[1]
return device_firmware_version
def _connect_vega(self, configuration):
self._connect_network(configuration)
self._read_sroms_from_file()
def _connect_polaris(self, configuration):
name = _get_serial_port_name(configuration)
self._connect_serial(name)
self._read_sroms_from_file()
def _connect_aurora(self, configuration):
name = _get_serial_port_name(configuration)
self._connect_serial(name)
self._find_wired_ports()
def _connect_network(self, configuration):
#try and ping first to save time with timeouts
param = '-n' if system().lower() == 'windows' else '-c'
ip_address = configuration.get("ip address")
port = configuration.get("port")
if call(['ping', param, '1', ip_address]) == 0:
self._device = ndicapy.ndiOpenNetwork(ip_address, port)
else:
raise IOError(f'Could not find a device at {ip_address}')
if not self._device:
raise IOError('Could not connect to network NDI device'
f'at {ip_address}')
ndicapy.ndiCommand(self._device, 'INIT:')
self._check_for_errors('Sending INIT command')
def _connect_serial(self, name):
"""
Attempts to open the serial port with name name
:raises: IOError if connection fails
"""
self._device = ndicapy.ndiOpen(name)
if not self._device:
raise IOError(f'Could not connect to serial NDI device at {name}')
ndicapy.ndiCommand(self._device, 'INIT:')
self._check_for_errors('Sending INIT command')
ndicapy.ndiCommand(self._device,
f'COMM:{ndicapy.NDI_115200:d}{ndicapy.NDI_8N1:03d}'
f'{ndicapy.NDI_NOHANDSHAKE:d}')
def _configure(self, configuration):
""" Reads a configuration dictionary
describing the tracker configuration.
and sets class variables.
raises: ValueError, KeyError
"""
if not "tracker type" in configuration:
raise KeyError("Configuration must contain 'Tracker type'")
tracker_type = configuration.get("tracker type")
if tracker_type in ("vega", "polaris", "aurora", "dummy"):
self._tracker_type = tracker_type
else:
raise ValueError(
"Supported trackers are 'vega', 'aurora', 'polaris', "
"and 'dummy'")
if self._tracker_type == "vega":
self._check_config_vega(configuration)
if self._tracker_type == "polaris":
self._check_config_polaris(configuration)
if self._tracker_type == "aurora":
pass
if self._tracker_type == "dummy":
self._check_config_dummy(configuration)
def _check_config_vega(self, configuration):
"""
Internal function to check configuration of a polaris vega
"""
if not "ip address" in configuration:
raise KeyError("Configuration for vega must contain"
"'ip address'")
if not "port" in configuration:
configuration.update({"port":8765})
if "romfiles" not in configuration:
raise KeyError("Configuration for vega and polaris must"
"contain a list of 'romfiles'")
for romfile in configuration.get("romfiles"):
if not os.path.exists(romfile):
raise FileNotFoundError(f"ROM file '{romfile}' not found.")
self._tool_descriptors.append({"description" : romfile})
def _check_config_polaris(self, configuration):
"""
Internal function to check configuration for polaris vicra or spectra
"""
if "romfiles" not in configuration:
raise KeyError("Configuration for vega and polaris must"
"contain a list of 'romfiles'")
for romfile in configuration.get("romfiles"):
if not os.path.exists(romfile):
raise FileNotFoundError(f"ROM file '{romfile}' not found.")
self._tool_descriptors.append({"description" : romfile})
def _check_config_dummy(self, configuration):
"""
Internal function to check configuration of a testing dummy
"""
if "romfiles" in configuration:
for romfile in configuration.get("romfiles"):
if not os.path.exists(romfile):
raise FileNotFoundError(f"ROM file '{romfile}' not found.")
self._tool_descriptors.append({"description" : romfile})
def close(self):
"""
Closes the connection to the NDI Tracker and
deletes the tracker device.
:raises Exception: ValueError
"""
if not self._device:
raise ValueError('close called with no NDI device')
if self._state == "tracking":
self.stop_tracking()
if self._tracker_type == "vega":
ndicapy.ndiCloseNetwork(self._device)
if self._tracker_type in ("aurora", "polaris"):
ndicapy.ndiClose(self._device)
self._device = None
self._state = None
def _read_sroms_from_file(self):
if not self._device:
raise ValueError('read srom called with no NDI device')
if self._state == "tracking":
self.stop_tracking()
#free ports that are waiting to be freed
ndicapy.ndiCommand(self._device, 'PHSR:01')
number_of_tools = ndicapy.ndiGetPHSRNumberOfHandles(self._device)
for tool_index in range(number_of_tools):
port_handle = ndicapy.ndiGetPHSRHandle(self._device, tool_index)
ndicapy.ndiCommand(self._device, f"PHF:{port_handle:02x}")
self._check_for_errors(f'freeing port handle {tool_index:02x}.')
for tool in self._tool_descriptors:
ndicapy.ndiCommand(self._device, 'PHRQ:*********1****')
port_handle = ndicapy.ndiGetPHRQHandle(self._device)
tool.update({"port handle" : port_handle})
if self._tracker_type == "aurora":
tool.update({"c_str port handle" : str(port_handle).encode()})
else:
tool.update({"c_str port handle" : int2byte(port_handle)})
self._check_for_errors(
f'getting srom file port handle {port_handle}.')
ndicapy.ndiPVWRFromFile(self._device, port_handle,
tool.get("description"))
self._check_for_errors(
f'setting srom file port handle {port_handle}.')
ndicapy.ndiCommand(self._device, 'PHSR:01')
def _initialise_ports(self):
"""Initialises each port in the list of tool descriptors"""
if not self._device:
raise ValueError('init ports called with no NDI device')
if not self._tracker_type == "dummy":
ndicapy.ndiCommand(self._device, 'PHSR:02')
for tool in self._tool_descriptors:
ndicapy.ndiCommand(self._device,
f'PINIT:{tool.get("port handle"):02x}')
self._check_for_errors('Initialising port handle '
f'{tool.get("port handle"):02x}.')
def _find_wired_ports(self):
"""For systems with wired tools, gets the number of tools plugged in
and sticks them in the tool descriptors list"""
if not self._device:
raise ValueError('find wired ports called with no NDI device')
ndicapy.ndiCommand(self._device, 'PHSR:02')
number_of_tools = ndicapy.ndiGetPHSRNumberOfHandles(self._device)
while number_of_tools > 0:
for ndi_tool_index in range(number_of_tools):
port_handle = ndicapy.ndiGetPHSRHandle(self._device,
ndi_tool_index)
self._tool_descriptors.append({"description" : ndi_tool_index,
"port handle" : port_handle,
"c_str port handle" :
int2byte(port_handle)})
ndicapy.ndiCommand(self._device,
f"PINIT:{port_handle:02x}")
ndicapy.ndiCommand(self._device, 'PHSR:02')
number_of_tools = ndicapy.ndiGetPHSRNumberOfHandles(self._device)
def _enable_tools(self):
if not self._device:
raise ValueError('enable tools called with no NDI device')
if not self._tracker_type == "dummy":
ndicapy.ndiCommand(self._device, "PHSR:03")
number_of_tools = ndicapy.ndiGetPHSRNumberOfHandles(self._device)
for tool_index in range(number_of_tools):
port_handle = ndicapy.ndiGetPHSRHandle(self._device, tool_index)
port_handle_already_present = False
for tool in self._tool_descriptors:
if tool.get("port handle") == port_handle:
port_handle_already_present = True
break
if not port_handle_already_present:
self._tool_descriptors.append({
"description" : tool_index,
"port handle" : port_handle,
"c_str port handle" :
int2byte(port_handle)})
mode = 'D'
ndicapy.ndiCommand(self._device,
f"PENA:{port_handle:02x}{mode}")
self._check_for_errors(f'Enabling port handle {port_handle}.')
def get_frame(self):
"""Gets a frame of tracking data from the NDI device.
:return:
port_numbers : list of port handles, one per tool
time_stamps : list of timestamps (cpu clock), one per tool
frame_numbers : list of framenumbers (tracker clock) one per tool
tracking : list of 4x4 tracking matrices, rotation and position,
or if use_quaternions is true, a list of tracking quaternions,
column 0-3 is the rotation as a quaternion (qw, qx, qy, qz),
column 4-6 is the translation (x,y,z).
tracking_quality : list the tracking quality, one per tool.
Note: The time stamp is based on the host computer clock. Read the
following extract from NDI's API Guide for advice on what to use:
"Use the frame number, and not the host computer clock, to identify when
data was collected. The frame number is incremented by 1 at a constant
rate of 60 Hz. Associating a time from the host computer clock to
replies from the system assumes that the duration of time between raw
data collection and when the reply is received by the host computer is
constant. This is not necessarily the case."
"""
port_handles = []
time_stamps = []
frame_numbers = []
tracking_rots = []
tracking_trans = []
tracking_quality = []
timestamp = time()
if not self._tracker_type == "dummy":
ndicapy.ndiCommand(self._device, self._capture_string)
for descriptor in self._tool_descriptors:
port_handles.append(descriptor.get("port handle"))
time_stamps.append(timestamp)
frame_numbers.append(self._get_frame(
self._device,
descriptor.get("c_str port handle")))
qtransform = self._get_transform(
self._device,
descriptor.get("c_str port handle"))
if not qtransform == "MISSING" and not qtransform == "DISABLED":
tracking_quality.append(qtransform[7])
transform = reshape(qtransform[0:7], [1, 7])
else:
tracking_quality.append(nan)
transform = full((1, 7), nan)
tracking_rots.append(transform[0][0:4])
tracking_trans.append(transform[0][4:7])
else:
for descriptor in self._tool_descriptors:
port_handles.append(descriptor.get(
"port handle"))
time_stamps.append(timestamp)
frame_numbers.append(0)
tracking_quality.append(0.0)
tracking_rots.append(full((1, 4), nan))
tracking_trans.append(full((1, 3), nan))
self.add_frame_to_buffer(port_handles, time_stamps, frame_numbers,
tracking_rots, tracking_trans, tracking_quality,
rot_is_quaternion = True)
return self.get_smooth_frame(port_handles)
def get_tool_descriptions(self):
""" Returns the port handles and tool descriptions """
port_handles = []
descriptions = []
for descriptor in self._tool_descriptors:
port_handles.append(descriptor.get("port handle"))
descriptions.append(descriptor.get("description"))
return port_handles, descriptions
def start_tracking(self):
"""
Tells the NDI devices to start tracking.
:raises Exception: ValueError
"""
if self._state != 'ready':
raise ValueError("""Called start tracking before device ready,
try calling connect first""")
ndicapy.ndiCommand(self._device, 'TSTART:')
self._check_for_errors('starting tracking.')
self._state = 'tracking'
def stop_tracking(self):
"""
Tells the NDI devices to stop tracking.
:raises Exception: ValueError
"""
ndicapy.ndiCommand(self._device, 'TSTOP:')
self._check_for_errors('stopping tracking.')
self._state = 'ready'
def _check_for_errors(self, message):
errnum = ndicapy.ndiGetError(self._device)
if errnum != ndicapy.NDI_OKAY:
ndicapy.ndiClose(self._device)
raise IOError(f'error when {message}. the error was: '
f'{ndicapy.ndiErrorString(errnum)}')