From: Heimburger, R. P <rph...@ba...> - 2004-09-30 12:44:24
|
I second that! Ralph Heimburger=20 Babcock & Wilcox Enterprise Systems 330-860-1858 -----Original Message----- From: pyt...@li... [mailto:pyt...@li...] On Behalf Of Daryl Fox Sent: Thursday, September 30, 2004 3:31 AM To: pyt...@li... Subject: [Pythoncard-users] Pythoncard to the rescue Hello all, I just wanted to drop a quick (well, maybe not so quick) note of thanks to Kevin and friends for making such a great tool. Yesterday a package of two Memsic 2125 Accelerometers arrived in the mail and I needed to check them out. I had intended to write a quick program on my basic stamp just to verify that they work. However, due to an accident involving a high voltage generator, the basic stamp and my LCD it looked like I was out of luck. I had access to a number of other micro controllers, but no other LCD that wasn't hard wired into another project. And hey, I wanted it to look nice too. :) I decided to give pythoncard a go since all that I really needed was a front end to display some data. I hacked together a little program for the Athena uC (small stamp clone) that just measures the pulse width on one of the 2125's outputs (two axis may come later) and dumps it to the serial port connected to my computer. --- test2125 --- ' Memsic 2125 Test dim T1 ' 2125 pulse width const Ser 5 ' Serial pin const Xout 0 ' Input from 2125 PULSEINSCALE =3D 4 input Xout output Ser loop: pulsein Xout, 3, T1 serout Ser, T1 goto loop Then I tossed together a quick front end using pythoncard: --- read2125.rsrc.py --- {'application':{'type':'Application', 'name':'Template', 'backgrounds': [ {'type':'Background', 'name':'bgTemplate', 'title':'Memsic 2125 Tester', 'position':(37, 81), 'size':(400, 207), 'menubar': {'type':'MenuBar', 'menus': [ {'type':'Menu', 'name':'menuFile', 'label':'&File', 'items': [ {'type':'MenuItem', 'name':'menuFileExit', 'label':'E&xit\tAlt+X', 'command':'exit', }, ] }, ] }, 'components': [ {'type':'StaticText', 'name':'labelPos2', 'position':(370, 75), 'text':'2g', }, {'type':'StaticText', 'name':'labelPos1', 'position':(280, 75), 'text':'1g', }, {'type':'StaticText', 'name':'label0', 'position':(190, 75), 'text':'0g', }, {'type':'StaticText', 'name':'labelNeg1', 'position':(95, 75), 'text':'-1g', }, {'type':'StaticText', 'name':'labelNeg2', 'position':(5, 75), 'text':'-2g', }, {'type':'TextField', 'name':'Force', 'position':(145, 105), 'alignment':'center', 'editable':False, }, {'type':'StaticText', 'name':'TitleText', 'position':(140, 15), 'text':'Force (1g =3D 9.81m/s^2)', }, {'type':'Gauge', 'name':'Gauge', 'position':(10, 40), 'size':(370, 28), 'layout':'horizontal', 'max':4000, 'value':2000, }, ] # end components } # end background ] # end backgrounds } } --- read2125.py --- #!/usr/bin/python """ __version__ =3D "$Revision: 1.5 $" __date__ =3D "$Date: 2004/04/30 16:26:12 $" """ from PythonCard import model, dialog from wxPython import wx import accel class MyBackground(model.Background): def on_initialize(self, event): self.Device =3D accel.Memsic() if not self.Device.Alive: dialog.alertDialog(self, 'ERROR: Cannot connect to device', 'Serial Error') self.close() self.Timer =3D wx.wxTimer(self.components.Force, -1) self.Timer.Start(100) # 100 ms updates def on_Force_timer(self, event): F =3D self.Device.GetForce() if F !=3D None: # Note: The gauge is scaled by 1000 because it needs # integers for value. self.components.Gauge.value =3D int((F + 2.0) * 1000) self.components.Force.text =3D '%0.3f' % F else: self.components.Force.text =3D 'N/A' def on_close(self, event): # Deamonize the thread to avoid this? self.Device.Stop() event.Skip() if __name__ =3D=3D '__main__': app =3D model.Application(MyBackground) app.MainLoop() And an interface class: --- accel.py --- import serial import Queue import threading class Memsic: def __init__(self): # Brain dead serial setup self.Ser =3D serial.Serial(0) self.Ser.baudrate =3D 9600 self.Ser.timeout =3D 1 self.Alive =3D False try: self.Ser.open() # COM1, 9600,N,8,1 except serial.SerialException: # Exception catching has not been tested return # Probably don't need to do this. May be easier just # to read as needed. Don't worry about overflow. Oh # well, threads are fun. self.RxQueue =3D Queue.Queue() self.Alive =3D True Thread =3D threading.Thread(target=3Dself.Reader) Thread.start() # Set this to the number of seconds per unit for the T1 # pulse width. For the BS2 it should be 2us (2e-6) but # may be different by default on other uC systems. self.Units =3D 32e-6 # 32us for Athena def Stop(self): self.Alive =3D False def Reader(self): while self.Alive: Data =3D self.Ser.read(1) # Abusing the empty() method if Data and self.RxQueue.empty(): Value =3D ord(Data) if Value < 0 or Value > 255: continue self.RxQueue.put(Value) def GetForce(self): try: Value =3D self.RxQueue.get(True, 0.250) except Queue.Empty: return None return self.F(Value) def F(self, Value): T1 =3D Value * self.Units # Assume that T2 =3D 10ms. For greater accuracy, we should # measure this every N cycles or so. This will only be # accurate at 25C (if properly calibrated). T2 =3D 10e-3 return ((T1/T2) - 0.5) * 8 The whole process took under an hour and looks (IMHO) great. Tip the board with the accelerometer and watch the pretty pictures. I'm sorry if this is too long a message or if I'm clogging up important developer talk, but I'm still stoked at how easy this was put together. Of course I still have a welt from the HV generator and I'm still out a microcontroller and LCD from the same, but this made my day. Kevin, I know you spend a lot of time answering simple (repetitive?) questions on this list ("How do I colour items in a list?") but thank you very much for all your hard work. -Daryl ------------------------------------------------------- This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more http://productguide.itmanagersjournal.com/guidepromo.tmpl _______________________________________________ Pythoncard-users mailing list Pyt...@li... https://lists.sourceforge.net/lists/listinfo/pythoncard-users |