Thursday, July 18, 2013

Python Real-Time Plotting

Finally got a reasonable script running some real-time plots in python. Just included the serial part for future expansion:
#!/usr/bin/python

import serial
from Tkinter import *
from pylab import *
import time as _time

endTime = float(raw_input('How long would you like to simulate: '))

arduino = serial.Serial('/dev/ttyACM0', 9600)
arduino.write('a')
 
startTime = _time.time()    # constant for start time 

def timeDelta():  # method to get current time difference 
    return _time.time() - startTime 

x=[]
plot(x,sin(x))
xlabel('Time')
ylabel('Reading')
title('Test')
ion()

while(arduino.isOpen()):
    x.append(timeDelta())
    plot(x,sin(x))
    ylim([-1,1])
    xlim([0, timeDelta()])
    draw()
    if timeDelta() >= endTime:
        arduino.close()
show(block =True)

No comments:

Post a Comment