test2017-1-13

Dependencies:   AT24C1024 DMX-STM32 mbed

Revision:
1:cf2f5992241c
Parent:
0:d94213e24c2e
Child:
2:198becff12f4
--- a/main.cpp	Fri Jan 13 03:28:16 2017 +0000
+++ b/main.cpp	Tue Feb 14 12:58:24 2017 +0000
@@ -1,59 +1,124 @@
-//#define
-
-
 #include "mbed.h"
 #include "AT24C1024.h"
 #include "AccelStepper.h"
 #include "DMX.h"
 
 Timer t;
-DigitalOut dmxEnable(D8);
+DigitalOut dmxEnable(D8);   //基本L(レシーブ)に固定します
 DMX dmx(PA_9, PA_10);
 
-AccelStepper stepper1(1, D9, D6); // step, direction
-AccelStepper stepper2(1, D10, D7); // step, direction
+I2C         i2c(D4, D5);      // SDA, SCL
+AT24C1024   at24c1024(i2c);     // Atmel 1Mbit EE-PROM
 
+AccelStepper stepper1(1, D9, D6);   // step, direction
+AccelStepper stepper2(1, D10, D7);  // step, direction
+
+//モーター1 CN0とCN1を使用
 DigitalOut stepper1Enable(D2);
 DigitalOut stepper1MS1(D11);
 DigitalOut stepper1MS2(D12);
 DigitalIn stepper1MS3(A4);
 
+//モーター2 CN2とCN3を使用
 DigitalOut stepper2Enable(D3);
 DigitalOut stepper2MS1(A5);
 DigitalOut stepper2MS2(A6);
 DigitalOut stepper2MS3(A7);
 
-DigitalIn EndSW4(A3); //CN7
-DigitalIn EndSW3(A2); //CN6
-DigitalIn EndSW2(A1); //CN5
-DigitalIn EndSW1(A0); //CN4
-DigitalOut myLED(LED3); //動作確認用LED
+DigitalIn EndSW1(A0); //CN4 //3端子エンドスイッチ1  予備
+DigitalIn EndSW2(A1); //CN5 //3端子エンドスイッチ2  予備
+DigitalIn EndSW3(A2); //CN6 //2端子エンドスイッチ1 モーター1用にする
+DigitalIn EndSW4(A3); //CN7 //2端子エンドスイッチ2 モーター2用にする
+
+DigitalOut myLED(LED3);     //動作確認用LED
+
+unsigned long position1, position2; //モーター1の設定位置
+unsigned long max1, max2;
+unsigned long speed1, speed2;
+unsigned long Acceleration1, Acceleration2;
+unsigned long param1, param2;
+unsigned long dmxInHighBit, dmxInLowBit;
+
+bool manipulateMode1 = true; //0:手動 1:自動
+bool manipulateMode2 = true; //0:手動 1:自動
+
+
+//モーター1のパラメーター DMXチャンネルリスト チャンネル数:12個
+#define CH_Origin 0
 
-long dt;
-unsigned int dmxInHighBit;
-unsigned int dmxInLowBit;
-int pastPosition;
+#define DMX_Position1_H CH_Origin  //モーター1制御用のDMX信号の始まりの値
+#define DMX_Position1_L CH_Origin + 1
+#define DMX_Speed1_H CH_Origin + 2
+#define DMX_Speed1_L CH_Origin + 3
+#define DMX_Acceleration1_H CH_Origin + 4
+#define DMX_Acceleration1_L CH_Origin + 5
+#define DMX_Max1_H CH_Origin + 6
+#define DMX_Max1_L CH_Origin + 7
+#define DMX_Offset1_H CH_Origin + 8
+#define DMX_Offset1_L CH_Origin + 9
+#define DMX_SaveValue1_H CH_Origin + 10
+#define DMX_SaveValue1_L CH_Origin + 11
+#define DMX_ParmSetMode1_H CH_Origin + 12
+#define DMX_ParmSetMode1_L CH_Origin + 13
 
