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