From: Daryl F. <fly...@sb...> - 2004-09-30 07:32:10
|
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 = 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 = 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__ = "$Revision: 1.5 $" __date__ = "$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 = accel.Memsic() if not self.Device.Alive: dialog.alertDialog(self, 'ERROR: Cannot connect to device', 'Serial Error') self.close() self.Timer = wx.wxTimer(self.components.Force, -1) self.Timer.Start(100) # 100 ms updates def on_Force_timer(self, event): F = self.Device.GetForce() if F != None: # Note: The gauge is scaled by 1000 because it needs # integers for value. self.components.Gauge.value = int((F + 2.0) * 1000) self.components.Force.text = '%0.3f' % F else: self.components.Force.text = 'N/A' def on_close(self, event): # Deamonize the thread to avoid this? self.Device.Stop() event.Skip() if __name__ == '__main__': app = 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 = serial.Serial(0) self.Ser.baudrate = 9600 self.Ser.timeout = 1 self.Alive = 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 = Queue.Queue() self.Alive = True Thread = threading.Thread(target=self.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 = 32e-6 # 32us for Athena def Stop(self): self.Alive = False def Reader(self): while self.Alive: Data = self.Ser.read(1) # Abusing the empty() method if Data and self.RxQueue.empty(): Value = ord(Data) if Value < 0 or Value > 255: continue self.RxQueue.put(Value) def GetForce(self): try: Value = self.RxQueue.get(True, 0.250) except Queue.Empty: return None return self.F(Value) def F(self, Value): T1 = Value * self.Units # Assume that T2 = 10ms. For greater accuracy, we should # measure this every N cycles or so. This will only be # accurate at 25C (if properly calibrated). T2 = 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 |
From: Kevin A. <al...@se...> - 2004-09-30 15:43:03
|
On Sep 30, 2004, at 12:30 AM, Daryl Fox wrote: > 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. This is very cool! Thank you for proving that the framework and tool enable people to rapidly solve problems like that which is exactly what it is supposed to do. Very cool. > 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 <snip> When I saw "accelerometer" I instantly thought, wow, maybe PythonCard is being used to test components being stuck up on Mt. St. Helens. ;-) I live in Portland, so people are naturally interested in the little quakes and dome growth. I guess this is what you were testing? http://www.parallax.com/detail.asp?product_id=28017 You realize of course that if you write a weblog that you have to make a post and include the relevant pictures :) If you don't have one then maybe I'll bug you for some additional material and post it on mine sometime or start a "PythonCard Success Stories" page on the main site. I'll probably tweak the code to use some of the 0.8 conventions and classes, but it will still work the same. I would love to hear other stories like this about what people are using PythonCard for, how it has saved them time, etc. ka |
From: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - 2004-10-01 07:07:13
|
On Thu, 30 Sep 2004 00:30:53 -0700, Daryl Fox wrote: >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. This was a great message - it gives us Pythoncard (and Python) beginners some inspiration and hopefully encourages people to persevere when they see what real-world, practical projects can be achieved. I'm starting to appreciate how intuitive and fun Python and Pythoncard is (coming from a C background mainly) and this sort of post helps prove that I'm not being over-optimistic in thinking I can start moving a lot of my projects over. >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. Seconded (from someone likely to be asking the simple questions). Neil |