a easy way to control stepper

Dependents:   Ex_stepper Example_for_learning_easy

Revision:
0:7caa07ab8d77
Child:
1:9a7c41e82ca5
diff -r 000000000000 -r 7caa07ab8d77 stepper.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stepper.cpp	Fri Sep 07 13:52:14 2018 +0000
@@ -0,0 +1,34 @@
+#include "stepper.h"
+
+
+const int unit_xy = 100;  //单位长度(xy移动一格)对应unit_xy转   大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
+const int unit_x = 103;  //单位长度(xy移动一格)对应unit_x转   大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
+const int unit_y = 103;  //单位长度(xy移动一格)对应unit_y转   大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
+
+float now_x = 0, now_y = 0;
+
+void rotate(Stepper stepper, double period, int pix) //id= 0--x,1--y  pix=3200为一圈
+{
+    double half_period = period / 2.0;
+    if (pix >= 0) {
+        stepper.dir = 1;
+    } 
+    else {
+        pix = -pix;
+        stepper.dir = 0;
+    }
+    for (int i = 0; i < pix; i++) {
+        stepper.step = 1;
+        wait(step_halfperiod);
+        stepper.step = 0;
+        wait(step_halfperiod);
+    }
+}
+
+void moveTo(Stepper xStepper, Stepper yStepper, double period, float x, float y)
+{
+    rotate(xStepper, (x - now_x)*unit_xy);
+    rotate(yStepper, (y - now_y)*unit_xy);
+    now_x = x;
+    now_y = y;
+}