# # hello.light.45.py # # receive and display light level # hello.light.45.py serial_port # # Neil Gershenfeld # CBA MIT 10/24/09 # # (c) Massachusetts Institute of Technology 2009 # Permission granted for experimental and personal use; # license for commercial sale available from MIT # #serial port seems like ttyUSB0 # or usb-FTDI_FT232R_USB_UART_A501JTMW-if00-port0 #or ../../ttyUSB0 from Tkinter import * import serial #Stuff to run Rscript and change the desktop background import os import subprocess #Creating an R vector or matrix, and filling its cells using Python code import rpy2.robjects as robjects from rpy2.robjects.packages import importr import rpy2.rinterface as rinterface import rpy2.robjects.lib.ggplot2 as ggplot2 import time #Python way to write csvs.... import csv #maybe try threading module #http://docs.python.org/2/library/threading.html#module-threading #CREATE THE FILE #Make a function that runs just the central r loop, not the intro bit. def r_refreshB(): # Ctrl-C to interrupt while True: idle() #Don't use sleep--it messes up the FTDI stuff base = importr('base') grdevices = importr('grDevices') r = robjects.r #SET UP DATAFRAME #Create a dataframe with two columns and dummy numbers #Note: range includes the bottom, but not the top number #Space doesn't need to be a perfect fit, rows get added as time goes past ttime=robjects.IntVector(range(1,101)) press=robjects.FloatVector(r.rep(robjects.NA_Real,100)) sensor=robjects.IntVector(r.rep(robjects.NA_Integer,100)) pressMax=robjects.FloatVector(r.rep(robjects.NA_Real,100)) i=robjects.IntVector((1,1)) counter=robjects.IntVector((1,1)) d = {'ttime': ttime, 'press': press, 'sensor': sensor, 'pressMax':pressMax} dataf = robjects.DataFrame(d) #r.X11() #Add something where we write out the database (graph?) on quitting #Variables for interpreting what's read in. eps = 0.5 # filter time constant filter = 0.0 # filtered value def idle(): global filter, eps, counter, i, dataf # # idle routine # byte2 = 0 byte3 = 0 byte4 = 0 ser.flush() while 1: # # find framing # byte1 = byte2 byte2 = byte3 byte3 = byte4 byte4 = ord(ser.read()) if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)): break pressV = ord(ser.read()) Psensor=ord(ser.read()) pressMax1=ord(ser.read()) #value=(1-eps)*filter+eps*pressV value=pressV #I need something here that says return the values, then run the dataset and graphing part when you have a value. #Main loop #Add to the dataset #Get a lot of data points and then run it...and then wait awhile before running it again. # if counter.rx(1)[0]<1000: # if 1: jj=counter.rx(1)[0]+1 counter=robjects.IntVector((jj,1)) ttime.rx[counter]=jj press.rx[counter]=value sensor.rx[counter]=Psensor pressMax.rx[counter]=pressMax1 d = {'ttime': ttime, 'press': press, 'sensor': sensor, 'pressmax':pressMax} dataf = robjects.DataFrame(d) robjects.DataFrame(d).to_csvfile("/home/perovich/Desktop/finalProject/danceStats/dancedata.csv", row_names=False) #Run the R code which will output an image file #Only do this once in awhile # if (counter.rx(1)[0])%500<1: if jj>200 and jj%50==0: retcode = subprocess.call(['/usr/bin/Rscript', '../danceStats/dancePredict.R']) os.system("gsettings set org.gnome.desktop.background picture-uri file:///home/perovich/Pictures/dance.png") #Make the variables visible for themodel #robjects.globalenv["press"]=press #robjects.globalenv["ttime"]=ttime #Right now plot every 10 data points. #This looks kind of spaztic--maybe average every 10 to get a smoother value? or every 5 or something else. #if jj%20==0: # gp=ggplot2.ggplot(dataf) # pp = gp + \ # ggplot2.aes_string(x='ttime', y='press', col='factor(sensor)') + \ # ggplot2.opts(title=sensor) + \ # ggplot2.xlim(0, 1500) + \ # ggplot2.ylim(0, 256) + \ # ggplot2.geom_point() #Make sure there's enough points before you add the model or else weird things happen. #pp.plot() #Here we could return it.... #proc = subprocess.Popen(['/usr/bin/Rscript', '../danceStats/dancePredict.R'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) #stdout, stderr = proc.communicate() #GET OUTPUT AND SET IT AS DESKTOP BACKGROUND #could really just do a popup window or something # # check command line arguments # if (len(sys.argv) != 2): print "command line: hello.pressuredoubleCheatVCC.44.py serial_port" sys.exit() port = sys.argv[1] # # open serial port # ser = serial.Serial(port,9600) ser.setDTR() # #Figure out how to add to the plot without redrawing the whole thing #(really the window flashing is the main problem here) # # start idle loop # r_refreshB() #idle() #r.mainloop()