1

this is my first question post.

Im seeing a refresh rate of about 1 frame every 5 seconds in my live plot using the below code. I want to see something close to real-time updates. The serial rate of the data being plotted is sufficient to support at least 2 frames per second.

I've built this simple script using examples i've found on this site, and read several questions pertaining to reapplication of those examples.

I am not a frequent/experienced coder.

import serial #import pyserial library

ser=serial.Serial('/dev/tty.usbmodem14611', baudrate = 9600, timeout=1) #set serial COM port, baud, [timeout if you are using a while loop]
my_list = []    # define a list to hold each data point as it comes in
count_list = [] # define a list to hold the x axis data point (in our case we will grab the computer time stamp of each data point)

import matplotlib.pyplot as plt #import the matplot library
import matplotlib.animation as animation #define the matplotlib animation function object
import time #import the python time library

fig = plt.figure() #define a figure object
ax1 = fig.add_subplot(1,1,1) #add a subplot to the figure, you can add more subplots for additional data types if desired

xar = [] #define a list to hold the new x dimension of plot on each update
yar = [] #define a list to hold the new y dimension of plot on each update

def animate(i): #define an animation function 

    arduinoData = ser.readline().decode('ascii') #read current data on serial monitor, decode as ascii data type
    Data = arduinoData.split() #split each word/text block into a temporary list named Data
    if len(Data) > 2: #check if Data has at least 3 words/text blocks
        if Data[0]=='Yaw,': #check if position 0 in the temporary list Data is equal to "Yaw,"

            p=Data[3] #set variable p equal to position 3 of the list Data
            s = p.replace(',', '') #set variable s equal to variable p, after removing the comma
            yar.append(float(s))
            print(float(s))

            #millis = int(round(time.time() * 1000)) #grab current EPOCH time of loop from local pc  
            xtime = time.time()  
            xar.append(xtime)



    ax1.clear() #clear previous axis data
    ax1.plot(xar,yar) #plot updated axis data for xar and yar lists

ani = animation.FuncAnimation(fig, animate, interval=1) #call the animation function with an interval of ? millis
plt.show() #show plot in gui
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.