投影機本体のプログラム

Dependencies:   mbed

Fork of AstroC_planetarium_master by Ryota Маэда

/media/uploads/Raccoon/main.jpg

Committer:
Raccoon
Date:
Sat Sep 23 08:05:10 2017 +0000
Revision:
0:ee25c3c95837
Child:
1:b6a142877de4
planetarium_master;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Raccoon 0:ee25c3c95837 1 #include "mbed.h"
Raccoon 0:ee25c3c95837 2 #define STEPPER_DELTA_T 100
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 0:ee25c3c95837 20 PwmOut R_led(p23);
Raccoon 0:ee25c3c95837 21 PwmOut G_led(p22);
Raccoon 0:ee25c3c95837 22 PwmOut B_led(p21);
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 0:ee25c3c95837 28 char data[6] = {0};
Raccoon 0:ee25c3c95837 29
Raccoon 0:ee25c3c95837 30 /*ステッピングモーター*/
Raccoon 0:ee25c3c95837 31 void stepper(void)
Raccoon 0:ee25c3c95837 32 {
Raccoon 0:ee25c3c95837 33 static unsigned int cnt = 0;
Raccoon 0:ee25c3c95837 34 static int i = 1;
Raccoon 0:ee25c3c95837 35 static unsigned int delay = 1000;
Raccoon 0:ee25c3c95837 36
Raccoon 0:ee25c3c95837 37 /*速度制限:MAX周期1500[us](脱調防止)*/
Raccoon 0:ee25c3c95837 38 delay = (255 - data[0]) + 15;
Raccoon 0:ee25c3c95837 39
Raccoon 0:ee25c3c95837 40 /*ステッピングモーターの制御*/
Raccoon 0:ee25c3c95837 41 if((++cnt > delay) && data[0]!=0)
Raccoon 0:ee25c3c95837 42 {
Raccoon 0:ee25c3c95837 43 AENBL = 1;
Raccoon 0:ee25c3c95837 44 BENBL = 1;
Raccoon 0:ee25c3c95837 45 switch(i)
Raccoon 0:ee25c3c95837 46 {
Raccoon 0:ee25c3c95837 47 case 1:
Raccoon 0:ee25c3c95837 48 APHASE = 1;
Raccoon 0:ee25c3c95837 49 break;
Raccoon 0:ee25c3c95837 50 case 2:
Raccoon 0:ee25c3c95837 51 BPHASE = 1;
Raccoon 0:ee25c3c95837 52 break;
Raccoon 0:ee25c3c95837 53 case 3:
Raccoon 0:ee25c3c95837 54 APHASE = 0;
Raccoon 0:ee25c3c95837 55 break;
Raccoon 0:ee25c3c95837 56 case 4:
Raccoon 0:ee25c3c95837 57 BPHASE = 0;
Raccoon 0:ee25c3c95837 58 break;
Raccoon 0:ee25c3c95837 59 }
Raccoon 0:ee25c3c95837 60
Raccoon 0:ee25c3c95837 61 if(++i==5)i = 1;
Raccoon 0:ee25c3c95837 62 cnt = 0;
Raccoon 0:ee25c3c95837 63 }
Raccoon 0:ee25c3c95837 64 else if(data[0]==0)
Raccoon 0:ee25c3c95837 65 {
Raccoon 0:ee25c3c95837 66 AENBL = 0;
Raccoon 0:ee25c3c95837 67 BENBL = 0;
Raccoon 0:ee25c3c95837 68 }
Raccoon 0:ee25c3c95837 69 }
Raccoon 0:ee25c3c95837 70
Raccoon 0:ee25c3c95837 71 /*spiでの書き込み用関数*/
Raccoon 0:ee25c3c95837 72 void spi_write(unsigned int config, unsigned int mask, unsigned int value)
Raccoon 0:ee25c3c95837 73 {
Raccoon 0:ee25c3c95837 74 unsigned int spi_data;
Raccoon 0:ee25c3c95837 75 spi_data = config | (mask & value);
Raccoon 0:ee25c3c95837 76 CS = 0;
Raccoon 0:ee25c3c95837 77 LDAC = 1;
Raccoon 0:ee25c3c95837 78 spi.write(spi_data);
Raccoon 0:ee25c3c95837 79 CS = 1;
Raccoon 0:ee25c3c95837 80 LDAC = 0;
Raccoon 0:ee25c3c95837 81 }
Raccoon 0:ee25c3c95837 82
Raccoon 0:ee25c3c95837 83 /*SPI通信でMCP4922(DAC)を動かすための関数*/
Raccoon 0:ee25c3c95837 84 void light_bulb(void)
Raccoon 0:ee25c3c95837 85 {
Raccoon 0:ee25c3c95837 86 static unsigned char ss = 0;
Raccoon 0:ee25c3c95837 87
Raccoon 0:ee25c3c95837 88 //MCP4922へ送信するデータ
Raccoon 0:ee25c3c95837 89 static unsigned int mask = 0b0000111111111111;
Raccoon 0:ee25c3c95837 90 unsigned int config;
Raccoon 0:ee25c3c95837 91 unsigned int value;
Raccoon 0:ee25c3c95837 92
Raccoon 0:ee25c3c95837 93 static unsigned int EX_MAX = 2510;//2.0[V]
Raccoon 0:ee25c3c95837 94 static unsigned int Milky_MAX = 3150;//2.5[V]
Raccoon 0:ee25c3c95837 95
Raccoon 0:ee25c3c95837 96 /*前回の電圧の値と違う場合のみ実行される*/
Raccoon 0:ee25c3c95837 97 if(ss==0 /*&& data[1]!=old_data[0]*/)
Raccoon 0:ee25c3c95837 98 {
Raccoon 0:ee25c3c95837 99 /*EX球(2V2A)用*/
Raccoon 0:ee25c3c95837 100 config = 0b0111000000000000;
Raccoon 0:ee25c3c95837 101 value = (data[1] * EX_MAX) / 255;
Raccoon 0:ee25c3c95837 102 spi_write(config, mask, value);
Raccoon 0:ee25c3c95837 103 ss = 1;
Raccoon 0:ee25c3c95837 104 }
Raccoon 0:ee25c3c95837 105 else if(ss==1 /*&& data[2]!=old_data[1]*/)
Raccoon 0:ee25c3c95837 106 {
Raccoon 0:ee25c3c95837 107 /*天の川(2.5V2A)用*/
Raccoon 0:ee25c3c95837 108 config = 0b1111000000000000;
Raccoon 0:ee25c3c95837 109 value = (data[2] * Milky_MAX) / 255;
Raccoon 0:ee25c3c95837 110 spi_write(config, mask, value);
Raccoon 0:ee25c3c95837 111 ss = 0;
Raccoon 0:ee25c3c95837 112 }
Raccoon 0:ee25c3c95837 113 }
Raccoon 0:ee25c3c95837 114
Raccoon 0:ee25c3c95837 115 /*Hueの値deg(0~180)を受け取るとRGBに変換して反映させる*/
Raccoon 0:ee25c3c95837 116 void HSB2RGB(float deg)
Raccoon 0:ee25c3c95837 117 {
Raccoon 0:ee25c3c95837 118 /*リセット*/
Raccoon 0:ee25c3c95837 119 R_led = 0.0;
Raccoon 0:ee25c3c95837 120 G_led = 0.0;
Raccoon 0:ee25c3c95837 121 B_led = 0.0;
Raccoon 0:ee25c3c95837 122
Raccoon 0:ee25c3c95837 123 if(0 < deg && deg <= 60)
Raccoon 0:ee25c3c95837 124 {
Raccoon 0:ee25c3c95837 125 R_led = 1.0 - (deg / 60);
Raccoon 0:ee25c3c95837 126 G_led = deg/60;
Raccoon 0:ee25c3c95837 127 B_led = 0.0;
Raccoon 0:ee25c3c95837 128 }
Raccoon 0:ee25c3c95837 129 else if(60 < deg && deg <= 120)
Raccoon 0:ee25c3c95837 130 {
Raccoon 0:ee25c3c95837 131 deg = deg - 60;
Raccoon 0:ee25c3c95837 132 R_led = 0.0;
Raccoon 0:ee25c3c95837 133 G_led = 1.0 - (deg / 60);
Raccoon 0:ee25c3c95837 134 B_led = deg/60;
Raccoon 0:ee25c3c95837 135 }
Raccoon 0:ee25c3c95837 136 else if(120 < deg && deg <= 180)
Raccoon 0:ee25c3c95837 137 {
Raccoon 0:ee25c3c95837 138 deg = deg - 120;
Raccoon 0:ee25c3c95837 139 R_led = deg/60;
Raccoon 0:ee25c3c95837 140 G_led = 0.0;
Raccoon 0:ee25c3c95837 141 B_led = 1.0 - (deg / 60);
Raccoon 0:ee25c3c95837 142 }
Raccoon 0:ee25c3c95837 143 }
Raccoon 0:ee25c3c95837 144
Raccoon 0:ee25c3c95837 145 /*RGB_LEDをダイナミック点灯するための関数*/
Raccoon 0:ee25c3c95837 146 void RGB_LED(void){
Raccoon 0:ee25c3c95837 147 static uint8_t dig;
Raccoon 0:ee25c3c95837 148 uint8_t binary = 0b00001;
Raccoon 0:ee25c3c95837 149 HSB2RGB((float)data[3]*180/255);
Raccoon 0:ee25c3c95837 150 /*switch(dig){
Raccoon 0:ee25c3c95837 151 case 0:
Raccoon 0:ee25c3c95837 152 signal = 0b00001;
Raccoon 0:ee25c3c95837 153 break;
Raccoon 0:ee25c3c95837 154 case 1:
Raccoon 0:ee25c3c95837 155 signal = 0b00010;
Raccoon 0:ee25c3c95837 156 break;
Raccoon 0:ee25c3c95837 157 case 2:
Raccoon 0:ee25c3c95837 158 signal = 0b00100;
Raccoon 0:ee25c3c95837 159 break;
Raccoon 0:ee25c3c95837 160 }*/
Raccoon 0:ee25c3c95837 161 signal = binary << dig;
Raccoon 0:ee25c3c95837 162 if(++dig == 3)dig = 0;
Raccoon 0:ee25c3c95837 163 }
Raccoon 0:ee25c3c95837 164
Raccoon 0:ee25c3c95837 165 /*シリアル通信受信(xbee)*/
Raccoon 0:ee25c3c95837 166 void receve(void)
Raccoon 0:ee25c3c95837 167 {
Raccoon 0:ee25c3c95837 168 static int sw = 0;
Raccoon 0:ee25c3c95837 169 /*送信側から送信開始の合図で'R'が送られてくる*/
Raccoon 0:ee25c3c95837 170 /*'R'を受信したら、データ受信開始*/
Raccoon 0:ee25c3c95837 171 /*5個のデータが順番に受信されるので、順番に配列に代入する*/
Raccoon 0:ee25c3c95837 172 if((sw == 0) && (xbee.getc() == 'R'))
Raccoon 0:ee25c3c95837 173 {
Raccoon 0:ee25c3c95837 174 sw = 1;
Raccoon 0:ee25c3c95837 175 }
Raccoon 0:ee25c3c95837 176 else if(1 <= sw && sw <= 6)
Raccoon 0:ee25c3c95837 177 {
Raccoon 0:ee25c3c95837 178 data[sw-1] = xbee.getc();
Raccoon 0:ee25c3c95837 179 if(++sw > 6)sw = 0;
Raccoon 0:ee25c3c95837 180 }
Raccoon 0:ee25c3c95837 181 }
Raccoon 0:ee25c3c95837 182
Raccoon 0:ee25c3c95837 183 int main(){
Raccoon 0:ee25c3c95837 184 /*ステッピングモーター*/
Raccoon 0:ee25c3c95837 185 AENBL = 1;
Raccoon 0:ee25c3c95837 186 APHASE = 0;
Raccoon 0:ee25c3c95837 187 BENBL = 1;
Raccoon 0:ee25c3c95837 188 BPHASE = 0;
Raccoon 0:ee25c3c95837 189 step_timer.attach_us(&stepper, STEPPER_DELTA_T);//100[us],10000[Hz]
Raccoon 0:ee25c3c95837 190
Raccoon 0:ee25c3c95837 191 /*spi通信(MCP4922)*/
Raccoon 0:ee25c3c95837 192 CS = 1;
Raccoon 0:ee25c3c95837 193 LDAC = 1;
Raccoon 0:ee25c3c95837 194 spi.format(16, 0);
Raccoon 0:ee25c3c95837 195 spi.frequency(1000000);
Raccoon 0:ee25c3c95837 196 spi_timer.attach_us(&light_bulb, 10000);
Raccoon 0:ee25c3c95837 197
Raccoon 0:ee25c3c95837 198 /*RGB_LED_3*/
Raccoon 0:ee25c3c95837 199 R_led.period(0.00004);
Raccoon 0:ee25c3c95837 200 G_led.period(0.00004);
Raccoon 0:ee25c3c95837 201 B_led.period(0.00004);
Raccoon 0:ee25c3c95837 202 rgb_timer.attach_us(&RGB_LED, 5000);
Raccoon 0:ee25c3c95837 203
Raccoon 0:ee25c3c95837 204 /*シリアル通信(xbee)*/
Raccoon 0:ee25c3c95837 205 xbee.baud(9600);
Raccoon 0:ee25c3c95837 206 xbee.format(8, Serial::None, 1);
Raccoon 0:ee25c3c95837 207 xbee.attach(&receve, Serial::RxIrq);//受信割り込み
Raccoon 0:ee25c3c95837 208
Raccoon 0:ee25c3c95837 209 while(1)__WFI();
Raccoon 0:ee25c3c95837 210 }