-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCARA_Final_copy_20240411153853.ino
307 lines (244 loc) · 6.12 KB
/
SCARA_Final_copy_20240411153853.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
302
/*
Matthew Costantino
03/24/2024
SCARA_Final.ino
This is the file for the finalized SCARA system code. The code
includes functionality to move the SCARA to a specified position,
pick up a piece at that position, move the SCARA to a second position,
and place the piece down at the second position.
*/
#include <AccelStepper.h>
AccelStepper Inner(1,3,2); //Inner motor with stepPin 3 and dirPin 2
AccelStepper Outer(1,5,4); //Outer motor with stepPin 5 and dirPin 4
#define switch1 10
#define switch2 11
#define actPos 8
#define actNeg 9
#define mag 12
int switchValue1 = 0, switchValue2 = 0;
long innerFirstAngle = 147, outerFirstAngle = 25;
long innerSecondAngle = 160, outerSecondAngle = 72;
long innerPlaceAngle = 0, outerPlaceAngle = 0;
int holding=0;
int castlevar=3;
//long innerCaptureFrom = 208, outerCaptureFrom = 17;
//long innerCaptureAt = 156, outerCaptureAt = 93;
void setup() {
pinMode(switch1,INPUT_PULLUP);
pinMode(switch2,INPUT_PULLUP);
pinMode(actPos, OUTPUT);
pinMode(actNeg,OUTPUT);
pinMode(mag, OUTPUT);
digitalWrite(mag,LOW);
Serial.begin(9600);
Inner.setMaxSpeed(225);
Inner.setSpeed(75);
Outer.setMaxSpeed(225);
Outer.setSpeed(75);
while (switchValue1 == 0) {
Inner.runSpeed();
if (digitalRead(switch1) == HIGH) {
switchValue1 = 1;
}
else {
switchValue1 = 0;
}
}
while (switchValue2 == 0) {
Outer.runSpeed();
if (digitalRead(switch2) == HIGH) {
switchValue2 = 1;
}
else {
switchValue2 = 0;
}
}
Inner.setMaxSpeed(90);
Inner.setAcceleration(30);
Outer.setMaxSpeed(90);
Outer.setAcceleration(30);
}
void loop() {
/*
if (innerCaptureAt != 0){
captureAt(innerCaptureAt,outerCaptureAt,innerCaptureFrom,outerCaptureFrom,outerCaptureAt);
}
if (innerFirstAngle != 0) {
pickUpAt(innerFirstAngle,outerFirstAngle, outerFirstAngle);
}
long innerPlaceAngle = innerSecondAngle - innerFirstAngle;
long outerPlaceAngle = outerSecondAngle - outerFirstAngle;
if (innerSecondAngle != 0) {
putDownAt(innerPlaceAngle,outerPlaceAngle, outerSecondAngle);
}
if(outerSecondAngle<0) {
Outer.move(5*outerSecondAngle);
while(Outer.currentPosition() != Outer.targetPosition()) {
Outer.run();
}
}
if (outerSecondAngle> 299) {
Outer.move(50);
while(Outer.currentPosition() != Outer.targetPosition()) {
Outer.run();
}
}
*/
castle(castlevar);
Inner.setMaxSpeed(225);
Inner.setSpeed(75);
Outer.setMaxSpeed(225);
Outer.setSpeed(75);
if (innerFirstAngle != 0 /*| innerCaptureAt != 0*/) {
switchValue1 = 0;
switchValue2 = 0;
}
while (switchValue1 == 0) {
Inner.runSpeed();
if (digitalRead(switch1) == HIGH) {
switchValue1 = 1;
}
else {
switchValue1 = 0;
}
}
while (switchValue2 == 0) {
Outer.runSpeed();
if (digitalRead(switch2) == HIGH) {
switchValue2 = 1;
}
else {
switchValue2 = 0;
}
}
innerFirstAngle = 0;
outerFirstAngle = 0;
innerSecondAngle = 0;
outerSecondAngle = 0;
castlevar = 0;
/*
innerCaptureAt = 0;
outerCaptureAt = 0;
innerCaptureFrom = 0;
outerCaptureFrom = 0;
*/
}
void pickUpAt(int innerAngle, int outerAngle, int absoluteAngle) {
int innerSteps = -2.88*innerAngle;
if (innerSteps>0) {
Inner.move(innerSteps);
}
else {
Inner.move(innerSteps);
}
int outerSteps = -2.88*outerAngle;
if (outerSteps>0) {
Outer.move(outerSteps);
}
else {
Outer.move(outerSteps);
}
if (absoluteAngle < 0 | absoluteAngle > 300) {
while(Inner.currentPosition() != Inner.targetPosition()) {
Inner.run();
}
while(Outer.currentPosition() != Outer.targetPosition()) {
Outer.run();
}
}
else {
while(Outer.currentPosition() != Outer.targetPosition()) {
Outer.run();
}
while(Inner.currentPosition() != Inner.targetPosition()) {
Inner.run();
}
}
digitalWrite(actPos, HIGH);
digitalWrite(actNeg, LOW);
digitalWrite(mag,HIGH);
delay(3000);
digitalWrite(actPos, LOW);
digitalWrite(actNeg, HIGH);
delay(2500);
holding = 1;
}
void putDownAt(int innerAngle, int outerAngle, int absoluteAngle) {
int innerSteps = -2.88*innerAngle;
if (innerSteps>0) {
Inner.move(innerSteps);
}
else {
Inner.move(innerSteps);
}
int outerSteps = -2.88*outerAngle;
if (outerSteps>0) {
Outer.move(outerSteps);
}
else {
Outer.move(outerSteps);
}
if (absoluteAngle < 0 | absoluteAngle > 300) {
while(Inner.currentPosition() != Inner.targetPosition()) {
Inner.run();
}
while(Outer.currentPosition() != Outer.targetPosition()) {
Outer.run();
}
}
else {
while(Outer.currentPosition() != Outer.targetPosition()) {
Outer.run();
}
while(Inner.currentPosition() != Inner.targetPosition()) {
Inner.run();
}
}
digitalWrite(actPos, HIGH);
digitalWrite(actNeg, LOW);
delay(2500);
digitalWrite(mag,LOW);
delay(1000);
digitalWrite(actPos, LOW);
digitalWrite(actNeg, HIGH);
delay(2500);
holding = 0;
}
void captureAt(int innerCapAt, int outerCapAt, int innerCapFrom, int outerCapFrom, int absoluteAngle) {
pickUpAt(innerCapAt,outerCapAt,absoluteAngle);
long innerCapPoint = 137 - innerCapAt;
long outerCapPoint = 295 - outerCapAt;
putDownAt(innerCapPoint,outerCapPoint,absoluteAngle);
long innerCapMove = innerCapFrom - 137;
long outerCapMove = outerCapFrom - 295;
pickUpAt(innerCapMove,outerCapMove,absoluteAngle);
long innerFinalMove = innerCapAt - innerCapFrom;
long outerFinalMove = outerCapAt - outerCapFrom;
putDownAt(innerFinalMove, outerFinalMove, absoluteAngle);
}
void castle(int castle) {
if (castle == 1) {
pickUpAt(133,55,55);
putDownAt(-1,-22,33);
pickUpAt(-13,-15,18);
putDownAt(16,24,42);
}
else if (castle == 2) {
pickUpAt(133,55,55);
putDownAt(-8,31,86);
pickUpAt(-9, 23,109);
putDownAt(14,-41,68);
}
else if (castle == 3) {
pickUpAt(197,54,54);
putDownAt(-22,32,86);
pickUpAt(-14,22,108);
putDownAt(25,-39,69);
}
else if (castle == 4) {
pickUpAt(197,54,54);
putDownAt(25,-21,33);
pickUpAt(30,-16,17);
putDownAt(-43,25,42);
}
}