Changeset 0b675ba in pyramid
- Timestamp:
- 10/10/13 02:49:00 (7 years ago)
- Branches:
- master
- Children:
- 94b751c
- Parents:
- 240e339
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
firmware/Puzzlebox_Pyramid/Puzzlebox_Pyramid.ino
r240e339 r0b675ba 26 26 For more details about the product please check http://puzzlebox.info 27 27 28 Original Author: Azureviolin <www.azureviolin.com>29 30 Last Modified 2013- 07-0428 Original Author: Hao Zhang <hz@puzzlebox.info> 29 30 Last Modified 2013-10-09 31 31 by Steve Castellotti <sc@puzzlebox.info> 32 32 … … 37 37 38 38 39 #define DEBUG_OUTPUT 0//1 for debug39 #define DEBUG_OUTPUT 1 //1 for debug 40 40 41 41 … … 59 59 char _channel='A'; 60 60 char _command; 61 byte _attention_threshold = 6 0; //treshold for launching Orbit helicopter61 byte _attention_threshold = 67; //treshold for launching Orbit helicopter 62 62 63 63 ///////////////MindWave Mobile Protocol/////////// … … 71 71 int payloadLength = 0; 72 72 byte payloadData[64] = { 73 0}; 73 0 74 }; 74 75 byte poorQuality = 0; 75 76 byte attention = 0; … … 94 95 95 96 ////////////RGB Panel////////////// 96 byte RGB_Panel[12][3] ={97 byte RGB_Panel[12][3] = { 97 98 {0,0,0}, 98 99 {0,0,0}, … … 108 109 {0,0,0}, 109 110 }; 110 111 112 // ################################################################113 114 ///////////Serial Input///////////115 void parseInput() {116 117 if (Serial.available() > 0) {118 119 Serial.print("Serial.available(): ");120 Serial.println(Serial.available());121 122 _command = Serial.read();123 124 125 Serial.print("Serial.read(): ");126 Serial.println(_command);127 128 switch (_command)129 {130 case 'P': _throttle=85; Serial.print("_throttle="); Serial.println(int(_throttle)); break;131 case 'O': _throttle=0; Serial.print("_throttle="); Serial.println(int(_throttle)); break;132 case 'U': _throttle+=1; Serial.print("_throttle="); Serial.println(int(_throttle)); break;133 case 'D': _throttle-=1; Serial.print("_throttle="); Serial.println(int(_throttle)); break;134 case 'L': _yaw+=15; Serial.print("_yaw="); Serial.println(int(_yaw)); break;135 case 'R': _yaw-=15; Serial.print("_yaw="); Serial.println(int(_yaw)); break;136 case 'F': _pitch+=5; Serial.print("_pitch="); Serial.println(int(_pitch)); break;137 case 'B': _pitch-=5; Serial.print("_pitch="); Serial.println(int(_pitch)); break;138 case '1': _channel='A'; Serial.println("_channel=A"); break;139 case '2': _channel='B'; Serial.println("_channel=B"); break;140 case '3': _channel='C'; Serial.println("_channel=C"); break;141 case 'p': setPitch(); break;142 case 't': setThrottle(); break;143 case 'y': setYaw(); break;144 case 'x': setThrottle(); break;145 }146 }147 } // parseInput()148 149 150 // ################################################################151 152 void setPitch() {153 154 char inByte=0;155 int a=0;156 int b=0;157 int c=0;158 int newPitch=0;159 160 while (Serial.available() == 0);161 inByte = Serial.read() - '0';162 //Serial.println(inByte);163 a = inByte;164 165 while (Serial.available() == 0);166 inByte = Serial.read() - '0';167 //Serial.println(inByte);168 b = inByte;169 170 while (Serial.available() == 0);171 inByte = Serial.read() - '0';172 //Serial.println(inByte);173 c = inByte;174 175 newPitch = (a * 100) + (b * 10) + c;176 177 if (newPitch < 0)178 newPitch=0;179 180 if (newPitch > 100)181 newPitch=100;182 183 _pitch=newPitch;184 185 Serial.print("_pitch=");186 Serial.println(int(_pitch));187 188 } // setPitch189 190 191 // ################################################################192 193 void setThrottle() {194 195 char inByte=0;196 int a=0;197 int b=0;198 int c=0;199 int newThrottle=0;200 201 while (Serial.available() == 0);202 inByte = Serial.read() - '0';203 //Serial.println(inByte);204 a = inByte;205 206 while (Serial.available() == 0);207 inByte = Serial.read() - '0';208 //Serial.println(inByte);209 b = inByte;210 211 while (Serial.available() == 0);212 inByte = Serial.read() - '0';213 //Serial.println(inByte);214 c = inByte;215 216 newThrottle = (a * 100) + (b * 10) + c;217 218 if (newThrottle < 0)219 newThrottle=0;220 221 if (newThrottle > 100)222 newThrottle=100;223 224 _throttle=newThrottle;225 226 Serial.print("_throttle=");227 Serial.println(int(_throttle));228 229 } // setThrottle230 231 232 // ################################################################233 234 void setYaw() {235 236 char inByte=0;237 int a=0;238 int b=0;239 int c=0;240 int newYaw=0;241 242 while (Serial.available() == 0);243 inByte = Serial.read() - '0';244 //Serial.println(inByte);245 a = inByte;246 247 while (Serial.available() == 0);248 inByte = Serial.read() - '0';249 //Serial.println(inByte);250 b = inByte;251 252 while (Serial.available() == 0);253 inByte = Serial.read() - '0';254 //Serial.println(inByte);255 c = inByte;256 257 newYaw = (a * 100) + (b * 10) + c;258 259 if (newYaw < 0)260 newYaw=0;261 262 if (newYaw > 100)263 newYaw=100;264 265 _yaw=newYaw;266 267 Serial.print("_yaw=");268 Serial.println(int(_yaw));269 270 } // setYaw271 111 272 112 … … 276 116 //SETUP// 277 117 ///////// 278 void setup() { 118 void setup() { 279 119 pinMode(13, OUTPUT); 280 120 Serial.begin(115200); … … 292 132 } 293 133 294 295 134 /////////////RGB//////////////// 296 135 Wire.begin(); // join i2c bus as master (address optional for master) 297 298 136 299 137 //////////Bluetooth/////////// … … 313 151 void loop() { 314 152 315 parseInput(); 316 153 parseBluetooth(); 154 155 // parseSerialInput(); 156 157 ////Send Flight Command through IR Emitter//// 158 if (millis()-lastLoop>_IR_period) { 159 if(att_buff>_attention_threshold) { 160 _throttle=85; 161 162 163 //Add Settings for _yaw and _pitch here for custom flight path 164 165 sendCode(formCode(_throttle,_yaw,_pitch)); 166 167 Serial1.flush(); 168 } 169 170 #if DEBUG_OUTPUT 171 Serial.print(" Attention:"); 172 Serial.print(att_buff); 173 Serial.print(" | Time per loop:"); 174 Serial.println(millis() - lastLoop, DEC); 175 lastLoop = millis(); 176 #endif 177 178 } // if (millis()-lastLoop>_IR_period) 179 180 }// Main loop 181 182 183 // ################################################################ 184 185 void parseBluetooth() { 186 317 187 ////MindWave Mobile Protocol//// 318 if (ReadOneByte() == 170) { 319 if (ReadOneByte() == 170) { 320 188 189 if(ReadOneByte() == 170) { 190 191 if(ReadOneByte() == 170) { 192 321 193 payloadLength = ReadOneByte(); 322 194 if(payloadLength > 169) //Payload length can not be greater than 169 … … 332 204 generatedChecksum = 255 - generatedChecksum; //Take one's compliment of generated checksum 333 205 334 if (checksum == generatedChecksum) {206 if (checksum == generatedChecksum) { 335 207 336 208 poorQuality = 200; … … 364 236 } // for loop 365 237 366 367 // Update RGB Panel Display 238 //update RGB Panel Display 368 239 369 240 if(bigPacket) { … … 401 272 updateFrame(att,med,poorQuality); //update buffer 402 273 sendFrame(0);//send current buffer to RGB Panel with no delay 403 404 405 274 406 }// end if(bigPacket)407 408 409 bigPacket = false; 410 411 }else {275 276 277 } // end if(bigPacket) 278 bigPacket = false; 279 } // if (checksum == generatedChecksum) 280 else { 412 281 #if DEBUG_OUTPUT 413 282 Serial.println("Checksum Error!"); … … 416 285 } // end if else for checksum 417 286 } // end if read 0xAA byte (170) 418 } // end if read 0xAA byte (170) (ReadOneByte() == 170)287 } // end if read 0xAA byte (170) 419 288 420 289 //read from button … … 433 302 434 303 435 ////Send Flight Command through IR Emitter//// 436 if (millis() - lastLoop > _IR_period) { 437 if (att_buff > _attention_threshold) { 438 if (_throttle != 85) { 439 _throttle=85; 440 Serial1.flush(); 441 442 } 443 444 // //Add Settings for _yaw and _pitch here for custom flight path 445 // 446 // sendCode(formCode(_throttle,_yaw,_pitch)); 447 448 // Serial1.flush(); 449 // } else { 450 // _throttle=0; 451 } 452 453 454 455 //Add Settings for _yaw and _pitch here for custom flight path 456 sendCode(formCode(_throttle,_yaw,_pitch)); 457 458 // #if DEBUG_OUTPUT 459 // Serial.print("Time per loop:"); 460 // Serial.print(millis() - lastLoop, DEC); 461 // Serial.print("Attention:"); 462 // Serial.println(att_buff); 463 // lastLoop = millis(); 464 // #endif 465 466 } 467 468 } // loop() 304 } // parseBluetooth 469 305 470 306 … … 473 309 // Read data from Serial1 UART on ADK 474 310 byte ReadOneByte() { 475 476 int ByteRead = 0; 477 478 // while(!Serial1.available()); 479 480 if (Serial1.available() > 0) 481 ByteRead = Serial1.read(); 311 int ByteRead; 312 313 while(!Serial1.available()); 314 ByteRead = Serial1.read(); 482 315 483 316 return ByteRead; … … 488 321 489 322 void setupBlueToothConnection() { 323 490 324 Serial1.begin(38400); //Set Bluetooth Module BaudRate (for communicating to ADK) to default baud rate 38400 491 325 … … 504 338 505 339 506 if(digitalRead(BUTTON_PIN)==0) 340 if(digitalRead(BUTTON_PIN)==0){ //if needs pair 507 341 //update RGB Panel, Red Indicates it's pairing new headset 508 342 RGB_Panel[5][0]=255; … … 514 348 Serial.println("Pyramid is inquiring!"); 515 349 delay(2000); // This delay is required. 516 350 517 351 //find the target slave, hence MindWave Mobile Headset 518 352 … … 520 354 char recvChar; 521 355 while(1) { 522 if(Serial1.available()) {356 if(Serial1.available()) { 523 357 recvChar = Serial1.read(); 524 358 Serial.print(recvChar); … … 526 360 nameIndex = recvBuf.indexOf(slaveName);//get the position of slave name 527 361 //nameIndex -= 1;//decrease the ';' in front of the slave name, to get the position of the end of the slave address 528 if ( nameIndex != -1 ) {362 if ( nameIndex != -1 ) { 529 363 //Serial.print(recvBuf); 530 364 addrIndex = (recvBuf.indexOf(retSymb,(nameIndex - retSymb.length()- 18) ) + retSymb.length());//get the start position of slave address … … 533 367 } 534 368 } 535 } // while369 } 536 370 537 371 Serial.println(); 372 538 373 //form the full connection command 539 374 connectCmd += slaveAddr; … … 547 382 Serial1.print(connectCmd);//send connection command 548 383 recvBuf = ""; 549 while(1) {550 if(Serial1.available()) {384 while(1) { 385 if(Serial1.available()) { 551 386 recvChar = Serial1.read(); 552 387 recvBuf += recvChar; 553 if 388 if(recvBuf.indexOf("CONNECT:OK") != -1) { 554 389 connectOK = 1; 555 390 Serial.println("Connected!"); 556 391 //Serial1.print("Connected!"); 557 392 break; 558 } else if (recvBuf.indexOf("CONNECT:FAIL") != -1){393 } else if (recvBuf.indexOf("CONNECT:FAIL") != -1){ 559 394 Serial.println("Connect again!"); 560 395 break; … … 564 399 } while(0 == connectOK); 565 400 566 } else { //if auto connected 401 } //end if needs pair 402 else { //if auto connected 567 403 Serial1.print("\r\n+STAUTO=1\r\n");// Permit Auto-connection 568 404 //update bottom RGB LED to blue … … 577 413 //If Bluetooth connection is established, top LED blue. 578 414 // Otherwise top LED red. 579 // if(digitalRead(A1)){//D5 for Pyramid Shield, A1 for testing 580 if (digitalRead(5)) {//D5 for Pyramid Shield, A1 for testing 581 415 if(digitalRead(5)){//D5 for Pyramid Shield, A1 for testing 582 416 RGB_Panel[11][0]=0; 583 417 RGB_Panel[11][1]=0; 584 418 RGB_Panel[11][2]=255; 585 419 sendFrame(0); 586 } else { 420 } 421 else{ 587 422 RGB_Panel[11][0]=255; 588 423 RGB_Panel[11][1]=0; … … 590 425 sendFrame(0); 591 426 } 592 593 } // setupBluetoothConnection() 427 428 } 429 // End setupBluetoothConnection 594 430 595 431 … … 608 444 609 445 // if the signal is good enough, light 6:00 LED in green. 610 if (poorQuality <1) {446 if (poorQuality <1) { 611 447 RGB_Panel[5][0]=0; 612 448 RGB_Panel[5][1]=255; … … 623 459 RGB_Panel[11][2]=0; 624 460 } 625 461 626 462 627 463 //light up & dim red LED according to attention level 628 for(int i=6; i<6+attention; i++) {464 for(int i=6; i<6+attention; i++) { 629 465 RGB_Panel[i][0]=255; 630 466 RGB_Panel[i][1]=0; 631 467 RGB_Panel[i][2]=0; 632 468 } 633 for(int i=6+attention; i<11; i++) {469 for(int i=6+attention; i<11; i++) { 634 470 RGB_Panel[i][0]=0; 635 471 RGB_Panel[i][1]=0; … … 638 474 639 475 //light up & dim blue LED according to meditation level 640 for(int i=4; i>4-meditation; i--) {476 for(int i=4; i>4-meditation; i--) { 641 477 RGB_Panel[i][0]=0; 642 478 RGB_Panel[i][1]=0; 643 479 RGB_Panel[i][2]=255; 644 480 } 645 for(int i=4-meditation; i>-1; i--) {481 for(int i=4-meditation; i>-1; i--) { 646 482 RGB_Panel[i][0]=0; 647 483 RGB_Panel[i][1]=0; … … 649 485 } 650 486 651 }// updateFrame()487 }// end updateFrame 652 488 653 489 … … 679 515 Wire.endTransmission(); // stop transmitting 680 516 //delay(delayTime); 681 } // sendFrame() 517 518 } // sendFrame 682 519 683 520 … … 691 528 pwmWrite(IR, OFF); 692 529 delayMicroseconds(offTime); 693 } // innerCycle ()530 } // innerCycle 694 531 695 532 … … 698 535 void emitCode(char BIT) { 699 536 //emitCode generate LOWs between HIGHs as same as the parameter. 700 if (BIT)701 innerCycle(671,732);// 1537 if 538 (BIT) innerCycle(671,732);// 1 702 539 else 703 540 innerCycle(337,402);// 0 704 } // emitCode ()705 706 707 // ################################################################ 708 709 void sendCode(long code) { 541 } // emitCode 542 543 544 // ################################################################ 545 546 void sendCode(long code) { 710 547 char n; 711 548 //starting code, with special time period. … … 713 550 innerCycle(730,392); 714 551 715 for (n=28;n>=0;n--) 716 emitCode((code >> n) & 1); //getting bits out from code 552 for (n=28;n>=0;n--) emitCode((code >> n) & 1); //getting bits out from code 553 717 554 } // sendCode 718 555 … … 721 558 722 559 long formCode(char throttle,char yaw,char pitch) { 723 724 560 char n; 725 561 long mainCode=0; … … 755 591 bitWrite(mainCode,0,1); //finish code 756 592 return mainCode; //mainCode is a 29 bit binary number 757 758 } // formCode() 759 760 761 762 763 764 765 766 593 } // formCode 594 595 596 // ################################################################ 597 598 void setThrottle() { 599 600 char inByte=0; 601 int a=0; 602 int b=0; 603 int c=0; 604 int newThrottle=0; 605 606 while (Serial.available() == 0); 607 inByte = Serial.read() - '0'; 608 a = inByte; 609 610 while (Serial.available() == 0); 611 inByte = Serial.read() - '0'; 612 b = inByte; 613 614 while (Serial.available() == 0); 615 inByte = Serial.read() - '0'; 616 c = inByte; 617 618 newThrottle = (a * 100) + (b * 10) + c; 619 620 if (newThrottle < 0) 621 newThrottle=0; 622 623 if (newThrottle > 100) 624 newThrottle=100; 625 626 _throttle=newThrottle; 627 628 Serial.print("_throttle="); 629 Serial.println(int(_throttle)); 630 631 } // setThrottle 632 633 634 // ################################################################ 635 636 void setPitch() { 637 638 char inByte=0; 639 int a=0; 640 int b=0; 641 int c=0; 642 int newPitch=0; 643 644 while (Serial.available() == 0); 645 inByte = Serial.read() - '0'; 646 //Serial.println(inByte); 647 a = inByte; 648 649 while (Serial.available() == 0); 650 inByte = Serial.read() - '0'; 651 //Serial.println(inByte); 652 b = inByte; 653 654 while (Serial.available() == 0); 655 inByte = Serial.read() - '0'; 656 //Serial.println(inByte); 657 c = inByte; 658 659 newPitch = (a * 100) + (b * 10) + c; 660 661 if (newPitch < 0) 662 newPitch=0; 663 664 if (newPitch > 100) 665 newPitch=100; 666 667 _pitch=newPitch; 668 669 Serial.print("_pitch="); 670 Serial.println(int(_pitch)); 671 672 } // setPitch 673 674 675 // ################################################################ 676 677 //void setThrottle() { 678 // 679 // char inByte=0; 680 // int a=0; 681 // int b=0; 682 // int c=0; 683 // int newThrottle=0; 684 // 685 // while (Serial.available() == 0); 686 // inByte = Serial.read() - '0'; 687 // //Serial.println(inByte); 688 // a = inByte; 689 // 690 // while (Serial.available() == 0); 691 // inByte = Serial.read() - '0'; 692 // //Serial.println(inByte); 693 // b = inByte; 694 // 695 // while (Serial.available() == 0); 696 // inByte = Serial.read() - '0'; 697 // //Serial.println(inByte); 698 // c = inByte; 699 // 700 // newThrottle = (a * 100) + (b * 10) + c; 701 // 702 // if (newThrottle < 0) 703 // newThrottle=0; 704 // 705 // if (newThrottle > 100) 706 // newThrottle=100; 707 // 708 // _throttle=newThrottle; 709 // 710 // Serial.print("_throttle="); 711 // Serial.println(int(_throttle)); 712 // 713 //} // setThrottle 714 715 716 // ################################################################ 717 718 void setYaw() { 719 720 char inByte=0; 721 int a=0; 722 int b=0; 723 int c=0; 724 int newYaw=0; 725 726 while (Serial.available() == 0); 727 inByte = Serial.read() - '0'; 728 //Serial.println(inByte); 729 a = inByte; 730 731 while (Serial.available() == 0); 732 inByte = Serial.read() - '0'; 733 //Serial.println(inByte); 734 b = inByte; 735 736 while (Serial.available() == 0); 737 inByte = Serial.read() - '0'; 738 //Serial.println(inByte); 739 c = inByte; 740 741 newYaw = (a * 100) + (b * 10) + c; 742 743 if (newYaw < 0) 744 newYaw=0; 745 746 if (newYaw > 100) 747 newYaw=100; 748 749 _yaw=newYaw; 750 751 Serial.print("_yaw="); 752 Serial.println(int(_yaw)); 753 754 } // setYaw 755 756 757 // ################################################################ 758 759 ///////////Serial Input/////////// 760 void parseSerialInput() { 761 762 if (Serial.available() > 0) { 763 764 // Serial.print("Serial.available(): "); 765 // Serial.println(Serial.available()); 766 767 _command = Serial.read(); 768 769 Serial.print("Serial.read(): "); 770 Serial.println(_command); 771 772 switch (_command) { 773 774 case 'P': _throttle=85; Serial.print("_throttle="); Serial.println(int(_throttle)); break; 775 case 'O': _throttle=0; Serial.print("_throttle="); Serial.println(int(_throttle)); break; 776 case 'U': _throttle+=1; Serial.print("_throttle="); Serial.println(int(_throttle)); break; 777 case 'D': _throttle-=1; Serial.print("_throttle="); Serial.println(int(_throttle)); break; 778 case 'L': _yaw+=15; Serial.print("_yaw="); Serial.println(int(_yaw)); break; 779 case 'R': _yaw-=15; Serial.print("_yaw="); Serial.println(int(_yaw)); break; 780 case 'F': _pitch+=5; Serial.print("_pitch="); Serial.println(int(_pitch)); break; 781 case 'B': _pitch-=5; Serial.print("_pitch="); Serial.println(int(_pitch)); break; 782 case '1': _channel='A'; Serial.println("_channel=A"); break; 783 case '2': _channel='B'; Serial.println("_channel=B"); break; 784 case '3': _channel='C'; Serial.println("_channel=C"); break; 785 case 'p': setPitch(); break; 786 case 't': setThrottle(); break; 787 case 'y': setYaw(); break; 788 case 'x': setThrottle(); break; 789 } 790 } 791 792 } // parseSerialInput() 793
Note: See TracChangeset
for help on using the changeset viewer.