# # hello.load.45.py # # receive and display step response and sonar reading # bike.sensor.py serial_port # # Neil Gershenfeld # CBA MIT 10/29/10 # # Adapted by Caroline Jaffe # 11/23/2015 # # (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.5 # filter time constant filter1 = 0.0 # filtered value filter_sonar = 0 eps_sonar = 0.1 bike_cnt = 0 # bike count counter = 0 # counter of readings for debug purposes reading_cnt = 0 # number of bike count readings sonar_thresh = 100 # sonar sensor reading threshold for detecting bike cap_thresh = 500 # capacitive touch sensor reading threshold for detecting bike DEBUG_MODE = 0 # print debug statements if true READ_TIME = 10 # number of readings below threshold for detecting bike def idle(parent,canvas): global filter1, eps, filter_sonar, eps_sonar, bike_cnt, counter, reading_cnt, sonar_thresh, cap_thresh, DEBUG_MODE, READ_TIME # # idle routine # byte2 = 0 byte3 = 0 byte4 = 0 ser.flush() # # find framing # while 1: byte1 = byte2 byte2 = byte3 byte3 = byte4 byte4 = ord(ser.read()) if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)): break counter +=1 if counter == 5: counter = 0 # # read and plot # # Cap readings up_low1 = ord(ser.read()) up_high1 = ord(ser.read()) down_low1 = ord(ser.read()) down_high1 = ord(ser.read()) # # sonar readings low = ord(ser.read()) high = ord(ser.read()) # # cap values and canvas up_value1 = 256*up_high1 + up_low1 down_value1 = 256*down_high1 + down_low1 value1 = (up_value1 + (1023 - down_value1))/2.0 filter1 = (1-eps)*filter1 + eps*value1 x1 = int(.2*WINDOW + (.9-.2)*WINDOW*filter1/1023.0) if counter == 0 and DEBUG_MODE: print "cap: " print filter1 canvas.itemconfigure("title1",text="Capacitive touch sensor reading:") canvas.itemconfigure("text1",text="%.1f"%filter1) canvas.coords('rect11',.2*WINDOW,.05*WINDOW,x1,.2*WINDOW) canvas.coords('rect12',x1,.05*WINDOW,.9*WINDOW,.2*WINDOW) # # Sonar values and canvas value = (256*high + low) filt = (1-eps)*filter_sonar+eps_sonar*value us = filt/8.0 # 8 MHz counter cm = us/58.0 x = int(.2*WINDOW + (.9-.2)*WINDOW*cm/50) if counter == 0 and DEBUG_MODE: print "sonar: " print cm canvas.itemconfigure("title2",text="Sonar sensor reading:") canvas.itemconfigure("text",text="%.0f cm"%cm) canvas.coords('rect2',.2*WINDOW,.3*WINDOW,x,.45*WINDOW) canvas.coords('rect2',x,.3*WINDOW,.9*WINDOW,.45*WINDOW) # cm_int = int(cm) if cm_int == 14 and DEBUG_MODE: print "ERROR???" print cm print value print high print low # Bike count calculation and canvas if filter1 < cap_thresh and cm < sonar_thresh: reading_cnt += 1 if reading_cnt == READ_TIME: bike_cnt += 1 reading_cnt = 0 canvas.itemconfigure("bike_text",text="Bike Count: %.0f"%bike_cnt) canvas.update() parent.after_idle(idle,parent,canvas) # # check command line arguments # if (len(sys.argv) != 2): print "command line: bike.sensor.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('bike.sensor.py (q to exit)') root.bind('q','exit') canvas = Canvas(root, width=WINDOW, height=.75*WINDOW, background='white') # canvas.create_text(.3*WINDOW,.03*WINDOW,text="1",font=("Helvetica", 24),tags="title1",fill="#0000b0") canvas.create_text(.1*WINDOW,.125*WINDOW,text="1",font=("Helvetica", 24),tags="text1",fill="#0000b0") canvas.create_rectangle(.2*WINDOW,.05*WINDOW,.3*WINDOW,.2*WINDOW, tags='rect11', fill='#b00000') canvas.create_rectangle(.3*WINDOW,.05*WINDOW,.9*WINDOW,.2*WINDOW, tags='rect12', fill='#0000b0') canvas.create_text(.3*WINDOW,.28*WINDOW,text="1",font=("Helvetica", 24),tags="title2",fill="#0000b0") canvas.create_text(.1*WINDOW,.375*WINDOW,text="2",font=("Helvetica", 24),tags="text",fill="#0000b0") canvas.create_rectangle(.2*WINDOW,.3*WINDOW,.3*WINDOW,.45*WINDOW, tags='rect1', fill='#b00000') canvas.create_rectangle(.3*WINDOW,.3*WINDOW,.9*WINDOW,.45*WINDOW, tags='rect2', fill='#0000b0') canvas.create_text(.2*WINDOW,.5*WINDOW,font=("Helvetica", 24),tags="bike_text",fill="#0000b0") canvas.pack() # # start idle loop # root.after(100,idle,root,canvas) root.mainloop()