Skip to content

Commit

Permalink
Tested the 1kHz sampling rate and seems to be OK
Browse files Browse the repository at this point in the history
  • Loading branch information
berndporr committed Aug 25, 2021
1 parent d9ffb8c commit 177dbcd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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)
```
Expand Down
4 changes: 2 additions & 2 deletions README_py.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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)

Expand Down
87 changes: 87 additions & 0 deletions examples/realtime_1kHz_scope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/python3
"""
Plots channels zero and one in two different windows. Requires pyqtgraph.
Copyright (c) 2018-2020, Bernd Porr <[email protected]>
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")

0 comments on commit 177dbcd

Please sign in to comment.