# # hello.step.45.py # # receive and display step response # hello.step.45.py serial_port # # Neil Gershenfeld # CBA MIT 10/29/10 # # (c) Massachusetts Institute of Technology 2010 # Permission granted for experimental and personal use; # license for commercial sale available from MIT # from Tkinter import * import serial WINDOW = 600 # window size eps = 0.9 # filter time constant byte1 = 0 byte2 = 0 w1 = 0.33 w2 = 0.33 w3 = 0.33 x1 = 0.1 y1 = 0.8 x2 = 0.9 y2 = 0.8 x3 = 0.5 y3 = 0.1 def read32(): b1 = ord(ser.read()) b2 = ord(ser.read()) b3 = ord(ser.read()) b4 = ord(ser.read()) return 256*256*256*b1 + 256*256*b2 + 256*b3 + b4 def posHeart(): global x1, x2, x3, y1, y2, y3, w1, w2, w3 x = w1 * x1 + w2 * x2 + w3 * x3 y = w1 * y1 + w2 * y2 + w3 * y3 canvas2.coords("photo",WINDOW*(x*(x2-x1+.2)+x1-.1),WINDOW*(y*(y1-y3 +.3)+y3-.1)) def idle(parent,canvas): global eps, byte1, byte2, w1, w2, w3 # # idle routine # ser.flush() # # find framing # while 1: byte2 = byte1 byte1 = ord(ser.read()) if (byte1 == 4): break num = ord(ser.read()) canvas.itemconfigure("clstxt",text="%d"%num) t1 = read32() canvas.itemconfigure("11txt",text="%d"%t1) t2 = read32() canvas.itemconfigure("12txt",text="%d"%t2) t3 = read32() canvas.itemconfigure("13txt",text="%d"%t3) canvas.update() if(num > 0): canvas2.itemconfigure("1txt",text="1") if(num > 1): canvas2.itemconfigure("2txt",text="2") if(num > 2): canvas2.itemconfigure("3txt",text="3") # t2 = t2 * t2 * t2 # t3 = t3 * t3 * t3 # s = t2 + t3 if(t1 < 20000 or t2 < 20000 or t3 < 20000): if(t1 == t2 and t2 == t3): t1 = 0.5 t2 = 0.5 t3 = 0.5 elif(t1 >= t2 and t1 >= t3): t1 = 1 t2 = 0 t3 = 0 elif(t2 >= t1 and t2 >= t3): t1 = 0 t2 = 1 t3 = 0 elif(t3 >= t1 and t3 >= t2): t1 = 0 t2 = 0 t3 = 1 w1 = eps * w1 + (1.0 - eps) * t1 w2 = eps * w2 + (1.0 - eps) * t2 w3 = eps * w3 + (1.0 - eps) * t3 s = w1 + w2 + w3 w1 = w1 / s w2 = w2 / s w3 = w3 / s posHeart() parent.after_idle(idle,parent,canvas) # # check command line arguments # if (len(sys.argv) != 2): print "command line: hello.step.45.py serial_port" sys.exit() port = sys.argv[1] # # open serial port # ser = serial.Serial(port,9600) ser.setDTR() # # set up GUI # root = Tk() root.title('hello.step.45.py (q to exit)') root.bind('q','exit') photo = PhotoImage(file="heart.gif") canvas2 = Canvas(root, width=WINDOW, height=WINDOW, background='white') canvas2.create_image(.5*WINDOW,.5*WINDOW,image=photo,tags="photo"); canvas2.create_text(x1*WINDOW,y1*WINDOW,text="",font=("Helvetica", 30),tags="1txt",fill="#0000b0") canvas2.create_text(x2*WINDOW,y2*WINDOW,text="",font=("Helvetica", 30),tags="2txt",fill="#0000b0") canvas2.create_text(x3*WINDOW,y3*WINDOW,text="",font=("Helvetica", 30),tags="3txt",fill="#0000b0") canvas2.pack() # # start idle loop # root.after(100,idle,root,canvas2) root.mainloop()