Changeset b62ab59 in pyramid
- Timestamp:
- 10/15/13 20:38:21 (7 years ago)
- Branches:
- master
- Children:
- f5d44ec
- Parents:
- 85e090e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
firmware/Puzzlebox_Pyramid/Puzzlebox_Pyramid.ino
r85e090e rb62ab59 36 36 #include <PWM.h> //For sending 38kHz Infrared to fly Orbit helicopter 37 37 38 38 39 // ADK 39 #include <Max3421e.h>40 #include <Usb.h>41 #include <AndroidAccessory.h>40 // #include <Max3421e.h> 41 // #include <Usb.h> 42 // #include <AndroidAccessory.h> 42 43 43 44 … … 53 54 #define BUTTON_PIN 4 54 55 55 // 56 #define IR_frequency 38400 56 //PWM IR_frequency (in Hz) required by IR 57 #define IR_frequency 38400 57 58 byte _IR_period = 40; 58 59 59 // Global variables for controlling Orbit helicopter 60 int _throttle_hover=85; 61 60 //G lobal variables for controlling Orbit helicopter 62 61 // variables start with "_" means global variable 63 62 char _throttle=0; //32~127, default 0 … … 67 66 char _command; 68 67 byte _attention_threshold = 67; //treshold for launching Orbit helicopter 68 int _throttle_hover=85; 69 69 70 70 // EEG … … 77 77 boolean modeSerial = false; 78 78 boolean modeADK = false; 79 80 // Android ADK81 // AndroidAccessory acc(82 // "puzzlebox",83 // // "PuzzleboxPyramid",84 // // "Puzzlebox Pyramid",85 // // "1.2",86 // // "http://pyramid.puzzlebox.info",87 // // "0000000012345678"88 // "TriggerRemote",89 // "DemoKit Arduino Board",90 // "1.0",91 // "http://www.android.com",92 // "0000000012345678"93 // );94 95 AndroidAccessory acc(96 "puzzlebox",97 "TriggerRemote",98 "DemoKit Arduino Board",99 "1.0",100 "http://www.android.com",101 "0000000012345678"102 );103 104 byte msg[2];105 byte led;106 79 107 80 // MindWave Mobile Protocol … … 127 100 128 101 // Bluetooth Connection with MindWave Mobile 129 String retSymb = "+RTINQ=";//start symble when there's any return 130 String slaveName = ";MindWave Mobile";// caution that ';'must be included, and make sure the slave name is right. 102 String retSymb = "+RTINQ="; //start symble when there's any return 103 String slaveName = ";MindWave Mobile"; // caution that ';'must be included, and make sure the slave name is right. 104 String connectCmd = "\r\n+CONN="; 131 105 int nameIndex = 0; 132 106 int addrIndex = 0; 133 107 String recvBuf; 134 108 String slaveAddr; 135 String connectCmd = "\r\n+CONN=";136 109 137 110 // RGB Panel … … 163 136 164 137 // Android ADK 165 delay(500);166 acc.powerOn();167 168 138 // delay(500); 139 // acc.powerOn(); 140 141 169 142 ////////Orbit Flight/////////// 170 // initialize all timers except for 0, to save time keeping functions143 // initialize all timers except for 0, to save time keeping functions 171 144 InitTimersSafe(); 172 145 173 // sets the IR_frequency for the specified pin146 // sets the IR_frequency for the specified pin 174 147 bool success = SetPinFrequencySafe(IR, IR_frequency); 175 148 176 // if the pin IR_frequency was set successfully, turn pin 13 on177 if 149 // if the pin IR_frequency was set successfully, turn pin 13 on 150 if(success) { 178 151 Serial.println("INFO: PWM pin frequency set"); 179 152 } … … 182 155 Wire.begin(); // join i2c bus as master (address optional for master) 183 156 184 157 185 158 setStartScreen(); 186 187 188 if (acc.isConnected()) {189 modeADK = true;190 Serial.println("INFO: ADK connection detected, setting modeADK");191 setColorWheel(255, 0, 0); // red192 }193 194 159 160 161 // if (acc.isConnected()) { 162 // modeADK = true; 163 // Serial.println("INFO: ADK connection detected, setting modeADK"); 164 // setColorWheel(255, 0, 0); // red 165 // } 166 167 195 168 //////////Bluetooth/////////// 169 196 170 if (! modeADK) { 197 171 198 delay(500); 199 200 setupBlueToothConnection(); 201 202 //wait 1s and flush the serial buffer 203 172 // delay(500); 173 174 setupBlueToothConnection(); 175 // wait 1s and flush the serial buffer 204 176 delay(1000); 205 Serial.flush(); //delete?177 // Serial.flush(); //delete? 206 178 Serial1.flush(); 207 }208 179 180 } 209 181 210 182 selectInput(); // Determine method for Pyramid control … … 227 199 parseSerialInput(); 228 200 229 else if (modeADK)230 parseADK();231 232 233 // read from button201 // else if (modeADK) 202 // parseADK(); 203 204 205 // Read from button 234 206 if (digitalRead(BUTTON_PIN)){ 235 207 RGB_Panel[11][0]=255; … … 245 217 } 246 218 247 // //Send Flight Command through IR Emitter////219 // Send Flight Command through IR Emitter 248 220 if (millis()-lastLoop>_IR_period) { 249 221 250 if (att_buff>_attention_threshold) {222 if (att_buff>_attention_threshold) { 251 223 252 224 // If detected Attention level is above the target threshold … … 257 229 _throttle=_throttle_hover; 258 230 259 260 // Add Settings for _yaw and _pitch here for custom flight path231 } 232 // Add Settings for _yaw and _pitch here for custom flight path 261 233 262 234 sendCode(formCode(_throttle,_yaw,_pitch)); 263 235 264 Serial1.flush(); 265 266 } 236 if (modeBluetooth) 237 Serial1.flush(); 238 239 // } 267 240 268 241 thisLoop = millis() - lastLoop; … … 275 248 Serial.println(thisLoop); 276 249 #endif 277 250 278 251 lastLoop = millis(); 279 252 280 253 } // if (millis()-lastLoop>_IR_period) 281 254 282 283 } // Main loop 255 }// Main loop 284 256 285 257 … … 290 262 // Determine method for Pyramid control 291 263 292 while ( (! Serial.available()) & (! Serial1.available()) & (! acc.isConnected()) ); 293 294 if (Serial.available()) { 295 modeSerial = true; 296 Serial.println("INFO: Serial input command received, setting modeSerial"); 297 } 298 299 else if (acc.isConnected()) { 300 modeADK = true; 301 Serial1.end(); 302 Serial.println("INFO: ADK connection detected, setting modeADK"); 303 setColorWheel(255, 0, 0); // red 304 } 305 306 else if (Serial1.available()) { 307 modeBluetooth = true; 308 Serial.println("INFO: Bluetooth input received, setting modeBluetooth"); 309 } 264 while ( (! Serial.available()) & (! Serial1.available()) ); 265 266 if (Serial.available()) { 267 modeSerial = true; 268 Serial.println("INFO: Serial input command received, setting modeSerial"); 269 } 270 271 else if (Serial1.available()) { 272 modeBluetooth = true; 273 Serial.println("INFO: Bluetooth input received, setting modeBluetooth"); 274 } 275 276 277 // while ( (! Serial.available()) & (! Serial1.available()) & (! acc.isConnected()) ); 278 // 279 // if (Serial.available()) { 280 // modeSerial = true; 281 // Serial.println("INFO: Serial input command received, setting modeSerial"); 282 // } 283 // 284 // else if (acc.isConnected()) { 285 // modeADK = true; 286 // Serial1.end(); 287 // Serial.println("INFO: ADK connection detected, setting modeADK"); 288 // setColorWheel(255, 0, 0); // red 289 // } 290 // 291 // else if (Serial1.available()) { 292 // modeBluetooth = true; 293 // Serial.println("INFO: Bluetooth input received, setting modeBluetooth"); 294 // } 310 295 311 296 … … 317 302 void parseBluetooth() { 318 303 319 // //MindWave Mobile Protocol////304 // MindWave Mobile Protocol 320 305 321 306 if(ReadOneByte() == 170) { … … 372 357 } // for loop 373 358 374 //update RGB Panel Display 359 360 // Update RGB Panel Display 375 361 376 362 if(bigPacket) { … … 385 371 lastReceivedPacket = millis(); 386 372 Serial.print("\n"); 387 373 388 374 389 375 if (poorQuality == 200) 390 376 setColorWheel(0,255,0); // Green 391 392 393 // Update RGB Panel when received data from MindWave Mobile377 378 379 // Update RGB Panel when received data from MindWave Mobile 394 380 int att, med; 395 381 if(attention==0) att=0; … … 411 397 } 412 398 413 updateFrame(att,med,poorQuality); // update buffer414 sendFrame(0);// send current buffer to RGB Panel with no delay399 updateFrame(att,med,poorQuality); // update buffer 400 sendFrame(0);// send current buffer to RGB Panel with no delay 415 401 416 402 … … 435 421 // Read data from Serial1 UART on ADK 436 422 byte ReadOneByte() { 423 437 424 int ByteRead; 438 425 439 426 while(!Serial1.available()); 427 440 428 ByteRead = Serial1.read(); 441 429 442 430 return ByteRead; 431 443 432 } 444 433 … … 450 439 setColorWheel(0,0,255); // Blue 451 440 452 Serial1.begin(38400); // Set Bluetooth Module BaudRate (for communicating to ADK) to default baud rate 38400441 Serial1.begin(38400); // Set Bluetooth Module BaudRate (for communicating to ADK) to default baud rate 38400 453 442 454 443 // if you want to change baudrate, say to 115200; … … 468 457 if(digitalRead(BUTTON_PIN)==0){ //if needs pair 469 458 //update RGB Panel, Red Indicates it's pairing new headset 470 setColorWheel(0,0,0); // Black471 459 RGB_Panel[5][0]=255; 472 460 RGB_Panel[5][1]=0; … … 532 520 Serial1.print("\r\n+STAUTO=1\r\n");// Permit Auto-connection 533 521 //update bottom RGB LED to blue 534 setColorWheel(0,0,0); // Black535 522 RGB_Panel[5][0]=0; 536 523 RGB_Panel[5][1]=0; … … 647 634 648 635 } // sendFrame 649 650 651 // ################################################################652 653 int calculateMeter(int value) {654 655 int result = 0;656 657 if (value == 0)658 result = 0;659 else if (value <= 20)660 result = 1;661 else if (value <= 40)662 result = 2;663 else if (value <= 60)664 result = 3;665 else if (value <= 80)666 result = 4;667 else if (value <= 100)668 result = 5;669 670 return(result);671 672 } // calculateMeter673 674 675 // ################################################################676 677 void updateFrameADK() {678 679 // update RGB_Panel[][] here to set next Frame you want to send680 // For Example:681 // RGB_Panel[0][0]=0;682 // RGB_Panel[0][1]=0;683 // RGB_Panel[0][2]=255;684 // will update the LED on 1 o'clock position to be blue.685 686 // This following code simulates when you wear a headset.687 // RGB_Panel[5][0]=0;688 // RGB_Panel[5][1]=255;689 // RGB_Panel[5][2]=0;690 691 // int attention=random(0,7); //generates 0~6692 // int meditation=random(0,7);693 694 695 int attention = calculateMeter(eegAttention);696 int meditation = calculateMeter(eegMeditation);697 698 699 for(int i=6; i<=6+attention; i++){700 RGB_Panel[i][0]=255;701 RGB_Panel[i][1]=0;702 RGB_Panel[i][2]=0;703 }704 705 if (meditation==6){706 RGB_Panel[11][0]=0;707 RGB_Panel[11][1]=0;708 RGB_Panel[11][2]=255;709 }710 711 for(int i=4; i>=4-meditation; i--){712 RGB_Panel[i][0]=0;713 RGB_Panel[i][1]=0;714 RGB_Panel[i][2]=255;715 }716 717 if (attention==6)718 RGB_Panel[11][0]=255;719 720 for(int i=6+attention; i<=11; i++){721 RGB_Panel[i][0]=0;722 RGB_Panel[i][1]=0;723 RGB_Panel[i][2]=0;724 }725 726 for(int i=4-meditation; i>=0; i--){727 RGB_Panel[i][0]=0;728 RGB_Panel[i][1]=0;729 RGB_Panel[i][2]=0;730 }731 732 733 } // updateFrame734 636 735 637 … … 751 653 //emitCode generate LOWs between HIGHs as same as the parameter. 752 654 if 753 (BIT) innerCycle(671,732); // 1655 (BIT) innerCycle(671,732); // 1 754 656 else 755 innerCycle(337,402);// 0 657 innerCycle(337,402); // 0 658 659 756 660 } // emitCode 757 661 … … 760 664 761 665 void sendCode(long code) { 762 763 // Serial.println("sendCode(formCode(", _throttle, ",", _yaw, ",", _pitch, "));");764 sendCode(formCode(_throttle,_yaw,_pitch));765 766 666 char n; 767 667 //starting code, with special time period. … … 769 669 innerCycle(730,392); 770 670 771 for (n=28;n>=0;n--) emitCode((code >> n) & 1); //getting bits out from code 671 for (n=28;n>=0;n--) 672 emitCode((code >> n) & 1); //getting bits out from code 772 673 773 674 } // sendCode … … 782 683 783 684 //throttle 784 for (n=6; n>=0; n--) bitWrite(mainCode,17+n,bitRead(throttle,n)); //getting the first 7 digits to mainCode 685 for (n=6; n>=0; n--) 686 bitWrite(mainCode,17+n,bitRead(throttle,n)); //getting the first 7 digits to mainCode 785 687 786 688 bitWrite(mainCode,16,1); //meaning unclear, possibly left button. 787 689 788 690 //channel selection first half 789 if (_channel=='C') bitWrite(mainCode,15,1); 790 else bitWrite(mainCode,15,0); //this digit equals 0 in channel A or B 691 if (_channel=='C') 692 bitWrite(mainCode,15,1); 693 else 694 bitWrite(mainCode,15,0); //this digit equals 0 in channel A or B 791 695 792 696 for (n=6; n>=0; n--) bitWrite(mainCode,8+n,bitRead(yaw,n));//yaw 793 697 794 698 //channel selection second half 795 if (_channel=='A') bitWrite(mainCode,7,1); 796 else bitWrite(mainCode,7,0); //if channel B or C, this digit equals 0; 797 798 bitWrite(mainCode,6,0);// meaning unclear, possibly right button. 799 800 for (n=5; n>=0; n--) bitWrite(mainCode,n,bitRead(pitch,n));//pitch 699 if (_channel=='A') 700 bitWrite(mainCode,7,1); 701 else 702 bitWrite(mainCode,7,0); // if channel B or C, this digit equals 0; 703 704 bitWrite(mainCode,6,0); // meaning unclear, possibly right button. 705 706 for (n=5; n>=0; n--) 707 bitWrite(mainCode,n,bitRead(pitch,n)); //pitch 801 708 802 709 // CheckSum 803 for (n=0; n<=20; n=n+4) checkSum += ((mainCode >> n) & B1111);//sum up every 4 digits in the code 804 805 checkSum=checkSum & B1111; //get the last 4 bits of the sum 806 checkSum=(16-checkSum) & B1111;//16-sum is the formula of this helicopter 710 for (n=0; n<=20; n=n+4) 711 checkSum += ((mainCode >> n) & B1111); // sum up every 4 digits in the code 712 713 checkSum=checkSum & B1111; // get the last 4 bits of the sum 714 checkSum=(16-checkSum) & B1111;// 16-sum is the formula of this helicopter 807 715 808 716 mainCode= (mainCode << 5) | (checkSum << 1); //get the last 4 digit of CheckSum 809 717 810 bitWrite(mainCode,0,1); // finish code811 return mainCode; // mainCode is a 29 bit binary number718 bitWrite(mainCode,0,1); // finish code 719 return mainCode; // mainCode is a 29 bit binary number 812 720 } // formCode 813 721 … … 1036 944 // ################################################################ 1037 945 1038 ///////////Serial Input///////////1039 946 void parseSerialInput() { 1040 947 1041 948 if (Serial.available() > 0) { 1042 949 1043 // if (! modeSerial) 1044 // Serial.println("INFO: Serial input command received, setting modeSerial"); 1045 // modeSerial = true; 950 if (! modeSerial) 951 Serial.println("INFO: Serial input command received, setting modeSerial"); 952 modeSerial = true; 953 954 Serial1.end(); 955 modeBluetooth = false; 956 modeADK = false; 1046 957 1047 958 1048 959 setColorWheel(255, 128, 0); 1049 960 1050 1051 // Serial.print("Serial.available(): ");1052 // Serial.println(Serial.available());1053 961 1054 962 _command = Serial.read(); … … 1059 967 switch (_command) { 1060 968 1061 case 'P': _throttle=_throttle_hover; Serial.print("_throttle="); Serial.println(int(_throttle)); break;969 case 'P': _throttle=_throttle_hover; setColorWheel(255,255,255); Serial.print("_throttle="); Serial.println(int(_throttle)); break; 1062 970 case 'O': _throttle=0; Serial.print("_throttle="); Serial.println(int(_throttle)); break; 1063 971 case 'U': _throttle+=1; Serial.print("_throttle="); Serial.println(int(_throttle)); break; … … 1083 991 1084 992 1085 // ################################################################ 1086 1087 void setOrbitThrottle(int throttle) { 1088 1089 setColorWheel(255,255,0); // Yellow 1090 1091 _throttle=throttle; 1092 setThrottle(); 1093 sendCode(formCode(_throttle,_yaw,_pitch)); 1094 delay(80); // originally 36, 51, 66, 80 1095 } // setOrbitThrottle 1096 1097 1098 // ################################################################ 1099 1100 void parseADK() { 1101 1102 // Android ADK 1103 1104 if (acc.isConnected()) { 1105 1106 int len = acc.read(msg, sizeof(msg), 1); 1107 1108 if (! modeADK) { 1109 Serial.println("INFO: ADK connection detected, setting modeADK"); 1110 modeADK = true; 1111 } 1112 1113 // Action taken by Arduino is tied to the message it receives from Android 1114 1115 if (len > 0) { 1116 1117 setColorWheel(255,255,255); // White 1118 1119 if (msg[0] == 0x1) { 1120 // Reserved 1121 } 1122 else if(msg[0] == 0x2) { 1123 // Reserved 1124 } 1125 else if (msg[0] == 0x3) { 1126 // Reserved 1127 } 1128 1129 // EEG Signal 1130 else if (msg[0] == 0x4) { 1131 eegSignal = 0; 1132 if (msg[1] == 0x1) { 1133 eegSignal = 10; 1134 } else if (msg[1] == 0x2) { 1135 eegSignal = 20; 1136 } else if (msg[1] == 0x3) { 1137 eegSignal = 30; 1138 } else if (msg[1] == 0x4) { 1139 eegSignal = 40; 1140 } else if (msg[1] == 0x5) { 1141 eegSignal = 50; 1142 } else if (msg[1] == 0x6) { 1143 eegSignal = 60; 1144 } else if (msg[1] == 0x7) { 1145 eegSignal = 70; 1146 } else if (msg[1] == 0x8) { 1147 eegSignal = 80; 1148 } else if (msg[1] == 0x9) { 1149 eegSignal = 90; 1150 } else { 1151 eegSignal = 100; 1152 } 1153 } 1154 1155 1156 // EEG Attention 1157 else if (msg[0] == 0x5) { 1158 eegAttention = 0; 1159 if (msg[1] == 0x1) { 1160 eegAttention = 10; 1161 } else if (msg[1] == 0x2) { 1162 eegAttention = 20; 1163 } else if (msg[1] == 0x3) { 1164 eegAttention = 30; 1165 } else if (msg[1] == 0x4) { 1166 eegAttention = 40; 1167 } else if (msg[1] == 0x5) { 1168 eegAttention = 50; 1169 } else if (msg[1] == 0x6) { 1170 eegAttention = 60; 1171 } else if (msg[1] == 0x7) { 1172 eegAttention = 70; 1173 } else if (msg[1] == 0x8) { 1174 eegAttention = 80; 1175 } else if (msg[1] == 0x9) { 1176 eegAttention = 90; 1177 } else { 1178 eegAttention = 100; 1179 } 1180 } 1181 1182 1183 // EEG Meditation 1184 else if (msg[0] == 0x6) { 1185 eegMeditation = 0; 1186 if (msg[1] == 0x1) { 1187 eegMeditation = 10; 1188 } else if (msg[1] == 0x2) { 1189 eegMeditation = 20; 1190 } else if (msg[1] == 0x3) { 1191 eegMeditation = 30; 1192 } else if (msg[1] == 0x4) { 1193 eegMeditation = 40; 1194 } else if (msg[1] == 0x5) { 1195 eegMeditation = 50; 1196 } else if (msg[1] == 0x6) { 1197 eegMeditation = 60; 1198 } else if (msg[1] == 0x7) { 1199 eegMeditation = 70; 1200 } else if (msg[1] == 0x8) { 1201 eegMeditation = 80; 1202 } else if (msg[1] == 0x9) { 1203 eegMeditation = 90; 1204 } else { 1205 eegMeditation = 100; 1206 } 1207 } 1208 1209 1210 // updateFrame(eegAttention, eegMeditation, eegSignal); 1211 updateFrameADK(); 1212 1213 1214 // Orbit Throttle 1215 if (msg[0] == 0x7) { 1216 1217 Serial.println(" (msg[0] == 0x7) "); 1218 1219 int throttle = 0; 1220 1221 if (msg[1] == 0x1) { 1222 throttle = _throttle_hover; 1223 Serial.println(" (msg[1] == 0x1) "); 1224 } 1225 else if (msg[1] == 0x0) 1226 Serial.println(" (msg[1] == 0x0) "); 1227 1228 setOrbitThrottle(throttle); 1229 1230 } else { 1231 1232 // sendFrame(1000); 1233 sendFrame(0); 1234 sendCode(formCode(_throttle,_yaw,_pitch)); 1235 delay(80); //cycle(); 1236 } 1237 1238 setColorWheel(255,0,0); // Red 1239 1240 } // len 1241 else { 1242 // setColorWheel(255, 0, 0); // red 1243 delay(80); //cycle(); 1244 } 1245 } // if acc.isConnected() 1246 1247 1248 } // parseADK 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 993 994 995 996 997 998 999 1000 1001 1002 1003
Note: See TracChangeset
for help on using the changeset viewer.