-//map関数です
+//モーター2のパラメーター DMXチャンネルリスト チャンネル数:12個
+#define DMX_Position2_H CH_Origin + 16
+#define DMX_Position2_L CH_Origin + 17
+#define DMX_Speed2_H CH_Origin + 18
+#define DMX_Speed2_L CH_Origin + 19
+#define DMX_Acceleration2_H CH_Origin + 20
+#define DMX_Acceleration2_L CH_Origin + 21
+#define DMX_Max2_H CH_Origin + 22
+#define DMX_Max2_L CH_Origin + 23
+#define DMX_Offset2_H CH_Origin + 24
+#define DMX_Offset2_L CH_Origin + 25
+#define DMX_SaveValue2_H CH_Origin + 26
+#define DMX_SaveValue2_L CH_Origin + 27
+#define DMX_ParmSetMode2_H CH_Origin + 28
+#define DMX_ParmSetMode2_L CH_Origin + 29
+
+//map
 long map(long x, long in_min, long in_max, long out_min, long out_max)
 {
     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
 }
 
+void blink()
+{
+    for(int i=0; i<5; i++) {
+        myLED=0;
+        wait_ms(60);
+        myLED=1;
+        wait_ms(60);
+    }
+}
+
 void checkSW()
 {
-    if(EndSW4.read() == 0) myLED = 1;
+    //モーター1用エンドスイッチのチェック
+    if(EndSW3.read() == 0) myLED = 1;
     else {
         stepper1.stop();
         stepper1.setCurrentPosition(0);
         myLED =0;
     }
+
+    //モーター2用エンドスイッチのチェック
+    if(EndSW4.read() == 0) myLED = 1;
+    else {
+        stepper2.stop();
+        stepper2.setCurrentPosition(0);
+        myLED =0;
+    }
 }
 
 void silence()
 {
     DigitalIn stepper1MS1(D11);
     DigitalIn stepper1MS2(D12);
+
+    DigitalIn stepper2MS1(A5);
+    DigitalIn stepper2MS2(A6);
 }
 
 void run()
@@ -62,6 +127,11 @@
     DigitalOut stepper1MS2(D12);
     stepper1MS1 = 1;
     stepper1MS2 = 1;
+
+    DigitalOut stepper2MS1(A5);
+    DigitalOut stepper2MS2(A6);
+    stepper2MS1 = 1;
+    stepper2MS2 = 1;
 }
 
 void init()
@@ -78,26 +148,194 @@
     while(stepper1.distanceToGo() != 0) {
         stepper1.run();
     }
