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