Unipolar Stepper Motor Library
UniStepperMotor.h@2:abb7425e1a5b, 2012-07-17 (annotated)
- Committer:
- bant62
- Date:
- Tue Jul 17 09:10:48 2012 +0000
- Revision:
- 2:abb7425e1a5b
- Parent:
- 1:ce3991daa407
add 256 step.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bant62 | 1:ce3991daa407 | 1 | /** |
bant62 | 1:ce3991daa407 | 2 | ***************************************************************************** |
bant62 | 1:ce3991daa407 | 3 | * File Name : UniStepperMotor.h |
bant62 | 1:ce3991daa407 | 4 | * |
bant62 | 1:ce3991daa407 | 5 | * Title : Unipolar Stepper Motor Class Header File |
bant62 | 1:ce3991daa407 | 6 | * Revision : 0.1 |
bant62 | 1:ce3991daa407 | 7 | * Notes : |
bant62 | 1:ce3991daa407 | 8 | * Target Board : mbed NXP LPC1768 |
bant62 | 1:ce3991daa407 | 9 | * Tool Chain : ???? |
bant62 | 1:ce3991daa407 | 10 | * |
bant62 | 1:ce3991daa407 | 11 | * Revision History: |
bant62 | 1:ce3991daa407 | 12 | * When Who Description of change |
bant62 | 1:ce3991daa407 | 13 | * ----------- ----------- ----------------------- |
bant62 | 1:ce3991daa407 | 14 | * 2012/07/8 Hiroshi M init |
bant62 | 1:ce3991daa407 | 15 | ***************************************************************************** |
bant62 | 1:ce3991daa407 | 16 | * |
bant62 | 1:ce3991daa407 | 17 | * Copyright (C) 2012 Hiroshi M, MIT License |
bant62 | 1:ce3991daa407 | 18 | * |
bant62 | 1:ce3991daa407 | 19 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
bant62 | 1:ce3991daa407 | 20 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
bant62 | 1:ce3991daa407 | 21 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
bant62 | 1:ce3991daa407 | 22 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
bant62 | 1:ce3991daa407 | 23 | * furnished to do so, subject to the following conditions: |
bant62 | 1:ce3991daa407 | 24 | * |
bant62 | 1:ce3991daa407 | 25 | * The above copyright notice and this permission notice shall be included in all copies or |
bant62 | 1:ce3991daa407 | 26 | * substantial portions of the Software. |
bant62 | 1:ce3991daa407 | 27 | * |
bant62 | 1:ce3991daa407 | 28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
bant62 | 1:ce3991daa407 | 29 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
bant62 | 1:ce3991daa407 | 30 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
bant62 | 1:ce3991daa407 | 31 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
bant62 | 1:ce3991daa407 | 32 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
bant62 | 1:ce3991daa407 | 33 | * |
bant62 | 1:ce3991daa407 | 34 | **/ |
bant62 | 1:ce3991daa407 | 35 | |
bant62 | 1:ce3991daa407 | 36 | #ifndef MBED_STEPPER_MOTOR_H |
bant62 | 1:ce3991daa407 | 37 | #define MBED_STEPPER_MOTOR_H |
bant62 | 1:ce3991daa407 | 38 | |
bant62 | 1:ce3991daa407 | 39 | /* Includes ----------------------------------------------------------------- */ |
bant62 | 1:ce3991daa407 | 40 | #include "mbed.h" |
bant62 | 1:ce3991daa407 | 41 | |
bant62 | 1:ce3991daa407 | 42 | /* typedef ------------------------------------------------------------------ */ |
bant62 | 1:ce3991daa407 | 43 | typedef enum {PWM_ON, PWM_OFF} pwm_state; |
bant62 | 1:ce3991daa407 | 44 | typedef enum {CLOCK_WISE, ANTI_CLOCK_WISE} motor_dir; |
bant62 | 1:ce3991daa407 | 45 | |
bant62 | 1:ce3991daa407 | 46 | struct speed_data_t { |
bant62 | 1:ce3991daa407 | 47 | int max_pulse_count; |
bant62 | 1:ce3991daa407 | 48 | int pwm_ratio; |
bant62 | 1:ce3991daa407 | 49 | uint8_t *clockwise_ptn; |
bant62 | 1:ce3991daa407 | 50 | uint8_t *anticlockwise_ptn; |
bant62 | 1:ce3991daa407 | 51 | int ptn_count; |
bant62 | 1:ce3991daa407 | 52 | }; |
bant62 | 1:ce3991daa407 | 53 | |
bant62 | 1:ce3991daa407 | 54 | /* define ------------------------------------------------------------------- */ |
bant62 | 2:abb7425e1a5b | 55 | #define PULSE_INTERVAL 14 // 14us |
bant62 | 2:abb7425e1a5b | 56 | #define MAXSPEED 255 |
bant62 | 1:ce3991daa407 | 57 | |
bant62 | 1:ce3991daa407 | 58 | /* macro -------------------------------------------------------------------- */ |
bant62 | 1:ce3991daa407 | 59 | /* variables ---------------------------------------------------------------- */ |
bant62 | 1:ce3991daa407 | 60 | /* class -------------------------------------------------------------------- */ |
bant62 | 1:ce3991daa407 | 61 | |
bant62 | 1:ce3991daa407 | 62 | class StepperMotor { |
bant62 | 1:ce3991daa407 | 63 | public: |
bant62 | 1:ce3991daa407 | 64 | StepperMotor(PinName x_pin_no, PinName y_pin_no, PinName nx_pin_no, PinName ny_pin_no, motor_dir set_direction); |
bant62 | 1:ce3991daa407 | 65 | ~StepperMotor(); |
bant62 | 1:ce3991daa407 | 66 | |
bant62 | 1:ce3991daa407 | 67 | void SetSpeed(int speed); |
bant62 | 1:ce3991daa407 | 68 | void PulseEnable(void); |
bant62 | 1:ce3991daa407 | 69 | void PulseDisable(void); |
bant62 | 1:ce3991daa407 | 70 | |
bant62 | 1:ce3991daa407 | 71 | private: |
bant62 | 1:ce3991daa407 | 72 | pwm_state state; |
bant62 | 1:ce3991daa407 | 73 | motor_dir direction; |
bant62 | 1:ce3991daa407 | 74 | motor_dir forward_direction; |
bant62 | 1:ce3991daa407 | 75 | motor_dir backward_direction; |
bant62 | 1:ce3991daa407 | 76 | |
bant62 | 1:ce3991daa407 | 77 | int ptn_index; |
bant62 | 1:ce3991daa407 | 78 | int ptn_count; |
bant62 | 1:ce3991daa407 | 79 | int pwm_ratio; |
bant62 | 1:ce3991daa407 | 80 | int pwm_on_count; |
bant62 | 1:ce3991daa407 | 81 | int pwm_off_count; |
bant62 | 1:ce3991daa407 | 82 | int max_pulse_count; |
bant62 | 1:ce3991daa407 | 83 | int pulse_count; |
bant62 | 1:ce3991daa407 | 84 | uint8_t *ptn; |
bant62 | 1:ce3991daa407 | 85 | uint8_t ptn_data; |
bant62 | 1:ce3991daa407 | 86 | |
bant62 | 1:ce3991daa407 | 87 | DigitalOut _x; |
bant62 | 1:ce3991daa407 | 88 | DigitalOut _y; |
bant62 | 1:ce3991daa407 | 89 | DigitalOut _nx; |
bant62 | 1:ce3991daa407 | 90 | DigitalOut _ny; |
bant62 | 1:ce3991daa407 | 91 | |
bant62 | 1:ce3991daa407 | 92 | Ticker Pulse; |
bant62 | 1:ce3991daa407 | 93 | |
bant62 | 1:ce3991daa407 | 94 | void SetPulse14us(void); |
bant62 | 1:ce3991daa407 | 95 | void PulseOut(void); |
bant62 | 1:ce3991daa407 | 96 | void PulseStop(void); |
bant62 | 1:ce3991daa407 | 97 | }; |
bant62 | 1:ce3991daa407 | 98 | |
bant62 | 1:ce3991daa407 | 99 | #endif |