-    silence();
+
+    stepper2.setMaxSpeed(2000);
+    stepper2.setAcceleration(20000);
+    stepper2.moveTo(-16000);
+
+    while(stepper2.currentPosition() != 0) {
+        stepper2.run();
+        checkSW();
+    }
+    stepper2.moveTo(500);
+    while(stepper2.distanceToGo() != 0) {
+        stepper2.run();
+    }
+    //silence();
+}
+
+void checkSafeBreak()
+{
+    if(EndSW3.read() == 0) myLED = 1;
+    else {
+        stepper1.stop();
+        while(1) {
+            blink();
+        }
+    }
+
+    //モーター2用エンドスイッチのチェック
+    if(EndSW4.read() == 0) myLED = 1;
+    else {
+        stepper2.stop();
+        while(1) {
+            blink();
+        }
+    }
+}
+
+//モーター1のパラメータ保存
+void paramStore1()
+{
+    //速度
+    uint8_t dt = dmx.get(2);
+    at24c1024.write(2, dt);
+    dt = dmx.get(3);
+    at24c1024.write(3, dt);
+    at24c1024.write(2, 0xf5);
+    wait_ms(2);
+
+    //加速度
+    dt = dmx.get(DMX_Acceleration1_H);
+    at24c1024.write(DMX_Acceleration1_H, dt);
+    dt = dmx.get(DMX_Acceleration1_L);
+    at24c1024.write(DMX_Acceleration1_L, dt);
+
+    //最大値
+    dt = dmx.get(DMX_Max1_H);
+    at24c1024.write(DMX_Max1_H, dt);
+    dt = dmx.get(DMX_Max1_L);
+    at24c1024.write(DMX_Max1_L, dt);
+
+    wait_ms(5);
+}
+
+//速度・加速度・最大値・オフセットの値をスライダーから取得
+void captureSliderValue()
+{
+    dmxInHighBit = dmx.get(DMX_Speed1_H);
+    dmxInLowBit = dmx.get(DMX_Speed1_L);
+    dmxInHighBit = dmxInHighBit << 8;
+    speed1 = (long)(dmxInHighBit + dmxInLowBit);
+
+    dmxInHighBit = dmx.get(DMX_Speed2_H);
+    dmxInLowBit = dmx.get(DMX_Speed2_L);
+    dmxInHighBit = dmxInHighBit << 8;
+    speed2 = (long)(dmxInHighBit + dmxInLowBit);
+
+    dmxInHighBit = dmx.get(DMX_Acceleration1_H);
+    dmxInLowBit = dmx.get(DMX_Acceleration1_L);
+    dmxInHighBit = dmxInHighBit << 8;
+    Acceleration1 = (long)(dmxInHighBit + dmxInLowBit);
+
+    dmxInHighBit = dmx.get(DMX_Acceleration2_H);
+    dmxInLowBit = dmx.get(DMX_Acceleration2_L);
+    dmxInHighBit = dmxInHighBit << 8;
+    Acceleration2 = (long)(dmxInHighBit + dmxInLowBit);
+
+    dmxInHighBit = dmx.get(DMX_Max1_H);
+    dmxInLowBit = dmx.get(DMX_Max1_L);
+    dmxInHighBit = dmxInHighBit << 8;
+    max1 = (long)(dmxInHighBit + dmxInLowBit);
+
+    dmxInHighBit = dmx.get(DMX_Max2_H);
+    dmxInLowBit = dmx.get(DMX_Max2_L);
+    dmxInHighBit = dmxInHighBit << 8;
+    max2 = (long)(dmxInHighBit + dmxInLowBit);
+}
 
 
+//モーター1の 速度・加速度・オフセットの値をEEPROMから取得
+void readROMValue1()
+{
+    //速度
+    unsigned long HighBit = at24c1024.read(DMX_Speed1_H);
+    uint8_t LowBit = at24c1024.read(DMX_Speed1_L);
+    HighBit = HighBit << 8;
+    speed1 = (unsigned long)(HighBit + LowBit);
+
+
+    //加速度
+    HighBit = at24c1024.read(DMX_Acceleration1_H);
+    LowBit = at24c1024.read(DMX_Acceleration1_L);
+    HighBit = HighBit << 8;
+    Acceleration1 = (unsigned long)(HighBit + LowBit);
+
+    //最大値
+    HighBit = at24c1024.read(DMX_Max1_H);
+    LowBit = at24c1024.read(DMX_Max1_L);
+    HighBit = HighBit << 8;
+    max1 = (unsigned long)(HighBit + LowBit);
+}
+
+
+//モーター2の 速度・加速度・オフセットの値をEEPROMから取得
+void readROMValue2()
+{
 }
 
 
 
