import Queue import os import sys import datetime as DT import collections import matplotlib.pyplot as plt from matplotlib import gridspec import numpy as np import multiprocessing as mp import time import datetime import matplotlib.dates as mdates import matplotlib.animation as animation from ABE_DeltaSigmaPi import DeltaSigma from ABE_helpers import ABEHelpers i2c_helper = ABEHelpers() bus = i2c_helper.get_smbus() adc = DeltaSigma(bus, 0x68, 0x69, 16) #Rename file to date base_dir = '/home/pi/Desktop/DATA' ts = time.time() filename_time = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d') filename_base = os.path.join(base_dir, filename_time) filename = '%s.txt' % filename_base # you will want to change read_delay to 5000 read_delay = int(5000) # in milliseconds write_delay = read_delay/1000.0 # in seconds window_size = 60 nlines = 8 ypadding = 0.5 datenums = collections.deque(maxlen=window_size) ys = [collections.deque(maxlen=window_size) for i in range(nlines)] def animate(i, queue): try: row = queue.get_nowait() except Queue.Empty: return datenums.append(mdates.date2num(row[0])) for i, y in enumerate(row[1:]): ys[i].append(y) for i, y in enumerate(ys): lines[i].set_data(datenums, y) ymin1 = min(min(y) for y in ys) ymin = ymin1 - ypadding ymax1 = max(max(y) for y in ys) ymax = ymax1 + ypadding xmin = min(datenums) xmax = max(datenums) if xmin < xmax: ax1.set_xlim(xmin, xmax) ax1.set_ylim(ymin, ymax) ax2.plot(0, 0) ax2.set_xlim(0, 1) ax2.set_ylim(0, 1) channel1 = row[-8] channel2 = row[-7] channel3 = row[-6] channel4 = row[-5] channel5 = row[-4] channel6 = row[-3] channel7 = row[-2] channel8 = row[-1] ax2.text(0.1,0.8,'CH1: %.02f \n CH2: %.02f \n CH3: %.02f \n CH4: %.02f \n CH5: %.02f \n CH6: %.02f \n CH7: %.02f \n CH8: %.02f \n' % (channel1,channel2,channel3,channel4,channel5,channel6,channel7,channel8) , ha='left', va='top', backgroundcolor='w') fig.canvas.draw() def write_data(filename, queue): while True: delay1 = DT.datetime.now() row = [] for i in range(nlines): # read from adc channels and print to screen channel = adc.read_voltage(i) row.append(channel) queue.put([delay1]+row) #print voltage variables to local file with open(filename, 'a') as DAQrecording: time1 = delay1.strftime('%Y-%m-%d') time2 = delay1.strftime('%H:%M:%S') row = [time1, time2] + row row = map(str, row) DAQrecording.write('{}\n'.format(', '.join(row))) #Delay until next 5 second interval delay2 = DT.datetime.now() difference = (delay2 - delay1).total_seconds() time.sleep(write_delay - difference) def main(): global fig, ax1, ax2, lines queue = mp.Queue() proc = mp.Process(target=write_data, args=(filename, queue)) # terminate proc when main process ends proc.daemon = True # spawn the writer in a separate process proc.start() fig, (ax1, ax2) = plt.subplots(1, 2, sharey=False) gs = gridspec.GridSpec(1,2, width_ratios=[3, 1] wspace=None) ax1 = plt.subplot(gs[0]) ax2 = plt.subplot(gs[1]) ax2.axes.xaxis.set_ticklabels([]) ax2.axes.yaxis.set_ticklabels([]) xfmt = mdates.DateFormatter('%H:%M:%S') ax1.xaxis.set_major_formatter(xfmt) # make matplotlib treat x-axis as times ax1.xaxis_date() fig.autofmt_xdate() fig.suptitle('Data Acquisition', fontsize=14, fontweight='bold') lines = [] for i in range(nlines): line, = ax1.plot([], []) lines.append(line) ani = animation.FuncAnimation(fig, animate, interval=read_delay, fargs=(queue,)) plt.show() if __name__ == '__main__': main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question