From 76ab1b6a4282ec09df7b2c1dd534b83e61b1251c Mon Sep 17 00:00:00 2001 From: Sonny Jeon Date: Fri, 28 Feb 2014 22:03:26 -0700 Subject: [PATCH] G38.2 probe feature rough draft installed. Working but needs testing. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - G38.2 straight probe now supported. Rough draft. May be tweaked more as testing ramps up. - G38.2 requires at least one axis word. Multiple axis words work too. When commanded, the probe cycle will move at the last ‘F’ feed rate specified in a straight line. - During a probe cycle: If the probe pin goes low (normal high), Grbl will record that immediate position and engage a feed hold. Meaning that the CNC machine will move a little past the probe switch point, so keep federates low to stop sooner. Once stopped, Grbl will issue a move to go back to the recorded probe trigger point. - During a probe cycle: If the probe switch does not engage by the time the machine has traveled to its target coordinates, Grbl will issue an ALARM and the user will be forced to reset Grbl. (Currently G38.3 probe without error isn’t supported, but would be easy to implement later.) - After a successful probe, Grbl will send a feedback message containing the recorded probe coordinates in the machine coordinate system. This is as the g-code standard on probe parameters specifies. - The recorded probe parameters are retained in Grbl memory and can be viewed with the ‘$#’ print parameters command. Upon a power-cycle, not a soft-reset, Grbl will re-zero these values. - Moved ‘$#’ command to require IDLE or ALARM mode, because it accesses EEPROM to fetch the coordinate system offsets. - Updated the Grbl version to v0.9d. - The probe cycle is subject to change upon testing or user-feedback. --- Makefile | 2 +- cpu_map.h | 11 +++++- gcode.c | 44 ++++++++-------------- gcode.h | 11 +++--- limits.c | 6 +-- main.c | 2 + motion_control.c | 98 ++++++++++++------------------------------------ motion_control.h | 6 +-- probe.c | 45 ++++++++++++++++++++++ probe.h | 36 ++++++++++++++++++ protocol.c | 6 ++- report.c | 84 ++++++++++++++--------------------------- report.h | 12 +++--- settings.h | 4 +- stepper.c | 11 +++--- system.c | 17 +++------ system.h | 10 +---- 17 files changed, 195 insertions(+), 210 deletions(-) create mode 100644 probe.c create mode 100644 probe.h diff --git a/Makefile b/Makefile index ea1ea61..ab8c9ec 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ CLOCK = 16000000 PROGRAMMER ?= -c avrisp2 -P usb OBJECTS = main.o motion_control.o gcode.o spindle_control.o coolant_control.o serial.o \ protocol.o stepper.o eeprom.o settings.o planner.o nuts_bolts.o limits.o \ - print.o report.o system.o + print.o probe.o report.o system.o # FUSES = -U hfuse:w:0xd9:m -U lfuse:w:0x24:m FUSES = -U hfuse:w:0xd2:m -U lfuse:w:0xff:m # update that line with this when programmer is back up: diff --git a/cpu_map.h b/cpu_map.h index 0538bbc..060135a 100644 --- a/cpu_map.h +++ b/cpu_map.h @@ -107,11 +107,18 @@ #define PIN_RESET 0 // Uno Analog Pin 0 #define PIN_FEED_HOLD 1 // Uno Analog Pin 1 #define PIN_CYCLE_START 2 // Uno Analog Pin 2 - #define PIN_PROBE 5 // Uno Analog Pin 5 #define PINOUT_INT PCIE1 // Pin change interrupt enable pin #define PINOUT_INT_vect PCINT1_vect #define PINOUT_PCMSK PCMSK1 // Pin change interrupt register - #define PINOUT_MASK ((1<. +*/ + +#include "system.h" +#include "probe.h" + + +// Probe pin initialization routine. +void probe_init() +{ + PROBE_DDR &= ~(PROBE_MASK); // Configure as input pins + PROBE_PORT |= PROBE_MASK; // Enable internal pull-up resistors. Normal high operation. +} + + +// Monitors probe pin state and records the system position when detected. Called by the +// stepper ISR per ISR tick. +// NOTE: This function must be extremely efficient as to not bog down the stepper ISR. +void probe_state_monitor() +{ + if (sys.probe_state == PROBE_ACTIVE) { + if (!(PROBE_PIN & PROBE_MASK)) { + sys.probe_state = PROBE_OFF; + memcpy(sys.probe_position, sys.position, sizeof(float)*N_AXIS); + sys.execute |= EXEC_FEED_HOLD; + } + } +} diff --git a/probe.h b/probe.h new file mode 100644 index 0000000..e5aef86 --- /dev/null +++ b/probe.h @@ -0,0 +1,36 @@ +/* + probe.h - code pertaining to probing methods + Part of Grbl + + Copyright (c) 2014 Sungeun K. Jeon + + 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 . +*/ + +#ifndef probe_h +#define probe_h + +// Values that define the probing state machine. +#define PROBE_OFF 0 // No probing. (Must be zero.) +#define PROBE_ACTIVE 1 // Actively watching the input pin. + + +// Probe pin initialization routine. +void probe_init(); + +// Monitors probe pin state and records the system position when detected. Called by the +// stepper ISR per ISR tick. +void probe_state_monitor(); + +#endif diff --git a/protocol.c b/protocol.c index c8406d0..2bf7bcd 100644 --- a/protocol.c +++ b/protocol.c @@ -173,9 +173,11 @@ void protocol_execute_runtime() if (rt_exec & (EXEC_ALARM | EXEC_CRIT_EVENT)) { sys.state = STATE_ALARM; // Set system alarm state - // Critical event. Only hard/soft limit errors currently qualify. + // Critical events. Hard/soft limit events identified by both critical event and alarm exec + // flags. Probe fail is identified by the critical event exec flag only. if (rt_exec & EXEC_CRIT_EVENT) { - report_alarm_message(ALARM_LIMIT_ERROR); + if (rt_exec & EXEC_ALARM) { report_alarm_message(ALARM_LIMIT_ERROR); } + else { report_alarm_message(ALARM_PROBE_FAIL); } report_feedback_message(MESSAGE_CRITICAL_EVENT); bit_false(sys.execute,EXEC_RESET); // Disable any existing reset do { diff --git a/report.c b/report.c index bcecfc8..e766ee5 100644 --- a/report.c +++ b/report.c @@ -79,8 +79,6 @@ void report_status_message(uint8_t status_code) printPgmString(PSTR("Homing not enabled")); break; case STATUS_OVERFLOW: printPgmString(PSTR("Line overflow")); break; - case STATUS_PROBE_ERROR: - printPgmString(PSTR("Probe error")); break; } printPgmString(PSTR("\r\n")); } @@ -95,8 +93,10 @@ void report_alarm_message(int8_t alarm_code) printPgmString(PSTR("Hard/soft limit")); break; case ALARM_ABORT_CYCLE: printPgmString(PSTR("Abort during cycle")); break; + case ALARM_PROBE_FAIL: + printPgmString(PSTR("Probe fail")); break; } - printPgmString(PSTR(". MPos?\r\n")); + printPgmString(PSTR("\r\n")); delay_ms(500); // Force delay to ensure message clears serial write buffer. } @@ -190,8 +190,28 @@ void report_grbl_settings() { } -// Prints gcode coordinate offset parameters -void report_gcode_parameters() +// Prints current probe parameters. Upon a probe command, these parameters are updated upon a +// successful probe or upon a failed probe with the G38.3 without errors command (if supported). +// These values are retained until Grbl is power-cycled, whereby they will be re-zeroed. +void report_probe_parameters() +{ + uint8_t i; + float print_position[N_AXIS]; + + // Report in terms of machine position. + printPgmString(PSTR("[Probe:")); + for (i=0; i< N_AXIS; i++) { + print_position[i] = sys.probe_position[i]/settings.steps_per_mm[i]; + if (bit_istrue(settings.flags,BITFLAG_REPORT_INCHES)) { print_position[i] *= INCH_PER_MM; } + printFloat(print_position[i]); + if (i < (N_AXIS-1)) { printPgmString(PSTR(",")); } + } + printPgmString(PSTR("]\r\n")); +} + + +// Prints Grbl NGC parameters (coordinate offsets, probing) +void report_ngc_parameters() { float coord_data[N_AXIS]; uint8_t coord_select, i; @@ -226,6 +246,7 @@ void report_gcode_parameters() if (i < (N_AXIS-1)) { printPgmString(PSTR(",")); } else { printPgmString(PSTR("]\r\n")); } } + report_probe_parameters(); // Print probe parameters. Not persistent in memory. } @@ -353,7 +374,7 @@ void report_realtime_status() if (i < (N_AXIS-1)) { printPgmString(PSTR(",")); } } -#ifdef USE_LINE_NUMBERS + #ifdef USE_LINE_NUMBERS // Report current line number printPgmString(PSTR(",Ln:")); int32_t ln=0; @@ -362,56 +383,7 @@ void report_realtime_status() ln = pb->line_number; } printInteger(ln); -#endif - - 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. It is kept separate from the "normal" report_realtime_status() to allow customization. -void report_realtime_status_probe() -{ - // **Under construction** Bare-bones status report. Provides real-time machine position relative to - // the system power on location (0,0,0) and work coordinate position (G54 and G92 applied). - uint8_t i; - 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]; - - printPgmString(PSTR("line_number; - } - printInteger(ln); -#endif + #endif printPgmString(PSTR(">\r\n")); } diff --git a/report.h b/report.h index f05bc42..91d7797 100644 --- a/report.h +++ b/report.h @@ -36,11 +36,11 @@ #define STATUS_ALARM_LOCK 12 #define STATUS_SOFT_LIMIT_ERROR 13 #define STATUS_OVERFLOW 14 -#define STATUS_PROBE_ERROR 15 // Define Grbl alarm codes. Less than zero to distinguish alarm error from status error. #define ALARM_LIMIT_ERROR -1 #define ALARM_ABORT_CYCLE -2 +#define ALARM_PROBE_FAIL -3 // Define Grbl feedback message codes. #define MESSAGE_CRITICAL_EVENT 1 @@ -70,13 +70,11 @@ void report_grbl_settings(); // Prints realtime status report void report_realtime_status(); -// Prints realtime position status report at the end of a probe cycle -// This is in leiu of saving the probe position to internal variables like an -// EMC machine -void report_realtime_status_probe(); +// Prints recorded probe position +void report_probe_parameters(); -// Prints Grbl persistent coordinate parameters -void report_gcode_parameters(); +// Prints Grbl NGC parameters (coordinate offsets, probe) +void report_ngc_parameters(); // Prints current g-code parser mode state void report_gcode_modes(); diff --git a/settings.h b/settings.h index 3551ec2..62dfcfc 100644 --- a/settings.h +++ b/settings.h @@ -25,8 +25,8 @@ #include "system.h" -#define GRBL_VERSION "0.9c" -#define GRBL_VERSION_BUILD "20140215" +#define GRBL_VERSION "0.9d" +#define GRBL_VERSION_BUILD "20140228" // Version of the EEPROM data. Will be used to migrate existing data from older versions of Grbl // when firmware is upgraded. Always stored in byte 0 of eeprom diff --git a/stepper.c b/stepper.c index 370d798..58ec9c1 100644 --- a/stepper.c +++ b/stepper.c @@ -24,6 +24,7 @@ #include "stepper.h" #include "settings.h" #include "planner.h" +#include "probe.h" // Some useful constants. @@ -282,12 +283,6 @@ ISR(TIMER1_COMPA_vect) { // SPINDLE_ENABLE_PORT ^= 1<