program_BlockPIL //{Ping,Ir,Lines} Sensor

Dependencies:   mbed HMC6352

Revision:
1:3ecb80796a7f
Parent:
0:010fe1eae7d2
--- 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');
         }
         
     }