1 /* 2 * Author: Jon Trulson <jtrulson@ics.com> 3 * Copyright (c) 2015 Intel Corporation. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 #pragma once 25 26 #include <stdint.h> 27 #include <sys/time.h> 28 29 #include <mraa/gpio.h> 30 #include <mraa/pwm.h> 31 32 namespace upm { 33 34 /** 35 * @brief ULN200XA Stepper Driver library 36 * @defgroup uln200xa libupm-uln200xa 37 * @ingroup seeed gpio motor 38 */ 39 40 /** 41 * @library uln200xa 42 * @sensor uln200xa 43 * @comname ULN200XA Stepper Driver 44 * @altname ULN2001A ULN2002A ULN2003A ULN2004A 45 * @type motor 46 * @man seeed 47 * @web http://www.seeedstudio.com/depot/Gear-Stepper-Motor-with-Driver-p-1685.html?cPath=39_40 48 * @con gpio 49 * 50 * @brief UPM module for the ULN200XA Darlington Stepper Driver 51 * 52 * This module was developed on a ULN2003A Stepper Driver. It 53 * should also support the ULN2001A, ULN2002A, and ULN2004A devices, when 54 * using to drive the 28BYJ-48 unipolar stepper motor. 55 * 56 * @image html uln200xa.jpg 57 * Example driving a stepper motor 58 * @snippet uln200xa.cxx Interesting 59 */ 60 61 62 class ULN200XA { 63 public: 64 65 /** 66 * Enum to specify the direction of a motor 67 */ 68 typedef enum { 69 DIR_CW = 0x01, 70 DIR_CCW = 0x02 71 } ULN200XA_DIRECTION_T; 72 73 /** 74 * ULN200XA constructor 75 * 76 * @param stepsPerRev Number of steps per full revolution 77 * @param i1 Digital pin to use for stepper input 1 78 * @param i2 Digital pin to use for stepper input 2 79 * @param i3 Digital pin to use for stepper input 3 80 * @param i4 Digital pin to use for stepper input 4 81 */ 82 ULN200XA(int stepsPerRev, int i1, int i2, int i3, int i4); 83 84 /** 85 * ULN200XA destructor 86 */ 87 ~ULN200XA(); 88 89 /** 90 * Returns the number of milliseconds elapsed since initClock() 91 * was last called. 92 * 93 * @return Elapsed milliseconds 94 */ 95 uint32_t getMillis(); 96 97 /** 98 * Resets the clock 99 * 100 */ 101 void initClock(); 102 103 /** 104 * Sets the speed of the stepper motor in revolutions per minute (RPM) 105 * 106 * @param speed Speed to set the motor to, in RPM 107 */ 108 void setSpeed(int speed); 109 110 /** 111 * Sets the direction of the motor, clockwise or counterclockwise 112 * 113 * @param dir Direction to set the motor to 114 */ 115 void setDirection(ULN200XA_DIRECTION_T dir); 116 117 /** 118 * Steps the stepper motor a specified number of steps 119 * 120 * @param steps Number of steps to move the stepper motor 121 */ 122 void stepperSteps(unsigned int steps); 123 124 /** 125 * Releases the stepper motor by removing power 126 * 127 */ 128 void release(); 129 130 private: 131 struct timeval m_startTime; 132 133 // stepper (4-wire) 134 mraa_gpio_context m_stepI1; 135 mraa_gpio_context m_stepI2; 136 mraa_gpio_context m_stepI3; 137 mraa_gpio_context m_stepI4; 138 139 // steps per revolution 140 int m_stepsPerRev; 141 int m_currentStep; 142 uint32_t m_stepDelay; 143 144 /** 145 * Steps the stepper motor one tick 146 * 147 */ 148 void stepperStep(); 149 150 /** 151 * Defines the step direction: 1 = forward, -1 = backward 152 * 153 */ 154 int m_stepDirection; 155 }; 156 } 157 158 159