投影機本体のプログラム

Dependencies:   mbed

Fork of AstroC_planetarium_master by Ryota Маэда

/media/uploads/Raccoon/main.jpg

Committer:
Raccoon
Date:
Tue Dec 26 01:24:33 2017 +0000
Revision:
2:9aaad52da789
Parent:
1:b6a142877de4
minor revision update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Raccoon 0:ee25c3c95837 1 #include "mbed.h"
Raccoon 1:b6a142877de4 2 #define MAX 4
Raccoon 0:ee25c3c95837 3
Raccoon 0:ee25c3c95837 4 /*ステッピングモーター*/
Raccoon 0:ee25c3c95837 5 Ticker step_timer;
Raccoon 0:ee25c3c95837 6 DigitalOut APHASE(p5);
Raccoon 0:ee25c3c95837 7 DigitalOut AENBL(p6);
Raccoon 0:ee25c3c95837 8 DigitalOut BPHASE(p7);
Raccoon 0:ee25c3c95837 9 DigitalOut BENBL(p8);
Raccoon 0:ee25c3c95837 10
Raccoon 0:ee25c3c95837 11 /*MCP4922*/
Raccoon 0:ee25c3c95837 12 Ticker spi_timer;
Raccoon 0:ee25c3c95837 13 SPI spi(p11, p12, p13);//mosi(SDO), miso, sclk
Raccoon 0:ee25c3c95837 14 DigitalOut CS(p14);
Raccoon 0:ee25c3c95837 15 DigitalOut LDAC(p17);
Raccoon 0:ee25c3c95837 16
Raccoon 0:ee25c3c95837 17 /*RGB_LED_3*/
Raccoon 0:ee25c3c95837 18 Ticker rgb_timer;
Raccoon 0:ee25c3c95837 19 BusOut signal(p24, p25, p26);
Raccoon 1:b6a142877de4 20 PwmOut R_led(p22);
Raccoon 1:b6a142877de4 21 PwmOut G_led(p21);
Raccoon 1:b6a142877de4 22 PwmOut B_led(p23);
Raccoon 0:ee25c3c95837 23
Raccoon 0:ee25c3c95837 24 /*シリアル通信(xbee)*/
Raccoon 0:ee25c3c95837 25 Serial xbee(p28, p27); // tx, rx
Raccoon 0:ee25c3c95837 26
Raccoon 0:ee25c3c95837 27 /*受信データ格納*/
Raccoon 2:9aaad52da789 28 uint8_t data[MAX] = {0};
Raccoon 2:9aaad52da789 29 uint8_t tmp_data[MAX][3] ={0};
Raccoon 2:9aaad52da789 30
Raccoon 2:9aaad52da789 31 void stepper(void);
Raccoon 2:9aaad52da789 32 void spi_write(unsigned int config, unsigned int mask, unsigned int value);
Raccoon 2:9aaad52da789 33 void LightBulb(void);
Raccoon 2:9aaad52da789 34 void DomeLapm(float val);
Raccoon 2:9aaad52da789 35 void RGB_LED(void);
Raccoon 2:9aaad52da789 36 void ArrayShift_3(uint8_t array[3]);
Raccoon 2:9aaad52da789 37 uint8_t MedianFilter_3(uint8_t src[3]);
Raccoon 2:9aaad52da789 38 void ChannelDecoding(uint8_t CodeWord);
Raccoon 2:9aaad52da789 39 void receve(void);
Raccoon 0:ee25c3c95837 40
Raccoon 0:ee25c3c95837 41 /*ステッピングモーター*/
Raccoon 0:ee25c3c95837 42 void stepper(void)
Raccoon 0:ee25c3c95837 43 {
Raccoon 0:ee25c3c95837 44 static unsigned int cnt = 0;
Raccoon 0:ee25c3c95837 45 static int i = 1;
Raccoon 0:ee25c3c95837 46 static unsigned int delay = 1000;
Raccoon 0:ee25c3c95837 47
Raccoon 1:b6a142877de4 48 delay = (255 - data[0]) + 10;
Raccoon 0:ee25c3c95837 49
Raccoon 0:ee25c3c95837 50 if((++cnt > delay) && data[0]!=0)
Raccoon 0:ee25c3c95837 51 {
Raccoon 0:ee25c3c95837 52 AENBL = 1;
Raccoon 0:ee25c3c95837 53 BENBL = 1;
Raccoon 0:ee25c3c95837 54 switch(i)
Raccoon 0:ee25c3c95837 55 {
Raccoon 0:ee25c3c95837 56 case 1:
Raccoon 0:ee25c3c95837 57 APHASE = 1;
Raccoon 0:ee25c3c95837 58 break;
Raccoon 0:ee25c3c95837 59 case 2:
Raccoon 0:ee25c3c95837 60 BPHASE = 1;
Raccoon 0:ee25c3c95837 61 break;
Raccoon 0:ee25c3c95837 62 case 3:
Raccoon 0:ee25c3c95837 63 APHASE = 0;
Raccoon 0:ee25c3c95837 64 break;
Raccoon 0:ee25c3c95837 65 case 4:
Raccoon 0:ee25c3c95837 66 BPHASE = 0;
Raccoon 0:ee25c3c95837 67 break;
Raccoon 0:ee25c3c95837 68 }
Raccoon 0:ee25c3c95837 69
Raccoon 0:ee25c3c95837 70 if(++i==5)i = 1;
Raccoon 0:ee25c3c95837 71 cnt = 0;
Raccoon 0:ee25c3c95837 72 }
Raccoon 0:ee25c3c95837 73 else if(data[0]==0)
Raccoon 0:ee25c3c95837 74 {
Raccoon 0:ee25c3c95837 75 AENBL = 0;
Raccoon 0:ee25c3c95837 76 BENBL = 0;
Raccoon 1:b6a142877de4 77 }
Raccoon 0:ee25c3c95837 78 }
Raccoon 0:ee25c3c95837 79
Raccoon 0:ee25c3c95837 80 /*spiでの書き込み用関数*/
Raccoon 2:9aaad52da789 81 void spi_write(int config, int mask, int value)
Raccoon 0:ee25c3c95837 82 {
Raccoon 0:ee25c3c95837 83 unsigned int spi_data;
Raccoon 0:ee25c3c95837 84 spi_data = config | (mask & value);
Raccoon 0:ee25c3c95837 85 CS = 0;
Raccoon 0:ee25c3c95837 86 LDAC = 1;
Raccoon 0:ee25c3c95837 87 spi.write(spi_data);
Raccoon 0:ee25c3c95837 88 CS = 1;
Raccoon 0:ee25c3c95837 89 LDAC = 0;
Raccoon 0:ee25c3c95837 90 }
Raccoon 0:ee25c3c95837 91
Raccoon 0:ee25c3c95837 92 /*SPI通信でMCP4922(DAC)を動かすための関数*/
Raccoon 2:9aaad52da789 93 void LightBulb(void)
Raccoon 2:9aaad52da789 94 {
Raccoon 2:9aaad52da789 95 static int mask = 0b0000111111111111;
Raccoon 2:9aaad52da789 96 int config;
Raccoon 2:9aaad52da789 97 int value;
Raccoon 0:ee25c3c95837 98
Raccoon 2:9aaad52da789 99 static int EX_MAX = 2300;
Raccoon 2:9aaad52da789 100
Raccoon 2:9aaad52da789 101 config = 0b0111000000000000;
Raccoon 2:9aaad52da789 102 value = (data[1] * EX_MAX) * 0.0039215f;
Raccoon 2:9aaad52da789 103 spi_write(config, mask, value);
Raccoon 0:ee25c3c95837 104 }
Raccoon 0:ee25c3c95837 105
Raccoon 2:9aaad52da789 106 /*室内灯(暖色系の色)*/
Raccoon 2:9aaad52da789 107 void DomeLamp(float val)
Raccoon 0:ee25c3c95837 108 {
Raccoon 1:b6a142877de4 109 R_led = val;
Raccoon 1:b6a142877de4 110 G_led = 0.38f*val;
Raccoon 2:9aaad52da789 111 B_led = 0.04f*val;
Raccoon 2:9aaad52da789 112 }
Raccoon 2:9aaad52da789 113
Raccoon 0:ee25c3c95837 114 /*RGB_LEDをダイナミック点灯するための関数*/
Raccoon 2:9aaad52da789 115 void RGB_LED(void)
Raccoon 2:9aaad52da789 116 {
Raccoon 0:ee25c3c95837 117 static uint8_t dig;
Raccoon 0:ee25c3c95837 118 uint8_t binary = 0b00001;
Raccoon 1:b6a142877de4 119
Raccoon 2:9aaad52da789 120 DomeLamp((float)data[3]*0.0039215f);
Raccoon 2:9aaad52da789 121
Raccoon 0:ee25c3c95837 122 signal = binary << dig;
Raccoon 0:ee25c3c95837 123 if(++dig == 3)dig = 0;
Raccoon 0:ee25c3c95837 124 }
Raccoon 0:ee25c3c95837 125
Raccoon 2:9aaad52da789 126 /*配列をシフトする*/
Raccoon 2:9aaad52da789 127 void ArrayShift_3(uint8_t array[3])
Raccoon 1:b6a142877de4 128 {
Raccoon 2:9aaad52da789 129 array[2] = array[1];
Raccoon 2:9aaad52da789 130 array[1] = array[0];
Raccoon 1:b6a142877de4 131 }
Raccoon 1:b6a142877de4 132
Raccoon 2:9aaad52da789 133 /*メディアンフィルター(要素数3の配列)*/
Raccoon 2:9aaad52da789 134 uint8_t MedianFilter_3(uint8_t src[3])
Raccoon 1:b6a142877de4 135 {
Raccoon 2:9aaad52da789 136 if(src[0]>=src[1])
Raccoon 1:b6a142877de4 137 {
Raccoon 2:9aaad52da789 138 if(src[1]>=src[2])return src[1];
Raccoon 2:9aaad52da789 139 else if(src[0]<=src[2])return src[0];
Raccoon 2:9aaad52da789 140 else return src[2];
Raccoon 1:b6a142877de4 141 }
Raccoon 2:9aaad52da789 142 else if(src[0]>src[2])return src[0];
Raccoon 2:9aaad52da789 143 else if(src[1]>src[2])return src[2];
Raccoon 2:9aaad52da789 144 else return src[1];
Raccoon 2:9aaad52da789 145 }
Raccoon 2:9aaad52da789 146
Raccoon 2:9aaad52da789 147 /*復号化*/
Raccoon 2:9aaad52da789 148 void ChannelDecoding(uint8_t CodeWord)
Raccoon 2:9aaad52da789 149 {
Raccoon 2:9aaad52da789 150 int index;
Raccoon 2:9aaad52da789 151 index = CodeWord & 0b00000111;
Raccoon 1:b6a142877de4 152
Raccoon 2:9aaad52da789 153 if(index<MAX)
Raccoon 1:b6a142877de4 154 {
Raccoon 2:9aaad52da789 155 tmp_data[index][0] = (CodeWord >> 3)*8.226f;
Raccoon 2:9aaad52da789 156 /*ノイズ除去*/
Raccoon 2:9aaad52da789 157 data[index] = MedianFilter_3(tmp_data[index]);
Raccoon 2:9aaad52da789 158 ArrayShift_3(tmp_data[index]);
Raccoon 1:b6a142877de4 159 }
Raccoon 2:9aaad52da789 160 }
Raccoon 1:b6a142877de4 161
Raccoon 0:ee25c3c95837 162 /*シリアル通信受信(xbee)*/
Raccoon 0:ee25c3c95837 163 void receve(void)
Raccoon 0:ee25c3c95837 164 {
Raccoon 2:9aaad52da789 165 ChannelDecoding(xbee.getc());
Raccoon 2:9aaad52da789 166 xbee.putc(1);
Raccoon 0:ee25c3c95837 167 }
Raccoon 0:ee25c3c95837 168
Raccoon 0:ee25c3c95837 169 int main(){
Raccoon 0:ee25c3c95837 170 /*ステッピングモーター*/
Raccoon 1:b6a142877de4 171 AENBL = 0;
Raccoon 0:ee25c3c95837 172 APHASE = 0;
Raccoon 1:b6a142877de4 173 BENBL = 0;
Raccoon 0:ee25c3c95837 174 BPHASE = 0;
Raccoon 2:9aaad52da789 175 step_timer.attach_us(&stepper, 100);
Raccoon 0:ee25c3c95837 176
Raccoon 0:ee25c3c95837 177 /*spi通信(MCP4922)*/
Raccoon 0:ee25c3c95837 178 CS = 1;
Raccoon 0:ee25c3c95837 179 LDAC = 1;
Raccoon 0:ee25c3c95837 180 spi.format(16, 0);
Raccoon 0:ee25c3c95837 181 spi.frequency(1000000);
Raccoon 2:9aaad52da789 182 spi_timer.attach_us(&LightBulb, 50000);
Raccoon 0:ee25c3c95837 183
Raccoon 0:ee25c3c95837 184 /*RGB_LED_3*/
Raccoon 2:9aaad52da789 185 R_led.period(0.00004);
Raccoon 2:9aaad52da789 186 G_led.period(0.00004);
Raccoon 2:9aaad52da789 187 B_led.period(0.00004);
Raccoon 2:9aaad52da789 188 rgb_timer.attach_us(&RGB_LED, 4000);
Raccoon 0:ee25c3c95837 189
Raccoon 0:ee25c3c95837 190 /*シリアル通信(xbee)*/
Raccoon 0:ee25c3c95837 191 xbee.baud(9600);
Raccoon 2:9aaad52da789 192 xbee.format(8, Serial::None, 1);
Raccoon 0:ee25c3c95837 193 xbee.attach(&receve, Serial::RxIrq);//受信割り込み
Raccoon 0:ee25c3c95837 194
Raccoon 1:b6a142877de4 195 while(1);
Raccoon 0:ee25c3c95837 196 }