felipe manrique
/
openwrt_serial
open wrt router linked at FRDM board to control a robot
main.cpp@0:69891c7752df, 2014-04-01 (annotated)
- Committer:
- felipeM
- Date:
- Tue Apr 01 02:52:18 2014 +0000
- Revision:
- 0:69891c7752df
finally worked :)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
felipeM | 0:69891c7752df | 1 | /* |
felipeM | 0:69891c7752df | 2 | |<--Temperature/Humidity sensor |
felipeM | 0:69891c7752df | 3 | ----->|<--Movemtent sensor |
felipeM | 0:69891c7752df | 4 | | |-->HBRIDGE-->MOTORS |
felipeM | 0:69891c7752df | 5 | v |
felipeM | 0:69891c7752df | 6 | *FRDM board | |
felipeM | 0:69891c7752df | 7 | *Webcam c270 | |
felipeM | 0:69891c7752df | 8 | *Serial_USB | |
felipeM | 0:69891c7752df | 9 | or sensors | |
felipeM | 0:69891c7752df | 10 | *usb memory | |
felipeM | 0:69891c7752df | 11 | *cellphone |<--->USB_HUB <--->MR3020 <---->INTERNET<----->USER |
felipeM | 0:69891c7752df | 12 | . (openwrt) |
felipeM | 0:69891c7752df | 13 | . 8080 server |
felipeM | 0:69891c7752df | 14 | . |
felipeM | 0:69891c7752df | 15 | -------------------------------------------------------------- |
felipeM | 0:69891c7752df | 16 | All supported in a 5V 2200mA battery pack |
felipeM | 0:69891c7752df | 17 | | |
felipeM | 0:69891c7752df | 18 | inalambric charger |
felipeM | 0:69891c7752df | 19 | */ |
felipeM | 0:69891c7752df | 20 | #include "mbed.h" |
felipeM | 0:69891c7752df | 21 | ////////// serial variables |
felipeM | 0:69891c7752df | 22 | bool Rxenable = false; |
felipeM | 0:69891c7752df | 23 | char inputString []="00000"; //a string to hold incoming data |
felipeM | 0:69891c7752df | 24 | bool stringComplete = false; //whether the string is complete |
felipeM | 0:69891c7752df | 25 | int i_pnt =0; |
felipeM | 0:69891c7752df | 26 | bool DEBUG =false; |
felipeM | 0:69891c7752df | 27 | Serial pc(USBTX, USBRX); |
felipeM | 0:69891c7752df | 28 | //TX RX pins, see https://mbed.org/handbook/mbed-FRDM-KL25Z |
felipeM | 0:69891c7752df | 29 | Serial uart(PTC4, PTC3); |
felipeM | 0:69891c7752df | 30 | //to test is you can use a hard bridge (piece of cable) between PTC4 and PTC3 open a terminal and write something :) |
felipeM | 0:69891c7752df | 31 | //you can integrate this to improve some interesting aplicattions like this: |
felipeM | 0:69891c7752df | 32 | //http://www.instructables.com/id/Temperature-sensor--weatherstation/http://www.instructables.com/id/Temperature-sensor--weatherstation/ |
felipeM | 0:69891c7752df | 33 | DigitalOut LED1_P(LED1); |
felipeM | 0:69891c7752df | 34 | DigitalOut LED2_P(LED2); |
felipeM | 0:69891c7752df | 35 | DigitalOut LED3_P(LED3); |
felipeM | 0:69891c7752df | 36 | /* |
felipeM | 0:69891c7752df | 37 | SerialEvent occurs whenever a new data comes in the |
felipeM | 0:69891c7752df | 38 | hardware serial RX. This routine is run between each |
felipeM | 0:69891c7752df | 39 | time loop() runs, so using delay inside loop can delay |
felipeM | 0:69891c7752df | 40 | response. Multiple bytes of data may be available. |
felipeM | 0:69891c7752df | 41 | */ |
felipeM | 0:69891c7752df | 42 | void serialEvent() { |
felipeM | 0:69891c7752df | 43 | if (pc.readable()) |
felipeM | 0:69891c7752df | 44 | { |
felipeM | 0:69891c7752df | 45 | if (DEBUG) pc.printf("pc.read\n"); |
felipeM | 0:69891c7752df | 46 | char inChar = (char)pc.getc(); // get the new byte: |
felipeM | 0:69891c7752df | 47 | if (inChar == '@' && Rxenable==false) |
felipeM | 0:69891c7752df | 48 | { // Can start recording after @ symbol |
felipeM | 0:69891c7752df | 49 | if (DEBUG) pc.printf("char@received\n"); |
felipeM | 0:69891c7752df | 50 | Rxenable = true; |
felipeM | 0:69891c7752df | 51 | } |
felipeM | 0:69891c7752df | 52 | if (Rxenable==true) |
felipeM | 0:69891c7752df | 53 | { |
felipeM | 0:69891c7752df | 54 | i_pnt=i_pnt+1; |
felipeM | 0:69891c7752df | 55 | inputString[i_pnt] = inChar; // add it to the inputString: |
felipeM | 0:69891c7752df | 56 | if (DEBUG) pc.printf("pnt %i\t char received %c\n",i_pnt,inChar); |
felipeM | 0:69891c7752df | 57 | // if the incoming character:q is a carriage return, set a flag |
felipeM | 0:69891c7752df | 58 | // so the main loop: can do something about it: |
felipeM | 0:69891c7752df | 59 | if (inChar == '\r') |
felipeM | 0:69891c7752df | 60 | { |
felipeM | 0:69891c7752df | 61 | stringComplete = true; |
felipeM | 0:69891c7752df | 62 | Rxenable = false; |
felipeM | 0:69891c7752df | 63 | if (DEBUG) pc.printf("inchar r\n"); |
felipeM | 0:69891c7752df | 64 | if (DEBUG) printf("input string%s\n",inputString); |
felipeM | 0:69891c7752df | 65 | i_pnt=0; |
felipeM | 0:69891c7752df | 66 | } |
felipeM | 0:69891c7752df | 67 | } |
felipeM | 0:69891c7752df | 68 | } |
felipeM | 0:69891c7752df | 69 | } |
felipeM | 0:69891c7752df | 70 | |
felipeM | 0:69891c7752df | 71 | void process_command() |
felipeM | 0:69891c7752df | 72 | { |
felipeM | 0:69891c7752df | 73 | char command=inputString[2]; |
felipeM | 0:69891c7752df | 74 | if (DEBUG) pc.printf("command %c\n",command); |
felipeM | 0:69891c7752df | 75 | switch (command) { |
felipeM | 0:69891c7752df | 76 | case '1': |
felipeM | 0:69891c7752df | 77 | //car_forward(); |
felipeM | 0:69891c7752df | 78 | LED1_P=1; |
felipeM | 0:69891c7752df | 79 | LED2_P=0; |
felipeM | 0:69891c7752df | 80 | LED3_P=0; |
felipeM | 0:69891c7752df | 81 | if (DEBUG) pc.printf("case1"); |
felipeM | 0:69891c7752df | 82 | // stringComplete=false; |
felipeM | 0:69891c7752df | 83 | break; |
felipeM | 0:69891c7752df | 84 | case '2': |
felipeM | 0:69891c7752df | 85 | LED1_P=0; |
felipeM | 0:69891c7752df | 86 | LED2_P=1; |
felipeM | 0:69891c7752df | 87 | LED3_P=0; |
felipeM | 0:69891c7752df | 88 | if (DEBUG) pc.printf("case2"); |
felipeM | 0:69891c7752df | 89 | // stringComplete=false; |
felipeM | 0:69891c7752df | 90 | //car_headLight(); |
felipeM | 0:69891c7752df | 91 | break; |
felipeM | 0:69891c7752df | 92 | case '3': |
felipeM | 0:69891c7752df | 93 | LED1_P=1; |
felipeM | 0:69891c7752df | 94 | LED2_P=1; |
felipeM | 0:69891c7752df | 95 | LED3_P=0; |
felipeM | 0:69891c7752df | 96 | if (DEBUG) pc.printf("case3"); |
felipeM | 0:69891c7752df | 97 | // stringComplete=false; |
felipeM | 0:69891c7752df | 98 | //car_headLight(); |
felipeM | 0:69891c7752df | 99 | break; |
felipeM | 0:69891c7752df | 100 | case '4': |
felipeM | 0:69891c7752df | 101 | LED1_P=0; |
felipeM | 0:69891c7752df | 102 | LED2_P=0; |
felipeM | 0:69891c7752df | 103 | LED3_P=1; |
felipeM | 0:69891c7752df | 104 | if (DEBUG) pc.printf("case4"); |
felipeM | 0:69891c7752df | 105 | // stringComplete=false; |
felipeM | 0:69891c7752df | 106 | //car_headLight(); |
felipeM | 0:69891c7752df | 107 | break; |
felipeM | 0:69891c7752df | 108 | case '5': |
felipeM | 0:69891c7752df | 109 | LED1_P=1; |
felipeM | 0:69891c7752df | 110 | LED2_P=0; |
felipeM | 0:69891c7752df | 111 | LED3_P=1; |
felipeM | 0:69891c7752df | 112 | if (DEBUG) pc.printf("case5"); |
felipeM | 0:69891c7752df | 113 | // stringComplete=false; |
felipeM | 0:69891c7752df | 114 | //car_headLight(); |
felipeM | 0:69891c7752df | 115 | break; |
felipeM | 0:69891c7752df | 116 | case '6': |
felipeM | 0:69891c7752df | 117 | LED1_P=0; |
felipeM | 0:69891c7752df | 118 | LED2_P=1; |
felipeM | 0:69891c7752df | 119 | LED3_P=1; |
felipeM | 0:69891c7752df | 120 | if (DEBUG) pc.printf("case6"); |
felipeM | 0:69891c7752df | 121 | // stringComplete=false; |
felipeM | 0:69891c7752df | 122 | //car_headLight(); |
felipeM | 0:69891c7752df | 123 | break; |
felipeM | 0:69891c7752df | 124 | case '7': |
felipeM | 0:69891c7752df | 125 | LED1_P=1; |
felipeM | 0:69891c7752df | 126 | LED2_P=1; |
felipeM | 0:69891c7752df | 127 | LED3_P=1; |
felipeM | 0:69891c7752df | 128 | if (DEBUG) pc.printf("case7"); |
felipeM | 0:69891c7752df | 129 | // stringComplete=false; |
felipeM | 0:69891c7752df | 130 | //car_headLight(); |
felipeM | 0:69891c7752df | 131 | break; |
felipeM | 0:69891c7752df | 132 | case '8': |
felipeM | 0:69891c7752df | 133 | LED1_P=0; |
felipeM | 0:69891c7752df | 134 | LED2_P=1; |
felipeM | 0:69891c7752df | 135 | LED3_P=0; |
felipeM | 0:69891c7752df | 136 | if (DEBUG) pc.printf("case8"); |
felipeM | 0:69891c7752df | 137 | // stringComplete=false; |
felipeM | 0:69891c7752df | 138 | //car_headLight(); |
felipeM | 0:69891c7752df | 139 | break; |
felipeM | 0:69891c7752df | 140 | case 'S': |
felipeM | 0:69891c7752df | 141 | LED1_P=1; |
felipeM | 0:69891c7752df | 142 | LED2_P=0; |
felipeM | 0:69891c7752df | 143 | LED3_P=1; |
felipeM | 0:69891c7752df | 144 | if (DEBUG) pc.printf("caseS"); |
felipeM | 0:69891c7752df | 145 | // stringComplete=false; |
felipeM | 0:69891c7752df | 146 | //car_headLight(); |
felipeM | 0:69891c7752df | 147 | break; |
felipeM | 0:69891c7752df | 148 | } |
felipeM | 0:69891c7752df | 149 | } |
felipeM | 0:69891c7752df | 150 | |
felipeM | 0:69891c7752df | 151 | int main() { |
felipeM | 0:69891c7752df | 152 | pc.printf("Openwrt-car wifi application\n"); |
felipeM | 0:69891c7752df | 153 | pc.baud(9600); |
felipeM | 0:69891c7752df | 154 | wait(0.001); |
felipeM | 0:69891c7752df | 155 | LED1_P=1; |
felipeM | 0:69891c7752df | 156 | LED2_P=1; |
felipeM | 0:69891c7752df | 157 | LED3_P=1; |
felipeM | 0:69891c7752df | 158 | while(1) { |
felipeM | 0:69891c7752df | 159 | serialEvent(); |
felipeM | 0:69891c7752df | 160 | if (stringComplete) { |
felipeM | 0:69891c7752df | 161 | process_command(); |
felipeM | 0:69891c7752df | 162 | //inputString = "00000"; //clear the string: |
felipeM | 0:69891c7752df | 163 | stringComplete = false; |
felipeM | 0:69891c7752df | 164 | } |
felipeM | 0:69891c7752df | 165 | } |
felipeM | 0:69891c7752df | 166 | } |