beta1

Dependents:   Ex_Stepper

Fork of a4998 by Keegan Hu

Files at this revision

API Documentation at this revision

Comitter:
glintligo
Date:
Tue Jul 17 00:28:36 2018 +0000
Parent:
2:57c57267b1da
Commit message:
beta2

Changed in this revision

a4988.cpp Show annotated file Show diff for this revision Revisions of this file
a4988.h Show annotated file Show diff for this revision Revisions of this file
--- a/a4988.cpp	Mon Jul 16 23:53:54 2018 +0000
+++ b/a4988.cpp	Tue Jul 17 00:28:36 2018 +0000
@@ -1,77 +1,109 @@
 #include "a4988.h"
 #include "mbed.h"
-Stepper::Stepper(PinName _en, PinName _stepPin, PinName _direction):en(_en),
-    stepPin(_stepPin),
-    direction(_direction)
+
+Stepper::Stepper(PinName _en, PinName _stepPin, PinName _direction) : en(_en),
+                                                                      stepPin(_stepPin),
+                                                                      direction(_direction)
 {
-    
 }
- 
-void Stepper::step(int dir, int frequency ,volatile int _remain)
+/**
+ * 转动特定脉冲
+ * @param dir          [运动方向]
+ * @param frequency    [脉冲周期]
+ * @param _remain      [脉冲个数]
+ */
+void Stepper::step(int dir, int frequency, volatile int _remain)
 {
-    if (dir == 1) {
+    if (dir == 1)
+    {
         direction = 0;
-    } else if (dir == 0) {
+    }
+    else if (dir == 0)
+    {
         direction = 1;
     }
-    else 
+    else
     {
         return;
     }
     remain = _remain;
-    step_ticker.attach(callback(this, &Stepper::step_control),0.5/frequency);
+    step_ticker.attach(callback(this, &Stepper::step_control), 0.5 / frequency);
 }
- 
+/**
+ * 启动步进电机
+ */
 void Stepper::enable()
 {
     en = 0;
 }
- 
+/**
+ * 关闭步进电机
+ */
 void Stepper::disable()
 {
     en = 1;
 }
-
+/**
+ * 步进电机控制程序
+ */
 void Stepper::step_control()
 {
-        if(remain <= 0)
-        {    
-            step_ticker.detach();     
-            return;
-        }
-        
-                if(stepPin){
-                        stepPin = 0; //STEP 1->0
-                        remain--;
-                }else{
-                        stepPin = 1; //STEP 0->1
-                }
+    if (remain <= 0)
+    {
+        step_ticker.detach();
+        return;
+    }
+
+    if (stepPin)
+    {
+        stepPin = 0; //STEP 1->0
+        remain--;
+    }
+    else
+    {
+        stepPin = 1; //STEP 0->1
+    }
 }
-
+/**
+ * 一直转
+ * @param dir          [运动方向]
+ * @param frequency    [脉冲周期]
+ */
 void Stepper::longrun(int dir, int frequency)
 {
-    if (dir == 1) {
+    if (dir == 1)
+    {
         direction = 0;
-    } else if (dir == 0) {
+    }
+    else if (dir == 0)
+    {
         direction = 1;
     }
-    else 
+    else
     {
         return;
     }
-    step_ticker.attach(callback(this, &Stepper::run_control),0.5/frequency);
+    step_ticker.attach(callback(this, &Stepper::run_control), 0.5 / frequency);
 }
-
+/**
+ * 一直转运动函数
+ */
 void Stepper::run_control()
 {
-                if(stepPin){
-                        stepPin = 0; //STEP 1->0
-                        remain--;
-                }else{
-                        stepPin = 1; //STEP 0->1
+    if (stepPin)
+    {
+        stepPin = 0; //STEP 1->0
+        remain--;
+    }
+    else
+    {
+        stepPin = 1; //STEP 0->1
+    }
 }
-}
+/**
+ * 停止一直转
+ */
 void Stepper::stoprun()
 {
     step_ticker.detach();
-}
\ No newline at end of file
+}
--- a/a4988.h	Mon Jul 16 23:53:54 2018 +0000
+++ b/a4988.h	Tue Jul 17 00:28:36 2018 +0000
@@ -1,18 +1,18 @@
 #ifndef __A4988_H
 #define __A4988_H
-#endif
 
 #include "mbed.h"
 class Stepper
 {
-public:
-    Stepper(PinName _en,PinName _stepPin, PinName _direction);
-    void step(int dir, int frequency ,volatile int _remain);
-    void longrun(int dir,int frequency);
+  public:
+    Stepper(PinName _en, PinName _stepPin, PinName _direction);
+    void step(int dir, int frequency, volatile int _remain);
+    void longrun(int dir, int frequency);
     void stoprun();
     void enable();
     void disable();
-private:
+
+  private:
     DigitalOut en;
     DigitalOut stepPin;
     DigitalOut direction;
@@ -21,3 +21,4 @@
     void step_control();
     void run_control();
 };
+#endif
\ No newline at end of file