-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiScAnt.ino
349 lines (291 loc) · 7.72 KB
/
PiScAnt.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#define X_STEP_PIN 54
#define X_DIR_PIN 55
#define X_ENABLE_PIN 38
#define X_MIN_PIN 3
#define Y_STEP_PIN 60
#define Y_DIR_PIN 61
#define Y_ENABLE_PIN 56
#define Y_MIN_PIN 14
#define Z_STEP_PIN 46
#define Z_DIR_PIN 48
#define Z_ENABLE_PIN 62
#define Z_MIN_PIN 18
// #define Z_MAX_PIN 19
#define E_STEP_PIN 26
#define E_DIR_PIN 28
#define E_ENABLE_PIN 24
#define E_MIN_PIN 2
#define Q_STEP_PIN 36
#define Q_DIR_PIN 34
#define Q_ENABLE_PIN 30
#define Q_MIN_PIN 15
#define SDPOWER -1
#define SDSS 53
#define LED_PIN 13
#define FAN_PIN 9
#define PS_ON_PIN 12
#define KILL_PIN -1
#define HEATER_0_PIN 10
#define LIGHT_PIN 8
#define TEMP_0_PIN 13 // ANALOG NUMBERING
#define TEMP_1_PIN 14 // ANALOG NUMBERING
// Calculate based on max input size expected for one command
#define INPUT_SIZE 15
void setup() {
pinMode(FAN_PIN, OUTPUT);
pinMode(HEATER_0_PIN, OUTPUT);
pinMode(LIGHT_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(X_STEP_PIN, OUTPUT);
pinMode(X_DIR_PIN, OUTPUT);
pinMode(X_ENABLE_PIN, OUTPUT);
pinMode(X_MIN_PIN, INPUT_PULLUP); // not needed
pinMode(Y_STEP_PIN, OUTPUT);
pinMode(Y_DIR_PIN, OUTPUT);
pinMode(Y_ENABLE_PIN, OUTPUT);
pinMode(Y_MIN_PIN, INPUT_PULLUP);
pinMode(Z_STEP_PIN, OUTPUT);
pinMode(Z_DIR_PIN, OUTPUT);
pinMode(Z_ENABLE_PIN, OUTPUT);
pinMode(Z_MIN_PIN, INPUT_PULLUP);
// pinMode(Z_MAX_PIN, INPUT_PULLUP); // not needed
pinMode(E_STEP_PIN, OUTPUT);
pinMode(E_DIR_PIN, OUTPUT);
pinMode(E_ENABLE_PIN, OUTPUT);
pinMode(E_MIN_PIN, INPUT_PULLUP); // used to be X_MAX
pinMode(Q_STEP_PIN, OUTPUT);
pinMode(Q_DIR_PIN, OUTPUT);
pinMode(Q_ENABLE_PIN, OUTPUT);
pinMode(Q_MIN_PIN, INPUT_PULLUP); // used to be Y_MAX
// deactivate all motors
digitalWrite(X_ENABLE_PIN, HIGH);
digitalWrite(Y_ENABLE_PIN, HIGH);
digitalWrite(Z_ENABLE_PIN, HIGH);
digitalWrite(E_ENABLE_PIN, HIGH);
digitalWrite(Q_ENABLE_PIN, HIGH);
Serial.begin(9600);
delay(1000);
// Serial.println("Arduino is ready to receive on Baud 9600...");
}
int steps;
// Serial inputs
String device;
String command;
byte status;
byte pressed = 1; // the pressed state pullup value for endstops when they're pressed
byte not_pressed = 0; // the non-pressed state
int STEP_PIN;
int DIR_PIN;
int ENABLE_PIN;
int MIN_PIN;
int MAX_PIN;
int delay_motor;
void loop() {
if (Serial.available() > 0) {
read_serial();
// X_R_100
// Y_R_100
// Z_R_100
// E_R_100
// Q_R_100
if (device == "X" | device == "Y" | device == "Z" | device == "E" | device == "Q") {
move_motor();
}
// light_x_0
// light_x_1
if (device == "light") {
light_switch();
}
// Z_home_x
if (command == "home") {
home_axis();
}
// x_deactivate_x
if (command == "deactivate") {
deactivate_motors();
}
// x_activate_x
if (command == "activate") {
activate_motors();
}
}
}
void home_axis() {
// Serial.print("Homing ");
// Serial.print(device);
// Serial.println(" axis...");
define_motor_pins();
// enable motor
digitalWrite(ENABLE_PIN, LOW);
// delayMicroseconds(100);
// home towards endstop direction
digitalWrite(DIR_PIN, HIGH); // left
steps = 0;
// run motor
while (1) {
if (digitalRead(MIN_PIN) == pressed) {
// Serial.println("Home reached.");
break;
}
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(delay_motor);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(delay_motor);
steps++;
}
// deactivate motor
// digitalWrite(ENABLE_PIN, HIGH);
// message end of action
//// Serial.println(steps);
Serial.println(2); // 2 because move_motor() already prints out 0 or 1
}
// read serial input
void read_serial() {
String input = Serial.readStringUntil('\n');
device = getValue(input, '_', 0);
command = getValue(input, '_', 1);
String number_str = getValue(input, '_', 2);
steps = number_str.toFloat();
// Serial.println("device: " + device);
// Serial.println("command: " + command);
// Serial.println("steps: " + steps);
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
// MOTOR COMMANDS
// move axis
void move_motor() {
define_motor_pins();
// enable motor
digitalWrite(ENABLE_PIN, LOW);
delayMicroseconds(delay_motor);
// check if right or left movement i desired
if (command == "L") {
digitalWrite(DIR_PIN, HIGH); // left
} else if (command == "R") {
digitalWrite(DIR_PIN, LOW); // right
}
// run motor X_R_100
int max_acc_steps = 15;
if(max_acc_steps > steps/2){
max_acc_steps = round(steps/2);
}
int min_delay_motor = delay_motor;
int start_delay_motor = 1.5*delay_motor;
int delay_change = round((start_delay_motor-min_delay_motor)/max_acc_steps);
int curr_delay_motor = start_delay_motor;
for (int step = 1; step <= steps; step++) {
Serial.println(curr_delay_motor);
if(step <= max_acc_steps){
curr_delay_motor -= delay_change;
if(curr_delay_motor < min_delay_motor){
curr_delay_motor = min_delay_motor;
}
}
else if(step >= (steps-max_acc_steps)){
curr_delay_motor += delay_change;
if(curr_delay_motor > start_delay_motor){
curr_delay_motor = start_delay_motor;
}
}
if (digitalRead(MIN_PIN) == not_pressed) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(curr_delay_motor);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(curr_delay_motor);
status = 0;
} else {
// Serial.print("limit ");
// Serial.print(device);
// Serial.println(" reached.");
status = 1;
break;
}
}
// deactivate motor
// digitalWrite(ENABLE_PIN, HIGH);
// message end of action
Serial.println(status);
}
void deactivate_motors() {
// Serial.println("deactivating all motors...");
digitalWrite(X_ENABLE_PIN, HIGH);
digitalWrite(Y_ENABLE_PIN, HIGH);
digitalWrite(Z_ENABLE_PIN, HIGH);
digitalWrite(E_ENABLE_PIN, HIGH);
digitalWrite(Q_ENABLE_PIN, HIGH);
Serial.println(0);
}
void activate_motors() {
// Serial.println("activating all motors...");
digitalWrite(X_ENABLE_PIN, LOW);
digitalWrite(Y_ENABLE_PIN, LOW);
digitalWrite(Z_ENABLE_PIN, LOW);
digitalWrite(E_ENABLE_PIN, LOW);
digitalWrite(Q_ENABLE_PIN, LOW);
Serial.println(0);
}
// LIGHT COMMANDS
void light_switch() {
if (steps == 1) {
// Serial.println("Light on.");
digitalWrite(LIGHT_PIN, HIGH);
} else {
digitalWrite(LIGHT_PIN, LOW);
// Serial.println("Light off.");
}
Serial.println(0);
}
// MOTOR PIN DEFINITIONS
void define_motor_pins() {
// Serial.println(device);
// find wich motor to move
if (device == "X") {
// Serial.println("motor: X");
STEP_PIN = 54;
DIR_PIN = 55;
ENABLE_PIN = 38;
MIN_PIN = 3;
delay_motor = 4000;
} else if (device == "Y") {
// Serial.println("motor: Y");
STEP_PIN = 60;
DIR_PIN = 61;
ENABLE_PIN = 56;
MIN_PIN = 14;
delay_motor = 4000;
} else if (device == "Z") {
// Serial.println("motor: Z");
STEP_PIN = 46;
DIR_PIN = 48;
ENABLE_PIN = 62;
MIN_PIN = 18;
delay_motor = 750;
} else if (device == "E") {
// Serial.println("motor: E");
STEP_PIN = 26;
DIR_PIN = 28;
ENABLE_PIN = 24;
MIN_PIN = 2;
delay_motor = 1000;
} else if (device == "Q") {
// Serial.println("motor: Q");
STEP_PIN = 36;
DIR_PIN = 34;
ENABLE_PIN = 30;
MIN_PIN = 15;
delay_motor = 1000;
}
}