a

Dependencies:   mbed

Fork of ESE350-Whack-a-Mole by Eric Berdinis

Committer:
shibulal
Date:
Mon Dec 07 22:32:48 2015 +0000
Revision:
1:0471772dfce5
Parent:
WhackAMole.cpp@0:ddc820578cb0
ew

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mlab4 0:ddc820578cb0 1 #include "mbed.h"
mlab4 0:ddc820578cb0 2 #include "MRF24J40.h"
mlab4 0:ddc820578cb0 3
mlab4 0:ddc820578cb0 4 #include <string>
mlab4 0:ddc820578cb0 5
mlab4 0:ddc820578cb0 6 // RF tranceiver to link with handheld.
mlab4 0:ddc820578cb0 7 MRF24J40 mrf(p11, p12, p13, p14, p21);
shibulal 1:0471772dfce5 8 InterruptIn in(p8);
mlab4 0:ddc820578cb0 9 // LEDs you can treat these as variables (led2 = 1 will turn led2 on!)
mlab4 0:ddc820578cb0 10 DigitalOut led1(LED1);
mlab4 0:ddc820578cb0 11 DigitalOut led2(LED2);
mlab4 0:ddc820578cb0 12 DigitalOut led3(LED3);
mlab4 0:ddc820578cb0 13 DigitalOut led4(LED4);
mlab4 0:ddc820578cb0 14
mlab4 0:ddc820578cb0 15 // Timer
mlab4 0:ddc820578cb0 16 Timer timer;
shibulal 1:0471772dfce5 17 Timer t1;
mlab4 0:ddc820578cb0 18 // Serial port for showing RX data.
mlab4 0:ddc820578cb0 19 Serial pc(USBTX, USBRX);
mlab4 0:ddc820578cb0 20
mlab4 0:ddc820578cb0 21 // Used for sending and receiving
mlab4 0:ddc820578cb0 22 char txBuffer[128];
mlab4 0:ddc820578cb0 23 char rxBuffer[128];
mlab4 0:ddc820578cb0 24 int rxLen;
shibulal 1:0471772dfce5 25 int t_period = 0;
shibulal 1:0471772dfce5 26 float t_freq = 0;
mlab4 0:ddc820578cb0 27
mlab4 0:ddc820578cb0 28 //***************** Do not change these methods (please) *****************//
mlab4 0:ddc820578cb0 29
mlab4 0:ddc820578cb0 30 /**
mlab4 0:ddc820578cb0 31 * Receive data from the MRF24J40.
mlab4 0:ddc820578cb0 32 *
mlab4 0:ddc820578cb0 33 * @param data A pointer to a char array to hold the data
mlab4 0:ddc820578cb0 34 * @param maxLength The max amount of data to read.
mlab4 0:ddc820578cb0 35 */
mlab4 0:ddc820578cb0 36 int rf_receive(char *data, uint8_t maxLength)
mlab4 0:ddc820578cb0 37 {
mlab4 0:ddc820578cb0 38 uint8_t len = mrf.Receive((uint8_t *)data, maxLength);
mlab4 0:ddc820578cb0 39 uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
mlab4 0:ddc820578cb0 40
mlab4 0:ddc820578cb0 41 if(len > 10) {
mlab4 0:ddc820578cb0 42 //Remove the header and footer of the message
mlab4 0:ddc820578cb0 43 for(uint8_t i = 0; i < len-2; i++) {
mlab4 0:ddc820578cb0 44 if(i<8) {
mlab4 0:ddc820578cb0 45 //Make sure our header is valid first
mlab4 0:ddc820578cb0 46 if(data[i] != header[i])
mlab4 0:ddc820578cb0 47 return 0;
mlab4 0:ddc820578cb0 48 } else {
mlab4 0:ddc820578cb0 49 data[i-8] = data[i];
mlab4 0:ddc820578cb0 50 }
mlab4 0:ddc820578cb0 51 }
mlab4 0:ddc820578cb0 52
mlab4 0:ddc820578cb0 53 //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10);
mlab4 0:ddc820578cb0 54 }
mlab4 0:ddc820578cb0 55 return ((int)len)-10;
mlab4 0:ddc820578cb0 56 }
mlab4 0:ddc820578cb0 57
mlab4 0:ddc820578cb0 58 /**
mlab4 0:ddc820578cb0 59 * Send data to another MRF24J40.
mlab4 0:ddc820578cb0 60 *
mlab4 0:ddc820578cb0 61 * @param data The string to send
mlab4 0:ddc820578cb0 62 * @param maxLength The length of the data to send.
mlab4 0:ddc820578cb0 63 * If you are sending a null-terminated string you can pass strlen(data)+1
mlab4 0:ddc820578cb0 64 */
mlab4 0:ddc820578cb0 65 void rf_send(char *data, uint8_t len)
mlab4 0:ddc820578cb0 66 {
mlab4 0:ddc820578cb0 67 //We need to prepend the message with a valid ZigBee header
mlab4 0:ddc820578cb0 68 uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
mlab4 0:ddc820578cb0 69 uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) );
mlab4 0:ddc820578cb0 70
mlab4 0:ddc820578cb0 71 for(uint8_t i = 0; i < len+8; i++) {
mlab4 0:ddc820578cb0 72 //prepend the 8-byte header
mlab4 0:ddc820578cb0 73 send_buf[i] = (i<8) ? header[i] : data[i-8];
mlab4 0:ddc820578cb0 74 }
mlab4 0:ddc820578cb0 75 //pc.printf("Sent: %s\r\n", send_buf+8);
mlab4 0:ddc820578cb0 76
mlab4 0:ddc820578cb0 77 mrf.Send(send_buf, len+8);
mlab4 0:ddc820578cb0 78 free(send_buf);
mlab4 0:ddc820578cb0 79 }
shibulal 1:0471772dfce5 80 void move_forward(){
shibulal 1:0471772dfce5 81 //fill here
shibulal 1:0471772dfce5 82 }
shibulal 1:0471772dfce5 83 void stop(){
shibulal 1:0471772dfce5 84 //fill here
shibulal 1:0471772dfce5 85 }
shibulal 1:0471772dfce5 86 void move_hv_transition_point_1(){
shibulal 1:0471772dfce5 87 move_forward();
shibulal 1:0471772dfce5 88 while (!(t_freq>=50 && t_freq<=150));
shibulal 1:0471772dfce5 89 stop();
shibulal 1:0471772dfce5 90 }
shibulal 1:0471772dfce5 91 void close_gate1(){
shibulal 1:0471772dfce5 92 strcpy(txBuffer, "Close_Gate_1");
shibulal 1:0471772dfce5 93 rf_send(txBuffer, strlen(txBuffer)+1);
shibulal 1:0471772dfce5 94 pc.printf("Sent: %s\r\n", txBuffer);
shibulal 1:0471772dfce5 95 while(true){
shibulal 1:0471772dfce5 96 rxLen = rf_receive(rxBuffer, 128);
shibulal 1:0471772dfce5 97 if (rxLen>0){
shibulal 1:0471772dfce5 98 pc.printf("Received: %s\r\n", rxBuffer);
shibulal 1:0471772dfce5 99 if(!strcmp("Gate_1_Closed", rxBuffer)) {
shibulal 1:0471772dfce5 100 break;
shibulal 1:0471772dfce5 101 }
shibulal 1:0471772dfce5 102 }
shibulal 1:0471772dfce5 103 }
shibulal 1:0471772dfce5 104 }
shibulal 1:0471772dfce5 105 void close_gate2(){
shibulal 1:0471772dfce5 106 strcpy(txBuffer, "Close_Gate_2");
shibulal 1:0471772dfce5 107 rf_send(txBuffer, strlen(txBuffer)+1);
shibulal 1:0471772dfce5 108 pc.printf("Sent: %s\r\n", txBuffer);
shibulal 1:0471772dfce5 109 while(true){
shibulal 1:0471772dfce5 110 rxLen = rf_receive(rxBuffer, 128);
shibulal 1:0471772dfce5 111 if (rxLen>0){
shibulal 1:0471772dfce5 112 pc.printf("Received: %s\r\n", rxBuffer);
shibulal 1:0471772dfce5 113 if(!strcmp("Gate_2_Closed", rxBuffer)) {
shibulal 1:0471772dfce5 114 break;
shibulal 1:0471772dfce5 115 }
shibulal 1:0471772dfce5 116 }
shibulal 1:0471772dfce5 117 }
shibulal 1:0471772dfce5 118 }
shibulal 1:0471772dfce5 119 void open_gate1(){
shibulal 1:0471772dfce5 120 strcpy(txBuffer, "Open_Gate_1");
shibulal 1:0471772dfce5 121 rf_send(txBuffer, strlen(txBuffer)+1);
shibulal 1:0471772dfce5 122 pc.printf("Sent: %s\r\n", txBuffer);
shibulal 1:0471772dfce5 123 while(true){
shibulal 1:0471772dfce5 124 rxLen = rf_receive(rxBuffer, 128);
shibulal 1:0471772dfce5 125 if (rxLen>0){
shibulal 1:0471772dfce5 126 pc.printf("Received: %s\r\n", rxBuffer);
shibulal 1:0471772dfce5 127 if(!strcmp("Gate_1_Opened", rxBuffer)) {
shibulal 1:0471772dfce5 128 break;
shibulal 1:0471772dfce5 129 }
shibulal 1:0471772dfce5 130 }
shibulal 1:0471772dfce5 131 }
shibulal 1:0471772dfce5 132 }
shibulal 1:0471772dfce5 133 void open_gate2(){
shibulal 1:0471772dfce5 134 strcpy(txBuffer, "Open_Gate_2");
shibulal 1:0471772dfce5 135 rf_send(txBuffer, strlen(txBuffer)+1);
shibulal 1:0471772dfce5 136 pc.printf("Sent: %s\r\n", txBuffer);
shibulal 1:0471772dfce5 137 while(true){
shibulal 1:0471772dfce5 138 rxLen = rf_receive(rxBuffer, 128);
shibulal 1:0471772dfce5 139 if (rxLen>0){
shibulal 1:0471772dfce5 140 pc.printf("Received: %s\r\n", rxBuffer);
shibulal 1:0471772dfce5 141 if(!strcmp("Gate_2_Opened", rxBuffer)) {
shibulal 1:0471772dfce5 142 break;
shibulal 1:0471772dfce5 143 }
shibulal 1:0471772dfce5 144 }
shibulal 1:0471772dfce5 145 }
shibulal 1:0471772dfce5 146 }
shibulal 1:0471772dfce5 147 void open_flap(){
shibulal 1:0471772dfce5 148 strcpy(txBuffer, "Open_Flap");
shibulal 1:0471772dfce5 149 rf_send(txBuffer, strlen(txBuffer)+1);
shibulal 1:0471772dfce5 150 pc.printf("Sent: %s\r\n", txBuffer);
shibulal 1:0471772dfce5 151 while(true){
shibulal 1:0471772dfce5 152 rxLen = rf_receive(rxBuffer, 128);
shibulal 1:0471772dfce5 153 if (rxLen>0){
shibulal 1:0471772dfce5 154 pc.printf("Received: %s\r\n", rxBuffer);
shibulal 1:0471772dfce5 155 if(!strcmp("Flap_Opened", rxBuffer)) {
shibulal 1:0471772dfce5 156 break;
shibulal 1:0471772dfce5 157 }
shibulal 1:0471772dfce5 158 }
shibulal 1:0471772dfce5 159 }
shibulal 1:0471772dfce5 160 }
shibulal 1:0471772dfce5 161 void close_flap(){
shibulal 1:0471772dfce5 162 strcpy(txBuffer, "Close_Flap");
shibulal 1:0471772dfce5 163 rf_send(txBuffer, strlen(txBuffer)+1);
shibulal 1:0471772dfce5 164 pc.printf("Sent: %s\r\n", txBuffer);
shibulal 1:0471772dfce5 165 while(true){
shibulal 1:0471772dfce5 166 rxLen = rf_receive(rxBuffer, 128);
shibulal 1:0471772dfce5 167 if (rxLen>0){
shibulal 1:0471772dfce5 168 pc.printf("Received: %s\r\n", rxBuffer);
shibulal 1:0471772dfce5 169 if(!strcmp("Flap_Closed", rxBuffer)) {
shibulal 1:0471772dfce5 170 break;
shibulal 1:0471772dfce5 171 }
shibulal 1:0471772dfce5 172 }
shibulal 1:0471772dfce5 173 }
shibulal 1:0471772dfce5 174 }
shibulal 1:0471772dfce5 175 void move_upto_first_level(){
shibulal 1:0471772dfce5 176 move_forward();
shibulal 1:0471772dfce5 177 while (!(t_freq>=200 && t_freq<=300));
shibulal 1:0471772dfce5 178 stop();
shibulal 1:0471772dfce5 179 }
shibulal 1:0471772dfce5 180 void move_upto_second_level(){
shibulal 1:0471772dfce5 181 move_forward();
shibulal 1:0471772dfce5 182 while (!(t_freq>=450 && t_freq<=550));
shibulal 1:0471772dfce5 183 stop();
shibulal 1:0471772dfce5 184 }
shibulal 1:0471772dfce5 185 void push(int floor){
shibulal 1:0471772dfce5 186 //activate conveyor to push
shibulal 1:0471772dfce5 187 if (floor==1){
shibulal 1:0471772dfce5 188 strcpy(txBuffer, "Push_1");
shibulal 1:0471772dfce5 189 }
shibulal 1:0471772dfce5 190 else if (floor==2){
shibulal 1:0471772dfce5 191 strcpy(txBuffer, "Push_2");
shibulal 1:0471772dfce5 192 }
shibulal 1:0471772dfce5 193 else if (floor==3){
shibulal 1:0471772dfce5 194 strcpy(txBuffer, "Push_3");
shibulal 1:0471772dfce5 195 }
shibulal 1:0471772dfce5 196 else if (floor==4){
shibulal 1:0471772dfce5 197 strcpy(txBuffer, "Push_4");
shibulal 1:0471772dfce5 198 }
shibulal 1:0471772dfce5 199 rf_send(txBuffer, strlen(txBuffer)+1);
shibulal 1:0471772dfce5 200 pc.printf("Sent: %s\r\n", txBuffer);
shibulal 1:0471772dfce5 201 //stop conveyor after waiting sufficient time
shibulal 1:0471772dfce5 202 }
shibulal 1:0471772dfce5 203 void pull(int floor){
shibulal 1:0471772dfce5 204 //activate conveyor to pull
shibulal 1:0471772dfce5 205 if (floor==1){
shibulal 1:0471772dfce5 206 strcpy(txBuffer, "Pull_1");
shibulal 1:0471772dfce5 207 }
shibulal 1:0471772dfce5 208 else if (floor==2){
shibulal 1:0471772dfce5 209 strcpy(txBuffer, "Pull_2");
shibulal 1:0471772dfce5 210 }
shibulal 1:0471772dfce5 211 else if (floor==3){
shibulal 1:0471772dfce5 212 strcpy(txBuffer, "Pull_3");
shibulal 1:0471772dfce5 213 }
shibulal 1:0471772dfce5 214 else if (floor==4){
shibulal 1:0471772dfce5 215 strcpy(txBuffer, "Pull_4");
shibulal 1:0471772dfce5 216 }
shibulal 1:0471772dfce5 217 rf_send(txBuffer, strlen(txBuffer)+1);
shibulal 1:0471772dfce5 218 pc.printf("Sent: %s\r\n", txBuffer);
shibulal 1:0471772dfce5 219 //stop conveyor after waiting sufficient time
shibulal 1:0471772dfce5 220 }
shibulal 1:0471772dfce5 221 void move_hv_transition_point_2(){
shibulal 1:0471772dfce5 222 move_forward();
shibulal 1:0471772dfce5 223 while (!(t_freq>=650 && t_freq<=750));
shibulal 1:0471772dfce5 224 stop();
shibulal 1:0471772dfce5 225 }
shibulal 1:0471772dfce5 226 void move_downto_second_level(){
shibulal 1:0471772dfce5 227 move_forward();
shibulal 1:0471772dfce5 228 while (!(t_freq>=950 && t_freq<=1050));
shibulal 1:0471772dfce5 229 stop();
shibulal 1:0471772dfce5 230 }
shibulal 1:0471772dfce5 231 void move_downto_first_level(){
shibulal 1:0471772dfce5 232 move_forward();
shibulal 1:0471772dfce5 233 while (!(t_freq>=1200 && t_freq<=1300));
shibulal 1:0471772dfce5 234 stop();
shibulal 1:0471772dfce5 235 }
shibulal 1:0471772dfce5 236 void move_to_startpoint(){
shibulal 1:0471772dfce5 237 move_forward();
shibulal 1:0471772dfce5 238 while (!(t_freq>=1450 && t_freq<=1550));
shibulal 1:0471772dfce5 239 stop();
shibulal 1:0471772dfce5 240 }
mlab4 0:ddc820578cb0 241
mlab4 0:ddc820578cb0 242 //***************** You can start coding here *****************//
shibulal 1:0471772dfce5 243 void flip() {
shibulal 1:0471772dfce5 244
shibulal 1:0471772dfce5 245 t_period = t1.read_us();
shibulal 1:0471772dfce5 246 t_freq = (1/(float)t_period)*1000000;
shibulal 1:0471772dfce5 247 t1.reset();
shibulal 1:0471772dfce5 248 }
mlab4 0:ddc820578cb0 249 int main (void)
mlab4 0:ddc820578cb0 250 {
shibulal 1:0471772dfce5 251 in.mode(PullDown);
shibulal 1:0471772dfce5 252 in.rise(&flip);
shibulal 1:0471772dfce5 253 char instruction[128];
mlab4 0:ddc820578cb0 254 uint8_t channel = 2;
mlab4 0:ddc820578cb0 255
mlab4 0:ddc820578cb0 256 //Set the Channel. 0 is default, 15 is max
mlab4 0:ddc820578cb0 257 mrf.SetChannel(channel);
mlab4 0:ddc820578cb0 258
mlab4 0:ddc820578cb0 259 //Start the timer
mlab4 0:ddc820578cb0 260 timer.start();
mlab4 0:ddc820578cb0 261
mlab4 0:ddc820578cb0 262 while(true) {
shibulal 1:0471772dfce5 263 while(true){
shibulal 1:0471772dfce5 264 rxLen = rf_receive(rxBuffer, 128);
shibulal 1:0471772dfce5 265 if (rxLen>0){
shibulal 1:0471772dfce5 266 pc.printf("Received: %s\r\n", rxBuffer);
shibulal 1:0471772dfce5 267 if((!strcmp("Park_1", rxBuffer))||(!strcmp("Park_2", rxBuffer))||(!strcmp("Park_3", rxBuffer))||(!strcmp("Park_4", rxBuffer))||(!strcmp("Retrieve_1", rxBuffer))||(!strcmp("Retrieve_2", rxBuffer))||(!strcmp("Retrieve_3", rxBuffer))||(!strcmp("Retrieve_4", rxBuffer))) {
shibulal 1:0471772dfce5 268 pc.printf("Instruction Received: %s\r\n", rxBuffer);
shibulal 1:0471772dfce5 269 strcpy(instruction,rxBuffer);
shibulal 1:0471772dfce5 270 break;
shibulal 1:0471772dfce5 271 }
shibulal 1:0471772dfce5 272 }
shibulal 1:0471772dfce5 273 }
shibulal 1:0471772dfce5 274 move_hv_transition_point_1();
shibulal 1:0471772dfce5 275 close_gate1();
shibulal 1:0471772dfce5 276 if ((!strcmp("Park_1",instruction))||(!strcmp("Retrieve_1",instruction))){
shibulal 1:0471772dfce5 277 move_upto_first_level();
shibulal 1:0471772dfce5 278 if (!strcmp("Park_1",instruction)){
shibulal 1:0471772dfce5 279 push(1);
shibulal 1:0471772dfce5 280 }
shibulal 1:0471772dfce5 281 else{
shibulal 1:0471772dfce5 282 pull(1);
shibulal 1:0471772dfce5 283 }
shibulal 1:0471772dfce5 284 }
shibulal 1:0471772dfce5 285 else if ((!strcmp("Park_2",instruction))||(!strcmp("Retrieve_2",instruction))){
shibulal 1:0471772dfce5 286 move_upto_second_level();
shibulal 1:0471772dfce5 287 if (!strcmp("Park_2",instruction)){
shibulal 1:0471772dfce5 288 push(2);
shibulal 1:0471772dfce5 289 }
shibulal 1:0471772dfce5 290 else{
shibulal 1:0471772dfce5 291 pull(2);
shibulal 1:0471772dfce5 292 }
shibulal 1:0471772dfce5 293 }
shibulal 1:0471772dfce5 294 move_hv_transition_point_2();
shibulal 1:0471772dfce5 295 open_gate1();
shibulal 1:0471772dfce5 296 open_flap();
shibulal 1:0471772dfce5 297 close_gate2();
shibulal 1:0471772dfce5 298 if ((!strcmp("Park_3",instruction))||(!strcmp("Retrieve_3",instruction))){
shibulal 1:0471772dfce5 299 move_downto_second_level();
shibulal 1:0471772dfce5 300 if (!strcmp("Park_3",instruction)){
shibulal 1:0471772dfce5 301 push(3);
shibulal 1:0471772dfce5 302 }
shibulal 1:0471772dfce5 303 else{
shibulal 1:0471772dfce5 304 pull(3);
shibulal 1:0471772dfce5 305 }
shibulal 1:0471772dfce5 306 }
shibulal 1:0471772dfce5 307 else if ((!strcmp("Park_4",instruction))||(!strcmp("Retrieve_4",instruction))){
shibulal 1:0471772dfce5 308 move_downto_first_level();
shibulal 1:0471772dfce5 309 if (!strcmp("Park_4",instruction)){
shibulal 1:0471772dfce5 310 push(4);
shibulal 1:0471772dfce5 311 }
shibulal 1:0471772dfce5 312 else{
shibulal 1:0471772dfce5 313 pull(4);
shibulal 1:0471772dfce5 314 }
shibulal 1:0471772dfce5 315 }
shibulal 1:0471772dfce5 316 move_to_startpoint();
shibulal 1:0471772dfce5 317 close_flap();
shibulal 1:0471772dfce5 318 open_gate2();
mlab4 0:ddc820578cb0 319
shibulal 1:0471772dfce5 320
shibulal 1:0471772dfce5 321
mlab4 0:ddc820578cb0 322 }
shibulal 1:0471772dfce5 323
shibulal 1:0471772dfce5 324
shibulal 1:0471772dfce5 325
shibulal 1:0471772dfce5 326 }
shibulal 1:0471772dfce5 327
shibulal 1:0471772dfce5 328