Fumiya Fujisawa
/
program_BlockPIL
program_BlockPIL //{Ping,Ir,Lines} Sensor
Revision 1:3ecb80796a7f, committed 2014-02-24
- Comitter:
- ryuna
- Date:
- Mon Feb 24 05:20:20 2014 +0000
- Parent:
- 0:010fe1eae7d2
- Commit message:
- program_Sensors value
Changed in this revision
diff -r 010fe1eae7d2 -r 3ecb80796a7f HMC6352.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HMC6352.lib Mon Feb 24 05:20:20 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/aberk/code/HMC6352/#83c0cb554099
diff -r 010fe1eae7d2 -r 3ecb80796a7f main.cpp --- a/main.cpp Sat Feb 22 07:05:09 2014 +0000 +++ b/main.cpp Mon Feb 24 05:20:20 2014 +0000 @@ -1,4 +1,5 @@ #include "mbed.h" +#include "HMC6352.h" /*PING DEFINE */ #define ERROR_PING 0xFFF #define ALL_PING 4 @@ -8,17 +9,19 @@ #define ALL_IR 10 //#define OVERLINE_IR 46 /*LINE DEFINE*/ -#define JUDGEVOL_LINE 0.8 +#define JUDGEVOL_LINE 0.9 #define ALL_LINE 4 //OTHERS #define ALL_KIND 3 //kind of sensors #define MAX_NUM 10 //sensors most number +extern void micon_tx(); /* -Ping , IrSensor , LineSensor +Ping , IrSensor , LineSensor ,compass */ - //LINE sensor is float value +Serial sensor(p13,p14); Serial pc(USBTX,USBRX); +HMC6352 compass(p9, p10); Timer time_ping; Timer time_ir; @@ -29,10 +32,13 @@ enum sensors{Ping,Ir,Line}; volatile unsigned int value_ping[ALL_PING] = {0}; volatile unsigned int value_ir[ALL_IR] = {0}; -volatile float value_line[ALL_LINE] = {0}; -DigitalOut myled(LED1); +volatile unsigned int value_ir_min = 0; +volatile unsigned int ir_min_num = 0; +volatile unsigned int value_line[ALL_LINE] = {0}; +volatile unsigned int value_compass = 0; -unsigned int moving_ave(enum sensors kind,int num, unsigned int data){ + +/*unsigned int*/void moving_ave(enum sensors kind,int num, unsigned int data){ static unsigned int sum[ALL_KIND][MAX_NUM] = {0}; static unsigned int temp[ALL_KIND][MAX_NUM][10] = {0}; //static unsigned int ave[MAX_NUM] = {0}; @@ -58,13 +64,17 @@ value_ir[num] = sum[kind][num]/100; break; case Line: - value_line[num] = sum[kind][num]/10000.0; + if((sum[kind][num]/10000.0) >=JUDGEVOL_LINE){ + value_line[num] = 1;//white + }else{ + value_line[num] = 0;//green + } break; default : break; } - return sum[kind][num];//return no config sum + //return sum[kind][num];//return no config sum } unsigned int ping_function (int num){ @@ -127,7 +137,7 @@ temp = (time_ir.read_us() - memory); time_ir.stop(); time_ir.reset(); - wait(0.01); + //wait(0.01); return temp; } } @@ -144,33 +154,54 @@ return (int)(memory*1000); } +void ir_min_fun(){ + static unsigned int min; + static int i, num = 0; + min = value_ir[0]; + for (i = 0;i <=ALL_IR -1;i++){ + if(min >= value_ir[i]){ + min = value_ir[i]; + num = i; + } + } + value_ir_min = min; + ir_min_num = num; +} int main() { //enum sensors kinds; + uint8_t compassdef = 0; int num[ALL_KIND] = {0}; - unsigned int value1[ALL_PING] = {0}; - unsigned int value2[ALL_IR] = {0}; - float value3[ALL_LINE] = {0}; + //unsigned int value1[ALL_PING] = {0}; + //unsigned int value2[ALL_IR] = {0}; + //float value3[ALL_LINE] = {0}; + //送信開始 + sensor.putc(1); + //送信空き割り込み設定 + sensor.attach(&micon_tx,Serial::TxIrq); + compass.setOpMode(HMC6352_CONTINUOUS, 1, 20); + compassdef = (compass.sample() / 10); + while(1) { - //value1[num[Ping]] = moving_ave(Ping,num[Ping],ping_function(num[Ping]))/10; - value2[num[Ir]] = moving_ave(Ir,num[Ir],ir_function(num[Ir]))/100; - //value3[num[Line]]= moving_ave(Line,num[Line],line_function(num[Line]))/10000.0; - //pc.printf("value1[%d]=%d ",num[Ping], value1[num[Ping]]); - pc.printf("value2[%d]=%d ",num[Ir], value2[num[Ir]]); - //pc.printf("value3[%d]=%lf ",num[Line], value3[num[Line]]); - num[Ping]++; - num[Ir]++; - num[Line]++; + value_compass = ((compass.sample() / 10) + 540 - compassdef) % 360; + moving_ave(Ping,num[Ping],ping_function(num[Ping])); + + pc.printf("value_compass=%d\n",value_compass); + //ir_min_fun(); + //num[Ping]++; + //num[Ir]++; + //num[Line]++; if(num[Ping]>= ALL_PING){ num[Ping] = 0; - //putchar('\n'); + putchar('\n'); } if(num[Ir]>= ALL_IR){ num[Ir] = 0; putchar('\n'); + pc.printf("min=%d ,num = %d\n",value_ir_min,ir_min_num); } if(num[Line]>= ALL_LINE){ num[Line] = 0; - //putchar('\n'); + putchar('\n'); } }
diff -r 010fe1eae7d2 -r 3ecb80796a7f usart.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/usart.cpp Mon Feb 24 05:20:20 2014 +0000 @@ -0,0 +1,70 @@ +#include "mbed.h" + +#define KEYCODE 0xAA +#define TX_CHECKCODE (tx_data[1]^tx_data[2]^tx_data[3]^tx_data[4]^tx_data[5]^tx_data[6]^tx_data[7]^tx_data[8]^tx_data[9]^tx_data[10]) +#define RX_CHECKCODE (rx_data[1]^rx_data[2]^rx_data[3]^rx_data[4]^rx_data[5]^rx_data[6]^rx_data[7]^rx_data[8]^rx_data[9]^rx_data[10]) +#define DATA_NUM 13 +#define CHECK (DATA_NUM - 1) +#define ALL_IR 10 +#define ALL_PING 4 +#define ALL_LINE 4 + + + +extern Serial sensor; +extern Serial pc; +extern unsigned int value_ping[ALL_PING]; +extern unsigned int value_ir[ALL_IR]; +extern unsigned int value_ir_min; +extern unsigned int ir_min_num; +extern unsigned int value_line[ALL_LINE]; +extern unsigned int value_compass; +void micon_rx(){ + + static uint8_t rx; + static uint8_t rx_data[DATA_NUM]; + + rx_data[rx] = sensor.getc(); + + if(rx_data[0] == KEYCODE){ + rx++; + } + + if(rx >= DATA_NUM){ + if(rx_data[CHECK] == RX_CHECKCODE){ + pc.printf("%d %d %d %d %d\n", rx_data[1], rx_data[2], rx_data[3], rx_data[4], rx_data[5]); + } + rx = 0; + } + +} + +void micon_tx(){ + + static uint8_t tx; + static uint8_t tx_data[DATA_NUM]; + + if(tx >= DATA_NUM){ + tx_data[0] = KEYCODE; + tx_data[1] = value_ir_min; + tx_data[2] = ir_min_num; + tx_data[3] = value_ping[0]; + tx_data[4] = value_ping[1]; + tx_data[5] = value_ping[2]; + tx_data[6] = value_ping[3]; + tx_data[7] = value_line[0]; + tx_data[8] = value_line[1]; + tx_data[9] = value_line[2]; + tx_data[10] = value_line[3]; + tx_data[11] = value_compass; + tx_data[12] = TX_CHECKCODE; + + tx = 0; + } + + + sensor.putc(tx_data[tx]); + //pc.printf("%d\n", tx_data[tx]); + tx++; + //wait(0.1); +} \ No newline at end of file