yusuke takahashi
/
Ball_fanction
IRIR
Diff: main.cpp
- Revision:
- 2:22b099fbdd14
- Parent:
- 1:d6c8be12a3de
--- a/main.cpp Tue Mar 05 09:45:33 2013 +0000 +++ b/main.cpp Fri Mar 08 03:49:58 2013 +0000 @@ -3,8 +3,10 @@ #define IR_TIME_NOTFOUND 833 /* 見つけられなかったと判断するまでの時間(単位:us) */ #define IR_COUNTMAX 487 /*最大パルス幅 パルスの存在しうる最大時間は487us*/ #define ALL_IR 9 +#define DIREC_IR 8 #define DIRECTION 16 #define TERM 0 +#define DELTA -2 #define SWAP(type,a,b) { type temp = a; a = b; b = temp; } Serial pc(USBTX, USBRX); // tx, rx @@ -49,14 +51,14 @@ int moving_ave(int data,int active_ir) { - static int tmp[14][ALL_IR]= {{0,0}}; + static int tmp[12][ALL_IR]= {{0,0}}; static int sum[ALL_IR] = {0}; int count = 0; - sum[active_ir] -= tmp[13][active_ir]; + sum[active_ir] -= tmp[11][active_ir]; sum[active_ir] += data; - tmp[13][active_ir] = tmp[12][active_ir]; - tmp[12][active_ir] = tmp[11][active_ir]; + //tmp[13][active_ir] = tmp[12][active_ir]; + //tmp[12][active_ir] = tmp[11][active_ir]; tmp[11][active_ir] = tmp[10][active_ir]; tmp[10][active_ir] = tmp[9][active_ir]; tmp[9][active_ir] = tmp[8][active_ir]; @@ -70,7 +72,7 @@ tmp[1][active_ir] = tmp[0][active_ir]; tmp[0][active_ir] = data; - for(int i=0;i<14;i++){ + for(int i=0;i<12;i++){ if(tmp[i][active_ir])count++; } @@ -124,7 +126,8 @@ if((timer_ir.read_us()-memory_ir)>=IR_TIME_NOTFOUND)break; if(!(sensor_ir)) { - value = moving_ave( (timer_ir.read_us()-memory_ir)/10 , active_ir ); + //value = moving_ave( (timer_ir.read_us()-memory_ir)/10 , active_ir ); + value = (timer_ir.read_us()-memory_ir)/10; break; } @@ -149,7 +152,7 @@ int min = 100,youso_min = 100; - for(int i = 0; i<ALL_IR; i++) { + for(int i = 0; i<DIREC_IR; i++) { if((ir_value[i]<min)&&(ir_value[i])) { min = ir_value[i]; youso_min = i; @@ -178,9 +181,9 @@ direc = youso_min*2; } - if(youso_min == 0){ + /*if(youso_min == 0){ direc = 0; - } + }*/ /******* direction end *******/ @@ -188,9 +191,7 @@ int dista; - if((ir_value[youso_min]>0)&&(ir_value[youso_min]<=25 + TERM)){ - dista = 10; - }else if((ir_value[youso_min]>25 + TERM)&&(ir_value[youso_min]<=28 + TERM)) { + if((ir_value[youso_min]<=28 + TERM)) { dista = 30; } else if((ir_value[youso_min]>28 + TERM)&&(ir_value[youso_min]<=35 + TERM)) { dista = 90; @@ -202,42 +203,55 @@ dista = 0; } - if(dista <= 30){ - if((direc == 0)&&(ir_value[8] <= 30)){ - dista = 10; - }else if((direc == 1)&&(ir_value[8] <= 30 )){ - dista = 10; - }else if((direc == 2)&&(ir_value[8] <= 40 )){ - dista = 10; - }else if((direc == 3)&&(ir_value[8] <= 43)){ - dista = 10; - }else if((direc == 4)&&(ir_value[8] <= 42)){ - dista = 10; - }else if((direc == 5)&&(ir_value[8] <= 42)){ - dista = 10; - }else if((direc == 6)&&(ir_value[8] <= 42)){ - dista = 10; - }else if((direc == 7)&&(ir_value[8] <= 40)){ - dista = 10; - }else if((direc == 8)&&(ir_value[8] <= 38)){ - dista = 10; - }else if((direc == 9)&&(ir_value[8] <= 34)){ - dista = 10; - }else if((direc == 10)&&(ir_value[8] <= 40)){ - dista = 10; - }else if((direc == 11)&&(ir_value[8] <= 42)){ - dista = 10; - }else if((direc == 12)&&(ir_value[8] <= 39)){ - dista = 10; - }else if((direc == 13)&&(ir_value[8] <= 40)){ - dista = 10; - }else if((direc == 14)&&(ir_value[8] <= 41)){ - dista = 10; - }else if((direc == 15)&&(ir_value[8] <= 33)){ - dista = 10; + int count_ir = 0,total_ir = 0; + + for(int i=0; i<DIREC_IR; i++){ + if(ir_value[i]){ + total_ir += ir_value[i]; + count_ir++; } - } + } + + double hihhihi = 0; + + hihhihi = (double)ir_value[youso_min]/(double)ir_value[8]; + + if((direc == 0)&&(hihhihi >= 0.80)){ + dista = 10; + }else if((direc == 1)&&(hihhihi >= 0.80)){ + dista = 10; + }else if((direc == 2)&&(hihhihi >= 0.65)){ + dista = 10; + }else if((direc == 3)&&(hihhihi >= 0.65)){ + dista = 10; + }else if((direc == 4)&&(hihhihi >= 0.80)){ + dista = 10; + }else if((direc == 5)&&(hihhihi >= 0.65)){ + dista = 10; + }else if((direc == 6)&&(hihhihi >= 0.65)){ + dista = 10; + }else if((direc == 7)&&(hihhihi >= 0.80)){ + dista = 10; + }else if((direc == 8)&&(hihhihi >= 0.80)){ + dista = 10; + }else if((direc == 9)&&(hihhihi >= 0.80)){ + dista = 10; + }else if((direc == 10)&&(hihhihi >= 0.65)){ + dista = 10; + }else if((direc == 11)&&(hihhihi >= 0.65)){ + dista = 10; + }else if((direc == 12)&&(hihhihi >= 0.80)){ + dista = 10; + }else if((direc == 13)&&(hihhihi >= 0.65)){ + dista = 10; + }else if((direc == 14)&&(hihhihi >= 0.65)){ + dista = 10; + }else if((direc == 15)&&(hihhihi >= 0.80)){ + dista = 10; + } + + /******** distance end *******/ *direction = Convert_Direction[direc];