I'm trying to plot data as quickly as possible with Python (PyQtGraph) received from a Teensy 3.2 which is sending analog data over a serial communication. The code can sufficiently plot a test waveform of higher frequencies (sine wave of about 5kHz), but it takes nearly 30 seconds for the plot to show a change in frequency. For example, if the test waveform is turned off, it continues to plot the sine wave for an additional half minute.
I've tried performing a "serial flush" to clear out the buffer on both the Python side and Teensy side, however, that seriously slows down the plot and the frequency response of my plot goes down to single hertz.
Python (Plotting) Side:
# Import libraries
from numpy import *
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import serial
import re
# Create object serial port
portName = "COM8"
baudrate = 115200
ser = serial.Serial(portName,baudrate)
### START QtApp #####
app = QtGui.QApplication([])
####################
win = pg.GraphicsWindow(title="Signal from serial port") # creates a window
p = win.addPlot(title="Realtime plot") # creates empty space for the plot in the window
curve = p.plot() # create an empty "plot" (a curve to plot)
windowWidth = 100 # width of the window displaying the curve - this is the time scale of the plot
Xm = linspace(0,0,windowWidth) # create array of zeros that is the size of the window width
ptr = -windowWidth # set first x position
# Realtime data plot. Each time this function is called, the data display is updated
def update():
global curve, ptr, Xm
Xm[:-1] = Xm[1:] # shift data in the temporal mean 1 sample left
if ser.isOpen(): # make sure there is data coming in
b1 = ser.read(1) # read the first byte of data
b2 = ser.read(1) # read the second byte of data
data = b1 + b2 # concatenate the two bytes
data_int = int.from_bytes(data, byteorder='big')
Xm[-1] = data_int # stack the data in the array
ptr += 1 # update x position for displaying the curve
curve.setData(Xm) # set the curve with this data
curve.setPos(ptr,0) # set x-y position in the graph to 0 and most recent data point - this creates the scrolling of the plot
QtGui.QApplication.processEvents() # process the plot
### MAIN PROGRAM #####
# this is a brutal infinite loop calling realtime data plot
while True: update()
### END QtApp ####
pg.QtGui.QApplication.exec_()
##################
Teensy 3.2 Side:
const int sensorPin = A9;
uint16_t sensorValue = 0;
byte b1;
byte b2;
int flag = 0;
IntervalTimer heartBeatTimer;
void setup()
{
analogReadRes(12);
Serial.begin(115200);
heartBeatTimer.begin(heartBeat, 140); // (1 / 115200 Baud) * 16 bits / integer = 139us per 16 bits sent. Interrupt at 140 us to synchronize with baud rate.
pinMode(13, OUTPUT);
}
void heartBeat()
{
flag = 1; // Interrupt routine every 140us
}
void loop()
{
if (flag == 1) {
sensorValue = analogRead(sensorPin); // read the analog pin as a 16 bit integer
b1 = (sensorValue >> 8) & 0xFF; // break up the reading to two bytes
b2 = sensorValue & 0xFF; // get the second byte
Serial.write(b1); // write the first byte (trying to speed things up by sending only strictly necessary data)
Serial.write(b2); // write the second byte
digitalWrite(13, HIGH); // just to make sure we're interrupting correctly
flag = 0; // wait for next interrupt
}
digitalWrite(13, LOW); // just to make sure we're interrupting correctly
}
Does anyone have any suggestions on how to speed things up?