001: /*
002: * $RCSfile: LogitechTracker.java,v $
003: *
004: * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.1 $
041: * $Date: 2007/09/25 20:01:19 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.trackers;
046:
047: import java.lang.String;
048: import javax.vecmath.*;
049: import javax.media.j3d.*; // Note the following is necessary only for the sleep calls.
050: import java.lang.Thread;
051:
052: /**
053: * NOTE: this class is very much out of date. It is using deprecated
054: * and unimplemented 1.1 API constants (POLLED and STREAMING) that are
055: * now gone in 1.2. The offending lines have been commented out
056: * and marked with an "OBSOLETE:" comment.
057: *<p>
058: *
059: * The LogitechTracker Class defines the code to make a logitech input
060: * device work correclty
061: *
062: * @version 1.11, 99/09/15 13:47:17
063: * @author Henry Sowizral
064: */
065: public class LogitechTracker extends Tracker {
066:
067: // This device's slave filename
068: String slaveFilename;
069:
070: // Current Sensor Read (last one we generated)
071: SensorRead currentRead;
072:
073: // Next SensorRead (the one we're now generating)
074: SensorRead nextRead;
075:
076: // The current sensor
077: int currentSensor = 0;
078:
079: // Holds a sensor's Euler Angles
080: Vector3d deviceEulerAngles = new Vector3d();
081:
082: // Holds the sensor's current Position
083: Vector3d deviceTranslateValues = new Vector3d();
084:
085: // The number of sensors associated with this device.
086: static final int SensorCount = 8;
087:
088: // The number of buttons associated with this device.
089: static final int ButtonCount = 5;
090:
091: // The initial position and orientation
092: Transform3D initialPosOrient = new Transform3D();
093:
094: static float PositionOrientation[] = new float[6];
095: static int ButtonArray[] = new int[1];
096:
097: /**
098: * Construct a new LogitechTracker
099: * @param view the view object
100: * @paran deviceFilename the Logitech's devide name
101: * @param mode the mode of access one of POLLED or STREAMING
102: * @param sensorCount the number of sensors associated with this logitech
103: * @param buttonCount the number of buttons associated with each sensor
104: */
105: public LogitechTracker(View view, String deviceFilename, int mode,
106: int sensorCount, int buttonCount) {
107: this .super (view, deviceFilename, mode, sensorCount, buttonCount);
108: }
109:
110: /**
111: * Construct a new LogitechTracker with a hand tracker
112: * @param view the view object
113: * @param masterFilename the Logitech's master port device name
114: * @paran slaveFilename the Logitech's slave port device name
115: * @param mode the mode of access one of POLLED or STREAMING
116: * @param sensorCount the number of sensors associated with this logitech
117: * @param buttonCount the number of buttons associated with each sensor
118: */
119: public LogitechTracker(View view, String masterFilename,
120: String slaveFilename, int mode, int sensorCount,
121: int buttonCount) {
122: this .super (view, masterFilename, mode, sensorCount, buttonCount);
123: this .slaveFilename = slaveFilename;
124: }
125:
126: native int StartUpPolled(int fd);
127:
128: native int StartUpStreaming(int fd);
129:
130: native int PollInput(int fd, float PosOrient[], int Buttons[]);
131:
132: native int ProcessStream(int fd, float PosOrient[], int Buttons[]);
133:
134: native int OpenSerialDeviceRaw(String filename, int baudRate);
135:
136: /**
137: * Code to initialize the device
138: * @param deviceFilename
139: */
140: public boolean initialize() {
141: return this .initialize(19200);
142: }
143:
144: /**
145: * Initializes the Logitech deviceFilename by opening the
146: * device and sending the Logitech the initialization information.
147: * @param deviceFilename the Logitech's deviceFilename
148: * @param baudRate the speed we want the Logitech to run at
149: */
150: public boolean initialize(int baudRate) {
151: fileDescriptor = this .OpenSerialDeviceRaw(deviceFilename,
152: baudRate);
153: if (fileDescriptor < 0) {
154: System.out.println("Unable to initialize Logitech on "
155: + deviceFilename + " error code " + fileDescriptor);
156: return false;
157: }
158:
159: try {
160: Thread.sleep(2000L);
161: } catch (InterruptedException e) {
162: System.out.println(e);
163: }
164:
165: /* OBSOLETE:
166: if(processingMode == POLLED){
167: System.out.println("Initializing Polled");
168: return (0 == this.StartUpPolled(fileDescriptor));
169: } else if (processingMode == STREAMING) {
170: System.out.println("Initializing Streaming");
171: return (0 == this.StartUpStreaming(fileDescriptor));
172: }
173: */
174:
175: return false;
176: }
177:
178: /**
179: * Code to set the device's current position and orientation as the devices
180: * nominal position and orientation(establish its reference frame relative
181: * to the "Tracker base" reference frame).
182: */
183: public void setNominalPositionAndOrientation() {
184: Transform3D BaseTransform = new Transform3D();
185:
186: /** The following is not MT safe
187: pollAndProcessInput();
188: */
189: (sensors[currentSensor].getCurrentSensorRead())
190: .get(BaseTransform);
191:
192: initialPosOrient.invert(BaseTransform);
193: newTransform.setIdentity();
194: ButtonArray[0] = 0;
195: sensors[currentSensor].setNextSensorRead(System
196: .currentTimeMillis(), newTransform, ButtonArray);
197: }
198:
199: /**
200: * Code to poll and then process the input from a logitech.
201: */
202: public void pollAndProcessInput() {
203: long time;
204:
205: System.out.println("pollAndProcessInput: entered");
206: this
207: .PollInput(fileDescriptor, PositionOrientation,
208: ButtonArray);
209: System.out.println("PositionOrientation: "
210: + PositionOrientation[0] + " " + PositionOrientation[0]
211: + " " + PositionOrientation[1] + " "
212: + PositionOrientation[2] + " " + PositionOrientation[3]
213: + " " + PositionOrientation[4] + " "
214: + PositionOrientation[5]);
215: System.out.println("Buttons " + ButtonArray[0]);
216:
217: time = System.currentTimeMillis();
218:
219: tmpVector.set(PositionOrientation[0], PositionOrientation[1],
220: PositionOrientation[2]);
221: newTransform.setTranslation(tmpVector);
222:
223: setMatrixFromValues(PositionOrientation, newMatrix);
224: newTransform.setRotationScale(newMatrix);
225:
226: newTransform.mul(initialPosOrient, newTransform);
227: sensors[currentSensor].setNextSensorRead(time, newTransform,
228: ButtonArray);
229: }
230:
231: Matrix3d oldMatrix = new Matrix3d();
232: Matrix3d newMatrix = new Matrix3d();
233: Matrix3d tmpMatrix = new Matrix3d();
234:
235: Transform3D oldTransform = new Transform3D();
236: Vector3d oldLocation = new Vector3d();
237:
238: Transform3D newTransform = new Transform3D();
239: Vector3d newLocation = new Vector3d();
240:
241: Vector3d tmpVector = new Vector3d();
242:
243: /**
244: * Code to process the device's streaming input.
245: */
246: public void processStreamInput() {
247: long time;
248:
249: System.out.println("processStreamInput: entered");
250: this .ProcessStream(fileDescriptor, PositionOrientation,
251: ButtonArray);
252: System.out.println("PositionOrientation: "
253: + PositionOrientation[0] + " " + PositionOrientation[0]
254: + " " + PositionOrientation[1] + " "
255: + PositionOrientation[2] + " " + PositionOrientation[3]
256: + " " + PositionOrientation[4] + " "
257: + PositionOrientation[5]);
258: time = System.currentTimeMillis();
259:
260: tmpVector.set(PositionOrientation[0], PositionOrientation[1],
261: PositionOrientation[2]);
262: newTransform.setTranslation(tmpVector);
263:
264: setMatrixFromValues(PositionOrientation, newMatrix);
265: newTransform.setRotationScale(newMatrix);
266:
267: newTransform.mul(initialPosOrient, newTransform);
268: sensors[currentSensor].setNextSensorRead(time, newTransform,
269: ButtonArray);
270: }
271:
272: /**
273: * Code to construct a delta Matrix from Logitech inputs
274: */
275: void setMatrixFromValues(float posOrient[], Matrix3d Delta) {
276: double sina, sinb, sinc, cosa, cosb, cosc;
277:
278: sina = Math.sin(posOrient[3]);
279: sinb = Math.sin(posOrient[4]);
280: sinc = Math.sin(posOrient[5]);
281:
282: cosa = Math.cos(posOrient[3]);
283: cosb = Math.cos(posOrient[4]);
284: cosc = Math.cos(posOrient[5]);
285:
286: Delta.m00 = cosb * cosc;
287: Delta.m01 = cosb * sinc;
288: Delta.m02 = -sinb;
289:
290: Delta.m10 = -(cosa * sinc) + (sina * sinb * sinc);
291: Delta.m11 = (cosa * cosc) + (sina * sinb * sinc);
292: Delta.m12 = sina * cosb;
293:
294: Delta.m20 = (sina * sinc) + (cosa * sinb * cosc);
295: Delta.m21 = -(sina * cosc) + (cosa * sinb * sinc);
296: Delta.m22 = cosa * cosb;
297: }
298:
299: }
|