Просмотр исходного кода

uln200xa: Initial implementation

This driver was tested on a Grove Geared Stepper with Driver, the
uln2003a paired with a 28BYJ-48 unipolar stepper motor.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
Jon Trulson 9 лет назад
Родитель
Сommit
2a3485cf98

+ 3
- 0
examples/c++/CMakeLists.txt Просмотреть файл

@@ -111,6 +111,7 @@ add_executable (waterlevel-example waterlevel.cxx)
111 111
 add_executable (tm1637-example tm1637.cxx)
112 112
 add_executable (zfm20-example zfm20.cxx)
113 113
 add_executable (zfm20-register-example zfm20-register.cxx)
114
+add_executable (uln200xa-example uln200xa.cxx)
114 115
 
115 116
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
116 117
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -200,6 +201,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/apds9002)
200 201
 include_directories (${PROJECT_SOURCE_DIR}/src/waterlevel)
201 202
 include_directories (${PROJECT_SOURCE_DIR}/src/tm1637)
202 203
 include_directories (${PROJECT_SOURCE_DIR}/src/zfm20)
204
+include_directories (${PROJECT_SOURCE_DIR}/src/uln200xa)
203 205
 
204 206
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
205 207
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -312,3 +314,4 @@ target_link_libraries (waterlevel-example waterlevel ${CMAKE_THREAD_LIBS_INIT})
312 314
 target_link_libraries (tm1637-example tm1637 ${CMAKE_THREAD_LIBS_INIT})
313 315
 target_link_libraries (zfm20-example zfm20 ${CMAKE_THREAD_LIBS_INIT})
314 316
 target_link_libraries (zfm20-register-example zfm20 ${CMAKE_THREAD_LIBS_INIT})
317
+target_link_libraries (uln200xa-example uln200xa ${CMAKE_THREAD_LIBS_INIT})

+ 62
- 0
examples/c++/uln200xa.cxx Просмотреть файл

@@ -0,0 +1,62 @@
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
+
25
+#include <unistd.h>
26
+#include <iostream>
27
+#include "uln200xa.h"
28
+
29
+using namespace std;
30
+
31
+int main ()
32
+{
33
+  //! [Interesting]
34
+
35
+  // Instantiate a Stepper motor on a ULN200XA Dual H-Bridge.
36
+
37
+  // This was tested with the Grove Gear Stepper Motor with Driver
38
+
39
+  // Wire the pins so that I1 is pin D8, I2 is pin D9, I3 is pin D10 and
40
+  // I4 is pin D11
41
+  upm::ULN200XA* uln200xa = new upm::ULN200XA(4096, 8, 9, 10, 11);
42
+
43
+  uln200xa->setSpeed(5);
44
+  uln200xa->setDirection(upm::ULN200XA::DIR_CW);
45
+  cout << "Rotating 1 revolution clockwise." << endl;
46
+  uln200xa->stepperSteps(4096);
47
+  cout << "Sleeping for 2 seconds..." << endl;
48
+  sleep(2);
49
+  cout << "Rotating 1/2 revolution counter clockwise." << endl;
50
+  uln200xa->setDirection(upm::ULN200XA::DIR_CCW);
51
+  uln200xa->stepperSteps(2048);
52
+
53
+  // turn off the power
54
+  uln200xa->release();
55
+
56
+  //! [Interesting]
57
+  cout << "Exiting..." << endl;
58
+
59
+  delete uln200xa;
60
+  return 0;
61
+}
62
+

+ 71
- 0
examples/javascript/uln200xa.js Просмотреть файл

