a easy way to control stepper

Dependents:   Ex_stepper Example_for_learning_easy

Committer:
Dennis_Yu
Date:
Fri Sep 07 13:52:14 2018 +0000
Revision:
0:7caa07ab8d77
Child:
1:9a7c41e82ca5
an easier version of stepper

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Dennis_Yu 0:7caa07ab8d77 1 #include "stepper.h"
Dennis_Yu 0:7caa07ab8d77 2
Dennis_Yu 0:7caa07ab8d77 3
Dennis_Yu 0:7caa07ab8d77 4 const int unit_xy = 100; //单位长度(xy移动一格)对应unit_xy转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
Dennis_Yu 0:7caa07ab8d77 5 const int unit_x = 103; //单位长度(xy移动一格)对应unit_x转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
Dennis_Yu 0:7caa07ab8d77 6 const int unit_y = 103; //单位长度(xy移动一格)对应unit_y转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
Dennis_Yu 0:7caa07ab8d77 7
Dennis_Yu 0:7caa07ab8d77 8 float now_x = 0, now_y = 0;
Dennis_Yu 0:7caa07ab8d77 9
Dennis_Yu 0:7caa07ab8d77 10 void rotate(Stepper stepper, double period, int pix) //id= 0--x,1--y pix=3200为一圈
Dennis_Yu 0:7caa07ab8d77 11 {
Dennis_Yu 0:7caa07ab8d77 12 double half_period = period / 2.0;
Dennis_Yu 0:7caa07ab8d77 13 if (pix >= 0) {
Dennis_Yu 0:7caa07ab8d77 14 stepper.dir = 1;
Dennis_Yu 0:7caa07ab8d77 15 }
Dennis_Yu 0:7caa07ab8d77 16 else {
Dennis_Yu 0:7caa07ab8d77 17 pix = -pix;
Dennis_Yu 0:7caa07ab8d77 18 stepper.dir = 0;
Dennis_Yu 0:7caa07ab8d77 19 }
Dennis_Yu 0:7caa07ab8d77 20 for (int i = 0; i < pix; i++) {
Dennis_Yu 0:7caa07ab8d77 21 stepper.step = 1;
Dennis_Yu 0:7caa07ab8d77 22 wait(step_halfperiod);
Dennis_Yu 0:7caa07ab8d77 23 stepper.step = 0;
Dennis_Yu 0:7caa07ab8d77 24 wait(step_halfperiod);
Dennis_Yu 0:7caa07ab8d77 25 }
Dennis_Yu 0:7caa07ab8d77 26 }
Dennis_Yu 0:7caa07ab8d77 27
Dennis_Yu 0:7caa07ab8d77 28 void moveTo(Stepper xStepper, Stepper yStepper, double period, float x, float y)
Dennis_Yu 0:7caa07ab8d77 29 {
Dennis_Yu 0:7caa07ab8d77 30 rotate(xStepper, (x - now_x)*unit_xy);
Dennis_Yu 0:7caa07ab8d77 31 rotate(yStepper, (y - now_y)*unit_xy);
Dennis_Yu 0:7caa07ab8d77 32 now_x = x;
Dennis_Yu 0:7caa07ab8d77 33 now_y = y;
Dennis_Yu 0:7caa07ab8d77 34 }