Program used to get X, Y, and Z inputs from the on-board accelerometer of the K64F chip. These components are used to calculate angles of pitch and roll. These angles are then used to detect various motions that are encoded and sent over the serial port to Unity gaming engine. This allows for the board to act as a controller for a Super Mario Bros. demo that we have created.
Fork of Project1_Group_Black by
Unity Super Mario Bros. Demo Project used with controller configuration code.
/media/uploads/mvanderpohl/super_mario_bros._demo_final_copy.zip
ControllerConfigA.cpp@0:083d79f59957, 2016-03-30 (annotated)
- Committer:
- mvanderpohl
- Date:
- Wed Mar 30 19:49:12 2016 +0000
- Revision:
- 0:083d79f59957
Publishing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mvanderpohl | 0:083d79f59957 | 1 | #include "mbed.h" |
mvanderpohl | 0:083d79f59957 | 2 | #include "FXOS8700Q.h" |
mvanderpohl | 0:083d79f59957 | 3 | //defines |
mvanderpohl | 0:083d79f59957 | 4 | #define DELAY 20 |
mvanderpohl | 0:083d79f59957 | 5 | |
mvanderpohl | 0:083d79f59957 | 6 | //I2C lines for FXOS8700Q accelerometer |
mvanderpohl | 0:083d79f59957 | 7 | FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); |
mvanderpohl | 0:083d79f59957 | 8 | |
mvanderpohl | 0:083d79f59957 | 9 | |
mvanderpohl | 0:083d79f59957 | 10 | Serial pc(PTC15, PTC14); //pins used in Bluetooth communication |
mvanderpohl | 0:083d79f59957 | 11 | //Serial pc(USBTX, USBRX); //pins used in serial USB communication |
mvanderpohl | 0:083d79f59957 | 12 | |
mvanderpohl | 0:083d79f59957 | 13 | DigitalIn sw2(SW2); |
mvanderpohl | 0:083d79f59957 | 14 | DigitalIn sw3(SW3); |
mvanderpohl | 0:083d79f59957 | 15 | |
mvanderpohl | 0:083d79f59957 | 16 | void getAccValues(); |
mvanderpohl | 0:083d79f59957 | 17 | void calculateTilt(float*,float*); |
mvanderpohl | 0:083d79f59957 | 18 | void sendDefault(int); |
mvanderpohl | 0:083d79f59957 | 19 | void sendJump(int); |
mvanderpohl | 0:083d79f59957 | 20 | void sendLeft(int); |
mvanderpohl | 0:083d79f59957 | 21 | void sendFastLeft(int); |
mvanderpohl | 0:083d79f59957 | 22 | void sendRight(int); |
mvanderpohl | 0:083d79f59957 | 23 | void sendFastRight(int); |
mvanderpohl | 0:083d79f59957 | 24 | void sendPause(int); |
mvanderpohl | 0:083d79f59957 | 25 | void sendDown(int); |
mvanderpohl | 0:083d79f59957 | 26 | |
mvanderpohl | 0:083d79f59957 | 27 | float accX, accY, accZ; |
mvanderpohl | 0:083d79f59957 | 28 | bool sendingData = false; |
mvanderpohl | 0:083d79f59957 | 29 | |
mvanderpohl | 0:083d79f59957 | 30 | int main() |
mvanderpohl | 0:083d79f59957 | 31 | { |
mvanderpohl | 0:083d79f59957 | 32 | //sensor values |
mvanderpohl | 0:083d79f59957 | 33 | float pitch, roll; |
mvanderpohl | 0:083d79f59957 | 34 | |
mvanderpohl | 0:083d79f59957 | 35 | //COUDL ADJUST BAUD RATE FOR UNITY |
mvanderpohl | 0:083d79f59957 | 36 | pc.baud(9600); |
mvanderpohl | 0:083d79f59957 | 37 | |
mvanderpohl | 0:083d79f59957 | 38 | //enable the accelerometer |
mvanderpohl | 0:083d79f59957 | 39 | acc.enable(); |
mvanderpohl | 0:083d79f59957 | 40 | |
mvanderpohl | 0:083d79f59957 | 41 | while (true) |
mvanderpohl | 0:083d79f59957 | 42 | { |
mvanderpohl | 0:083d79f59957 | 43 | getAccValues(); |
mvanderpohl | 0:083d79f59957 | 44 | calculateTilt(&pitch, &roll); |
mvanderpohl | 0:083d79f59957 | 45 | |
mvanderpohl | 0:083d79f59957 | 46 | //IF MOVING RIGHT |
mvanderpohl | 0:083d79f59957 | 47 | if(roll > 0.6) |
mvanderpohl | 0:083d79f59957 | 48 | { |
mvanderpohl | 0:083d79f59957 | 49 | if(roll > 0.8) |
mvanderpohl | 0:083d79f59957 | 50 | { |
mvanderpohl | 0:083d79f59957 | 51 | sendFastRight(DELAY); |
mvanderpohl | 0:083d79f59957 | 52 | sendingData = true; |
mvanderpohl | 0:083d79f59957 | 53 | } |
mvanderpohl | 0:083d79f59957 | 54 | else |
mvanderpohl | 0:083d79f59957 | 55 | { |
mvanderpohl | 0:083d79f59957 | 56 | sendRight(DELAY); |
mvanderpohl | 0:083d79f59957 | 57 | sendingData = true; |
mvanderpohl | 0:083d79f59957 | 58 | } |
mvanderpohl | 0:083d79f59957 | 59 | } |
mvanderpohl | 0:083d79f59957 | 60 | |
mvanderpohl | 0:083d79f59957 | 61 | //IF MOVING LEFT |
mvanderpohl | 0:083d79f59957 | 62 | if(roll < -0.6) |
mvanderpohl | 0:083d79f59957 | 63 | { |
mvanderpohl | 0:083d79f59957 | 64 | if(roll < -0.8) |
mvanderpohl | 0:083d79f59957 | 65 | { |
mvanderpohl | 0:083d79f59957 | 66 | sendFastLeft(DELAY); |
mvanderpohl | 0:083d79f59957 | 67 | sendingData = true; |
mvanderpohl | 0:083d79f59957 | 68 | } |
mvanderpohl | 0:083d79f59957 | 69 | else |
mvanderpohl | 0:083d79f59957 | 70 | { |
mvanderpohl | 0:083d79f59957 | 71 | sendLeft(DELAY); |
mvanderpohl | 0:083d79f59957 | 72 | sendingData = true; |
mvanderpohl | 0:083d79f59957 | 73 | } |
mvanderpohl | 0:083d79f59957 | 74 | } |
mvanderpohl | 0:083d79f59957 | 75 | |
mvanderpohl | 0:083d79f59957 | 76 | //IF DUCKING OR GOING DOWN PIPE |
mvanderpohl | 0:083d79f59957 | 77 | if(pitch < -0.8) |
mvanderpohl | 0:083d79f59957 | 78 | { |
mvanderpohl | 0:083d79f59957 | 79 | sendDown(DELAY); |
mvanderpohl | 0:083d79f59957 | 80 | sendingData = true; |
mvanderpohl | 0:083d79f59957 | 81 | } |
mvanderpohl | 0:083d79f59957 | 82 | |
mvanderpohl | 0:083d79f59957 | 83 | //IF JUMPING |
mvanderpohl | 0:083d79f59957 | 84 | if(sw2 == 0) |
mvanderpohl | 0:083d79f59957 | 85 | { |
mvanderpohl | 0:083d79f59957 | 86 | sendJump(DELAY); |
mvanderpohl | 0:083d79f59957 | 87 | sendingData = true; |
mvanderpohl | 0:083d79f59957 | 88 | } |
mvanderpohl | 0:083d79f59957 | 89 | |
mvanderpohl | 0:083d79f59957 | 90 | if(sw3 == 0) |
mvanderpohl | 0:083d79f59957 | 91 | { |
mvanderpohl | 0:083d79f59957 | 92 | sendPause(DELAY); |
mvanderpohl | 0:083d79f59957 | 93 | sendingData = true; |
mvanderpohl | 0:083d79f59957 | 94 | } |
mvanderpohl | 0:083d79f59957 | 95 | |
mvanderpohl | 0:083d79f59957 | 96 | //only send default value if we have no valid data to send |
mvanderpohl | 0:083d79f59957 | 97 | if(sendingData == false) |
mvanderpohl | 0:083d79f59957 | 98 | { |
mvanderpohl | 0:083d79f59957 | 99 | sendDefault(DELAY); |
mvanderpohl | 0:083d79f59957 | 100 | } |
mvanderpohl | 0:083d79f59957 | 101 | |
mvanderpohl | 0:083d79f59957 | 102 | //done sending data |
mvanderpohl | 0:083d79f59957 | 103 | sendingData = false; |
mvanderpohl | 0:083d79f59957 | 104 | |
mvanderpohl | 0:083d79f59957 | 105 | |
mvanderpohl | 0:083d79f59957 | 106 | //remember multiple if statements can happen each loop and contribute thier own delay so in the worse case |
mvanderpohl | 0:083d79f59957 | 107 | //we delay number of actions * DELAY otherwise we send default case and delay for default time |
mvanderpohl | 0:083d79f59957 | 108 | //there is also a slight delay in calculating pitch/roll and getting accel values |
mvanderpohl | 0:083d79f59957 | 109 | |
mvanderpohl | 0:083d79f59957 | 110 | }//end while |
mvanderpohl | 0:083d79f59957 | 111 | }//end main |
mvanderpohl | 0:083d79f59957 | 112 | |
mvanderpohl | 0:083d79f59957 | 113 | void getAccValues() |
mvanderpohl | 0:083d79f59957 | 114 | { |
mvanderpohl | 0:083d79f59957 | 115 | //read x y z from the accelerometer |
mvanderpohl | 0:083d79f59957 | 116 | acc.getX(&accX); |
mvanderpohl | 0:083d79f59957 | 117 | acc.getY(&accY); |
mvanderpohl | 0:083d79f59957 | 118 | acc.getZ(&accZ); |
mvanderpohl | 0:083d79f59957 | 119 | } |
mvanderpohl | 0:083d79f59957 | 120 | |
mvanderpohl | 0:083d79f59957 | 121 | void calculateTilt(float* pitch, float* roll) |
mvanderpohl | 0:083d79f59957 | 122 | { |
mvanderpohl | 0:083d79f59957 | 123 | //calculate pitch and roll |
mvanderpohl | 0:083d79f59957 | 124 | *pitch = atan2(accY,(sqrt(pow(accX, 2) + pow(accZ, 2 )))); |
mvanderpohl | 0:083d79f59957 | 125 | *roll = atan2((-1 * accX),accZ); |
mvanderpohl | 0:083d79f59957 | 126 | } |
mvanderpohl | 0:083d79f59957 | 127 | |
mvanderpohl | 0:083d79f59957 | 128 | void sendDefault(int delay) |
mvanderpohl | 0:083d79f59957 | 129 | { |
mvanderpohl | 0:083d79f59957 | 130 | pc.printf("%d",0); |
mvanderpohl | 0:083d79f59957 | 131 | wait_ms(delay); |
mvanderpohl | 0:083d79f59957 | 132 | } |
mvanderpohl | 0:083d79f59957 | 133 | |
mvanderpohl | 0:083d79f59957 | 134 | void sendJump(int delay) |
mvanderpohl | 0:083d79f59957 | 135 | { |
mvanderpohl | 0:083d79f59957 | 136 | pc.printf("%d", 1); |
mvanderpohl | 0:083d79f59957 | 137 | wait_ms(delay); |
mvanderpohl | 0:083d79f59957 | 138 | } |
mvanderpohl | 0:083d79f59957 | 139 | |
mvanderpohl | 0:083d79f59957 | 140 | void sendLeft(int delay) |
mvanderpohl | 0:083d79f59957 | 141 | { |
mvanderpohl | 0:083d79f59957 | 142 | pc.printf("%d",3); |
mvanderpohl | 0:083d79f59957 | 143 | wait_ms(delay); |
mvanderpohl | 0:083d79f59957 | 144 | } |
mvanderpohl | 0:083d79f59957 | 145 | |
mvanderpohl | 0:083d79f59957 | 146 | void sendRight(int delay) |
mvanderpohl | 0:083d79f59957 | 147 | { |
mvanderpohl | 0:083d79f59957 | 148 | pc.printf("%d",4); |
mvanderpohl | 0:083d79f59957 | 149 | wait_ms(delay); |
mvanderpohl | 0:083d79f59957 | 150 | } |
mvanderpohl | 0:083d79f59957 | 151 | void sendFastRight(int delay) |
mvanderpohl | 0:083d79f59957 | 152 | { |
mvanderpohl | 0:083d79f59957 | 153 | pc.printf("%d",5); |
mvanderpohl | 0:083d79f59957 | 154 | wait_ms(delay); |
mvanderpohl | 0:083d79f59957 | 155 | } |
mvanderpohl | 0:083d79f59957 | 156 | void sendFastLeft(int delay) |
mvanderpohl | 0:083d79f59957 | 157 | { |
mvanderpohl | 0:083d79f59957 | 158 | pc.printf("%d",6); |
mvanderpohl | 0:083d79f59957 | 159 | wait_ms(delay); |
mvanderpohl | 0:083d79f59957 | 160 | } |
mvanderpohl | 0:083d79f59957 | 161 | void sendPause(int delay) |
mvanderpohl | 0:083d79f59957 | 162 | { |
mvanderpohl | 0:083d79f59957 | 163 | pc.printf("%d",7); |
mvanderpohl | 0:083d79f59957 | 164 | wait_ms(delay); |
mvanderpohl | 0:083d79f59957 | 165 | } |
mvanderpohl | 0:083d79f59957 | 166 | void sendDown(int delay) |
mvanderpohl | 0:083d79f59957 | 167 | { |
mvanderpohl | 0:083d79f59957 | 168 | pc.printf("%d",8); |
mvanderpohl | 0:083d79f59957 | 169 | wait_ms(delay); |
mvanderpohl | 0:083d79f59957 | 170 | } |