Michael Marzano / Mbed 2 deprecated Linear_Stepper_Motor_Nema17

Dependencies:   mbed

Committer:
mikermarza
Date:
Mon Apr 27 17:26:51 2020 +0000
Revision:
7:0d941d1140ad
Parent:
5:955bbc08ee78
Child:
8:f1d869d9b8df
added software stops for maximum and minimum rotations

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mikermarza 0:54c5be5f26f4 1 /** Linear Stepper Motor control library
mikermarza 0:54c5be5f26f4 2 *
mikermarza 0:54c5be5f26f4 3 * @class LinStepMtr
mikermarza 0:54c5be5f26f4 4 * @author Mike Marzano
mikermarza 0:54c5be5f26f4 5 * @version 0.0 ()
mikermarza 0:54c5be5f26f4 6 *
mikermarza 0:54c5be5f26f4 7 * This is the driver for A Nema 17 linear stepper motor
mikermarza 0:54c5be5f26f4 8 *
mikermarza 0:54c5be5f26f4 9 * ----------------------IMPORTANT--------------------
mikermarza 0:54c5be5f26f4 10 *
mikermarza 0:54c5be5f26f4 11 * ---------------------------------------------------
mikermarza 0:54c5be5f26f4 12 */
mikermarza 0:54c5be5f26f4 13
mikermarza 0:54c5be5f26f4 14 #ifndef MBED_LIN_STEP_MTR
mikermarza 0:54c5be5f26f4 15 #define MBED_LIN_STEP_MTR
mikermarza 0:54c5be5f26f4 16
mikermarza 0:54c5be5f26f4 17 #include "mbed.h"
mikermarza 0:54c5be5f26f4 18
mikermarza 0:54c5be5f26f4 19
mikermarza 0:54c5be5f26f4 20
mikermarza 0:54c5be5f26f4 21 /**********
mikermarza 0:54c5be5f26f4 22 * Defines *
mikermarza 0:54c5be5f26f4 23 **********/
mikermarza 3:2138b69ee3bd 24 #define DEFAULT_RPM 300 // RPM (1rpm = 3.333 steps/sec)
mikermarza 3:2138b69ee3bd 25 #define MAX_RPM 400 // RPM
mikermarza 3:2138b69ee3bd 26 #define MIN_RPM 150 // in RPM
mikermarza 7:0d941d1140ad 27 #define MAX_DOUBLE_VAL 9223372036854775800
mikermarza 7:0d941d1140ad 28 #define MIN_DOUBLE_VAL -9223372036854775800
mikermarza 0:54c5be5f26f4 29
mikermarza 0:54c5be5f26f4 30 /** Linear Stepper Motor control class
mikermarza 0:54c5be5f26f4 31 *
mikermarza 0:54c5be5f26f4 32 * Example:
mikermarza 0:54c5be5f26f4 33 * @code
mikermarza 0:54c5be5f26f4 34 * #include "mbed.h"
mikermarza 0:54c5be5f26f4 35 * #include "LinStepMtr.h"
mikermarza 0:54c5be5f26f4 36 *
mikermarza 0:54c5be5f26f4 37 *
mikermarza 0:54c5be5f26f4 38 * @endcode
mikermarza 0:54c5be5f26f4 39 */
mikermarza 0:54c5be5f26f4 40
mikermarza 0:54c5be5f26f4 41 class LinStepMtr {
mikermarza 0:54c5be5f26f4 42
mikermarza 0:54c5be5f26f4 43 public:
mikermarza 0:54c5be5f26f4 44
mikermarza 0:54c5be5f26f4 45 /** Direction Control **/
mikermarza 0:54c5be5f26f4 46 typedef enum {
mikermarza 0:54c5be5f26f4 47 CW = 0, // Motor is spinning in CLOCKWISE direction
mikermarza 0:54c5be5f26f4 48 CCW = 1, // Motor is spinning in COUNTER-CLOCKWISE direction
mikermarza 0:54c5be5f26f4 49 } Direction;
mikermarza 0:54c5be5f26f4 50
mikermarza 0:54c5be5f26f4 51 /** Steps of motor for movement *
mikermarza 0:54c5be5f26f4 52 * In form A B A' B' */
mikermarza 0:54c5be5f26f4 53 typedef enum {
mikermarza 0:54c5be5f26f4 54 STOP = 0b0000,
mikermarza 0:54c5be5f26f4 55 ONE = 0b1100,
mikermarza 0:54c5be5f26f4 56 TWO = 0b0110,
mikermarza 0:54c5be5f26f4 57 THREE = 0b0011,
mikermarza 0:54c5be5f26f4 58 FOUR = 0b1001
mikermarza 0:54c5be5f26f4 59 } Step_Num;
mikermarza 0:54c5be5f26f4 60
mikermarza 0:54c5be5f26f4 61 /** Create a linear stepper motor object connected to specified DigitalOut pins
mikermarza 0:54c5be5f26f4 62 *
mikermarza 0:54c5be5f26f4 63 * @param A_f DigitalOut pin for Forward Control of H-Brigde Port A (AIN1)
mikermarza 0:54c5be5f26f4 64 * @param A_r DigitalOut pin for Reverse Control of H-Brigde Port A (AIN2)
mikermarza 0:54c5be5f26f4 65 * @param B_f DigitalOut pin for Forward Control of H-Brigde Port B (BIN1)
mikermarza 0:54c5be5f26f4 66 * @param B_r DigitalOut pin for Reverse Control of H-Brigde Port B (BIN2)
mikermarza 0:54c5be5f26f4 67 */
mikermarza 0:54c5be5f26f4 68 LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r);
mikermarza 0:54c5be5f26f4 69
mikermarza 0:54c5be5f26f4 70 /** Create a linear stepper motor object connected to specified DigitalOut pins
mikermarza 0:54c5be5f26f4 71 *
mikermarza 0:54c5be5f26f4 72 * @param A_f DigitalOut pin for Forward Control of H-Brigde Port A (AIN1)
mikermarza 0:54c5be5f26f4 73 * @param A_r DigitalOut pin for Reverse Control of H-Brigde Port A (AIN2)
mikermarza 0:54c5be5f26f4 74 * @param B_f DigitalOut pin for Forward Control of H-Brigde Port B (BIN1)
mikermarza 0:54c5be5f26f4 75 * @param B_r DigitalOut pin for Reverse Control of H-Brigde Port B (BIN2)
mikermarza 3:2138b69ee3bd 76 * @param m_rpm Sets the max speed in RPM of the motor
mikermarza 0:54c5be5f26f4 77 */
mikermarza 3:2138b69ee3bd 78 LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r, int m_rpm);
mikermarza 0:54c5be5f26f4 79
mikermarza 0:54c5be5f26f4 80 /** Destructor
mikermarza 0:54c5be5f26f4 81 */
mikermarza 0:54c5be5f26f4 82 ~LinStepMtr();
mikermarza 0:54c5be5f26f4 83
mikermarza 0:54c5be5f26f4 84 /** Gets the current speed in RPM
mikermarza 0:54c5be5f26f4 85 *
mikermarza 0:54c5be5f26f4 86 */
mikermarza 0:54c5be5f26f4 87 float get_speed();
mikermarza 0:54c5be5f26f4 88
mikermarza 7:0d941d1140ad 89
mikermarza 0:54c5be5f26f4 90 /** Sets the value of speed in RPM
mikermarza 0:54c5be5f26f4 91 */
mikermarza 7:0d941d1140ad 92 void set_speed(float rpm);
mikermarza 0:54c5be5f26f4 93
mikermarza 0:54c5be5f26f4 94 /** Gets the number of revolutions since motor was initialized.
mikermarza 0:54c5be5f26f4 95 * Positive means more CW than CCW movement, negative is opposite
mikermarza 0:54c5be5f26f4 96 */
mikermarza 0:54c5be5f26f4 97 double get_rev();
mikermarza 0:54c5be5f26f4 98
mikermarza 7:0d941d1140ad 99 /** Getters and Setters for {min,max}_rev_cnt
mikermarza 7:0d941d1140ad 100 *
mikermarza 7:0d941d1140ad 101 */
mikermarza 7:0d941d1140ad 102 void set_min_rev_cnt(double rc);
mikermarza 7:0d941d1140ad 103 double get_min_rev_cnt();
mikermarza 7:0d941d1140ad 104 void set_max_rev_cnt(double rc);
mikermarza 7:0d941d1140ad 105 double get_max_rev_cnt();
mikermarza 7:0d941d1140ad 106
mikermarza 7:0d941d1140ad 107
mikermarza 7:0d941d1140ad 108 void RESET_rev_cnts();
mikermarza 7:0d941d1140ad 109
mikermarza 0:54c5be5f26f4 110
mikermarza 0:54c5be5f26f4 111 /** Gets the current direction
mikermarza 0:54c5be5f26f4 112 *
mikermarza 0:54c5be5f26f4 113 */
mikermarza 0:54c5be5f26f4 114 Direction get_dir();
mikermarza 0:54c5be5f26f4 115
mikermarza 0:54c5be5f26f4 116 /** Set the direction
mikermarza 0:54c5be5f26f4 117 *
mikermarza 0:54c5be5f26f4 118 */
mikermarza 0:54c5be5f26f4 119 //void set_dir(Direction d);
mikermarza 0:54c5be5f26f4 120
mikermarza 0:54c5be5f26f4 121
mikermarza 7:0d941d1140ad 122 /** Rotates the motor for a set number of rotations in the given direction
mikermarza 0:54c5be5f26f4 123 *
mikermarza 0:54c5be5f26f4 124 */
mikermarza 7:0d941d1140ad 125 double rotate(Direction d, float rev=.25);
mikermarza 0:54c5be5f26f4 126
mikermarza 0:54c5be5f26f4 127
mikermarza 7:0d941d1140ad 128 //private:
mikermarza 0:54c5be5f26f4 129 class Step {
mikermarza 0:54c5be5f26f4 130 public:
mikermarza 0:54c5be5f26f4 131 //constructior
mikermarza 0:54c5be5f26f4 132 Step() : cur_step(ONE) {}; // Step();
mikermarza 0:54c5be5f26f4 133
mikermarza 0:54c5be5f26f4 134 //increment step post incr operator
mikermarza 0:54c5be5f26f4 135 Step_Num operator++();
mikermarza 0:54c5be5f26f4 136 //decrement step post incr operator
mikermarza 0:54c5be5f26f4 137 Step_Num operator--();
mikermarza 1:757a52db1604 138 //Sets step
mikermarza 1:757a52db1604 139 void operator=(Step_Num s);
mikermarza 0:54c5be5f26f4 140 //getter for cur_step
mikermarza 0:54c5be5f26f4 141 Step_Num get_cur_step();
mikermarza 0:54c5be5f26f4 142
mikermarza 0:54c5be5f26f4 143 private:
mikermarza 7:0d941d1140ad 144 /* static const int step_one = 0b1100;
mikermarza 0:54c5be5f26f4 145 static const int step_two = 0b0110;
mikermarza 0:54c5be5f26f4 146 static const int step_three = 0b0011;
mikermarza 7:0d941d1140ad 147 static const int step_four = 0b1001; */
mikermarza 7:0d941d1140ad 148 Step_Num cur_step;
mikermarza 0:54c5be5f26f4 149
mikermarza 0:54c5be5f26f4 150 };
mikermarza 0:54c5be5f26f4 151
mikermarza 3:2138b69ee3bd 152 /** Spins up the motor by stepping up from min speed to desired speed
mikermarza 0:54c5be5f26f4 153 *
mikermarza 0:54c5be5f26f4 154 */
mikermarza 3:2138b69ee3bd 155 void spin_up(float rpm=-1);
mikermarza 0:54c5be5f26f4 156
mikermarza 3:2138b69ee3bd 157 /** Spins down the motor by stepping speed down from current speed to min_speed
mikermarza 0:54c5be5f26f4 158 *
mikermarza 0:54c5be5f26f4 159 */
mikermarza 3:2138b69ee3bd 160 int spin_down(float rpm=-1);
mikermarza 0:54c5be5f26f4 161
mikermarza 0:54c5be5f26f4 162 /** Variables **/
mikermarza 0:54c5be5f26f4 163 BusOut mtr_ctrl; // 4-bit Bus Controlling the H-Brigde
mikermarza 0:54c5be5f26f4 164 // form A B A' B'
mikermarza 3:2138b69ee3bd 165 const int max_speed; // Software Limit for max rpm in steps/second
mikermarza 3:2138b69ee3bd 166 const float max_rpm;
mikermarza 3:2138b69ee3bd 167 static const int min_speed
mikermarza 7:0d941d1140ad 168 = (float)MIN_RPM * 10 / 3; // Software Limit for min rpm in steps/sec
mikermarza 3:2138b69ee3bd 169 static const int min_rpm = MIN_RPM;
mikermarza 7:0d941d1140ad 170 volatile int speed; // Speed of Rotation (in steps per second)
mikermarza 7:0d941d1140ad 171 volatile double rev_cnt; // Current Revolution of motor
mikermarza 7:0d941d1140ad 172 volatile double min_rev_cnt; // software limit for lowest rev count the moter can reach.
mikermarza 7:0d941d1140ad 173 volatile double max_rev_cnt; // software limit for highest rev count the moter can reach.
mikermarza 7:0d941d1140ad 174 Step cur_step; // Current Step, i.e. which one was just written to the motor
mikermarza 7:0d941d1140ad 175 Direction dir; // The direction for the motor to run
mikermarza 0:54c5be5f26f4 176 };
mikermarza 0:54c5be5f26f4 177 #endif