@@ -0,0 +1,71 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+/*
4
+* Author:  Jon Trulson <jtrulson@ics.com>
5
+* Copyright (c) 2015 Intel Corporation.
6
+*
7
+* Permission is hereby granted, free of charge, to any person obtaining
8
+* a copy of this software and associated documentation files (the
9
+* "Software"), to deal in the Software without restriction, including
10
+* without limitation the rights to use, copy, modify, merge, publish,
11
+* distribute, sublicense, and/or sell copies of the Software, and to
12
+* permit persons to whom the Software is furnished to do so, subject to
13
+* the following conditions:
14
+*
15
+* The above copyright notice and this permission notice shall be
16
+* included in all copies or substantial portions of the Software.
17
+*
18
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+*/
26
+
27
+var Uln200xa_lib = require('jsupm_uln200xa');
28
+
29
+// Instantiate a Stepper motor on a ULN200XA Darlington Motor Driver
30
+// This was tested with the Grove Geared Step Motor with Driver
31
+
32
+// Instantiate a ULN2003XA stepper object
33
+var myUln200xa_obj = new Uln200xa_lib.ULN200XA(4096, 8, 9, 10, 11);
34
+
35
+myUln200xa_obj.goForward = function()
36
+{
37
+    myUln200xa_obj.setSpeed(5); // 5 RPMs
38
+    myUln200xa_obj.setDirection(Uln200xa_lib.ULN200XA.DIR_CW);
39
+    console.log("Rotating 1 revolution clockwise.");
40
+    myUln200xa_obj.stepperSteps(4096);
41
+};
42
+
43
+myUln200xa_obj.reverseDirection = function()
44
+{
45
+	console.log("Rotating 1/2 revolution counter clockwise.");
46
+	myUln200xa_obj.setDirection(Uln200xa_lib.ULN200XA.DIR_CCW);
47
+	myUln200xa_obj.stepperSteps(2048);
48
+};
49
+
50
+myUln200xa_obj.stop = function()
51
+{
52
+	myUln200xa_obj.release();
53
+};
54
+
55
+myUln200xa_obj.quit = function()
56
+{
57
+	myUln200xa_obj = null;
58
+	Uln200xa_lib.cleanUp();
59
+	Uln200xa_lib = null;
60
+	console.log("Exiting");
61
+	process.exit(0);
62
+};
63
+
64
+// Run ULN200xa driven stepper
65
+myUln200xa_obj.goForward();
66
+setTimeout(myUln200xa_obj.reverseDirection, 2000);
67
+setTimeout(function()
68
+{
69
+	myUln200xa_obj.stop();
70
+	myUln200xa_obj.quit();
71
+}, 2000);

+ 66
- 0
examples/python/uln200xa.py Просмотреть файл

@@ -0,0 +1,66 @@
1
+#!/usr/bin/python
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
+
25
+import time, sys, signal, atexit
26
+import pyupm_uln200xa as upmULN200XA
27
+
28
+# Instantiate a Stepper motor on a ULN200XA Darlington Motor Driver
29
+# This was tested with the Grove Geared Step Motor with Driver
30
+
31
+# Instantiate a ULN2003XA stepper object
32
+myUln200xa = upmULN200XA.ULN200XA(4096, 8, 9, 10, 11)
33
+
34
+## Exit handlers ##
35
+# This stops python from printing a stacktrace when you hit control-C
36
+def SIGINTHandler(signum, frame):
37
+	raise SystemExit
38
+
39
+# This lets you run code on exit,
40
+# including functions from myUln200xa
41
+def exitHandler():
42
+	print "Exiting"
43
+	sys.exit(0)
44
+
45
+# Register exit handlers
46
+atexit.register(exitHandler)
47
+signal.signal(signal.SIGINT, SIGINTHandler)
48
+
49
+
50
+myUln200xa.setSpeed(5) # 5 RPMs
51
+myUln200xa.setDirection(upmULN200XA.ULN200XA.DIR_CW)
52
+
53
+print "Rotating 1 revolution clockwise."
54
+myUln200xa.stepperSteps(4096)
55
+
56
+print "Sleeping for 2 seconds..."
57
+time.sleep(2)
58
+
59
+print "Rotating 1/2 revolution counter clockwise."
60
+myUln200xa.setDirection(upmULN200XA.ULN200XA.DIR_CCW)
61
+myUln200xa.stepperSteps(2048)
62
+
63
+# release
64
+myUln200xa.release()
65
+
66
+# exitHandler is called automatically

+ 5
- 0
src/uln200xa/CMakeLists.txt Просмотреть файл

@@ -0,0 +1,5 @@
1
+set (libname "uln200xa")
2
+set (libdescription "upm uln200xa darlington stepper driver")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 8
- 0
src/uln200xa/jsupm_uln200xa.i Просмотреть файл

@@ -0,0 +1,8 @@
1
+%module jsupm_uln200xa
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "uln200xa.h"
6
+%}
7
+
8
+%include "uln200xa.h"

+ 9
- 0
src/uln200xa/pyupm_uln200xa.i Просмотреть файл

