-
Notifications
You must be signed in to change notification settings - Fork 3
/
openclino.ino
301 lines (263 loc) · 6.02 KB
/
openclino.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Define steps per revolution
const int motorSteps = 200;
const int subStep = 16;
const unsigned long stepsPerRevolution = motorSteps * subStep;
// pins
const int buttonPin = 2;
const int enablePinX = 3;
const int stepPinX = 4;
const int dirPinX = 5;
const int enablePinY = 6;
const int stepPinY = 7;
const int dirPinY = 8;
// pulley diameters
const float xMotD = 9.5;
const float xPulleyD = 31.5;
const float yMotD = 9.5;
const float yTurnD = 31.5;
const float yPulleyD = 18.5;
const double xRatio = xMotD / xPulleyD; // diameters
const double yRatio = yMotD / yPulleyD;
const int nStepsXperRot = stepsPerRevolution / xRatio;
const int nStepsYperRot = stepsPerRevolution / yRatio;
int buttonState = 0;
void wake(bool x, bool y)
{
if (x == true)
{
digitalWrite(enablePinX, LOW);
}
else
{
digitalWrite(enablePinX, HIGH);
}
if (y == true)
{
digitalWrite(enablePinY, LOW);
}
else
{
digitalWrite(enablePinY, HIGH);
}
}
void xClockwise(bool foo)
{
if (foo == true)
{
digitalWrite(dirPinX, HIGH);
}
else
{
digitalWrite(dirPinX, LOW);
}
}
void yClockwise(bool foo)
{
if (foo == true)
{
digitalWrite(dirPinY, LOW);
}
else
{
digitalWrite(dirPinY, HIGH);
}
}
// function to continuous spin x and y given <runTime> and rpm where if 0 keep still
// function to spin <degX, degY> and <rpms>
// void spin_degs():
// think about smoothing
void spin_continuous(int t = 1, float speedX = 10, float speedY = 10, int finalDelay = 100)
{
bool xClock;
bool yClock;
if (speedX >= 0)
{
xClock = true;
}
else
{
xClock = false;
}
if (speedY >= 0)
{
yClock = true;
}
else
{
yClock = false;
}
long IntervalX = (6e7 / speedX) / nStepsXperRot;
long IntervalY = (6e7 / speedY) / nStepsYperRot;
unsigned long stepsY = 0;
unsigned long stepsX = 0;
unsigned long previousTimeX = micros();
unsigned long previousTimeY = micros();
while (true)
{
unsigned long currentTimeX = micros();
unsigned long currentTimeY = micros();
bool xGo = false; // whether to step x
bool yGo = false; // whether to step y
bool skipCompensation = false;
digitalWrite(stepPinX, HIGH);
digitalWrite(stepPinY, HIGH);
if (currentTimeX - previousTimeX > IntervalX)
{
xGo = true;
}
if (currentTimeY - previousTimeY > IntervalY)
{
yGo = true;
}
if (yGo == true && xGo == true && yClock == false && xClock != yClock)
{
skipCompensation = true;
}
if (xGo)
{
xClockwise(xClock);
digitalWrite(stepPinX, LOW);
digitalWrite(stepPinX, HIGH);
previousTimeX = currentTimeX;
if (skipCompensation == false)
{
yClockwise(!xClock); // spin y motor with x
digitalWrite(stepPinY, LOW); // compensator
digitalWrite(stepPinY, HIGH);
}
stepsX++;
}
if (yGo)
{
yClockwise(yClock);
digitalWrite(stepPinY, LOW);
digitalWrite(stepPinY, HIGH);
previousTimeY = currentTimeY;
stepsY++;
}
// if (stepsX >= nStepsX && stepsY >= nStepsY) {keepGoing = false;} // check if finished
}
delay(finalDelay);
}
void spin_degs(float degX, float degY, float speedX = 60, float speedY = 60, int finalDelay = 100)
{
// wake(true, true);
// define directions because y motor has to constantly change direction to compensate
bool xClock;
bool yClock;
if (degX >= 0)
{
xClock = true;
}
else
{
xClock = false;
}
if (degY >= 0)
{
yClock = true;
}
else
{
yClock = false;
}
int nStepsX = nStepsXperRot * abs(degX / 360.); // define fraction of full turn
int nStepsY = nStepsYperRot * abs(degY / 360.);
bool keepGoing = true;
long IntervalX = (6e7 / speedX) / nStepsXperRot;
long IntervalY = (6e7 / speedY) / nStepsYperRot;
unsigned long stepsY = 0;
unsigned long stepsX = 0;
unsigned long previousTimeX = micros();
unsigned long previousTimeY = micros();
while (keepGoing)
{
unsigned long currentTimeX = micros();
unsigned long currentTimeY = micros();
bool xGo = false; // whether to step x
bool yGo = false; // whether to step y
bool skipCompensation = false;
digitalWrite(stepPinX, HIGH);
digitalWrite(stepPinY, HIGH);
if (currentTimeX - previousTimeX > IntervalX && stepsX <= nStepsX)
{
xGo = true;
}
if (currentTimeY - previousTimeY > IntervalY && stepsY <= nStepsY)
{
yGo = true;
}
if (yGo == true && xGo == true && yClock == false && xClock != yClock)
{
skipCompensation = true;
}
if (xGo)
{
xClockwise(xClock);
digitalWrite(stepPinX, LOW);
digitalWrite(stepPinX, HIGH);
previousTimeX = currentTimeX;
if (skipCompensation == false)
{
yClockwise(!xClock); // spin y motor with x
digitalWrite(stepPinY, LOW); // compensator
digitalWrite(stepPinY, HIGH);
}
stepsX++;
}
if (yGo)
{
yClockwise(yClock);
digitalWrite(stepPinY, LOW);
digitalWrite(stepPinY, HIGH);
previousTimeY = currentTimeY;
stepsY++;
}
if (stepsX >= nStepsX && stepsY >= nStepsY)
{
keepGoing = false;
} // check if finished
}
delay(finalDelay);
}
void show_off()
{
spin_degs(180, 180);
spin_degs(-180, -180);
spin_degs(-180, 180);
spin_degs(180, -180);
}
void RPM()
{
spin_degs(random(-360, 360), random(-360, 360));
}
void setup()
{
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(stepPinX, OUTPUT);
pinMode(dirPinX, OUTPUT);
pinMode(stepPinY, OUTPUT);
pinMode(dirPinY, OUTPUT);
pinMode(enablePinX, OUTPUT);
pinMode(enablePinY, OUTPUT);
Serial.begin(9600);
Serial.print("--\nHello\n--");
wake(false, false);
}
void loop()
{
buttonState = digitalRead(buttonPin);
wake(false, false);
if (buttonState == HIGH)
{
digitalWrite(LED_BUILTIN, HIGH);
wake(true, true);
spin_continuous(1, 60, 60);
// show_off();
// RPM();
}
digitalWrite(LED_BUILTIN, LOW);
delay(10); // Wait a second
}