/* report.c - reporting and messaging methods Part of Grbl Copyright (c) 2012-2016 Sungeun K. Jeon for Gnea Research LLC Grbl 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. Grbl 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. You should have received a copy of the GNU General Public License along with Grbl. If not, see . */ /* This file functions as the primary feedback interface for Grbl. Any outgoing data, such as the protocol status messages, feedback messages, and status reports, are stored here. For the most part, these functions primarily are called from protocol.c methods. If a different style feedback is desired (i.e. JSON), then a user can change these following methods to accomodate their needs. */ #include "grbl.h" // Handles the primary confirmation protocol response for streaming interfaces and human-feedback. // For every incoming line, this method responds with an 'ok' for a successful command or an // 'error:' to indicate some error event with the line or some critical system error during // operation. Errors events can originate from the g-code parser, settings module, or asynchronously // from a critical error, such as a triggered hard limit. Interface should always monitor for these // responses. // NOTE: In silent mode, all error codes are greater than zero. // TODO: Install silent mode to return only numeric values, primarily for GUIs. void report_status_message(uint8_t status_code) { switch(status_code) { case STATUS_OK: // STATUS_OK printPgmString(PSTR("ok\r\n")); break; default: #ifdef REPORT_GUI_MODE printPgmString(PSTR("error:")); print_uint8_base10(status_code); #else printPgmString(PSTR("error: ")); switch(status_code) { case STATUS_EXPECTED_COMMAND_LETTER: printPgmString(PSTR("Expected command letter")); break; case STATUS_BAD_NUMBER_FORMAT: printPgmString(PSTR("Bad number format")); break; case STATUS_INVALID_STATEMENT: printPgmString(PSTR("Invalid statement")); break; case STATUS_NEGATIVE_VALUE: printPgmString(PSTR("Value < 0")); break; case STATUS_SETTING_DISABLED: printPgmString(PSTR("Setting disabled")); break; case STATUS_SETTING_STEP_PULSE_MIN: printPgmString(PSTR("Value < 3 usec")); break; case STATUS_SETTING_READ_FAIL: printPgmString(PSTR("EEPROM read fail. Using defaults")); break; case STATUS_IDLE_ERROR: printPgmString(PSTR("Not idle")); break; case STATUS_SYSTEM_GC_LOCK: printPgmString(PSTR("G-code lock")); break; case STATUS_SOFT_LIMIT_ERROR: printPgmString(PSTR("Homing not enabled")); break; case STATUS_OVERFLOW: printPgmString(PSTR("Line overflow")); break; #ifdef MAX_STEP_RATE_HZ case STATUS_MAX_STEP_RATE_EXCEEDED: printPgmString(PSTR("Step rate > 30kHz")); break; #endif case STATUS_CHECK_DOOR: printPgmString(PSTR("Check Door")); break; // case STATUS_LINE_LENGTH_EXCEEDED: // Supported on Grbl-Mega only. // printPgmString(PSTR("Line length exceeded")); break; case STATUS_TRAVEL_EXCEEDED: printPgmString(PSTR("Travel exceeded")); break; case STATUS_INVALID_JOG_COMMAND: printPgmString(PSTR("Invalid jog command")); break; // Common g-code parser errors. case STATUS_GCODE_UNSUPPORTED_COMMAND: printPgmString(PSTR("Unsupported command")); break; case STATUS_GCODE_MODAL_GROUP_VIOLATION: printPgmString(PSTR("Modal group violation")); break; case STATUS_GCODE_UNDEFINED_FEED_RATE: printPgmString(PSTR("Undefined feed rate")); break; default: // Remaining g-code parser errors with error codes printPgmString(PSTR("Invalid gcode ID:")); print_uint8_base10(status_code); // Print error code for user reference } #endif printPgmString(PSTR("\r\n")); } } // Prints alarm messages. void report_alarm_message(int8_t alarm_code) { #ifdef REPORT_GUI_MODE printPgmString(PSTR("ALARM:")); print_uint8_base10(alarm_code); #else printPgmString(PSTR("ALARM: ")); switch (alarm_code) { case ALARM_HARD_LIMIT_ERROR: printPgmString(PSTR("Hard limit")); break; case ALARM_SOFT_LIMIT_ERROR: printPgmString(PSTR("Soft limit")); break; case ALARM_ABORT_CYCLE: printPgmString(PSTR("Abort during cycle")); break; case ALARM_PROBE_FAIL_INITIAL: case ALARM_PROBE_FAIL_CONTACT: printPgmString(PSTR("Probe fail")); break; case ALARM_HOMING_FAIL_RESET: case ALARM_HOMING_FAIL_DOOR: case ALARM_HOMING_FAIL_PULLOFF: case ALARM_HOMING_FAIL_APPROACH: printPgmString(PSTR("Homing fail")); break; } #endif printPgmString(PSTR("\r\n")); delay_ms(500); // Force delay to ensure message clears serial write buffer. } // Prints feedback messages. This serves as a centralized method to provide additional // user feedback for things that are not of the status/alarm message protocol. These are // messages such as setup warnings, switch toggling, and how to exit alarms. // NOTE: For interfaces, messages are always placed within brackets. And if silent mode // is installed, the message number codes are less than zero. // TODO: Install silence feedback messages option in settings void report_feedback_message(uint8_t message_code) { printPgmString(PSTR("[MSG:")); switch(message_code) { case MESSAGE_CRITICAL_EVENT: printPgmString(PSTR("Reset to continue")); break; case MESSAGE_ALARM_LOCK: printPgmString(PSTR("'$H'|'$X' to unlock")); break; case MESSAGE_ALARM_UNLOCK: printPgmString(PSTR("Caution: Unlocked")); break; case MESSAGE_ENABLED: printPgmString(PSTR("Enabled")); break; case MESSAGE_DISABLED: printPgmString(PSTR("Disabled")); break; case MESSAGE_SAFETY_DOOR_AJAR: printPgmString(PSTR("Check Door")); break; case MESSAGE_CHECK_LIMITS: printPgmString(PSTR("Check Limits")); break; case MESSAGE_PROGRAM_END: printPgmString(PSTR("Pgm End")); break; case MESSAGE_RESTORE_DEFAULTS: printPgmString(PSTR("Restoring defaults")); break; case MESSAGE_SPINDLE_RESTORE: printPgmString(PSTR("Restoring spindle")); break; } printPgmString(PSTR("]\r\n")); } // Welcome message void report_init_message() { printPgmString(PSTR("\r\nGrbl " GRBL_VERSION " ['$' for help]\r\n")); } // Grbl help message void report_grbl_help() { #ifdef REPORT_GUI_MODE printPgmString(PSTR("[HLP:$$ $# $G $I $N $x=val $Nx=line $J=line $C $X $H ~ ! ? ctrl-x]\r\n")); #else printPgmString(PSTR("$$ (view Grbl settings)\r\n" "$# (view # parameters)\r\n" "$G (view parser state)\r\n" "$I (view build info)\r\n" "$N (view startup blocks)\r\n" "$x=value (save Grbl setting)\r\n" "$Nx=line (save startup block)\r\n" "$J=line (jog)\r\n" "$C (check gcode mode)\r\n" "$X (kill alarm lock)\r\n" "$H (run homing cycle)\r\n" "~ (cycle start)\r\n" "! (feed hold)\r\n" "? (current status)\r\n" "ctrl-x (reset Grbl)\r\n")); #endif } static void report_int_setting(uint8_t n, int val) { serial_write('$'); print_uint8_base10(n); serial_write('='); print_uint8_base10(val); printPgmString(PSTR("\r\n")); } static void report_float_setting(uint8_t n, float val, uint8_t decimal_places) { serial_write('$'); print_uint8_base10(n); serial_write('='); printFloat(val, decimal_places); printPgmString(PSTR("\r\n")); } // Grbl global settings print out. // NOTE: The numbering scheme here must correlate to storing in settings.c void report_grbl_settings() { // Print Grbl settings. #ifdef REPORT_GUI_MODE report_int_setting(0,settings.pulse_microseconds); report_int_setting(1,settings.stepper_idle_lock_time); report_int_setting(2,settings.step_invert_mask); report_int_setting(3,settings.dir_invert_mask); report_int_setting(4,bit_istrue(settings.flags,BITFLAG_INVERT_ST_ENABLE)); report_int_setting(5,bit_istrue(settings.flags,BITFLAG_INVERT_LIMIT_PINS)); report_int_setting(6,bit_istrue(settings.flags,BITFLAG_INVERT_PROBE_PIN)); report_int_setting(10,settings.status_report_mask); report_float_setting(11,settings.junction_deviation,N_DECIMAL_SETTINGVALUE); report_float_setting(12,settings.arc_tolerance,N_DECIMAL_SETTINGVALUE); report_int_setting(13,bit_istrue(settings.flags,BITFLAG_REPORT_INCHES)); report_int_setting(20,bit_istrue(settings.flags,BITFLAG_SOFT_LIMIT_ENABLE)); report_int_setting(21,bit_istrue(settings.flags,BITFLAG_HARD_LIMIT_ENABLE)); report_int_setting(22,bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)); report_int_setting(23,settings.homing_dir_mask); report_float_setting(24,settings.homing_feed_rate,N_DECIMAL_SETTINGVALUE); report_float_setting(25,settings.homing_seek_rate,N_DECIMAL_SETTINGVALUE); report_int_setting(26,settings.homing_debounce_delay); report_float_setting(27,settings.homing_pulloff,N_DECIMAL_SETTINGVALUE); report_float_setting(30,settings.rpm_max,N_DECIMAL_RPMVALUE); report_float_setting(31,settings.rpm_min,N_DECIMAL_RPMVALUE); #ifdef VARIABLE_SPINDLE report_int_setting(32,bit_istrue(settings.flags,BITFLAG_LASER_MODE)); #else report_int_setting(32,0); #endif // Print axis settings uint8_t idx, set_idx; uint8_t val = AXIS_SETTINGS_START_VAL; for (set_idx=0; set_idx'); printString(line); // Echo startup line to indicate execution. serial_write(':'); report_status_message(status_code); } // Prints build info line void report_build_info(char *line) { printPgmString(PSTR("[VER:" GRBL_VERSION "." GRBL_VERSION_BUILD ":")); printString(line); printPgmString(PSTR("]\r\n")); } // Prints the character string line Grbl has received from the user, which has been pre-parsed, // and has been sent into protocol_execute_line() routine to be executed by Grbl. void report_echo_line_received(char *line) { printPgmString(PSTR("[echo: ")); printString(line); printPgmString(PSTR("]\r\n")); } // Prints real-time data. This function grabs a real-time snapshot of the stepper subprogram // and the actual location of the CNC machine. Users may change the following function to their // specific needs, but the desired real-time data report must be as short as possible. This is // requires as it minimizes the computational overhead and allows grbl to keep running smoothly, // especially during g-code programs with fast, short line segments and high frequency reports (5-20Hz). void report_realtime_status() { #ifdef USE_CLASSIC_REALTIME_REPORT uint8_t idx; int32_t current_position[N_AXIS]; // Copy current state of the system position variable memcpy(current_position,sys_position,sizeof(sys_position)); float print_position[N_AXIS]; // Report current machine state switch (sys.state) { case STATE_IDLE: printPgmString(PSTR("line_number; } printInteger(ln); #endif #ifdef REPORT_REALTIME_RATE // Report realtime rate printPgmString(PSTR(",F:")); printFloat_RateValue(st_get_realtime_rate()); #endif #ifdef REPORT_ALL_PIN_STATES if (bit_istrue(settings.status_report_mask, ( BITFLAG_RT_STATUS_LIMIT_PINS| BITFLAG_RT_STATUS_PROBE_PIN | BITFLAG_RT_STATUS_CONTROL_PINS ))) { printPgmString(PSTR(",Pin:")); if (bit_istrue(settings.status_report_mask,BITFLAG_RT_STATUS_LIMIT_PINS)) { print_uint8_base2_ndigit(limits_get_state(),N_AXIS); } printPgmString(PSTR("|")); if (bit_istrue(settings.status_report_mask,BITFLAG_RT_STATUS_PROBE_PIN)) { if (probe_get_state()) { printPgmString(PSTR("1")); } else { printPgmString(PSTR("0")); } } printPgmString(PSTR("|")); if (bit_istrue(settings.status_report_mask,BITFLAG_RT_STATUS_CONTROL_PINS)) { print_uint8_base2_ndigit(system_control_get_state(),N_CONTROL_PIN); } } #else if (bit_istrue(settings.status_report_mask,BITFLAG_RT_STATUS_LIMIT_PINS)) { printPgmString(PSTR(",Lim:")); print_uint8_base2_ndigit(limits_get_state(),N_AXIS); } #endif if (bit_istrue(settings.status_report_mask,BITFLAG_RT_STATUS_OVERRIDES)) { printPgmString(PSTR(",Ov:")); print_uint8_base10(sys.f_override); serial_write(','); print_uint8_base10(sys.r_override); serial_write(','); print_uint8_base10(sys.spindle_speed_ovr); if (sys.toggle_ovr_mask) { printPgmString(PSTR("|T:")); if (sys.toggle_ovr_mask & TOGGLE_OVR_STOP_ACTIVE_MASK) { serial_write('S'); } if (sys.toggle_ovr_mask & TOGGLE_OVR_FLOOD_COOLANT) { serial_write('F'); } #ifdef ENABLE_M7 if (sys.toggle_ovr_mask & TOGGLE_OVR_MIST_COOLANT) { serial_write('M'); } #endif bit_false(sys.toggle_ovr_mask, (TOGGLE_OVR_FLOOD_COOLANT|TOGGLE_OVR_FLOOD_COOLANT)); } } printPgmString(PSTR(">\r\n")); #else uint8_t idx; int32_t current_position[N_AXIS]; // Copy current state of the system position variable memcpy(current_position,sys_position,sizeof(sys_position)); float print_position[N_AXIS]; system_convert_array_steps_to_mpos(print_position,current_position); // Report current machine state and sub-states switch (sys.state) { case STATE_IDLE: printPgmString(PSTR("line_number; if (ln > 0) { printPgmString(PSTR("|Ln:")); printInteger(ln); } } #endif #endif // Report realtime rate #ifdef REPORT_FIELD_CURRENT_RATE printPgmString(PSTR("|F:")); printFloat_RateValue(st_get_realtime_rate()); #endif #ifdef REPORT_FIELD_PIN_STATE uint8_t lim_pin_state = limits_get_state(); uint8_t ctrl_pin_state = system_control_get_state(); uint8_t prb_pin_state = probe_get_state(); if (lim_pin_state | ctrl_pin_state | prb_pin_state) { printPgmString(PSTR("|Pn:")); if (prb_pin_state) { serial_write('P'); } if (lim_pin_state) { if (bit_istrue(lim_pin_state,bit(X_AXIS))) { serial_write('X'); } if (bit_istrue(lim_pin_state,bit(Y_AXIS))) { serial_write('Y'); } if (bit_istrue(lim_pin_state,bit(Z_AXIS))) { serial_write('Z'); } } if (ctrl_pin_state) { #ifdef ENABLE_SAFETY_DOOR_INPUT_PIN if (bit_istrue(ctrl_pin_state,CONTROL_PIN_INDEX_SAFETY_DOOR)) { serial_write('D'); } #endif if (bit_istrue(ctrl_pin_state,CONTROL_PIN_INDEX_RESET)) { serial_write('R'); } if (bit_istrue(ctrl_pin_state,CONTROL_PIN_INDEX_FEED_HOLD)) { serial_write('H'); } if (bit_istrue(ctrl_pin_state,CONTROL_PIN_INDEX_CYCLE_START)) { serial_write('S'); } } } #endif #ifdef REPORT_FIELD_WORK_COORD_OFFSET if (sys.report_wco_counter++ >= REPORT_WCO_REFRESH_BUSY_COUNT) { if (sys.state & (STATE_HOMING | STATE_CYCLE | STATE_HOLD | STATE_JOG | STATE_SAFETY_DOOR)) { sys.report_wco_counter = 1; // Reset counter for slow refresh } else { sys.report_wco_counter = (REPORT_WCO_REFRESH_BUSY_COUNT-REPORT_WCO_REFRESH_IDLE_COUNT+1); } if (sys.report_ovr_counter >= REPORT_OVR_REFRESH_BUSY_COUNT) { sys.report_ovr_counter = (REPORT_OVR_REFRESH_BUSY_COUNT-1); // Set override on next report. } printPgmString(PSTR("|WCO:")); float axis_offset; uint8_t idx; for (idx=0; idx= REPORT_OVR_REFRESH_BUSY_COUNT) { if (sys.state & (STATE_HOMING | STATE_CYCLE | STATE_HOLD | STATE_JOG | STATE_SAFETY_DOOR)) { sys.report_ovr_counter = 1; // Reset counter for slow refresh } else { sys.report_ovr_counter = (REPORT_OVR_REFRESH_BUSY_COUNT-REPORT_OVR_REFRESH_IDLE_COUNT+1); } printPgmString(PSTR("|Ov:")); print_uint8_base10(sys.f_override); serial_write(','); print_uint8_base10(sys.r_override); serial_write(','); print_uint8_base10(sys.spindle_speed_ovr); if (sys.toggle_ovr_mask) { printPgmString(PSTR("|T:")); if (sys.toggle_ovr_mask & TOGGLE_OVR_STOP_ACTIVE_MASK) { serial_write('S'); } if (sys.toggle_ovr_mask & TOGGLE_OVR_FLOOD_COOLANT) { serial_write('F'); } #ifdef ENABLE_M7 if (sys.toggle_ovr_mask & TOGGLE_OVR_MIST_COOLANT) { serial_write('M'); } #endif bit_false(sys.toggle_ovr_mask, (TOGGLE_OVR_FLOOD_COOLANT|TOGGLE_OVR_FLOOD_COOLANT)); } } #endif printPgmString(PSTR(">\r\n")); #endif } #ifdef DEBUG void report_realtime_debug() { } #endif