Michael Marzano / Mbed 2 deprecated Linear_Stepper_Motor_Nema17

Dependencies:   mbed

lin_step_mtr.h

Committer:
mikermarza
Date:
2020-04-27
Revision:
7:0d941d1140ad
Parent:
5:955bbc08ee78
Child:
8:f1d869d9b8df

File content as of revision 7:0d941d1140ad:

/** Linear Stepper Motor control library
 *  
 *  @class   LinStepMtr
 *  @author  Mike Marzano
 *  @version 0.0 ()
 *  
 *  This is the driver for A Nema 17 linear stepper motor
 *  
 *  ----------------------IMPORTANT--------------------
 *
 *  ---------------------------------------------------
 */
 
#ifndef MBED_LIN_STEP_MTR
#define MBED_LIN_STEP_MTR
 
#include "mbed.h"


 
/**********
* Defines *
**********/
#define DEFAULT_RPM 300   // RPM (1rpm = 3.333 steps/sec)
#define MAX_RPM 400       // RPM
#define MIN_RPM 150       // in RPM
#define MAX_DOUBLE_VAL 9223372036854775800
#define MIN_DOUBLE_VAL -9223372036854775800
 
/** Linear Stepper Motor control class
 *
 *  Example:
 *  @code
 *  #include "mbed.h"
 *  #include "LinStepMtr.h"
 *
 *  
 *  @endcode
 */
 
class LinStepMtr {
 
public:
    
    /** Direction Control **/
    typedef enum {
        CW = 0,     // Motor is spinning in CLOCKWISE direction
        CCW = 1,    // Motor is spinning in COUNTER-CLOCKWISE direction
    } Direction;
    
    /** Steps of motor for movement *
      * In form A B A' B'           */
    typedef enum {
            STOP =  0b0000,
            ONE =   0b1100,
            TWO =   0b0110,
            THREE = 0b0011,
            FOUR =  0b1001
        } Step_Num;
    
    /** Create a linear stepper motor object connected to specified DigitalOut pins
     *
     *  @param A_f DigitalOut pin for Forward Control of H-Brigde Port A (AIN1)
     *  @param A_r DigitalOut pin for Reverse Control of H-Brigde Port A (AIN2)
     *  @param B_f DigitalOut pin for Forward Control of H-Brigde Port B (BIN1)
     *  @param B_r DigitalOut pin for Reverse Control of H-Brigde Port B (BIN2)
     */
    LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r);
    
    /** Create a linear stepper motor object connected to specified DigitalOut pins
     *
     *  @param A_f DigitalOut pin for Forward Control of H-Brigde Port A (AIN1)
     *  @param A_r DigitalOut pin for Reverse Control of H-Brigde Port A (AIN2)
     *  @param B_f DigitalOut pin for Forward Control of H-Brigde Port B (BIN1)
     *  @param B_r DigitalOut pin for Reverse Control of H-Brigde Port B (BIN2)
     *  @param m_rpm Sets the max speed  in RPM of the motor
     */
    LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r, int m_rpm);
    
    /** Destructor
     */
    ~LinStepMtr();
    
    /** Gets the current speed in RPM
     *
     */
    float get_speed();
    
    
    /** Sets the value of speed in RPM 
     */
    void set_speed(float rpm);
    
    /** Gets the number of revolutions since motor was initialized.
     *  Positive means more CW than CCW movement, negative is opposite
     */
    double get_rev();
    
    /** Getters and Setters for {min,max}_rev_cnt
     *
     */
    void set_min_rev_cnt(double rc);
    double get_min_rev_cnt();
    void set_max_rev_cnt(double rc);
    double get_max_rev_cnt();
    
    
    void RESET_rev_cnts();
    
    
    /** Gets the current direction
     *
     */
    Direction get_dir();
    
    /** Set the direction
     *
     */
    //void set_dir(Direction d);
    
       
    /** Rotates the motor for a set number of rotations in the given direction
     *
     */
    double rotate(Direction d, float rev=.25);
    
    
//private:
    class Step {
     public:     
        //constructior
        Step() : cur_step(ONE) {}; // Step();
        
        //increment step post incr operator
        Step_Num operator++();
        //decrement step post incr operator
        Step_Num operator--();
        //Sets step
        void operator=(Step_Num s);
        //getter for cur_step
        Step_Num get_cur_step();
        
     private:
/*        static const int step_one =     0b1100;
        static const int step_two =     0b0110;
        static const int step_three =   0b0011;
        static const int step_four =    0b1001; */
        Step_Num cur_step;
     
     }; 
    
    /** Spins up the motor by stepping up from min speed to desired speed
     *
     */
    void spin_up(float rpm=-1);
    
    /** Spins down the motor by stepping speed down from current speed to min_speed
     *
     */
    int spin_down(float rpm=-1);
        
    /** Variables **/
    BusOut mtr_ctrl;                  // 4-bit Bus Controlling the H-Brigde
                                            // form A B A' B'
    const int max_speed;                    // Software Limit for max rpm in steps/second
    const float max_rpm;
    static const int min_speed 
        = (float)MIN_RPM * 10 / 3;      // Software Limit for min rpm in steps/sec
    static const int min_rpm = MIN_RPM;
    volatile int speed;                          // Speed of Rotation (in steps per second)
    volatile double rev_cnt;                     // Current Revolution of motor
    volatile double min_rev_cnt;                 // software limit for lowest rev count the moter can reach.
    volatile double max_rev_cnt;                 // software limit for highest rev count the moter can reach. 
    Step cur_step;                      // Current Step, i.e. which one was just written to the motor 
    Direction dir;                      // The direction for the motor to run
};
#endif