forked from triffid/FiveD_on_Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcode_process.c
399 lines (352 loc) · 10.1 KB
/
gcode_process.c
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "gcode_process.h"
#include <string.h>
#include "gcode_parse.h"
#include "dda_queue.h"
#include "watchdog.h"
#include "delay.h"
#include "serial.h"
#include "sermsg.h"
#include "temp.h"
#include "heater.h"
#include "timer.h"
#include "sersendf.h"
#include "debug.h"
/****************************************************************************
* *
* Command Received - process it *
* *
****************************************************************************/
void process_gcode_command() {
uint32_t backup_f;
// convert relative to absolute
if (next_target.option_relative) {
next_target.target.X += startpoint.X;
next_target.target.Y += startpoint.Y;
next_target.target.Z += startpoint.Z;
}
// E ALWAYS relative, otherwise we overflow our registers after only a few layers
// next_target.target.E += startpoint.E;
// easier way to do this
// startpoint.E = 0;
// moved to dda.c, end of dda_create() and dda_queue.c, next_move()
if (next_target.seen_G) {
switch (next_target.G) {
// G0 - rapid, unsynchronised motion
// since it would be a major hassle to force the dda to not synchronise, just provide a fast feedrate and hope it's close enough to what host expects
case 0:
backup_f = next_target.target.F;
next_target.target.F = MAXIMUM_FEEDRATE_X * 2;
enqueue(&next_target.target);
next_target.target.F = backup_f;
break;
// G1 - synchronised motion
case 1:
enqueue(&next_target.target);
break;
// G2 - Arc Clockwise
// unimplemented
// G3 - Arc Counter-clockwise
// unimplemented
// G4 - Dwell
case 4:
// wait for all moves to complete
for (;queue_empty() == 0;)
wd_reset();
// delay
delay_ms(next_target.P);
break;
// G20 - inches as units
case 20:
next_target.option_inches = 1;
break;
// G21 - mm as units
case 21:
next_target.option_inches = 0;
break;
// G30 - go home via point
case 30:
enqueue(&next_target.target);
// no break here, G30 is move and then go home
// G28 - go home
case 28:
/*
Home XY first
*/
// hit endstops, no acceleration- we don't care about skipped steps
startpoint.F = MAXIMUM_FEEDRATE_X;
SpecialMoveXY(-250L * STEPS_PER_MM_X, -250L * STEPS_PER_MM_Y, MAXIMUM_FEEDRATE_X);
startpoint.X = startpoint.Y = 0;
// move forward a bit
SpecialMoveXY(5 * STEPS_PER_MM_X, 5 * STEPS_PER_MM_Y, SEARCH_FEEDRATE_X);
// move back in to endstops slowly
SpecialMoveXY(-20 * STEPS_PER_MM_X, -20 * STEPS_PER_MM_Y, SEARCH_FEEDRATE_X);
// wait for queue to complete
for (;queue_empty() == 0;)
wd_reset();
// this is our home point
startpoint.X = startpoint.Y = current_position.X = current_position.Y = 0;
/*
Home Z
*/
// hit endstop, no acceleration- we don't care about skipped steps
startpoint.F = MAXIMUM_FEEDRATE_Z;
SpecialMoveZ(-250L * STEPS_PER_MM_Z, MAXIMUM_FEEDRATE_Z);
startpoint.Z = 0;
// move forward a bit
SpecialMoveZ(5 * STEPS_PER_MM_Z, SEARCH_FEEDRATE_Z);
// move back into endstop slowly
SpecialMoveZ(-20L * STEPS_PER_MM_Z, SEARCH_FEEDRATE_Z);
// wait for queue to complete
for (;queue_empty() == 0;)
wd_reset();
// this is our home point
startpoint.Z = current_position.Z = 0;
/*
Home E
*/
// extruder only runs one way and we have no "endstop", just set this point as home
startpoint.E = current_position.E = 0;
/*
Home F
*/
// F has been left at SEARCH_FEEDRATE_Z by the last move, this is a usable "home"
// uncomment the following or substitute if you prefer a different default feedrate
// startpoint.F = SEARCH_FEEDRATE_Z
break;
// G90 - absolute positioning
case 90:
next_target.option_relative = 0;
break;
// G91 - relative positioning
case 91:
next_target.option_relative = 1;
break;
// G92 - set home
case 92:
startpoint.X = startpoint.Y = startpoint.Z = startpoint.E =
current_position.X = current_position.Y = current_position.Z = current_position.E = 0;
startpoint.F =
current_position.F = SEARCH_FEEDRATE_Z;
break;
// unknown gcode: spit an error
default:
serial_writestr_P(PSTR("E: Bad G-code "));
serwrite_uint8(next_target.G);
serial_writechar('\n');
}
}
else if (next_target.seen_M) {
switch (next_target.M) {
// M101- extruder on
case 101:
if (temp_achieved() == 0) {
enqueue(NULL);
}
do {
// backup feedrate, move E very quickly then restore feedrate
backup_f = startpoint.F;
startpoint.F = MAXIMUM_FEEDRATE_E;
SpecialMoveE(E_STARTSTOP_STEPS, MAXIMUM_FEEDRATE_E);
startpoint.F = backup_f;
} while (0);
break;
// M102- extruder reverse
// M103- extruder off
case 103:
do {
// backup feedrate, move E very quickly then restore feedrate
backup_f = startpoint.F;
startpoint.F = MAXIMUM_FEEDRATE_E;
SpecialMoveE(-E_STARTSTOP_STEPS, MAXIMUM_FEEDRATE_E);
startpoint.F = backup_f;
} while (0);
break;
// M104- set temperature
case 104:
temp_set(next_target.S);
if (next_target.S) {
enable_heater();
power_on();
}
else {
disable_heater();
}
break;
// M105- get temperature
case 105:
temp_print();
break;
// M106- fan on
#ifdef FAN_PIN
case 106:
enable_fan();
break;
// M107- fan off
case 107:
disable_fan();
break;
#endif
// M109- set temp and wait
case 109:
temp_set(next_target.S);
if (next_target.S) {
enable_heater();
power_on();
}
else {
disable_heater();
}
enqueue(NULL);
break;
// M110- set line number
case 110:
next_target.N_expected = next_target.S - 1;
break;
// M111- set debug level
#ifdef DEBUG
case 111:
debug_flags = next_target.S;
break;
#endif
// M112- immediate stop
case 112:
disableTimerInterrupt();
queue_flush();
power_off();
break;
// M113- extruder PWM
// M114- report XYZEF to host
case 114:
sersendf_P(PSTR("X:%ld,Y:%ld,Z:%ld,E:%ld,F:%ld\n"), current_position.X, current_position.Y, current_position.Z, current_position.E, current_position.F);
break;
// M115- capabilities string
case 115:
serial_writestr_P(PSTR("FIRMWARE_NAME:FiveD_on_Arduino FIRMWARE_URL:http%3A//github.com/triffid/FiveD_on_Arduino/ PROTOCOL_VERSION:1.0 MACHINE_TYPE:Mendel EXTRUDER_COUNT:1 HEATER_COUNT:1\n"));
break;
#ifdef HEATER_PIN
// M130- heater P factor
case 130:
if (next_target.seen_S)
p_factor = next_target.S;
break;
// M131- heater I factor
case 131:
if (next_target.seen_S)
i_factor = next_target.S;
break;
// M132- heater D factor
case 132:
if (next_target.seen_S)
d_factor = next_target.S;
break;
// M133- heater I limit
case 133:
if (next_target.seen_S)
i_limit = next_target.S;
break;
// M134- save PID settings to eeprom
case 134:
heater_save_settings();
break;
#endif /* HEATER_PIN */
// M190- power on
case 190:
power_on();
#ifdef X_ENABLE_PIN
WRITE(X_ENABLE_PIN, 0);
#endif
#ifdef Y_ENABLE_PIN
WRITE(Y_ENABLE_PIN, 0);
#endif
#ifdef Z_ENABLE_PIN
WRITE(Z_ENABLE_PIN, 0);
#endif
steptimeout = 0;
break;
// M191- power off
case 191:
#ifdef X_ENABLE_PIN
WRITE(X_ENABLE_PIN, 1);
#endif
#ifdef Y_ENABLE_PIN
WRITE(Y_ENABLE_PIN, 1);
#endif
#ifdef Z_ENABLE_PIN
WRITE(Z_ENABLE_PIN, 1);
#endif
power_off();
break;
#ifdef DEBUG
// M140- echo off
case 140:
debug_flags &= ~DEBUG_ECHO;
serial_writestr_P(PSTR("Echo off\n"));
break;
// M141- echo on
case 141:
debug_flags |= DEBUG_ECHO;
serial_writestr_P(PSTR("Echo on\n"));
break;
// DEBUG: return current position
case 250:
serial_writestr_P(PSTR("{X:"));
serwrite_int32(current_position.X);
serial_writestr_P(PSTR(",Y:"));
serwrite_int32(current_position.Y);
serial_writestr_P(PSTR(",Z:"));
serwrite_int32(current_position.Z);
serial_writestr_P(PSTR(",E:"));
serwrite_int32(current_position.E);
serial_writestr_P(PSTR(",F:"));
serwrite_int32(current_position.F);
serial_writestr_P(PSTR(",c:"));
serwrite_uint32(movebuffer[mb_tail].c);
serial_writestr_P(PSTR("}\n"));
serial_writestr_P(PSTR("{X:"));
serwrite_int32(movebuffer[mb_tail].endpoint.X);
serial_writestr_P(PSTR(",Y:"));
serwrite_int32(movebuffer[mb_tail].endpoint.Y);
serial_writestr_P(PSTR(",Z:"));
serwrite_int32(movebuffer[mb_tail].endpoint.Z);
serial_writestr_P(PSTR(",E:"));
serwrite_int32(movebuffer[mb_tail].endpoint.E);
serial_writestr_P(PSTR(",F:"));
serwrite_int32(movebuffer[mb_tail].endpoint.F);
serial_writestr_P(PSTR(",c:"));
#ifdef ACCELERATION_REPRAP
serwrite_uint32(movebuffer[mb_tail].end_c);
#else
serwrite_uint32(movebuffer[mb_tail].c);
#endif
serial_writestr_P(PSTR("}\n"));
print_queue();
break;
// DEBUG: read arbitrary memory location
case 253:
if (next_target.seen_P == 0)
next_target.P = 1;
for (; next_target.P; next_target.P--) {
serwrite_hex8(*(volatile uint8_t *)(next_target.S));
next_target.S++;
}
serial_writechar('\n');
break;
// DEBUG: write arbitrary memory locatiom
case 254:
serwrite_hex8(next_target.S);
serial_writechar(':');
serwrite_hex8(*(volatile uint8_t *)(next_target.S));
serial_writestr_P(PSTR("->"));
serwrite_hex8(next_target.P);
serial_writechar('\n');
(*(volatile uint8_t *)(next_target.S)) = next_target.P;
break;
#endif /* DEBUG */
// unknown mcode: spit an error
default:
serial_writestr_P(PSTR("E: Bad M-code "));
serwrite_uint8(next_target.M);
serial_writechar('\n');
}
}
}