@@ -0,0 +1,9 @@
1
+%module pyupm_uln200xa
2
+%include "../upm.i"
3
+
4
+%feature("autodoc", "3");
5
+
6
+%include "uln200xa.h"
7
+%{
8
+    #include "uln200xa.h"
9
+%}

+ 242
- 0
src/uln200xa/uln200xa.cxx Просмотреть файл

@@ -0,0 +1,242 @@
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
+
25
+#include <iostream>
26
+
27
+#include "uln200xa.h"
28
+
29
+using namespace upm;
30
+using namespace std;
31
+
32
+ULN200XA::ULN200XA(int stepsPerRev, int i1, int i2, int i3, int i4)
33
+{
34
+  m_stepsPerRev = stepsPerRev;
35
+  m_currentStep = 0;
36
+  m_stepDelay = 0;
37
+  m_stepDirection = 1;          // default is forward
38
+
39
+  if ( !(m_stepI1 = mraa_gpio_init(i1)) )
40
+    {
41
+      cerr << __FUNCTION__ << ": mraa_gpio_init(i1) failed" << endl;
42
+      return;
43
+    }
44
+  mraa_gpio_dir(m_stepI1, MRAA_GPIO_OUT);
45
+
46
+  if ( !(m_stepI2 = mraa_gpio_init(i2)) )
47
+    {
48
+      cerr << __FUNCTION__ << ": mraa_gpio_init(i2) failed" << endl;
49
+      mraa_gpio_close(m_stepI1);
50
+      return;
51
+    }
52
+  mraa_gpio_dir(m_stepI2, MRAA_GPIO_OUT);
53
+
54
+  if ( !(m_stepI3 = mraa_gpio_init(i3)) )
55
+    {
56
+      cerr << __FUNCTION__ << ": mraa_gpio_init(i3) failed" << endl;
57
+      mraa_gpio_close(m_stepI1);
58
+      mraa_gpio_close(m_stepI2);
59
+      return;
60
+    }
61
+  mraa_gpio_dir(m_stepI3, MRAA_GPIO_OUT);
62
+
63
+  if ( !(m_stepI4 = mraa_gpio_init(i4)) )
64
+    {
65
+      cerr << __FUNCTION__ << ": mraa_gpio_init(i4) failed" << endl;
66
+      mraa_gpio_close(m_stepI1);
67
+      mraa_gpio_close(m_stepI2);
68
+      mraa_gpio_close(m_stepI3);
69
+      
70
+      return;
71
+    }
72
+  mraa_gpio_dir(m_stepI4, MRAA_GPIO_OUT);
73
+
74
+  // set default speed to 1
75
+  setSpeed(1);
76
+}
77
+
78
+void ULN200XA::initClock()
79
+{
80
+  gettimeofday(&m_startTime, NULL);
81
+}
82
+
83
+uint32_t ULN200XA::getMillis()
84
+{
85
+  struct timeval elapsed, now;
86
+  uint32_t elapse;
87
+
88
+  // get current time
89
+  gettimeofday(&now, NULL);
90
+
91
+  // compute the delta since m_startTime
92
+  if( (elapsed.tv_usec = now.tv_usec - m_startTime.tv_usec) < 0 )
93
+    {
94
+      elapsed.tv_usec += 1000000;
95
+      elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec - 1;
96
+    }
97
+  else
98
+    {
99
+      elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec;
100
+    }
101
+
102
+  elapse = (uint32_t)((elapsed.tv_sec * 1000) + (elapsed.tv_usec / 1000));
103
+
104
+  // never return 0
105
+  if (elapse == 0)
106
+    elapse = 1;
107
+
108
+  return elapse;
109
+}
110
+
111
+
112
+ULN200XA::~ULN200XA()
113
+{
114
+  mraa_gpio_close(m_stepI1);
115
+  mraa_gpio_close(m_stepI2);
116
+  mraa_gpio_close(m_stepI3);
117
+  mraa_gpio_close(m_stepI4);
118
+}
119
+
120
+void ULN200XA::setSpeed(int speed)
121
+{
122
+  m_stepDelay = 60 * 1000 / m_stepsPerRev / speed;
123
+}
124
+
125
+void ULN200XA::setDirection(ULN200XA_DIRECTION_T dir)
126
+{
127
+  switch (dir)
128
+    {
129
+    case DIR_CW:
130
+      m_stepDirection = 1;
131
+      break;
132
+    case DIR_CCW:
133
+      m_stepDirection = -1;
134
+      break;
135
+    }
136
+}
137
+
138
+void ULN200XA::stepperStep()
139
+{
140
+  int step = m_currentStep % 8;
141
+
142
+  // This motor requires a different sequencing order in 8-steps than
143
+  // usual.
144
+
145
+  //   Step I0 I1 I2 I3
146
+  //     1  0  0  0  1
147
+  //     2  0  0  1  1
148
+  //     3  0  0  1  0
149
+  //     4  0  1  1  0
150
+  //     5  0  1  0  0
151
+  //     6  1  1  0  0
152
+  //     7  1  0  0  0
153
+  //     8  1  0  0  1
154
+
155
+  switch (step)
156
+    {
157
+    case 0:    // 0001
158
+      mraa_gpio_write(m_stepI1, 0);
159
+      mraa_gpio_write(m_stepI2, 0);
160
+      mraa_gpio_write(m_stepI3, 0);
161
+      mraa_gpio_write(m_stepI4, 1);
162
+      break;
163
+    case 1:    // 0011
164
+      mraa_gpio_write(m_stepI1, 0);
165
+      mraa_gpio_write(m_stepI2, 0);
166
+      mraa_gpio_write(m_stepI3, 1);
167
+      mraa_gpio_write(m_stepI4, 1);
168
+      break;
169
+    case 2:    // 0010
170
+      mraa_gpio_write(m_stepI1, 0);
171
+      mraa_gpio_write(m_stepI2, 0);
172
+      mraa_gpio_write(m_stepI3, 1);
173
+      mraa_gpio_write(m_stepI4, 0);
174
+      break;
175
+    case 3:    // 0110
176
+      mraa_gpio_write(m_stepI1, 0);
177
+      mraa_gpio_write(m_stepI2, 1);
178
+      mraa_gpio_write(m_stepI3, 1);
179
+      mraa_gpio_write(m_stepI4, 0);
180
+      break;
181
+    case 4:    // 0100
182
+      mraa_gpio_write(m_stepI1, 0);
183
+      mraa_gpio_write(m_stepI2, 1);
184
+      mraa_gpio_write(m_stepI3, 0);
185
+      mraa_gpio_write(m_stepI4, 0);
186
+      break;
187
+    case 5:    // 1100
188
+      mraa_gpio_write(m_stepI1, 1);
189
+      mraa_gpio_write(m_stepI2, 1);
190
+      mraa_gpio_write(m_stepI3, 0);
191
+      mraa_gpio_write(m_stepI4, 0);
192
+      break;
193
+    case 6:    // 1000
194
+      mraa_gpio_write(m_stepI1, 1);
195
+      mraa_gpio_write(m_stepI2, 0);
196
+      mraa_gpio_write(m_stepI3, 0);
197
+      mraa_gpio_write(m_stepI4, 0);
198
+      break;
199
+    case 7:    // 1001
200
+      mraa_gpio_write(m_stepI1, 1);
201
+      mraa_gpio_write(m_stepI2, 0);
202
+      mraa_gpio_write(m_stepI3, 0);
203
+      mraa_gpio_write(m_stepI4, 1);
204
+      break;
205
+    }
206
+}
207
+
208
+void ULN200XA::stepperSteps(unsigned int steps)
209
+{
210
+  while (steps > 0)
211
+    {
212
+      if (getMillis() >= m_stepDelay)
213
+        {
214
+          // reset the clock
215
+          initClock();
216
+
217
+          m_currentStep += m_stepDirection;
218
+
219
+          if (m_stepDirection == 1)
220
+            {
221
+              if (m_currentStep >= m_stepsPerRev)
222
+                m_currentStep = 0;
223
+            }
224
+          else
225
+            {
226
+              if (m_currentStep <= 0)
227
+                m_currentStep = m_stepsPerRev;
228
+            }
229
+
230
+          steps--;
231
+          stepperStep();
232
+        }
233
+    }
234
+}
235
+
236
+void ULN200XA::release()
237
+{
238
+  mraa_gpio_write(m_stepI1, 0);
239
+  mraa_gpio_write(m_stepI2, 0);
240
+  mraa_gpio_write(m_stepI3, 0);
241
+  mraa_gpio_write(m_stepI4, 0);
242
+}

