|
@@ -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
|
+}
|