Zac Dannelly
/
testGet
code for interface on mbed coordinator
main.cpp@0:a0bf1c99732b, 2015-06-12 (annotated)
- Committer:
- dannellyz
- Date:
- Fri Jun 12 14:58:34 2015 +0000
- Revision:
- 0:a0bf1c99732b
code for interface on mbed coordinator;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dannellyz | 0:a0bf1c99732b | 1 | // Example of parsing the serial string from the fullAppBaordSend |
dannellyz | 0:a0bf1c99732b | 2 | /* Code derived from the work of Joe Bradshaw: |
dannellyz | 0:a0bf1c99732b | 3 | http://developer.mbed.org/users/jebradshaw/ */ |
dannellyz | 0:a0bf1c99732b | 4 | /******************Declaration of necessary libraries and objects************************/ |
dannellyz | 0:a0bf1c99732b | 5 | #include "mbed.h" |
dannellyz | 0:a0bf1c99732b | 6 | #include "C12832_lcd.h" // Include for LCD code |
dannellyz | 0:a0bf1c99732b | 7 | /******************Declaration of necessary variables***********************************/ |
dannellyz | 0:a0bf1c99732b | 8 | DigitalOut myled(LED1); //For LED |
dannellyz | 0:a0bf1c99732b | 9 | Serial pc(USBTX, USBRX); //tx, rx via USB connection |
dannellyz | 0:a0bf1c99732b | 10 | Serial xbee(p9, p10); //tx, rx via Xbee socket |
dannellyz | 0:a0bf1c99732b | 11 | C12832_LCD lcd; // Decalre LCD screen |
dannellyz | 0:a0bf1c99732b | 12 | LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
dannellyz | 0:a0bf1c99732b | 13 | float totalTime = 0.0; //Total time used to calulate average wait time |
dannellyz | 0:a0bf1c99732b | 14 | float avgTime = 0.0; //Average time when error detected |
dannellyz | 0:a0bf1c99732b | 15 | int errors = 0; //Number of errors |
dannellyz | 0:a0bf1c99732b | 16 | int total = 0; //Total number of transmissisons |
dannellyz | 0:a0bf1c99732b | 17 | float pctErr = 0.0; //Percent of transmissions in error |
dannellyz | 0:a0bf1c99732b | 18 | void keepAverage(float newNum); //Prototype for funciton to make calculations |
dannellyz | 0:a0bf1c99732b | 19 | |
dannellyz | 0:a0bf1c99732b | 20 | int main() |
dannellyz | 0:a0bf1c99732b | 21 | { |
dannellyz | 0:a0bf1c99732b | 22 | char abstr[25]; //used to store the whole app board string |
dannellyz | 0:a0bf1c99732b | 23 | int joystick; //int value correspponding to the joystick bus |
dannellyz | 0:a0bf1c99732b | 24 | float Lpot; //float correspionding to left potentiometer |
dannellyz | 0:a0bf1c99732b | 25 | float Rpot; //float correspionding to right potentiometer |
dannellyz | 0:a0bf1c99732b | 26 | int id; |
dannellyz | 0:a0bf1c99732b | 27 | pc.baud(9600); //crank up the PC baudrate (USB) to avoid latency between data acquisition |
dannellyz | 0:a0bf1c99732b | 28 | //xbee.baud(9600); |
dannellyz | 0:a0bf1c99732b | 29 | //Setup LCD screen |
dannellyz | 0:a0bf1c99732b | 30 | lcd.cls(); |
dannellyz | 0:a0bf1c99732b | 31 | lcd.locate(0,1); |
dannellyz | 0:a0bf1c99732b | 32 | //Set up timers for transmission information |
dannellyz | 0:a0bf1c99732b | 33 | Timer t1; |
dannellyz | 0:a0bf1c99732b | 34 | Timer t2; |
dannellyz | 0:a0bf1c99732b | 35 | //Start timers |
dannellyz | 0:a0bf1c99732b | 36 | t1.start(); |
dannellyz | 0:a0bf1c99732b | 37 | t2.start(); |
dannellyz | 0:a0bf1c99732b | 38 | while(1) { |
dannellyz | 0:a0bf1c99732b | 39 | while(xbee.readable()) //clear out the remaining characters in the buffer |
dannellyz | 0:a0bf1c99732b | 40 | char c = xbee.getc(); |
dannellyz | 0:a0bf1c99732b | 41 | //read the serial string from the xbee (starts with '$', ends with \r\n |
dannellyz | 0:a0bf1c99732b | 42 | xbee.scanf("$%s\r\n", &abstr); |
dannellyz | 0:a0bf1c99732b | 43 | //Verify that the string has the APPBOARD header |
dannellyz | 0:a0bf1c99732b | 44 | if(strncmp(abstr, "APPBOARD", 9)) { |
dannellyz | 0:a0bf1c99732b | 45 | //Verify the tring was able to be parsed |
dannellyz | 0:a0bf1c99732b | 46 | if (sscanf(abstr, "APPBOARD,%f,%f,%d,%d",&Lpot,&Rpot,&joystick,&id) >= 1) { |
dannellyz | 0:a0bf1c99732b | 47 | //Check which input the transmission came from an act accordingly |
dannellyz | 0:a0bf1c99732b | 48 | if (id==0) { |
dannellyz | 0:a0bf1c99732b | 49 | //Read time and report how long it has been since node was pooled |
dannellyz | 0:a0bf1c99732b | 50 | if(t2.read() > 1) { |
dannellyz | 0:a0bf1c99732b | 51 | //If time is more than 1 second consider it an error |
dannellyz | 0:a0bf1c99732b | 52 | //Run the analysis program |
dannellyz | 0:a0bf1c99732b | 53 | keepAverage(t2.read()); |
dannellyz | 0:a0bf1c99732b | 54 | } |
dannellyz | 0:a0bf1c99732b | 55 | //Reset time |
dannellyz | 0:a0bf1c99732b | 56 | t2.reset(); |
dannellyz | 0:a0bf1c99732b | 57 | //Print information to LCD screen |
dannellyz | 0:a0bf1c99732b | 58 | lcd.locate(0,1); |
dannellyz | 0:a0bf1c99732b | 59 | lcd.printf("M%d: L:%.02f R:%.02f J:%d %.02f\r\n",id,Lpot,Rpot,joystick,t1.read()); |
dannellyz | 0:a0bf1c99732b | 60 | } |
dannellyz | 0:a0bf1c99732b | 61 | if (id==1) { |
dannellyz | 0:a0bf1c99732b | 62 | if(t1.read() > 1) { |
dannellyz | 0:a0bf1c99732b | 63 | keepAverage(t1.read()); |
dannellyz | 0:a0bf1c99732b | 64 | } |
dannellyz | 0:a0bf1c99732b | 65 | t1.reset(); |
dannellyz | 0:a0bf1c99732b | 66 | lcd.locate(0,14); |
dannellyz | 0:a0bf1c99732b | 67 | lcd.printf("M%d: L:%.02f R:%.02f J:%d %.02f\r\n",id,Lpot,Rpot,joystick,t2.read()); |
dannellyz | 0:a0bf1c99732b | 68 | } |
dannellyz | 0:a0bf1c99732b | 69 | //Print values to screen |
dannellyz | 0:a0bf1c99732b | 70 | pc.printf("Mbed %d: L-%.02f:L R-%.02f Joystick:%d\r\n",id,Lpot,Rpot,joystick); |
dannellyz | 0:a0bf1c99732b | 71 | } else { |
dannellyz | 0:a0bf1c99732b | 72 | //Incorrect parse |
dannellyz | 0:a0bf1c99732b | 73 | pc.printf("BAD parse %s\r\n", abstr); |
dannellyz | 0:a0bf1c99732b | 74 | } |
dannellyz | 0:a0bf1c99732b | 75 | } |
dannellyz | 0:a0bf1c99732b | 76 | //Increment total |
dannellyz | 0:a0bf1c99732b | 77 | total+=1; |
dannellyz | 0:a0bf1c99732b | 78 | myled = !myled; //toggle LED for activity verification |
dannellyz | 0:a0bf1c99732b | 79 | |
dannellyz | 0:a0bf1c99732b | 80 | }//while(1) |
dannellyz | 0:a0bf1c99732b | 81 | }//main |
dannellyz | 0:a0bf1c99732b | 82 | |
dannellyz | 0:a0bf1c99732b | 83 | void keepAverage(float newNum) |
dannellyz | 0:a0bf1c99732b | 84 | { |
dannellyz | 0:a0bf1c99732b | 85 | Timer t3; |
dannellyz | 0:a0bf1c99732b | 86 | t3.start(); |
dannellyz | 0:a0bf1c99732b | 87 | errors ++; |
dannellyz | 0:a0bf1c99732b | 88 | totalTime += newNum; |
dannellyz | 0:a0bf1c99732b | 89 | avgTime = totalTime/(float)errors; |
dannellyz | 0:a0bf1c99732b | 90 | pctErr = (float)errors/(float)total; |
dannellyz | 0:a0bf1c99732b | 91 | pc.printf("overNum:%f avgErrTime:%.04f totalErrs:%d totalOut:%d pctErr:%.02f\r\n", newNum,avgTime,errors,total,pctErr); |
dannellyz | 0:a0bf1c99732b | 92 | FILE *fp = fopen("/local/out.csv", "a"); // Open "out.txt" on the local file system for writing |
dannellyz | 0:a0bf1c99732b | 93 | fprintf(fp,"%f,%.04f,%d,%d,%.02f,%.02f\r\n", newNum,avgTime,errors,total,pctErr,t3.read()); |
dannellyz | 0:a0bf1c99732b | 94 | fclose(fp); |
dannellyz | 0:a0bf1c99732b | 95 | } |