25 from __future__
import absolute_import
26 from .wiimoteConstants
import *
27 from .wiiutils
import *
33 Holds the state of a WIIRemote-plus.
35 The state is passed in and is as communicated
36 by one message from the WII+ device. We unpack
37 the information and place it into individual
38 dictionaries for callers to grab.
40 Public instance variables:
41 o time Time in fractional seconds since beginning of Epoch of when
42 state was measured (Float).
43 o ascTime Time when state was measured (Human-readable)
44 o rumble True/False if wiimote vibration is on/off
45 o angleRate A GyroReading instance containing gyro (a.k.a. angular rate) measurement
46 o acc A WIIReading instance containing accelerometer measurement corrected by
47 the calibration information that is stored in the Wiimote
48 o accRaw A WIIReading instance containing accelerometer measurement uncorrected
49 o buttons A dictionary for which buttons are being held down. That could be
50 multiple buttons. Keys are:
51 BTN_1, BTN_2, BTN_PLUS, BTN_MINUS, BTN_A, BTN_B,
52 BTN_UP, BTN_DOWN, BTN_LEFT, BTN_RIGHT, BTN_HOME
54 o IRSources Dictionary with on/off values for which IR lights are
55 being sensed. Keys are:
58 o motionPlusPresent True if a gyro Motion+ is plugged into the Wiimote. Else False
60 o nunchukPresent True if nunchuk is plugged in. Else False
61 o nunchukAccRaw A WIIReading instance with acceleromoter measurement from the nunchuk (raw values)
62 o nunchukAcc The same, but zeroed using factory calibration
63 o nunchukStickRaw A tuple with the two axes of the joystick on the nunchuk, raw readings
64 o nunchukStick A tuple with the two axes of the joystick on the nunchuk, zeroed to be [-1, 1]
65 o nunchukButtons A dictionary for which nunchuk buttons are down. Keys are BTN_C and BTN_Z
68 o setAccelerometerCalibration Bias setting for accelerometer. This triplet is used to
69 turn raw accelerometer values into calibrated values.
70 o setGyroCalibration Bias setting for gyro. This triplet is used to
71 turn raw gyro values into calibrated values.
74 _accCalibrationZero =
None
75 _gyroZeroReading =
None
76 _nunchukZeroReading =
None
77 _nunchukJoystickZero =
None
79 def __init__(self, state, theTime, theRumble, buttonStatus):
80 """Unpack the given state, normalizing if normalizers are passed in."""
92 self.
buttons = {BTN_1:
False, BTN_2:
False, BTN_PLUS:
False,
93 BTN_MINUS:
False, BTN_A:
False, BTN_B:
False,
94 BTN_UP:
False, BTN_DOWN:
False, BTN_LEFT:
False,
95 BTN_RIGHT:
False, BTN_HOME:
False}
107 CLASSIC_BTN_L:
False, CLASSIC_BTN_R:
False,
108 CLASSIC_BTN_X:
False, CLASSIC_BTN_Y:
False,
109 CLASSIC_BTN_ZL:
False, CLASSIC_BTN_ZR:
False,
110 CLASSIC_BTN_PLUS:
False, CLASSIC_BTN_MINUS:
False,
111 CLASSIC_BTN_UP:
False, CLASSIC_BTN_DOWN:
False,
112 CLASSIC_BTN_LEFT:
False, CLASSIC_BTN_RIGHT:
False,
113 CLASSIC_BTN_HOME:
False}
118 if buttonStatus == 0:
119 for key
in self.
buttons.keys():
123 self.
buttons[BTN_1] = (buttonStatus & BTN_1) > 0
124 self.
buttons[BTN_2] = (buttonStatus & BTN_2) > 0
125 self.
buttons[BTN_PLUS] = (buttonStatus & BTN_PLUS) > 0
126 self.
buttons[BTN_MINUS] = (buttonStatus & BTN_MINUS) > 0
127 self.
buttons[BTN_A] = (buttonStatus & BTN_A) > 0
128 self.
buttons[BTN_B] = (buttonStatus & BTN_B) > 0
129 self.
buttons[BTN_UP] = (buttonStatus & BTN_UP) > 0
130 self.
buttons[BTN_DOWN] = (buttonStatus & BTN_DOWN) > 0
131 self.
buttons[BTN_LEFT] = (buttonStatus & BTN_LEFT) > 0
132 self.
buttons[BTN_RIGHT] = (buttonStatus & BTN_RIGHT) > 0
133 self.
buttons[BTN_HOME] = (buttonStatus & BTN_HOME) > 0
135 for msgComp
in state:
141 if msgType == WII_MSG_TYPE_ACC:
143 accStatus = msgComp[1]
158 elif msgType == WII_MSG_TYPE_IR:
162 IRStatus = msgComp[1]
171 elif msgType == WII_MSG_TYPE_MOTIONPLUS:
176 gyroDict = msgComp[1]
178 if gyroDict
is not None:
191 elif msgType == WII_MSG_TYPE_NUNCHUK:
193 if nunChuk
is not None:
209 calibration = [127, 127]
214 joyx = -(joyx-calibration[0])/100.
215 joyy = (joyy-calibration[1])/100.
223 nunButtons = nunChuk[
'buttons']
227 elif msgType == WII_MSG_TYPE_CLASSIC:
229 if clasSic
is not None:
233 clasButtons = clasSic[
'buttons']
234 self.
classicButtons[CLASSIC_BTN_A] = (clasButtons & CLASSIC_BTN_A) > 0
235 self.
classicButtons[CLASSIC_BTN_B] = (clasButtons & CLASSIC_BTN_B) > 0
236 self.
classicButtons[CLASSIC_BTN_DOWN] = (clasButtons & CLASSIC_BTN_DOWN) > 0
237 self.
classicButtons[CLASSIC_BTN_HOME] = (clasButtons & CLASSIC_BTN_HOME) > 0
238 self.
classicButtons[CLASSIC_BTN_L] = (clasButtons & CLASSIC_BTN_L) > 0
239 self.
classicButtons[CLASSIC_BTN_LEFT] = (clasButtons & CLASSIC_BTN_LEFT) > 0
240 self.
classicButtons[CLASSIC_BTN_MINUS] = (clasButtons & CLASSIC_BTN_MINUS) > 0
241 self.
classicButtons[CLASSIC_BTN_PLUS] = (clasButtons & CLASSIC_BTN_PLUS) > 0
242 self.
classicButtons[CLASSIC_BTN_R] = (clasButtons & CLASSIC_BTN_R) > 0
243 self.
classicButtons[CLASSIC_BTN_RIGHT] = (clasButtons & CLASSIC_BTN_RIGHT) > 0
244 self.
classicButtons[CLASSIC_BTN_UP] = (clasButtons & CLASSIC_BTN_UP) > 0
245 self.
classicButtons[CLASSIC_BTN_X] = (clasButtons & CLASSIC_BTN_X) > 0
246 self.
classicButtons[CLASSIC_BTN_Y] = (clasButtons & CLASSIC_BTN_Y) > 0
247 self.
classicButtons[CLASSIC_BTN_ZL] = (clasButtons & CLASSIC_BTN_ZL) > 0
248 self.
classicButtons[CLASSIC_BTN_ZR] = (clasButtons & CLASSIC_BTN_ZR) > 0
253 """Set the current accelerometer zeroing calibration."""
259 """Return current accelerometer zeroing offset as two lists of x/y/z: the
260 zero-reading, and the one-reading."""
265 """Set the x/y/z zeroing offsets for the gyro. Argument is a list"""
271 """Return current gyro zeroing offset as a list of x/y/z. """
276 """Set the current nunchuk accelerometer zeroing calibration."""
282 """Set the origin for the nunchuk joystick"""
287 """Return current nunchuk accelerometer zeroing offset as two lists of x/y/z: the
288 zero-reading, and the one-reading."""
294 res =
'Time: ' + self.
ascTime +
'\n'
313 butRes +=
', 4Way-up'
315 butRes +=
', 4Way-down'
317 butRes +=
', 4Way-left'
319 butRes +=
', 4Way-right'
325 res +=
'Buttons: none.\n'
327 res +=
'Buttons: ' + butRes.lstrip(
', ') +
'\n'
330 if self.
acc is not None:
331 res +=
'Accelerator: (' + \
332 repr(self.
acc[X]) +
',' + \
333 repr(self.
acc[Y]) +
',' + \
334 repr(self.
acc[Z]) +
')\n'
338 res +=
'Gyro (angular rate): (' + \
345 res +=
'Rumble: On.\n'
347 res +=
'Rumble: Off.\n'
354 irRes +=
'IR source 1'
357 irRes +=
'IR source 2'
360 irRes +=
'IR source 3'
363 irRes +=
'IR source 4'
366 res += irRes.lstrip(
', ') +
'\n'
368 res +=
'No IR sources detected.\n'
377 """Instances hold one 3-D reading.
380 [X], [Y], [Z] to obtain respective axis paramters.
381 tuple() to obtain x/y/z as a NumPy array.
382 +,-,/ to add or subtract readings from each other
383 as one vector operation (pairwise for each dimension).
391 """Create a (possibly) time stamped WII Reading.
393 Parameter xyz is an array of x,y,z coordinates of the
394 reading. WIIReading instances can be added, subtracted, and
395 divided into each other. The operations are pairwise over
396 x, y, and z. A numpy array of x,y,z is available by
397 calling tuple(). The time stamp is available via time().
401 self.
_measurement = np.array([xyz[X], xyz[Y], xyz[Z]], dtype=np.float64)
404 if key
not in (X, Y, Z):
405 raise AttributeError(
"Attempt to index into a 3-D measurement array with index " + repr(key) +
".")
422 """Adding two readings returns a numpy tuple with readings added pairwise."""
427 """Subtracting two readings returns a numpy tuple with components subtracted pairwise."""
432 """Dividing two readings returns a numpy tuple with components divided pairwise."""
437 """Return a numpy tuple that with X, Y, Z scaled by the given factor."""
443 """Instances hold one gyroscope reading.
446 [PHI], [THETA], [PSI] to obtain respective axis paramters.
447 tuple() to obtain phi/theta/psi as a NumPy array.
448 +,-,/ to add or subtract readings from each other
449 as one vector operation (pairwise for each dimension).
456 """Create a (possibly) time stamped WII Reading.
458 Parameter phiThetaPsi is an array of phi,theta,psi coordinates of the
459 gyro reading. GyroReading instances can be added, subtracted, and
460 divided into each other. The operations are pairwise over
461 phi, theta, and psi. A numpy array of phi,theta,psi is available by
462 calling tuple(). The time stamp is available via time().
466 self.
_measurement = np.array([phiThetaPsi[PHI], phiThetaPsi[THETA], phiThetaPsi[PSI]], dtype=np.float64)
469 if key
not in (PHI, THETA, PSI):
470 raise AttributeError(
"Attempt to index into a 3-D measurement array with index " + repr(key) +
".")
474 return '[PHI (roll)=' + repr(self.
_measurement[PHI]) + \
488 """Adding two gyro readings returns a new reading with components added pairwise."""
492 """Subtracting two gyro readings returns a new reading
493 with components subtracted pairwise.
499 """Dividing two readings returns a numpy tuple with components divided pairwise."""
504 """Return a numpy tuple that with X, Y, Z scaled by the given factor."""