Ryota Маэда
/
AstroC_planetarium_master
投影機本体のプログラム
Fork of AstroC_planetarium_master by
Diff: main.cpp
- Revision:
- 2:9aaad52da789
- Parent:
- 1:b6a142877de4
--- a/main.cpp Thu Dec 21 13:02:23 2017 +0000 +++ b/main.cpp Tue Dec 26 01:24:33 2017 +0000 @@ -1,6 +1,4 @@ #include "mbed.h" -#define STEPPER_DELTA_T 150 -#define k 5 #define MAX 4 /*ステッピングモーター*/ @@ -27,8 +25,18 @@ Serial xbee(p28, p27); // tx, rx /*受信データ格納*/ -unsigned char data[MAX] = {0}; -unsigned char tmp_data[MAX][k] ={0}; +uint8_t data[MAX] = {0}; +uint8_t tmp_data[MAX][3] ={0}; + +void stepper(void); +void spi_write(unsigned int config, unsigned int mask, unsigned int value); +void LightBulb(void); +void DomeLapm(float val); +void RGB_LED(void); +void ArrayShift_3(uint8_t array[3]); +uint8_t MedianFilter_3(uint8_t src[3]); +void ChannelDecoding(uint8_t CodeWord); +void receve(void); /*ステッピングモーター*/ void stepper(void) @@ -37,10 +45,8 @@ 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; @@ -72,7 +78,7 @@ } /*spiでの書き込み用関数*/ -void spi_write(unsigned int config, unsigned int mask, unsigned int value) +void spi_write(int config, int mask, int value) { unsigned int spi_data; spi_data = config | (mask & value); @@ -84,118 +90,80 @@ } /*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] +void LightBulb(void) +{ + static int mask = 0b0000111111111111; + int config; + int value; - 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; - } + static int EX_MAX = 2300; + + config = 0b0111000000000000; + value = (data[1] * EX_MAX) * 0.0039215f; + spi_write(config, mask, value); } -//light// -void light(float val) +/*室内灯(暖色系の色)*/ +void DomeLamp(float val) { R_led = val; G_led = 0.38f*val; - B_led = 0.0; -} - + B_led = 0.04f*val; +} + /*RGB_LEDをダイナミック点灯するための関数*/ -void RGB_LED(void){ +void RGB_LED(void) +{ static uint8_t dig; uint8_t binary = 0b00001; - light((float)data[3]*0.0039215f); - + DomeLamp((float)data[3]*0.0039215f); + signal = binary << dig; - if(++dig == 3)dig = 0; } -void swap(unsigned char *x, unsigned char *y) +/*配列をシフトする*/ +void ArrayShift_3(uint8_t array[3]) { - unsigned char tmp; - tmp = *x; - *x = *y; - *y = tmp; + array[2] = array[1]; + array[1] = array[0]; } -/*通信のスパイクノイズの除去*/ -/*k(奇数) の値で参照する過去の値の数を変更する*/ -unsigned char MedianFilter(unsigned char array[k]) +/*メディアンフィルター(要素数3の配列)*/ +uint8_t MedianFilter_3(uint8_t src[3]) { - 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--) + if(src[0]>=src[1]) { - N[i]=array[i]; - if(i!=k-1)array[i+1] = array[i]; + if(src[1]>=src[2])return src[1]; + else if(src[0]<=src[2])return src[0]; + else return src[2]; } + else if(src[0]>src[2])return src[0]; + else if(src[1]>src[2])return src[2]; + else return src[1]; +} + +/*復号化*/ +void ChannelDecoding(uint8_t CodeWord) +{ + int index; + index = CodeWord & 0b00000111; - for(i=0; i<index; i++) + if(index<MAX) { - 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; - } - } + tmp_data[index][0] = (CodeWord >> 3)*8.226f; + /*ノイズ除去*/ + data[index] = MedianFilter_3(tmp_data[index]); + ArrayShift_3(tmp_data[index]); } - 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);//受信確認 + ChannelDecoding(xbee.getc()); + xbee.putc(1); } int main(){ @@ -204,24 +172,24 @@ APHASE = 0; BENBL = 0; BPHASE = 0; - step_timer.attach_us(&stepper, STEPPER_DELTA_T);//100[us],10000[Hz] + step_timer.attach_us(&stepper, 100); /*spi通信(MCP4922)*/ CS = 1; LDAC = 1; spi.format(16, 0); spi.frequency(1000000); - spi_timer.attach_us(&light_bulb, 50000); + spi_timer.attach_us(&LightBulb, 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 + R_led.period(0.00004); + G_led.period(0.00004); + B_led.period(0.00004); + rgb_timer.attach_us(&RGB_LED, 4000); /*シリアル通信(xbee)*/ xbee.baud(9600); - xbee.format(8, Serial::None, 2); + xbee.format(8, Serial::None, 1); xbee.attach(&receve, Serial::RxIrq);//受信割り込み while(1);