a easy way to control stepper

Dependents:   Ex_stepper Example_for_learning_easy

Committer:
Dennis_Yu
Date:
Tue Oct 16 07:31:28 2018 +0000
Revision:
2:8f2ae2b5c552
Parent:
1:9a7c41e82ca5
update

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 1:9a7c41e82ca5 5 //const int unit_x = 103; //单位长度(xy移动一格)对应unit_x转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step
Dennis_Yu 1:9a7c41e82ca5 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 2:8f2ae2b5c552 10 void rotate(stepper myStepper, 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 2:8f2ae2b5c552 14 myStepper.dir = 1;
Dennis_Yu 0:7caa07ab8d77 15 }
Dennis_Yu 0:7caa07ab8d77 16 else {
Dennis_Yu 0:7caa07ab8d77 17 pix = -pix;
Dennis_Yu 2:8f2ae2b5c552 18 myStepper.dir = 0;
Dennis_Yu 0:7caa07ab8d77 19 }
Dennis_Yu 0:7caa07ab8d77 20 for (int i = 0; i < pix; i++) {
Dennis_Yu 2:8f2ae2b5c552 21 myStepper.step = 1;
Dennis_Yu 1:9a7c41e82ca5 22 wait(half_period);
Dennis_Yu 2:8f2ae2b5c552 23 myStepper.step = 0;
Dennis_Yu 1:9a7c41e82ca5 24 wait(half_period);
Dennis_Yu 0:7caa07ab8d77 25 }
Dennis_Yu 0:7caa07ab8d77 26 }
Dennis_Yu 0:7caa07ab8d77 27
Dennis_Yu 2:8f2ae2b5c552 28 void moveTo(stepper xStepper, stepper yStepper, double period, float x, float y)
Dennis_Yu 0:7caa07ab8d77 29 {
Dennis_Yu 1:9a7c41e82ca5 30 rotate(xStepper, period, (x - now_x)*unit_xy);
Dennis_Yu 1:9a7c41e82ca5 31 rotate(yStepper, period, (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 }