noka @willow-micro / AsyncStepper
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AsyncStepper.hpp Source File

AsyncStepper.hpp

Go to the documentation of this file.
00001 // -*- coding:utf-8-unix -*-
00002 /*!
00003   @file   AsyncStepper.hpp
00004   
00005   @brief  Drive a stepper motor using A4988 with asynchronous.
00006   
00007   @author   T.Kawamura
00008   @version  1.0
00009   @date     2018-07-12  T.Kawamura  Written for C++/mbed.
00010   @see
00011   Copyright (C) 2018 Takuma Kawamura.
00012   Released under the MIT license.
00013   http://opensource.org/licenses/mit-license.php
00014 */
00015 #ifndef ASYNCSTEPPER_H
00016 #define ASYNCSTEPPER_H
00017 
00018 #include "mbed.h"
00019 
00020 /**
00021      Disable all interuupts and save the status of interrupts.
00022      This macro is usable at ONLY Cortex-M Series. 
00023 */
00024 #define DISABLE_INTERRUPTS uint32_t primask = __get_PRIMASK();  __disable_irq()
00025 /**
00026      Enable all interuupts when the status of interrupts is ENABLED.
00027      This macro is usable at ONLY Cortex-M Series. 
00028 */
00029 #define RESTORE_INTERRUPTS if( !(primask & 1) ) __enable_irq()
00030 
00031 using namespace std;
00032 
00033 enum stepMode_e {
00034   FULL, HALF, QUARTER, EIGHTH, SIXTEENTH
00035 };
00036 enum direction_e {
00037   NEGATIVE, POSITIVE
00038 };
00039 enum stopMode_e {
00040   FREE, LOCKED
00041 };
00042 
00043 /*! 
00044   @class AsyncStepper
00045   @brief Drive a stepper motor using A4988 with asynchronous
00046  */
00047 class AsyncStepper{
00048 private:
00049   Ticker *ticker;
00050   DigitalOut *enableOut;
00051   DigitalOut *dirOut;
00052   DigitalOut *stepOut;
00053   BusOut *stepModeBus;
00054 
00055   stepMode_e stepMode;
00056   PinName ms1Pin;
00057   PinName ms2Pin;
00058   PinName ms3Pin;
00059   uint32_t oneRotationFullSteps;
00060   stopMode_e stopMode;
00061   uint32_t oneRotationSteps;
00062   uint64_t pulseWidth_us;
00063   volatile uint32_t halfPulseCount;
00064   volatile uint32_t currentMaxStepCount;
00065   volatile bool stopPulseOut;
00066   
00067   void ISR_PULSEOUT( void );
00068   
00069   
00070 public:
00071   /*!
00072     @brief Create a new AsyncStepper port.
00073     
00074     @param enablePin  A4988 Enable pin
00075     @param stepPin    A4988 Step pin
00076     @param dirPin     A4988 Direction pin
00077     @param rpm        RPM
00078     @param stepMode   Division ratio
00079     @param ms1Pin     A4988 MS1
00080     @param ms2Pin     A4988 MS2
00081     @param ms3Pin     A4988 MS1
00082     @param oneRotationFullSteps Steps per rotation of your stepper motor
00083     @param stopMode   Enable/Disable holding torque
00084   */
00085   AsyncStepper( PinName enablePin, PinName stepPin, PinName dirPin, uint32_t rpm, stepMode_e stepMode=FULL, PinName ms1Pin=NC, PinName ms2Pin=NC, PinName ms3Pin=NC, uint32_t oneRotationFullSteps=200, stopMode_e stopMode=FREE );
00086 
00087   /*! 
00088     @brief Destrutor of AsyncStepper
00089   */
00090   virtual ~AsyncStepper();
00091 
00092   /*! 
00093     @brief Set RPM of the stepper motor
00094     @param rpm 
00095   */
00096   virtual void SetRPM( uint32_t rpm );
00097 
00098   /*! 
00099     @brief Set division ratio of the stepper motor
00100     @param stepMode division ratio
00101   */
00102   virtual void SetStepMode( stepMode_e stepMode );
00103 
00104   /*! 
00105     @brief Apply the current to the stepper motor
00106   */
00107   virtual void Enable( void );
00108 
00109   /*! 
00110     @brief Stop the current to the stepper motor
00111   */
00112   virtual void Disable( void );
00113 
00114   /*! 
00115     @brief Check the stepper motor stopping
00116     
00117     @retval true Yes (Stopping)
00118     @retval false No
00119   */
00120   virtual bool IsStopping( void );
00121 
00122   /*! 
00123     @brief Rotate the stepper motor
00124     
00125     @param direction POSITIVE or NEGATIVE
00126     @param steps Steps of rotation
00127   */
00128   virtual void Rotate( direction_e direction, uint32_t steps );
00129 };
00130 
00131 
00132 #endif