Michael Marzano / Mbed 2 deprecated Linear_Stepper_Motor_Nema17

Dependencies:   mbed

Committer:
mikermarza
Date:
Tue Apr 28 20:57:32 2020 +0000
Revision:
8:f1d869d9b8df
Parent:
7:0d941d1140ad
Child:
11:2507965c1bec
All filled out. All documentation completed

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 8:f1d869d9b8df 113 /** Getters for {min,max}_rev_cnt, these revolution counts are used as software
mikermarza 8:f1d869d9b8df 114 * stops to prevent the motor from going too far. Default to max/min double
mikermarza 8:f1d869d9b8df 115 *
mikermarza 8:f1d869d9b8df 116 * @return the minimum or maximum revevolution count limit
mikermarza 8:f1d869d9b8df 117 */
mikermarza 8:f1d869d9b8df 118 double get_min_rev_cnt();
mikermarza 8:f1d869d9b8df 119 double get_max_rev_cnt();
mikermarza 8:f1d869d9b8df 120
mikermarza 7:0d941d1140ad 121 /** Getters and Setters for {min,max}_rev_cnt
mikermarza 7:0d941d1140ad 122 *
mikermarza 8:f1d869d9b8df 123 * @param rc new revolution count limit value
mikermarza 7:0d941d1140ad 124 */
mikermarza 7:0d941d1140ad 125 void set_min_rev_cnt(double rc);
mikermarza 7:0d941d1140ad 126 void set_max_rev_cnt(double rc);
mikermarza 7:0d941d1140ad 127
mikermarza 8:f1d869d9b8df 128 /** Rests the revolution count limits to max/min of double
mikermarza 8:f1d869d9b8df 129 */
mikermarza 7:0d941d1140ad 130 void RESET_rev_cnts();
mikermarza 7:0d941d1140ad 131
mikermarza 0:54c5be5f26f4 132
mikermarza 0:54c5be5f26f4 133 /** Gets the current direction
mikermarza 8:f1d869d9b8df 134 *
mikermarza 8:f1d869d9b8df 135 * @return the current direction of motion
mikermarza 0:54c5be5f26f4 136 */
mikermarza 0:54c5be5f26f4 137 Direction get_dir();
mikermarza 0:54c5be5f26f4 138
mikermarza 8:f1d869d9b8df 139 /** NOT SUPORTED - Set the direction
mikermarza 0:54c5be5f26f4 140 *
mikermarza 8:f1d869d9b8df 141 * @param d new direction of motion
mikermarza 0:54c5be5f26f4 142 */
mikermarza 0:54c5be5f26f4 143 //void set_dir(Direction d);
mikermarza 0:54c5be5f26f4 144
mikermarza 0:54c5be5f26f4 145
mikermarza 7:0d941d1140ad 146 /** Rotates the motor for a set number of rotations in the given direction
mikermarza 0:54c5be5f26f4 147 *
mikermarza 8:f1d869d9b8df 148 * @param d direction of rotation
mikermarza 8:f1d869d9b8df 149 * @param rev number of revolutions to rotate (defaults to 1 revolution)
mikermarza 8:f1d869d9b8df 150 * @return the current revolution count after rotation is complete
mikermarza 0:54c5be5f26f4 151 */
mikermarza 8:f1d869d9b8df 152 double rotate(Direction d, float rev);
mikermarza 0:54c5be5f26f4 153
mikermarza 0:54c5be5f26f4 154
mikermarza 8:f1d869d9b8df 155 private:
mikermarza 8:f1d869d9b8df 156 /** Private class Step
mikermarza 8:f1d869d9b8df 157 *
mikermarza 8:f1d869d9b8df 158 * The Linear stepper motor need to have its coils turned on and off in a
mikermarza 8:f1d869d9b8df 159 * precise pattern in order to rotate. Each on/off configuration is called a
mikermarza 8:f1d869d9b8df 160 * step. This class allows for those configurations, which are defined in the
mikermarza 8:f1d869d9b8df 161 * StepNum enum, to be easily written to the motor control as well as allows
mikermarza 8:f1d869d9b8df 162 * for definition of pre- increment/decrement operators.
mikermarza 8:f1d869d9b8df 163 */
mikermarza 0:54c5be5f26f4 164 class Step {
mikermarza 0:54c5be5f26f4 165 public:
mikermarza 0:54c5be5f26f4 166 //constructior
mikermarza 8:f1d869d9b8df 167 Step() : cur_step(ONE) {}; // Default state is step ONE
mikermarza 0:54c5be5f26f4 168
mikermarza 8:f1d869d9b8df 169 //increment step pre increment operator
mikermarza 0:54c5be5f26f4 170 Step_Num operator++();
mikermarza 8:f1d869d9b8df 171 //decrement step pre decrement operator
mikermarza 0:54c5be5f26f4 172 Step_Num operator--();
mikermarza 8:f1d869d9b8df 173 //Sets step equal to the given step
mikermarza 1:757a52db1604 174 void operator=(Step_Num s);
mikermarza 0:54c5be5f26f4 175 //getter for cur_step
mikermarza 0:54c5be5f26f4 176 Step_Num get_cur_step();
mikermarza 0:54c5be5f26f4 177
mikermarza 0:54c5be5f26f4 178 private:
mikermarza 8:f1d869d9b8df 179 // the current step.
mikermarza 7:0d941d1140ad 180 Step_Num cur_step;
mikermarza 0:54c5be5f26f4 181
mikermarza 0:54c5be5f26f4 182 };
mikermarza 0:54c5be5f26f4 183
mikermarza 8:f1d869d9b8df 184 //FEATURE TO BE ADDED
mikermarza 3:2138b69ee3bd 185 /** Spins up the motor by stepping up from min speed to desired speed
mikermarza 0:54c5be5f26f4 186 *
mikermarza 8:f1d869d9b8df 187 * @param rpm the ending speed (defaults to current set speed)
mikermarza 0:54c5be5f26f4 188 */
mikermarza 8:f1d869d9b8df 189 //void spin_up(float rpm=-1);
mikermarza 0:54c5be5f26f4 190
mikermarza 8:f1d869d9b8df 191 //FEATURE TO BE ADDED
mikermarza 3:2138b69ee3bd 192 /** Spins down the motor by stepping speed down from current speed to min_speed
mikermarza 0:54c5be5f26f4 193 *
mikermarza 8:f1d869d9b8df 194 * @param rpm the ending speed (defaults to turning motor off)
mikermarza 0:54c5be5f26f4 195 */
mikermarza 8:f1d869d9b8df 196 //void spin_down(float rpm=-1);
mikermarza 0:54c5be5f26f4 197
mikermarza 0:54c5be5f26f4 198 /** Variables **/
mikermarza 8:f1d869d9b8df 199 BusOut mtr_ctrl; // 4-bit Bus Controlling the H-Brigde
mikermarza 8:f1d869d9b8df 200 // form A B A' B'
mikermarza 8:f1d869d9b8df 201 const int max_speed; // Software Limit for max motor speed in steps/second
mikermarza 8:f1d869d9b8df 202 const float max_rpm; // Software Limit for max motor speed in rpm
mikermarza 8:f1d869d9b8df 203 static const int min_speed
mikermarza 8:f1d869d9b8df 204 = (float)MIN_RPM * 10 / 3; // Software Limit for min motor speed in steps/sec
mikermarza 8:f1d869d9b8df 205 static const int min_rpm = MIN_RPM; // Software Limit for min motor speed in rpm
mikermarza 8:f1d869d9b8df 206 volatile int speed; // Speed of Rotation (in steps per second)
mikermarza 8:f1d869d9b8df 207 volatile double rev_cnt; // Current Revolution count of motor
mikermarza 8:f1d869d9b8df 208 volatile double min_rev_cnt; // software limit for lowest rev count the moter can reach.
mikermarza 8:f1d869d9b8df 209 volatile double max_rev_cnt; // software limit for highest rev count the moter can reach.
mikermarza 7:0d941d1140ad 210 Step cur_step; // Current Step, i.e. which one was just written to the motor
mikermarza 7:0d941d1140ad 211 Direction dir; // The direction for the motor to run
mikermarza 0:54c5be5f26f4 212 };
mikermarza 0:54c5be5f26f4 213 #endif