+//操作モード切り替えボタンのチェック
+void checkManipulateButton()
+{
+    dmxInHighBit = dmx.get(DMX_ParmSetMode1_H);
+    dmxInLowBit = dmx.get(DMX_ParmSetMode1_L);
+    if(dmxInHighBit == 102 && dmxInLowBit == 255) {
+        manipulateMode1 = !manipulateMode1;
+        blink();
+    }
+
+    dmxInHighBit = dmx.get(DMX_ParmSetMode2_H);
+    dmxInLowBit = dmx.get(DMX_ParmSetMode2_L);
+    if(dmxInHighBit == 102 && dmxInLowBit == 255) {
+        manipulateMode2 = !manipulateMode2;
+        blink();
+    }
+}
+
+//書き込みボタンのチェック
+void checkWriteButton()
+{
+    dmxInHighBit = dmx.get(DMX_SaveValue1_H);
+    dmxInLowBit = dmx.get(DMX_SaveValue1_L);
+    dmxInHighBit = dmxInHighBit << 8;
+    param1 = (long)(dmxInHighBit + dmxInLowBit);
+
+    dmxInHighBit = dmx.get(DMX_SaveValue2_H);
+    dmxInLowBit = dmx.get(DMX_SaveValue2_L);
+    dmxInHighBit = dmxInHighBit << 8;
+    param2 = (long)(dmxInHighBit + dmxInLowBit);
+
+    if(param1 == 24932) {
+        paramStore1();
+        blink();
+    } else;
+
+    if(param2 == 24932) {
+        //paramStore2();
+        //blink();
+    } else;
+}
+
+
 int main()
 {
-
+    //モーター1と2のコンフィギュレーション
     stepper1MS1 = 1;
     stepper1MS2 = 1;
-    //stepper1MS3 = 0;
     stepper1Enable = 0;
 
+    stepper2MS1 = 1;
+    stepper2MS2 = 1;
+    stepper2Enable = 0;
+
     t.start();
 
     dmxEnable = 0;  //0:receiver 1:sender
 
     stepper1.setCurrentPosition(-1);
+    stepper2.setCurrentPosition(-1);
 
     init();
 
@@ -105,100 +343,70 @@
     stepper1.setAcceleration(30000);
     stepper1.setCurrentPosition(0);
 
-    //run();
+    stepper2.setMaxSpeed(25000);
+    stepper2.setAcceleration(30000);
+    stepper2.setCurrentPosition(0);
+
+    myLED =1;
+
+    at24c1024.write(2, 0xf5);
+    wait_ms(2);
+
 
     while(1) {
 
-        
-        dmxInHighBit = dmx.get(0);
-        dmxInLowBit = dmx.get(1);
-        dmxInHighBit = dmxInHighBit << 8;
-        dt = (long)(dmxInHighBit + dmxInLowBit);
-        //if(unsigned (stepper1.distanceToGo()) > 0)
-//            run();
-//        else
-//            silence();
-        dt = map(dt, 0, 0xFFFF, 0, 32000); 
-        //dt = dt*100;
+        //操作モード切り替えボタンのチェック
+        checkManipulateButton();
+
+        //書き込みボタンチェック
+        checkWriteButton();
 
-        //printf("%d\n", dt);
-        //wait(1);
-        stepper1.moveTo(dt);//
-//        if(unsigned ((stepper1.distanceToGo() - stepper1.currentPosition())) > 100)
-//            run();
-        checkSW();
+        //モーター1の目標位置をスライダーから取得
+        dmxInHighBit = dmx.get(DMX_Position1_H);
+        dmxInLowBit = dmx.get(DMX_Position1_L);
+        dmxInHighBit = dmxInHighBit << 8;
+        position1 = (long)(dmxInHighBit + dmxInLowBit);
 
-        stepper1.run();
-        
-
-            
-//        if (stepper1.distanceToGo() == 0)
-//            silence();
-//        else
-//            run();
-    }
-}
+        //モーター1の目標位置をスライダーから取得
+        dmxInHighBit = dmx.get(DMX_Position2_H);
+        dmxInLowBit = dmx.get(DMX_Position2_L);
+        dmxInHighBit = dmxInHighBit << 8;
+        position2 = (long)(dmxInHighBit + dmxInLowBit);
 
 
-//    printf("\r\n*** AT24C1024 Test ***\r\n");
-//
-//    // Byte Read/Write
-//    //
-//    uint8_t dt = 0x55;
-//    printf("Byte Write: %02x\r\n", dt);
-//    at24c1024.write(0, dt);     // write addr=0 data=dt
-//    wait_ms(5);
-//    dt = at24c1024.read(0);     // read addr=0
-//    printf("Byte Read: %02x\r\n", dt);
-//
-//    // Page Read/Write
-//    //
-//    uint8_t eep_buf[258];
-//    for (int i = 0; i < 256; i++) {
-//        eep_buf[i] = i;
-//    }
-//    printf("Page Write: 0..255\r\n");
-//    AT24C_STATUS status = at24c1024.write_page(0x1ff00, eep_buf, sizeof(eep_buf));
-//    wait_ms(5);
-//    printf("Status: %d\r\n", status);
-//    printf("Page Read:\r\n");
-//    status = at24c1024.read_page(0x1ff00, eep_buf, sizeof(eep_buf));
-//    printf("Status: %d\r\n", status);
-//    for (int i = 0; i < sizeof(eep_buf); i++) {
-//        printf("%d ", eep_buf[i]);
-//    }
-//    printf("\r\n");
+        //手動モードの場合、現在のスライダーの値を取得、パラメーターを設定
+        if(manipulateMode1 == true) {
+            captureSliderValue();
+        }
 
