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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
8 #include "GcodeDispatch.h"
10 #include "libs/Kernel.h"
12 #include "utils/Gcode.h"
13 #include "libs/nuts_bolts.h"
14 #include "modules/robot/Conveyor.h"
15 #include "libs/SerialMessage.h"
16 #include "libs/StreamOutput.h"
17 #include "libs/StreamOutputPool.h"
18 #include "libs/FileStream.h"
19 #include "libs/AppendFileStream.h"
21 #include "checksumm.h"
22 #include "ConfigValue.h"
23 #include "PublicDataRequest.h"
24 #include "PublicData.h"
25 #include "SimpleShell.h"
29 #define return_error_on_unhandled_gcode_checksum CHECKSUM("return_error_on_unhandled_gcode")
30 #define panel_display_message_checksum CHECKSUM("display_message")
31 #define panel_checksum CHECKSUM("panel")
33 // goes in Flash, list of Mxxx codes that are allowed when in Halted state
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
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;
42 GcodeDispatch::GcodeDispatch()
49 // Called when the module has just been loaded
50 void GcodeDispatch::on_module_loaded()
52 this->register_for_event(ON_CONSOLE_LINE_RECEIVED
);
55 // When a command is received, if it is a Gcode, dispatch it as an object via an event
56 void GcodeDispatch::on_console_line_received(void *line
)
58 SerialMessage new_message
= *static_cast<SerialMessage
*>(line
);
59 string possible_command
= new_message
.message
;
64 // just reply ok to empty lines
65 if(possible_command
.empty()) {
66 new_message
.stream
->printf("ok\r\n");
72 char first_char
= possible_command
[0];
75 if(first_char
== '$') {
76 // ignore as simpleshell will handle it
79 }else if(islower(first_char
)) {
80 // ignore all lowercase as they are simpleshell commands
84 if ( first_char
== 'G' || first_char
== 'M' || first_char
== 'T' || first_char
== 'N' ) {
87 if ( first_char
== 'N' ) {
88 Gcode full_line
= Gcode(possible_command
, new_message
.stream
, false);
89 ln
= (int) full_line
.get_value('N');
90 int chksum
= (int) full_line
.get_value('*');
92 //Catch message if it is M110: Set Current Line Number
93 if ( full_line
.has_m
) {
94 if ( full_line
.m
== 110 ) {
96 new_message
.stream
->printf("ok\r\n");
101 //Strip checksum value from possible_command
102 size_t chkpos
= possible_command
.find_first_of("*");
103 possible_command
= possible_command
.substr(0, chkpos
);
105 if ( chkpos
!= string::npos
) {
106 for (auto c
= possible_command
.cbegin(); *c
!= '*' && c
!= possible_command
.cend(); c
++)
108 cs
&= 0xff; // Defensive programming...
111 //Strip line number value from possible_command
112 size_t lnsize
= possible_command
.find_first_not_of("N0123456789.,- ");
113 possible_command
= possible_command
.substr(lnsize
);
116 //Assume checks succeeded
118 ln
= currentline
+ 1;
122 size_t comment
= possible_command
.find_first_of(";(");
123 if( comment
!= string::npos
) {
124 possible_command
= possible_command
.substr(0, comment
);
127 //If checksum passes then process message, else request resend
128 int nextline
= currentline
+ 1;
129 if( cs
== 0x00 && ln
== nextline
) {
130 if( first_char
== 'N' ) {
131 currentline
= nextline
;
134 while(possible_command
.size() > 0) {
135 // assumes G or M are always the first on the line
136 size_t nextcmd
= possible_command
.find_first_of("GM", 2);
137 string single_command
;
138 if(nextcmd
== string::npos
) {
139 single_command
= possible_command
;
140 possible_command
= "";
142 single_command
= possible_command
.substr(0, nextcmd
);
143 possible_command
= possible_command
.substr(nextcmd
);
147 if(!uploading
|| upload_stream
!= new_message
.stream
) {
148 // Prepare gcode for dispatch
149 Gcode
*gcode
= new Gcode(single_command
, new_message
.stream
);
151 if(THEKERNEL
->is_halted()) {
152 // we ignore all commands until M999, unless it is in the exceptions list (like M105 get temp)
153 if(gcode
->has_m
&& gcode
->m
== 999) {
154 THEKERNEL
->call_event(ON_HALT
, (void *)1); // clears on_halt
156 // fall through and pass onto other modules
158 }else if(!is_allowed_mcode(gcode
->m
)) {
159 // ignore everything, return error string to host
160 if(THEKERNEL
->is_grbl_mode()) {
161 new_message
.stream
->printf("error:Alarm lock\n");
164 new_message
.stream
->printf("!!\r\n");
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
174 // valid version seem to include G53 G0 X1 Y2 Z3 G53 X1 Y2
175 if(possible_command
.empty()) {
176 // use last gcode G1 or G0 if none on the line, and pass through as if it was a G0/G1
177 // TODO it is really an error if the last is not G0 thru G3
178 if(modal_group_1
> 3) {
180 new_message
.stream
->printf("ok - Invalid G53\r\n");
184 gcode
->g
= modal_group_1
;
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
);
190 possible_command
= "";
191 if(!gcode
->has_g
|| gcode
->g
> 1) {
192 // not G0 or G1 so ignore it as it is invalid
194 new_message
.stream
->printf("ok - Invalid G53\r\n");
198 // makes it handle the parameters as a machine position
199 THEKERNEL
->robot
->next_command_is_MCS
= true;
203 // remember last modal group 1 code
205 modal_group_1
= gcode
->g
;
211 case 28: // start upload command
214 this->upload_filename
= "/sd/" + single_command
.substr(4); // rest of line is filename
216 upload_fd
= fopen(this->upload_filename
.c_str(), "w");
217 if(upload_fd
!= NULL
) {
218 this->uploading
= true;
219 new_message
.stream
->printf("Writing to file: %s\r\nok\r\n", this->upload_filename
.c_str());
221 new_message
.stream
->printf("open failed, File: %s.\r\nok\r\n", this->upload_filename
.c_str());
224 // only save stuff from this stream
225 upload_stream
= new_message
.stream
;
227 //printf("Start Uploading file: %s, %p\n", upload_filename.c_str(), upload_fd);
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
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
);
244 case 112: // emergency stop, do the best we can with this
245 // this is also handled out-of-band (it is now with ^X in the serial driver)
246 // disables heaters and motors, ignores further incoming Gcode and clears block queue
247 THEKERNEL
->call_event(ON_HALT
, nullptr);
248 THEKERNEL
->streams
->printf("ok Emergency Stop Requested - reset or M999 required to exit HALT state\r\n");
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
);
257 new_message
.stream
->printf("ok\r\n");
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)
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
270 SimpleShell::parse_command("help", "", new_message
.stream
);
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());
281 new_message
.stream
->printf("ok\r\n");
285 case 500: // M500 save volatile settings to config-override
286 THEKERNEL
->conveyor
->wait_for_empty_queue(); //just to be safe as it can take a while to run
287 //remove(THEKERNEL->config_override_filename()); // seems to cause a hang every now and then
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
294 // replace stream with one that writes to config-override file
295 gcode
->stream
= new AppendFileStream(THEKERNEL
->config_override_filename());
296 // dispatch the M500 here so we can free up the stream when done
297 THEKERNEL
->call_event(ON_GCODE_RECEIVED
, gcode
);
298 delete gcode
->stream
;
301 new_message
.stream
->printf("Settings Stored to %s\r\nok\r\n", THEKERNEL
->config_override_filename());
304 case 502: // M502 deletes config-override so everything defaults to what is in config
305 remove(THEKERNEL
->config_override_filename());
307 new_message
.stream
->printf("config override file deleted %s, reboot needed\r\nok\r\n", THEKERNEL
->config_override_filename());
310 case 503: { // M503 display live settings and indicates if there is an override file
311 FILE *fd
= fopen(THEKERNEL
->config_override_filename(), "r");
314 new_message
.stream
->printf("; config override present: %s\n", THEKERNEL
->config_override_filename());
317 new_message
.stream
->printf("; No config override\n");
320 break; // fall through to process by modules
325 //printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m);
327 THEKERNEL
->call_event(ON_GCODE_RECEIVED
, gcode
);
329 new_message
.stream
->printf("\r\n");
331 if(!gcode
->txt_after_ok
.empty()) {
332 new_message
.stream
->printf("ok %s\r\n", gcode
->txt_after_ok
.c_str());
333 gcode
->txt_after_ok
.clear();
336 if(THEKERNEL
->is_ok_per_line() || THEKERNEL
->is_grbl_mode()) {
337 // only send ok once per line if this is a multi g code line send ok on the last one
338 if(possible_command
.empty())
339 new_message
.stream
->printf("ok\r\n");
341 // maybe should do the above for all hosts?
342 new_message
.stream
->printf("ok\r\n");
349 // we are uploading and it is the upload stream so so save it
350 if(single_command
.substr(0, 3) == "M29") {
351 // done uploading, close file
355 upload_filename
.clear();
356 upload_stream
= nullptr;
357 new_message
.stream
->printf("Done saving file.\r\nok\r\n");
361 if(upload_fd
== NULL
) {
362 // error detected writing to file so discard everything until it stops
363 new_message
.stream
->printf("ok\r\n");
367 single_command
.append("\n");
369 if(fwrite(single_command
.c_str(), 1, single_command
.size(), upload_fd
) != single_command
.size()) {
370 // error writing to file
371 new_message
.stream
->printf("Error:error writing to file.\r\n");
377 cnt
+= single_command
.size();
379 // HACK ALERT to get around fwrite corruption close and re open for append
381 upload_fd
= fopen(upload_filename
.c_str(), "a");
384 new_message
.stream
->printf("ok\r\n");
385 //printf("uploading file write ok\n");
392 new_message
.stream
->printf("rs N%d\r\n", nextline
);
395 } else if( (n
=possible_command
.find_first_of("XYZF")) == 0 || (first_char
== ' ' && n
!= string::npos
) ) {
396 // handle pycam syntax, use last modal group 1 command and resubmit if an X Y Z or F is found on its own line
398 snprintf(buf
, sizeof(buf
), "G%d ", modal_group_1
);
399 possible_command
.insert(0, buf
);
402 // Ignore comments and blank lines
403 } else if ( first_char
== ';' || first_char
== '(' || first_char
== ' ' || first_char
== '\n' || first_char
== '\r' ) {
404 new_message
.stream
->printf("ok\r\n");