View Javadoc

1   /*
2    * @(#)$Id: WiiMoteTools.java
3    *
4    * Author       :   Arthur Vogels, arthur.vogels@gmail.com
5    *
6    * Purpose      :	Provides methods for use with the Wii Remote.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         	Reason
13   *
14   * Dec 02, 2008     arthurvogels    Initial Release
15   *
16   * -----------------------------------------------------------------------
17   *
18   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
19   *
20   * This software is the proprietary information of ETH Zurich.
21   * Use is subject to license terms.
22   * 
23   */
24  
25  package org.ximtec.igesture.util.additionswiimote;
26  
27  import org.ximtec.igesture.util.additions3d.Point3D;
28  import org.ximtec.igesture.util.additions3d.RecordedGesture3D;
29  import org.ximtec.igesture.util.additions3d.AccelerationSample;
30  import org.ximtec.igesture.util.additions3d.Accelerations;
31  
32  public class WiiMoteTools {
33  
34  
35  	/** 
36  	 * Converts the acceleration data from the WiiMote into timestamped 3D positions in a RecordedGesture3D object
37  	 * 
38  	 * @param acc the acceleration data coming from the WiiMote controller
39  	 * @return
40  	 */
41  	public static RecordedGesture3D accelerationsToTraces(Accelerations acc){
42  		//Variables
43  		RecordedGesture3D gesture = new RecordedGesture3D();
44  		double xVelocity = 0; 									//assume initial x velocity is zero
45  		double yVelocity = 0; 									//assume initial y velocity is zero
46  		double zVelocity = 0; 									//assume initial z velocity is zero
47  		double xPosition = 0; 									//assume initial x position is zero
48  		double yPosition = 0; 									//assume initial y position is zero
49  		double zPosition = 0; 									//assume initial z position is zero
50  		long tOld = 0; 												//Timestamp of old sample
51  		long tNew = 0;												//Timestamp of new sample
52  		double a; 												//Acceleration
53  		Point3D point = new Point3D();							//Point in 3d Space
54  		
55  		//Loop through acceleration samples
56  		for(int i = 0; i<acc.numberOfSamples(); i++){
57  			AccelerationSample sample = acc.getSample(i);							//Get current sample
58  			
59  			sample.setXAcceleration(sample.getXAcceleration() * -1);
60  			sample.setYAcceleration(sample.getYAcceleration() * -1);
61  			sample.setZAcceleration(sample.getZAcceleration() * -1);
62  			
63  			tNew = sample.getTimeStamp(); 								//Get timestamp from sample
64  			if (i==0) { //For the first sample we do not know tOld, therefore we make tOld = tNew
65  				tOld = tNew;
66  			}
67  			double t = (tNew - tOld) * 0.001;							//Time in seconds since last sample
68  			tOld = tNew;												//Old time of next sample = new time of this sample
69  			//System.out.println("Time between samples: " + t + " seconds.");
70  			
71  			a = sample.getXAcceleration() * 10; //1G = 10m/s2			//Get X acceleration for current sample
72  			xVelocity = xVelocity + (a * t); 							//Compute new X velocity
73  			xPosition = ((a * t * t) * 0.5) + (xVelocity * t) + xPosition; 	//Compute new X position
74  			//System.err.println("X Velocity: " + xVelocity);
75  			//System.err.println("X Position: " + xPosition);
76  			
77  			a = sample.getYAcceleration() * 10; //1G = 10m/s2			//Get Y acceleration for current sample
78  			yVelocity = yVelocity + (a * t); 							//Compute new Y velocity
79  			yPosition = ((a * t * t) / 2) + yVelocity * t + yPosition; 	//Compute new Y position
80  			
81  			a = (sample.getZAcceleration() + 1) * 10; //1G = 10m/s2		//Get Z acceleration for current sample
82  			zVelocity = zVelocity + (a * t); 							//Compute new Z velocity
83  			zPosition = ((a * t * t) / 2) + zVelocity * t + zPosition; 	//Compute new Z position
84  
85  			point.set(xPosition, yPosition, zPosition);					//Put x, y and z positions into point
86  			point.setTimeStamp(sample.getTimeStamp());					//Add timestamp to point
87  			
88  			gesture.add(point);											//Add new point to gesture
89  			point = new Point3D();
90  			sample = new AccelerationSample();
91  			//System.err.println("x: " + point.getX());// + ", y: " + point.getY() + ", z: " + point.getZ());
92  			//System.err.println("xAcc: " + sample.getXAcceleration() + ", yAcc: " + sample.getYAcceleration() + ", zAcc: " + sample.getZAcceleration());
93  		}
94  		//Include source accelerations
95  		gesture.setAccelerations(acc);
96  		//Return
97  		return gesture;
98  	}
99  	
100 }