-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmpydev.py
439 lines (351 loc) · 15.3 KB
/
mpydev.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
# -*- coding: utf-8 -*-
#
# This file is part of PyGaze - the open-source toolbox for eye tracking
#
# PyGaze is a Python module for easily creating gaze contingent experiments
# or other software (as well as non-gaze contingent experiments/software)
# Copyright (C) 2012-2018 Edwin S. Dalmaijer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
import os
import copy
import time
from ctypes import windll, c_int, c_double, byref
from threading import Thread, Lock
import numpy
# Load BIOPAC's mpdev DLL.
try:
mpdev = windll.LoadLibrary('mpdev.dll')
except:
try:
mpdev = windll.LoadLibrary(os.path.join(os.path.dirname(os.path.abspath(__file__)),'mpdev.dll'))
except:
raise Exception("Error in mpydev: could not load mpdev.dll")
# Function to handle errors from the mpdev DLL functions.
def check_returncode(returncode):
"""
desc:
Checks a BioPac MP150 returncode, and returns it's meaning as a human
readable string.
arguments:
returncode:
desc: A code returned by one of the functions from the mpdev DLL
type: int
returns:
desc: A string describing the error
type: str
"""
if returncode == 1:
meaning = "MPSUCCESS"
else:
meaning = "UNKNOWN"
return meaning
# class definition
class BioPac:
"""
desc:
Class to communicate with BIOPAC devices such as the MP150, MP160, and
MP36R. This class works through mpdev.dll, which should be installed
separately.
"""
def __init__(self, devname, n_channels=3, samplerate=200, \
logfile='default', overwrite=False):
"""
desc:
Finds a BioPac device, and initializes a connection.
arguments:
devname:
desc: Name of the device that should be connected, e.g.
"MP150", "MP160", or "MP36R"
type: str
keywords:
n_channels:
desc: The number of channels that should be recorded from.
Default = 3
type: int
samplerate:
desc: The sampling rate in Hertz (default = 200)
type: int
logfile:
desc: Name of the logfile (optionally with path), which will
be used to create a textfile, e.g.
'default_BIOPAC_data.tsv' (default = 'default')
type:str
overwrite:
desc: Indicates whether the log file should be overwritten if
a file with the same name already exists. (default = False)
type: bool
"""
# Dict with the supported devices and their codes.
self._supported = { \
"MP150": 101, \
"MP160": 103, \
"MP36R": 103, \
}
# Check whether the passed device name is valid.
if devname.upper() not in self._supported.keys():
raise Exception("ERROR in mpydev: Unknown device name '%s'. Supported devices are: %s" \
% (devname, self._supported.keys()))
# Set the device name and code properties.
self._devname = devname.upper()
self._devcode = self._supported[self._devname]
# Set the sampling properties.
self._samplerate = float(samplerate)
self._sampletime = 1000.0 / self._samplerate
self._sampletimesec = self._sampletime / 1000.0
# Check the channels, and verify that there aren't over 16.
if n_channels > 0 and n_channels <= 16:
self._n_channels = int(n_channels)
else:
raise Exception("ERROR in mpydev: 1-16 channels can be recorded; you requested %d channels" \
% (int(n_channels)))
# Set the log file name.
self._logfilename = "%s_BIOPAC_data.tsv" % (logfile)
# Check if the logfile already exists.
if os.path.isfile(self._logfilename) and not overwrite:
# Find a file name that isn't used yet by incrementing a number.
i = 1
while os.path.isfile(self._logfilename):
i += 1
self._logfilename = "%s_%d_BIOPAC_data.tsv" % (i, logfile)
# Pre-create properties that are used by methods.
self._newestsample = numpy.zeros(n_channels, dtype=float)
self._buffer = []
self._buffch = 0
# Connect to the BIOPAC device. The first passed variable is the
# device code (101 for MP150, 103 for MP160 or MP36R), the second
# passed variable is for the communication method (11), and the third
# argument is for the way to connect to a device ('auto' is for
# automatically connecting to the first responding device).
try:
result = mpdev.connectMPDev(c_int(self._devcode), c_int(11), b'auto')
except:
result = "failed to call connectMPDev"
if check_returncode(result) != "MPSUCCESS":
raise Exception("Error in mpydev: failed to connect to the device: %s" \
% (result))
# Get the connection start time.
self._starting_time = time.time()
# Set the device's sampling rate.
try:
result = mpdev.setSampleRate(c_double(self._sampletime))
except:
result = "failed to call setSampleRate"
if check_returncode(result) != "MPSUCCESS":
raise Exception("Error in mpydev: failed to set samplerate: %s" \
% (result))
# Set Channels to acquire.
try:
# Create a total of 16 channels, with the requested amount set to
# on (1), and the others set to off (0).
channels = self._n_channels * [1]
channels.extend((16-self._n_channels) * [0])
# Convert the channel list into a c_int_Array.
channels = (c_int * len(channels))(*channels)
# Set the channels through mpdev.
result = mpdev.setAcqChannels(byref(channels))
except:
result = "failed to call setAcqChannels"
if check_returncode(result) != "MPSUCCESS":
raise Exception("Error in mpydev: failed to set channels to acquire: %s" \
% (result))
# Start data acquisition.
try:
result = mpdev.startAcquisition()
except:
result = "failed to call startAcquisition"
if check_returncode(result) != "MPSUCCESS":
raise Exception("Error in mpydev: failed to start acquisition: %s" \
% (result))
# Open a new log file.
self._logfile = open(self._logfilename, 'w')
# Write a header to the logfile.
header = ["timestamp"]
header.extend(["channel_%d" % i for i in range(self._n_channels)])
self._logfile.write("\t".join(header))
# Create logging lock to prevent simultaneous access to the lof file
# from different Threads.
self._loglock = Lock()
# Start the sample processing Thread. This will run int the background
# to collect and optionally log samples.
self._recording = False
self._recordtobuff = False
self._connected = True
self._spthread = Thread(target=self._sampleprocesser)
self._spthread.daemon = True
self._spthread.name = "sampleprocesser"
self._spthread.start()
def start_recording(self):
"""
desc:
Starts writing MP150 samples to the log file.
"""
# Signal to the sample processing thread that recording is active.
self._recording = True
def stop_recording(self):
"""
desc:
Stops writing MP150 samples to the log file.
"""
# Signal to the sample processing thread that recording stopped.
self._recording = False
# Consolidate logged data from the internal buffer to disk.
self._loglock.acquire(True)
# Internal buffer to RAM.
self._logfile.flush()
# # RAM file cache to disk.
os.fsync(self._logfile.fileno())
self._loglock.release()
def start_recording_to_buffer(self, channel=0):
"""
desc:
Starts recording to an internal buffer.
keywords:
channel:
desc: The channel from which needs to be recorded.
(default = 0)
type: int
"""
# Clear the internal buffer.
self._buffer = []
self._buffch = channel
# Signal to the sample processing thread that recording to the internal
# buffer is active.
self._recordtobuff = True
def stop_recording_to_buffer(self):
"""
desc:
Stops recording samples to an internal buffer.
"""
# Signal to the sample processing thread that recording stopped.
self._recordtobuff = False
def sample(self):
"""
desc:
Returns the most recent sample provided by the BIOPAC device.
returns:
desc: The latest BIOPAC output values for the requested channels,
as a list of floats.
type: list
"""
return self._newestsample
def get_buffer(self):
"""
desc:
Returns the internal sample buffer, which is filled up when
start_recording_to_buffer is called. This function is
safest to call only after stop_recording_to_buffer is called
returns:
desc: A NumPy array containing samples from since
start_recording_to_buffer was last called, until
get_buffer or stop_recording_to_buffer was called
type: numpy.array
"""
return numpy.array(self._buffer)
def log(self, msg):
"""
desc:
Writes a message to the log file.
arguments:
msg:
desc: The message that is to be written to the log file.
type: str
"""
# Get the call timestamp.
t = self.get_timestamp()
# Wait for the logging lock to be released, then lock it.
self._loglock.acquire(True)
# Log the message, including the recorded timestamp.
self._logfile.write("\nMSG\t%d\t%s" % (t, msg))
# Release the logging lock.
self._loglock.release()
def close(self):
"""
desc:
Closes the connection to the BIOPAC device.
"""
# Stop recording if it's still on.
if self._recording:
self.stop_recording()
# Close the log file.
self._logfile.close()
# Signal to the sample processing Thread that it can stop.
self._connected = False
# Close the connection with the BIOPAC device.
try:
result = mpdev.disconnectMPDev()
except:
result = "failed to call disconnectMPDev"
if check_returncode(result) != "MPSUCCESS":
raise Exception("Error in mpydev: failed to close the connection to the BIOPAC: %s" \
% (result))
def get_timestamp(self):
"""
desc:
Returns the time in milliseconds since the connection was opened
returns:
desc: Time (milliseconds) since connection was opened
type: int
"""
return int((time.time()-self._starting_time) * 1000)
def _sampleprocesser(self):
"""
desc:
Processes samples while self._recording is True (INTERNAL USE!)
"""
# Run until the connection is closed.
while self._connected:
# Attempt to get a new sample from the BIOPAC.
try:
# Start with a list of 0s.
data = self._n_channels * [0.0]
# Convert the list to a c_double_Array.
data = (c_double * len(data))(*data)
# Get the most recent sample from the BIOPAC.
result = mpdev.getMostRecentSample(byref(data))
# Get a timestamp.
t = self.get_timestamp()
# Convert the returned array into a tuple.
data = tuple(data)
# Throw a fit when data could not be obtained.
except:
result = "failed to call getMPBuffer"
if check_returncode(result) != "MPSUCCESS":
raise Exception("Error in mpydev: failed to obtain a sample from the MP150: %s" % result)
# Check if the new sample is in fact new.
if data != self._newestsample:
# Update the internal newest sample.
self._newestsample = copy.deepcopy(data)
# Write the new sample to file.
if self._recording:
# Collect the data into a single list.
line = [t]
line.extend(data)
# Wait for the logging lock to be released, then lock it.
self._loglock.acquire(True)
# Log the data as a string of tab-separated values.
self._logfile.write("\n" + "\t".join(map(str, line)))
# Release the logging lock.
self._loglock.release()
# Add the sample to the buffer.
if self._recordtobuff:
self._buffer.append(self._newestsample[self._buffch])
# Pause until the next sample is available.
# This is commented out, because it is currently unnecessary:
# getMostRecentSample blocks until a new sample is available, so
# we don't have to manually wait. If it wouldn't block, waiting
# for a bit would be advantageous, as it avoids wasting computing
# resources on continuously checking whether a new sample is
# available.
#time.sleep(self._sampletimesec)