
We brought the joy and insanity of QWOP to real life. Test your coordination while helping this little guy win his race.
Dependencies: mbed
main.cpp@0:b95af9fb38a2, 2014-09-29 (annotated)
- Committer:
- adarsh5723
- Date:
- Mon Sep 29 05:50:52 2014 +0000
- Revision:
- 0:b95af9fb38a2
[x]QWOP augmented reality code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adarsh5723 | 0:b95af9fb38a2 | 1 | #define USB_KEYBOARD 0 |
adarsh5723 | 0:b95af9fb38a2 | 2 | |
adarsh5723 | 0:b95af9fb38a2 | 3 | #include "mbed.h" |
adarsh5723 | 0:b95af9fb38a2 | 4 | #include <math.h> |
adarsh5723 | 0:b95af9fb38a2 | 5 | |
adarsh5723 | 0:b95af9fb38a2 | 6 | #if USB_KEYBOARD |
adarsh5723 | 0:b95af9fb38a2 | 7 | #include "USBKeyboard.h" |
adarsh5723 | 0:b95af9fb38a2 | 8 | #endif |
adarsh5723 | 0:b95af9fb38a2 | 9 | |
adarsh5723 | 0:b95af9fb38a2 | 10 | |
adarsh5723 | 0:b95af9fb38a2 | 11 | |
adarsh5723 | 0:b95af9fb38a2 | 12 | #if USB_KEYBOARD |
adarsh5723 | 0:b95af9fb38a2 | 13 | //USBKeyboard keyboard; |
adarsh5723 | 0:b95af9fb38a2 | 14 | USBKeyboard keyboard(USBTX, USBRX); |
adarsh5723 | 0:b95af9fb38a2 | 15 | #else |
adarsh5723 | 0:b95af9fb38a2 | 16 | Serial pc(USBTX, USBRX); |
adarsh5723 | 0:b95af9fb38a2 | 17 | #endif |
adarsh5723 | 0:b95af9fb38a2 | 18 | |
adarsh5723 | 0:b95af9fb38a2 | 19 | // Leg sensors |
adarsh5723 | 0:b95af9fb38a2 | 20 | AnalogIn leg_left(A1); |
adarsh5723 | 0:b95af9fb38a2 | 21 | AnalogIn leg_right(A0); |
adarsh5723 | 0:b95af9fb38a2 | 22 | |
adarsh5723 | 0:b95af9fb38a2 | 23 | // Arm sensors |
adarsh5723 | 0:b95af9fb38a2 | 24 | AnalogIn arm_left(A3); |
adarsh5723 | 0:b95af9fb38a2 | 25 | AnalogIn arm_right(A2); |
adarsh5723 | 0:b95af9fb38a2 | 26 | |
adarsh5723 | 0:b95af9fb38a2 | 27 | DigitalOut ledRed(LED1); |
adarsh5723 | 0:b95af9fb38a2 | 28 | DigitalOut ledGreen(LED2); |
adarsh5723 | 0:b95af9fb38a2 | 29 | DigitalOut ledBlue(LED3); |
adarsh5723 | 0:b95af9fb38a2 | 30 | // Total count |
adarsh5723 | 0:b95af9fb38a2 | 31 | const unsigned int TOTAL_COUNTS = 10; |
adarsh5723 | 0:b95af9fb38a2 | 32 | |
adarsh5723 | 0:b95af9fb38a2 | 33 | // Clock count |
adarsh5723 | 0:b95af9fb38a2 | 34 | unsigned int clock_count = 0; |
adarsh5723 | 0:b95af9fb38a2 | 35 | |
adarsh5723 | 0:b95af9fb38a2 | 36 | // Threshold values |
adarsh5723 | 0:b95af9fb38a2 | 37 | float al_low = 0.457f; // left arm low |
adarsh5723 | 0:b95af9fb38a2 | 38 | float al_high = 0.530f; // left arm high |
adarsh5723 | 0:b95af9fb38a2 | 39 | float ar_low = 0.39f; // right arm low |
adarsh5723 | 0:b95af9fb38a2 | 40 | float ar_high = 0.420f; // right arm high |
adarsh5723 | 0:b95af9fb38a2 | 41 | float ll_low = 0.579f; // left leg low |
adarsh5723 | 0:b95af9fb38a2 | 42 | float ll_high = 0.60f; // left leg high |
adarsh5723 | 0:b95af9fb38a2 | 43 | float lr_low = 0.490f; // right leg low |
adarsh5723 | 0:b95af9fb38a2 | 44 | float lr_high = 0.510f; // right leg high |
adarsh5723 | 0:b95af9fb38a2 | 45 | |
adarsh5723 | 0:b95af9fb38a2 | 46 | unsigned int map_reading_to_modulo(float low, float high, float reading) { |
adarsh5723 | 0:b95af9fb38a2 | 47 | //(low, 1), (high, TOTAL_COUNTS - 1); |
adarsh5723 | 0:b95af9fb38a2 | 48 | float slope = ((TOTAL_COUNTS - 1) / (high - low)); // calculate slope |
adarsh5723 | 0:b95af9fb38a2 | 49 | float result = slope * reading - (slope * low); // transform reading from 1 to TOTAL_COUNTS |
adarsh5723 | 0:b95af9fb38a2 | 50 | return (int(floor(result)) + 1); |
adarsh5723 | 0:b95af9fb38a2 | 51 | } |
adarsh5723 | 0:b95af9fb38a2 | 52 | |
adarsh5723 | 0:b95af9fb38a2 | 53 | void check_arm_left(float reading) { |
adarsh5723 | 0:b95af9fb38a2 | 54 | if (reading < al_low) { |
adarsh5723 | 0:b95af9fb38a2 | 55 | // Lower than the threshold |
adarsh5723 | 0:b95af9fb38a2 | 56 | // Fire an event every single clock count |
adarsh5723 | 0:b95af9fb38a2 | 57 | #if USB_KEYBOARD |
adarsh5723 | 0:b95af9fb38a2 | 58 | keyboard.keyCode('o'); |
adarsh5723 | 0:b95af9fb38a2 | 59 | #else |
adarsh5723 | 0:b95af9fb38a2 | 60 | pc.putc('o'); |
adarsh5723 | 0:b95af9fb38a2 | 61 | #endif |
adarsh5723 | 0:b95af9fb38a2 | 62 | |
adarsh5723 | 0:b95af9fb38a2 | 63 | } else if (al_low <= reading && reading < al_high ) { |
adarsh5723 | 0:b95af9fb38a2 | 64 | // Find mapping |
adarsh5723 | 0:b95af9fb38a2 | 65 | int mod = map_reading_to_modulo(al_low, al_high, reading); |
adarsh5723 | 0:b95af9fb38a2 | 66 | //int real_reading = int(floor(reading)); |
adarsh5723 | 0:b95af9fb38a2 | 67 | if (clock_count % mod == 0) { |
adarsh5723 | 0:b95af9fb38a2 | 68 | #if USB_KEYBOARD |
adarsh5723 | 0:b95af9fb38a2 | 69 | keyboard.keyCode('o'); |
adarsh5723 | 0:b95af9fb38a2 | 70 | #else |
adarsh5723 | 0:b95af9fb38a2 | 71 | pc.putc('o'); |
adarsh5723 | 0:b95af9fb38a2 | 72 | #endif |
adarsh5723 | 0:b95af9fb38a2 | 73 | } |
adarsh5723 | 0:b95af9fb38a2 | 74 | } else { |
adarsh5723 | 0:b95af9fb38a2 | 75 | // Don't do anything |
adarsh5723 | 0:b95af9fb38a2 | 76 | } |
adarsh5723 | 0:b95af9fb38a2 | 77 | } |
adarsh5723 | 0:b95af9fb38a2 | 78 | |
adarsh5723 | 0:b95af9fb38a2 | 79 | |
adarsh5723 | 0:b95af9fb38a2 | 80 | void check_arm_right(float reading) { |
adarsh5723 | 0:b95af9fb38a2 | 81 | if (reading < ar_low) { |
adarsh5723 | 0:b95af9fb38a2 | 82 | // Lower than the threshold |
adarsh5723 | 0:b95af9fb38a2 | 83 | // Fire an event every single clock count |
adarsh5723 | 0:b95af9fb38a2 | 84 | #if USB_KEYBOARD |
adarsh5723 | 0:b95af9fb38a2 | 85 | keyboard.keyCode('p'); |
adarsh5723 | 0:b95af9fb38a2 | 86 | #else |
adarsh5723 | 0:b95af9fb38a2 | 87 | pc.putc('p'); |
adarsh5723 | 0:b95af9fb38a2 | 88 | #endif |
adarsh5723 | 0:b95af9fb38a2 | 89 | } else if (ar_low <= reading && reading < ar_high ) { |
adarsh5723 | 0:b95af9fb38a2 | 90 | // Find mapping |
adarsh5723 | 0:b95af9fb38a2 | 91 | int mod = map_reading_to_modulo(ar_low, ar_high, reading); |
adarsh5723 | 0:b95af9fb38a2 | 92 | //int real_reading = int(floor(reading)); |
adarsh5723 | 0:b95af9fb38a2 | 93 | if (clock_count % mod == 0) { |
adarsh5723 | 0:b95af9fb38a2 | 94 | #if USB_KEYBOARD |
adarsh5723 | 0:b95af9fb38a2 | 95 | keyboard.keyCode('p'); |
adarsh5723 | 0:b95af9fb38a2 | 96 | #else |
adarsh5723 | 0:b95af9fb38a2 | 97 | pc.putc('p'); |
adarsh5723 | 0:b95af9fb38a2 | 98 | #endif |
adarsh5723 | 0:b95af9fb38a2 | 99 | } |
adarsh5723 | 0:b95af9fb38a2 | 100 | } else { |
adarsh5723 | 0:b95af9fb38a2 | 101 | // Don't do anything |
adarsh5723 | 0:b95af9fb38a2 | 102 | } |
adarsh5723 | 0:b95af9fb38a2 | 103 | } |
adarsh5723 | 0:b95af9fb38a2 | 104 | |
adarsh5723 | 0:b95af9fb38a2 | 105 | |
adarsh5723 | 0:b95af9fb38a2 | 106 | |
adarsh5723 | 0:b95af9fb38a2 | 107 | void check_leg_left(float reading) { |
adarsh5723 | 0:b95af9fb38a2 | 108 | ledGreen = 1; |
adarsh5723 | 0:b95af9fb38a2 | 109 | ledBlue = 1; |
adarsh5723 | 0:b95af9fb38a2 | 110 | ledRed = 1; |
adarsh5723 | 0:b95af9fb38a2 | 111 | if (reading < ll_low) { |
adarsh5723 | 0:b95af9fb38a2 | 112 | // Lower than the threshold |
adarsh5723 | 0:b95af9fb38a2 | 113 | // Fire an event every single clock count |
adarsh5723 | 0:b95af9fb38a2 | 114 | #if USB_KEYBOARD |
adarsh5723 | 0:b95af9fb38a2 | 115 | keyboard.keyCode('q'); |
adarsh5723 | 0:b95af9fb38a2 | 116 | #else |
adarsh5723 | 0:b95af9fb38a2 | 117 | pc.putc('q'); |
adarsh5723 | 0:b95af9fb38a2 | 118 | #endif |
adarsh5723 | 0:b95af9fb38a2 | 119 | ledRed = 0; |
adarsh5723 | 0:b95af9fb38a2 | 120 | |
adarsh5723 | 0:b95af9fb38a2 | 121 | } else if (ll_low <= reading && reading < ll_high ) { |
adarsh5723 | 0:b95af9fb38a2 | 122 | // Find mapping |
adarsh5723 | 0:b95af9fb38a2 | 123 | ledGreen = 0; |
adarsh5723 | 0:b95af9fb38a2 | 124 | int mod = map_reading_to_modulo(al_low, al_high, reading); |
adarsh5723 | 0:b95af9fb38a2 | 125 | //int real_reading = int(floor(reading)); |
adarsh5723 | 0:b95af9fb38a2 | 126 | if (clock_count % mod == 0) { |
adarsh5723 | 0:b95af9fb38a2 | 127 | #if USB_KEYBOARD |
adarsh5723 | 0:b95af9fb38a2 | 128 | keyboard.keyCode('q'); |
adarsh5723 | 0:b95af9fb38a2 | 129 | #else |
adarsh5723 | 0:b95af9fb38a2 | 130 | pc.putc('q'); |
adarsh5723 | 0:b95af9fb38a2 | 131 | #endif |
adarsh5723 | 0:b95af9fb38a2 | 132 | } |
adarsh5723 | 0:b95af9fb38a2 | 133 | } else { |
adarsh5723 | 0:b95af9fb38a2 | 134 | ledBlue = 0; |
adarsh5723 | 0:b95af9fb38a2 | 135 | // Don't do anything |
adarsh5723 | 0:b95af9fb38a2 | 136 | } |
adarsh5723 | 0:b95af9fb38a2 | 137 | } |
adarsh5723 | 0:b95af9fb38a2 | 138 | |
adarsh5723 | 0:b95af9fb38a2 | 139 | |
adarsh5723 | 0:b95af9fb38a2 | 140 | void check_leg_right(float reading) { |
adarsh5723 | 0:b95af9fb38a2 | 141 | if (reading < lr_low) { |
adarsh5723 | 0:b95af9fb38a2 | 142 | // Higher than the threshold |
adarsh5723 | 0:b95af9fb38a2 | 143 | // Fire an event every single clock count |
adarsh5723 | 0:b95af9fb38a2 | 144 | #if USB_KEYBOARD |
adarsh5723 | 0:b95af9fb38a2 | 145 | keyboard.keyCode('w'); |
adarsh5723 | 0:b95af9fb38a2 | 146 | #else |
adarsh5723 | 0:b95af9fb38a2 | 147 | pc.putc('w'); |
adarsh5723 | 0:b95af9fb38a2 | 148 | #endif |
adarsh5723 | 0:b95af9fb38a2 | 149 | |
adarsh5723 | 0:b95af9fb38a2 | 150 | } else if (lr_low <= reading && reading < lr_high ) { |
adarsh5723 | 0:b95af9fb38a2 | 151 | // Find mapping |
adarsh5723 | 0:b95af9fb38a2 | 152 | int mod = map_reading_to_modulo(al_low, al_high, reading); |
adarsh5723 | 0:b95af9fb38a2 | 153 | //int real_reading = int(floor(reading)); |
adarsh5723 | 0:b95af9fb38a2 | 154 | if (clock_count % mod == 0) { |
adarsh5723 | 0:b95af9fb38a2 | 155 | #if USB_KEYBOARD |
adarsh5723 | 0:b95af9fb38a2 | 156 | keyboard.keyCode('w'); |
adarsh5723 | 0:b95af9fb38a2 | 157 | #else |
adarsh5723 | 0:b95af9fb38a2 | 158 | pc.putc('w'); |
adarsh5723 | 0:b95af9fb38a2 | 159 | #endif |
adarsh5723 | 0:b95af9fb38a2 | 160 | } |
adarsh5723 | 0:b95af9fb38a2 | 161 | } else { |
adarsh5723 | 0:b95af9fb38a2 | 162 | |
adarsh5723 | 0:b95af9fb38a2 | 163 | // Don't do anything |
adarsh5723 | 0:b95af9fb38a2 | 164 | } |
adarsh5723 | 0:b95af9fb38a2 | 165 | } |
adarsh5723 | 0:b95af9fb38a2 | 166 | |
adarsh5723 | 0:b95af9fb38a2 | 167 | |
adarsh5723 | 0:b95af9fb38a2 | 168 | int main() { |
adarsh5723 | 0:b95af9fb38a2 | 169 | ledRed = 1; |
adarsh5723 | 0:b95af9fb38a2 | 170 | ledGreen = 1; |
adarsh5723 | 0:b95af9fb38a2 | 171 | ledBlue = 0; |
adarsh5723 | 0:b95af9fb38a2 | 172 | wait(1.0f); |
adarsh5723 | 0:b95af9fb38a2 | 173 | while(1) { |
adarsh5723 | 0:b95af9fb38a2 | 174 | #if 1 |
adarsh5723 | 0:b95af9fb38a2 | 175 | |
adarsh5723 | 0:b95af9fb38a2 | 176 | ledRed = 1; |
adarsh5723 | 0:b95af9fb38a2 | 177 | ledGreen = 1; |
adarsh5723 | 0:b95af9fb38a2 | 178 | |
adarsh5723 | 0:b95af9fb38a2 | 179 | // Check for movement |
adarsh5723 | 0:b95af9fb38a2 | 180 | check_arm_left(arm_left.read()); |
adarsh5723 | 0:b95af9fb38a2 | 181 | check_arm_right(arm_right.read()); |
adarsh5723 | 0:b95af9fb38a2 | 182 | check_leg_left(leg_left.read()); |
adarsh5723 | 0:b95af9fb38a2 | 183 | check_leg_right(leg_right.read()); |
adarsh5723 | 0:b95af9fb38a2 | 184 | |
adarsh5723 | 0:b95af9fb38a2 | 185 | // Cycle the Clock |
adarsh5723 | 0:b95af9fb38a2 | 186 | wait(0.1f); // Should be around 25 iterations pers second |
adarsh5723 | 0:b95af9fb38a2 | 187 | clock_count = (clock_count + 1) % TOTAL_COUNTS; |
adarsh5723 | 0:b95af9fb38a2 | 188 | #else |
adarsh5723 | 0:b95af9fb38a2 | 189 | //Debug code |
adarsh5723 | 0:b95af9fb38a2 | 190 | keyboard.keyCode('q'); |
adarsh5723 | 0:b95af9fb38a2 | 191 | keyboard.keyCode('w'); |
adarsh5723 | 0:b95af9fb38a2 | 192 | keyboard.keyCode('o'); |
adarsh5723 | 0:b95af9fb38a2 | 193 | keyboard.keyCode('p'); |
adarsh5723 | 0:b95af9fb38a2 | 194 | wait(1.0f); |
adarsh5723 | 0:b95af9fb38a2 | 195 | #endif |
adarsh5723 | 0:b95af9fb38a2 | 196 | } |
adarsh5723 | 0:b95af9fb38a2 | 197 | } |
adarsh5723 | 0:b95af9fb38a2 | 198 |