+        //自動モードの場合、EEPROMから値を取得、パラメーターを設定
+        else {
+            readROMValue1();
+        }
 
-//I2C         i2c(D4, D5);      // SDA, SCL
-//AT24C1024   at24c1024(i2c);     // Atmel 1Mbit EE-PROM
-
-//    printf("\r\n*** AT24C1024 Test ***\r\n");
-//
-//    // Byte Read/Write
-//    //
-//    uint8_t dt = 0x22;
-//    printf("Byte Write: %02x\r\n", dt);
-//    at24c1024.write(0, dt);     // write addr=0 data=dt
-//    wait_ms(10);
-//    dt = at24c1024.read(0);     // read addr=0
-//    printf("Byte Read: %02x\r\n", dt);
+//        //手動モードの場合、現在のスライダーの値を取得、パラメーターを設定
+//        if(manipulateMode2 == true) {
+//            captureSliderValue();
+//        }
 //
-//    // Page Read/Write
-//    //
-//    uint8_t eep_buf[258];
-//    for (int i = 0; i < 256; i++) {
-//        eep_buf[i] = i;
-//    }
-//    printf("Page Write: 0..255\r\n");
-//    AT24C_STATUS status = at24c1024.write_page(0x1ff00, eep_buf, sizeof(eep_buf));
-//    wait_ms(5);
-//    printf("Status: %d\r\n", status);
-//    printf("Page Read:\r\n");
-//    status = at24c1024.read_page(0x00, eep_buf, sizeof(eep_buf));
-//    printf("Status: %d\r\n", status);
-//    for (int i = 0; i < sizeof(eep_buf); i++) {
-//        printf("%02x ", eep_buf[i]);
-//    }
-//    printf("\r\n");
-//}
\ No newline at end of file
+//        //自動モードの場合、EEPROMから値を取得、パラメーターを設定
+//        else {
+//            readROMValue2();
+//        }
+
+        position1 = map(position1, 0, 0xFFFF, 0, max1);
+        stepper1.setMaxSpeed(speed1);
+        stepper1.setAcceleration(Acceleration1);
+
+//        position2 = map(position2, 0, 0xFFFF, 0, max2);
+//        stepper2.setMaxSpeed(speed2);
+//        stepper2.setAcceleration(Acceleration2);
+
+        stepper1.moveTo(position1);
+        //stepper2.moveTo(position2);
+
+        checkSafeBreak();
+        stepper1.run();
+        //stepper2.run();
+    }
+}
\ No newline at end of file