-
Notifications
You must be signed in to change notification settings - Fork 1
/
graspi.py
397 lines (304 loc) · 15 KB
/
graspi.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
#! /usr/bin/python3
# -*- coding: utf-8 -*-
"""
graspi.py - GeigerLog commands to handle the Geiger counter clicks as interrupts
on a Raspi4 to serve as CPM/CPS input
using ???
"""
###############################################################################
# This file is part of GeigerLog.
#
# GeigerLog 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.
#
# GeigerLog 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 GeigerLog. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
__author__ = "ullix"
__copyright__ = "Copyright 2016, 2017, 2018, 2019, 2020"
__credits__ = [""]
__license__ = "GPL3"
from gutils import *
def initRaspi():
"""Start the thread to monitor the audio pulses for clicks"""
#global sounddevThread
#testing
global cpm_counter
cpm_counter = np.full(60, 0)
fncname ="initRaspi: "
dprint(fncname + "Initialzing RaspiCounter")
setDebugIndent(1)
errmsg = "" # what errors can be here?
gglobs.RaspiDeviceName = "RaspiCounter"
if gglobs.RaspiPulseDir == "auto": gglobs.RaspiPulseDir = False # => negative
#~if gglobs.RaspiPulseDir == "auto": gglobs.RaspiPulseDir = True # => positive
if gglobs.RaspiPulseMax == "auto": gglobs.RaspiPulseMax = 32768 # +/- 16 bit
if gglobs.RaspiThreshold == "auto": gglobs.RaspiThreshold = 60 # % of max
if gglobs.RaspiVariables == "auto": gglobs.RaspiVariables = "CPM2nd, CPS2nd"
if gglobs.RaspiCalibration == "auto": gglobs.RaspiCalibration = 154 # CPM/(µSv/h), = 0.065 µSv/h/CPM
# the calibration of those vars must also be set, according to the set RaspiVariables
if "2nd" in gglobs.RaspiVariables: gglobs.calibration2nd = gglobs.RaspiCalibration # CPM2nd, CPS2nd
elif "3rd" in gglobs.RaspiVariables: gglobs.calibration3rd = gglobs.RaspiCalibration # CPM3rd, CPS3rd
else: gglobs.calibration1st = gglobs.RaspiCalibration # CPM, CPS, CPM1st, CPS1st
DevVars = gglobs.RaspiVariables.split(",")
for i in range(0, len(DevVars)): DevVars[i] = DevVars[i].strip()
gglobs.DevicesVars["Raspi"] = DevVars
#import RPi.GPIO as GPIO
#~GPIO.setmode(GPIO.BOARD)
gglobs.RaspiDevice = sd.default.device
gglobs.RaspiFormat = sd.default.dtype
gglobs.RaspiFormatText = sd.default.dtype
gglobs.RaspiChannels = sd.default.channels
gglobs.RaspiRate = sd.default.samplerate
#gglobs.RaspiChunk = 32 # in gglobs defined
gglobs.RaspiLatency = sd.default.latency
dprint(fncname + "DEVICE:{}, CHANNELS:{}, RATE:{}, CHUNK:{}, FORMAT:{}, Latency:{}"\
.format(
gglobs.RaspiDevice,
gglobs.RaspiChannels,
gglobs.RaspiRate,
gglobs.RaspiChunk,
gglobs.RaspiFormat,
gglobs.RaspiLatency
)
)
gglobs.RaspiConnection = True
setDebugIndent(0)
return errmsg
def terminateRaspi():
"""Stop the Raspi counting"""
fncname ="terminateRaspi: "
if not gglobs.RaspiConnection: return fncname + "No RaspiCounter connection"
gglobs.RaspiConnection = False
def sounddevThreadTarget(Dummy):
"""The thread to read the sounddev input"""
#~global cpm_counter, getThreadData
global cpm_counter
fncname = "sounddevThreadTarget: "
#getThreadData = True
gglobs.RaspiLastCps = 0
gglobs.RaspiLastCpm = 0
cps_count = 0
llimit = gglobs.RaspiThreshold / 100 * gglobs.RaspiPulseMax
ilimit = 10000 # the |pulse| must NOT exceed this value on first count in order to be countesd
# higher values give MORE counts!
cpm_counter = np.full(60, 0) # storing last 60 sec of CPS values
gglobs.RaspiMultiPulses = np.array([0]) # time courses for last ~40 of the pulses
gglobs.RaspiRecording = np.array([0])
nandata = np.full(10, gglobs.NAN) # creating a gap of 10 values
chunks40 = (gglobs.RaspiChunk + 10) * 40 # the length of 40 single pulses with gaps
CHUNKstream = sd.InputStream(blocksize=gglobs.RaspiChunk) # no callback so we get blocking read
CHUNKstream.start()
cstart = time.time() # to record the 1 sec collection period
while not gglobs.RaspiThreadStop:
#~wstart = time.time()
try:
record, overflowed = CHUNKstream.read(gglobs.RaspiChunk)
npdata = record.reshape(-1) # convert from '[ [1] [0] [3] ...[2] ]' to '[1, 0, 3, ..., 2]'
gglobs.RaspiRecording = np.concatenate((gglobs.RaspiRecording, npdata))[-gglobs.RaspiRate:]
except Exception as e:
info = fncname + "Exception reading stream "
exceptPrint(e, sys.exc_info(), info)
npdata = np.array([0])
#~print("npdata: ", type(npdata), npdata[:10])
#~wdt = (time.time() - wstart) * 1000
#~xstart = time.time()
if overflowed:
# Input overflow.
# In a stream opened with blocksize=0, indicates that data prior to
# the first sample of the input buffer was discarded due to an overflow,
# possibly because the stream callback is using too much CPU time.
#
# In a stream opened with a non-zero blocksize, it indicates that
# data prior to one or more samples in the input buffer was discarded.
# This can happen in full-duplex and input-only streams (including
# playrec() and rec()).
pass
dprint("'overflowed' HAPPENED: Stream calls: record len:{}, overflowed:{}".format(len(record), overflowed) )
# Determine if there is a count
# ignore records catching pulses when already down; they were probably
# already counted during last record
gotCount = False
if gglobs.RaspiPulseDir: # True -> positive pulse
if npdata[0] < +ilimit and npdata.max() > +llimit:
gotCount = True
else: # False -> negative pulse
if npdata[0] > -ilimit and npdata.min() < -llimit:
gotCount = True
if gotCount:
cps_count += 1
gglobs.RaspiMultiPulses = np.concatenate((gglobs.RaspiMultiPulses, nandata, npdata))[-chunks40:]
#~print("RaspiMultiPulses: ", len(gglobs.RaspiMultiPulses), gglobs.RaspiMultiPulses)
#print("got count: Stream calls: {:7.3f}ms, record len:{}, overflowed:{}".format(dt, len(record), overflowed) )
deltat = time.time() - cstart
if deltat >= 1:
gglobs.RaspiLastCps = cps_count / deltat * 1.05 # correct for calc time
cpm_counter = np.append(cpm_counter, gglobs.RaspiLastCps)[1:]
cps_count = 0
cstart = time.time()
#~xdt = (time.time() - xstart) * 1000
#print("wdt:{:0.3f}ms, xdt:{:0.3f}ms".format(wdt, xdt))
CHUNKstream.stop()
CHUNKstream.close()
def getRaspiInfo(extended = False):
"""Info on settings of the sounddev thread"""
if not gglobs.RaspiConnection: return "No connected device"
RaspiInfo = """Connected Device: '{}'
Configured Variables: {}
Geiger tube calib. factor: {:0.1f} CPM/(µSv/h) ({:0.4f} µSv/h/CPM)"""\
.format(
gglobs.RaspiDeviceName,
gglobs.RaspiVariables,
gglobs.RaspiCalibration, 1 / gglobs.RaspiCalibration
)
if extended == True:
RaspiInfo += """
Sampling Details:
- DEVICE: Input:'{}', Output:'{}'
- FORMAT: Input:'{}', Output:'{}'
- CHANNELS (1=Mono,2=Stereo): Input:'{}', Output:'{}'
- LATENCY [sec]: Input:'{}', Output:'{}'
- Host API Index {}
- RATE: {} (Samples per second)
- CHUNK: {} (Samples per read)
- Pulse Height Max {} (System reported max signal)
- Pulse Direction {} (negative or positive)
- Pulse Threshold {}% of Pulse Height Max to trigger count"""\
.format(
sd.default.device[0],
sd.default.device[1],
sd.default.dtype[0],
sd.default.dtype[1],
sd.default.channels[0],
sd.default.channels[1],
sd.default.latency[0],
sd.default.latency[1],
sd.default.hostapi,
sd.default.samplerate,
gglobs.RaspiChunk,
gglobs.RaspiPulseMax,
"POSITIVE" if gglobs.RaspiPulseDir else "NEGATIVE",
gglobs.RaspiThreshold,
)
RaspiInfo += "\nFull Device Query:\n"
RaspiInfo += str(sd.query_devices())
RaspiInfo += "\n"
return RaspiInfo
def toggleRaspiPulseDir():
"""toggle which pulse direction is sensed"""
gglobs.RaspiPulseDir = not gglobs.RaspiPulseDir
fprint(header("Switching Pulse Direction of RaspiCounter"))
if gglobs.RaspiPulseDir: direction = "POSITIVE"
else: direction = "NEGATIVE"
gglobs.RaspiMultiPulses = gglobs.RaspiMultiPulses[-10:]
fprint("New pulse direction setting: {}".format(direction))
def getRaspiValues(varlist):
"""Read all sounddev data; return empty dict when not available"""
global cpm_counter
#testing
gglobs.RaspiLastCps = int(np.random.poisson(10, size=1))
cpm_counter = np.append(cpm_counter, gglobs.RaspiLastCps)[1:]
###
fncname = "getRaspiValues: "
alldata = {}
if not gglobs.RaspiConnection: # RaspiCounter is NOT connected!
dprint(fncname + "RaspiCounter is not connected")
return alldata
if varlist == None:
return alldata
for vname in varlist:
if vname in ("CPM", "CPM1st", "CPM2nd", "CPM3rd"):
cpm = int(round(np.sum(cpm_counter), 0))
cpm = scaleVarValues(vname, cpm, gglobs.ValueScale[vname])
alldata.update( {vname: cpm})
elif vname in ("CPS", "CPS1st", "CPS2nd", "CPS3rd"):
cps = int(round(gglobs.RaspiLastCps, 0))
cps = scaleVarValues(vname, cps, gglobs.ValueScale[vname] )
alldata.update( {vname: cps})
vprint("{:22s} Variables:{} Data:{} ".format(fncname, varlist, alldata))
return alldata
##############################################################################
# following only relevant for use via 'main()'
def plotDataT(RaspiMultiPulses, inversrate):
"""This one used to plot either RaspiPlotSingle or RaspiMultiPulses of sound data"""
if len(RaspiMultiPulses) == 0: return
fncname = "plotDataT: "
#~print(fncname + "starting with RaspiMultiPulses, inversrate: ", RaspiMultiPulses, inversrate)
# Set "QT_STYLE_OVERRIDE" to avoid this message:
#~ QApplication: invalid style override 'gtk' passed, ignoring it.
#~ Available styles: Windows, Fusion
os.environ["QT_STYLE_OVERRIDE"] = ""
fig = plt.figure("Total", figsize=(14, 6))
vprint("plotDataT: open figs count: {}, current fig: #{}".format(len(plt.get_fignums()), plt.gcf().number))
if True: # accumulate the counts (don't delete the figure)
pass
else: # show a fresh record every time
fig.clf()
#print("plotDataT: RaspiMultiPulses:", len(RaspiMultiPulses), RaspiMultiPulses)
x = np.arange(len(gglobs.RaspiMultiPulses)) * inversrate * 1000
#~print("plotDataT: x :", len(x), x)
plt.xlabel("millisec")
plt.plot(x, gglobs.RaspiMultiPulses, 'k.', linestyle='solid', linewidth=0.3, markersize=0.5)
plt.ylim(-35000, 35000)
plt.pause(.5)
def main():
gglobs.debug = True
gglobs.verbose = True
fncname = "graspi.py: main: "
gglobs.proglogPath = "dummy"
duration = 10 # sec of recording
inversrate = 1 / gglobs.RaspiRate
gglobs.RaspiMultiPulses = np.zeros(256)
gglobs.devel = True # to allow sound
gglobs.RaspiActivation = True
gglobs.RaspiDeviceName = "None" # set by init
gglobs.RaspiPulseDir = False # False for negative; True for positive pulse
gglobs.RaspiThreshold = 60 # Percentage of pulse height to trigger a count
print("Available Devices")
print(sd.query_devices())
print()
RESET = sd.default.reset()
DEVICE = sd.default.device #~DEVICE = sd.default.device = None, None --> no device!
#~DEVICE = sd.default.device = 8,8 --> device 8 for output, device 8 for input
sd.default.dtype = ('int16', 'int16')
sd.default.channels = (1, 1)
FORMAT = sd.default.dtype[1]
CHANNELS = sd.default.channels[1] # Mono; for Stereo set: #CHANNELS = 2
#RATE = 96000
#RATE = 44100
#RATE = 22050
#RATE = 11025
RATE = sd.default.samplerate = 2**15 # = 32768
#CHUNK = 64
#CHUNK = 128
CHUNK = 256
#CHUNK = 512
#CHUNK = 1024
#CHUNK = 2048
#CHUNK = 4096
start = time.time()
while (time.time() - start) <= duration:
time.sleep(.15)
now = time.time()
#~mystream.start()
#~mystream.stop()
try:
getLongChunk(1.1)
#print("time: {:5.3f}, gglobs.RaspiLastCps: {:5.1f}, gglobs.RaspiLastCpm: {:5.1f}".format((now -start), gglobs.RaspiLastCps, gglobs.RaspiLastCpm ))
plotDataT(gglobs.RaspiMultiPulses, inversrate)
except Exception as e:
print(fncname + "Exception :", e)
#terminateRaspi()
plotDataT(gglobs.RaspiMultiPulses, inversrate)
print(fncname + "Habe fertig")
plt.show()
if __name__ == '__main__':
import matplotlib.pyplot as plt # plotting possible only when run as main
main()