#!/usr/bin/python import matplotlib matplotlib.use('TkAgg') from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg from matplotlib.backend_bases import key_press_handler from matplotlib.figure import Figure from matplotlib.pyplot import ion import serial from time import sleep, time import Tkinter as _Tk root = _Tk.Tk() root.wm_title('Data-Logging') spinBox = _Tk.Spinbox(root, from_=0, to=240) sbLabel = _Tk.Label(root, text='How long would you like to test (in seconds): ') raw_data = [] _time = [] f = Figure(figsize=(5,4), dpi=100) a = f.add_subplot(111) a.plot(_time, raw_data) a.set_xlabel('Time') a.set_ylabel('Input') a.set_title('Light Reading') ion() canvas = FigureCanvasTkAgg(f, master=root) toolbar = NavigationToolbar2TkAgg(canvas, root) toolbar.update() canvas._tkcanvas.pack(side='bottom', fill='both', expand=1) canvas.get_tk_widget().pack(side='bottom', fill='both', expand=1) def beginLogging(): runTime = int(spinBox.get()) arduino = serial.Serial('/dev/ttyACM0', 9600) sleep(2.0) arduino.flush() arduino.flushInput() arduino.flushOutput() arduino.write('s') startTime = time() while(arduino.isOpen()): raw_data.append(int(arduino.readline())) _time.append(time() - startTime) arduino.flush() a.plot(_time, raw_data) a.set_ylim(0, max(raw_data)+75) canvas.draw() if((time() - startTime) >= runTime): arduino.close() #killButton = _Tk.Button(root, text='Quit', command = root.quit) #killButton.pack(side = 'right', fill = 'both', expand = 0) startButton = _Tk.Button(root, text='Start', command = beginLogging) startButton.pack(side = 'right', fill = 'both', expand = 0) spinBox.pack(side='right', fill='both', expand = 0) sbLabel.pack(side='right', fill='both', expand = 0) def on_key_event(event): print('you pressed %s' %event.key) key_press_handler(event, canvas, toolbar) canvas.mpl_connect('key_press_event', on_key_event) root.mainloop()
Sunday, July 21, 2013
Real-Time Data Acquisition with Tk GUI
Looks like I finally got the interface working the way I want. Here's the source:
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)
Python GUI
Made a GUI with python to control the on-board LED a few days ago. Seems to be really responsive. Also made the program in Visual C++ on Windows.
#!/usr/bin/python from Tkinter import * import serial arduino = serial.Serial('/dev/ttyACM0', 9600) def ledOn(): arduino.write('a') return def ledOff(): arduino.write('b') return app = Tk() app.title("Blink") app.geometry('125x75+200+200') buttonON = Button(app, text = 'LED ON', command = ledOn) buttonON.pack(padx = 10, pady = 5) buttonOFF = Button(app, text = 'LED OFF', command = ledOff) buttonOFF.pack(padx = 10, pady = 5) app.mainloop()
Subscribe to:
Posts (Atom)