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

Dependencies:   IRM2121_2 Ping mbed HMC6352 Watchdog

Fork of CatPot_SensorRight by CatPot 2015-2016

main.cpp

Committer:
ryuna
Date:
2015-03-10
Revision:
15:3d27e435e014
Parent:
14:f6f5dad3e75f
Child:
16:83721dac1049

File content as of revision 15:3d27e435e014:

/*
 *main用LPC1768と通信するSensor用LPC1768のプログラム()
 *
 *現在は2つのLPC1768でSerial通信します.
 * 
 */
 
#include "mbed.h"
 
#include "MultiSerial.h"
#include "IRM2121.h"
#include "Ping.h"
#include "HMC6352.h"
#include "Watchdog.h"
 
#define PING_COUNT 5 //5回に1回にPing処理

#define ADDRESS 0xAA//
#define DATA_NUM 9
 
 
IRM2121 Ir[12] = {p5,p6,p7,p8,  p11,p12,    p15,p16,p17,p18,p19,p20};

Ping Ping1(p22, p21);
Ping Ping2(p24, p23);
Ping Ping3(p25, p26);
Ping Ping4(p30, p29);

DigitalOut Led[4] = {LED1, LED2, LED3, LED4};
 
HMC6352 hmc6352(p28,p27);
MultiSerial Mbed(p13, p14);
Serial pc(USBTX,USBRX);
 

void IrSort(unsigned int input[], uint8_t output[]){
    
    /*
     *1,2位の値とその場所の番号をアウトプットに格納する.
     *値が255以上だとボールを検知してないことになる。
     */
    unsigned int  near[2] = {1000,1000};
    uint8_t num[2] = {12,0}, i;
    
    for(i = 0; i < 12; i++ ){
        if(!input[i]){
            continue;
        }
        
        if(near[1] < input[i]){
            continue;
        }
        if(near[0] < input[i]){
            near[1] = input[i];
            num[1] = i;
            continue;
        }
        near[1] = near[0];
        num [1]  = num[0];
        near[0] = input[i];
        num [0]   = i;
    }
 
    if(num[0] == 12){//not found
        output[0] = 255;
        output[1] = 255;
        output[2] = 255;
        return;
    }
    if(num[1] == 12){
        num[1] = 0;
        
    }
    output[0] = num[0]*12 + num[1];//rank1*12 + rank2
    output[1] = near[0] /10;//rank1
    output[2] = near[1] /10;//rank2
    
}
 
 
 
int main() {
    
    /*begin SetUp*/
    Watchdog g;
    g.Configure(1.0f);//1秒でWDT発生
    hmc6352.setOpMode(HMC6352_CONTINUOUS, 1, 20);
    
    /*end   Setup*/
    
    int CompassDef = 0,Compass;
    /*Count*/
    uint8_t PingCk = 0;
    
    /*Data*/
    unsigned int PingData[4] = {0};
    unsigned int IrBase[12] = {0};
    uint8_t IrData[3] = {0};
    uint8_t CompassData[2] = {0};
    
    /*Serial*/
    uint8_t tx_data[DATA_NUM]={0};
    Mbed.write_data(tx_data, ADDRESS);
    Mbed.start_write();
 
    CompassDef = (hmc6352.sample() / 10);
    while(1) {
        
        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);
        
        Compass= ((hmc6352.sample() / 10) + 540 - CompassDef) % 360;
        if(Compass >255 ){
            CompassData[0] = 255;
            CompassData[1] = Compass - 255 ;
        }else{
            CompassData[0] = Compass;
            CompassData[1] = 0;
        }
        if(PingCk == 2){
            
            Ping1.Send();    
            wait_ms(30);
            PingData[0] = Ping1.Read_cm();
            if(PingData[0]>0xFF) PingData[0]=0xFF;
        }
        if(PingCk == 3){
            
            Ping2.Send();    
            wait_ms(30);
            PingData[1] = Ping2.Read_cm();
            if(PingData[1]>0xFF) PingData[1]=0xFF;
        }
        if(PingCk == 4){
            
            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;
        }
        
        //ex.
        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];
        tx_data[7] = CompassData[0];
        tx_data[8] = CompassData[1];        
        
        
        
        Led[0] = 0;//周回の終わり
        
        wait_ms(1);
        //pc.printf("%d\t %d\t %d\t %d\t %d\t %d\t\n",tx_data[3],tx_data[4],tx_data[5],tx_data[6],tx_data[7],tx_data[8]);
        
        
    }
}