+ 157
- 0
src/uln200xa/uln200xa.h Просмотреть файл

@@ -0,0 +1,157 @@
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 UPM support for the ULN200XA series of Darlington Stepper drivers
36
+   * @defgroup uln200xa libupm-uln200xa
37
+   * @ingroup seeed gpio motor
38
+   */
39
+
40
+  /**
41
+   * @sensor uln200xa
42
+   * @library uln200xa
43
+   * @comname ULN200XA Stepper Driver
44
+   * @altname uln2001a uln2002a uln2003a uln2004a
45
+   * @type motor
46
+   * @man seeed
47
+   * @con gpio
48
+   * @web http://www.seeedstudio.com/depot/Gear-Stepper-Motor-with-Driver-p-1685.html?cPath=39_40
49
+   *
50
+   * @brief UPM module for the ULN200XA Darlington Stepper driver
51
+   *
52
+   * This module was developed on a ULBN2003A Stepper Driver.  It
53
+   * should support the uln2001a, 2002a, 2003a, and 2004a devices, for
54
+   * use in driving a unipolar stepper motor, the 28BYJ-48.
55
+   *
56
+   * Example driving a stepper motor
57
+   * @snippet uln200xa.cxx Interesting
58
+   */
59
+
60
+
61
+  class ULN200XA {
62
+  public:
63
+
64
+    /**
65
+     * Enum to specify the direction of a motor
66
+     */
67
+    typedef enum {
68
+      DIR_CW   = 0x01,
69
+      DIR_CCW  = 0x02
70
+    } ULN200XA_DIRECTION_T;
71
+
72
+    /**
73
+     * ULN200XA constructor
74
+     *
75
+     * @param stepsPerRev number of steps per full revolution
76
+     * @param i1 digital pin to use for stepper input 1
77
+     * @param i2 digital pin to use for stepper input 2
78
+     * @param i3 digital pin to use for stepper input 3
79
+     * @param i4 digital pin to use for stepper input 4
80
+     */
81
+    ULN200XA(int stepsPerRev, int i1, int i2, int i3, int i4);
82
+
83
+    /**
84
+     * ULN200XA Destructor
85
+     */
86
+    ~ULN200XA();
87
+
88
+    /**
89
+     * Return the number of milliseconds elapsed since initClock()
90
+     * was last called.
91
+     *
92
+     * @return elapsed milliseconds
93
+     */
94
+    uint32_t getMillis();
95
+
96
+    /**
97
+     * Reset the Clock
98
+     *
99
+     */
100
+    void initClock();
101
+
102
+    /**
103
+     * set the speed of the stepper in RPM (Rotation Per Minute
104
+     *
105
+     * @param speed speed to set the motor to in RPM's
106
+     */
107
+    void setSpeed(int speed);
108
+
109
+    /**
110
+     * set the direction of the motor, clockwise or counter clockwise
111
+     *
112
+     * @param dir direction to set the motor to
113
+     */
114
+    void setDirection(ULN200XA_DIRECTION_T dir);
115
+
116
+    /**
117
+     * step the stepper motor a specified number of steps
118
+     *
119
+     * @param steps number of steps to move the stepper motor
120
+     */
121
+    void stepperSteps(unsigned int steps);
122
+
123
+    /**
124
+     * release the stepper, by removing power
125
+     *
126
+     */
127
+    void release();
128
+
129
+  private:
130
+    struct timeval m_startTime;
131
+
132
+    // stepper (4-wire)
133
+    mraa_gpio_context m_stepI1;
134
+    mraa_gpio_context m_stepI2;
135
+    mraa_gpio_context m_stepI3;
136
+    mraa_gpio_context m_stepI4;
137
+
138
+    // steps per revolution
139
+    int m_stepsPerRev;
140
+    int m_currentStep;
141
+    uint32_t m_stepDelay;
142
+
143
+    /**
144
+     * step the motor one tick
145
+     *
146
+     */
147
+    void stepperStep();
148
+
149
+    /**
150
+     * step direction - 1 = forward, -1 = backward
151
+     *
152
+     */
153
+    int m_stepDirection;
154
+  };
155
+}
156
+
157
+