a easy way to control stepper

Dependents:   Ex_stepper Example_for_learning_easy

stepper.cpp

Committer:
Dennis_Yu
Date:
2018-10-16
Revision:
2:8f2ae2b5c552
Parent:
1:9a7c41e82ca5

File content as of revision 2:8f2ae2b5c552:

#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 myStepper, double period, int pix) //id= 0--x,1--y  pix=3200为一圈
{
    double half_period = period / 2.0;
    if (pix >= 0) {
        myStepper.dir = 1;
    } 
    else {
        pix = -pix;
        myStepper.dir = 0;
    }
    for (int i = 0; i < pix; i++) {
        myStepper.step = 1;
        wait(half_period);
        myStepper.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;
}