コントローラーのプログラム

Dependencies:   mbed

/media/uploads/Raccoon/controller.jpg

Revision:
1:a213bf47cc4d
Parent:
0:a17db953c0b2
Child:
2:6383151eb64b
diff -r a17db953c0b2 -r a213bf47cc4d main.cpp
--- a/main.cpp	Thu Dec 21 13:03:34 2017 +0000
+++ b/main.cpp	Tue Dec 26 01:25:07 2017 +0000
@@ -1,6 +1,6 @@
 #include "mbed.h"
 #define MAX 4
- 
+
 Serial xbee(PA_9, PA_10); // tx, rx
 AnalogIn ain_0(PA_5);
 AnalogIn ain_1(PA_6);
@@ -15,81 +15,51 @@
 
 Ticker adc_timer;
 Ticker Tr_timer;
-Ticker led_timer;
-char data[MAX] = {0};
-unsigned int sw = 0;
-volatile int RECEVED = 0;
+uint8_t data[MAX];
 
-void transmit(void)
+/*情報源符号化(data-5bit, index-3bitにして符号化)*/
+uint8_t ChannelCoding(float data_f, int index)
 {
-    if(sw == 0)
-    {
-        xbee.putc('R');
-    }
-    else if(1 <= sw && sw <= MAX)
-    { 
-        xbee.putc(data[sw-1]);
-    }
+    uint8_t tmp;
+    tmp = data_f * 31;
+    return ((tmp << 3) | index);
 }
 
-void switching(void)
+/*送信*/
+void transmit(void)
 {
-    if(sw == 0)
-    {
-        sw = 1;
-    }
-    else if(sw > 0)
-    {
-        if(++sw > MAX){
-            sw = 0;
-        }
-    }
+    static int i = 0;
+    xbee.putc(data[i]);
+    if(++i >= MAX)i=0;
 }
 
+/*受信*/
 void receve(void)
 {
-    RECEVED = 1;
+    myled = 1;
 }
     
 void adcRead(void){
     /*ステッピングモーター*/
-    data[0] = ain_0 * 255;
-    
+    data[0] = ChannelCoding(ain_0, 0);
     /*EX球*/
-    data[1] = ain_1 * 255;
-    
-    /*天の川*/
-    data[2] = ain_2 * 255;
+    data[1] = ChannelCoding(ain_1, 1);
+    /*室内灯*/
+    data[2] = ChannelCoding(ain_2, 2);
     
-    data[3] = ain_3 * 255;
-}
-
-void led_off(void)
-{
-    static int cnt = 0;
-    if(RECEVED == 1)
-    {
-        myled = 1;
-        RECEVED = 0;
-        cnt = 0;
-    }
-    else if(++cnt > 50)
-    {
-        myled = 0;
-        RECEVED = 0;
-    }
+    data[3] = ChannelCoding(ain_3, 3);
 }
 
 int main() {
+    for(int i=0; i<MAX; i++)data[i] = ChannelCoding(0.0f, i);//配列dataを初期化
+    myled = 0;
     sw_0.mode(PullUp);
     sw_1.mode(PullUp);
     sw_2.mode(PullUp);
     xbee.baud(9600);
     xbee.format(8, Serial::None, 1);
-    xbee.attach(&switching, Serial::TxIrq);//送信完了時に割り込み
     xbee.attach(&receve, Serial::RxIrq);//受信割り込み
-    Tr_timer.attach_us(&transmit, 30000);//送信
+    Tr_timer.attach_us(&transmit, 20000);//送信
     adc_timer.attach_us(&adcRead, 20000);//ADC用
-    led_timer.attach_us(&led_off, 10000);//通信確認led用
     while(1)__WFI();
 }
\ No newline at end of file