// Initialize the config subsystem void settings_init() { if(!read_global_settings()) { report_status_message(STATUS_SETTING_READ_FAIL); settings_restore(SETTINGS_RESTORE_ALL); // Force restore all EEPROM data. report_grbl_settings(); } }
// Initialize the config subsystem void settings_init() { if(!read_global_settings()) { report_status_message(STATUS_SETTING_READ_FAIL); settings_reset(); report_grbl_settings(); } // Read all parameter data into a dummy variable. If error, reset to zero, otherwise do nothing. float coord_data[N_AXIS]; uint8_t i; for (i=0; i<=SETTING_INDEX_NCOORD; i++) { if (!settings_read_coord_data(i, coord_data)) { report_status_message(STATUS_SETTING_READ_FAIL); } } // NOTE: Startup lines are handled and called by main.c at the end of initialization. }
// Prints Grbl NGC parameters (coordinate offsets, probing) void report_ngc_parameters() { float coord_data[N_AXIS]; uint8_t coord_select, i; for (coord_select = 0; coord_select <= SETTING_INDEX_NCOORD; coord_select++) { if (!(settings_read_coord_data(coord_select,coord_data))) { report_status_message(STATUS_SETTING_READ_FAIL); return; } printPgmString(PSTR("[G")); switch (coord_select) { case 6: printPgmString(PSTR("28")); break; case 7: printPgmString(PSTR("30")); break; default: print_uint8_base10(coord_select+54); break; // G54-G59 } printPgmString(PSTR(":")); for (i=0; i<N_AXIS; i++) { printFloat_CoordValue(coord_data[i]); if (i < (N_AXIS-1)) { printPgmString(PSTR(",")); } else { printPgmString(PSTR("]\r\n")); } } } printPgmString(PSTR("[G92:")); // Print G92,G92.1 which are not persistent in memory for (i=0; i<N_AXIS; i++) { printFloat_CoordValue(gc_state.coord_offset[i]); if (i < (N_AXIS-1)) { printPgmString(PSTR(",")); } else { printPgmString(PSTR("]\r\n")); } } printPgmString(PSTR("[TLO:")); // Print tool length offset value printFloat_CoordValue(gc_state.tool_length_offset); printPgmString(PSTR("]\r\n")); report_probe_parameters(); // Print probe parameters. Not persistent in memory. }
void gc_init() { memset(&gc_state, 0, sizeof(gc_state)); // Load default G54 coordinate system. if (!(settings_read_coord_data(gc_state.modal.coord_select,gc_state.coord_system))) { report_status_message(STATUS_SETTING_READ_FAIL); } }
void gc_init() { memset(&gc, 0, sizeof(gc)); gc.feed_rate = settings.default_feed_rate; select_plane(X_AXIS, Y_AXIS, Z_AXIS); gc.absolute_mode = true; // Load default G54 coordinate system. if (!(settings_read_coord_data(gc.coord_select,gc.coord_system))) { report_status_message(STATUS_SETTING_READ_FAIL); } }
// Initialize the config subsystem void settings_init() { if(!read_global_settings()) { report_status_message(STATUS_SETTING_READ_FAIL); settings_restore(SETTINGS_RESTORE_ALL); // Force restore all EEPROM data. report_grbl_settings(); } // NOTE: Checking paramater data, startup lines, and build info string should be done here, // but it seems fairly redundant. Each of these can be manually checked and reset or restored. // Check all parameter data into a dummy variable. If error, reset to zero, otherwise do nothing. // float coord_data[N_AXIS]; // uint8_t i; // for (i=0; i<=SETTING_INDEX_NCOORD; i++) { // if (!settings_read_coord_data(i, coord_data)) { // report_status_message(STATUS_SETTING_READ_FAIL); // } // } // NOTE: Startup lines are checked and executed by protocol_main_loop at the end of initialization. }
// Prints Grbl NGC parameters (coordinate offsets, probing) void report_ngc_parameters() { float coord_data[N_AXIS]; uint8_t coord_select, i; for (coord_select = 0; coord_select <= SETTING_INDEX_NCOORD; coord_select++) { if (!(settings_read_coord_data(coord_select,coord_data))) { report_status_message(STATUS_SETTING_READ_FAIL); return; } printPgmString(PSTR("[G")); switch (coord_select) { case 0: printPgmString(PSTR("54:")); break; case 1: printPgmString(PSTR("55:")); break; case 2: printPgmString(PSTR("56:")); break; case 3: printPgmString(PSTR("57:")); break; case 4: printPgmString(PSTR("58:")); break; case 5: printPgmString(PSTR("59:")); break; case 6: printPgmString(PSTR("28:")); break; case 7: printPgmString(PSTR("30:")); break; // case 8: printPgmString(PSTR("92:")); break; // G92.2, G92.3 not supported. Hence not stored. } for (i=0; i<N_AXIS; i++) { if (bit_istrue(settings.flags,BITFLAG_REPORT_INCHES)) { printFloat(coord_data[i]*INCH_PER_MM); } else { printFloat(coord_data[i]); } if (i < (N_AXIS-1)) { printPgmString(PSTR(",")); } else { printPgmString(PSTR("]\r\n")); } } } printPgmString(PSTR("[G92:")); // Print G92,G92.1 which are not persistent in memory for (i=0; i<N_AXIS; i++) { if (bit_istrue(settings.flags,BITFLAG_REPORT_INCHES)) { printFloat(gc.coord_offset[i]*INCH_PER_MM); } else { printFloat(gc.coord_offset[i]); } if (i < (N_AXIS-1)) { printPgmString(PSTR(",")); } else { printPgmString(PSTR("]\r\n")); } } report_probe_parameters(); // Print probe parameters. Not persistent in memory. }
// Directs and executes one line of formatted input from protocol_process. While mostly // incoming streaming g-code blocks, this also executes Grbl internal commands, such as // settings, initiating the homing cycle, and toggling switch states. This differs from // the realtime command module by being susceptible to when Grbl is ready to execute the // next line during a cycle, so for switches like block delete, the switch only effects // the lines that are processed afterward, not necessarily real-time during a cycle, // since there are motions already stored in the buffer. However, this 'lag' should not // be an issue, since these commands are not typically used during a cycle. uint8_t system_execute_line(char *line) { uint8_t char_counter = 1; uint8_t helper_var = 0; // Helper variable float parameter, value; switch( line[char_counter] ) { case 0 : report_grbl_help(); break; case '$': case 'G': case 'C': case 'X': if ( line[(char_counter+1)] != 0 ) { return(STATUS_INVALID_STATEMENT); } switch( line[char_counter] ) { case '$' : // Prints Grbl settings if ( sys.state & (STATE_CYCLE | STATE_HOLD) ) { return(STATUS_IDLE_ERROR); } // Block during cycle. Takes too long to print. else { report_grbl_settings(); } break; case 'G' : // Prints gcode parser state // TODO: Move this to realtime commands for GUIs to request this data during suspend-state. report_gcode_modes(); break; case 'C' : // Set check g-code mode [IDLE/CHECK] // Perform reset when toggling off. Check g-code mode should only work if Grbl // is idle and ready, regardless of alarm locks. This is mainly to keep things // simple and consistent. if ( sys.state == STATE_CHECK_MODE ) { mc_reset(); report_feedback_message(MESSAGE_DISABLED); } else { if (sys.state) { return(STATUS_IDLE_ERROR); } // Requires no alarm mode. sys.state = STATE_CHECK_MODE; report_feedback_message(MESSAGE_ENABLED); } break; case 'X' : // Disable alarm lock [ALARM] if (sys.state == STATE_ALARM) { report_feedback_message(MESSAGE_ALARM_UNLOCK); sys.state = STATE_IDLE; // Don't run startup script. Prevents stored moves in startup from causing accidents. #ifndef DEFAULTS_TRINAMIC if (system_check_safety_door_ajar()) { // Check safety door switch before returning. bit_true(sys_rt_exec_state, EXEC_SAFETY_DOOR); protocol_execute_realtime(); // Enter safety door mode. } #endif } // Otherwise, no effect. break; // case 'J' : break; // Jogging methods // TODO: Here jogging can be placed for execution as a seperate subprogram. It does not need to be // susceptible to other realtime commands except for e-stop. The jogging function is intended to // be a basic toggle on/off with controlled acceleration and deceleration to prevent skipped // steps. The user would supply the desired feedrate, axis to move, and direction. Toggle on would // start motion and toggle off would initiate a deceleration to stop. One could 'feather' the // motion by repeatedly toggling to slow the motion to the desired location. Location data would // need to be updated real-time and supplied to the user through status queries. // More controlled exact motions can be taken care of by inputting G0 or G1 commands, which are // handled by the planner. It would be possible for the jog subprogram to insert blocks into the // block buffer without having the planner plan them. It would need to manage de/ac-celerations // on its own carefully. This approach could be effective and possibly size/memory efficient. // } // break; } break; default : // Block any system command that requires the state as IDLE/ALARM. (i.e. EEPROM, homing) if ( !(sys.state == STATE_IDLE || sys.state == STATE_ALARM) ) { return(STATUS_IDLE_ERROR); } switch( line[char_counter] ) { case '#' : // Print Grbl NGC parameters if ( line[++char_counter] != 0 ) { return(STATUS_INVALID_STATEMENT); } else { report_ngc_parameters(); } break; case 'H' : // Perform homing cycle [IDLE/ALARM] if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_HOMING; // Set system state variable // Only perform homing if Grbl is idle or lost. // TODO: Likely not required. #ifndef DEFAULTS_TRINAMIC if (system_check_safety_door_ajar()) { // Check safety door switch before homing. bit_true(sys_rt_exec_state, EXEC_SAFETY_DOOR); protocol_execute_realtime(); // Enter safety door mode. } #endif mc_homing_cycle(); if (!sys.abort) { // Execute startup scripts after successful homing. sys.state = STATE_IDLE; // Set to IDLE when complete. st_go_idle(); // Set steppers to the settings idle state before returning. system_execute_startup(line); } } else { return(STATUS_SETTING_DISABLED); } break; case 'I' : // Print or store build info. [IDLE/ALARM] if ( line[++char_counter] == 0 ) { settings_read_build_info(line); report_build_info(line); } else { // Store startup line [IDLE/ALARM] if(line[char_counter++] != '=') { return(STATUS_INVALID_STATEMENT); } helper_var = char_counter; // Set helper variable as counter to start of user info line. do { line[char_counter-helper_var] = line[char_counter]; } while (line[char_counter++] != 0); settings_store_build_info(line); } break; case 'R' : // Restore defaults [IDLE/ALARM] if (line[++char_counter] != 'S') { return(STATUS_INVALID_STATEMENT); } if (line[++char_counter] != 'T') { return(STATUS_INVALID_STATEMENT); } if (line[++char_counter] != '=') { return(STATUS_INVALID_STATEMENT); } if (line[char_counter+2] != 0) { return(STATUS_INVALID_STATEMENT); } switch (line[++char_counter]) { case '$': settings_restore(SETTINGS_RESTORE_DEFAULTS); break; case '#': settings_restore(SETTINGS_RESTORE_PARAMETERS); break; case '*': settings_restore(SETTINGS_RESTORE_ALL); break; default: return(STATUS_INVALID_STATEMENT); } report_feedback_message(MESSAGE_RESTORE_DEFAULTS); mc_reset(); // Force reset to ensure settings are initialized correctly. break; case 'N' : // Startup lines. [IDLE/ALARM] if ( line[++char_counter] == 0 ) { // Print startup lines for (helper_var=0; helper_var < N_STARTUP_LINE; helper_var++) { if (!(settings_read_startup_line(helper_var, line))) { report_status_message(STATUS_SETTING_READ_FAIL); } else { report_startup_line(helper_var,line); } } break; } else { // Store startup line [IDLE Only] Prevents motion during ALARM. if (sys.state != STATE_IDLE) { return(STATUS_IDLE_ERROR); } // Store only when idle. helper_var = true; // Set helper_var to flag storing method. // No break. Continues into default: to read remaining command characters. } default : // Storing setting methods [IDLE/ALARM] if(!read_float(line, &char_counter, ¶meter)) { return(STATUS_BAD_NUMBER_FORMAT); } if(line[char_counter++] != '=') { return(STATUS_INVALID_STATEMENT); } if (helper_var) { // Store startup line // Prepare sending gcode block to gcode parser by shifting all characters helper_var = char_counter; // Set helper variable as counter to start of gcode block do { line[char_counter-helper_var] = line[char_counter]; } while (line[char_counter++] != 0); // Execute gcode block to ensure block is valid. helper_var = gc_execute_line(line); // Set helper_var to returned status code. if (helper_var) { return(helper_var); } else { helper_var = trunc(parameter); // Set helper_var to int value of parameter settings_store_startup_line(helper_var,line); } } else { // Store global setting. if(!read_float(line, &char_counter, &value)) { return(STATUS_BAD_NUMBER_FORMAT); } if((line[char_counter] != 0) || (parameter > 255)) { return(STATUS_INVALID_STATEMENT); } return(settings_store_global_setting((uint8_t)parameter, value)); } } } return(STATUS_OK); // If '$' command makes it to here, then everything's ok. }
// Process and report status one line of incoming serial data. Performs an initial filtering // by removing spaces and comments and capitalizing all letters. void protocol_process() { uint8_t c; while((c = serial_read()) != SERIAL_NO_DATA) { if ((c == '\n') || (c == '\r')) { // End of line reached // Runtime command check point before executing line. Prevent any furthur line executions. // NOTE: If there is no line, this function should quickly return to the main program when // the buffer empties of non-executable data. protocol_execute_runtime(); if (sys.abort) { return; } // Bail to main program upon system abort if (char_counter > 0) {// Line is complete. Then execute! line[char_counter] = 0; uint8_t crc = 0; uint8_t i; for(i = 0; i < char_counter-2; i++) { if (line[i] == '*') { if (atoi(line[i+1]) != crc){ report_status_message(STATUS_CRC_ERROR); protocol_reset_line_buffer(); } else line[i] = 0; } crc ^= line[i]; } report_status_message(protocol_execute_line(line)); } else { // Empty or comment line. Skip block. report_status_message(STATUS_OK); // Send status message for syncing purposes. } protocol_reset_line_buffer(); } else { if (iscomment) { // Throw away all comment characters if (c == ')') { // End of comment. Resume line. iscomment = false; } } else { if (c <= ' ') { // Throw away whitepace and control characters } else if (c == '/') { // Block delete not supported. Ignore character. } else if (c == '(') { // Enable comments flag and ignore all characters until ')' or EOL. iscomment = true; } else if (char_counter >= LINE_BUFFER_SIZE-1) { // Report line buffer overflow and reset report_status_message(STATUS_OVERFLOW); protocol_reset_line_buffer(); } else if (c >= 'a' && c <= 'z') { // Upcase lowercase line[char_counter++] = c-'a'+'A'; } else { line[char_counter++] = c; } } } } }
// Directs and executes one line of formatted input from protocol_process. While mostly // incoming streaming g-code blocks, this also executes Grbl internal commands, such as // settings, initiating the homing cycle, and toggling switch states. This differs from // the runtime command module by being susceptible to when Grbl is ready to execute the // next line during a cycle, so for switches like block delete, the switch only effects // the lines that are processed afterward, not necessarily real-time during a cycle, // since there are motions already stored in the buffer. However, this 'lag' should not // be an issue, since these commands are not typically used during a cycle. uint8_t protocol_execute_line(char *line) { // Grbl internal command and parameter lines are of the form '$4=374.3' or '$' for help if(line[0] == '$') { uint8_t char_counter = 1; uint8_t helper_var = 0; // Helper variable float parameter, value; switch( line[char_counter] ) { case 0 : report_grbl_help(); break; case '$' : // Prints Grbl settings if ( line[++char_counter] != 0 ) { return(STATUS_UNSUPPORTED_STATEMENT); } else { report_grbl_settings(); } break; case '#' : // Print gcode parameters if ( line[++char_counter] != 0 ) { return(STATUS_UNSUPPORTED_STATEMENT); } else { report_gcode_parameters(); } break; case 'G' : // Prints gcode parser state if ( line[++char_counter] != 0 ) { return(STATUS_UNSUPPORTED_STATEMENT); } else { report_gcode_modes(); } break; case 'C' : // Set check g-code mode if ( line[++char_counter] != 0 ) { return(STATUS_UNSUPPORTED_STATEMENT); } // Perform reset when toggling off. Check g-code mode should only work if Grbl // is idle and ready, regardless of alarm locks. This is mainly to keep things // simple and consistent. if ( sys.state == STATE_CHECK_MODE ) { mc_reset(); report_feedback_message(MESSAGE_DISABLED); } else { if (sys.state) { return(STATUS_IDLE_ERROR); } sys.state = STATE_CHECK_MODE; report_feedback_message(MESSAGE_ENABLED); } break; case 'X' : // Disable alarm lock if ( line[++char_counter] != 0 ) { return(STATUS_UNSUPPORTED_STATEMENT); } if (sys.state == STATE_ALARM) { report_feedback_message(MESSAGE_ALARM_UNLOCK); sys.state = STATE_IDLE; // Don't run startup script. Prevents stored moves in startup from causing accidents. } break; case 'H' : // Perform homing cycle if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { // Only perform homing if Grbl is idle or lost. if ( sys.state==STATE_IDLE || sys.state==STATE_ALARM ) { mc_go_home(); if (!sys.abort) { protocol_execute_startup(); } // Execute startup scripts after successful homing. } else { return(STATUS_IDLE_ERROR); } } else { return(STATUS_SETTING_DISABLED); } break; // case 'J' : break; // Jogging methods // TODO: Here jogging can be placed for execution as a seperate subprogram. It does not need to be // susceptible to other runtime commands except for e-stop. The jogging function is intended to // be a basic toggle on/off with controlled acceleration and deceleration to prevent skipped // steps. The user would supply the desired feedrate, axis to move, and direction. Toggle on would // start motion and toggle off would initiate a deceleration to stop. One could 'feather' the // motion by repeatedly toggling to slow the motion to the desired location. Location data would // need to be updated real-time and supplied to the user through status queries. // More controlled exact motions can be taken care of by inputting G0 or G1 commands, which are // handled by the planner. It would be possible for the jog subprogram to insert blocks into the // block buffer without having the planner plan them. It would need to manage de/ac-celerations // on its own carefully. This approach could be effective and possibly size/memory efficient. case 'N' : // Startup lines. if ( line[++char_counter] == 0 ) { // Print startup lines for (helper_var=0; helper_var < N_STARTUP_LINE; helper_var++) { if (!(settings_read_startup_line(helper_var, line))) { report_status_message(STATUS_SETTING_READ_FAIL); } else { report_startup_line(helper_var,line); } } break; } else { // Store startup line helper_var = true; // Set helper_var to flag storing method. // No break. Continues into default: to read remaining command characters. } default : // Storing setting methods if(!read_float(line, &char_counter, ¶meter)) { return(STATUS_BAD_NUMBER_FORMAT); } if(line[char_counter++] != '=') { return(STATUS_UNSUPPORTED_STATEMENT); } if (helper_var) { // Store startup line // Prepare sending gcode block to gcode parser by shifting all characters helper_var = char_counter; // Set helper variable as counter to start of gcode block do { line[char_counter-helper_var] = line[char_counter]; } while (line[char_counter++] != 0); // Execute gcode block to ensure block is valid. helper_var = gc_execute_line(line); // Set helper_var to returned status code. if (helper_var) { return(helper_var); } else { helper_var = trunc(parameter); // Set helper_var to int value of parameter settings_store_startup_line(helper_var,line); } } else { // Store global setting. if(!read_float(line, &char_counter, &value)) { return(STATUS_BAD_NUMBER_FORMAT); } if(line[char_counter] != 0) { return(STATUS_UNSUPPORTED_STATEMENT); } return(settings_store_global_setting(parameter, value)); } } return(STATUS_OK); // If '$' command makes it to here, then everything's ok. } else { return(gc_execute_line(line)); // Everything else is gcode } }
/* GRBL PRIMARY LOOP: */ void protocol_main_loop() { // ------------------------------------------------------------ // Complete initialization procedures upon a power-up or reset. // ------------------------------------------------------------ // Print welcome message report_init_message(); // Check for and report alarm state after a reset, error, or an initial power up. if (sys.state == STATE_ALARM) { report_feedback_message(MESSAGE_ALARM_LOCK); } else { // All systems go! But first check for safety door. #ifndef DEFAULTS_TRINAMIC if (system_check_safety_door_ajar()) { bit_true(sys_rt_exec_state, EXEC_SAFETY_DOOR); protocol_execute_realtime(); // Enter safety door mode. Should return as IDLE state. } else { sys.state = STATE_IDLE; // Set system to ready. Clear all state flags. } #endif system_execute_startup(line); // Execute startup script. } // --------------------------------------------------------------------------------- // Primary loop! Upon a system abort, this exits back to main() to reset the system. // --------------------------------------------------------------------------------- uint8_t comment = COMMENT_NONE; uint8_t char_counter = 0; uint8_t c; for (;;) { // Process one line of incoming serial data, as the data becomes available. Performs an // initial filtering by removing spaces and comments and capitalizing all letters. // NOTE: While comment, spaces, and block delete(if supported) handling should technically // be done in the g-code parser, doing it here helps compress the incoming data into Grbl's // line buffer, which is limited in size. The g-code standard actually states a line can't // exceed 256 characters, but the Arduino Uno does not have the memory space for this. // With a better processor, it would be very easy to pull this initial parsing out as a // seperate task to be shared by the g-code parser and Grbl's system commands. while((c = serial_read()) != SERIAL_NO_DATA) { if ((c == '\n') || (c == '\r')) { // End of line reached line[char_counter] = 0; // Set string termination character. protocol_execute_line(line); // Line is complete. Execute it! comment = COMMENT_NONE; char_counter = 0; } else { if (comment != COMMENT_NONE) { // Throw away all comment characters if (c == ')') { // End of comment. Resume line. But, not if semicolon type comment. if (comment == COMMENT_TYPE_PARENTHESES) { comment = COMMENT_NONE; } } } else { if (c <= ' ') { // Throw away whitepace and control characters } else if (c == '/') { // Block delete NOT SUPPORTED. Ignore character. // NOTE: If supported, would simply need to check the system if block delete is enabled. } else if (c == '(') { // Enable comments flag and ignore all characters until ')' or EOL. // NOTE: This doesn't follow the NIST definition exactly, but is good enough for now. // In the future, we could simply remove the items within the comments, but retain the // comment control characters, so that the g-code parser can error-check it. comment = COMMENT_TYPE_PARENTHESES; } else if (c == ';') { // NOTE: ';' comment to EOL is a LinuxCNC definition. Not NIST. comment = COMMENT_TYPE_SEMICOLON; // TODO: Install '%' feature // } else if (c == '%') { // Program start-end percent sign NOT SUPPORTED. // NOTE: This maybe installed to tell Grbl when a program is running vs manual input, // where, during a program, the system auto-cycle start will continue to execute // everything until the next '%' sign. This will help fix resuming issues with certain // functions that empty the planner buffer to execute its task on-time. } else if (char_counter >= (LINE_BUFFER_SIZE-1)) { // Detect line buffer overflow. Report error and reset line buffer. report_status_message(STATUS_OVERFLOW); comment = COMMENT_NONE; char_counter = 0; } else if (c >= 'a' && c <= 'z') { // Upcase lowercase line[char_counter++] = c-'a'+'A'; } else { line[char_counter++] = c; } } } } // If there are no more characters in the serial read buffer to be processed and executed, // this indicates that g-code streaming has either filled the planner buffer or has // completed. In either case, auto-cycle start, if enabled, any queued moves. protocol_auto_cycle_start(); protocol_execute_realtime(); // Runtime command check point. if (sys.abort) { return; } // Bail to main() program loop to reset system. } return; /* Never reached */ }