Ryota Маэда
/
AstroC_planetarium_master
投影機本体のプログラム
Fork of AstroC_planetarium_master by
main.cpp@1:b6a142877de4, 2017-12-21 (annotated)
- Committer:
- Raccoon
- Date:
- Thu Dec 21 13:02:23 2017 +0000
- Revision:
- 1:b6a142877de4
- Parent:
- 0:ee25c3c95837
- Child:
- 2:9aaad52da789
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Raccoon | 0:ee25c3c95837 | 1 | #include "mbed.h" |
Raccoon | 1:b6a142877de4 | 2 | #define STEPPER_DELTA_T 150 |
Raccoon | 1:b6a142877de4 | 3 | #define k 5 |
Raccoon | 1:b6a142877de4 | 4 | #define MAX 4 |
Raccoon | 0:ee25c3c95837 | 5 | |
Raccoon | 0:ee25c3c95837 | 6 | /*ステッピングモーター*/ |
Raccoon | 0:ee25c3c95837 | 7 | Ticker step_timer; |
Raccoon | 0:ee25c3c95837 | 8 | DigitalOut APHASE(p5); |
Raccoon | 0:ee25c3c95837 | 9 | DigitalOut AENBL(p6); |
Raccoon | 0:ee25c3c95837 | 10 | DigitalOut BPHASE(p7); |
Raccoon | 0:ee25c3c95837 | 11 | DigitalOut BENBL(p8); |
Raccoon | 0:ee25c3c95837 | 12 | |
Raccoon | 0:ee25c3c95837 | 13 | /*MCP4922*/ |
Raccoon | 0:ee25c3c95837 | 14 | Ticker spi_timer; |
Raccoon | 0:ee25c3c95837 | 15 | SPI spi(p11, p12, p13);//mosi(SDO), miso, sclk |
Raccoon | 0:ee25c3c95837 | 16 | DigitalOut CS(p14); |
Raccoon | 0:ee25c3c95837 | 17 | DigitalOut LDAC(p17); |
Raccoon | 0:ee25c3c95837 | 18 | |
Raccoon | 0:ee25c3c95837 | 19 | /*RGB_LED_3*/ |
Raccoon | 0:ee25c3c95837 | 20 | Ticker rgb_timer; |
Raccoon | 0:ee25c3c95837 | 21 | BusOut signal(p24, p25, p26); |
Raccoon | 1:b6a142877de4 | 22 | PwmOut R_led(p22); |
Raccoon | 1:b6a142877de4 | 23 | PwmOut G_led(p21); |
Raccoon | 1:b6a142877de4 | 24 | PwmOut B_led(p23); |
Raccoon | 0:ee25c3c95837 | 25 | |
Raccoon | 0:ee25c3c95837 | 26 | /*シリアル通信(xbee)*/ |
Raccoon | 0:ee25c3c95837 | 27 | Serial xbee(p28, p27); // tx, rx |
Raccoon | 0:ee25c3c95837 | 28 | |
Raccoon | 0:ee25c3c95837 | 29 | /*受信データ格納*/ |
Raccoon | 1:b6a142877de4 | 30 | unsigned char data[MAX] = {0}; |
Raccoon | 1:b6a142877de4 | 31 | unsigned char tmp_data[MAX][k] ={0}; |
Raccoon | 0:ee25c3c95837 | 32 | |
Raccoon | 0:ee25c3c95837 | 33 | /*ステッピングモーター*/ |
Raccoon | 0:ee25c3c95837 | 34 | void stepper(void) |
Raccoon | 0:ee25c3c95837 | 35 | { |
Raccoon | 0:ee25c3c95837 | 36 | static unsigned int cnt = 0; |
Raccoon | 0:ee25c3c95837 | 37 | static int i = 1; |
Raccoon | 0:ee25c3c95837 | 38 | static unsigned int delay = 1000; |
Raccoon | 0:ee25c3c95837 | 39 | |
Raccoon | 0:ee25c3c95837 | 40 | /*速度制限:MAX周期1500[us](脱調防止)*/ |
Raccoon | 1:b6a142877de4 | 41 | delay = (255 - data[0]) + 10; |
Raccoon | 0:ee25c3c95837 | 42 | |
Raccoon | 0:ee25c3c95837 | 43 | /*ステッピングモーターの制御*/ |
Raccoon | 0:ee25c3c95837 | 44 | if((++cnt > delay) && data[0]!=0) |
Raccoon | 0:ee25c3c95837 | 45 | { |
Raccoon | 0:ee25c3c95837 | 46 | AENBL = 1; |
Raccoon | 0:ee25c3c95837 | 47 | BENBL = 1; |
Raccoon | 0:ee25c3c95837 | 48 | switch(i) |
Raccoon | 0:ee25c3c95837 | 49 | { |
Raccoon | 0:ee25c3c95837 | 50 | case 1: |
Raccoon | 0:ee25c3c95837 | 51 | APHASE = 1; |
Raccoon | 0:ee25c3c95837 | 52 | break; |
Raccoon | 0:ee25c3c95837 | 53 | case 2: |
Raccoon | 0:ee25c3c95837 | 54 | BPHASE = 1; |
Raccoon | 0:ee25c3c95837 | 55 | break; |
Raccoon | 0:ee25c3c95837 | 56 | case 3: |
Raccoon | 0:ee25c3c95837 | 57 | APHASE = 0; |
Raccoon | 0:ee25c3c95837 | 58 | break; |
Raccoon | 0:ee25c3c95837 | 59 | case 4: |
Raccoon | 0:ee25c3c95837 | 60 | BPHASE = 0; |
Raccoon | 0:ee25c3c95837 | 61 | break; |
Raccoon | 0:ee25c3c95837 | 62 | } |
Raccoon | 0:ee25c3c95837 | 63 | |
Raccoon | 0:ee25c3c95837 | 64 | if(++i==5)i = 1; |
Raccoon | 0:ee25c3c95837 | 65 | cnt = 0; |
Raccoon | 0:ee25c3c95837 | 66 | } |
Raccoon | 0:ee25c3c95837 | 67 | else if(data[0]==0) |
Raccoon | 0:ee25c3c95837 | 68 | { |
Raccoon | 0:ee25c3c95837 | 69 | AENBL = 0; |
Raccoon | 0:ee25c3c95837 | 70 | BENBL = 0; |
Raccoon | 1:b6a142877de4 | 71 | } |
Raccoon | 0:ee25c3c95837 | 72 | } |
Raccoon | 0:ee25c3c95837 | 73 | |
Raccoon | 0:ee25c3c95837 | 74 | /*spiでの書き込み用関数*/ |
Raccoon | 0:ee25c3c95837 | 75 | void spi_write(unsigned int config, unsigned int mask, unsigned int value) |
Raccoon | 0:ee25c3c95837 | 76 | { |
Raccoon | 0:ee25c3c95837 | 77 | unsigned int spi_data; |
Raccoon | 0:ee25c3c95837 | 78 | spi_data = config | (mask & value); |
Raccoon | 0:ee25c3c95837 | 79 | CS = 0; |
Raccoon | 0:ee25c3c95837 | 80 | LDAC = 1; |
Raccoon | 0:ee25c3c95837 | 81 | spi.write(spi_data); |
Raccoon | 0:ee25c3c95837 | 82 | CS = 1; |
Raccoon | 0:ee25c3c95837 | 83 | LDAC = 0; |
Raccoon | 0:ee25c3c95837 | 84 | } |
Raccoon | 0:ee25c3c95837 | 85 | |
Raccoon | 0:ee25c3c95837 | 86 | /*SPI通信でMCP4922(DAC)を動かすための関数*/ |
Raccoon | 0:ee25c3c95837 | 87 | void light_bulb(void) |
Raccoon | 0:ee25c3c95837 | 88 | { |
Raccoon | 0:ee25c3c95837 | 89 | static unsigned char ss = 0; |
Raccoon | 0:ee25c3c95837 | 90 | |
Raccoon | 0:ee25c3c95837 | 91 | //MCP4922へ送信するデータ |
Raccoon | 0:ee25c3c95837 | 92 | static unsigned int mask = 0b0000111111111111; |
Raccoon | 0:ee25c3c95837 | 93 | unsigned int config; |
Raccoon | 0:ee25c3c95837 | 94 | unsigned int value; |
Raccoon | 0:ee25c3c95837 | 95 | |
Raccoon | 1:b6a142877de4 | 96 | static unsigned int EX_MAX = 2300;//2.0[V] |
Raccoon | 0:ee25c3c95837 | 97 | static unsigned int Milky_MAX = 3150;//2.5[V] |
Raccoon | 0:ee25c3c95837 | 98 | |
Raccoon | 1:b6a142877de4 | 99 | if(ss==0)//減らせる |
Raccoon | 0:ee25c3c95837 | 100 | { |
Raccoon | 0:ee25c3c95837 | 101 | /*EX球(2V2A)用*/ |
Raccoon | 0:ee25c3c95837 | 102 | config = 0b0111000000000000; |
Raccoon | 1:b6a142877de4 | 103 | value = (data[1] * EX_MAX) * 0.0039215f; |
Raccoon | 0:ee25c3c95837 | 104 | spi_write(config, mask, value); |
Raccoon | 0:ee25c3c95837 | 105 | ss = 1; |
Raccoon | 0:ee25c3c95837 | 106 | } |
Raccoon | 1:b6a142877de4 | 107 | else if(ss==1)//減らせる |
Raccoon | 0:ee25c3c95837 | 108 | { |
Raccoon | 0:ee25c3c95837 | 109 | /*天の川(2.5V2A)用*/ |
Raccoon | 0:ee25c3c95837 | 110 | config = 0b1111000000000000; |
Raccoon | 1:b6a142877de4 | 111 | //value = (data[2] * Milky_MAX) / 255; |
Raccoon | 1:b6a142877de4 | 112 | value = (data[1] * EX_MAX) * 0.0039215f; |
Raccoon | 0:ee25c3c95837 | 113 | spi_write(config, mask, value); |
Raccoon | 0:ee25c3c95837 | 114 | ss = 0; |
Raccoon | 0:ee25c3c95837 | 115 | } |
Raccoon | 0:ee25c3c95837 | 116 | } |
Raccoon | 0:ee25c3c95837 | 117 | |
Raccoon | 1:b6a142877de4 | 118 | //light// |
Raccoon | 1:b6a142877de4 | 119 | void light(float val) |
Raccoon | 0:ee25c3c95837 | 120 | { |
Raccoon | 1:b6a142877de4 | 121 | R_led = val; |
Raccoon | 1:b6a142877de4 | 122 | G_led = 0.38f*val; |
Raccoon | 0:ee25c3c95837 | 123 | B_led = 0.0; |
Raccoon | 1:b6a142877de4 | 124 | } |
Raccoon | 0:ee25c3c95837 | 125 | |
Raccoon | 0:ee25c3c95837 | 126 | /*RGB_LEDをダイナミック点灯するための関数*/ |
Raccoon | 0:ee25c3c95837 | 127 | void RGB_LED(void){ |
Raccoon | 0:ee25c3c95837 | 128 | static uint8_t dig; |
Raccoon | 0:ee25c3c95837 | 129 | uint8_t binary = 0b00001; |
Raccoon | 1:b6a142877de4 | 130 | |
Raccoon | 1:b6a142877de4 | 131 | light((float)data[3]*0.0039215f); |
Raccoon | 1:b6a142877de4 | 132 | |
Raccoon | 0:ee25c3c95837 | 133 | signal = binary << dig; |
Raccoon | 1:b6a142877de4 | 134 | |
Raccoon | 0:ee25c3c95837 | 135 | if(++dig == 3)dig = 0; |
Raccoon | 0:ee25c3c95837 | 136 | } |
Raccoon | 0:ee25c3c95837 | 137 | |
Raccoon | 1:b6a142877de4 | 138 | void swap(unsigned char *x, unsigned char *y) |
Raccoon | 1:b6a142877de4 | 139 | { |
Raccoon | 1:b6a142877de4 | 140 | unsigned char tmp; |
Raccoon | 1:b6a142877de4 | 141 | tmp = *x; |
Raccoon | 1:b6a142877de4 | 142 | *x = *y; |
Raccoon | 1:b6a142877de4 | 143 | *y = tmp; |
Raccoon | 1:b6a142877de4 | 144 | } |
Raccoon | 1:b6a142877de4 | 145 | |
Raccoon | 1:b6a142877de4 | 146 | /*通信のスパイクノイズの除去*/ |
Raccoon | 1:b6a142877de4 | 147 | /*k(奇数) の値で参照する過去の値の数を変更する*/ |
Raccoon | 1:b6a142877de4 | 148 | unsigned char MedianFilter(unsigned char array[k]) |
Raccoon | 1:b6a142877de4 | 149 | { |
Raccoon | 1:b6a142877de4 | 150 | int i, j; |
Raccoon | 1:b6a142877de4 | 151 | static int index = (int)(k-1)/2; |
Raccoon | 1:b6a142877de4 | 152 | unsigned char tmp; |
Raccoon | 1:b6a142877de4 | 153 | unsigned char N[k]; |
Raccoon | 1:b6a142877de4 | 154 | |
Raccoon | 1:b6a142877de4 | 155 | /*copy*/ |
Raccoon | 1:b6a142877de4 | 156 | for(i=k-1; i>=0; i--) |
Raccoon | 1:b6a142877de4 | 157 | { |
Raccoon | 1:b6a142877de4 | 158 | N[i]=array[i]; |
Raccoon | 1:b6a142877de4 | 159 | if(i!=k-1)array[i+1] = array[i]; |
Raccoon | 1:b6a142877de4 | 160 | } |
Raccoon | 1:b6a142877de4 | 161 | |
Raccoon | 1:b6a142877de4 | 162 | for(i=0; i<index; i++) |
Raccoon | 1:b6a142877de4 | 163 | { |
Raccoon | 1:b6a142877de4 | 164 | for(j=k-1; i<j; j--) |
Raccoon | 1:b6a142877de4 | 165 | { |
Raccoon | 1:b6a142877de4 | 166 | if(N[j] < N[j-1]) |
Raccoon | 1:b6a142877de4 | 167 | { |
Raccoon | 1:b6a142877de4 | 168 | //swap(&N[j], &N[j-1]); |
Raccoon | 1:b6a142877de4 | 169 | tmp = N[j]; |
Raccoon | 1:b6a142877de4 | 170 | N[j] = N[j-1]; |
Raccoon | 1:b6a142877de4 | 171 | N[j-1] = tmp; |
Raccoon | 1:b6a142877de4 | 172 | } |
Raccoon | 1:b6a142877de4 | 173 | } |
Raccoon | 1:b6a142877de4 | 174 | } |
Raccoon | 1:b6a142877de4 | 175 | return N[index]; |
Raccoon | 1:b6a142877de4 | 176 | } |
Raccoon | 1:b6a142877de4 | 177 | |
Raccoon | 0:ee25c3c95837 | 178 | /*シリアル通信受信(xbee)*/ |
Raccoon | 0:ee25c3c95837 | 179 | void receve(void) |
Raccoon | 0:ee25c3c95837 | 180 | { |
Raccoon | 0:ee25c3c95837 | 181 | static int sw = 0; |
Raccoon | 0:ee25c3c95837 | 182 | /*送信側から送信開始の合図で'R'が送られてくる*/ |
Raccoon | 0:ee25c3c95837 | 183 | /*'R'を受信したら、データ受信開始*/ |
Raccoon | 0:ee25c3c95837 | 184 | /*5個のデータが順番に受信されるので、順番に配列に代入する*/ |
Raccoon | 0:ee25c3c95837 | 185 | if((sw == 0) && (xbee.getc() == 'R')) |
Raccoon | 0:ee25c3c95837 | 186 | { |
Raccoon | 0:ee25c3c95837 | 187 | sw = 1; |
Raccoon | 0:ee25c3c95837 | 188 | } |
Raccoon | 1:b6a142877de4 | 189 | else if(1 <= sw && sw <= MAX)//条件式減らせる |
Raccoon | 0:ee25c3c95837 | 190 | { |
Raccoon | 1:b6a142877de4 | 191 | //data[sw-1] = xbee.getc(); |
Raccoon | 1:b6a142877de4 | 192 | |
Raccoon | 1:b6a142877de4 | 193 | tmp_data[sw-1][0] = xbee.getc(); |
Raccoon | 1:b6a142877de4 | 194 | data[sw-1] = MedianFilter(tmp_data[sw-1]); |
Raccoon | 1:b6a142877de4 | 195 | |
Raccoon | 1:b6a142877de4 | 196 | if(++sw > MAX)sw = 0; |
Raccoon | 0:ee25c3c95837 | 197 | } |
Raccoon | 1:b6a142877de4 | 198 | xbee.putc(1);//受信確認 |
Raccoon | 0:ee25c3c95837 | 199 | } |
Raccoon | 0:ee25c3c95837 | 200 | |
Raccoon | 0:ee25c3c95837 | 201 | int main(){ |
Raccoon | 0:ee25c3c95837 | 202 | /*ステッピングモーター*/ |
Raccoon | 1:b6a142877de4 | 203 | AENBL = 0; |
Raccoon | 0:ee25c3c95837 | 204 | APHASE = 0; |
Raccoon | 1:b6a142877de4 | 205 | BENBL = 0; |
Raccoon | 0:ee25c3c95837 | 206 | BPHASE = 0; |
Raccoon | 0:ee25c3c95837 | 207 | step_timer.attach_us(&stepper, STEPPER_DELTA_T);//100[us],10000[Hz] |
Raccoon | 0:ee25c3c95837 | 208 | |
Raccoon | 0:ee25c3c95837 | 209 | /*spi通信(MCP4922)*/ |
Raccoon | 0:ee25c3c95837 | 210 | CS = 1; |
Raccoon | 0:ee25c3c95837 | 211 | LDAC = 1; |
Raccoon | 0:ee25c3c95837 | 212 | spi.format(16, 0); |
Raccoon | 0:ee25c3c95837 | 213 | spi.frequency(1000000); |
Raccoon | 1:b6a142877de4 | 214 | spi_timer.attach_us(&light_bulb, 50000); |
Raccoon | 0:ee25c3c95837 | 215 | |
Raccoon | 0:ee25c3c95837 | 216 | /*RGB_LED_3*/ |
Raccoon | 1:b6a142877de4 | 217 | R_led.period(0.000048);//0.00004 |
Raccoon | 1:b6a142877de4 | 218 | G_led.period(0.000048); |
Raccoon | 1:b6a142877de4 | 219 | B_led.period(0.000048); |
Raccoon | 1:b6a142877de4 | 220 | rgb_timer.attach_us(&RGB_LED, 4800);//4000 |
Raccoon | 0:ee25c3c95837 | 221 | |
Raccoon | 0:ee25c3c95837 | 222 | /*シリアル通信(xbee)*/ |
Raccoon | 0:ee25c3c95837 | 223 | xbee.baud(9600); |
Raccoon | 1:b6a142877de4 | 224 | xbee.format(8, Serial::None, 2); |
Raccoon | 0:ee25c3c95837 | 225 | xbee.attach(&receve, Serial::RxIrq);//受信割り込み |
Raccoon | 0:ee25c3c95837 | 226 | |
Raccoon | 1:b6a142877de4 | 227 | while(1); |
Raccoon | 0:ee25c3c95837 | 228 | } |