-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAQToolbox.py
executable file
·1225 lines (880 loc) · 40.3 KB
/
DAQToolbox.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Universiteit Antwerpen
# Functional Morphology
# Falk Mielke
# 2019/06/06
# contains parts as ADAPTATION of https://github.com/adafruit/Adafruit_Python_GPIO
# used to connect I2C via the FT232H interface
#
# THIS IS THE REDUCED VERSION OF THE IOTOOLBOX
################################################################################
### Libraries ###
################################################################################
import os as OS
import sys as SYS
import select as SEL # for timed user input
import re as RE # regular expressions
import time as TI # time, for process pause and date
import atexit as EXIT # commands to shut down processes
import subprocess as SP
import threading as TH # threading for trigger
import queue as QU # data buffer
from collections import deque as DEQue # double ended queue
import numpy as NP # numerics
import pandas as PD # data storage
import scipy.signal as SIG
import math as MATH
import matplotlib as MP # plotting
MP.use('TkAgg')
import matplotlib.pyplot as MPP # plot control
# import ftdi1 as FTDI
# import pyftdi as FTDI
uldaq_skip_import = False
if not uldaq_skip_import:
import uldaq as UL # MCC DAQ negotiation
import logging
logger = logging.getLogger(__name__)
### MCC DAQ drivers and library
# follow readme in https://github.com/mccdaq/uldaq
# download $ wget -N https://github.com/mccdaq/uldaq/releases/download/v1.2.0/libuldaq-1.2.0.tar.bz2
# extract $ tar -xvjf libuldaq-1.2.0.tar.bz2 && cd libuldaq-1.2.0
# build $ ./configure && make -j2 && sudo make install -j2
# if "make" fails, you might need to do: ln -s /usr/bin/autom-1.16 /usr/bin/aclocal-1.14 && ln -s /usr/bin/automake-1.16 /usr/bin/automake-1.14
# pip install uldaq
################################################################################
### Global Specifications ###
################################################################################
coordinates = ['x', 'y', 'z']
################################################################################
### Plotting ###
################################################################################
the_font = { \
# It's really sans-serif, but using it doesn't override \sffamily, so we tell Matplotlib
# to use the "serif" font because then Matplotlib won't ask for any special families.
# 'family': 'serif' \
# , 'serif': 'Iwona' \
'family': 'sans-serif'
, 'sans-serif': 'DejaVu Sans'
, 'size': 10#*1.27 \
}
def PreparePlot():
# select some default rc parameters
MP.rcParams['text.usetex'] = True
MPP.rc('font',**the_font)
# Tell Matplotlib how to ask TeX for this font.
# MP.texmanager.TexManager.font_info['iwona'] = ('iwona', r'\usepackage[light,math]{iwona}')
MP.rcParams['text.latex.preamble'] = [\
r'\usepackage{upgreek}'
, r'\usepackage{cmbright}'
, r'\usepackage{sansmath}'
]
MP.rcParams['pdf.fonttype'] = 42 # will make output TrueType (whatever that means)
def PolishAx(ax):
# axis cosmetics
ax.get_xaxis().set_tick_params(which='both', direction='out')
ax.get_yaxis().set_tick_params(which='both', direction='out')
ax.tick_params(top = False)
ax.tick_params(right = False)
# ax.tick_params(left=False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.spines['left'].set_visible(True)
ax.spines['right'].set_visible(False)
################################################################################
### Base GPIO Functionality ###
################################################################################
RISING = 1
FALLING = 2
BOTH = 3
PUD_OFF = 0
PUD_DOWN = 1
PUD_UP = 2
################################################################################
### MCC USB1608G DAQ ###
################################################################################
instrument_labels = { '01DF5B18': 'blue' \
, '01DF5AFB': 'green'
}
if not uldaq_skip_import:
status_dict = { \
UL.ScanStatus.IDLE: 'idle' \
, UL.ScanStatus.RUNNING: 'running' \
}
class MCCDAQ(object):
# generic device functions
# for output pin values
LOW = 0
HIGH = 255
# analog input recording mode
if not uldaq_skip_import:
recording_mode = UL.ScanOption.BLOCKIO
# recording_mode = UL.ScanOption.CONTINUOUS
alive = False
#______________________________________________________________________
# constructor
#______________________________________________________________________
def __init__(self, device_nr = 0):
self.daq_device = None
self.times = {} # storage of timing info
# for mcc daq configuration
self.range_index = 0
self.AssembleAndConnect(descriptor_index = device_nr)
self.label = str(self)
EXIT.register(self.Quit)
def AssembleAndConnect(self, descriptor_index = 0):
# connect to the DAQ device
try:
interface_type = UL.InterfaceType.USB
# Get descriptors for all of the available DAQ devices.
devices = UL.get_daq_device_inventory(interface_type)
number_of_devices = len(devices)
if number_of_devices == 0:
raise Exception('Error: No DAQ devices found')
# print('Found', number_of_devices, 'DAQ device(s):')
# for i in range(number_of_devices):
# print(' ', devices[i].product_name, ' (', devices[i].unique_id, ')', sep='')
# Create the DAQ device object associated with the specified descriptor index.
self.daq_device = UL.DaqDevice(devices[descriptor_index])
### digital input
port_types_index = 0
# Get the DioDevice object and verify that it is valid.
self.digital_io = self.daq_device.get_dio_device()
if self.digital_io is None:
raise Exception('Error: The DAQ device does not support digital input')
# Get the port types for the device(AUXPORT, FIRSTPORTA, ...)
dio_info = self.digital_io.get_info()
port_types = dio_info.get_port_types()
if port_types_index >= len(port_types):
port_types_index = len(port_types) - 1
self.port = port_types[port_types_index]
### analog input
# Get the AiDevice object and verify that it is valid.
self.analog_input = self.daq_device.get_ai_device()
if self.analog_input is None:
raise Exception('Error: The DAQ device does not support analog input')
# Verify that the specified device supports hardware pacing for analog input.
self.ai_info = self.analog_input.get_info()
if not self.ai_info.has_pacer():
raise Exception('\nError: The specified DAQ device does not support hardware paced analog input')
### connect
# Establish a connection to the DAQ device.
# print (dir(descriptor))
self.daq_device.connect()
except Exception as e:
print('constructor fail\n', e)
print('\nConnected to', str(self), '.')
# for digital I/O
def SetPins(self):
# implemented by sub class
raise TypeError('Digital pin setup not implemented!')
pass
#______________________________________________________________________
# DAQ status
#______________________________________________________________________
def GetDAQStatus(self):
return self.analog_input.get_scan_status()
def DAQIsRunning(self):
return self.analog_input.get_scan_status()[0] is UL.ScanStatus.RUNNING
def DAQIsIdle(self):
return self.analog_input.get_scan_status()[0] is UL.ScanStatus.IDLE
#______________________________________________________________________
# recording preparation
#______________________________________________________________________
def CountChannels(self, n_channels, channel_labels):
### channel settings
if (n_channels is None) and (channel_labels is None):
raise Exception('Error: please provide either channel_labels or n_channels.')
if channel_labels is None:
# label by number of channels per default
self.channel_labels = [ 'a_%i' % (nr) for nr in range(n_channels) ]
else:
# count channels from the labels
self.channel_labels = channel_labels
n_channels = len(channel_labels)
self.low_channel = 0 # record from channel...
self.high_channel = n_channels-1 # ... to (incl) channel
self.n_channels = n_channels
def PrepareAnalogAcquisition(self):
# generate settings for a recording
# note that a single prep is usually enough for successive recordings of the same kind
try:
# recording_duration = 3 # s
samples_per_channel = int(self.recording_duration*self.sampling_rate)
# The default input mode is SINGLE_ENDED.
input_mode = UL.AiInputMode.SINGLE_ENDED
# If SINGLE_ENDED input mode is not supported, set to DIFFERENTIAL.
if self.ai_info.get_num_chans_by_mode(UL.AiInputMode.SINGLE_ENDED) <= 0:
input_mode = UL.AiInputMode.DIFFERENTIAL
# print (dir(UL.AInScanFlag))
flags = UL.AInScanFlag.DEFAULT
# Get the number of channels and validate the high channel number.
number_of_channels = self.ai_info.get_num_chans_by_mode(input_mode)
if self.high_channel >= number_of_channels:
self.high_channel = number_of_channels - 1
channel_count = self.high_channel - self.low_channel + 1
# self.StdOut (input_mode, channel_count)
# Get a list of supported ranges and validate the range index.
ranges = self.ai_info.get_ranges(input_mode)
if self.range_index >= len(ranges):
self.range_index = len(ranges) - 1
trigger_types = self.ai_info.get_trigger_types()
# [<TriggerType.POS_EDGE: 1>, <TriggerType.NEG_EDGE: 2>, <TriggerType.HIGH: 4>, <TriggerType.LOW: 8>]
self.analog_input.set_trigger(trigger_types[1], 0, 0, 0, 0)
# Allocate a buffer to receive the data.
self.buffer = UL.create_float_buffer(channel_count, samples_per_channel)
recording_mode = self.recording_mode
# store settings (keywords for self.analog_input.a_in_scan)
self.recording_settings = dict( \
low_channel = self.low_channel \
, high_channel = self.high_channel \
, input_mode = input_mode \
, analog_range = ranges[self.range_index] \
, samples_per_channel = samples_per_channel \
, rate = self.sampling_rate \
, options = recording_mode \
, flags = flags \
, data = self.buffer \
)
except Exception as e:
print('\n', e)
#______________________________________________________________________
# I/O
#______________________________________________________________________
def NOOP(self):
# defined by subclasses
pass
def __str__(self):
descriptor = self.daq_device.get_descriptor()
return descriptor.dev_string + ' "' + instrument_labels[descriptor.unique_id] + '"'
#______________________________________________________________________
# Destructor
#______________________________________________________________________
def __enter__(self):
# required for context management ("with")
return self
def __exit__(self, exc_type, exc_value, traceback):
# exiting when in context manager
if not self.alive:
return
self.Quit()
def Quit(self):
# safely exit
if not self.alive:
SYS.exit()
return
if self.daq_device:
# Stop the acquisition if it is still running.
if not self.DAQIsIdle():
self.analog_input.scan_stop()
if self.daq_device.is_connected():
self.daq_device.disconnect()
self.daq_device.release()
print('safely exited %s.' % (self.label))
self.alive = False
SYS.exit()
################################################################################
### MCC DAQ single digital input ###
################################################################################
class DAQ_DigitalInput(MCCDAQ):
# generic device functions
#______________________________________________________________________
# Construction
#______________________________________________________________________
def __init__(self, digital_pin, scan_frq, *args, **kwargs):
# variable preparation
self.digital_pin = digital_pin
self.scan_frq = scan_frq
super(DAQ_DigitalInput, self).__init__(*args, **kwargs)
self.SetPins()
self.alive = True
def SetPins(self):
# set pin to input
self.digital_io.d_config_bit(self.port, self.digital_pin, UL.DigitalDirection.INPUT)
#______________________________________________________________________
# I/O
#______________________________________________________________________
def Read(self):
# read out the trigger bit
return self.digital_io.d_bit_in(self.port, self.digital_pin)
def Record(self):
previous = self.Read()
# loop until bit changes
t0 = TI.time()
while True:
TI.sleep(1/self.scan_frq)
# check trigger
current = self.Read()
if current != previous:
print (TI.time(), current)
previous = current
if TI.time() > (t0 + 10):
break
#______________________________________________________________________
# Test
#______________________________________________________________________
def TestMCCPinIn(pin_nr = None):
if pin_nr is None:
return
daq = DAQ_DigitalInput(digital_pin = pin_nr, scan_frq = 1e6)
daq.Record()
################################################################################
### MCC DAQ single digital output ###
################################################################################
class DAQ_DigitalOutput(MCCDAQ):
# generic device functions
#______________________________________________________________________
# Construction
#______________________________________________________________________
def __init__(self, digital_pin, *args, **kwargs):
# variable preparation
self.digital_pin = digital_pin
super(DAQ_DigitalOutput, self).__init__(*args, **kwargs)
self.SetPins()
self.alive = True
def SetPins(self):
# set pin to output
self.digital_io.d_config_bit(self.port, self.digital_pin, UL.DigitalDirection.OUTPUT)
#______________________________________________________________________
# I/O
#______________________________________________________________________
def LED(self, value_bool):
self.digital_io.d_bit_out(self.port, self.digital_pin, self.HIGH if value_bool else self.LOW)
def Flash(self, times = 1, duration = 1):
self.LED(False)
for _ in range(times):
self.LED(True)
TI.sleep(duration)
self.LED(False)
TI.sleep(duration)
#______________________________________________________________________
# test
#______________________________________________________________________
def TestMCCPinOut(pin_nr = None):
if pin_nr is None:
return
daq = DAQ_DigitalOutput(digital_pin = pin_nr)
daq.Flash(times = 3, duration = 1)
################################################################################
### MCC DAQ Analog Recording ###
################################################################################
class AnalogInput(MCCDAQ):
# generic device functions
#______________________________________________________________________
# Construction
#______________________________________________________________________
def __init__(self, sampling_rate = 1e1, recording_duration = 1. \
, n_channels = None, channel_labels = None \
, scan_frq = 1e3 \
, *args, **kwargs \
):
self.sampling_rate = sampling_rate
self.recording_duration = recording_duration
self.scan_frq = scan_frq
# setup for analog input
self.CountChannels(n_channels, channel_labels)
super(AnalogInput, self).__init__(*args, **kwargs)
self.PrepareAnalogAcquisition()
self.alive = True
#______________________________________________________________________
# I/O
#______________________________________________________________________
def Record(self, wait_time = None):
self.times['start'] = TI.time()
self.rate = self.analog_input.a_in_scan(**self.recording_settings)
if wait_time is None:
self.Wait()
else:
TI.sleep( wait_time )
def Wait(self, verbose = True):
# sleeps until recording is finished.
# counter = 0
while True:
TI.sleep(1/self.scan_frq)
# if (not self.trigger.waiting):
# self.Stop()
# # for precise daq buffer sync
# counter += 1
# if (counter % 100) == 0:
# self.StoreAIPointer()
# also stop when daq was lost
if self.DAQIsIdle():
self.times['stop'] = TI.time()
break
if verbose:
print('done recording')# %i.' % (self.recording_counter))
#______________________________________________________________________
# Data reporting
#______________________________________________________________________
def RetrieveOutput(self, verbose = False):
if verbose:
print ('\t', self.rate, 'Hz on DAQ')
# take data from the buffer
data = NP.array(self.buffer).reshape([-1,len(self.channel_labels)])
data = PD.DataFrame(data, columns = self.channel_labels)
data.index = NP.arange(data.shape[0]) / self.rate + self.times['start']
data.index.name = 'time'
return self.times, data
################################################################################
### Force Plate Settings ###
################################################################################
def AssembleForceplateSettings():
forceplate_settings = {'amti': {}, 'joystick': {}, 'kistler': {}, 'dualkistler': {}, 'dualkistler2': {}, 'test': {}}
### AMTI
forceplate_settings['amti']['measures'] \
= ['forces', 'moments']
forceplate_settings['amti']['data_columns'] \
= { \
'forces': ['F_x', 'F_y', 'F_z'] \
, 'moments': ['M_x', 'M_y', 'M_z'] \
}
forceplate_settings['amti']['channel_order'] \
= ['F_x', 'F_y', 'F_z', 'M_x', 'M_y', 'M_z'] # channel order on the MCC board
forceplate_settings['amti']['v_range'] \
= 10. # V
forceplate_settings['amti']['colors'] \
= { 'F_x': (0.2,0.2,0.8), 'F_y': (0.2,0.8,0.2), 'F_z': (0.8,0.2,0.2) \
, 'M_x': (0.2,0.2,0.8), 'M_y': (0.2,0.8,0.2), 'M_z': (0.8,0.2,0.2) \
}
### Kistler
forceplate_settings['kistler']['measures'] \
= ['Fxy', 'Fz']
forceplate_settings['kistler']['data_columns'] \
= { \
'Fxy': ['Fx12', 'Fx34', 'Fy14', 'Fy23'] \
, 'Fz': ['Fz1', 'Fz2', 'Fz3', 'Fz4'] \
}
forceplate_settings['kistler']['channel_order'] \
= ['Fx12', 'Fx34', 'Fy14', 'Fy23', 'Fz1', 'Fz2', 'Fz3', 'Fz4'] # channel order on the MCC board
forceplate_settings['kistler']['v_range'] \
= 10. # V
forceplate_settings['kistler']['colors'] \
= { 'Fx12': (0.4,0.2,0.8), 'Fx34': (0.2,0.4,0.8), 'Fy14': (0.2,0.8,0.4), 'Fy23': (0.4,0.8,0.2) \
, 'Fz1': (0.8,0.2,0.4), 'Fz2': (0.8,0.4,0.2), 'Fz3': (0.8,0.4,0.4), 'Fz4': (0.8,0.2,0.2) \
}
# kistler_calib_gain100_1000
### Dual Kistler
forceplate_settings['dualkistler']['measures'] \
= ['Fxy', 'Fz']
forceplate_settings['dualkistler']['data_columns'] \
= { \
'Fxy': ['AFx12', 'AFx34', 'AFy14', 'AFy23'] \
+ ['BFx12', 'BFx34', 'BFy14', 'BFy23'] \
, 'Fz': ['AFz1', 'AFz2', 'AFz3', 'AFz4'] \
+ ['BFz1', 'BFz2', 'BFz3', 'BFz4'] \
}
forceplate_settings['dualkistler']['channel_order'] \
= [ 'AFx12', 'AFx34', 'AFy14', 'AFy23' \
, 'BFx12', 'BFx34', 'BFy14', 'BFy23' \
, 'AFz1', 'AFz2', 'AFz3', 'AFz4' \
, 'BFz1', 'BFz2', 'BFz3', 'BFz4' \
] # channel order on the MCC board
forceplate_settings['dualkistler']['v_range'] \
= 10. # V
forceplate_settings['dualkistler']['colors'] \
= { 'AFx12': (0.4, 0.2, 0.8), 'AFx34': (0.2, 0.4, 0.8), 'AFy14': (0.2, 0.8, 0.4), 'AFy23': (0.4, 0.8, 0.2) \
, 'BFx12': (0.4, 0.2, 0.8), 'BFx34': (0.2, 0.4, 0.8), 'BFy14': (0.2, 0.8, 0.4), 'BFy23': (0.4, 0.8, 0.2) \
, 'AFz1': (0.8, 0.2, 0.4), 'AFz2': (0.8, 0.4, 0.2), 'AFz3': (0.8, 0.4, 0.4), 'AFz4': (0.8, 0.2, 0.2) \
, 'BFz1': (0.8, 0.2, 0.4), 'BFz2': (0.8, 0.4, 0.2), 'BFz3': (0.8, 0.4, 0.4), 'BFz4': (0.8, 0.2, 0.2) \
}
# kistler_calib_gain100_1000
### Dual Kistler
forceplate_settings['dualkistler2']['measures'] \
= ['Fxy', 'Fz']
forceplate_settings['dualkistler2']['data_columns'] \
= { \
'Fxy': ['CFx12', 'CFx34', 'CFy14', 'CFy23'] \
+ ['DFx12', 'DFx34', 'DFy14', 'DFy23'] \
, 'Fz': ['CFz1', 'CFz2', 'CFz3', 'CFz4'] \
+ ['DFz1', 'DFz2', 'DFz3', 'DFz4'] \
}
forceplate_settings['dualkistler2']['channel_order'] \
= [ 'CFx12', 'CFx34', 'CFy14', 'CFy23' \
, 'DFx12', 'DFx34', 'DFy14', 'DFy23' \
, 'CFz1', 'CFz2', 'CFz3', 'CFz4' \
, 'DFz1', 'DFz2', 'DFz3', 'DFz4' \
] # channel order on the MCC board
forceplate_settings['dualkistler2']['v_range'] \
= 10. # V
forceplate_settings['dualkistler2']['colors'] \
= { 'CFx12': (0.4, 0.2, 0.8), 'CFx34': (0.2, 0.4, 0.8), 'CFy14': (0.2, 0.8, 0.4), 'CFy23': (0.4, 0.8, 0.2) \
, 'DFx12': (0.4, 0.2, 0.8), 'DFx34': (0.2, 0.4, 0.8), 'DFy14': (0.2, 0.8, 0.4), 'DFy23': (0.4, 0.8, 0.2) \
, 'CFz1': (0.8, 0.2, 0.4), 'CFz2': (0.8, 0.4, 0.2), 'CFz3': (0.8, 0.4, 0.4), 'CFz4': (0.8, 0.2, 0.2) \
, 'DFz1': (0.8, 0.2, 0.4), 'DFz2': (0.8, 0.4, 0.2), 'DFz3': (0.8, 0.4, 0.4), 'DFz4': (0.8, 0.2, 0.2) \
}
# kistler_calib_gain100_1000
### Joystick
forceplate_settings['joystick']['measures'] \
= ['forces', 'moments']
forceplate_settings['joystick']['data_columns'] \
= { \
'forces': ['x'] \
, 'moments': ['y'] \
}
forceplate_settings['joystick']['channel_order'] \
= ['x', 'y'] # channel order on the MCC board
forceplate_settings['joystick']['v_range'] \
= 3.3 # V
forceplate_settings['joystick']['colors'] \
= {'x': (0.2,0.2,0.8), 'y': (0.2,0.8,0.2), 'z': (0.8,0.2,0.2)}
forceplate_settings['test']['channel_order'] \
= ['v'] # channel order on the MCC board
forceplate_settings['test']['v_range'] \
= 10. # V
forceplate_settings['test']['colors'] \
= {'v': (0.2,0.2,0.2)}
return forceplate_settings
forceplate_settings = AssembleForceplateSettings()
################################################################################
### MCC USB1608G DAQ, continuous acquisition ###
################################################################################
class Oscilloscope(AnalogInput):
# a MCC DAQ device, wired for analog input
if not uldaq_skip_import:
recording_mode = UL.ScanOption.CONTINUOUS
def __init__(self, *args, **kwargs):
super(Oscilloscope, self).__init__(*args, **kwargs)
self.PreparePlot()
def Exit(self, event):
if event.key in ['q', 'e', 'escape', '<space>']:
self.playing = False
def PreparePlot(self):
self.fig, self.ax = MPP.subplots(1, 1)
# self.ax.set_ylim(-2.5, 2.5)
self.ax.set_ylim(-10.5, 10.5)
self.ax.yaxis.tick_right()
self.ax.yaxis.set_label_position("right")
self.ax.set_title('press "E" to exit.')
# draw all empty
#MPP.show()
MPP.draw()
self.fig.canvas.draw()
self.fig.canvas.mpl_connect('key_press_event', self.Exit)
# cache the background
self.plot_background = self.fig.canvas.copy_from_bbox(self.ax.bbox)
# prepare lines
self.channel_handles = [self.ax.plot([0], [0], linestyle = '-')[0] for _ in range(len(self.channel_labels))]
def Show(self, window = 2):
# window: view time in seconds
self.ax.set_xlim(-window, 0)
self.recording_duration = window
self.PrepareAnalogAcquisition()
self.Record()
# while (TI.time() - t0) <= 2.5:
# TI.sleep(0.25)
# print (self.GetStatus(), NP.round(TI.time() - t0, 2), 's')
# print ('blub')
self.playing = True
t0 = TI.time()
dt = TI.time() - t0
while self.playing:
dt = TI.time() - t0
data = NP.array(self.buffer).reshape([-1,len(self.channel_labels)])
timer = dt % window
data = NP.roll(data, -int(timer * self.sampling_rate)-1, axis = 0)
time = NP.arange(data.shape[0]) / self.sampling_rate
time -= NP.max(time)
# restore background
self.fig.canvas.restore_region(self.plot_background)
# adjust and redraw data
for ch, line in enumerate(self.channel_handles):
line.set_data(time[:-1], data[:-1, ch]) # plot one less to avoid flickering
self.ax.draw_artist(line)
# fill in the axes rectangle
self.fig.canvas.blit(self.ax.bbox)
# TI.sleep(0.1)
MPP.pause(1e-3)
# if dt > 5.:
# self.playing = False
# stop after a time
self.analog_input.scan_stop()
def Record(self):
self.times['start'] = TI.time()
self.rate = self.analog_input.a_in_scan(**self.recording_settings)
def Close(self):
MPP.close(self.fig)
super(Oscilloscope, self).Close()
def __exit__(self, *args, **kwargs):
MPP.close(self.fig)
super(Oscilloscope, self).__exit__(*args, **kwargs)
################################################################################
### Trigger on MCC DAQ Digital I/O ###
################################################################################
class MultiTrigger(object):
# a connection to a digital input on a daq device
def __init__(self, digital_io, port, pin_nr = [0], rising = True):
self.digital_io = digital_io
self.port = port
self.pin_nr = pin_nr
self.rising = rising
def Read(self):
# read out the trigger bit
return NP.array([self.digital_io.d_bit_in(self.port, pin) for pin in self.pin_nr], dtype = bool)
def Await(self, scan_frq = 1e3):
# wait until the trigger bit encounters a rising/falling edge.
### triggering loop
# store previous status
previous = self.Read()
triggered_bit = None
try:
# first, wait for baseline condition (FALSE on rising, TRUE on falling edge)
if NP.any(previous == self.rising):
# wait for a single switch
while True:
TI.sleep(1/scan_frq)
current = self.Read()
if NP.any(NP.logical_xor(current, previous)):
previous = current
break
previous = current
# loop until bit changes
while True:
TI.sleep(1/scan_frq)
current = self.Read()
if NP.any(NP.logical_xor(current, previous)):
triggered_bit = self.pin_nr[NP.argmax(NP.logical_xor(current, previous))]
break
previous = current
except KeyboardInterrupt as ki:
raise ki
# trigger was successful
return triggered_bit
################################################################################
### LED Indicator on MCC DAQ ###
################################################################################
class LED(object):
# a connection to a digital input on a daq device
HIGH = 255
LOW = 0
def __init__(self, digital_io, port, pin_nr = 0):
self.digital_io = digital_io
self.port = port
self.pin_nr = pin_nr
self.Switch(False)
def Switch(self, value_bool):
self.digital_io.d_bit_out(self.port, self.pin_nr, self.HIGH if value_bool else self.LOW)
################################################################################
### MCC USB1608G Force Plate ###
################################################################################
class TriggeredForcePlateDAQ(AnalogInput):
# record on external trigger
if not uldaq_skip_import:
recording_mode = UL.ScanOption.EXTTRIGGER
#______________________________________________________________________
# Construction
#______________________________________________________________________
def __init__(self, fp_type, pins \
, *args, **kwargs):
self.fp_type = fp_type
# indicator and trigger pins
self.pins = pins
self.is_triggered = False
self.has_indicator = False
if (self.pins is not None) and (type(self.pins) is dict):
self.has_indicator = self.pins.get('led', None) is not None
# stores actual data
self.Empty()
## initialize DAQ
kwargs['channel_labels'] = forceplate_settings[self.fp_type]['channel_order']
super(TriggeredForcePlateDAQ, self).__init__(*args, **kwargs)
self.SetPins()
## connect LED
if self.has_indicator:
self.led = LED( digital_io = self.digital_io \
, port = self.port \
, pin_nr = self.pins['led']\
)
# store label
self.label = instrument_labels[self.daq_device.get_descriptor().unique_id]
def Empty(self):
# remove previous data
self.sync = []
self.data = None
def SetPins(self):
# set pin to input
if self.has_indicator:
# set pin to output
self.digital_io.d_config_bit(self.port, self.pins['led'], UL.DigitalDirection.OUTPUT)
#______________________________________________________________________
# Control
#______________________________________________________________________
def Indicate(self, value):
# flash an LED
if not self.has_indicator:
return
self.led.Switch(value)
def StdOut(self, *args, **kwargs):
print(*args, **kwargs)
#______________________________________________________________________
# I/O
#______________________________________________________________________
def Record(self):
self.StdOut('waiting for trigger... ' , end = '\r')
self.TriggeredRecording()
self.StdOut('done! ', ' '*20)
# # store data
# times, data = self.RetrieveOutput()
# self.store.append([times, data])
def TriggeredRecording(self):
# start recording in the background (will wait for trigger)
self.rate = self.analog_input.a_in_scan(**self.recording_settings)