Anton Shestakov <engored@ya.ru>, Sun, 13 Oct 2013 13:14:35 +0900
Basics.
chukmote/WiiChuck.h
Permissions: -rw-r--r--
* Nunchuck -- Use a Wii Nunchuck * Tim Hirzel http://www.growdown.com notes on Wii Nunchuck Behavior. This library provides an improved derivation of rotation angles from the nunchuck accelerometer data. The biggest different over existing libraries (that I know of ) is the full 360 degrees of Roll data from teh combination of the x and z axis accelerometer data using the math library atan2. It is accurate with 360 degrees of roll (rotation around axis coming out of the c button, the front of the wii), and about 180 degrees of pitch (rotation about the axis coming out of the side of the wii). (read more below) In terms of mapping the wii position to angles, its important to note that while the Nunchuck sense Pitch, and Roll, it does not sense Yaw, or the compass direction. This creates an important disparity where the nunchuck only works within one hemisphere. At a result, when the pitch values are less than about 10, and greater than about 170, the Roll data gets very unstable. essentially, the roll data flips over 180 degrees very quickly. To understand this property better, rotate the wii around the axis of the joystick. You see the sensor data stays constant (with noise). Because of this, it cant know the difference between arriving upside via 180 degree Roll, or 180 degree pitch. It just assumes its always * This file is an adaptation of the code by these authors: * Tod E. Kurt, http://todbot.com/blog/ * The Wii Nunchuck reading code is taken from Windmeadow Labs * http://www.windmeadow.com/node/42 * Conversion to Arduino 1.0 by Danjovic * http://hotbit.blogspot.com // these may need to be adjusted for each nunchuck for calibration #define RADIUS 210 // probably pretty universal #define DEFAULT_ZERO_JOY_X 129 #define DEFAULT_ZERO_JOY_Y 128 uint8_t status[6]; // array to store wiichuck output //int accelArray[3][AVERAGE_N]; // X,Y,Z uint8_t zeroJoyX; // these are about where mine are uint8_t zeroJoyY; // use calibrateJoy when the stick is at zero to correct // instead of the common 0x40 -> 0x00 initialization, we // use 0xF0 -> 0x55 followed by 0xFB -> 0x00. // this lets us use 3rd party nunchucks (like cheap $4 ebay ones) // while still letting us use official oness. // only side effect is that we no longer need to decode bytes in _nunchuk_decode_byte // see http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1264805255 Wire.beginTransmission(0x52); // device address Wire.beginTransmission(0x52); Wire.write((uint8_t)0x00); zeroJoyX = DEFAULT_ZERO_JOY_X; zeroJoyY = DEFAULT_ZERO_JOY_Y; Wire.requestFrom (0x52, 6); // request data from nunchuck while (Wire.available ()) { // receive byte as an integer status[cnt] = _nunchuk_decode_byte (Wire.read()); // //if (averageCounter >= AVERAGE_N) //accelArray[i][averageCounter] = ((int)status[i+2] << 2) + ((status[5] & (B00000011 << ((i+1)*2) ) >> ((i+1)*2))); angles[i] = (status[i+2] << 2) + ((status[5] & (B00000011 << ((i+1)*2) ) >> ((i+1)*2))); //accelYArray[averageCounter] = ((int)status[3] << 2) + ((status[5] & B00110000) >> 4); //accelZArray[averageCounter] = ((int)status[4] << 2) + ((status[5] & B11000000) >> 6); buttonZ = !( status[5] & B00000001); buttonC = !((status[5] & B00000010) >> 1); _send_zero(); // send the request for next bytes // UNCOMMENT FOR DEBUGGING // total = 0; // accelArray[xyz][averageCounter] * FAST_WEIGHT; return (float)angles[0] - ZEROX; // total = 0; // accelArray[xyz][averageCounter] * FAST_WEIGHT; return (float)angles[1] - ZEROY; // total = 0; // accelArray[xyz][averageCounter] * FAST_WEIGHT; return (float)angles[2] - ZEROZ; return (buttonZ && ! lastZ); return (buttonC && ! lastC); // for using the joystick like a directional button bool rightJoy(int thresh=60) { return (readJoyX() > thresh and lastJoyX <= thresh); // for using the joystick like a directional button bool leftJoy(int thresh=60) { return (readJoyX() < -thresh and lastJoyX >= -thresh); return (int) joyX - zeroJoyX; return (int)joyY - zeroJoyY; // R, the radius, generally hovers around 210 (at least it does with mine) // return sqrt(readAccelX() * readAccelX() +readAccelY() * readAccelY() + readAccelZ() * readAccelZ()); return (int)(atan2(readAccelX(),readAccelZ())/ M_PI * 180.0); // returns pitch in degrees return (int) (acos(readAccelY()/RADIUS)/ M_PI * 180.0); // optionally swap 'RADIUS' for 'R()' uint8_t _nunchuk_decode_byte (uint8_t x) //decode is only necessary with certain initializations Wire.beginTransmission (0x52); // transmit to device 0x52 Wire.write ((uint8_t)0x00); // sends one byte Wire.endTransmission (); // stop transmitting