27 from wiimoteConstants
import *
28 from wiiutils
import *
36 """Holds the state of a WIIRemote-plus. 38 The state is passed in and is as communicated 39 by one message from the WII+ device. We unpack 40 the information and place it into individual 41 dictionaries for callers to grab. 43 Public instance variables: 44 o time Time in fractional seconds since beginning of Epoch of when 45 state was measured (Float). 46 o ascTime Time when state was measured (Human-readable) 47 o rumble True/False if wiimote vibration is on/off 48 o angleRate A GyroReading instance containing gyro (a.k.a. angular rate) measurement 49 o acc A WIIReading instance containing accelerometer measurement corrected by 50 the calibration information that is stored in the Wiimote 51 o accRaw A WIIReading instance containing accelerometer measurement uncorrected 52 o buttons A dictionary for which buttons are being held down. That could be 53 multiple buttons. Keys are: 54 BTN_1, BTN_2, BTN_PLUS, BTN_MINUS, BTN_A, BTN_B, 55 BTN_UP, BTN_DOWN, BTN_LEFT, BTN_RIGHT, BTN_HOME 57 o IRSources Dictionary with on/off values for which IR lights are 58 being sensed. Keys are: 61 o motionPlusPresent True if a gyro Motion+ is plugged into the Wiimote. Else False 63 o nunchukPresent True if nunchuk is plugged in. Else False 64 o nunchukAccRaw A WIIReading instance with acceleromoter measurement from the nunchuk (raw values) 65 o nunchukAcc The same, but zeroed using factory calibration 66 o nunchukStickRaw A tuple with the two axes of the joystick on the nunchuk, raw readings 67 o nunchukStick A tuple with the two axes of the joystick on the nunchuk, zeroed to be [-1, 1] 68 o nunchukButtons A dictionary for which nunchuk buttons are down. Keys are BTN_C and BTN_Z 71 o setAccelerometerCalibration Bias setting for accelerometer. This triplet is used to 72 turn raw accelerometer values into calibrated values. 73 o setGyroCalibration Bias setting for gyro. This triplet is used to 74 turn raw gyro values into calibrated values. 77 _accCalibrationZero =
None 78 _gyroZeroReading =
None 79 _nunchukZeroReading =
None 80 _nunchukJoystickZero =
None 86 def __init__(self, state, theTime, theRumble, buttonStatus):
87 """Unpack the given state, normalizing if normalizers are passed in.""" 100 self.
buttons = {BTN_1:
False, BTN_2:
False, BTN_PLUS:
False,
101 BTN_MINUS:
False, BTN_A:
False, BTN_B:
False,
102 BTN_UP:
False, BTN_DOWN:
False, BTN_LEFT:
False,
103 BTN_RIGHT:
False, BTN_HOME:
False}
115 CLASSIC_BTN_L:
False, CLASSIC_BTN_R:
False,
116 CLASSIC_BTN_X:
False, CLASSIC_BTN_Y:
False,
117 CLASSIC_BTN_ZL:
False, CLASSIC_BTN_ZR:
False,
118 CLASSIC_BTN_PLUS:
False, CLASSIC_BTN_MINUS:
False,
119 CLASSIC_BTN_UP:
False, CLASSIC_BTN_DOWN:
False,
120 CLASSIC_BTN_LEFT:
False, CLASSIC_BTN_RIGHT:
False,
121 CLASSIC_BTN_HOME:
False}
126 if buttonStatus == 0:
127 for key
in self.buttons.keys():
131 self.
buttons[BTN_1] = (buttonStatus & BTN_1) > 0
132 self.
buttons[BTN_2] = (buttonStatus & BTN_2) > 0
133 self.
buttons[BTN_PLUS] = (buttonStatus & BTN_PLUS) > 0
134 self.
buttons[BTN_MINUS] = (buttonStatus & BTN_MINUS) > 0
135 self.
buttons[BTN_A] = (buttonStatus & BTN_A) > 0
136 self.
buttons[BTN_B] = (buttonStatus & BTN_B) > 0
137 self.
buttons[BTN_UP] = (buttonStatus & BTN_UP) > 0
138 self.
buttons[BTN_DOWN] = (buttonStatus & BTN_DOWN) > 0
139 self.
buttons[BTN_LEFT] = (buttonStatus & BTN_LEFT) > 0
140 self.
buttons[BTN_RIGHT] = (buttonStatus & BTN_RIGHT) > 0
141 self.
buttons[BTN_HOME] = (buttonStatus & BTN_HOME) > 0
143 for msgComp
in state:
149 if msgType == WII_MSG_TYPE_ACC:
151 accStatus = msgComp[1]
163 elif msgType == WII_MSG_TYPE_IR:
167 IRStatus = msgComp[1]
176 elif msgType == WII_MSG_TYPE_MOTIONPLUS:
181 gyroDict = msgComp[1]
183 if gyroDict
is not None:
196 elif msgType == WII_MSG_TYPE_NUNCHUK:
197 nunChuk = msgComp[1];
198 if nunChuk
is not None:
214 calibration = [127, 127]
219 joyx = -(joyx-calibration[0])/100.
220 joyy = (joyy-calibration[1])/100.
228 nunButtons = nunChuk[
'buttons']
232 elif msgType == WII_MSG_TYPE_CLASSIC:
233 clasSic = msgComp[1];
234 if clasSic
is not None:
238 clasButtons = clasSic[
'buttons']
239 self.
classicButtons[CLASSIC_BTN_A] = (clasButtons & CLASSIC_BTN_A) > 0
240 self.
classicButtons[CLASSIC_BTN_B] = (clasButtons & CLASSIC_BTN_B) > 0
241 self.
classicButtons[CLASSIC_BTN_DOWN] = (clasButtons & CLASSIC_BTN_DOWN) > 0
242 self.
classicButtons[CLASSIC_BTN_HOME] = (clasButtons & CLASSIC_BTN_HOME) > 0
243 self.
classicButtons[CLASSIC_BTN_L] = (clasButtons & CLASSIC_BTN_L) > 0
244 self.
classicButtons[CLASSIC_BTN_LEFT] = (clasButtons & CLASSIC_BTN_LEFT) > 0
245 self.
classicButtons[CLASSIC_BTN_MINUS] = (clasButtons & CLASSIC_BTN_MINUS) > 0
246 self.
classicButtons[CLASSIC_BTN_PLUS] = (clasButtons & CLASSIC_BTN_PLUS) > 0
247 self.
classicButtons[CLASSIC_BTN_R] = (clasButtons & CLASSIC_BTN_R) > 0
248 self.
classicButtons[CLASSIC_BTN_RIGHT] = (clasButtons & CLASSIC_BTN_RIGHT) > 0
249 self.
classicButtons[CLASSIC_BTN_UP] = (clasButtons & CLASSIC_BTN_UP) > 0
250 self.
classicButtons[CLASSIC_BTN_X] = (clasButtons & CLASSIC_BTN_X) > 0
251 self.
classicButtons[CLASSIC_BTN_Y] = (clasButtons & CLASSIC_BTN_Y) > 0
252 self.
classicButtons[CLASSIC_BTN_ZL] = (clasButtons & CLASSIC_BTN_ZL) > 0
253 self.
classicButtons[CLASSIC_BTN_ZR] = (clasButtons & CLASSIC_BTN_ZR) > 0
263 """Set the current accelerometer zeroing calibration.""" 273 """Return current accelerometer zeroing offset as two lists of x/y/z: the 274 zero-reading, and the one-reading.""" 275 return (cls._accCalibrationZero.tuple(), cls._accCalibrationOne.tuple())
283 """Set the x/y/z zeroing offsets for the gyro. Argument is a list""" 293 """Return current gyro zeroing offset as a list of x/y/z. """ 294 return cls._gyroZeroReading.tuple()
302 """Set the current nunchuk accelerometer zeroing calibration.""" 312 """Set the origin for the nunchuk joystick""" 321 """Return current nunchuk accelerometer zeroing offset as two lists of x/y/z: the 322 zero-reading, and the one-reading.""" 323 return (cls._nunchukZeroReading.tuple(), cls._nunchukOneReading.tuple())
333 res =
'Time: ' + self.
ascTime +
'\n' 352 butRes +=
', 4Way-up' 354 butRes +=
', 4Way-down' 356 butRes +=
', 4Way-left' 358 butRes +=
', 4Way-right' 365 res +=
'Buttons: none.\n' 367 res +=
'Buttons: ' + butRes.lstrip(
', ') +
'\n' 371 if self.
acc is not None:
372 res +=
'Accelerator: (' + \
373 `self.
acc[X]` +
',' + \
374 `self.
acc[Y]` +
',' + \
375 `self.
acc[Z]` +
')\n' 380 res +=
'Gyro (angular rate): (' + \
388 res +=
'Rumble: On.\n' 390 res +=
'Rumble: Off.\n' 398 irRes +=
'IR source 1' 401 irRes +=
'IR source 2' 404 irRes +=
'IR source 3' 407 irRes +=
'IR source 4' 410 res += irRes.lstrip(
', ') +
'\n' 412 res +=
'No IR sources detected.\n' 430 """Instances hold one 3-D reading. 433 [X], [Y], [Z] to obtain respective axis paramters. 434 tuple() to obtain x/y/z as a NumPy array. 435 +,-,/ to add or subtract readings from each other 436 as one vector operation (pairwise for each dimension). 445 """Create a (possibly) time stamped WII Reading. 447 Parameter xyz is an array of x,y,z coordinates of the 448 reading. WIIReading instances can be added, subtracted, and 449 divided into each other. The operations are pairwise over 450 x, y, and z. A numpy array of x,y,z is available by 451 calling tuple(). The time stamp is available via time(). 455 self.
_measurement = np.array([xyz[X], xyz[Y], xyz[Z]],dtype=np.float64)
458 if key
not in (X,Y,Z):
459 raise AttributeError(
"Attempt to index into a 3-D measurement array with index " + `key` +
".")
475 """Adding two readings returns a numpy tuple with readings added pairwise.""" 480 """Subtracting two readings returns a numpy tuple with components subtracted pairwise.""" 485 """Dividing two readings returns a numpy tuple with components divided pairwise.""" 490 """Return a numpy tuple that with X, Y, Z scaled by the given factor.""" 499 """Instances hold one gyroscope reading. 502 [PHI], [THETA], [PSI] to obtain respective axis paramters. 503 tuple() to obtain phi/theta/psi as a NumPy array. 504 +,-,/ to add or subtract readings from each other 505 as one vector operation (pairwise for each dimension). 513 """Create a (possibly) time stamped WII Reading. 515 Parameter phiThetaPsi is an array of phi,theta,psi coordinates of the 516 gyro reading. GyroReading instances can be added, subtracted, and 517 divided into each other. The operations are pairwise over 518 phi, theta, and psi. A numpy array of phi,theta,psi is available by 519 calling tuple(). The time stamp is available via time(). 523 self.
_measurement = np.array([phiThetaPsi[PHI], phiThetaPsi[THETA], phiThetaPsi[PSI]],dtype=np.float64)
527 if key
not in (PHI,THETA,PSI):
528 raise AttributeError(
"Attempt to index into a 3-D measurement array with index " + `key` +
".")
532 return '[PHI (roll)=' + repr(self.
_measurement[PHI]) + \
544 """Adding two gyro readings returns a new reading with components added pairwise.""" 548 """Subtracting two gyro readings returns a new reading 549 with components subtracted pairwise. 555 """Dividing two readings returns a numpy tuple with components divided pairwise.""" 560 """Return a numpy tuple that with X, Y, Z scaled by the given factor.""" def scale(self, scaleFactor)
def setAccelerometerCalibration(cls, zeroReading, oneReading)
def setNunchukAccelerometerCalibration(cls, zeroReading, oneReading)
def __getitem__(self, key)
def getGyroCalibration(cls)
def setNunchukJoystickCalibration(cls, readings)
def getNunchukAccelerometerCalibration(cls)
def __init__(self, xyz, theTime=None)
def setGyroCalibration(cls, zeroReading)
def __init__(self, state, theTime, theRumble, buttonStatus)
def scale(self, scaleFactor)
def __init__(self, phiThetaPsi, theTime=None)
def getAccelerometerCalibration(cls)
def __getitem__(self, key)