Michael Marzano / Mbed 2 deprecated Linear_Stepper_Motor_Nema17

Dependencies:   mbed

Committer:
mikermarza
Date:
Mon Apr 27 01:01:59 2020 +0000
Revision:
5:955bbc08ee78
Parent:
4:1dc350268172
Child:
7:0d941d1140ad
Added code for starting in a given direction

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 #include "rtos.h"
mikermarza 0:54c5be5f26f4 19
mikermarza 0:54c5be5f26f4 20
mikermarza 0:54c5be5f26f4 21
mikermarza 0:54c5be5f26f4 22 /**********
mikermarza 0:54c5be5f26f4 23 * Defines *
mikermarza 0:54c5be5f26f4 24 **********/
mikermarza 3:2138b69ee3bd 25 #define DEFAULT_RPM 300 // RPM (1rpm = 3.333 steps/sec)
mikermarza 3:2138b69ee3bd 26 #define MAX_RPM 400 // RPM
mikermarza 3:2138b69ee3bd 27 #define MIN_RPM 150 // in RPM
mikermarza 0:54c5be5f26f4 28
mikermarza 0:54c5be5f26f4 29 /** Linear Stepper Motor control class
mikermarza 0:54c5be5f26f4 30 *
mikermarza 0:54c5be5f26f4 31 * Example:
mikermarza 0:54c5be5f26f4 32 * @code
mikermarza 0:54c5be5f26f4 33 * #include "mbed.h"
mikermarza 0:54c5be5f26f4 34 * #include "LinStepMtr.h"
mikermarza 0:54c5be5f26f4 35 *
mikermarza 0:54c5be5f26f4 36 *
mikermarza 0:54c5be5f26f4 37 * @endcode
mikermarza 0:54c5be5f26f4 38 */
mikermarza 0:54c5be5f26f4 39
mikermarza 0:54c5be5f26f4 40 class Step {
mikermarza 0:54c5be5f26f4 41 public:
mikermarza 0:54c5be5f26f4 42
mikermarza 0:54c5be5f26f4 43 private:
mikermarza 0:54c5be5f26f4 44
mikermarza 0:54c5be5f26f4 45 };
mikermarza 0:54c5be5f26f4 46
mikermarza 0:54c5be5f26f4 47 class LinStepMtr {
mikermarza 0:54c5be5f26f4 48
mikermarza 0:54c5be5f26f4 49 public:
mikermarza 0:54c5be5f26f4 50
mikermarza 0:54c5be5f26f4 51 /** Constants for motor rotate control */
mikermarza 0:54c5be5f26f4 52 typedef enum {
mikermarza 0:54c5be5f26f4 53 COIL_A_FOR = 0, // Forward Polarity in H-Bright Port A
mikermarza 0:54c5be5f26f4 54 COIL_B_FOR = 1, // Forward Polarity in H-Bright Port B
mikermarza 0:54c5be5f26f4 55 COIL_A_REV = 2, // Reverse Polarity in H-Bright Port A
mikermarza 0:54c5be5f26f4 56 COIL_B_REV = 3, // Reverse Polarity in H-Bright Port B
mikermarza 0:54c5be5f26f4 57 STOP_MOTOR = 4 // Turn Off Both Coils
mikermarza 0:54c5be5f26f4 58 } Polarity;
mikermarza 0:54c5be5f26f4 59
mikermarza 0:54c5be5f26f4 60 /** Direction Control **/
mikermarza 0:54c5be5f26f4 61 typedef enum {
mikermarza 0:54c5be5f26f4 62 CW = 0, // Motor is spinning in CLOCKWISE direction
mikermarza 0:54c5be5f26f4 63 CCW = 1, // Motor is spinning in COUNTER-CLOCKWISE direction
mikermarza 0:54c5be5f26f4 64 } Direction;
mikermarza 0:54c5be5f26f4 65
mikermarza 0:54c5be5f26f4 66 /** Steps of motor for movement *
mikermarza 0:54c5be5f26f4 67 * In form A B A' B' */
mikermarza 0:54c5be5f26f4 68 typedef enum {
mikermarza 0:54c5be5f26f4 69 STOP = 0b0000,
mikermarza 0:54c5be5f26f4 70 ONE = 0b1100,
mikermarza 0:54c5be5f26f4 71 TWO = 0b0110,
mikermarza 0:54c5be5f26f4 72 THREE = 0b0011,
mikermarza 0:54c5be5f26f4 73 FOUR = 0b1001
mikermarza 0:54c5be5f26f4 74 } Step_Num;
mikermarza 0:54c5be5f26f4 75
mikermarza 0:54c5be5f26f4 76 /** Create a linear stepper motor object connected to specified DigitalOut pins
mikermarza 0:54c5be5f26f4 77 *
mikermarza 0:54c5be5f26f4 78 * @param A_f DigitalOut pin for Forward Control of H-Brigde Port A (AIN1)
mikermarza 0:54c5be5f26f4 79 * @param A_r DigitalOut pin for Reverse Control of H-Brigde Port A (AIN2)
mikermarza 0:54c5be5f26f4 80 * @param B_f DigitalOut pin for Forward Control of H-Brigde Port B (BIN1)
mikermarza 0:54c5be5f26f4 81 * @param B_r DigitalOut pin for Reverse Control of H-Brigde Port B (BIN2)
mikermarza 0:54c5be5f26f4 82 */
mikermarza 0:54c5be5f26f4 83 LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r);
mikermarza 0:54c5be5f26f4 84
mikermarza 0:54c5be5f26f4 85 /** Create a linear stepper motor object connected to specified DigitalOut pins
mikermarza 0:54c5be5f26f4 86 *
mikermarza 0:54c5be5f26f4 87 * @param A_f DigitalOut pin for Forward Control of H-Brigde Port A (AIN1)
mikermarza 0:54c5be5f26f4 88 * @param A_r DigitalOut pin for Reverse Control of H-Brigde Port A (AIN2)
mikermarza 0:54c5be5f26f4 89 * @param B_f DigitalOut pin for Forward Control of H-Brigde Port B (BIN1)
mikermarza 0:54c5be5f26f4 90 * @param B_r DigitalOut pin for Reverse Control of H-Brigde Port B (BIN2)
mikermarza 3:2138b69ee3bd 91 * @param m_rpm Sets the max speed in RPM of the motor
mikermarza 0:54c5be5f26f4 92 */
mikermarza 3:2138b69ee3bd 93 LinStepMtr(PinName A_f, PinName A_r, PinName B_f, PinName B_r, int m_rpm);
mikermarza 0:54c5be5f26f4 94
mikermarza 0:54c5be5f26f4 95 /** Destructor
mikermarza 0:54c5be5f26f4 96 */
mikermarza 0:54c5be5f26f4 97 ~LinStepMtr();
mikermarza 0:54c5be5f26f4 98
mikermarza 0:54c5be5f26f4 99 /** Gets the current speed in RPM
mikermarza 0:54c5be5f26f4 100 *
mikermarza 0:54c5be5f26f4 101 */
mikermarza 0:54c5be5f26f4 102 float get_speed();
mikermarza 0:54c5be5f26f4 103
mikermarza 0:54c5be5f26f4 104 /** Sets the value of speed in RPM
mikermarza 0:54c5be5f26f4 105 */
mikermarza 0:54c5be5f26f4 106 //void set_speed(float f);
mikermarza 0:54c5be5f26f4 107
mikermarza 0:54c5be5f26f4 108 /** Gets the number of revolutions since motor was initialized.
mikermarza 0:54c5be5f26f4 109 * Positive means more CW than CCW movement, negative is opposite
mikermarza 0:54c5be5f26f4 110 */
mikermarza 0:54c5be5f26f4 111 double get_rev();
mikermarza 0:54c5be5f26f4 112
mikermarza 0:54c5be5f26f4 113
mikermarza 0:54c5be5f26f4 114 /** Gets the current direction
mikermarza 0:54c5be5f26f4 115 *
mikermarza 0:54c5be5f26f4 116 */
mikermarza 0:54c5be5f26f4 117 Direction get_dir();
mikermarza 0:54c5be5f26f4 118
mikermarza 0:54c5be5f26f4 119 /** Set the direction
mikermarza 0:54c5be5f26f4 120 *
mikermarza 0:54c5be5f26f4 121 */
mikermarza 0:54c5be5f26f4 122 //void set_dir(Direction d);
mikermarza 0:54c5be5f26f4 123
mikermarza 0:54c5be5f26f4 124 /** Initializes the rotate thread and sets parameters before you begin
mikermarza 0:54c5be5f26f4 125 *
mikermarza 0:54c5be5f26f4 126 */
mikermarza 3:2138b69ee3bd 127 void init(double rpm=DEFAULT_RPM, Direction d=CW);
mikermarza 0:54c5be5f26f4 128
mikermarza 0:54c5be5f26f4 129 /** Ends possibility of motion
mikermarza 0:54c5be5f26f4 130 *
mikermarza 0:54c5be5f26f4 131 */
mikermarza 0:54c5be5f26f4 132 void end();
mikermarza 0:54c5be5f26f4 133
mikermarza 0:54c5be5f26f4 134 /** Rotates the motor for a set number of rotations
mikermarza 0:54c5be5f26f4 135 *
mikermarza 0:54c5be5f26f4 136 */
mikermarza 1:757a52db1604 137 //double rotate(float num_rev);
mikermarza 0:54c5be5f26f4 138
mikermarza 0:54c5be5f26f4 139 /** Starts continuous motion in the current direction
mikermarza 0:54c5be5f26f4 140 *
mikermarza 0:54c5be5f26f4 141 */
mikermarza 0:54c5be5f26f4 142 void start();
mikermarza 0:54c5be5f26f4 143
mikermarza 5:955bbc08ee78 144 /** Starts continuous motion in the given direction
mikermarza 5:955bbc08ee78 145 *
mikermarza 5:955bbc08ee78 146 */
mikermarza 5:955bbc08ee78 147 void start(Direction d);
mikermarza 5:955bbc08ee78 148
mikermarza 0:54c5be5f26f4 149 /** Stops continuous motion
mikermarza 0:54c5be5f26f4 150 *
mikermarza 0:54c5be5f26f4 151 */
mikermarza 0:54c5be5f26f4 152 void stop();
mikermarza 0:54c5be5f26f4 153
mikermarza 0:54c5be5f26f4 154 /** Changes the direction of motion
mikermarza 0:54c5be5f26f4 155 *
mikermarza 0:54c5be5f26f4 156 */
mikermarza 0:54c5be5f26f4 157 void change_dir(Direction d);
mikermarza 0:54c5be5f26f4 158
mikermarza 0:54c5be5f26f4 159 /** Changes the speed
mikermarza 0:54c5be5f26f4 160 *
mikermarza 0:54c5be5f26f4 161 */
mikermarza 3:2138b69ee3bd 162 void change_speed(float rpm);
mikermarza 0:54c5be5f26f4 163
mikermarza 0:54c5be5f26f4 164
mikermarza 0:54c5be5f26f4 165
mikermarza 5:955bbc08ee78 166
mikermarza 0:54c5be5f26f4 167
mikermarza 0:54c5be5f26f4 168 class Step {
mikermarza 0:54c5be5f26f4 169 public:
mikermarza 0:54c5be5f26f4 170 //constructior
mikermarza 0:54c5be5f26f4 171 Step() : cur_step(ONE) {}; // Step();
mikermarza 0:54c5be5f26f4 172
mikermarza 0:54c5be5f26f4 173 //increment step post incr operator
mikermarza 0:54c5be5f26f4 174 Step_Num operator++();
mikermarza 0:54c5be5f26f4 175 //decrement step post incr operator
mikermarza 0:54c5be5f26f4 176 Step_Num operator--();
mikermarza 1:757a52db1604 177 //Sets step
mikermarza 1:757a52db1604 178 void operator=(Step_Num s);
mikermarza 0:54c5be5f26f4 179 //getter for cur_step
mikermarza 0:54c5be5f26f4 180 Step_Num get_cur_step();
mikermarza 0:54c5be5f26f4 181
mikermarza 0:54c5be5f26f4 182 private:
mikermarza 0:54c5be5f26f4 183 static const int step_one = 0b1100;
mikermarza 0:54c5be5f26f4 184 static const int step_two = 0b0110;
mikermarza 0:54c5be5f26f4 185 static const int step_three = 0b0011;
mikermarza 0:54c5be5f26f4 186 static const int step_four = 0b1001;
mikermarza 0:54c5be5f26f4 187 volatile Step_Num cur_step;
mikermarza 0:54c5be5f26f4 188
mikermarza 0:54c5be5f26f4 189 };
mikermarza 5:955bbc08ee78 190 //private:
mikermarza 0:54c5be5f26f4 191
mikermarza 0:54c5be5f26f4 192 /** Continuously rotates the motor in the specified direction
mikermarza 0:54c5be5f26f4 193 * lives in it's own thread
mikermarza 0:54c5be5f26f4 194 */
mikermarza 0:54c5be5f26f4 195 void rotate();
mikermarza 1:757a52db1604 196 static void rotate_help(void const *args);
mikermarza 0:54c5be5f26f4 197
mikermarza 3:2138b69ee3bd 198 /** Spins up the motor by stepping up from min speed to desired speed
mikermarza 0:54c5be5f26f4 199 *
mikermarza 0:54c5be5f26f4 200 */
mikermarza 3:2138b69ee3bd 201 void spin_up(float rpm=-1);
mikermarza 0:54c5be5f26f4 202
mikermarza 3:2138b69ee3bd 203 /** Spins down the motor by stepping speed down from current speed to min_speed
mikermarza 0:54c5be5f26f4 204 *
mikermarza 0:54c5be5f26f4 205 */
mikermarza 3:2138b69ee3bd 206 int spin_down(float rpm=-1);
mikermarza 0:54c5be5f26f4 207
mikermarza 0:54c5be5f26f4 208 /** Variables **/
mikermarza 0:54c5be5f26f4 209 BusOut mtr_ctrl; // 4-bit Bus Controlling the H-Brigde
mikermarza 0:54c5be5f26f4 210 // form A B A' B'
mikermarza 3:2138b69ee3bd 211 const int max_speed; // Software Limit for max rpm in steps/second
mikermarza 3:2138b69ee3bd 212 const float max_rpm;
mikermarza 3:2138b69ee3bd 213 static const int min_speed
mikermarza 3:2138b69ee3bd 214 = (float)MIN_RPM * 10 / 3; // Software Limit for min rpm in steps/sec
mikermarza 3:2138b69ee3bd 215 static const int min_rpm = MIN_RPM;
mikermarza 0:54c5be5f26f4 216 volatile int speed; // Speed of Rotation (in steps per second)
mikermarza 0:54c5be5f26f4 217 volatile double rev_cnt; // Current Revolution of motor
mikermarza 0:54c5be5f26f4 218 Step cur_step; // Current Step, i.e. which one was just written to the motor
mikermarza 0:54c5be5f26f4 219 volatile Direction dir; // The direction for the motor to run
mikermarza 0:54c5be5f26f4 220
mikermarza 1:757a52db1604 221 Thread *rotate_th; // Thread to run the rotate function
mikermarza 0:54c5be5f26f4 222 volatile bool stop_mtr; // Flag for if motor should be off;
mikermarza 0:54c5be5f26f4 223 volatile bool terminate; // Flag for if the rotate function should stop executing
mikermarza 0:54c5be5f26f4 224
mikermarza 0:54c5be5f26f4 225 private:
mikermarza 0:54c5be5f26f4 226 volatile Polarity cur_state; // Current State of H-Brige Controls
mikermarza 0:54c5be5f26f4 227 };
mikermarza 0:54c5be5f26f4 228 #endif