Michael Marzano / Mbed 2 deprecated Linear_Stepper_Motor_Nema17

Dependencies:   mbed

lin_step_mtr.cpp

Committer:
mikermarza
Date:
2020-04-20
Revision:
2:6c324fded7c1
Parent:
1:757a52db1604
Child:
3:2138b69ee3bd

File content as of revision 2:6c324fded7c1:

// Code for the lin_step_mtr driver

#include "lin_step_mtr.h"
#include "debug.h"

//Construtor
LinStepMtr::LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r, int m_speed=MAX_SPEED)
        :mtr_ctrl(B_r, A_r, B_f,A_f), max_speed((m_speed > MAX_SPEED) ? MAX_SPEED:m_speed)
{
    mtr_ctrl = 0x0;
    speed = floor((double)DEFAULT_SPEED * 10 / 3);
    dir = CW;
    cur_step = ONE;
    
    stop_mtr = true;
    terminate = false;
    rotate_th = NULL;
    
    cur_state = STOP_MOTOR;
}

LinStepMtr::LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r)
    :mtr_ctrl(B_r,A_r,B_f,A_f), max_speed(MAX_SPEED)
{
    mtr_ctrl = 0x0;
    speed = floor((double)DEFAULT_SPEED * 10 / 3);
    dir = CW;
    cur_step = ONE;
    
    stop_mtr = true;
    terminate = false;
    
    cur_state = STOP_MOTOR;
}

LinStepMtr::~LinStepMtr()
{
    this->end();
}


float LinStepMtr::get_speed()
{
    return (float) speed * 3 / 10;
}

LinStepMtr::Direction LinStepMtr::get_dir()
{
    return dir;   
}

void LinStepMtr::init(double rpm, Direction d)
{
    terminate = false;
    speed = floor(rpm * 10 / 3);
    dir = d;
    if (!rotate_th) {
        rotate_th = new Thread(LinStepMtr::rotate_help, this); 
    }
}

void LinStepMtr::end() {
    terminate = true;
    Thread::wait(100);
    if (rotate_th) {
        delete rotate_th;
        rotate_th = NULL;   
    }
}

void LinStepMtr::start() {
    stop_mtr = false;   
}

void LinStepMtr::stop() {
    stop_mtr = true;   
}

void LinStepMtr::rotate_help(void const *args)
{
    LinStepMtr *instPtr = static_cast<LinStepMtr *>(const_cast<void *>(args));
    
    instPtr->rotate();
}

void LinStepMtr::rotate()
{
    pc.printf("Called rotate()\n");
 /*
    while(1) {
        
        mtr_ctrl = ++cur_step;
        rev_cnt +=.005;
        wait(pause);
    }
*/
 
    while(!terminate) {
        if(!stop_mtr){
            switch(dir) {
                case CW:
                    mtr_ctrl = ++cur_step;
                    rev_cnt +=.005;
                    break;
                case CCW:
                    mtr_ctrl = --cur_step;
                    rev_cnt-=.005;
                    break;
            }
           wait(1/ (float) speed);   
        } else {
            mtr_ctrl = STOP;   
            Thread::yield();
        }
    }   

}

// Private Step Class functions
/*
LinStepMtr::Step::Step() : cur_step(ONE)
{
            pc.printf("\n\nCalled Constructor\n  cur_step: %x\n",cur_step);  
            
            pc.printf("Step_Num val:\n  ONE = %x\n  TWO = %x\n  THREE = %x\n  FOUR = %x\n\n", ONE, TWO, THREE, FOUR); 
};
*/
LinStepMtr::Step_Num LinStepMtr::Step::get_cur_step()
{
        return cur_step;
}
        
LinStepMtr::Step_Num LinStepMtr::Step::operator++()
{    
    switch(cur_step){
        case ONE:
            cur_step=TWO;
            break;
        case TWO:
            cur_step = THREE;
            break;
        case THREE:
            cur_step = FOUR;
            break;
        case FOUR:
            cur_step = ONE; 
            break;      
    }
    //pc.printf("  CUR_STEP = %x\n",cur_step);
    return cur_step;
}

LinStepMtr::Step_Num LinStepMtr::Step::operator--()
{
    switch(cur_step){
        case ONE:
            cur_step=FOUR;
            break;
        case TWO:
            cur_step = ONE;
            break;
        case THREE:
            cur_step = TWO;
            break;
        case FOUR:
            cur_step = THREE; 
            break;      
    }
    return cur_step;
}

void LinStepMtr::Step::operator=(LinStepMtr::Step_Num s)
{
    cur_step = s;   
}