投影機本体のプログラム

Dependencies:   mbed

Fork of AstroC_planetarium_master by Ryota Маэда

/media/uploads/Raccoon/main.jpg

main.cpp

Committer:
Raccoon
Date:
2017-12-21
Revision:
1:b6a142877de4
Parent:
0:ee25c3c95837
Child:
2:9aaad52da789

File content as of revision 1:b6a142877de4:

#include "mbed.h"
#define STEPPER_DELTA_T 150
#define k 5
#define MAX 4

/*ステッピングモーター*/
Ticker step_timer;
DigitalOut APHASE(p5);
DigitalOut AENBL(p6);
DigitalOut BPHASE(p7);
DigitalOut BENBL(p8);

/*MCP4922*/
Ticker spi_timer;
SPI spi(p11, p12, p13);//mosi(SDO), miso, sclk
DigitalOut CS(p14);
DigitalOut LDAC(p17);

/*RGB_LED_3*/
Ticker rgb_timer;
BusOut signal(p24, p25, p26);
PwmOut R_led(p22);
PwmOut G_led(p21);
PwmOut B_led(p23);

/*シリアル通信(xbee)*/
Serial xbee(p28, p27); // tx, rx

/*受信データ格納*/
unsigned char data[MAX] = {0};
unsigned char tmp_data[MAX][k] ={0};

/*ステッピングモーター*/
void stepper(void)
{
    static unsigned int cnt = 0;
    static int i = 1;
    static unsigned int delay = 1000;
    
    /*速度制限:MAX周期1500[us](脱調防止)*/
    delay = (255 - data[0]) + 10;
    
    /*ステッピングモーターの制御*/
    if((++cnt > delay) && data[0]!=0)
    {
        AENBL = 1;
        BENBL = 1;
        switch(i)
        {
        case 1:
            APHASE = 1;
            break;
        case 2:
            BPHASE = 1;
            break;
        case 3:
            APHASE = 0;
            break;
        case 4:
            BPHASE = 0;
            break;
        }
        
        if(++i==5)i = 1;
        cnt = 0;
    }
    else if(data[0]==0)
    {
        AENBL = 0;
        BENBL = 0;
    }
}

/*spiでの書き込み用関数*/
void spi_write(unsigned int config, unsigned int mask, unsigned int value)
{
    unsigned int spi_data;
    spi_data = config | (mask & value);
    CS = 0;
    LDAC = 1;
    spi.write(spi_data);
    CS = 1;
    LDAC = 0;
}    

/*SPI通信でMCP4922(DAC)を動かすための関数*/
void light_bulb(void)
{
    static unsigned char ss = 0;
    
    //MCP4922へ送信するデータ
    static unsigned int mask = 0b0000111111111111;
    unsigned int config;
    unsigned int value;
    
    static unsigned int EX_MAX = 2300;//2.0[V]
    static unsigned int Milky_MAX = 3150;//2.5[V]
    
    if(ss==0)//減らせる
    {
        /*EX球(2V2A)用*/
        config = 0b0111000000000000;
        value = (data[1] * EX_MAX) * 0.0039215f;
        spi_write(config, mask, value);
        ss = 1;
    }
    else if(ss==1)//減らせる
    {
        /*天の川(2.5V2A)用*/
        config = 0b1111000000000000;
        //value = (data[2] * Milky_MAX) / 255;
        value = (data[1] * EX_MAX) * 0.0039215f;
        spi_write(config, mask, value);
        ss = 0;
    }
}

//light//
void light(float val)
{
    R_led = val;
    G_led = 0.38f*val;
    B_led = 0.0;
}   

/*RGB_LEDをダイナミック点灯するための関数*/
void RGB_LED(void){
    static uint8_t dig;
    uint8_t binary = 0b00001;
    
    light((float)data[3]*0.0039215f);
    
    signal = binary << dig;
    
    if(++dig == 3)dig = 0;
}

void swap(unsigned char *x, unsigned char *y)
{
    unsigned char tmp;
    tmp = *x;
    *x = *y;
    *y = tmp;
}

/*通信のスパイクノイズの除去*/
/*k(奇数) の値で参照する過去の値の数を変更する*/
unsigned char MedianFilter(unsigned char array[k])
{
    int i, j;
    static int index = (int)(k-1)/2;
    unsigned char tmp;
    unsigned char N[k];
    
    /*copy*/
    for(i=k-1; i>=0; i--)
    {
        N[i]=array[i];
        if(i!=k-1)array[i+1] = array[i];
    }
    
    for(i=0; i<index; i++)
    {
        for(j=k-1; i<j; j--)
        {
            if(N[j] < N[j-1])
            {
                //swap(&N[j], &N[j-1]);
                tmp = N[j];
                N[j] = N[j-1];
                N[j-1] = tmp;
            }
        }
    }
    return N[index];
}

/*シリアル通信受信(xbee)*/
void receve(void)
{
    static int sw = 0;
    /*送信側から送信開始の合図で'R'が送られてくる*/
    /*'R'を受信したら、データ受信開始*/
    /*5個のデータが順番に受信されるので、順番に配列に代入する*/
    if((sw == 0) && (xbee.getc() == 'R'))
    { 
        sw = 1;
    }
    else if(1 <= sw && sw <= MAX)//条件式減らせる
    {
        //data[sw-1] = xbee.getc();
        
        tmp_data[sw-1][0] = xbee.getc();
        data[sw-1] = MedianFilter(tmp_data[sw-1]);
        
        if(++sw > MAX)sw = 0;
    }
    xbee.putc(1);//受信確認
}

int main(){
    /*ステッピングモーター*/
    AENBL = 0;
    APHASE = 0;
    BENBL = 0;
    BPHASE = 0;
    step_timer.attach_us(&stepper, STEPPER_DELTA_T);//100[us],10000[Hz]
    
    /*spi通信(MCP4922)*/
    CS = 1;
    LDAC = 1;
    spi.format(16, 0);
    spi.frequency(1000000);
    spi_timer.attach_us(&light_bulb, 50000);
    
    /*RGB_LED_3*/
    R_led.period(0.000048);//0.00004
    G_led.period(0.000048);
    B_led.period(0.000048);
    rgb_timer.attach_us(&RGB_LED, 4800);//4000
    
    /*シリアル通信(xbee)*/
    xbee.baud(9600);
    xbee.format(8, Serial::None, 2);
    xbee.attach(&receve, Serial::RxIrq);//受信割り込み
    
    while(1);
}