From bf37ab7e7be6b6f44a86bf03c223113f46facc7a Mon Sep 17 00:00:00 2001 From: Sonny Jeon Date: Sat, 17 Jan 2015 08:12:37 -0700 Subject: [PATCH] Fully configurable pins for NO or NC switches. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - All pins, which include limits, control command, and probe pins, can now all be configured to trigger as active-low or active-high and whether the pin has its internal pull-up resistor enabled. This should allow for just about all types of NO and NC switch configurations. - The probe pin invert setting hasn’t been added to the Grbl settings, like the others, and will have to wait until v1.0. But for now, it’s available as a compile-time option in config.h. - Fixed a variable spindle bug. --- config.h | 21 ++++++++++++++++++++ cpu_map.h | 50 +++++++++++++++++++++++------------------------ defaults.h | 2 +- limits.c | 8 ++++---- main.c | 2 +- planner.c | 2 +- probe.c | 6 +++--- settings.h | 2 +- spindle_control.c | 2 +- system.c | 38 ++++++++++++++++++++++------------- 10 files changed, 82 insertions(+), 51 deletions(-) diff --git a/config.h b/config.h index 60d240b..9a55119 100644 --- a/config.h +++ b/config.h @@ -143,6 +143,11 @@ // have the same steps per mm internally. // #define COREXY // Default disabled. Uncomment to enable. +// Inverts pin logic of the control command pins. This essentially means when this option is enabled +// you can use normally-closed switches, rather than the default normally-open switches. +// NOTE: Will eventually be added to Grbl settings in v1.0. +// #define INVERT_CONTROL_PIN // Default disabled. Uncomment to enable. + // --------------------------------------------------------------------------------------- // ADVANCED CONFIGURATION OPTIONS: @@ -162,6 +167,22 @@ // step smoothing. See stepper.c for more details on the AMASS system works. #define ADAPTIVE_MULTI_AXIS_STEP_SMOOTHING // Default enabled. Comment to disable. +// By default, Grbl sets all input pins to normal-high operation with their internal pull-up resistors +// enabled. This simplifies the wiring for users by requiring only a switch connected to ground, +// although its recommended that users take the extra step of wiring in low-pass filter to reduce +// electrical noise detected by the pin. If the user inverts the pin in Grbl settings, this just flips +// which high or low reading indicates an active signal. In normal operation, this means the user +// needs to connect a normal-open switch, but if inverted, this means the user should connect a +// normal-closed switch. +// The following options disable the internal pull-up resistors, sets the pins to a normal-low +// operation, and switches much be now connect to Vcc instead of ground. This also flips the meaning +// of the invert pin Grbl setting, where an inverted setting now means the user should connect a +// normal-open switch and vice versa. +// WARNING: When the pull-ups are disabled, this requires additional wiring with pull-down resistors! +//#define DISABLE_LIMIT_PIN_PULL_UP +//#define DISABLE_PROBE_PIN_PULL_UP +//#define DISABLE_CONTROL_PIN_PULL_UP + // Sets which axis the tool length offset is applied. Assumes the spindle is always parallel with // the selected axis with the tool oriented toward the negative direction. In other words, a positive // tool length offset value is subtracted from the current location. diff --git a/cpu_map.h b/cpu_map.h index 3e46c30..4bdb61a 100644 --- a/cpu_map.h +++ b/cpu_map.h @@ -60,7 +60,7 @@ #define STEPPERS_DISABLE_MASK (1< 0); // The active cycle axes should now be homed and machine limits have been located. By - // default, grbl defines machine space as all negative, as do most CNCs. Since limit switches + // default, Grbl defines machine space as all negative, as do most CNCs. Since limit switches // can be on either side of an axes, check and set axes machine zero appropriately. Also, // set up pull-off maneuver from axes limit switches that have been homed. This provides // some initial clearance off the switches and should also help prevent them from falsely diff --git a/main.c b/main.c index cd83478..bdda1e7 100644 --- a/main.c +++ b/main.c @@ -47,7 +47,7 @@ int main(void) { // Initialize system upon power-up. serial_init(); // Setup serial baud rate and interrupts - settings_init(); // Load grbl settings from EEPROM + settings_init(); // Load Grbl settings from EEPROM stepper_init(); // Configure stepper pins and interrupt timers system_init(); // Configure pinout pins and pin-change interrupt diff --git a/planner.c b/planner.c index cc41535..c0b1742 100644 --- a/planner.c +++ b/planner.c @@ -371,7 +371,7 @@ uint8_t plan_check_full_buffer() colinear with the circle center. The circular segment joining the two paths represents the path of centripetal acceleration. Solve for max velocity based on max acceleration about the radius of the circle, defined indirectly by junction deviation. This may be also viewed as - path width or max_jerk in the previous grbl version. This approach does not actually deviate + path width or max_jerk in the previous Grbl version. This approach does not actually deviate from path, but used as a robust way to compute cornering speeds, as it takes into account the nonlinearities of both the junction angle and junction velocity. diff --git a/probe.c b/probe.c index e7063b0..5d0224f 100644 --- a/probe.c +++ b/probe.c @@ -30,11 +30,11 @@ uint8_t probe_invert_mask; void probe_init() { PROBE_DDR &= ~(PROBE_MASK); // Configure as input pins - if (bit_istrue(settings.flags,BITFLAG_INVERT_PROBE_PIN)) { + #ifdef DISABLE_PROBE_PIN_PULL_UP PROBE_PORT &= ~(PROBE_MASK); // Normal low operation. Requires external pull-down. - } else { + #else PROBE_PORT |= PROBE_MASK; // Enable internal pull-up resistors. Normal high operation. - } + #endif // probe_configure_invert_mask(false); // Initialize invert mask. Not required. Updated when in-use. } diff --git a/settings.h b/settings.h index c201ad7..426ea40 100644 --- a/settings.h +++ b/settings.h @@ -29,7 +29,7 @@ #define GRBL_VERSION "0.9h" -#define GRBL_VERSION_BUILD "20150114" +#define GRBL_VERSION_BUILD "20150117" // 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/spindle_control.c b/spindle_control.c index 84b8612..b928ada 100644 --- a/spindle_control.c +++ b/spindle_control.c @@ -113,7 +113,7 @@ void spindle_set_state(uint8_t state, float rpm) void spindle_run(uint8_t state, float rpm) { - if (sys.state != STATE_CHECK_MODE) { return; } + if (sys.state == STATE_CHECK_MODE) { return; } protocol_buffer_synchronize(); // Empty planner buffer to ensure spindle is set when programmed. spindle_set_state(state, rpm); } diff --git a/system.c b/system.c index 046b72b..9c42845 100644 --- a/system.c +++ b/system.c @@ -30,10 +30,14 @@ void system_init() { - PINOUT_DDR &= ~(PINOUT_MASK); // Configure as input pins - PINOUT_PORT |= PINOUT_MASK; // Enable internal pull-up resistors. Normal high operation. - PINOUT_PCMSK |= PINOUT_MASK; // Enable specific pins of the Pin Change Interrupt - PCICR |= (1 << PINOUT_INT); // Enable Pin Change Interrupt + CONTROL_DDR &= ~(CONTROL_MASK); // Configure as input pins + #ifdef DISABLE_CONTROL_PIN_PULL_UP + CONTROL_PORT &= ~(CONTROL_MASK); // Normal low operation. Requires external pull-down. + #else + CONTROL_PORT |= CONTROL_MASK; // Enable internal pull-up resistors. Normal high operation. + #endif + CONTROL_PCMSK |= CONTROL_MASK; // Enable specific pins of the Pin Change Interrupt + PCICR |= (1 << CONTROL_INT); // Enable Pin Change Interrupt } @@ -41,15 +45,19 @@ void system_init() // only the realtime command execute variable to have the main program execute these when // its ready. This works exactly like the character-based realtime commands when picked off // directly from the incoming serial data stream. -ISR(PINOUT_INT_vect) +ISR(CONTROL_INT_vect) { - // Enter only if any pinout pin is actively low. - if ((PINOUT_PIN & PINOUT_MASK) ^ PINOUT_MASK) { - if (bit_isfalse(PINOUT_PIN,bit(PIN_RESET))) { + uint8_t pin = (CONTROL_PIN & CONTROL_MASK); + #ifndef INVERT_CONTROL_PIN + pin ^= CONTROL_MASK; + #endif + // Enter only if any CONTROL pin is detected as active. + if (pin) { + if (bit_istrue(pin,bit(RESET_BIT))) { mc_reset(); - } else if (bit_isfalse(PINOUT_PIN,bit(PIN_FEED_HOLD))) { + } else if (bit_istrue(pin,bit(FEED_HOLD_BIT))) { bit_true(sys.rt_exec_state, EXEC_FEED_HOLD); - } else if (bit_isfalse(PINOUT_PIN,bit(PIN_CYCLE_START))) { + } else if (bit_istrue(pin,bit(CYCLE_START_BIT))) { bit_true(sys.rt_exec_state, EXEC_CYCLE_START); } } @@ -208,15 +216,17 @@ uint8_t system_execute_line(char *line) float system_convert_axis_steps_to_mpos(int32_t *steps, uint8_t idx) { + float pos; #ifdef COREXY if (idx==A_MOTOR) { - return((0.5*(steps[A_MOTOR] + steps[B_MOTOR]))/settings.steps_per_mm[idx]); - } else if (idx==B_MOTOR) { - return((0.5*(steps[A_MOTOR] - steps[B_MOTOR]))/settings.steps_per_mm[idx]); + pos = 0.5*((steps[A_MOTOR] + steps[B_MOTOR])/settings.steps_per_mm[idx]); + } else { // (idx==B_MOTOR) + pos = 0.5*((steps[A_MOTOR] - steps[B_MOTOR])/settings.steps_per_mm[idx]); } #else - return((float)steps[idx]/settings.steps_per_mm[idx]); + pos = steps[idx]/settings.steps_per_mm[idx]; #endif + return(pos); }