Commit | Line | Data |
---|---|---|
de91760a | 1 | /* |
4cff3ded AW |
2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). |
3 | Smoothie 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. | |
4 | Smoothie 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. | |
de91760a | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
4cff3ded AW |
6 | */ |
7 | ||
b14f84e0 JM |
8 | #include "GcodeDispatch.h" |
9 | ||
4cff3ded | 10 | #include "libs/Kernel.h" |
00e607c7 | 11 | #include "Robot.h" |
4cff3ded | 12 | #include "utils/Gcode.h" |
436a2cd1 | 13 | #include "libs/nuts_bolts.h" |
3fceb8eb | 14 | #include "modules/robot/Conveyor.h" |
423df6df | 15 | #include "libs/SerialMessage.h" |
838b33b4 | 16 | #include "libs/StreamOutput.h" |
17704c93 | 17 | #include "libs/StreamOutputPool.h" |
c4d4e3f8 | 18 | #include "libs/FileStream.h" |
825f27de | 19 | #include "libs/AppendFileStream.h" |
61134a65 | 20 | #include "Config.h" |
7af0714f | 21 | #include "checksumm.h" |
8d54c34c | 22 | #include "ConfigValue.h" |
c89338bd JM |
23 | #include "PublicDataRequest.h" |
24 | #include "PublicData.h" | |
5c051155 JM |
25 | #include "SimpleShell.h" |
26 | #include "utils.h" | |
5534e39e | 27 | #include "LPC17xx.h" |
4cff3ded | 28 | |
b375ba1d | 29 | #define return_error_on_unhandled_gcode_checksum CHECKSUM("return_error_on_unhandled_gcode") |
c89338bd JM |
30 | #define panel_display_message_checksum CHECKSUM("display_message") |
31 | #define panel_checksum CHECKSUM("panel") | |
b375ba1d | 32 | |
4fb7087f | 33 | // goes in Flash, list of Mxxx codes that are allowed when in Halted state |
f88068ec | 34 | static const int allowed_mcodes[]= {2,5,9,30,105,114,119,80,81,911,503,106,107}; // get temp, get pos, get endstops etc |
4fb7087f JM |
35 | static bool is_allowed_mcode(int m) { |
36 | for (size_t i = 0; i < sizeof(allowed_mcodes)/sizeof(int); ++i) { | |
37 | if(allowed_mcodes[i] == m) return true; | |
38 | } | |
39 | return false; | |
40 | } | |
41 | ||
b375ba1d JM |
42 | GcodeDispatch::GcodeDispatch() |
43 | { | |
b375ba1d JM |
44 | uploading = false; |
45 | currentline = -1; | |
ae7c4219 | 46 | modal_group_1= 0; |
b375ba1d | 47 | } |
4cff3ded AW |
48 | |
49 | // Called when the module has just been loaded | |
adf89655 JM |
50 | void GcodeDispatch::on_module_loaded() |
51 | { | |
4cff3ded AW |
52 | this->register_for_event(ON_CONSOLE_LINE_RECEIVED); |
53 | } | |
54 | ||
55 | // When a command is received, if it is a Gcode, dispatch it as an object via an event | |
adf89655 JM |
56 | void GcodeDispatch::on_console_line_received(void *line) |
57 | { | |
58 | SerialMessage new_message = *static_cast<SerialMessage *>(line); | |
de91760a | 59 | string possible_command = new_message.message; |
b6c86164 | 60 | |
62567509 MC |
61 | int ln = 0; |
62 | int cs = 0; | |
c4c70e85 | 63 | |
2a58aabf | 64 | // just reply ok to empty lines |
543c4b6d JM |
65 | if(possible_command.empty()) { |
66 | new_message.stream->printf("ok\r\n"); | |
67 | return; | |
68 | } | |
69 | ||
2a58aabf JM |
70 | try_again: |
71 | ||
c4c70e85 | 72 | char first_char = possible_command[0]; |
c2885de8 | 73 | unsigned int n; |
6c0193b3 JM |
74 | |
75 | if(first_char == '$') { | |
76 | // ignore as simpleshell will handle it | |
77 | return; | |
78 | ||
79 | }else if(islower(first_char)) { | |
80 | // ignore all lowercase as they are simpleshell commands | |
81 | return; | |
82 | } | |
83 | ||
7d105bc4 | 84 | if ( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'S' || first_char == 'N' ) { |
9a29161f | 85 | |
62567509 | 86 | //Get linenumber |
adf89655 | 87 | if ( first_char == 'N' ) { |
aae5a789 | 88 | Gcode full_line = Gcode(possible_command, new_message.stream, false); |
534e17b9 MC |
89 | ln = (int) full_line.get_value('N'); |
90 | int chksum = (int) full_line.get_value('*'); | |
62567509 | 91 | |
f337d8fd | 92 | //Catch message if it is M110: Set Current Line Number |
b9b1bb25 JM |
93 | if ( full_line.has_m ) { |
94 | if ( full_line.m == 110 ) { | |
f337d8fd MC |
95 | currentline = ln; |
96 | new_message.stream->printf("ok\r\n"); | |
97 | return; | |
98 | } | |
99 | } | |
100 | ||
534e17b9 | 101 | //Strip checksum value from possible_command |
62567509 | 102 | size_t chkpos = possible_command.find_first_of("*"); |
de91760a | 103 | possible_command = possible_command.substr(0, chkpos); |
534e17b9 | 104 | //Calculate checksum |
adf89655 JM |
105 | if ( chkpos != string::npos ) { |
106 | for (auto c = possible_command.cbegin(); *c != '*' && c != possible_command.cend(); c++) | |
7a2ca498 | 107 | cs = cs ^ *c; |
62567509 | 108 | cs &= 0xff; // Defensive programming... |
534e17b9 | 109 | cs -= chksum; |
62567509 | 110 | } |
534e17b9 | 111 | //Strip line number value from possible_command |
9883efb8 | 112 | size_t lnsize = possible_command.find_first_not_of("N0123456789.,- "); |
de91760a | 113 | possible_command = possible_command.substr(lnsize); |
f337d8fd | 114 | |
adf89655 | 115 | } else { |
534e17b9 MC |
116 | //Assume checks succeeded |
117 | cs = 0x00; | |
118 | ln = currentline + 1; | |
9a29161f MC |
119 | } |
120 | ||
436a2cd1 | 121 | //Remove comments |
30c006fd | 122 | size_t comment = possible_command.find_first_of(";("); |
adf89655 JM |
123 | if( comment != string::npos ) { |
124 | possible_command = possible_command.substr(0, comment); | |
125 | } | |
436a2cd1 | 126 | |
62567509 MC |
127 | //If checksum passes then process message, else request resend |
128 | int nextline = currentline + 1; | |
adf89655 | 129 | if( cs == 0x00 && ln == nextline ) { |
f337d8fd | 130 | if( first_char == 'N' ) { |
534e17b9 | 131 | currentline = nextline; |
f337d8fd MC |
132 | } |
133 | ||
d6190d18 | 134 | while(possible_command.size() > 0) { |
49d2ca86 JM |
135 | // assumes G or M are always the first on the line |
136 | size_t nextcmd = possible_command.find_first_of("GM", 2); | |
d6190d18 | 137 | string single_command; |
85bb8e89 | 138 | if(nextcmd == string::npos) { |
d6190d18 MC |
139 | single_command = possible_command; |
140 | possible_command = ""; | |
adf89655 JM |
141 | } else { |
142 | single_command = possible_command.substr(0, nextcmd); | |
2c4285ec | 143 | possible_command = possible_command.substr(nextcmd); |
d6190d18 | 144 | } |
702023f3 | 145 | |
b375ba1d | 146 | |
bb270b88 JM |
147 | if(!uploading || upload_stream != new_message.stream) { |
148 | // Prepare gcode for dispatch | |
adf89655 | 149 | Gcode *gcode = new Gcode(single_command, new_message.stream); |
c4c70e85 | 150 | |
73706276 | 151 | if(THEKERNEL->is_halted()) { |
4fb7087f | 152 | // we ignore all commands until M999, unless it is in the exceptions list (like M105 get temp) |
b375ba1d | 153 | if(gcode->has_m && gcode->m == 999) { |
728477c4 | 154 | THEKERNEL->call_event(ON_HALT, (void *)1); // clears on_halt |
7baa50df | 155 | new_message.stream->printf("WARNING: After HALT you should HOME as position is currently unknown\n"); |
76217df5 | 156 | // fall through and pass onto other modules |
4fb7087f JM |
157 | |
158 | }else if(!is_allowed_mcode(gcode->m)) { | |
76217df5 | 159 | // ignore everything, return error string to host |
6c0193b3 | 160 | if(THEKERNEL->is_grbl_mode()) { |
07186543 | 161 | new_message.stream->printf("error:Alarm lock\n"); |
6c0193b3 JM |
162 | |
163 | }else{ | |
164 | new_message.stream->printf("!!\r\n"); | |
165 | } | |
76217df5 JM |
166 | delete gcode; |
167 | continue; | |
b375ba1d | 168 | } |
b375ba1d JM |
169 | } |
170 | ||
c2885de8 | 171 | if(gcode->has_g) { |
00e607c7 JM |
172 | if(gcode->g == 53) { // G53 makes next movement command use machine coordinates |
173 | // this is ugly to implement as there may or may not be a G0/G1 on the same line | |
a3be54e3 | 174 | // valid version seem to include G53 G0 X1 Y2 Z3 G53 X1 Y2 |
00e607c7 | 175 | if(possible_command.empty()) { |
5d2319a9 | 176 | // use last gcode G1 or G0 if none on the line, and pass through as if it was a G0/G1 |
ae7c4219 JM |
177 | // TODO it is really an error if the last is not G0 thru G3 |
178 | if(modal_group_1 > 3) { | |
5d2319a9 JM |
179 | delete gcode; |
180 | new_message.stream->printf("ok - Invalid G53\r\n"); | |
181 | return; | |
182 | } | |
183 | // use last G0 or G1 | |
ae7c4219 | 184 | gcode->g= modal_group_1; |
00e607c7 JM |
185 | |
186 | }else{ | |
187 | delete gcode; | |
188 | // extract next G0/G1 from the rest of the line, ignore if it is not one of these | |
189 | gcode = new Gcode(possible_command, new_message.stream); | |
5d2319a9 | 190 | possible_command= ""; |
00e607c7 JM |
191 | if(!gcode->has_g || gcode->g > 1) { |
192 | // not G0 or G1 so ignore it as it is invalid | |
193 | delete gcode; | |
194 | new_message.stream->printf("ok - Invalid G53\r\n"); | |
195 | return; | |
196 | } | |
197 | } | |
198 | // makes it handle the parameters as a machine position | |
c8bac202 | 199 | THEROBOT->next_command_is_MCS= true; |
5d2319a9 | 200 | |
00e607c7 | 201 | } |
5d2319a9 | 202 | |
ae7c4219 JM |
203 | // remember last modal group 1 code |
204 | if(gcode->g < 4) { | |
205 | modal_group_1= gcode->g; | |
206 | } | |
c2885de8 | 207 | } |
b375ba1d | 208 | |
adf89655 JM |
209 | if(gcode->has_m) { |
210 | switch (gcode->m) { | |
211 | case 28: // start upload command | |
212 | delete gcode; | |
213 | ||
214 | this->upload_filename = "/sd/" + single_command.substr(4); // rest of line is filename | |
215 | // open file | |
216 | upload_fd = fopen(this->upload_filename.c_str(), "w"); | |
217 | if(upload_fd != NULL) { | |
218 | this->uploading = true; | |
6d91d256 | 219 | new_message.stream->printf("Writing to file: %s\r\nok\r\n", this->upload_filename.c_str()); |
adf89655 | 220 | } else { |
6d91d256 | 221 | new_message.stream->printf("open failed, File: %s.\r\nok\r\n", this->upload_filename.c_str()); |
adf89655 | 222 | } |
bb270b88 JM |
223 | |
224 | // only save stuff from this stream | |
225 | upload_stream= new_message.stream; | |
226 | ||
adf89655 JM |
227 | //printf("Start Uploading file: %s, %p\n", upload_filename.c_str(), upload_fd); |
228 | continue; | |
229 | ||
01a8d21a JM |
230 | case 30: // end of program |
231 | if(!THEKERNEL->is_grbl_mode()) break; // Special case M30 as it is also delete sd card file so only do this if in grbl mode | |
232 | // fall through to M2 | |
233 | case 2: | |
8ad60a4c JM |
234 | { |
235 | modal_group_1= 1; // set to G1 | |
236 | // issue M5 and M9 in case spindle and coolant are being used | |
237 | Gcode gc1("M5", &StreamOutput::NullStream); | |
238 | THEKERNEL->call_event(ON_GCODE_RECEIVED, &gc1); | |
239 | Gcode gc2("M9", &StreamOutput::NullStream); | |
240 | THEKERNEL->call_event(ON_GCODE_RECEIVED, &gc2); | |
241 | } | |
242 | break; | |
243 | ||
17704c93 | 244 | case 112: // emergency stop, do the best we can with this |
e714bd32 | 245 | // this is also handled out-of-band (it is now with ^X in the serial driver) |
b375ba1d | 246 | // disables heaters and motors, ignores further incoming Gcode and clears block queue |
728477c4 | 247 | THEKERNEL->call_event(ON_HALT, nullptr); |
e714bd32 | 248 | THEKERNEL->streams->printf("ok Emergency Stop Requested - reset or M999 required to exit HALT state\r\n"); |
b375ba1d | 249 | delete gcode; |
17704c93 JM |
250 | return; |
251 | ||
c89338bd JM |
252 | case 117: // M117 is a special non compliant Gcode as it allows arbitrary text on the line following the command |
253 | { // concatenate the command again and send to panel if enabled | |
254 | string str= single_command.substr(4) + possible_command; | |
255 | PublicData::set_value( panel_checksum, panel_display_message_checksum, &str ); | |
256 | delete gcode; | |
257 | new_message.stream->printf("ok\r\n"); | |
258 | return; | |
259 | } | |
260 | ||
e714bd32 | 261 | case 1000: // M1000 is a special command that will pass thru the raw lowercased command to the simpleshell (for hosts that do not allow such things) |
5c051155 JM |
262 | { |
263 | // reconstruct entire command line again | |
264 | string str= single_command.substr(5) + possible_command; | |
265 | while(is_whitespace(str.front())){ str= str.substr(1); } // strip leading whitespace | |
266 | ||
267 | delete gcode; | |
268 | ||
269 | if(str.empty()) { | |
270 | SimpleShell::parse_command("help", "", new_message.stream); | |
271 | ||
272 | }else{ | |
273 | string args= lc(str); | |
274 | string cmd = shift_parameter(args); | |
275 | // find command and execute it | |
276 | if(!SimpleShell::parse_command(cmd.c_str(), args, new_message.stream)) { | |
277 | new_message.stream->printf("Command not found: %s\n", cmd.c_str()); | |
278 | } | |
279 | } | |
280 | ||
281 | new_message.stream->printf("ok\r\n"); | |
282 | return; | |
283 | } | |
284 | ||
adf89655 | 285 | case 500: // M500 save volatile settings to config-override |
04782655 | 286 | THEKERNEL->conveyor->wait_for_idle(); //just to be safe as it can take a while to run |
c4d4e3f8 | 287 | //remove(THEKERNEL->config_override_filename()); // seems to cause a hang every now and then |
5534e39e | 288 | __disable_irq(); |
c4d4e3f8 JM |
289 | { |
290 | FileStream fs(THEKERNEL->config_override_filename()); | |
291 | fs.printf("; DO NOT EDIT THIS FILE\n"); | |
292 | // this also will truncate the existing file instead of deleting it | |
293 | } | |
f2f0dfed | 294 | // replace stream with one that writes to config-override file |
825f27de | 295 | gcode->stream = new AppendFileStream(THEKERNEL->config_override_filename()); |
adf89655 | 296 | // dispatch the M500 here so we can free up the stream when done |
314ab8f7 | 297 | THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode ); |
adf89655 JM |
298 | delete gcode->stream; |
299 | delete gcode; | |
5534e39e | 300 | __enable_irq(); |
347854ff | 301 | new_message.stream->printf("Settings Stored to %s\r\nok\r\n", THEKERNEL->config_override_filename()); |
adf89655 JM |
302 | continue; |
303 | ||
c0b50fa8 JM |
304 | case 501: // load config override |
305 | case 504: // save to specific config override file | |
306 | { | |
307 | string arg= get_arguments(single_command + possible_command); // rest of line is filename | |
308 | if(arg.empty()) arg= "/sd/config-override"; | |
309 | else arg= "/sd/config-override." + arg; | |
310 | new_message.stream->printf("args: <%s>\n", arg.c_str()); | |
311 | SimpleShell::parse_command((gcode->m == 501) ? "load_command" : "save_command", arg, new_message.stream); | |
312 | } | |
313 | delete gcode; | |
314 | new_message.stream->printf("ok\r\n"); | |
315 | return; | |
316 | ||
618c9b0f | 317 | case 502: // M502 deletes config-override so everything defaults to what is in config |
347854ff | 318 | remove(THEKERNEL->config_override_filename()); |
adf89655 | 319 | delete gcode; |
5534e39e | 320 | new_message.stream->printf("config override file deleted %s, reboot needed\r\nok\r\n", THEKERNEL->config_override_filename()); |
adf89655 JM |
321 | continue; |
322 | ||
323 | case 503: { // M503 display live settings and indicates if there is an override file | |
347854ff | 324 | FILE *fd = fopen(THEKERNEL->config_override_filename(), "r"); |
adf89655 JM |
325 | if(fd != NULL) { |
326 | fclose(fd); | |
347854ff | 327 | new_message.stream->printf("; config override present: %s\n", THEKERNEL->config_override_filename()); |
adf89655 JM |
328 | |
329 | } else { | |
330 | new_message.stream->printf("; No config override\n"); | |
331 | } | |
08d9afc6 | 332 | gcode->add_nl= true; |
618c9b0f | 333 | break; // fall through to process by modules |
adf89655 | 334 | } |
c0b50fa8 | 335 | |
44e8c359 | 336 | } |
33e4cc02 JM |
337 | } |
338 | ||
339 | //printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m); | |
44e8c359 | 340 | //Dispatch message! |
314ab8f7 | 341 | THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode ); |
44e8c359 | 342 | |
166836be JM |
343 | if (gcode->is_error) { |
344 | // report error | |
345 | if(THEKERNEL->is_grbl_mode()) { | |
346 | new_message.stream->printf("error: "); | |
347 | }else{ | |
8e777b5f | 348 | new_message.stream->printf("Error: "); |
166836be JM |
349 | } |
350 | ||
351 | if(!gcode->txt_after_ok.empty()) { | |
352 | new_message.stream->printf("%s\r\n", gcode->txt_after_ok.c_str()); | |
353 | gcode->txt_after_ok.clear(); | |
354 | ||
355 | }else{ | |
356 | new_message.stream->printf("unknown\r\n"); | |
357 | } | |
358 | ||
359 | }else{ | |
360 | ||
361 | if(gcode->add_nl) | |
362 | new_message.stream->printf("\r\n"); | |
363 | ||
364 | if(!gcode->txt_after_ok.empty()) { | |
365 | new_message.stream->printf("ok %s\r\n", gcode->txt_after_ok.c_str()); | |
366 | gcode->txt_after_ok.clear(); | |
05cdadd6 | 367 | |
05cdadd6 | 368 | } else { |
166836be JM |
369 | if(THEKERNEL->is_ok_per_line() || THEKERNEL->is_grbl_mode()) { |
370 | // only send ok once per line if this is a multi g code line send ok on the last one | |
371 | if(possible_command.empty()) | |
372 | new_message.stream->printf("ok\r\n"); | |
373 | } else { | |
374 | // maybe should do the above for all hosts? | |
375 | new_message.stream->printf("ok\r\n"); | |
376 | } | |
05cdadd6 JM |
377 | } |
378 | } | |
44e8c359 JM |
379 | |
380 | delete gcode; | |
381 | ||
adf89655 | 382 | } else { |
bb270b88 | 383 | // we are uploading and it is the upload stream so so save it |
44e8c359 JM |
384 | if(single_command.substr(0, 3) == "M29") { |
385 | // done uploading, close file | |
386 | fclose(upload_fd); | |
adf89655 JM |
387 | upload_fd = NULL; |
388 | uploading = false; | |
44e8c359 | 389 | upload_filename.clear(); |
bb270b88 | 390 | upload_stream= nullptr; |
6d91d256 | 391 | new_message.stream->printf("Done saving file.\r\nok\r\n"); |
44e8c359 JM |
392 | continue; |
393 | } | |
adf89655 | 394 | |
44e8c359 JM |
395 | if(upload_fd == NULL) { |
396 | // error detected writing to file so discard everything until it stops | |
397 | new_message.stream->printf("ok\r\n"); | |
398 | continue; | |
399 | } | |
adf89655 | 400 | |
44e8c359 | 401 | single_command.append("\n"); |
adf89655 | 402 | static int cnt = 0; |
44e8c359 JM |
403 | if(fwrite(single_command.c_str(), 1, single_command.size(), upload_fd) != single_command.size()) { |
404 | // error writing to file | |
405 | new_message.stream->printf("Error:error writing to file.\r\n"); | |
406 | fclose(upload_fd); | |
adf89655 | 407 | upload_fd = NULL; |
44e8c359 | 408 | continue; |
adf89655 JM |
409 | |
410 | } else { | |
411 | cnt += single_command.size(); | |
412 | if (cnt > 400) { | |
44e8c359 JM |
413 | // HACK ALERT to get around fwrite corruption close and re open for append |
414 | fclose(upload_fd); | |
adf89655 JM |
415 | upload_fd = fopen(upload_filename.c_str(), "a"); |
416 | cnt = 0; | |
44e8c359 JM |
417 | } |
418 | new_message.stream->printf("ok\r\n"); | |
419 | //printf("uploading file write ok\n"); | |
420 | } | |
421 | } | |
d6190d18 | 422 | } |
adf89655 JM |
423 | |
424 | } else { | |
62567509 | 425 | //Request resend |
f337d8fd | 426 | new_message.stream->printf("rs N%d\r\n", nextline); |
62567509 | 427 | } |
2bb8b390 | 428 | |
c2885de8 | 429 | } else if( (n=possible_command.find_first_of("XYZF")) == 0 || (first_char == ' ' && n != string::npos) ) { |
ae7c4219 | 430 | // handle pycam syntax, use last modal group 1 command and resubmit if an X Y Z or F is found on its own line |
c2885de8 | 431 | char buf[6]; |
ae7c4219 | 432 | snprintf(buf, sizeof(buf), "G%d ", modal_group_1); |
c2885de8 JM |
433 | possible_command.insert(0, buf); |
434 | goto try_again; | |
435 | ||
adf89655 JM |
436 | // Ignore comments and blank lines |
437 | } else if ( first_char == ';' || first_char == '(' || first_char == ' ' || first_char == '\n' || first_char == '\r' ) { | |
b6c86164 | 438 | new_message.stream->printf("ok\r\n"); |
4cff3ded AW |
439 | } |
440 | } | |
441 |