Michael Marzano / Mbed 2 deprecated Linear_Stepper_Motor_Nema17

Dependencies:   mbed

Committer:
mikermarza
Date:
Wed Apr 29 04:00:45 2020 +0000
Revision:
11:2507965c1bec
Parent:
8:f1d869d9b8df
Updated Documentation

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 8:f1d869d9b8df 5 * @version 1.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 * Defines *
mikermarza 0:54c5be5f26f4 21 **********/
mikermarza 3:2138b69ee3bd 22 #define DEFAULT_RPM 300 // RPM (1rpm = 3.333 steps/sec)
mikermarza 3:2138b69ee3bd 23 #define MAX_RPM 400 // RPM
mikermarza 8:f1d869d9b8df 24 #define MIN_RPM 200 // in RPM
mikermarza 7:0d941d1140ad 25 #define MAX_DOUBLE_VAL 9223372036854775800
mikermarza 7:0d941d1140ad 26 #define MIN_DOUBLE_VAL -9223372036854775800
mikermarza 0:54c5be5f26f4 27
mikermarza 0:54c5be5f26f4 28 /** Linear Stepper Motor control class
mikermarza 0:54c5be5f26f4 29 *
mikermarza 0:54c5be5f26f4 30 * Example:
mikermarza 0:54c5be5f26f4 31 * @code
mikermarza 0:54c5be5f26f4 32 * #include "mbed.h"
mikermarza 8:f1d869d9b8df 33 * #include "lin_step_mtr.h"
mikermarza 8:f1d869d9b8df 34 *
mikermarza 8:f1d869d9b8df 35 * // motor's leads for A,A',B,B' that are hooked upto a dual H-bridge
mikermarza 8:f1d869d9b8df 36 * LinStepMtr mtr(p20, p19, p17, p18);
mikermarza 0:54c5be5f26f4 37 *
mikermarza 8:f1d869d9b8df 38 * int main() {
mikermarza 8:f1d869d9b8df 39 * // Rotates the motor Clockwise at 400 rpm for 10 revolutions
mikermarza 8:f1d869d9b8df 40 * mtr.set_speed(400);
mikermarza 8:f1d869d9b8df 41 * mtr.rotate(LinStepMtr::CW,10);
mikermarza 8:f1d869d9b8df 42 *
mikermarza 8:f1d869d9b8df 43 * // Rotates the motor Counterclockwise at 200 rpm for 5 revolutions
mikermarza 8:f1d869d9b8df 44 * mtr.set_speed(200);
mikermarza 8:f1d869d9b8df 45 * mtr.rotate(LinStepMtr::CCW, 5);
mikermarza 8:f1d869d9b8df 46 * }
mikermarza 0:54c5be5f26f4 47 * @endcode
mikermarza 0:54c5be5f26f4 48 */
mikermarza 8:f1d869d9b8df 49
mikermarza 0:54c5be5f26f4 50 class LinStepMtr {
mikermarza 0:54c5be5f26f4 51
mikermarza 0:54c5be5f26f4 52 public:
mikermarza 0:54c5be5f26f4 53
mikermarza 0:54c5be5f26f4 54 /** Direction Control **/
mikermarza 0:54c5be5f26f4 55 typedef enum {
mikermarza 0:54c5be5f26f4 56 CW = 0, // Motor is spinning in CLOCKWISE direction
mikermarza 0:54c5be5f26f4 57 CCW = 1, // Motor is spinning in COUNTER-CLOCKWISE direction
mikermarza 0:54c5be5f26f4 58 } Direction;
mikermarza 0:54c5be5f26f4 59
mikermarza 0:54c5be5f26f4 60 /** Steps of motor for movement *
mikermarza 0:54c5be5f26f4 61 * In form A B A' B' */
mikermarza 0:54c5be5f26f4 62 typedef enum {
mikermarza 0:54c5be5f26f4 63 STOP = 0b0000,
mikermarza 0:54c5be5f26f4 64 ONE = 0b1100,
mikermarza 0:54c5be5f26f4 65 TWO = 0b0110,
mikermarza 0:54c5be5f26f4 66 THREE = 0b0011,
mikermarza 0:54c5be5f26f4 67 FOUR = 0b1001
mikermarza 0:54c5be5f26f4 68 } Step_Num;
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 0:54c5be5f26f4 76 */
mikermarza 0:54c5be5f26f4 77 LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r);
mikermarza 0:54c5be5f26f4 78
mikermarza 0:54c5be5f26f4 79 /** Create a linear stepper motor object connected to specified DigitalOut pins
mikermarza 0:54c5be5f26f4 80 *
mikermarza 0:54c5be5f26f4 81 * @param A_f DigitalOut pin for Forward Control of H-Brigde Port A (AIN1)
mikermarza 0:54c5be5f26f4 82 * @param A_r DigitalOut pin for Reverse Control of H-Brigde Port A (AIN2)
mikermarza 0:54c5be5f26f4 83 * @param B_f DigitalOut pin for Forward Control of H-Brigde Port B (BIN1)
mikermarza 0:54c5be5f26f4 84 * @param B_r DigitalOut pin for Reverse Control of H-Brigde Port B (BIN2)
mikermarza 3:2138b69ee3bd 85 * @param m_rpm Sets the max speed in RPM of the motor
mikermarza 0:54c5be5f26f4 86 */
mikermarza 3:2138b69ee3bd 87 LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r, int m_rpm);
mikermarza 0:54c5be5f26f4 88
mikermarza 0:54c5be5f26f4 89 /** Destructor
mikermarza 0:54c5be5f26f4 90 */
mikermarza 0:54c5be5f26f4 91 ~LinStepMtr();
mikermarza 0:54c5be5f26f4 92
mikermarza 0:54c5be5f26f4 93 /** Gets the current speed in RPM
mikermarza 0:54c5be5f26f4 94 *
mikermarza 8:f1d869d9b8df 95 * @return motor's speed in RPM
mikermarza 0:54c5be5f26f4 96 */
mikermarza 0:54c5be5f26f4 97 float get_speed();
mikermarza 0:54c5be5f26f4 98
mikermarza 7:0d941d1140ad 99
mikermarza 0:54c5be5f26f4 100 /** Sets the value of speed in RPM
mikermarza 8:f1d869d9b8df 101 *
mikermarza 8:f1d869d9b8df 102 * @param rpm speed in rpm (maximum value of 400 rpm, min value of 200 rpm)
mikermarza 0:54c5be5f26f4 103 */
mikermarza 7:0d941d1140ad 104 void set_speed(float rpm);
mikermarza 0:54c5be5f26f4 105
mikermarza 0:54c5be5f26f4 106 /** Gets the number of revolutions since motor was initialized.
mikermarza 0:54c5be5f26f4 107 * Positive means more CW than CCW movement, negative is opposite
mikermarza 8:f1d869d9b8df 108 *
mikermarza 8:f1d869d9b8df 109 * @return revolution count
mikermarza 0:54c5be5f26f4 110 */
mikermarza 0:54c5be5f26f4 111 double get_rev();
mikermarza 0:54c5be5f26f4 112
mikermarza 11:2507965c1bec 113 /** Sets the value of min_rev_cnt. This value is used as a software stop to
mikermarza 11:2507965c1bec 114 * prevent the motor from rotating any farther while going counterclockwise.
mikermarza 11:2507965c1bec 115 * Defaults to the minimum value of a double.
mikermarza 8:f1d869d9b8df 116 *
mikermarza 11:2507965c1bec 117 * @param rc new minimum revolution count limit value
mikermarza 11:2507965c1bec 118 */
mikermarza 11:2507965c1bec 119 void set_min_rev_cnt(double rc);
mikermarza 11:2507965c1bec 120
mikermarza 11:2507965c1bec 121 /** Sets the value of max_rev_cnt. This value is used as a software stop to
mikermarza 11:2507965c1bec 122 * prevent the motor from rotating any farther while going clockwise
mikermarza 11:2507965c1bec 123 * Defaults to the maximum value of a double.
mikermarza 11:2507965c1bec 124 *
mikermarza 11:2507965c1bec 125 * @param rc new maximum revolution count limit value
mikermarza 11:2507965c1bec 126 */
mikermarza 11:2507965c1bec 127 void set_max_rev_cnt(double rc);
mikermarza 11:2507965c1bec 128
mikermarza 11:2507965c1bec 129 /** Gets the value of the current minumum revolution count
mikermarza 11:2507965c1bec 130 *
mikermarza 11:2507965c1bec 131 * @return the current minimum revevolution count limit
mikermarza 8:f1d869d9b8df 132 */
mikermarza 8:f1d869d9b8df 133 double get_min_rev_cnt();
mikermarza 8:f1d869d9b8df 134
mikermarza 11:2507965c1bec 135 /** Gets the value of the current maximum revolution count
mikermarza 7:0d941d1140ad 136 *
mikermarza 11:2507965c1bec 137 * @return the current maximum revevolution count limit
mikermarza 7:0d941d1140ad 138 */
mikermarza 11:2507965c1bec 139 double get_max_rev_cnt();
mikermarza 7:0d941d1140ad 140
mikermarza 8:f1d869d9b8df 141 /** Rests the revolution count limits to max/min of double
mikermarza 8:f1d869d9b8df 142 */
mikermarza 7:0d941d1140ad 143 void RESET_rev_cnts();
mikermarza 7:0d941d1140ad 144
mikermarza 0:54c5be5f26f4 145
mikermarza 0:54c5be5f26f4 146 /** Gets the current direction
mikermarza 8:f1d869d9b8df 147 *
mikermarza 8:f1d869d9b8df 148 * @return the current direction of motion
mikermarza 0:54c5be5f26f4 149 */
mikermarza 0:54c5be5f26f4 150 Direction get_dir();
mikermarza 0:54c5be5f26f4 151
mikermarza 8:f1d869d9b8df 152 /** NOT SUPORTED - Set the direction
mikermarza 0:54c5be5f26f4 153 *
mikermarza 8:f1d869d9b8df 154 * @param d new direction of motion
mikermarza 0:54c5be5f26f4 155 */
mikermarza 0:54c5be5f26f4 156 //void set_dir(Direction d);
mikermarza 0:54c5be5f26f4 157
mikermarza 0:54c5be5f26f4 158
mikermarza 7:0d941d1140ad 159 /** Rotates the motor for a set number of rotations in the given direction
mikermarza 0:54c5be5f26f4 160 *
mikermarza 8:f1d869d9b8df 161 * @param d direction of rotation
mikermarza 8:f1d869d9b8df 162 * @param rev number of revolutions to rotate (defaults to 1 revolution)
mikermarza 8:f1d869d9b8df 163 * @return the current revolution count after rotation is complete
mikermarza 0:54c5be5f26f4 164 */
mikermarza 8:f1d869d9b8df 165 double rotate(Direction d, float rev);
mikermarza 0:54c5be5f26f4 166
mikermarza 0:54c5be5f26f4 167
mikermarza 8:f1d869d9b8df 168 private:
mikermarza 8:f1d869d9b8df 169 /** Private class Step
mikermarza 8:f1d869d9b8df 170 *
mikermarza 8:f1d869d9b8df 171 * The Linear stepper motor need to have its coils turned on and off in a
mikermarza 8:f1d869d9b8df 172 * precise pattern in order to rotate. Each on/off configuration is called a
mikermarza 8:f1d869d9b8df 173 * step. This class allows for those configurations, which are defined in the
mikermarza 8:f1d869d9b8df 174 * StepNum enum, to be easily written to the motor control as well as allows
mikermarza 8:f1d869d9b8df 175 * for definition of pre- increment/decrement operators.
mikermarza 8:f1d869d9b8df 176 */
mikermarza 0:54c5be5f26f4 177 class Step {
mikermarza 0:54c5be5f26f4 178 public:
mikermarza 0:54c5be5f26f4 179 //constructior
mikermarza 8:f1d869d9b8df 180 Step() : cur_step(ONE) {}; // Default state is step ONE
mikermarza 0:54c5be5f26f4 181
mikermarza 8:f1d869d9b8df 182 //increment step pre increment operator
mikermarza 0:54c5be5f26f4 183 Step_Num operator++();
mikermarza 8:f1d869d9b8df 184 //decrement step pre decrement operator
mikermarza 0:54c5be5f26f4 185 Step_Num operator--();
mikermarza 8:f1d869d9b8df 186 //Sets step equal to the given step
mikermarza 1:757a52db1604 187 void operator=(Step_Num s);
mikermarza 0:54c5be5f26f4 188 //getter for cur_step
mikermarza 0:54c5be5f26f4 189 Step_Num get_cur_step();
mikermarza 0:54c5be5f26f4 190
mikermarza 0:54c5be5f26f4 191 private:
mikermarza 8:f1d869d9b8df 192 // the current step.
mikermarza 7:0d941d1140ad 193 Step_Num cur_step;
mikermarza 0:54c5be5f26f4 194
mikermarza 0:54c5be5f26f4 195 };
mikermarza 0:54c5be5f26f4 196
mikermarza 8:f1d869d9b8df 197 //FEATURE TO BE ADDED
mikermarza 3:2138b69ee3bd 198 /** Spins up the motor by stepping up from min speed to desired speed
mikermarza 0:54c5be5f26f4 199 *
mikermarza 8:f1d869d9b8df 200 * @param rpm the ending speed (defaults to current set speed)
mikermarza 0:54c5be5f26f4 201 */
mikermarza 8:f1d869d9b8df 202 //void spin_up(float rpm=-1);
mikermarza 0:54c5be5f26f4 203
mikermarza 8:f1d869d9b8df 204 //FEATURE TO BE ADDED
mikermarza 3:2138b69ee3bd 205 /** Spins down the motor by stepping speed down from current speed to min_speed
mikermarza 0:54c5be5f26f4 206 *
mikermarza 8:f1d869d9b8df 207 * @param rpm the ending speed (defaults to turning motor off)
mikermarza 0:54c5be5f26f4 208 */
mikermarza 8:f1d869d9b8df 209 //void spin_down(float rpm=-1);
mikermarza 0:54c5be5f26f4 210
mikermarza 0:54c5be5f26f4 211 /** Variables **/
mikermarza 8:f1d869d9b8df 212 BusOut mtr_ctrl; // 4-bit Bus Controlling the H-Brigde
mikermarza 8:f1d869d9b8df 213 // form A B A' B'
mikermarza 8:f1d869d9b8df 214 const int max_speed; // Software Limit for max motor speed in steps/second
mikermarza 8:f1d869d9b8df 215 const float max_rpm; // Software Limit for max motor speed in rpm
mikermarza 8:f1d869d9b8df 216 static const int min_speed
mikermarza 8:f1d869d9b8df 217 = (float)MIN_RPM * 10 / 3; // Software Limit for min motor speed in steps/sec
mikermarza 8:f1d869d9b8df 218 static const int min_rpm = MIN_RPM; // Software Limit for min motor speed in rpm
mikermarza 8:f1d869d9b8df 219 volatile int speed; // Speed of Rotation (in steps per second)
mikermarza 8:f1d869d9b8df 220 volatile double rev_cnt; // Current Revolution count of motor
mikermarza 8:f1d869d9b8df 221 volatile double min_rev_cnt; // software limit for lowest rev count the moter can reach.
mikermarza 8:f1d869d9b8df 222 volatile double max_rev_cnt; // software limit for highest rev count the moter can reach.
mikermarza 7:0d941d1140ad 223 Step cur_step; // Current Step, i.e. which one was just written to the motor
mikermarza 7:0d941d1140ad 224 Direction dir; // The direction for the motor to run
mikermarza 0:54c5be5f26f4 225 };
mikermarza 0:54c5be5f26f4 226 #endif