void jogging() // Tests jog port pins and moves steppers { uint8_t jog_bits, jog_bits_old, out_bits0, jog_exit, last_sys_state; uint8_t i, limit_state, spindle_bits; uint32_t dest_step_rate, step_rate, step_delay; // Step delay after pulse float work_position, mm_per_step, mm_per_step_z; switch (sys.state) { case STATE_CYCLE: case STATE_HOMING: case STATE_INIT: LED_PORT |= (1<<LED_ERROR_BIT); return; case STATE_ALARM: case STATE_QUEUED: LED_PORT &= ~(1<<LED_ERROR_BIT); break; default: LED_PORT |= (1<<LED_ERROR_BIT); } last_sys_state = sys.state; spindle_bits = (~PINOUT_PIN) & (1<<PIN_SPIN_TOGGLE); // active low if (spindle_bits) { if (spindle_status) { // gc.spindle_direction = 0; spindle_run(0); } else { // gc.spindle_direction = 1; // also update gcode spindle status spindle_run(1); } spindle_btn_release(); delay_ms(20); } jog_bits = (~JOGSW_PIN) & JOGSW_MASK; // active low if (!jog_bits) { return; } // nothing pressed // At least one jog/joystick switch is active if (jog_bits & (1<<JOG_ZERO)) { // Zero-Button gedrückt jog_btn_release(); sys.state = last_sys_state; if (bit_isfalse(PINOUT_PIN,bit(PIN_RESET))) { // RESET und zusätzlich ZERO gedrückt: Homing 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 { // Zero current work position sys_sync_current_position(); // gc.coord_system[i] Current work coordinate system (G54+). Stores offset from absolute machine // position in mm. Loaded from EEPROM when called. // gc.coord_offset[i] Retains the G92 coordinate offset (work coordinates) relative to // machine zero in mm. Non-persistent. Cleared upon reset and boot. for (i=0; i<=2; i++) { // Axes indices are consistent, so loop may be used. gc.coord_offset[i] = gc.position[i] - gc.coord_system[i]; } // Z-Achse um bestimmten Betrag zurückziehen mc_line(gc.position[X_AXIS], gc.position[Y_AXIS], gc.position[Z_AXIS] + (settings.z_zero_pulloff * settings.z_scale), settings.default_seek_rate, false); plan_synchronize(); // Make sure the motion completes gc.position[Z_AXIS] = gc.position[Z_AXIS] - (settings.z_zero_gauge * settings.z_scale); gc.coord_offset[Z_AXIS] = gc.position[Z_AXIS] - gc.coord_system[Z_AXIS]; // The gcode parser position circumvented by the pull-off maneuver, so sync position vectors. // Sets the planner position vector to current steps. Called by the system abort routine. // Sets g-code parser position in mm. Input in steps. Called by the system abort and hard sys_sync_current_position(); // Syncs all internal position vectors to the current system position. } return; } ADCSRA = ADCSRA_init | (1<<ADIF); //0x93, clear ADIF uint8_t reverse_flag = 0; uint8_t out_bits = 0; uint8_t jog_select = 0; out_bits0 = (0) ^ (settings.invert_mask); ADCSRA = ADCSRA_init | (1<<ADIF); //0x93, clear ADIF ADCSRA = ADCSRA_init | (1<<ADSC); //0xC3; start conversion sys.state = STATE_JOG; // check for reverse switches if (jog_bits & (1<<JOGREV_X_BIT)) { // X reverse switch on out_bits0 ^= (1<<X_DIRECTION_BIT); out_bits = out_bits0 ^ (1<<X_STEP_BIT); reverse_flag = 1; } if (jog_bits & (1<<JOGREV_Y_BIT)) { // Y reverse switch on out_bits0 ^= (1<<Y_DIRECTION_BIT); out_bits = out_bits0 ^ (1<<Y_STEP_BIT); reverse_flag = 1; jog_select = 1; } if (jog_bits & (1<<JOGREV_Z_BIT)) { // Z reverse switch on out_bits0 ^= (1<<Z_DIRECTION_BIT); out_bits = out_bits0 ^ (1<<Z_STEP_BIT); // reverse_flag = 1; // positive Z dir! jog_select = 2; } // check for forward switches if (jog_bits & (1<<JOGFWD_X_BIT)) { // X forward switch on out_bits = out_bits0 ^ (1<<X_STEP_BIT); } if (jog_bits & (1<<JOGFWD_Y_BIT)) { // Y forward switch on out_bits = out_bits0 ^ (1<<Y_STEP_BIT); jog_select = 1; } if (jog_bits & (1<<JOGFWD_Z_BIT)) { // Z forward switch on out_bits = out_bits0 ^ (1<<Z_STEP_BIT); reverse_flag = 1; // positive Z dir! jog_select = 2; } dest_step_rate = ADCH; // set initial dest_step_rate according to analog input dest_step_rate = (dest_step_rate * JOG_SPEED_FAC) + JOG_MIN_SPEED; step_rate = JOG_MIN_SPEED; // set initial step rate jog_exit = 0; while (!(ADCSRA && (1<<ADIF))) {} // warte bis ADIF-Bit gesetzt ADCSRA = ADCSRA_init; // exit conversion st_wake_up(); // prepare direction with small delay, direction settle time STEPPING_PORT = (STEPPING_PORT & ~STEPPING_MASK) | (out_bits0 & STEPPING_MASK); delay_us(10); jog_bits_old = jog_bits; i = 0; // now index for sending position data // Report machine position; note Z scaling if (bit_istrue(settings.flags,BITFLAG_REPORT_INCHES)) { mm_per_step = 1/(settings.steps_per_mm[jog_select] * INCH_PER_MM); mm_per_step_z = 1/(settings.steps_per_mm[jog_select] * INCH_PER_MM * settings.z_scale); } else { mm_per_step = 1/settings.steps_per_mm[jog_select]; mm_per_step_z = 1/(settings.steps_per_mm[jog_select] * settings.z_scale); } work_position = print_position[jog_select]; for(;;) { // repeat until button/joystick released // report_realtime_status(); // benötigt viel Zeit! ADCSRA = ADCSRA_init | (1<<ADIF); //0x93, clear ADIF // Get limit pin state #ifdef LIMIT_SWITCHES_ACTIVE_HIGH // When in an active-high switch configuration limit_state = LIMIT_PIN; #else limit_state = LIMIT_PIN ^ LIMIT_MASK; #endif if ((limit_state & LIMIT_MASK) && reverse_flag) { jog_exit = 1; } // immediate stop jog_bits = (~JOGSW_PIN) & JOGSW_MASK; // active low if (jog_bits == jog_bits_old) { // nothing changed if (step_rate < (dest_step_rate - 5)) { // Hysterese für A/D-Wandlung step_rate += JOG_RAMP; // accellerate } if (step_rate > (dest_step_rate + 5)) { // Hysterese für A/D-Wandlung step_rate -= JOG_RAMP; // brake } } else { if (step_rate > (JOG_MIN_SPEED*2)) { // switch change happened, fast brake to complete stop step_rate = ((step_rate * 99) / 100) - 10; } else { jog_exit = 1; } // finished to stop and exit } // stop and exit if done if (jog_exit || (sys.execute & EXEC_RESET)) { st_go_idle(); sys.state = last_sys_state; sys_sync_current_position(); if (jog_exit) { report_realtime_status(); } return; } ADCSRA = ADCSRA_init | (1<<ADSC); //0xC3; start ADC conversion // Both direction and step pins appropriately inverted and set. Perform one step STEPPING_PORT = (STEPPING_PORT & ~STEPPING_MASK) | (out_bits & STEPPING_MASK); delay_us(settings.pulse_microseconds/2); step_delay = (1000000/step_rate) - settings.pulse_microseconds - 100; // 100 = fester Wert für Schleifenzeit STEPPING_PORT = (STEPPING_PORT & ~STEPPING_MASK) | (out_bits0 & STEPPING_MASK); // update position registers, Q&D Z fix! if (jog_select==2) { if (reverse_flag) { sys.position[2]++; // sys.position ist in Steps! work_position += mm_per_step_z; // relative print_position in mm since last report } else { sys.position[2]--; work_position -= mm_per_step_z; // relative print_position in mm since last report } } else { if (reverse_flag) { sys.position[jog_select]--; // sys.position ist in Steps! work_position -= mm_per_step; } else { sys.position[jog_select]++; work_position += mm_per_step; // relative print_position in mm since last report } } if (sys.execute & EXEC_STATUS_REPORT) { if (step_delay > 250) { // status report requested, print short msg only printPgmString(PSTR("Jog")); serial_write(88 + jog_select); // 88 = X + 1 = Y etc. serial_write(44); printFloat(work_position); serial_write(13); serial_write(10); step_delay -= 250; } else { printPgmString(PSTR("JogF\r\n")); } sys.execute = 0; } delay_us(step_delay); while (!(ADCSRA && (1<<ADIF))) {} // warte ggf. bis ADIF-Bit gesetzt ADCSRA = ADCSRA_init; // exit conversion dest_step_rate = ADCH; // set next dest_step_rate according to analog input dest_step_rate = (dest_step_rate * JOG_SPEED_FAC) + JOG_MIN_SPEED; } }
// Executes run-time commands, when required. This is called from various check points in the main // program, primarily where there may be a while loop waiting for a buffer to clear space or any // point where the execution time from the last check point may be more than a fraction of a second. // This is a way to execute runtime commands asynchronously (aka multitasking) with grbl's g-code // parsing and planning functions. This function also serves as an interface for the interrupts to // set the system runtime flags, where only the main program handles them, removing the need to // define more computationally-expensive volatile variables. This also provides a controlled way to // execute certain tasks without having two or more instances of the same task, such as the planner // recalculating the buffer upon a feedhold or override. // NOTE: The sys.execute variable flags are set by any process, step or serial interrupts, pinouts, // limit switches, or the main program. void protocol_execute_runtime() { if (sys.execute) { // Enter only if any bit flag is true uint8_t rt_exec = sys.execute; // Avoid calling volatile multiple times // System alarm. Everything has shutdown by something that has gone severely wrong. Report // the source of the error to the user. If critical, Grbl disables by entering an infinite // loop until system reset/abort. if (rt_exec & (EXEC_ALARM | EXEC_CRIT_EVENT)) { sys.state = STATE_ALARM; // Set system alarm state // Critical event. Only hard limit qualifies. Update this as new critical events surface. if (rt_exec & EXEC_CRIT_EVENT) { report_alarm_message(ALARM_HARD_LIMIT); report_feedback_message(MESSAGE_CRITICAL_EVENT); bit_false(sys.execute,EXEC_RESET); // Disable any existing reset do { // Nothing. Block EVERYTHING until user issues reset or power cycles. Hard limits // typically occur while unattended or not paying attention. Gives the user time // to do what is needed before resetting, like killing the incoming stream. } while (bit_isfalse(sys.execute,EXEC_RESET)); // Standard alarm event. Only abort during motion qualifies. } else { // Runtime abort command issued during a cycle, feed hold, or homing cycle. Message the // user that position may have been lost and set alarm state to enable the alarm lockout // to indicate the possible severity of the problem. report_alarm_message(ALARM_ABORT_CYCLE); } bit_false(sys.execute,(EXEC_ALARM | EXEC_CRIT_EVENT)); } // Execute system abort. if (rt_exec & EXEC_RESET) { sys.abort = true; // Only place this is set true. return; // Nothing else to do but exit. } // Execute and serial print status if (rt_exec & EXEC_STATUS_REPORT) { report_realtime_status(); bit_false(sys.execute,EXEC_STATUS_REPORT); } // Initiate stepper feed hold if (rt_exec & EXEC_FEED_HOLD) { st_feed_hold(); // Initiate feed hold. bit_false(sys.execute,EXEC_FEED_HOLD); } // Reinitializes the stepper module running state and, if a feed hold, re-plans the buffer. // NOTE: EXEC_CYCLE_STOP is set by the stepper subsystem when a cycle or feed hold completes. if (rt_exec & EXEC_CYCLE_STOP) { st_cycle_reinitialize(); bit_false(sys.execute,EXEC_CYCLE_STOP); } if (rt_exec & EXEC_CYCLE_START) { st_cycle_start(); // Issue cycle start command to stepper subsystem if (bit_istrue(settings.flags,BITFLAG_AUTO_START)) { sys.auto_start = true; // Re-enable auto start after feed hold. } bit_false(sys.execute,EXEC_CYCLE_START); } } // Overrides flag byte (sys.override) and execution should be installed here, since they // are runtime and require a direct and controlled interface to the main stepper program. }
// Executes run-time commands, when required. This is called from various check points in the main // program, primarily where there may be a while loop waiting for a buffer to clear space or any // point where the execution time from the last check point may be more than a fraction of a second. // This is a way to execute realtime commands asynchronously (aka multitasking) with grbl's g-code // parsing and planning functions. This function also serves as an interface for the interrupts to // set the system realtime flags, where only the main program handles them, removing the need to // define more computationally-expensive volatile variables. This also provides a controlled way to // execute certain tasks without having two or more instances of the same task, such as the planner // recalculating the buffer upon a feedhold or override. // NOTE: The sys_rt_exec_state variable flags are set by any process, step or serial interrupts, pinouts, // limit switches, or the main program. void protocol_execute_realtime() { uint8_t rt_exec; // Temp variable to avoid calling volatile multiple times. do { // If system is suspended, suspend loop restarts here. // Check and execute alarms. rt_exec = sys_rt_exec_alarm; // Copy volatile sys_rt_exec_alarm. if (rt_exec) { // Enter only if any bit flag is true // System alarm. Everything has shutdown by something that has gone severely wrong. Report // the source of the error to the user. If critical, Grbl disables by entering an infinite // loop until system reset/abort. sys.state = STATE_ALARM; // Set system alarm state if (rt_exec & EXEC_ALARM_HARD_LIMIT) { report_alarm_message(ALARM_HARD_LIMIT_ERROR); } else if (rt_exec & EXEC_ALARM_SOFT_LIMIT) { report_alarm_message(ALARM_SOFT_LIMIT_ERROR); } else if (rt_exec & EXEC_ALARM_ABORT_CYCLE) { report_alarm_message(ALARM_ABORT_CYCLE); } else if (rt_exec & EXEC_ALARM_PROBE_FAIL) { report_alarm_message(ALARM_PROBE_FAIL); } else if (rt_exec & EXEC_ALARM_HOMING_FAIL) { report_alarm_message(ALARM_HOMING_FAIL); } // Halt everything upon a critical event flag. Currently hard and soft limits flag this. if (rt_exec & EXEC_CRITICAL_EVENT) { report_feedback_message(MESSAGE_CRITICAL_EVENT); bit_false_atomic(sys_rt_exec_state,EXEC_RESET); // Disable any existing reset do { // Nothing. Block EVERYTHING until user issues reset or power cycles. Hard limits // typically occur while unattended or not paying attention. Gives the user time // to do what is needed before resetting, like killing the incoming stream. The // same could be said about soft limits. While the position is not lost, the incoming // stream could be still engaged and cause a serious crash if it continues afterwards. // TODO: Allow status reports during a critical alarm. Still need to think about implications of this. // if (sys_rt_exec_state & EXEC_STATUS_REPORT) { // report_realtime_status(); // bit_false_atomic(sys_rt_exec_state,EXEC_STATUS_REPORT); // } } while (bit_isfalse(sys_rt_exec_state,EXEC_RESET)); } bit_false_atomic(sys_rt_exec_alarm,0xFF); // Clear all alarm flags } // Check amd execute realtime commands rt_exec = sys_rt_exec_state; // Copy volatile sys_rt_exec_state. if (rt_exec) { // Enter only if any bit flag is true // Execute system abort. if (rt_exec & EXEC_RESET) { sys.abort = true; // Only place this is set true. return; // Nothing else to do but exit. } // Execute and serial print status if (rt_exec & EXEC_STATUS_REPORT) { report_realtime_status(); bit_false_atomic(sys_rt_exec_state,EXEC_STATUS_REPORT); } // Execute hold states. // NOTE: The math involved to calculate the hold should be low enough for most, if not all, // operational scenarios. Once hold is initiated, the system enters a suspend state to block // all main program processes until either reset or resumed. if (rt_exec & (EXEC_MOTION_CANCEL | EXEC_FEED_HOLD | EXEC_SAFETY_DOOR)) { // TODO: CHECK MODE? How to handle this? Likely nothing, since it only works when IDLE and then resets Grbl. // State check for allowable states for hold methods. if ((sys.state == STATE_IDLE) || (sys.state & (STATE_CYCLE | STATE_HOMING | STATE_MOTION_CANCEL | STATE_HOLD | STATE_SAFETY_DOOR))) { // If in CYCLE state, all hold states immediately initiate a motion HOLD. if (sys.state == STATE_CYCLE) { st_update_plan_block_parameters(); // Notify stepper module to recompute for hold deceleration. sys.suspend = SUSPEND_ENABLE_HOLD; // Initiate holding cycle with flag. } // If IDLE, Grbl is not in motion. Simply indicate suspend ready state. if (sys.state == STATE_IDLE) { sys.suspend = SUSPEND_ENABLE_READY; } // Execute and flag a motion cancel with deceleration and return to idle. Used primarily by probing cycle // to halt and cancel the remainder of the motion. if (rt_exec & EXEC_MOTION_CANCEL) { // MOTION_CANCEL only occurs during a CYCLE, but a HOLD and SAFETY_DOOR may been initiated beforehand // to hold the CYCLE. If so, only flag that motion cancel is complete. if (sys.state == STATE_CYCLE) { sys.state = STATE_MOTION_CANCEL; } sys.suspend |= SUSPEND_MOTION_CANCEL; // Indicate motion cancel when resuming. Special motion complete. } // Execute a feed hold with deceleration, only during cycle. if (rt_exec & EXEC_FEED_HOLD) { // Block SAFETY_DOOR state from prematurely changing back to HOLD. if (bit_isfalse(sys.state,STATE_SAFETY_DOOR)) { sys.state = STATE_HOLD; } } // Execute a safety door stop with a feed hold, only during a cycle, and disable spindle/coolant. // NOTE: Safety door differs from feed holds by stopping everything no matter state, disables powered // devices (spindle/coolant), and blocks resuming until switch is re-engaged. The power-down is // executed here, if IDLE, or when the CYCLE completes via the EXEC_CYCLE_STOP flag. if (rt_exec & EXEC_SAFETY_DOOR) { report_feedback_message(MESSAGE_SAFETY_DOOR_AJAR); // If already in active, ready-to-resume HOLD, set CYCLE_STOP flag to force de-energize. // NOTE: Only temporarily sets the 'rt_exec' variable, not the volatile 'rt_exec_state' variable. if (sys.suspend & SUSPEND_ENABLE_READY) { bit_true(rt_exec,EXEC_CYCLE_STOP); } sys.suspend |= SUSPEND_ENERGIZE; sys.state = STATE_SAFETY_DOOR; } } bit_false_atomic(sys_rt_exec_state,(EXEC_MOTION_CANCEL | EXEC_FEED_HOLD | EXEC_SAFETY_DOOR)); } // Execute a cycle start by starting the stepper interrupt to begin executing the blocks in queue. if (rt_exec & EXEC_CYCLE_START) { // Block if called at same time as the hold commands: feed hold, motion cancel, and safety door. // Ensures auto-cycle-start doesn't resume a hold without an explicit user-input. if (!(rt_exec & (EXEC_FEED_HOLD | EXEC_MOTION_CANCEL | EXEC_SAFETY_DOOR))) { // Cycle start only when IDLE or when a hold is complete and ready to resume. // NOTE: SAFETY_DOOR is implicitly blocked. It reverts to HOLD when the door is closed. if ((sys.state == STATE_IDLE) || ((sys.state & (STATE_HOLD | STATE_MOTION_CANCEL)) && (sys.suspend & SUSPEND_ENABLE_READY))) { // Re-energize powered components, if disabled by SAFETY_DOOR. if (sys.suspend & SUSPEND_ENERGIZE) { // Delayed Tasks: Restart spindle and coolant, delay to power-up, then resume cycle. if (gc_state.modal.spindle != SPINDLE_DISABLE) { spindle_set_state(gc_state.modal.spindle, gc_state.spindle_speed); delay_ms(SAFETY_DOOR_SPINDLE_DELAY); // TODO: Blocking function call. Need a non-blocking one eventually. } if (gc_state.modal.coolant != COOLANT_DISABLE) { coolant_set_state(gc_state.modal.coolant); delay_ms(SAFETY_DOOR_COOLANT_DELAY); // TODO: Blocking function call. Need a non-blocking one eventually. } // TODO: Install return to pre-park position. } // Start cycle only if queued motions exist in planner buffer and the motion is not canceled. if (plan_get_current_block() && bit_isfalse(sys.suspend,SUSPEND_MOTION_CANCEL)) { sys.state = STATE_CYCLE; st_prep_buffer(); // Initialize step segment buffer before beginning cycle. st_wake_up(); } else { // Otherwise, do nothing. Set and resume IDLE state. sys.state = STATE_IDLE; } sys.suspend = SUSPEND_DISABLE; // Break suspend state. } } bit_false_atomic(sys_rt_exec_state,EXEC_CYCLE_START); } // Reinitializes the cycle plan and stepper system after a feed hold for a resume. Called by // realtime command execution in the main program, ensuring that the planner re-plans safely. // NOTE: Bresenham algorithm variables are still maintained through both the planner and stepper // cycle reinitializations. The stepper path should continue exactly as if nothing has happened. // NOTE: EXEC_CYCLE_STOP is set by the stepper subsystem when a cycle or feed hold completes. if (rt_exec & EXEC_CYCLE_STOP) { if (sys.state & (STATE_HOLD | STATE_SAFETY_DOOR)) { // Hold complete. Set to indicate ready to resume. Remain in HOLD or DOOR states until user // has issued a resume command or reset. if (sys.suspend & SUSPEND_ENERGIZE) { // De-energize system if safety door has been opened. spindle_stop(); coolant_stop(); // TODO: Install parking motion here. } bit_true(sys.suspend,SUSPEND_ENABLE_READY); } else { // Motion is complete. Includes CYCLE, HOMING, and MOTION_CANCEL states. sys.suspend = SUSPEND_DISABLE; sys.state = STATE_IDLE; } bit_false_atomic(sys_rt_exec_state,EXEC_CYCLE_STOP); } } // Overrides flag byte (sys.override) and execution should be installed here, since they // are realtime and require a direct and controlled interface to the main stepper program. // Reload step segment buffer if (sys.state & (STATE_CYCLE | STATE_HOLD | STATE_MOTION_CANCEL | STATE_SAFETY_DOOR | STATE_HOMING)) { st_prep_buffer(); } // If safety door was opened, actively check when safety door is closed and ready to resume. // NOTE: This unlocks the SAFETY_DOOR state to a HOLD state, such that CYCLE_START can activate a resume. if (sys.state == STATE_SAFETY_DOOR) { if (bit_istrue(sys.suspend,SUSPEND_ENABLE_READY)) { #ifndef DEFAULTS_TRINAMIC if (!(system_check_safety_door_ajar())) { sys.state = STATE_HOLD; // Update to HOLD state to indicate door is closed and ready to resume. } #endif } } } while(sys.suspend); // Check for system suspend state before exiting. }