a easy way to control stepper

Dependents:   Ex_stepper Example_for_learning_easy

stepper.cpp

Committer:
Dennis_Yu
Date:
2018-09-07
Revision:
1:9a7c41e82ca5
Parent:
0:7caa07ab8d77
Child:
2:8f2ae2b5c552

File content as of revision 1:9a7c41e82ca5:

#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(half_period);
        stepper.step = 0;
        wait(half_period);
    }
}

void moveTo(Stepper xStepper, Stepper yStepper, double period, float x, float y)
{
    rotate(xStepper, period, (x - now_x)*unit_xy);
    rotate(yStepper, period, (y - now_y)*unit_xy);
    now_x = x;
    now_y = y;
}