我在这个论坛上看到了有关该主题的其他问题,但没有一个可以帮助我理解如何处理这个问题。在我看来,它们中的大多数也是关于相当复杂和冗长的代码。我相信我正在做一些相当简单的事情/想做一些相当简单的事情。我希望有人能帮帮忙!下面是广泛的解释,然后是我当前的代码。
注意:请不要删除此问题。我对以下内容进行了很多思考,并仔细浏览了相关线程,但无济于事。我也认为发布这个是有道理的,因为它部分与一个更通用的问题有关:如何在后台运行回调的同时实时绘图(参见最后的摘要),可以总结为我的总体目标。
设置和目标: National Instruments 采集模块(这很重要) NI cDAQ9178,通过接口nidaqmx-python
,由 NI 维护的包,文档在此处。那里输入了一些模拟信号,目标是以一定的采样率(大约 1000 Hz)连续采集它(直到我决定停止采集),同时实时绘制信号。绘图不需要经常刷新(10Hz 刷新率甚至可以)。我在 conda 虚拟环境中使用带有 Python 3.7 的 Windows 10,并在 PyCharm 中完成编辑。理想情况下,事情应该在 PyCharm 和任何终端中都有效。
情况: nidaqmx-python
提供允许注册回调(根据自己的意愿定义)的高级函数,每次一定数量的样本(在我的情况下为 100,但这并不严格)填充 PC 缓冲区时都会调用这些回调。这个想法是,下面定义的回调在该点读取缓冲区,并做一些事情(在我的情况下,为了简洁起见,我已经取出了一些低通滤波,一些存储到全局变量data
中,也许还有绘图 - 见以下)。
问题:我一直在胡闹,将实时数据绘制在回调中,但是使用 matplotlib 这是一场噩梦,因为回调使用主线程以外的线程,并且 matplotlib 不喜欢从任何地方调用在主线程之外。我已经用谷歌搜索了其他为实时绘图而优化的库(而且,我在想,希望线程安全)但这并不容易:我无法让 vispy 工作,甚至无法安装 pyqtgraph,只是为了给你一些例子。然后我在网上看到了几篇关于使用 matplotlib 管理相当不错的实时动画的帖子,尽管它的开发考虑了发布而不是这些应用程序;所以我想让我们试一试。
我的看法:由于我无法让 matplotlib 从回调内部完成工作,因此我做了以下操作(这是您在下面看到的代码):在回调之后和任务开始之后task.start()
(特定于nidaqmx-python
),我只是创建绘制全局变量的while
循环buffer
。我认为这是一个不错的技巧:看,buffer
每 0.1 秒左右(没关系)由回调更新(调用它),另一方面,while
循环buffer
一遍又一遍地绘制变量,每次在绘制之前擦除,有效地产生一个实时的情节。
注意:我完全知道绘图部分不如它可以制作的那么好(我可能应该使用 matplotlib 的 ax API 和subplots
,更不用说动画了),但我现在不在乎。我稍后会处理它并对其进行改进以提高效率。
我想要什么:这实际上做了我想要的......除了,为了阻止它,我在循环周围引入了try:
andexcept:
语句while
,正如你在下面的代码中看到的那样。自然,按下CTRL+C
确实会破坏循环......但它也会破坏整个运行脚本并给我留下以下错误:forrtl: error (200): program aborting due to control-C event
在 PyCharm 中,以及从终端运行时的以下精度:
Image PC Routine Line Source
libifcoremd.dll 00007FFECF413B58 Unknown Unknown Unknown
KERNELBASE.dll 00007FFF219F60A3 Unknown Unknown Unknown
KERNEL32.DLL 00007FFF23847BD4 Unknown Unknown Unknown
ntdll.dll 00007FFF240CCED1 Unknown Unknown Unknown
QObject::~QObject: Timers cannot be stopped from another thread
不便之处在于我别无选择,只能关闭 python shell(再次考虑 PyCharm),并且我无法访问我宝贵的变量data
,其中包含......好吧,我的数据。
Guess:显然,回调不喜欢以这种方式停止。该nidaqmx_python
任务应该用 停止task.stop()
。我尝试task.stop()
在 KeyboardInterrupt 之后立即放置except:
,但这无济于事,因为CTRL+C
将脚本停止在顶部 / 而不是中断 while 循环。我相信需要一些更复杂的方法来停止我的任务。这几天我一直在考虑这个问题,但想不出一种同时拥有这两种东西的方法:我可以停止一项任务,同时进行实时绘图。请注意,如果没有绘图,很容易在ENTER
按键时停止任务:只需在最后写
input('Press ENTER to stop task')
task.stop()
但当然,简单地执行上述操作并不允许我包含实时绘图部分。
摘要:我无法从连续读取数据的回调中调用 matplotlib,因此我while
在单独的块中编写了一个用于实时绘图的循环,但随后我发现没有办法停止该while
循环而不会出现上述错误(它抱怨我认为回调是从另一个线程停止的)。
我希望我很清楚,如果没有,请询问!
代码:我已经清理它以使其尽可能接近显示问题的 MWE,尽管我当然知道你们中的大多数人没有 NI daq 可以玩和连接以便能够运行这个。无论如何......这里是:
import matplotlib.pyplot as plt
import numpy as np
import nidaqmx
from nidaqmx import stream_readers
from nidaqmx import constants
sfreq = 1000
bufsize = 100
with nidaqmx.Task() as task:
# Here we set up the task ... nevermind
task.ai_channels.add_ai_voltage_chan("cDAQ2Mod1/ai1")
task.timing.cfg_samp_clk_timing(rate=sfreq, sample_mode=constants.AcquisitionType.CONTINUOUS,
samps_per_chan=bufsize)
# Here we define a stream to be read continuously
stream = stream_readers.AnalogMultiChannelReader(task.in_stream)
data = np.zeros((1, 0)) # initializing an empty numpy array for my total data
buffer = np.zeros((1, bufsize)) # defined so that global buffer can be written to by the callback
# This is my callback to read data continuously
def reading_task_callback(task_idx, event_type, num_samples, callback_data): # bufsize is passed to num_samples when this is called
global data
global buffer
buffer = np.zeros((1, num_samples))
# This is the reading part
stream.read_many_sample(buffer, num_samples, timeout=constants.WAIT_INFINITELY)
data = np.append(data, buffer, axis=1) # appends buffered data to variable data
return 0 # Absolutely needed for this callback to be well defined (see nidaqmx doc).
# Here is the heavy lifting I believe: the above callback is registered
task.register_every_n_samples_acquired_into_buffer_event(bufsize, reading_task_callback)
task.start() # The task is started (callback called periodically)
print('Acquiring sensor data. Press CTRL+C to stop the run.\n') # This should work ...
fig = plt.figure()
try:
while True:
# Poor's man plot updating
plt.clf()
plt.plot(buffer.T)
plt.show()
plt.pause(0.01) # 100 Hz refresh rate
except KeyboardInterrupt: # stop loop with CTRL+C ... or so I thought :-(
plt.close(fig)
pass
task.stop() # I believe I never get to this part after pressing CTRL+C ...
# Some prints at the end ... nevermind
print('Total number of acquired samples: ', len(data.T),'\n')
print('Sampling frequency: ', sfreq, 'Hz\n')
print('Buffer size: ', bufsize, '\n')
print('Acquisition duration: ', len(data.T)/sfreq, 's\n')
任何输入将不胜感激。提前谢谢各位!
编辑:在下面接受的答案之后,我重写了上面的代码并提出了以下内容,现在可以按预期工作(对不起,这次我没有清理它,有些行与当前问题无关):
# Stream read from a task that is set up to read continuously
import matplotlib.pyplot as plt
import numpy as np
import nidaqmx
from nidaqmx import stream_readers
from nidaqmx import constants
from scipy import signal
import threading
running = True
sfreq = 1000
bufsize = 100
bufsizeb = 100
global task
def askUser(): # it might be better to put this outside of task
global running
input("Press return to stop.")
running = False
def main():
global running
global data
global buffer
global data_filt
global buffer_filt
global b
global z
print('Acquiring sensor data...')
with nidaqmx.Task() as task: # maybe we can use target as above
thread = threading.Thread(target=askUser)
thread.start()
task.ai_channels.add_ai_voltage_chan("cDAQ2Mod1/ai1")
task.timing.cfg_samp_clk_timing(rate=sfreq, sample_mode=constants.AcquisitionType.CONTINUOUS,
samps_per_chan=bufsize)
# unclear samps_per_chan is needed here above or why it would be different than bufsize
stream = stream_readers.AnalogMultiChannelReader(task.in_stream)
data = np.zeros((1, 0)) # probably not the most elegant way of initializing an empty numpy array
buffer = np.zeros((1, bufsizeb)) # defined so that global buffer can be written in the callback
data_filt = np.zeros((1, 0)) # probably not the most elegant way of initializing an empty numpy array
buffer_filt = np.zeros((1, bufsizeb)) # defined so that global buffer can be written in the callback
b = signal.firwin(150, 0.004)
z = signal.lfilter_zi(b, 1)
def reading_task_callback(task_idx, event_type, num_samples, callback_data): # bufsizeb is passed to num_samples
global data
global buffer
global data_filt
global buffer_filt
global z
global b
if running:
# It may be wiser to read slightly more than num_samples here, to make sure one does not miss any sample,
# see: https://documentation.help/NI-DAQmx-Key-Concepts/contCAcqGen.html
buffer = np.zeros((1, num_samples))
stream.read_many_sample(buffer, num_samples, timeout=constants.WAIT_INFINITELY)
data = np.append(data, buffer, axis=1) # appends buffered data to variable data
# IIR Filtering, low-pass
buffer_filt = np.zeros((1, num_samples))
for i, x in enumerate(np.squeeze(buffer)): # squeeze required for x to be just a scalar (which lfilter likes)
buffer_filt[0,i], z = signal.lfilter(b, 1, [x], zi=z)
data_filt = np.append(data_filt, buffer_filt, axis=1) # appends buffered filtered data to variable data_filt
return 0 # Absolutely needed for this callback to be well defined (see nidaqmx doc).
task.register_every_n_samples_acquired_into_buffer_event(bufsizeb, reading_task_callback) # bufsizeb instead
task.start()
while running: # this is perfect: it "stops" the console just like sleep in a way that the task does not stop
plt.clf()
plt.plot(buffer.T)
plt.draw()
plt.pause(0.01) # 100 Hz refresh rate
# plt.close(fig) # maybe no need to close it for now
# task.join() # this is for threads I guess ... (seems useless to my case?)
# Some prints at the end ...
print('Total number of acquired samples:', len(data.T))
print('Sampling frequency:', sfreq, 'Hz')
print('Buffer size:', bufsize)
print('Acquisition duration:', len(data.T)/sfreq, 's')
if __name__ == '__main__':
main()
task.stop()
请注意,我毕竟不需要 a ,因为连续获取任务与此包一起工作的方式是读取任何task.start()
不是 asleep
或类似内容的代码行会使任务停止(至少这是我的理解)。