ryo seki
/
serialSend5
a
Revision 1:ed00e9c4ba6e, committed 2013-04-30
- Comitter:
- akudohune
- Date:
- Tue Apr 30 09:06:57 2013 +0000
- Parent:
- 0:62ac378df546
- Commit message:
- ooo
Changed in this revision
diff -r 62ac378df546 -r ed00e9c4ba6e IR.cpp --- a/IR.cpp Fri Apr 19 09:14:08 2013 +0000 +++ b/IR.cpp Tue Apr 30 09:06:57 2013 +0000 @@ -6,7 +6,7 @@ int direction = 0; int Distance = 0; -int IR_found; +int IR_found = 0; void IR_Position(){
diff -r 62ac378df546 -r ed00e9c4ba6e main.cpp --- a/main.cpp Fri Apr 19 09:14:08 2013 +0000 +++ b/main.cpp Tue Apr 30 09:06:57 2013 +0000 @@ -2,12 +2,32 @@ #include "TextLCD.h" #include "main.h" +int ave5point(int source) +{ + static int tmp[5] = {0}; + static int sum = 0; + + sum -= tmp[4]; + sum += source; + tmp[4] = tmp[3]; + tmp[3] = tmp[2]; + tmp[2] = tmp[1]; + tmp[1] = tmp[0]; + tmp[0] = source; + + for(uint8_t i = 0; i < 5; i++){ + if((tmp[i] > 180) || (tmp[i] < 10)){ + return 0xFF; + } + } + + return (int)(sum / 5.0); -int a; -int xbee; +} void init() { + device.baud(BAUD_RATE); device.printf("START"); device2.printf("START"); device.attach(&dev_tx,Serial::TxIrq); @@ -20,25 +40,47 @@ lcd.cls(); } + int main() { + uint8_t flag = 0,state = 0; + init(); for(;;){ - //mbedleds = 0; + if((!Button) && (!flag)){ + wait(1); + flag = 1; + state = 1; + }else if((!Button) && (flag)){ + wait(1); + flag = 0; + state = 0; + } + mbedleds = 0; Ultrasonic(); - //lcd.cls(); + IR_Position(); + AveDistance = ave5point(Distance); - IR_Position(); lcd.cls(); - lcd.locate(0,0); - lcd.printf("R:%d,F:%d",(int)ultrasonicValue[1],(int)ultrasonicValue[0]); - lcd.locate(0,1); - lcd.printf("L:%d,B:%d",(int)ultrasonicValue[3],(int)ultrasonicValue[2]); - + if(state){ + lcd.locate(0,0); + lcd.printf("R:%d,F:%d",(int)ultrasonicValue[1],(int)ultrasonicValue[0]); + lcd.locate(0,1); + lcd.printf("L:%d,B:%d",(int)ultrasonicValue[3],(int)ultrasonicValue[2]); + }else{ + lcd.locate(0,0); + lcd.printf("%d,%d",direction,data[2]);//data[2] + lcd.locate(0,1); + lcd.printf("%d,%d",AveDistance,data2[0]); + } + /* + pc.printf("R:%d,\tF:%d\t",(int)ultrasonicValue[1],(int)ultrasonicValue[0]); + pc.printf("L:%d,\tB:%d\n",(int)ultrasonicValue[3],(int)ultrasonicValue[2]); + */ /* lcd.locate(0,0); lcd.printf("%d",(data[0] << 8) + data[1]); @@ -47,9 +89,9 @@ */ /* lcd.locate(0,0); - lcd.printf("%d,%d",direction,data[2]); + lcd.printf("%d,%d",direction,data[3]);//data[2] lcd.locate(0,1); - lcd.printf("%d",Distance); + lcd.printf("%d",AveDistance); */ /* lcd.locate(0,0);
diff -r 62ac378df546 -r ed00e9c4ba6e main.h --- a/main.h Fri Apr 19 09:14:08 2013 +0000 +++ b/main.h Tue Apr 30 09:06:57 2013 +0000 @@ -6,9 +6,11 @@ BusOut mbedleds(LED4,LED3,LED2,LED1); Serial device2(p28, p27); // tx, rx Serial device(p13, p14); // tx, rx -Serial pc(USBTX, USBRX); // tx, rx +//Serial pc(USBTX, USBRX); // tx, rx TextLCD lcd(p5, p6, p10, p7, p9, p8); +int AveDistance; + extern uint16_t ultrasonicVal[4]; extern double ultrasonicValue[4]; extern int direction; @@ -21,5 +23,7 @@ extern void dev2_rx(void); extern void dev2_tx(void); -extern int data[4]; -extern int data2[5]; +extern uint8_t data[4]; +extern uint8_t data2[5]; + +#define BAUD_RATE 19200 \ No newline at end of file
diff -r 62ac378df546 -r ed00e9c4ba6e uart1.cpp --- a/uart1.cpp Fri Apr 19 09:14:08 2013 +0000 +++ b/uart1.cpp Tue Apr 30 09:06:57 2013 +0000 @@ -3,39 +3,43 @@ #include "uart1.h" extern Serial device; -extern BusOut mbedleds; +extern BusOut mbedleds; +//extern Serial pc; extern int direction; extern int Distance; extern int IR_found; +extern int AveDistance; extern uint16_t ultrasonicVal[4]; -extern int xbee; -int data[4]; +uint8_t xbee; +uint8_t data[4]; + +extern uint8_t data2[5]; void dev_rx() { - static uint8_t count2; + static uint8_t count2 = 0; static uint8_t RecData[RECEIVE_DATA_NUM]; + + RecData[count2] = device.getc(); - //mbedleds = 2; + if(RecData[KEY2] == KEYCODE2){ - if(RecData[KEY2] == KEYCODE2){ count2++; }else{ count2 = 0; } if(count2 >= RECEIVE_DATA_NUM){ if(RecData[CHECK2] == CHECKCODE2){ - mbedleds = 1; + //pc.printf("a"); + //mbedleds = 15; data[0] = RecData[DATA1]; data[1] = RecData[DATA2]; data[2] = RecData[DATA3]; data[3] = RecData[DATA4]; - if(data[0] == 25){ - mbedleds = 15; - } + //pc.printf("%d\n",data[2]); } count2 = 0; } @@ -43,17 +47,14 @@ void dev_tx() { - - static uint8_t count; + static uint8_t count = 0; static uint8_t SendData0[SEND_DATA_NUM]; - //mbedleds = 1; - xbee = IR_found; - if(count >= SEND_DATA_NUM){ + xbee = data2[0]; SendData0[KEY] = KEYCODE; SendData0[DIRECTIONN] = direction; - SendData0[DISTANCE] = Distance; + SendData0[DISTANCE] = AveDistance; SendData0[SONIC1_1] = ultrasonicVal[0]; SendData0[SONIC1_2] = (ultrasonicVal[0] >> 8); SendData0[SONIC2_1] = ultrasonicVal[1]; @@ -62,11 +63,13 @@ SendData0[SONIC3_2] = (ultrasonicVal[2] >> 8); SendData0[SONIC4_1] = ultrasonicVal[3]; SendData0[SONIC4_2] = (ultrasonicVal[3] >> 8); + SendData0[IR_FOUND] = IR_found; SendData0[XBEE] = xbee; SendData0[CHECK] = CHECKCODE; + //pc.printf("%d\t%d\t%d\t%d\n",SendData0[DIRECTIONN],SendData0[DISTANCE],SendData0[IR_FOUND],SendData0[CHECK]); + count = 0; - } device.putc(SendData0[count]);
diff -r 62ac378df546 -r ed00e9c4ba6e uart1.h --- a/uart1.h Fri Apr 19 09:14:08 2013 +0000 +++ b/uart1.h Tue Apr 30 09:06:57 2013 +0000 @@ -1,9 +1,9 @@ -#define SEND_DATA_NUM 13 +#define SEND_DATA_NUM 14 #define RECEIVE_DATA_NUM 6 #define KEYCODE 120 -#define CHECKCODE (SendData0[DIRECTIONN] ^ SendData0[DISTANCE] ^ SendData0[SONIC1_1] ^ SendData0[SONIC1_2] ^ SendData0[SONIC2_1] ^ SendData0[SONIC2_2] ^ SendData0[SONIC3_1] ^ SendData0[SONIC3_2] ^ SendData0[SONIC4_1] ^ SendData0[SONIC4_2] ^ SendData0[XBEE]) +#define CHECKCODE (SendData0[DIRECTIONN] ^ SendData0[DISTANCE] ^ SendData0[SONIC1_1] ^ SendData0[SONIC1_2] ^ SendData0[SONIC2_1] ^ SendData0[SONIC2_2] ^ SendData0[SONIC3_1] ^ SendData0[SONIC3_2] ^ SendData0[SONIC4_1] ^ SendData0[SONIC4_2] ^ SendData0[IR_FOUND] ^ SendData0[XBEE]) #define KEYCODE2 35 #define CHECKCODE2 (RecData[DATA1] ^ RecData[DATA2] ^ RecData[DATA3] ^ RecData[DATA4]) @@ -20,6 +20,7 @@ SONIC3_2, SONIC4_1, SONIC4_2, + IR_FOUND, XBEE, CHECK, };
diff -r 62ac378df546 -r ed00e9c4ba6e uart2.cpp --- a/uart2.cpp Fri Apr 19 09:14:08 2013 +0000 +++ b/uart2.cpp Tue Apr 30 09:06:57 2013 +0000 @@ -3,28 +3,32 @@ #include "uart2.h" extern Serial device2; -extern BusOut mbedleds; +extern BusOut mbedleds; -int data2[5]; +extern uint8_t data[4]; + +uint8_t data2[5]; void dev2_rx() { - static uint8_t count3; + static uint8_t count3 = 0; static uint8_t RecData2[RECEIVE2_DATA_NUM]; RecData2[count3] = device2.getc(); - //mbedleds = 2; + if(RecData2[KEY3] == KEYCODE4){ count3++; + }else{ count3 = 0; } if(count3 >= RECEIVE2_DATA_NUM){ if(RecData2[CHECK3] == CHECKCODE4){ - mbedleds = 1; + //mbedleds = 8; + mbedleds = 15; data2[0] = RecData2[DATA31]; data2[1] = RecData2[DATA32]; data2[2] = RecData2[DATA33]; @@ -37,14 +41,13 @@ void dev2_tx() { - - static uint8_t count4; + static uint8_t count4 = 0; static uint8_t SendData2[SEND2_DATA_NUM]; if(count4 >= SEND2_DATA_NUM){ SendData2[KEY4] = KEYCODE3; SendData2[DATA41] = 33; - SendData2[DATA42] = 57; + SendData2[DATA42] = data[2]; SendData2[DATA43] = 52; SendData2[DATA44] = 33; SendData2[DATA45] = 38;
diff -r 62ac378df546 -r ed00e9c4ba6e ultrasonic.cpp --- a/ultrasonic.cpp Fri Apr 19 09:14:08 2013 +0000 +++ b/ultrasonic.cpp Tue Apr 30 09:06:57 2013 +0000 @@ -20,7 +20,7 @@ DigitalIn PingPin(ultrasonic_pin[i]); timer2.reset(); while(PingPin == 0){ - if(timer2.read_us() > 1000){ //1ms以上応答なし + if(timer2.read_us() > 1500){ //1.5ms以上応答なし ultrasonicValue[i] = PING_ERR; flag = 1; break;