From 177dbcde6a369977ba73264e9a93d8ad97a91f1a Mon Sep 17 00:00:00 2001 From: Bernd Porr Date: Wed, 25 Aug 2021 14:11:39 +0100 Subject: [PATCH] Tested the 1kHz sampling rate and seems to be OK --- README.md | 4 +- README_py.rst | 4 +- examples/realtime_1kHz_scope.py | 87 +++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100755 examples/realtime_1kHz_scope.py diff --git a/README.md b/README.md index fb553a6b..b9599552 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ PyFirmata2 turns your Arduino into a data acquisition card controlled by Python. -Up to 100Hz precise sampling at the analogue ports for digital filtering. +Up to 1kHz precise sampling at the analogue ports for digital filtering. Just upload the default firmata sketch into your Arduino and you are all set. @@ -89,7 +89,7 @@ check for the latest addition: `ls -l -t /dev/*`. ### Starting sampling at a given sampling interval In order to sample analogue data you need to specify a -sampling interval in ms. The smallest interval is 10ms: +sampling interval in ms. The smallest interval is 1ms: ``` board.samplingOn(samplinginterval in ms) ``` diff --git a/README_py.rst b/README_py.rst index 0af18d15..53dc6227 100644 --- a/README_py.rst +++ b/README_py.rst @@ -4,7 +4,7 @@ pyFirmata2 PyFirmata2 turns your Arduino into a data acquisition card controlled by Python. -Up to 100Hz precise sampling at the analogue ports for digital filtering. +Up to 1kHz precise sampling at the analogue ports for digital filtering. Just upload the default firmata sketch into your Arduino and you are all set. @@ -90,7 +90,7 @@ Starting sampling at a given sampling interval ---------------------------------------------- In order to sample analogue data you need to specify a -sampling interval in ms. The smallest interval is 10ms:: +sampling interval in ms. The smallest interval is 1ms:: board.samplingOn(samplinginterval in ms) diff --git a/examples/realtime_1kHz_scope.py b/examples/realtime_1kHz_scope.py new file mode 100755 index 00000000..0645cab7 --- /dev/null +++ b/examples/realtime_1kHz_scope.py @@ -0,0 +1,87 @@ +#!/usr/bin/python3 +""" +Plots channels zero and one in two different windows. Requires pyqtgraph. + +Copyright (c) 2018-2020, Bernd Porr +see LICENSE file. + +""" + +import sys + +import pyqtgraph as pg +from pyqtgraph.Qt import QtCore, QtGui + +import numpy as np + +from pyfirmata2 import Arduino +PORT = Arduino.AUTODETECT + +# create a global QT application object +app = QtGui.QApplication(sys.argv) + +# signals to all threads in endless loops that we'd like to run these +running = True + +class QtPanningPlot: + + def __init__(self,title): + self.win = pg.GraphicsLayoutWidget() + self.win.setWindowTitle(title) + self.plt = self.win.addPlot() + self.plt.setYRange(-1,1) + self.plt.setXRange(0,500) + self.curve = self.plt.plot() + self.data = [] + # any additional initalisation code goes here (filters etc) + self.timer = QtCore.QTimer() + self.timer.timeout.connect(self.update) + self.timer.start(100) + self.layout = QtGui.QGridLayout() + self.win.setLayout(self.layout) + self.win.show() + + def update(self): + self.data=self.data[-500:] + if self.data: + self.curve.setData(np.hstack(self.data)) + + def addData(self,d): + self.data.append(d) + +# Let's create two instances of plot windows +qtPanningPlot1 = QtPanningPlot("Arduino 1st channel") + +# sampling rate: 100Hz +samplingRate = 1000 + +# called for every new sample at channel 0 which has arrived from the Arduino +# "data" contains the new sample +def callBack(data): + # filter your channel 0 samples here: + # data = self.filter_of_channel0.dofilter(data) + # send the sample to the plotwindow + qtPanningPlot1.addData(data) + +# Get the Ardunio board. +board = Arduino(PORT) + +# Set the sampling rate in the Arduino +board.samplingOn(1000 / samplingRate) + +# Register the callback which adds the data to the animated plot +# The function "callback" (see above) is called when data has +# arrived on channel 0. +board.analog[0].register_callback(callBack) + +# Enable the callback +board.analog[0].enable_reporting() +board.analog[1].enable_reporting() + +# showing all the windows +app.exec_() + +# needs to be called to close the serial port +board.exit() + +print("Finished")