First draft of Wii Robot Racing, a simple example of using Wii Remote connected via bluetooth to control an m3pi robot, plus RFID \"powerups\" to modify behaviour

Dependencies:   mbed m3pi ID12RFIDIRQ

Committer:
simon
Date:
Mon Apr 11 23:03:22 2011 +0000
Revision:
0:e2ef12467862

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:e2ef12467862 1 /* Copyright (c) 2011, mbed
simon 0:e2ef12467862 2 *
simon 0:e2ef12467862 3 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 0:e2ef12467862 4 * of this software and associated documentation files (the "Software"), to deal
simon 0:e2ef12467862 5 * in the Software without restriction, including without limitation the rights
simon 0:e2ef12467862 6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 0:e2ef12467862 7 * copies of the Software, and to permit persons to whom the Software is
simon 0:e2ef12467862 8 * furnished to do so, subject to the following conditions:
simon 0:e2ef12467862 9 *
simon 0:e2ef12467862 10 * The above copyright notice and this permission notice shall be included in
simon 0:e2ef12467862 11 * all copies or substantial portions of the Software.
simon 0:e2ef12467862 12 *
simon 0:e2ef12467862 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 0:e2ef12467862 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 0:e2ef12467862 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 0:e2ef12467862 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 0:e2ef12467862 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 0:e2ef12467862 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 0:e2ef12467862 19 * THE SOFTWARE.
simon 0:e2ef12467862 20 */
simon 0:e2ef12467862 21
simon 0:e2ef12467862 22 // Simple decode class for Wii Remote data
simon 0:e2ef12467862 23
simon 0:e2ef12467862 24 #include "Wiimote.h"
simon 0:e2ef12467862 25
simon 0:e2ef12467862 26 // Wii Report Formats:
simon 0:e2ef12467862 27 // * http://wiibrew.org/wiki/Wiimote#0x37:_Core_Buttons_and_Accelerometer_with_10_IR_bytes_and_6_Extension_Bytes
simon 0:e2ef12467862 28 // * http://wiki.wiimoteproject.com/Reports#0x37_BTN_.2B_XLR_.2B_IR_.2B_6_EXT
simon 0:e2ef12467862 29
simon 0:e2ef12467862 30 // Input Report 0x37:
simon 0:e2ef12467862 31 //
simon 0:e2ef12467862 32 // 0 [ ? | X(1:0) | PLUS | UP | DOWN | RIGHT | LEFT ]
simon 0:e2ef12467862 33 // 1 [ HOME | Z(1) | Y(1) | MINUS | A | B | ONE | TWO ]
simon 0:e2ef12467862 34 // 2 [ X(9:2) ]
simon 0:e2ef12467862 35 // 3 [ Y(9:2) ]
simon 0:e2ef12467862 36 // 4 [ Z(9:2) ]
simon 0:e2ef12467862 37 //
simon 0:e2ef12467862 38
simon 0:e2ef12467862 39 #include <stdio.h>
simon 0:e2ef12467862 40 #include <math.h>
simon 0:e2ef12467862 41
simon 0:e2ef12467862 42 #define WII_ZERO 0x206
simon 0:e2ef12467862 43 #define WII_1G 0x6A
simon 0:e2ef12467862 44
simon 0:e2ef12467862 45 void Wiimote::decode(char* data) {
simon 0:e2ef12467862 46
simon 0:e2ef12467862 47 // read buttons
simon 0:e2ef12467862 48 left = (data[0] >> 0) & 0x1;
simon 0:e2ef12467862 49 right = (data[0] >> 1) & 0x1;
simon 0:e2ef12467862 50 down = (data[0] >> 2) & 0x1;
simon 0:e2ef12467862 51 up = (data[0] >> 3) & 0x1;
simon 0:e2ef12467862 52 plus = (data[0] >> 4) & 0x1;
simon 0:e2ef12467862 53 two = (data[1] >> 0) & 0x1;
simon 0:e2ef12467862 54 one = (data[1] >> 1) & 0x1;
simon 0:e2ef12467862 55 b = (data[1] >> 2) & 0x1;
simon 0:e2ef12467862 56 a = (data[1] >> 3) & 0x1;
simon 0:e2ef12467862 57 minus = (data[1] >> 4) & 0x1;
simon 0:e2ef12467862 58 home = (data[1] >> 7) & 0x1;
simon 0:e2ef12467862 59
simon 0:e2ef12467862 60 // read accelerometers
simon 0:e2ef12467862 61 rawx = (data[2] << 2) | (data[0] & 0x60) >> 5;
simon 0:e2ef12467862 62 rawy = (data[3] << 2) | (data[1] & 0x20) >> 4;
simon 0:e2ef12467862 63 rawz = (data[4] << 2) | (data[1] & 0x40) >> 5;
simon 0:e2ef12467862 64
simon 0:e2ef12467862 65 // calculate accelerometer gravity
simon 0:e2ef12467862 66 x = (float)((int)rawx - WII_ZERO) / (float)WII_1G;
simon 0:e2ef12467862 67 y = (float)((int)rawy - WII_ZERO) / (float)WII_1G;
simon 0:e2ef12467862 68 z = (float)((int)rawz - WII_ZERO) / (float)WII_1G;
simon 0:e2ef12467862 69
simon 0:e2ef12467862 70 // calculate wheel angle
simon 0:e2ef12467862 71 wheel = atan2(-y, -x) * (180.0 / 3.141592);
simon 0:e2ef12467862 72 }
simon 0:e2ef12467862 73
simon 0:e2ef12467862 74 void Wiimote::dump() {
simon 0:e2ef12467862 75 printf("%d%d%d%d%d%d%d%d%d%d%d %.3f %.3f %.3f %.2f\n", left, right, down, up, plus, two, one, b, a, minus, home, x, y, z, wheel);
simon 0:e2ef12467862 76 }
simon 0:e2ef12467862 77
simon 0:e2ef12467862 78
simon 0:e2ef12467862 79 // Accelerometer data
simon 0:e2ef12467862 80 // y-
simon 0:e2ef12467862 81 // +---+ +--+
simon 0:e2ef12467862 82 // | + | + B
simon 0:e2ef12467862 83 // | A | A |
simon 0:e2ef12467862 84 // | | | |
simon 0:e2ef12467862 85 // x+ | | x- z+ | | z-
simon 0:e2ef12467862 86 // | 1 | 1 |
simon 0:e2ef12467862 87 // | 2 | 2 |
simon 0:e2ef12467862 88 // +---+ +--+
simon 0:e2ef12467862 89 // y+
simon 0:e2ef12467862 90 //
simon 0:e2ef12467862 91 // x+ 0x19B
simon 0:e2ef12467862 92 // x0 0x205
simon 0:e2ef12467862 93 // x- 0x26E
simon 0:e2ef12467862 94 //
simon 0:e2ef12467862 95 // y+ 0x19C 0x6A
simon 0:e2ef12467862 96 // y0 0x206
simon 0:e2ef12467862 97 // y- 0x26E 0x68
simon 0:e2ef12467862 98 //
simon 0:e2ef12467862 99 // z+ 0x19C
simon 0:e2ef12467862 100 // z0 0x208
simon 0:e2ef12467862 101 // z- 0x26E