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 | ||
8 | #include <string> | |
9 | using std::string; | |
4cff3ded AW |
10 | #include "libs/Module.h" |
11 | #include "libs/Kernel.h" | |
12 | #include "utils/Gcode.h" | |
17704c93 | 13 | #include "Pauser.h" |
436a2cd1 | 14 | #include "libs/nuts_bolts.h" |
4cff3ded | 15 | #include "GcodeDispatch.h" |
3fceb8eb | 16 | #include "modules/robot/Conveyor.h" |
423df6df | 17 | #include "libs/SerialMessage.h" |
838b33b4 | 18 | #include "libs/StreamOutput.h" |
17704c93 | 19 | #include "libs/StreamOutputPool.h" |
f2f0dfed | 20 | #include "libs/FileStream.h" |
61134a65 | 21 | #include "Config.h" |
7af0714f | 22 | #include "checksumm.h" |
8d54c34c | 23 | #include "ConfigValue.h" |
4cff3ded | 24 | |
b375ba1d JM |
25 | #define return_error_on_unhandled_gcode_checksum CHECKSUM("return_error_on_unhandled_gcode") |
26 | ||
4fb7087f JM |
27 | // goes in Flash, list of Mxxx codes that are allowed when in Halted state |
28 | static const int allowed_mcodes[]= {105,114}; // get temp, get pos | |
29 | static bool is_allowed_mcode(int m) { | |
30 | for (size_t i = 0; i < sizeof(allowed_mcodes)/sizeof(int); ++i) { | |
31 | if(allowed_mcodes[i] == m) return true; | |
32 | } | |
33 | return false; | |
34 | } | |
35 | ||
b375ba1d JM |
36 | GcodeDispatch::GcodeDispatch() |
37 | { | |
38 | halted= false; | |
39 | uploading = false; | |
40 | currentline = -1; | |
41 | last_g= 255; | |
42 | } | |
4cff3ded AW |
43 | |
44 | // Called when the module has just been loaded | |
adf89655 JM |
45 | void GcodeDispatch::on_module_loaded() |
46 | { | |
314ab8f7 | 47 | return_error_on_unhandled_gcode = THEKERNEL->config->value( return_error_on_unhandled_gcode_checksum )->by_default(false)->as_bool(); |
4cff3ded | 48 | this->register_for_event(ON_CONSOLE_LINE_RECEIVED); |
b375ba1d JM |
49 | this->register_for_event(ON_HALT); |
50 | } | |
51 | ||
52 | void GcodeDispatch::on_halt(void *arg) | |
53 | { | |
54 | // set halt stream and ignore everything until M999 | |
728477c4 | 55 | this->halted= (arg == nullptr); |
4cff3ded AW |
56 | } |
57 | ||
58 | // When a command is received, if it is a Gcode, dispatch it as an object via an event | |
adf89655 JM |
59 | void GcodeDispatch::on_console_line_received(void *line) |
60 | { | |
61 | SerialMessage new_message = *static_cast<SerialMessage *>(line); | |
de91760a | 62 | string possible_command = new_message.message; |
b6c86164 | 63 | |
62567509 MC |
64 | int ln = 0; |
65 | int cs = 0; | |
c4c70e85 JM |
66 | |
67 | try_again: | |
68 | ||
69 | char first_char = possible_command[0]; | |
c2885de8 | 70 | unsigned int n; |
adf89655 | 71 | if ( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'N' ) { |
9a29161f | 72 | |
62567509 | 73 | //Get linenumber |
adf89655 | 74 | if ( first_char == 'N' ) { |
aae5a789 | 75 | Gcode full_line = Gcode(possible_command, new_message.stream, false); |
534e17b9 MC |
76 | ln = (int) full_line.get_value('N'); |
77 | int chksum = (int) full_line.get_value('*'); | |
62567509 | 78 | |
f337d8fd | 79 | //Catch message if it is M110: Set Current Line Number |
b9b1bb25 JM |
80 | if ( full_line.has_m ) { |
81 | if ( full_line.m == 110 ) { | |
f337d8fd MC |
82 | currentline = ln; |
83 | new_message.stream->printf("ok\r\n"); | |
84 | return; | |
85 | } | |
86 | } | |
87 | ||
534e17b9 | 88 | //Strip checksum value from possible_command |
62567509 | 89 | size_t chkpos = possible_command.find_first_of("*"); |
de91760a | 90 | possible_command = possible_command.substr(0, chkpos); |
534e17b9 | 91 | //Calculate checksum |
adf89655 JM |
92 | if ( chkpos != string::npos ) { |
93 | for (auto c = possible_command.cbegin(); *c != '*' && c != possible_command.cend(); c++) | |
7a2ca498 | 94 | cs = cs ^ *c; |
62567509 | 95 | cs &= 0xff; // Defensive programming... |
534e17b9 | 96 | cs -= chksum; |
62567509 | 97 | } |
534e17b9 | 98 | //Strip line number value from possible_command |
9883efb8 | 99 | size_t lnsize = possible_command.find_first_not_of("N0123456789.,- "); |
de91760a | 100 | possible_command = possible_command.substr(lnsize); |
f337d8fd | 101 | |
adf89655 | 102 | } else { |
534e17b9 MC |
103 | //Assume checks succeeded |
104 | cs = 0x00; | |
105 | ln = currentline + 1; | |
9a29161f MC |
106 | } |
107 | ||
436a2cd1 | 108 | //Remove comments |
30c006fd | 109 | size_t comment = possible_command.find_first_of(";("); |
adf89655 JM |
110 | if( comment != string::npos ) { |
111 | possible_command = possible_command.substr(0, comment); | |
112 | } | |
436a2cd1 | 113 | |
62567509 MC |
114 | //If checksum passes then process message, else request resend |
115 | int nextline = currentline + 1; | |
adf89655 | 116 | if( cs == 0x00 && ln == nextline ) { |
f337d8fd | 117 | if( first_char == 'N' ) { |
534e17b9 | 118 | currentline = nextline; |
f337d8fd MC |
119 | } |
120 | ||
d6190d18 | 121 | while(possible_command.size() > 0) { |
43c443ef | 122 | size_t nextcmd = possible_command.find_first_of("GM", possible_command.find_first_of("GM") + 1); |
d6190d18 | 123 | string single_command; |
85bb8e89 | 124 | if(nextcmd == string::npos) { |
d6190d18 MC |
125 | single_command = possible_command; |
126 | possible_command = ""; | |
adf89655 JM |
127 | } else { |
128 | single_command = possible_command.substr(0, nextcmd); | |
2c4285ec | 129 | possible_command = possible_command.substr(nextcmd); |
d6190d18 | 130 | } |
702023f3 | 131 | |
b375ba1d | 132 | |
44e8c359 JM |
133 | if(!uploading) { |
134 | //Prepare gcode for dispatch | |
adf89655 | 135 | Gcode *gcode = new Gcode(single_command, new_message.stream); |
c4c70e85 | 136 | |
b375ba1d | 137 | if(halted) { |
4fb7087f | 138 | // we ignore all commands until M999, unless it is in the exceptions list (like M105 get temp) |
b375ba1d | 139 | if(gcode->has_m && gcode->m == 999) { |
728477c4 | 140 | THEKERNEL->call_event(ON_HALT, (void *)1); // clears on_halt |
b375ba1d | 141 | halted= false; |
76217df5 | 142 | // fall through and pass onto other modules |
4fb7087f JM |
143 | |
144 | }else if(!is_allowed_mcode(gcode->m)) { | |
76217df5 | 145 | // ignore everything, return error string to host |
b375ba1d | 146 | new_message.stream->printf("!!\r\n"); |
76217df5 JM |
147 | delete gcode; |
148 | continue; | |
b375ba1d | 149 | } |
b375ba1d JM |
150 | } |
151 | ||
c2885de8 JM |
152 | if(gcode->has_g) { |
153 | last_g= gcode->g; | |
154 | } | |
b375ba1d | 155 | |
adf89655 JM |
156 | if(gcode->has_m) { |
157 | switch (gcode->m) { | |
158 | case 28: // start upload command | |
159 | delete gcode; | |
160 | ||
161 | this->upload_filename = "/sd/" + single_command.substr(4); // rest of line is filename | |
162 | // open file | |
163 | upload_fd = fopen(this->upload_filename.c_str(), "w"); | |
164 | if(upload_fd != NULL) { | |
165 | this->uploading = true; | |
166 | new_message.stream->printf("Writing to file: %s\r\n", this->upload_filename.c_str()); | |
167 | } else { | |
168 | new_message.stream->printf("open failed, File: %s.\r\n", this->upload_filename.c_str()); | |
169 | } | |
170 | //printf("Start Uploading file: %s, %p\n", upload_filename.c_str(), upload_fd); | |
171 | continue; | |
172 | ||
17704c93 JM |
173 | case 112: // emergency stop, do the best we can with this |
174 | // TODO this really needs to be handled out-of-band | |
b375ba1d | 175 | // disables heaters and motors, ignores further incoming Gcode and clears block queue |
728477c4 | 176 | THEKERNEL->call_event(ON_HALT, nullptr); |
b375ba1d JM |
177 | THEKERNEL->streams->printf("ok Emergency Stop Requested - reset or M999 required to continue\r\n"); |
178 | delete gcode; | |
17704c93 JM |
179 | return; |
180 | ||
adf89655 | 181 | case 500: // M500 save volatile settings to config-override |
f2f0dfed | 182 | // replace stream with one that writes to config-override file |
347854ff | 183 | gcode->stream = new FileStream(THEKERNEL->config_override_filename()); |
adf89655 | 184 | // dispatch the M500 here so we can free up the stream when done |
314ab8f7 | 185 | THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode ); |
adf89655 JM |
186 | delete gcode->stream; |
187 | delete gcode; | |
347854ff | 188 | new_message.stream->printf("Settings Stored to %s\r\nok\r\n", THEKERNEL->config_override_filename()); |
adf89655 JM |
189 | continue; |
190 | ||
618c9b0f | 191 | case 502: // M502 deletes config-override so everything defaults to what is in config |
347854ff MM |
192 | remove(THEKERNEL->config_override_filename()); |
193 | new_message.stream->printf("config override file deleted %s, reboot needed\r\nok\r\n", THEKERNEL->config_override_filename()); | |
adf89655 JM |
194 | delete gcode; |
195 | continue; | |
196 | ||
197 | case 503: { // M503 display live settings and indicates if there is an override file | |
347854ff | 198 | FILE *fd = fopen(THEKERNEL->config_override_filename(), "r"); |
adf89655 JM |
199 | if(fd != NULL) { |
200 | fclose(fd); | |
347854ff | 201 | new_message.stream->printf("; config override present: %s\n", THEKERNEL->config_override_filename()); |
adf89655 JM |
202 | |
203 | } else { | |
204 | new_message.stream->printf("; No config override\n"); | |
205 | } | |
08d9afc6 | 206 | gcode->add_nl= true; |
618c9b0f | 207 | break; // fall through to process by modules |
adf89655 | 208 | } |
44e8c359 | 209 | } |
33e4cc02 JM |
210 | } |
211 | ||
212 | //printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m); | |
44e8c359 | 213 | //Dispatch message! |
314ab8f7 | 214 | THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode ); |
adf89655 | 215 | if(gcode->add_nl) |
44e8c359 JM |
216 | new_message.stream->printf("\r\n"); |
217 | ||
adf89655 | 218 | if( return_error_on_unhandled_gcode == true && gcode->accepted_by_module == false) |
44e8c359 JM |
219 | new_message.stream->printf("ok (command unclaimed)\r\n"); |
220 | else if(!gcode->txt_after_ok.empty()) { | |
221 | new_message.stream->printf("ok %s\r\n", gcode->txt_after_ok.c_str()); | |
222 | gcode->txt_after_ok.clear(); | |
adf89655 | 223 | } else |
44e8c359 JM |
224 | new_message.stream->printf("ok\r\n"); |
225 | ||
226 | delete gcode; | |
227 | ||
adf89655 | 228 | } else { |
44e8c359 JM |
229 | // we are uploading a file so save it |
230 | if(single_command.substr(0, 3) == "M29") { | |
231 | // done uploading, close file | |
232 | fclose(upload_fd); | |
adf89655 JM |
233 | upload_fd = NULL; |
234 | uploading = false; | |
44e8c359 JM |
235 | upload_filename.clear(); |
236 | new_message.stream->printf("Done saving file.\r\n"); | |
237 | continue; | |
238 | } | |
adf89655 | 239 | |
44e8c359 JM |
240 | if(upload_fd == NULL) { |
241 | // error detected writing to file so discard everything until it stops | |
242 | new_message.stream->printf("ok\r\n"); | |
243 | continue; | |
244 | } | |
adf89655 | 245 | |
44e8c359 | 246 | single_command.append("\n"); |
adf89655 | 247 | static int cnt = 0; |
44e8c359 JM |
248 | if(fwrite(single_command.c_str(), 1, single_command.size(), upload_fd) != single_command.size()) { |
249 | // error writing to file | |
250 | new_message.stream->printf("Error:error writing to file.\r\n"); | |
251 | fclose(upload_fd); | |
adf89655 | 252 | upload_fd = NULL; |
44e8c359 | 253 | continue; |
adf89655 JM |
254 | |
255 | } else { | |
256 | cnt += single_command.size(); | |
257 | if (cnt > 400) { | |
44e8c359 JM |
258 | // HACK ALERT to get around fwrite corruption close and re open for append |
259 | fclose(upload_fd); | |
adf89655 JM |
260 | upload_fd = fopen(upload_filename.c_str(), "a"); |
261 | cnt = 0; | |
44e8c359 JM |
262 | } |
263 | new_message.stream->printf("ok\r\n"); | |
264 | //printf("uploading file write ok\n"); | |
265 | } | |
266 | } | |
d6190d18 | 267 | } |
adf89655 JM |
268 | |
269 | } else { | |
62567509 | 270 | //Request resend |
f337d8fd | 271 | new_message.stream->printf("rs N%d\r\n", nextline); |
62567509 | 272 | } |
2bb8b390 | 273 | |
c2885de8 | 274 | } else if( (n=possible_command.find_first_of("XYZF")) == 0 || (first_char == ' ' && n != string::npos) ) { |
c4c70e85 JM |
275 | // handle pycam syntax, use last G0 or G1 and resubmit if an X Y Z or F is found on its own line |
276 | if(last_g != 0 && last_g != 1) { | |
277 | //if no last G1 or G0 ignore | |
278 | //THEKERNEL->streams->printf("ignored: %s\r\n", possible_command.c_str()); | |
279 | return; | |
280 | } | |
c2885de8 JM |
281 | char buf[6]; |
282 | snprintf(buf, sizeof(buf), "G%d ", last_g); | |
283 | possible_command.insert(0, buf); | |
284 | goto try_again; | |
285 | ||
adf89655 JM |
286 | // Ignore comments and blank lines |
287 | } else if ( first_char == ';' || first_char == '(' || first_char == ' ' || first_char == '\n' || first_char == '\r' ) { | |
b6c86164 | 288 | new_message.stream->printf("ok\r\n"); |
4cff3ded AW |
289 | } |
290 | } | |
291 |