Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
lin_step_mtr.h
- Committer:
- mikermarza
- Date:
- 2020-04-20
- Revision:
- 1:757a52db1604
- Parent:
- 0:54c5be5f26f4
- Child:
- 3:2138b69ee3bd
File content as of revision 1:757a52db1604:
/** 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" #include "rtos.h" /********** * Defines * **********/ #define DEFAULT_SPEED 300 // RPM (1rpm = 3.333 steps/sec) #define MAX_SPEED 400 // RPM #define MIN_SPEED 150 // in RPM /** Linear Stepper Motor control class * * Example: * @code * #include "mbed.h" * #include "LinStepMtr.h" * * * @endcode */ class Step { public: private: }; class LinStepMtr { public: /** Constants for motor rotate control */ typedef enum { COIL_A_FOR = 0, // Forward Polarity in H-Bright Port A COIL_B_FOR = 1, // Forward Polarity in H-Bright Port B COIL_A_REV = 2, // Reverse Polarity in H-Bright Port A COIL_B_REV = 3, // Reverse Polarity in H-Bright Port B STOP_MOTOR = 4 // Turn Off Both Coils } Polarity; /** 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_speed Sets the max speed in RPM of the motor */ LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r, int m_speed); /** Destructor */ ~LinStepMtr(); /** Gets the current speed in RPM * */ float get_speed(); /** Sets the value of speed in RPM */ //void set_speed(float f); /** Gets the number of revolutions since motor was initialized. * Positive means more CW than CCW movement, negative is opposite */ double get_rev(); /** Gets the current direction * */ Direction get_dir(); /** Set the direction * */ //void set_dir(Direction d); /** Initializes the rotate thread and sets parameters before you begin * */ void init(double rpm=DEFAULT_SPEED, Direction d=CW); /** Ends possibility of motion * */ void end(); /** Rotates the motor for a set number of rotations * */ //double rotate(float num_rev); /** Starts continuous motion in the current direction * */ void start(); /** Stops continuous motion * */ void stop(); /** Changes the direction of motion * */ void change_dir(Direction d); /** Changes the speed * */ void change_speed(float s); /** Spins up the motor by stepping up from min speed to current speed * */ void spin_up(); void spin_up(double rpm); /** Spins down the motor by stepping speed down from current speed to 0 * */ void spin_down(); void spin_down(double rpm); 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; volatile Step_Num cur_step; }; //private: /** Continuously rotates the motor in the specified direction * lives in it's own thread */ void rotate(); static void rotate_help(void const *args); /** Spins up the motor by stepping up from min speed to current speed * */ void spin_up(float rpm); /** Spins down the motor by stepping speed down from current speed to 0 * */ void spin_down(float rpm); /** 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 RPM static const int min_speed = MIN_SPEED; // Software Limit for min rpm in RPM volatile int speed; // Speed of Rotation (in steps per second) volatile double rev_cnt; // Current Revolution of motor Step cur_step; // Current Step, i.e. which one was just written to the motor volatile Direction dir; // The direction for the motor to run Thread *rotate_th; // Thread to run the rotate function volatile bool stop_mtr; // Flag for if motor should be off; volatile bool terminate; // Flag for if the rotate function should stop executing private: volatile Polarity cur_state; // Current State of H-Brige Controls }; #endif