############################################################################## #Python-Based GUI Testing Suite for the Arduino Control of the CEB Press. #Adapted from a GUI Based RC Helicopter Control Unit (2008) #Written July 23, 2011 by William Neal - william.j.a.neal@gmail.com ############################################################################## # #To operate the program.... #(a) Load the "CEB Testing" code into the Arduino #(b) Make sure that the CEB electronics module is correctly connected to the #sensors and solenoids #(c) Connect the Arduino to the Computer using a USB Cable #(d) Start this Python program (Linux) using the command line: python CEB_Testing.py #or whatever way you stary programs in other systems. #(e) Press the Connect button and verify that it has made the connection # using the output from the terminal (Linux). #(f) Press the other buttons as required to test the system # ############################################################################## #The following modules are required for the program to work: #You may need to install these. # from Tkinter import * import serial from PIL import Image, ImageTk ############################################################################### #This is the object used to connect to the Arduino #The writechar function sends a single character to the Arduino #The three lines of code (currently commented out) optionally receive characters #from the Arduino ############################################################################### class Arduino: def writechar(self, char): print "Sending ", char, " to the Arduino" self.connection1.write(char) #s = self.connection1.read(21) # read up to ten bytes (timeout) #line = self.connection1.readline() # read a '\n' terminated line #print s, line def connection(self): return serial.Serial('/dev/ttyACM0', baudrate=9600, timeout=0.1) ############################################################################### #This is the application object. #It contains the code for the buttons and the actions associated with the buttons # ############################################################################### class App: def __init__(self,parent): #The frame instance is stored in a local variable 'f'. #After creating the widget, we immediately call the #pack method to make the frame visible. f = Frame(parent, background = "light blue", width = 600) f.pack() #Here is the code for the buttons. #Note the parameters that determine the text, font, colors, height, width and position within the array of buttons #The command parameter links a button to a function that executes some code. #These functions appear after the button code in this file self.blink = Button(f, text="LED Blink", font = "Arial 24 bold", background = "green",foreground = "white", activebackground="yellow",height=4, width=10,command=self.blink).grid(row=2, column=3, columnspan=1) self.hall0 = Button(f, text="Sensor 1", font = "Arial 24 bold",background = "green",foreground = "white", activebackground="yellow",height=4, width=10,command=self.hallsensor).grid(row=1, column=1, columnspan=1) self.mosfet1 = Button(f, text="Mosfet P11", font = "Arial 24 bold", background = "blue",foreground = "white", activebackground="yellow",height=4, width=10,command=self.mosfet1).grid(row=1, column=2) self.mosfet2 = Button(f, text="Mosfet P10", font = "Arial 24 bold", background = "blue",foreground = "white", activebackground="yellow", height=4, width=10,command=self.mosfet2).grid(row=0, column=2) self.mosfet3 = Button(f, text="Mosfet P9", font = "Arial 24 bold", background = "blue",foreground = "white", activebackground="yellow",height=4, width=10,command=self.mosfet3).grid(row=0, column=1) self.mosfet4 = Button(f, text="Mosfet P6", font = "Arial 24 bold", background = "blue",foreground = "white", activebackground="yellow", height=4, width=10,command=self.mosfet4).grid(row=0, column=0) self.mosfet5 = Button(f, text="Mosfet P3", font = "Arial 24 bold", background = "blue",foreground = "white", activebackground="yellow",height=4, width=10,command=self.mosfet5).grid(row=1, column=0) self.openconn = Button(f,background = "green",activebackground="blue",foreground = "white",text="Open\n Connection", font = "Arial 24 bold", height=4, width=10,command=self.connect).grid(row=2, column=0) self.closeconn = Button(f, background = "red", activebackground="red",foreground = "white", text="Close\n Connection", font = "Arial 24 bold", height=4, width=10,command=self.disconnect).grid(row=2, column=1) self.exit = Button(f, text="Exit", font = "Arial 24 bold", activebackground="red",foreground = "white", background = "orange", height=4, width=10, command=f.destroy).grid(row=2,column=2) #This is the code that executes when you click the buttons #Set LED 13 to blink - for testing def blink(self): print "Testing the Blink Pattern on LED 13. Check the LED on the Arduino board or run an LED from pin 13 through a 100 Ohm resistor and into the ground (GND)." Arduino_Conn.writechar("A111") #Set up the Arduino for testing the Hall Sensors def hallsensor(self): print "The Hall Sensor should be set up with the Input pin in 12, the Output pin in A0 and the ground attached to the Arduino GND. An LED with a resistor should be in PIN 13. Test the sensor by bringing a magnet close by. The LED should light up. If it is not working, use the Serial Monitor to determine the levels being read by the sensor. Change the sensitivity level in the Arduino COde if necessary." to_write = "B100" Arduino_Conn.writechar(to_write) #Set up the Arduino for testing the MOSFETs def mosfet1(self): print "Testing Mosfet P11 - Look for the LED to light up and fade out." Arduino_Conn.writechar("C901") def mosfet2(self): print "Testing Mosfet P10 - Look for the LED to light up and fade out." Arduino_Conn.writechar("C902") def mosfet3(self): print "Testing Mosfet P9 - Look for the LED to light up and fade out." Arduino_Conn.writechar("C903") def mosfet4(self): print "Testing Mosfet P6 - Look for the LED to light up and fade out." Arduino_Conn.writechar("C904") def mosfet5(self): print "Testing Mosfet P3 - Look for the LED to light up and fade out." Arduino_Conn.writechar("C905") #(Not developed yet - merely a possibility) def autopress(self): #Custom program this subroutine to bring the Mosfets through a particular sequence. #Be sure not to run both sides of the hydraulic cylinders a the same time!!! print "Program an automatic routine for the CEB press here" #Connection to the Arduino def connect(self): print "The connection to the arduino is being established" Arduino_Conn.connection1 = Arduino_Conn.connection() print "The connection seems to have been established" #Disconnection from the Arduino def disconnect(self): Arduino_Conn.connection1.flushInput() Arduino_Conn.connection1.flushOutput() Arduino_Conn.connection1.close() print "Disconnected" Arduino_Conn = Arduino() root = Tk() root.title('Open Source Ecology - CEB Electronics Test Application') app = App(root) root.mainloop()