The API for a Stepper Motor providing position and speed control for an Automotive Gauge Stepper Motor.
Dependents: ECE_4180_Lab_4 4180_Lab2_Stepper 4180lab02_stepper 4180Lab2-11 ... more
StepperMotor_X27168.h@0:c346170974bc, 2015-10-20 (annotated)
- Committer:
- agarg45
- Date:
- Tue Oct 20 00:36:06 2015 +0000
- Revision:
- 0:c346170974bc
Stepper Motor X27168 ver 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
agarg45 | 0:c346170974bc | 1 | /** Stepper Motor control library |
agarg45 | 0:c346170974bc | 2 | * |
agarg45 | 0:c346170974bc | 3 | * @class StepperMotor_X27168 |
agarg45 | 0:c346170974bc | 4 | * @author Aditya Garg, Nisarg Himani |
agarg45 | 0:c346170974bc | 5 | * @version 1.0 (Oct-19-2015) |
agarg45 | 0:c346170974bc | 6 | * |
agarg45 | 0:c346170974bc | 7 | * This is the driver for Automotive Gauge Stepper Motor X27168 |
agarg45 | 0:c346170974bc | 8 | * |
agarg45 | 0:c346170974bc | 9 | * ----------------------IMPORTANT-------------------- |
agarg45 | 0:c346170974bc | 10 | * The API assumes motor is postioned at zero as there |
agarg45 | 0:c346170974bc | 11 | * is no way to detect or control that in software |
agarg45 | 0:c346170974bc | 12 | * --------------------------------------------------- |
agarg45 | 0:c346170974bc | 13 | */ |
agarg45 | 0:c346170974bc | 14 | |
agarg45 | 0:c346170974bc | 15 | #ifndef MBED_STEPPER_MOTOR_X27168 |
agarg45 | 0:c346170974bc | 16 | #define MBED_STEPPER_MOTOR_X27168 |
agarg45 | 0:c346170974bc | 17 | |
agarg45 | 0:c346170974bc | 18 | #include "mbed.h" |
agarg45 | 0:c346170974bc | 19 | |
agarg45 | 0:c346170974bc | 20 | #define MAX_POS 629 // Maximum Steps Possible (not degrees) |
agarg45 | 0:c346170974bc | 21 | #define DEFAULT_SPEED 500 // Default Speed in Steps per second |
agarg45 | 0:c346170974bc | 22 | |
agarg45 | 0:c346170974bc | 23 | /** Stepper Motor control class |
agarg45 | 0:c346170974bc | 24 | * |
agarg45 | 0:c346170974bc | 25 | * Example: |
agarg45 | 0:c346170974bc | 26 | * @code |
agarg45 | 0:c346170974bc | 27 | * #include "mbed.h" |
agarg45 | 0:c346170974bc | 28 | * #include "StepperMotor_X27168.h" |
agarg45 | 0:c346170974bc | 29 | * |
agarg45 | 0:c346170974bc | 30 | * StepperMotor_X27168 smotor(p25, p26, p23, p22); |
agarg45 | 0:c346170974bc | 31 | * |
agarg45 | 0:c346170974bc | 32 | * int main() { |
agarg45 | 0:c346170974bc | 33 | * |
agarg45 | 0:c346170974bc | 34 | * smotor.step_position(180); |
agarg45 | 0:c346170974bc | 35 | * wait(0.5); |
agarg45 | 0:c346170974bc | 36 | * |
agarg45 | 0:c346170974bc | 37 | * smotor.step_position(100); |
agarg45 | 0:c346170974bc | 38 | * wait(0.5); |
agarg45 | 0:c346170974bc | 39 | * |
agarg45 | 0:c346170974bc | 40 | * smotor.angle_position(270); |
agarg45 | 0:c346170974bc | 41 | * wait(0.5); |
agarg45 | 0:c346170974bc | 42 | * |
agarg45 | 0:c346170974bc | 43 | * smotor.step_position(0); |
agarg45 | 0:c346170974bc | 44 | * wait(0.5); |
agarg45 | 0:c346170974bc | 45 | * } |
agarg45 | 0:c346170974bc | 46 | * @endcode |
agarg45 | 0:c346170974bc | 47 | */ |
agarg45 | 0:c346170974bc | 48 | |
agarg45 | 0:c346170974bc | 49 | class StepperMotor_X27168 { |
agarg45 | 0:c346170974bc | 50 | |
agarg45 | 0:c346170974bc | 51 | public: |
agarg45 | 0:c346170974bc | 52 | |
agarg45 | 0:c346170974bc | 53 | /** Constants for motor rotate control */ |
agarg45 | 0:c346170974bc | 54 | typedef enum { |
agarg45 | 0:c346170974bc | 55 | COIL_A_FOR = 0, // Forward Polarity in H-Bright Port A |
agarg45 | 0:c346170974bc | 56 | COIL_B_FOR = 1, // Forward Polarity in H-Bright Port B |
agarg45 | 0:c346170974bc | 57 | COIL_A_REV = 2, // Reverse Polarity in H-Bright Port A |
agarg45 | 0:c346170974bc | 58 | COIL_B_REV = 3, // Reverse Polarity in H-Bright Port B |
agarg45 | 0:c346170974bc | 59 | STOP_MOTOR = 4, // Turn Off Both Coils |
agarg45 | 0:c346170974bc | 60 | } Polarity; |
agarg45 | 0:c346170974bc | 61 | |
agarg45 | 0:c346170974bc | 62 | /** Create a stepper motor object connected to specified DigitalOut pins |
agarg45 | 0:c346170974bc | 63 | * |
agarg45 | 0:c346170974bc | 64 | * @param A_f DigitalOut pin for Forward Control of H-Brigde Port A (AIN1) |
agarg45 | 0:c346170974bc | 65 | * @param A_r DigitalOut pin for Reverse Control of H-Brigde Port A (AIN2) |
agarg45 | 0:c346170974bc | 66 | * @param B_f DigitalOut pin for Forward Control of H-Brigde Port B (BIN1) |
agarg45 | 0:c346170974bc | 67 | * @param B_r DigitalOut pin for Reverse Control of H-Brigde Port B (BIN2) |
agarg45 | 0:c346170974bc | 68 | */ |
agarg45 | 0:c346170974bc | 69 | StepperMotor_X27168(PinName A_f, PinName A_r, PinName B_f, PinName B_r); |
agarg45 | 0:c346170974bc | 70 | |
agarg45 | 0:c346170974bc | 71 | /** Create a stepper motor object connected to specified DigitalOut pins |
agarg45 | 0:c346170974bc | 72 | * starting at specified position |
agarg45 | 0:c346170974bc | 73 | * |
agarg45 | 0:c346170974bc | 74 | * @param A_f DigitalOut pin for Forward Control of H-Brigde Port A (AIN1) |
agarg45 | 0:c346170974bc | 75 | * @param A_r DigitalOut pin for Reverse Control of H-Brigde Port A (AIN2) |
agarg45 | 0:c346170974bc | 76 | * @param B_f DigitalOut pin for Forward Control of H-Brigde Port B (BIN1) |
agarg45 | 0:c346170974bc | 77 | * @param B_r DigitalOut pin for Reverse Control of H-Brigde Port B (BIN2) |
agarg45 | 0:c346170974bc | 78 | * @param init_step_position Rotate of given initial step position |
agarg45 | 0:c346170974bc | 79 | */ |
agarg45 | 0:c346170974bc | 80 | StepperMotor_X27168(PinName A_f, PinName A_r, PinName B_f, PinName B_r, int init_step_position); |
agarg45 | 0:c346170974bc | 81 | |
agarg45 | 0:c346170974bc | 82 | /** Set the motor speed (i.e. number of steps per second) |
agarg45 | 0:c346170974bc | 83 | * Motor will malfuntion is speed is faster than the |
agarg45 | 0:c346170974bc | 84 | * the maximum capability if the motor. |
agarg45 | 0:c346170974bc | 85 | * |
agarg45 | 0:c346170974bc | 86 | * @param s steps per second : lower number makes the turn slower (default = 1000) |
agarg45 | 0:c346170974bc | 87 | */ |
agarg45 | 0:c346170974bc | 88 | void set_speed(float s); |
agarg45 | 0:c346170974bc | 89 | |
agarg45 | 0:c346170974bc | 90 | /** Get the motor speed (i.e. number of steps per second) |
agarg45 | 0:c346170974bc | 91 | * |
agarg45 | 0:c346170974bc | 92 | * @return steps per second |
agarg45 | 0:c346170974bc | 93 | */ |
agarg45 | 0:c346170974bc | 94 | int get_speed(); |
agarg45 | 0:c346170974bc | 95 | |
agarg45 | 0:c346170974bc | 96 | /** Set the maximum steps the motor should take (not degrees) |
agarg45 | 0:c346170974bc | 97 | * |
agarg45 | 0:c346170974bc | 98 | * @param p maximum_steps :(ignored if parameter is greater than 629, the physical limit of the motor) |
agarg45 | 0:c346170974bc | 99 | */ |
agarg45 | 0:c346170974bc | 100 | void set_max_position(int p); |
agarg45 | 0:c346170974bc | 101 | |
agarg45 | 0:c346170974bc | 102 | /** Get the motor maximum position (int steps not degress) |
agarg45 | 0:c346170974bc | 103 | * |
agarg45 | 0:c346170974bc | 104 | * @return maximum position |
agarg45 | 0:c346170974bc | 105 | */ |
agarg45 | 0:c346170974bc | 106 | int get_max_position(); |
agarg45 | 0:c346170974bc | 107 | |
agarg45 | 0:c346170974bc | 108 | /** Turn the motor one step (1/2 Degree) |
agarg45 | 0:c346170974bc | 109 | * |
agarg45 | 0:c346170974bc | 110 | * @param dir 0 CLOCKWISE/FORWARD |
agarg45 | 0:c346170974bc | 111 | * 1 ANTI-CLOCKWISE/REVERSE |
agarg45 | 0:c346170974bc | 112 | * 2 STOP |
agarg45 | 0:c346170974bc | 113 | * |
agarg45 | 0:c346170974bc | 114 | * @return current_position of the motor |
agarg45 | 0:c346170974bc | 115 | */ |
agarg45 | 0:c346170974bc | 116 | int step(int dir); |
agarg45 | 0:c346170974bc | 117 | |
agarg45 | 0:c346170974bc | 118 | /** Turn the motor to a specific step |
agarg45 | 0:c346170974bc | 119 | * |
agarg45 | 0:c346170974bc | 120 | * @param pos desired position in steps (0-max_position) |
agarg45 | 0:c346170974bc | 121 | */ |
agarg45 | 0:c346170974bc | 122 | void step_position(int pos); |
agarg45 | 0:c346170974bc | 123 | |
agarg45 | 0:c346170974bc | 124 | /** Turn the motor to a specific degree angle with a resolution of 0.5 degrees |
agarg45 | 0:c346170974bc | 125 | * |
agarg45 | 0:c346170974bc | 126 | * @param angle desired angle (0-(max_positon/2)) |
agarg45 | 0:c346170974bc | 127 | */ |
agarg45 | 0:c346170974bc | 128 | void angle_position(float angle); |
agarg45 | 0:c346170974bc | 129 | |
agarg45 | 0:c346170974bc | 130 | private: |
agarg45 | 0:c346170974bc | 131 | BusOut motor_control; // 4-bit Bus Controlling the H-Brigde |
agarg45 | 0:c346170974bc | 132 | int max_position; // Software Limit to motor rotation |
agarg45 | 0:c346170974bc | 133 | int speed; // Speed of Rotation |
agarg45 | 0:c346170974bc | 134 | int cur_position; // Current Position of Motor (0-max_position) |
agarg45 | 0:c346170974bc | 135 | Polarity cur_state; // Current State of H-Brige Controls |
agarg45 | 0:c346170974bc | 136 | }; |
agarg45 | 0:c346170974bc | 137 | #endif |