My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

queue.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * queue.cpp - The G-code command queue
  24. */
  25. #include "queue.h"
  26. #include "gcode.h"
  27. #include "../lcd/ultralcd.h"
  28. #include "../sd/cardreader.h"
  29. #include "../module/planner.h"
  30. #include "../Marlin.h"
  31. #if HAS_COLOR_LEDS
  32. #include "../feature/leds/leds.h"
  33. #endif
  34. /**
  35. * GCode line number handling. Hosts may opt to include line numbers when
  36. * sending commands to Marlin, and lines will be checked for sequentiality.
  37. * M110 N<int> sets the current line number.
  38. */
  39. long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  40. /**
  41. * GCode Command Queue
  42. * A simple ring buffer of BUFSIZE command strings.
  43. *
  44. * Commands are copied into this buffer by the command injectors
  45. * (immediate, serial, sd card) and they are processed sequentially by
  46. * the main loop. The gcode.process_next_command method parses the next
  47. * command and hands off execution to individual handler functions.
  48. */
  49. uint8_t commands_in_queue = 0, // Count of commands in the queue
  50. cmd_queue_index_r = 0, // Ring buffer read position
  51. cmd_queue_index_w = 0; // Ring buffer write position
  52. char command_queue[BUFSIZE][MAX_CMD_SIZE];
  53. /**
  54. * Serial command injection
  55. */
  56. // Number of characters read in the current line of serial input
  57. static int serial_count = 0;
  58. bool send_ok[BUFSIZE];
  59. /**
  60. * Next Injected Command pointer. NULL if no commands are being injected.
  61. * Used by Marlin internally to ensure that commands initiated from within
  62. * are enqueued ahead of any pending serial or sd card commands.
  63. */
  64. static const char *injected_commands_P = NULL;
  65. void queue_setup() {
  66. // Send "ok" after commands by default
  67. for (uint8_t i = 0; i < COUNT(send_ok); i++) send_ok[i] = true;
  68. }
  69. /**
  70. * Clear the Marlin command queue
  71. */
  72. void clear_command_queue() {
  73. cmd_queue_index_r = cmd_queue_index_w;
  74. commands_in_queue = 0;
  75. }
  76. /**
  77. * Once a new command is in the ring buffer, call this to commit it
  78. */
  79. inline void _commit_command(bool say_ok) {
  80. send_ok[cmd_queue_index_w] = say_ok;
  81. if (++cmd_queue_index_w >= BUFSIZE) cmd_queue_index_w = 0;
  82. commands_in_queue++;
  83. }
  84. /**
  85. * Copy a command from RAM into the main command buffer.
  86. * Return true if the command was successfully added.
  87. * Return false for a full buffer, or if the 'command' is a comment.
  88. */
  89. inline bool _enqueuecommand(const char* cmd, bool say_ok/*=false*/) {
  90. if (*cmd == ';' || commands_in_queue >= BUFSIZE) return false;
  91. strcpy(command_queue[cmd_queue_index_w], cmd);
  92. _commit_command(say_ok);
  93. return true;
  94. }
  95. /**
  96. * Enqueue with Serial Echo
  97. */
  98. bool enqueue_and_echo_command(const char* cmd, bool say_ok/*=false*/) {
  99. if (_enqueuecommand(cmd, say_ok)) {
  100. SERIAL_ECHO_START();
  101. SERIAL_ECHOPAIR(MSG_ENQUEUEING, cmd);
  102. SERIAL_CHAR('"');
  103. SERIAL_EOL();
  104. return true;
  105. }
  106. return false;
  107. }
  108. /**
  109. * Inject the next "immediate" command, when possible, onto the front of the queue.
  110. * Return true if any immediate commands remain to inject.
  111. */
  112. static bool drain_injected_commands_P() {
  113. if (injected_commands_P != NULL) {
  114. size_t i = 0;
  115. char c, cmd[30];
  116. strncpy_P(cmd, injected_commands_P, sizeof(cmd) - 1);
  117. cmd[sizeof(cmd) - 1] = '\0';
  118. while ((c = cmd[i]) && c != '\n') i++; // find the end of this gcode command
  119. cmd[i] = '\0';
  120. if (enqueue_and_echo_command(cmd)) // success?
  121. injected_commands_P = c ? injected_commands_P + i + 1 : NULL; // next command or done
  122. }
  123. return (injected_commands_P != NULL); // return whether any more remain
  124. }
  125. /**
  126. * Record one or many commands to run from program memory.
  127. * Aborts the current queue, if any.
  128. * Note: drain_injected_commands_P() must be called repeatedly to drain the commands afterwards
  129. */
  130. void enqueue_and_echo_commands_P(const char * const pgcode) {
  131. injected_commands_P = pgcode;
  132. drain_injected_commands_P(); // first command executed asap (when possible)
  133. }
  134. /**
  135. * Send an "ok" message to the host, indicating
  136. * that a command was successfully processed.
  137. *
  138. * If ADVANCED_OK is enabled also include:
  139. * N<int> Line number of the command, if any
  140. * P<int> Planner space remaining
  141. * B<int> Block queue space remaining
  142. */
  143. void ok_to_send() {
  144. gcode.refresh_cmd_timeout();
  145. if (!send_ok[cmd_queue_index_r]) return;
  146. SERIAL_PROTOCOLPGM(MSG_OK);
  147. #if ENABLED(ADVANCED_OK)
  148. char* p = command_queue[cmd_queue_index_r];
  149. if (*p == 'N') {
  150. SERIAL_PROTOCOL(' ');
  151. SERIAL_ECHO(*p++);
  152. while (NUMERIC_SIGNED(*p))
  153. SERIAL_ECHO(*p++);
  154. }
  155. SERIAL_PROTOCOLPGM(" P"); SERIAL_PROTOCOL(int(BLOCK_BUFFER_SIZE - planner.movesplanned() - 1));
  156. SERIAL_PROTOCOLPGM(" B"); SERIAL_PROTOCOL(BUFSIZE - commands_in_queue);
  157. #endif
  158. SERIAL_EOL();
  159. }
  160. /**
  161. * Send a "Resend: nnn" message to the host to
  162. * indicate that a command needs to be re-sent.
  163. */
  164. void flush_and_request_resend() {
  165. //char command_queue[cmd_queue_index_r][100]="Resend:";
  166. MYSERIAL.flush();
  167. SERIAL_PROTOCOLPGM(MSG_RESEND);
  168. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  169. ok_to_send();
  170. }
  171. void gcode_line_error(const char* err, bool doFlush = true) {
  172. SERIAL_ERROR_START();
  173. serialprintPGM(err);
  174. SERIAL_ERRORLN(gcode_LastN);
  175. //Serial.println(gcode_N);
  176. if (doFlush) flush_and_request_resend();
  177. serial_count = 0;
  178. }
  179. /**
  180. * Get all commands waiting on the serial port and queue them.
  181. * Exit when the buffer is full or when no more characters are
  182. * left on the serial port.
  183. */
  184. inline void get_serial_commands() {
  185. static char serial_line_buffer[MAX_CMD_SIZE];
  186. static bool serial_comment_mode = false;
  187. // If the command buffer is empty for too long,
  188. // send "wait" to indicate Marlin is still waiting.
  189. #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
  190. static millis_t last_command_time = 0;
  191. const millis_t ms = millis();
  192. if (commands_in_queue == 0 && !MYSERIAL.available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) {
  193. SERIAL_ECHOLNPGM(MSG_WAIT);
  194. last_command_time = ms;
  195. }
  196. #endif
  197. /**
  198. * Loop while serial characters are incoming and the queue is not full
  199. */
  200. while (commands_in_queue < BUFSIZE && MYSERIAL.available() > 0) {
  201. char serial_char = MYSERIAL.read();
  202. /**
  203. * If the character ends the line
  204. */
  205. if (serial_char == '\n' || serial_char == '\r') {
  206. serial_comment_mode = false; // end of line == end of comment
  207. if (!serial_count) continue; // skip empty lines
  208. serial_line_buffer[serial_count] = 0; // terminate string
  209. serial_count = 0; //reset buffer
  210. char* command = serial_line_buffer;
  211. while (*command == ' ') command++; // skip any leading spaces
  212. char *npos = (*command == 'N') ? command : NULL, // Require the N parameter to start the line
  213. *apos = strchr(command, '*');
  214. if (npos) {
  215. bool M110 = strstr_P(command, PSTR("M110")) != NULL;
  216. if (M110) {
  217. char* n2pos = strchr(command + 4, 'N');
  218. if (n2pos) npos = n2pos;
  219. }
  220. gcode_N = strtol(npos + 1, NULL, 10);
  221. if (gcode_N != gcode_LastN + 1 && !M110) {
  222. gcode_line_error(PSTR(MSG_ERR_LINE_NO));
  223. return;
  224. }
  225. if (apos) {
  226. byte checksum = 0, count = 0;
  227. while (command[count] != '*') checksum ^= command[count++];
  228. if (strtol(apos + 1, NULL, 10) != checksum) {
  229. gcode_line_error(PSTR(MSG_ERR_CHECKSUM_MISMATCH));
  230. return;
  231. }
  232. // if no errors, continue parsing
  233. }
  234. else {
  235. gcode_line_error(PSTR(MSG_ERR_NO_CHECKSUM));
  236. return;
  237. }
  238. gcode_LastN = gcode_N;
  239. // if no errors, continue parsing
  240. }
  241. else if (apos) { // No '*' without 'N'
  242. gcode_line_error(PSTR(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM), false);
  243. return;
  244. }
  245. // Movement commands alert when stopped
  246. if (IsStopped()) {
  247. char* gpos = strchr(command, 'G');
  248. if (gpos) {
  249. const int codenum = strtol(gpos + 1, NULL, 10);
  250. switch (codenum) {
  251. case 0:
  252. case 1:
  253. case 2:
  254. case 3:
  255. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  256. LCD_MESSAGEPGM(MSG_STOPPED);
  257. break;
  258. }
  259. }
  260. }
  261. #if DISABLED(EMERGENCY_PARSER)
  262. // If command was e-stop process now
  263. if (strcmp(command, "M108") == 0) {
  264. wait_for_heatup = false;
  265. #if ENABLED(ULTIPANEL)
  266. wait_for_user = false;
  267. #endif
  268. }
  269. if (strcmp(command, "M112") == 0) kill(PSTR(MSG_KILLED));
  270. if (strcmp(command, "M410") == 0) { quickstop_stepper(); }
  271. #endif
  272. #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
  273. last_command_time = ms;
  274. #endif
  275. // Add the command to the queue
  276. _enqueuecommand(serial_line_buffer, true);
  277. }
  278. else if (serial_count >= MAX_CMD_SIZE - 1) {
  279. // Keep fetching, but ignore normal characters beyond the max length
  280. // The command will be injected when EOL is reached
  281. }
  282. else if (serial_char == '\\') { // Handle escapes
  283. if (MYSERIAL.available() > 0) {
  284. // if we have one more character, copy it over
  285. serial_char = MYSERIAL.read();
  286. if (!serial_comment_mode) serial_line_buffer[serial_count++] = serial_char;
  287. }
  288. // otherwise do nothing
  289. }
  290. else { // it's not a newline, carriage return or escape char
  291. if (serial_char == ';') serial_comment_mode = true;
  292. if (!serial_comment_mode) serial_line_buffer[serial_count++] = serial_char;
  293. }
  294. } // queue has space, serial has data
  295. }
  296. #if ENABLED(SDSUPPORT)
  297. /**
  298. * Get commands from the SD Card until the command buffer is full
  299. * or until the end of the file is reached. The special character '#'
  300. * can also interrupt buffering.
  301. */
  302. inline void get_sdcard_commands() {
  303. static bool stop_buffering = false,
  304. sd_comment_mode = false;
  305. if (!IS_SD_PRINTING) return;
  306. /**
  307. * '#' stops reading from SD to the buffer prematurely, so procedural
  308. * macro calls are possible. If it occurs, stop_buffering is triggered
  309. * and the buffer is run dry; this character _can_ occur in serial com
  310. * due to checksums, however, no checksums are used in SD printing.
  311. */
  312. if (commands_in_queue == 0) stop_buffering = false;
  313. uint16_t sd_count = 0;
  314. bool card_eof = card.eof();
  315. while (commands_in_queue < BUFSIZE && !card_eof && !stop_buffering) {
  316. const int16_t n = card.get();
  317. char sd_char = (char)n;
  318. card_eof = card.eof();
  319. if (card_eof || n == -1
  320. || sd_char == '\n' || sd_char == '\r'
  321. || ((sd_char == '#' || sd_char == ':') && !sd_comment_mode)
  322. ) {
  323. if (card_eof) {
  324. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  325. card.printingHasFinished();
  326. #if ENABLED(PRINTER_EVENT_LEDS)
  327. LCD_MESSAGEPGM(MSG_INFO_COMPLETED_PRINTS);
  328. set_led_color(0, 255, 0); // Green
  329. #if HAS_RESUME_CONTINUE
  330. enqueue_and_echo_commands_P(PSTR("M0")); // end of the queue!
  331. #else
  332. safe_delay(1000);
  333. #endif
  334. set_led_color(0, 0, 0); // OFF
  335. #endif
  336. card.checkautostart(true);
  337. }
  338. else if (n == -1) {
  339. SERIAL_ERROR_START();
  340. SERIAL_ECHOLNPGM(MSG_SD_ERR_READ);
  341. }
  342. if (sd_char == '#') stop_buffering = true;
  343. sd_comment_mode = false; // for new command
  344. if (!sd_count) continue; // skip empty lines (and comment lines)
  345. command_queue[cmd_queue_index_w][sd_count] = '\0'; // terminate string
  346. sd_count = 0; // clear sd line buffer
  347. _commit_command(false);
  348. }
  349. else if (sd_count >= MAX_CMD_SIZE - 1) {
  350. /**
  351. * Keep fetching, but ignore normal characters beyond the max length
  352. * The command will be injected when EOL is reached
  353. */
  354. }
  355. else {
  356. if (sd_char == ';') sd_comment_mode = true;
  357. if (!sd_comment_mode) command_queue[cmd_queue_index_w][sd_count++] = sd_char;
  358. }
  359. }
  360. }
  361. #endif // SDSUPPORT
  362. /**
  363. * Add to the circular command queue the next command from:
  364. * - The command-injection queue (injected_commands_P)
  365. * - The active serial input (usually USB)
  366. * - The SD card file being actively printed
  367. */
  368. void get_available_commands() {
  369. // if any immediate commands remain, don't get other commands yet
  370. if (drain_injected_commands_P()) return;
  371. get_serial_commands();
  372. #if ENABLED(SDSUPPORT)
  373. get_sdcard_commands();
  374. #endif
  375. }
  376. /**
  377. * Get the next command in the queue, optionally log it to SD, then dispatch it
  378. */
  379. void advance_command_queue() {
  380. if (!commands_in_queue) return;
  381. #if ENABLED(SDSUPPORT)
  382. if (card.saving) {
  383. char* command = command_queue[cmd_queue_index_r];
  384. if (strstr_P(command, PSTR("M29"))) {
  385. // M29 closes the file
  386. card.closefile();
  387. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  388. ok_to_send();
  389. }
  390. else {
  391. // Write the string from the read buffer to SD
  392. card.write_command(command);
  393. if (card.logging)
  394. gcode.process_next_command(); // The card is saving because it's logging
  395. else
  396. ok_to_send();
  397. }
  398. }
  399. else
  400. gcode.process_next_command();
  401. #else
  402. gcode.process_next_command();
  403. #endif // SDSUPPORT
  404. // The queue may be reset by a command handler or by code invoked by idle() within a handler
  405. if (commands_in_queue) {
  406. --commands_in_queue;
  407. if (++cmd_queue_index_r >= BUFSIZE) cmd_queue_index_r = 0;
  408. }
  409. }