A fork of a fine man's work. simplified. No microstepping etc, just a work in progress

Fork of BipoarStepperMotor by Harsha vardan

sMotor.cpp

Committer:
InBrewJ
Date:
2015-02-03
Revision:
4:a3d8d60147dd
Parent:
3:944e51dd1e4c
Child:
5:f9404f00deda

File content as of revision 4:a3d8d60147dd:

/*
##############################################
##    Program Created by Harshavardan61     ##
##############################################
        ---- harshavardan61@gmail.com -----
        
This library was made for 4-Phase Stepper Motors
I don't take any resposability for the damage caused to your equipment.

*/

#include "sMotor.h"

#include "mbed.h"

int motorSpeed; // Steper speed

sMotor::sMotor(PinName A0, PinName A1, PinName A2, PinName A3, const int maxSteps) : _A0(A0), _A1(A1), _A2(A2), _A3(A3) { // Defenition of motor pins
    _A0=0;
    _A1=0;
    _A2=0;
    _A3=0;
    _maxSteps = maxSteps;
    _motorPosition = maxSteps;
}

//UP
void sMotor::anticlockwise(int num_steps) { // rotate the motor num_steps UP
    short subStep = 0;
    for (int i = 0; i < num_steps && _motorPosition < _maxSteps; i++) {        
        subStep %= 4;
        switch (subStep) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
            case 0: {
                _A0=1;
                _A1=0;
                _A2=0;
                _A3=1;
            }
            break;
            case 1: {
                _A0=0;
                _A1=0;
                _A2=1;
                _A3=1;
            }
            break;
            case 2: {
                _A0=0;
                _A1=1;
                _A2=1;
                _A3=0;
            }
            break;
            case 3: {
                _A0=1;
                _A1=1;
                _A2=0;
                _A3=0;
            }
            break;            
        }

        subStep++;
        _motorPosition++;
        wait_ms(motorSpeed); // wait time defines the speed 
    }
}

// DOWN
void sMotor::clockwise(int num_steps) { // rotate the motor num_steps DOWN
    short subStep = 0;    
    for (int i = 0; i < num_steps && _motorPosition > 0; i++) {    
        subStep %= 4;
        switch (subStep) {
            case 0: {
                _A0=1;
                _A1=1;
                _A2=0;
                _A3=0;
            }
            break;
            case 1: {
                _A0=0;
                _A1=1;
                _A2=1;
                _A3=0;
            }
            break;
            case 2: {
                _A0=0;
                _A1=0;
                _A2=1;
                _A3=1;
            }
            break;
            case 3: {
                _A0=1;
                _A1=0;
                _A2=0;
                _A3=1;
            }
            break;            
        }
    
        subStep++;
        _motorPosition--;
        wait_ms(motorSpeed); // wait time defines the speed 
    }
}
void sMotor::step(int num_steps, int direction, int speed) {// steper function: number of steps, direction (0- right, 1- left), speed (default 1200)
    //int count=0; // initalize step count
    motorSpeed=speed; //set motor speed
    if (!direction) // shaft DOWN
        //do {
            //printf("Doing Down: %d\n", count);
            clockwise(num_steps);
            //count++;
        //} while (count<num_steps); // turn number of steps applied 
    else if (direction)// Shaft UP
        //do {
            //printf("Doing Up: %d\n", count);
            anticlockwise(num_steps);
            //count++;
        //} while (count<num_steps);// turn number of steps applied 

}