右側のほうのセンサLPC1114FN28 fumiya版

Dependencies:   IRM2121_2 Ping mbed HMC6352 Watchdog

Fork of CatPot_SensorRight by CatPot 2015-2016

Revision:
13:c73f90a890b6
Parent:
12:4bf564955c53
Child:
14:f6f5dad3e75f
--- a/main.cpp	Thu Mar 05 10:01:37 2015 +0000
+++ b/main.cpp	Thu Mar 05 13:51:29 2015 +0000
@@ -1,7 +1,7 @@
 /*
- *メインと通信するLPC1114のプログラム()
+ *main用LPC1768と通信するSensor用LPC1768のプログラム()
  *
- *現在は両方がmainと通信します.
+ *現在は2つのLPC1768でSerial通信します.
  * 
  */
  
@@ -10,20 +10,24 @@
 #include "IRM2121.h"
 #include "Ping.h"
 #include "MultiSerial.h"
+#include "HMC6352.h"
+#include "Watchdog.h"
  
 #define PING_COUNT 5 //5回に1回にPing処理
 #define IR_LEVEL 400 //irのしきい値
 #define ADDRESS 0xAA//
-#define DATA_NUM 8
+#define DATA_NUM 7
  
  
-IRM2121 Ir[6] = {p25, p26, p27, p28, p29, p30};
- 
+IRM2121 Ir[12] = {p5,p6,p7,p8,  p11,p12,    p15,p16,p17,p18,p19,p20};
+
 Ping Ping1(p21, p22);
 Ping Ping2(p23, p24);
-DigitalOut Led(LED1);
+Ping Ping3(p25, p26);
+Ping Ping4(p29, p30);
+
+DigitalOut Led[4] = {LED1, LED2, LED3, LED4};
  
-//I2CSlave Mbed(dp5,dp27);//sda,scl
 MultiSerial Mbed(p13, p14);
 Serial pc(USBTX,USBRX);
  
@@ -50,7 +54,7 @@
     return  sum[num]/10;   
 }
  
