闭环步进电机

Dependencies:   mbed

Revision:
1:cbd6a3232d5b
Parent:
0:5b4f19f8cd85
diff -r 5b4f19f8cd85 -r cbd6a3232d5b main.cpp
--- a/main.cpp	Fri Mar 30 12:03:12 2018 +0000
+++ b/main.cpp	Thu Apr 05 01:37:17 2018 +0000
@@ -2,17 +2,23 @@
 
 #include "mbed.h"
 #include "as5047.h"
+#include "GUI.h"
 #include <string> 
 #include <sstream>
 
 // software ssel
-SPI motor_encoder(D11, D12, D13); // mosi, miso, sclk
-DigitalOut motor_encoder_cs(D3); // ssel
-DigitalOut motor_dir(D2); // ssel
-Serial pc(SERIAL_TX, SERIAL_RX);
+SPI motor_encoder(PB_15, PB_14, PB_13); // mosi, miso, sclk
+DigitalOut motor_encoder_cs(PB_12); // ssel
+DigitalOut motor_dir(PA_11); // ssel
+Serial pc(PA_2, PA_3,9600);
+Serial screen(PB_10, PB_11,115200);
 MotorType Motor;
 Ticker PIDInterrupt;  //PID中断 500Hz
 
+int counter = 0;
+
+DigitalIn Mode(PA_12);
+
 void PIDInterruptIRQ(); 
 
 
@@ -20,28 +26,44 @@
 {
     string UartBuf = "";
     char temp = 0;
+    InitGUI(&screen);
     StepMotorInit(&Motor, &motor_dir, &motor_encoder_cs, &motor_encoder);
     Encoder_Init(&Motor);
     PIDInterrupt.attach(&PIDInterruptIRQ, 0.002);
     Motor.Goal = 5000;
     while(1)
     {
-        if(pc.readable())
+        if(pc.readable())// && (Mode.read() != 0))
         {
             temp = pc.getc();
-            if((temp != 'i') && (temp != ';') && ((temp < '0') || (temp > '9')))   //收到的非法字符
+            if((temp != 'p') && (temp != 'i') && (temp != 'x') && (temp != ';') && ((temp < '0') || (temp > '9')))   //收到的非法字符
                 continue;
-            else if(temp == 'i')  //请求信息
-                pc.printf("Position : %d, PWM Freq : %d, Goal : %d\r\n", Motor.Encoder, Motor.PIDControl, Motor.Goal);
+            else if(temp == 'x')  //请求信息
+                pc.printf("Position : %d, PWM Freq : %d, Goal : %d Kp : %.2f Ki : %.2f\r\n", Motor.Encoder, Motor.PIDControl, Motor.Goal, Motor.Kp, Motor.Ki);
             else if((temp >= '0') && (temp <= '9'))  //收到0-9
                 UartBuf = UartBuf + temp;
-            else  //收到 ;
+            else if (temp == ';') //收到 ;
             {
                 Motor.Goal = atoi(UartBuf.c_str());
                 pc.printf("Goal is set at %d\r\n", Motor.Goal);
                 UartBuf = "";
+                counter = 0;
+            }
+            else if (temp == 'p') //收到 ;
+            {
+                Motor.Kp = (float)atoi(UartBuf.c_str())/10;
+                pc.printf("Kp is set at %.2f\r\n", Motor.Kp);
+                UartBuf = "";
+            }
+            else if (temp == 'i') //收到 ;
+            {
+                Motor.Ki = (float)atoi(UartBuf.c_str())/10;
+                pc.printf("Ki is set at %.2f\r\n", Motor.Ki);
+                UartBuf = "";
             }
         }
+        if(counter < 1500)
+            RefreshGUI(&screen, Motor.PIDControl, Motor.Kp, Motor.Ki, Motor.Goal, Motor.Encoder);
         //pc.printf("Angle : %d, PID Value : %d, Difference : %d\r\n", Motor.Encoder, Motor.PIDControl, Motor.Difference);
     }
 }
@@ -59,6 +81,15 @@
     MotorPIDController(&Motor);
     //更新脉冲频率
     StepMotorSetFreq(&Motor);
+    /*if(Mode.read() == 0)
+    {
+        MotorPIDController(&Motor);
+        //更新脉冲频率
+        StepMotorSetFreq(&Motor);
+    }*/
+    counter++;
+    if(counter > 60000)
+        counter = 2001;
 }