新型モータードライバのメインプログラム

Dependencies:   mbed

Revision:
1:e45830806a1c
Parent:
0:cf3829614495
Child:
2:c77482250d0b
--- a/main.cpp	Wed May 11 07:31:23 2016 +0000
+++ b/main.cpp	Sat May 14 05:58:39 2016 +0000
@@ -1,5 +1,17 @@
 #include "mbed.h"
 
+//重要受信データのマスク
+#define LEAD_PACKET_MASK        0b10000000
+#define PACKET_ADDRESS_MASK     0b00011111
+#define PACKET_MODE_MASK        0b01100000
+#define PACKET_DATA_MASK        0b00111111
+
+//モータードライバの動作モード
+#define ORDER_FOLLOWING_MODE    0
+#define ENCODER_FOLLOWING_MODE  1
+#define SINGLE_ORDER_MODE       2
+#define SEND_POWER_MODE         3
+
 #define F_MD 18000 //PWMキャリア周波数設定
 
 /*
@@ -12,39 +24,85 @@
  *  Low_A  Low_B
  */
  
-//Serial pc(USBTX, USBRX);
 Serial comPort(dp16, dp15);
-//DigitalOut myled1(LED1); //汎用IOで使用するためLED1は使用できない
-DigitalOut myled(dp24);
 DigitalOut driverEnable(dp28);
+DigitalOut LED(dp24);
+//dp5 = ハイサイドPWM1, dp27 = ハイサイドPWM2 (オープンドレイン)
 //dp1 = ローサイドPWM1, dp2 = ローサイドPWM2
-//dp5 = ハイサイドPWM1, dp27 = ハイサイドPWM2 (オープンドレイン)
 PwmOut Low_A(dp1);
 PwmOut Low_B(dp2);
 DigitalOut High_A(dp5);
 DigitalOut High_B(dp27);
 
 AnalogIn ain(dp4);
-BusInOut led7(dp6, dp9, dp10, dp11, dp13, dp14, dp17, dp18);
+
+//グローバル変数
+uint8_t lastRXdata = 0;
+uint8_t address = 0;
+uint8_t mode = 0;
+uint8_t nowOrderAddress = 0;
+uint8_t dataNumber = 0;
+uint8_t sendPowerFlag = 0;
+
+void orderFollowManager(uint8_t reciveData, uint8_t dataNumber){
+    
+}
+
+void encoderFollowManager(uint8_t reciveData, uint8_t dataNumber){
+    
+}
+
+void S_orderFollowManager(uint8_t reciveData, uint8_t dataNumber){
+    
+}
+
+void sendPowerManager(uint8_t reciveData, uint8_t dataNumber){
+    
+}
 
 void com_rx() {
-    /*switch (comPort.getc()) {
-        case 255
+    static uint8_t reciveData;
+    reciveData = comPort.getc();
+    
+    if (reciveData | LEAD_PACKET_MASK) {
+        nowOrderAddress = reciveData | PACKET_ADDRESS_MASK;
+        dataNumber = 0;
+    }
+    
+    if (nowOrderAddress == address) {
+        dataNumber++;
+        lastRXdata = reciveData | PACKET_DATA_MASK;
         
-    }*/
+        if (dataNumber == 1) {
+            mode = (reciveData | PACKET_MODE_MASK) >> 5;
+        } else {
+            switch (mode) {
+                case ORDER_FOLLOWING_MODE:
+                    orderFollowManager(reciveData, dataNumber);
+                    break;
+                case ENCODER_FOLLOWING_MODE:
+                    encoderFollowManager(reciveData, dataNumber);
+                    break;
+                case SINGLE_ORDER_MODE:
+                    S_orderFollowManager(reciveData, dataNumber);
+                    break;
+                case SEND_POWER_MODE:
+                    sendPowerManager(reciveData, dataNumber);
+                    break;
+            }
+        }    
+    }
 }
 
 int main() {
-    //comPort.baud(9600);
-    //comPort.format(8, Serial::None, 1);
+    comPort.baud(9600);
+    comPort.format(8, Serial::Odd, 1);
     comPort.attach(&com_rx, Serial::RxIrq);
 
     int FP_MD = 1000000 / F_MD; //PWM一周期の周期(マイクロ秒)
     
-    led7.output();
-    
     High_A = 0;
-    High_B = 1;
+    High_B = 0;
     
     Low_A.period_us(FP_MD);
     Low_A.write(0);
@@ -52,10 +110,8 @@
     Low_B.write(0);
     //初期設定完了
     
-    //flipper.attach(&flip, 2.0);
-    //flipper.attach(&flip, 2.0);
-    //myled1 = 1;
-    led7 = 0b11011010;
+    LED = 1;
+    High_B = 1;
     driverEnable = 1;
     while (1) {
         Low_A.write((ain.read() - 0.2) * 1.25 < 0 ? 0 : (ain.read() - 0.2) * 1.25);