# accelerometer-Wiimotelib121-PY24.py print""" Rob Salgado (salgado@physics.syr.edu) and Tav Hawkins (taviare@physics.syr.edu) version July 15, 2008 http://physics.syr.edu/~salgado/software/vpython/ """ # uses Python 2.4 and WiimoteLib_1.2.1 and pythonnet from visual import * from random import * import CLR # pythonnet's CLR will call WiimoteLib (see below) print""" This [Windows-only] VPython program draws an arrow whose components are from the three-axis accelerometer (ADXL330) in the Wiimote. In order to read in the real-time data from the the Wiimote, we use the procedure presented by Nishio Hirokazu in http://nishiohirokazu.blogspot.com/2008/01/get-values-from-wii-remote-through.html, which uses Brian Peek's (Windows-only) WiimoteLib v1.2.1 [a library for using a Nintendo Wii Remote (Wiimote) from .NET.] (http://www.codeplex.com/WiimoteLib/Release/ProjectReleases.aspx?ReleaseId=10283) described in "Code4Fun - Managed Library for Nintendo's Wiimote (Brian Peek)" (http://blogs.msdn.com/coding4fun/archive/2007/03/14/1879033.aspx). To use .NET with Python, we used Brian Lloyd's Python for .NET (http://pythonnet.sourceforge.net/). Ideally, we would have liked to use the latest versions of the various software packages [which should be possible]. However, for now, the combination of versions that worked for us [with our program] is: - Python 2.4, VPython 3.2.9 - pythonnet-1.0-rc2-py2.4-clr1.1 - WiimoteLib v1.2.1 I'm sure there is a platform-independent solution in which Python communicates directly with Bluetooth. Of course, one now needs to know details of the protocol for talking with the Wiimote. Note: This was just thrown together as a "proof of concept". USAGE========= After pairing your Wiimote with your computer's Bluetooth radio, your Wiimote's three-axis accelerometer readings are represented by an arrow. (Think of the accelerometer as "a small sphere suspended by identical springs attached to the corners of a cube".) When your Wiimote is sitting normally upright on your desk, it will read a_z=1. (The small sphere is displaced downwards, and the net force on the sphere due to the springs is upward.) When your Wiimote is in freefall (and not rotating), it will read zero. To show this, gently toss (without rotation) your Wiimote to someone. When the accelerometer value is small enough, the background color of the scene changes to white (since the corresponding small arrow is hard to see). """ ##based on http://nishiohirokazu.blogspot.com/2008/01/get-values-from-wii-remote-through.html ## ##original from Nishio Hirokazu's article ## ##import clr ##clr.AddReferenceToFile("wiimotelib.dll") ## modified based on ## ## http://mail.python.org/pipermail/pythondotnet/2006-March/000450.html ##import CLR from CLR.System.Reflection import Assembly my_dll_name = Assembly.LoadWithPartialName( "WiimoteLib" ) from CLR.WiimoteLib import * wii = CLR.WiimoteLib.Wiimote() def get_value(sender, args): global a a = args wii.WiimoteChanged += CLR.WiimoteLib.WiimoteChangedEventHandler(get_value) wii.Connect() wii.SetReportType(wii.InputReport.IRAccel, True) ### WII=a.WiimoteState WIIset=wii WIIset.SetLEDs(0,0,0,0) #setLEDs off off off off (to save battery life) ##################################################### ##################################################### ##################################################### scene.x=0 scene.y=0 scene.width=600 scene.height=600 scene.autoscale=0 scene.title="VPython - Wiimote Accelerometers" scene.range=(2,2,2) myARROW=arrow(axis=(1,0,0) , fixedwidth = 1, shaftwidth=0.1) arrow(color=color.red,axis=(1,0,0), shaftwidth=0.01, fixedwidth=1) arrow(color=color.green,axis=(0,1,0), shaftwidth=0.01 , fixedwidth=1) arrow(color=color.blue,axis=(0,0,1), shaftwidth=0.01, fixedwidth=1) #Acceleration=label(xoffset=50, yoffset=60) scene.autoscale=0 scene.eye=vector(0-1,10,0+.2) scene.forward=vector(0,1,1) scene.up=vector(0,0,1) scene.forward=vector(0+1,1,0-.2) calibrate=vector(0,0,0) while True: #rate(100) myARROW.axis=vector( WII.AccelState.X,WII.AccelState.Y,WII.AccelState.Z)-calibrate if mag(calibrate)==0 and mag(myARROW.axis)<0.1: scene.background=color.white else: scene.background=color.black if scene.mouse.clicked: scene.mouse.getclick() if mag(calibrate)==0: calibrate=vector( WII.AccelState.X,WII.AccelState.Y,WII.AccelState.Z) else: calibrate=vector(0,0,0) #Acceleration.pos=myARROW.axis #Acceleration.text="%+6.4f %+6.4f %+6.4f" % (WII.AccelState.X, # WII.AccelState.Y, # WII.AccelState.Z) scene.title="%+6.4f %+6.4f %+6.4f" % (WII.AccelState.X, WII.AccelState.Y, WII.AccelState.Z)