forked from ashelly/grbl
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprotocol.c
383 lines (328 loc) · 14.9 KB
/
protocol.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
/*
protocol.c - controls Grbl execution protocol and procedures
Part of Grbl
Copyright (c) 2011-2014 Sungeun K. Jeon
Copyright (c) 2009-2011 Simen Svale Skogsrud
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include "system.h"
#include "serial.h"
#include "settings.h"
#include "protocol.h"
#include "gcode.h"
#include "planner.h"
#include "stepper.h"
#include "motion_control.h"
#include "motor_driver.h"
#include "report.h"
#include "systick.h"
#include "magazine.h"
#define STATUS_REPORT_RATE_MS 333 //3 Hz
static char line[LINE_BUFFER_SIZE]; // Line to be executed. Zero-terminated.
// Directs and executes one line of formatted input from protocol_process. While mostly
// incoming streaming g-code blocks, this also directs and executes Grbl internal commands,
// such as settings, initiating the homing cycle, and toggling switch states.
static uint8_t protocol_execute_line(char *line)
{
protocol_execute_runtime(); // Runtime command check point.
// Bail to calling function upon system abort
if (sys.abort) {
return STATUS_ABORT;
}
uint8_t status = STATUS_OK;
if (line[0] == '$') {
// Grbl '$' system command
status = system_execute_line(line);
} else if (sys.state == STATE_ALARM) {
// Everything else is gcode. Block if in alarm mode.
status = STATUS_ALARM_LOCK;
report_status_message(STATUS_ALARM_LOCK);
} else if (line[0] == CMD_LINE_START) {
// This is a special start command which is guaranteed to be sequenced after
// the previous line - it won't be picked out of the serial stream while
// gc_execute_line is still parsing the previous line.
SYS_EXEC |= EXEC_CYCLE_START;
} else {
status = gc_execute_line(line);
}
/* If there was an error, report it */
if (status != STATUS_IDLE_WAIT) {
report_status_message(status);
}
return status;
}
/*
GRBL PRIMARY LOOP:
*/
void protocol_main_loop()
{
// ------------------------------------------------------------
// Complete initialization procedures upon a power-up or reset.
// ------------------------------------------------------------
// Print welcome message
report_init_message();
/* Request Grbl status upon startup */
SYS_EXEC |= EXEC_RUNTIME_REPORT;
sysflags.report_rqsts |= REQUEST_STATUS_REPORT;
// Check for and report alarm state after a reset, error, or an initial power up.
if (sys.state == STATE_ALARM) {
report_feedback_message(MESSAGE_ALARM_LOCK);
} else {
// All systems go!
sys.state = STATE_IDLE; // Set system to ready. Clear all state flags.
system_execute_startup(line); // Execute startup script.
}
// ---------------------------------------------------------------------------------
// Primary loop! Upon a system abort, this exits back to main() to reset the system.
// ---------------------------------------------------------------------------------
bool iscomment = false;
uint8_t char_counter = 0;
uint8_t c;
for (;;) {
// Process one line of incoming Gcode data, as the data becomes available. Performs an
// initial filtering by removing spaces and comments and capitalizing all letters.
while((c = serial_read()) != SERIAL_NO_DATA) {
if ((c == '\n') || (c == '\r')) { // End of line reached
line[char_counter] = 0; // Set string termination character.
// Line is complete. Execute it!
while (protocol_execute_line(line) == STATUS_IDLE_WAIT);
iscomment = false;
char_counter = 0;
} else {
if (iscomment) {
// Throw away all comment characters
if (c == ')') {
// End of comment. Resume line.
iscomment = false;
}
} else {
if (c <= ' ') {
// Throw away whitepace and control characters
} else if (c == '/') {
// Block delete NOT SUPPORTED. Ignore character.
// NOTE: If supported, would simply need to check the system if block delete is enabled.
} else if (c == '(') {
// Enable comments flag and ignore all characters until ')' or EOL.
// NOTE: This doesn't follow the NIST definition exactly, but is good enough for now.
// In the future, we could simply remove the items within the comments, but retain the
// comment control characters, so that the g-code parser can error-check it.
iscomment = true;
} else if (char_counter >= LINE_BUFFER_SIZE-1) {
// Detect line buffer overflow. Report error and reset line buffer.
report_status_message(STATUS_OVERFLOW);
iscomment = false;
char_counter = 0;
} else {
/* Put the capitalized letter in the buffer */
line[char_counter++] = (c >= 'a' && c <= 'z') ? (c - 'a' + 'A') : c;
}
}
}
}
// If there are no more characters in the serial read buffer to be processed and executed,
// this indicates that g-code streaming has either filled the planner buffer or has
// completed. In either case, auto-cycle start, if enabled, any queued moves.
protocol_auto_cycle_start();
protocol_execute_runtime(); // Runtime command check point.
if (sys.abort) { return; } // Bail to main() program loop to reset system.
}
return; /* Never reached */
}
static void protocol_check_required_reports()
{
/* Report state changes */
if (sys.state != sys.old_state) {
sys.old_state = sys.state;
SYS_EXEC |= EXEC_RUNTIME_REPORT;
sysflags.report_rqsts |= REQUEST_STATUS_REPORT;
}
/* Update limit state and report if changed */
sys.limit_state = LIMIT_PIN & LIMIT_MASK; /* Note LIMIT_PIN is not a single
pin, but short for PORT IN */
if (sys.limit_state != sys.old_limit_state) {
sys.old_limit_state = sys.limit_state;
SYS_EXEC |= EXEC_RUNTIME_REPORT;
sysflags.report_rqsts |= REQUEST_LIMIT_REPORT;
}
/* Check if the ESTOP status changed */
if ((ESTOP_PIN & ESTOP_MASK) != sys.last_estop_state) {
sys.last_estop_state = (ESTOP_PIN & ESTOP_MASK);
SYS_EXEC |= EXEC_RUNTIME_REPORT;
sysflags.report_rqsts |= REQUEST_LIMIT_REPORT;
}
}
static void maybe_reinit_motors(void)
{
uint8_t estop_state = (ESTOP_PIN & ESTOP_MASK);
if ((estop_state != sys.last_estop_state) && !estop_state) {
motor_drv_init();
}
}
// Executes run-time commands, when required. This is called from various check points in the main
// program, primarily where there may be a while loop waiting for a buffer to clear space or any
// point where the execution time from the last check point may be more than a fraction of a second.
// This is a way to execute runtime commands asynchronously (aka multitasking) with grbl's g-code
// parsing and planning functions. This function also serves as an interface for the interrupts to
// set the system runtime flags, where only the main program handles them, removing the need to
// define more computationally-expensive volatile variables. This also provides a controlled way to
// execute certain tasks without having two or more instances of the same task, such as the planner
// recalculating the buffer upon a feedhold or override.
// NOTE: The sysflags.execute variable flags are set by any process, step or serial interrupts, pinouts,
// limit switches, or the main program.
void protocol_execute_runtime()
{
uint8_t rt_exec = SYS_EXEC; // Copy to avoid calling volatile multiple times
// Service SysTick Callbacks
systick_service_callbacks();
if (settings.spi_motor_drivers) {
maybe_reinit_motors();
}
protocol_check_required_reports();
st_check_disable();
/* TODO: Figure out what exactly is causing this off-by-one type
* error in the reporting system */
/* If we're idle and there are line numbers that have not been
* reported, flush out the buffer to ensure our cutting software
* fires all of it's associated callbacks */
if (sys.state == STATE_IDLE && linenumber_peek()) {
request_eol_report();
}
if (rt_exec) { // Enter only if any bit flag is true
// System alarm. Everything has shutdown by something that has gone severely wrong. Report
// the source of the error to the user. If critical, Grbl disables by entering an infinite
// loop until system reset/abort.
if (rt_exec & (EXEC_ALARM | EXEC_CRIT_EVENT)) {
sys.state = STATE_ALARM; // Set system alarm state
// Critical events. Error events identified by sys.alarm flags
report_alarm_message(sys.alarm);
// check critical
if (rt_exec & EXEC_CRIT_EVENT) {
report_feedback_message(MESSAGE_CRITICAL_EVENT);
bit_false(SYS_EXEC,EXEC_RESET); // Disable any existing reset
do {
// Block EVERYTHING until user issues reset or power cycles.
// Hard limits typically occur while unattended or not
// paying attention. Gives the user time to do what is
// needed before resetting, like killing the incoming
// stream. The same could be said about soft limits. While
// the position is not lost, the incoming stream could be
// still engaged and cause a serious crash if it continues
// afterwards.
} while (bit_isfalse(SYS_EXEC,EXEC_RESET));
}
bit_false(SYS_EXEC,(EXEC_ALARM | EXEC_CRIT_EVENT));
sys.alarm = 0; //clear reported alarms
}
// Execute system abort.
if (rt_exec & EXEC_RESET) {
sys.abort = true; // Only place this is set true.
return; // Nothing else to do but exit.
}
// Execute and serial print status
if (rt_exec & EXEC_RUNTIME_REPORT) {
uint8_t reports=sysflags.report_rqsts;
sysflags.report_rqsts=0; //TODO: potential race here if isr requests another right after we read it.
if (reports & REQUEST_STATUS_REPORT) {
if (!report_realtime_status()){ //ensure no more lines to report
reports &= ~REQUEST_STATUS_REPORT;
}
}
//elsif forces 1 report per loop max
else if (reports & REQUEST_LIMIT_REPORT) {
report_limit_pins();
reports &= ~REQUEST_LIMIT_REPORT;
}
else if (reports & REQUEST_COUNTER_REPORT) {
report_counters();
reports &= ~REQUEST_COUNTER_REPORT;
}
else if (reports & REQUEST_VOLTAGE_REPORT) {
report_voltage();
reports &= ~REQUEST_VOLTAGE_REPORT;
}
else if (reports & REQUEST_EDGE_REPORT) {
magazine_report_edge_events();
reports &= ~REQUEST_EDGE_REPORT;
}
if (0==(sysflags.report_rqsts|=reports)) { //if all reports done and no new requests, clear report flag
bit_false(SYS_EXEC,EXEC_RUNTIME_REPORT);
}
}
// Execute a feed hold with deceleration, only during cycle.
if (rt_exec & EXEC_FEED_HOLD) {
// !!! During a cycle, the segment buffer has just been reloaded and full. So the math involved
// with the feed hold should be fine for most, if not all, operational scenarios.
if (sys.state == STATE_CYCLE) {
sys.state = STATE_HOLD;
st_update_plan_block_parameters();
st_prep_buffer();
sys.flags &=~ SYSFLAG_AUTOSTART; // Disable planner auto start upon feed hold.
}
bit_false(SYS_EXEC,EXEC_FEED_HOLD);
}
// Execute a cycle start by starting the stepper interrupt begin executing the blocks in queue.
// block Start while homing/force-servoing.
if ((rt_exec & EXEC_CYCLE_START) && !(sys.state & (STATE_HOMING | STATE_FORCESERVO | STATE_PROBING))) {
if (sys.state == STATE_QUEUED) {
sys.state = STATE_CYCLE;
st_prep_buffer(); // Initialize step segment buffer before beginning cycle.
st_wake_up();
if (bit_istrue(settings.flags,BITFLAG_AUTO_START)) {
sys.flags |= SYSFLAG_AUTOSTART; // Re-enable auto start after feed hold.
} else {
sys.flags &= ~SYSFLAG_AUTOSTART; // Reset auto start per settings.
}
}
}
// Reinitializes the cycle plan and stepper system after a feed hold for a resume. Called by
// runtime command execution in the main program, ensuring that the planner re-plans safely.
// NOTE: Bresenham algorithm variables are still maintained through both the planner and stepper
// cycle reinitializations. The stepper path should continue exactly as if nothing has happened.
// NOTE: EXEC_CYCLE_STOP is set by the stepper subsystem when a cycle or feed hold completes.
if (rt_exec & EXEC_CYCLE_STOP) {
if ( plan_get_current_block() ) { sys.state = STATE_QUEUED; }
else { sys.state = STATE_IDLE; }
bit_false(SYS_EXEC,EXEC_CYCLE_STOP);
}
}
// Overrides flag byte (sys.override) and execution should be installed here, since they
// are runtime and require a direct and controlled interface to the main stepper program.
// Reload step segment buffer
if (sys.state & (STATE_CYCLE | STATE_HOLD | STATE_HOMING | STATE_FORCESERVO | STATE_PROBING))
st_prep_buffer();
// Clear IO Reset bit.
IO_RESET_PORT &= (~IO_RESET_MASK);
}
// Block until all buffered steps are executed or in a cycle state. Works with feed hold
// during a synchronize call, if it should happen. Also, waits for clean cycle end.
void protocol_buffer_synchronize()
{
// Check and set auto start to resume cycle after synchronize and caller completes.
if (sys.state == STATE_CYCLE) { sys.flags |= SYSFLAG_AUTOSTART; }
while (plan_get_current_block() || (sys.state == STATE_CYCLE)) {
protocol_execute_runtime(); // Check and execute run-time commands
if (sys.abort) { return; } // Check for system abort
}
}
// Auto-cycle start has two purposes: 1. Resumes a plan_synchronize() call from a function that
// requires the planner buffer to empty (spindle enable, dwell, etc.) 2. As a user setting that
// automatically begins the cycle when a user enters a valid motion command manually. This is
// intended as a beginners feature to help new users to understand g-code. It can be disabled
// as a beginner tool, but (1.) still operates. If disabled, the operation of cycle start is
// manually issuing a cycle start command whenever the user is ready and there is a valid motion
// command in the planner queue.
// NOTE: This function is called from the main loop and mc_line() only and executes when one of
// two conditions exist respectively: There are no more blocks sent (i.e. streaming is finished,
// single commands), or the planner buffer is full and ready to go.
void protocol_auto_cycle_start() {
if (sys.flags & SYSFLAG_AUTOSTART) { SYS_EXEC |= EXEC_CYCLE_START; }
}