-void IrSort(unsigned int input[], char output[]){
+void IrSort(unsigned int input[], uint8_t output[]){
     
     /*
      *1,2位の値とその場所の番号をアウトプットに格納する.
@@ -59,7 +63,7 @@
     unsigned int  near[2] = {1000,1000};
     uint8_t num[2] = {12,0}, i;
     
-    for(i = 0; i < 6; i++ ){
+    for(i = 0; i < 12; i++ ){
         if(!input[i]){
             continue;
         }
@@ -98,10 +102,8 @@
 int main() {
     
     /*begin SetUp*/
-    //default Mbed.frequency();
-    //Mbed.address(I2C_ADDRESS);
-    //Watchdog g;
-    //g.kick(1.0f);//1秒でWDT発生
+    Watchdog g;
+    g.Configure(1.0f);//1秒でWDT発生
     /*end   Setup*/
     
     
@@ -109,132 +111,93 @@
     uint8_t PingCk = 0;
     
     /*Data*/
-    uint8_t PingData[2] = {0};
-    char IrData[3] = {0};
-    unsigned int IrBase[6] = {0};
-    
-    /*State(operate_value etc...)*/
-    //int I2CState;
-    
-    /* I2Cdata*/
-    //char SendData[9]={0};
-    //char AddressData[1] = {0};
+    unsigned int PingData[4] = {0};
+    unsigned int IrBase[12] = {0};
+    uint8_t IrData[3] = {0};
     
     /*Serial*/
     uint8_t tx_data[DATA_NUM]={0};
-    
     Mbed.write_data(tx_data, ADDRESS);
     Mbed.start_write();
  
     while(1) {
-        Led = 1;//周回の初め    
- 
-        //通信していないとき
-        if(1/*!AddressData[0]*/)
-        {
-            PingCk ++;
-            
-            IrBase[0] = Ir[0].Read();
-            IrBase[1] = Ir[1].Read();
-            IrBase[2] = Ir[2].Read();
-            IrBase[3] = Ir[3].Read();
-            IrBase[4] = Ir[4].Read();
-            IrBase[5] = Ir[5].Read();
-            
-            /*平均化
-            
-            IrTemp[0] = IrMoving_ave(0,IrTemp[0]);
-            IrTemp[1] = IrMoving_ave(1,IrTemp[1]);
-            IrTemp[2] = IrMoving_ave(2,IrTemp[2]);
-            IrTemp[3] = IrMoving_ave(3,IrTemp[3]);
-            IrTemp[4] = IrMoving_ave(4,IrTemp[4]);
-            IrTemp[5] = IrMoving_ave(5,IrTemp[5]);
-            
-            */
+        
+        g.Service();
+        
+        PingCk ++;
+        
+        Led[0] = 1;//周回の初め    
+        
+        
+        
+        IrBase[0]  = Ir[0].Read();
+        IrBase[1]  = Ir[1].Read();
+        IrBase[2]  = Ir[2].Read();
+        IrBase[3]  = Ir[3].Read();
+        IrBase[4]  = Ir[4].Read();
+        IrBase[5]  = Ir[5].Read();
+        IrBase[6]  = Ir[6].Read();
+        IrBase[7]  = Ir[7].Read();
+        IrBase[8]  = Ir[8].Read();
+        IrBase[9]  = Ir[9].Read();
+        IrBase[10] = Ir[10].Read();
+        IrBase[11] = Ir[11].Read();
+        
+        /*平均化
+        IrTemp[0] = IrMoving_ave(0,IrTemp[0]);
+        IrTemp[1] = IrMoving_ave(1,IrTemp[1]);
+        IrTemp[2] = IrMoving_ave(2,IrTemp[2]);
+        IrTemp[3] = IrMoving_ave(3,IrTemp[3]);
+        IrTemp[4] = IrMoving_ave(4,IrTemp[4]);
+        IrTemp[5] = IrMoving_ave(5,IrTemp[5]);
+        */
+        
+        IrSort(IrBase,IrData);
+        
+        if(PingCk == 2){
             
-            
-            IrSort(IrBase,IrData);
+            Ping1.Send();    
+            wait_ms(30);
+            PingData[0] = Ping1.Read_cm();
+            if(PingData[0]>0xFF) PingData[0]=0xFF;
+        }
+        if(PingCk == 3){
             
-            if(PingCk == 2){
-                
-                Ping1.Send();    
-                wait_ms(30);
-                PingData[0] = Ping1.Read_cm();
-                if(PingData[0]>125) PingData[0]=125;
-            }
-            
+            Ping2.Send();    
+            wait_ms(30);
+            PingData[1] = Ping2.Read_cm();
+            if(PingData[1]>0xFF) PingData[1]=0xFF;
+        }
+        if(PingCk == 4){
             
-            if(PingCk >= PING_COUNT){
-                
-                Ping2.Send();    
-                wait_ms(30);
-                PingData[1] = Ping2.Read_cm();
-                if(PingData[1]>125) PingData[1]=125;
-                PingCk = 0;
-                
-                //pc.printf("%d \t %d \t %d \n",PingData[0],PingData[1],IrBase[0]);
+            Ping3.Send();    
+            wait_ms(30);
+            PingData[2] = Ping3.Read_cm();
+            if(PingData[2]>0xFF) PingData[2]=0xFF;
+        }
+        if(PingCk >= PING_COUNT){
             
-            }
+            Ping4.Send();    
+            wait_ms(30);
+            PingData[3] = Ping4.Read_cm();
+            if(PingData[3]>0xFF) PingData[3]=0xFF;
             
-        
+            PingCk = 0;
         }
         
-        tx_data[0] = PingData[0];
-        tx_data[1] = PingData[1];
-        tx_data[2] = IrData[0];
-        tx_data[3] = IrData[1];
-        tx_data[4] = IrData[2];
-        
-        /*I2CState = Mbed.receive();
+        tx_data[0] = IrData[0];
+        tx_data[1] = IrData[1];
+        tx_data[2] = IrData[2];
+        tx_data[3] = PingData[0];
+        tx_data[4] = PingData[1];
+        tx_data[5] = PingData[2];
+        tx_data[6] = PingData[3];
         
-        switch(I2CState){    
-            case I2CSlave::ReadAddressed:
-            
-                 if(AddressData[0]){
-                    switch(AddressData[0]){
-                        case 0x01://PING
-                                SendData[0] = (char)PingData[0];
-                                SendData[1] = (char)PingData[1];
-                                Mbed.write(SendData, 2);
-                                break;
-                        case 0x02://BALL
-                                SendData[0] = (char)IrData[0];
-                                SendData[1] = (char)IrData[1];
-                                SendData[2] = (char)IrData[2];
-                                Mbed.write(SendData, 3);
-                                break;                   
-                            
-                        default:
-                                break;
-                    }
-                    AddressData[0] = 0;
-                 }else{
-                    SendData[0] = (char)IrData[0];         
-                    SendData[1] = (char)IrData[1];
-                    SendData[2] = (char)IrData[2];
-                    SendData[3] = (char)PingData[0];
-                    SendData[4] = (char)PingData[1];
-                    Mbed.write(SendData, 5);
-                 }
-                 
-                 break;
-            case I2CSlave::WriteAddressed:
-                 Mbed.read(AddressData,1);
-                 break;
-                 
-            case I2CSlave::WriteGeneral:
-                 
-                 break;
+        
+        
         
-        }
-        */
-        Led = 0;
-        /*
-        pc.printf("%d \t %d \t %d ",IrBase[0],IrBase[1],IrBase[2]);
+        Led[0] = 0;//周回の終わり
         
-        pc.printf("\t %d \t %d \t %d \n",IrBase[3],IrBase[4],IrBase[5]);
-        */
-        
-        //wait(0.1);
+        wait_ms(1);
     }
 }
\ No newline at end of file