# # hello3.step.py # receive and plot step response # Neil Gershenfeld CBA MIT # 10/29/05 # from Tkinter import * import serial WINDOW = 600 NSAMPLES = 254 MAX = 1040 eps = .9 saveflag = 0 index = 0 path = [] step = [] path_filt = [] step_filt = [] baseline = [] i = 0 samples = [] calbsamples = [] isCalibrated = False calbValue = 0.0 def idle(parent,canvas): global index, channel, baseline, path, path_filt, step, step_filt, saveflag global samples, calbsamples, calbValue, isCalibrated # # idle routine # #eps = float(sfilter.get()) lowbits = ord(ser.read()) hibits = ord(ser.read()) one = ord(ser.read()) two = ord(ser.read()) three = ord(ser.read()) four = ord(ser.read()) while 1: one = two two = three three = four four = ord(ser.read()) if ((one == 1) & (two == 2) & (three == 3) & (four == 4)): print "got framing @#" break print "one two three four:", one, ",", two, ",", three, ",", four print "low:", lowbits, " hibits:", hibits voltage = 256*hibits + lowbits voltageAD = voltage print "voltage A/D: " , voltage voltage = float(voltage/1023.0) voltage = voltage * 5.0 print "Voltage Real: ", voltage #if isCalibrated == False: # calbsamples.append(voltageAD) # canvas.itemconfigure("y0",text="Calibrating sample %d of 40."%len(calbsamples)) # if len(calbsamples) == 40: # isCalibrated = True # sum = 0.0 # for samp in calbsamples: # sum += samp # calbValue = sum/40.0 #if isCalibrated: # samples.append(voltageAD) # if len(samples) == 10: # sum = 0.0 # for samp in samples: # sum = sum + samp # avg = sum/10.0 # samples = [] # print "10 sample average is %d" %avg # if avg > (calbValue+1): # canvas.itemconfigure("y0",text="There is someting on the scale. avg: %d"%avg) # else: # canvas.itemconfigure("y0",text="Nothing on scale. avg: %d"%avg) canvas.itemconfigure("y0",text="Distance: %d (higher number is closer)"%voltageAD) parent.after_idle(idle,parent,canvas) # # open serial port # #ser = serial.Serial('/dev/ttyUSB0',9600) ser = serial.Serial('/dev/ttyS0',9600) #ser = serial.Serial('COM1',9600) ser.setDTR() # # find framing # print "finding framing ..." byte2 = 0 byte3 = 0 byte4 = 0 while 1: byte1 = byte2 byte2 = byte3 byte3 = byte4 byte4 = ord(ser.read()) if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)): print "start plotting" break # # start plotting # root = Tk() root.title('voltmeter') root.bind('q','exit') canvas = Canvas(root, width=WINDOW, height=.2*WINDOW, background='yellow') canvas.create_text(.5*WINDOW,.1*WINDOW,font=("Helvetica", 24),tags="y0",fill="#0000b0") canvas.pack() ioframe = Frame(root) ioframe.pack() root.after(100,idle,root,canvas) root.mainloop()