diff --git a/Marlin/src/HAL/HAL_AVR/fast_pwm.cpp b/Marlin/src/HAL/HAL_AVR/fast_pwm.cpp index 282b70de7..4884ede63 100644 --- a/Marlin/src/HAL/HAL_AVR/fast_pwm.cpp +++ b/Marlin/src/HAL/HAL_AVR/fast_pwm.cpp @@ -23,7 +23,7 @@ #include "../../inc/MarlinConfigPre.h" -#if ENABLED(FAST_PWM_FAN) +#if ENABLED(FAST_PWM_FAN) || SPINDLE_LASER_PWM #include "HAL.h" @@ -278,5 +278,5 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255 } } -#endif // FAST_PWM_FAN +#endif // FAST_PWM_FAN || SPINDLE_LASER_PWM #endif // __AVR__ diff --git a/Marlin/src/HAL/HAL_LPC1768/fast_pwm.cpp b/Marlin/src/HAL/HAL_LPC1768/fast_pwm.cpp index 7af3a07ee..1cc203277 100644 --- a/Marlin/src/HAL/HAL_LPC1768/fast_pwm.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/fast_pwm.cpp @@ -24,7 +24,7 @@ #include "../../inc/MarlinConfigPre.h" -#if ENABLED(FAST_PWM_FAN) +#if ENABLED(FAST_PWM_FAN) || SPINDLE_LASER_PWM #include @@ -36,5 +36,5 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255 pwm_write_ratio(pin, invert ? 1.0f - (float)v / v_size : (float)v / v_size); } -#endif // FAST_PWM_FAN +#endif // FAST_PWM_FAN || SPINDLE_LASER_PWM #endif // TARGET_LPC1768 diff --git a/Marlin/src/feature/spindle_laser.cpp b/Marlin/src/feature/spindle_laser.cpp index 81865fddb..775e7a864 100644 --- a/Marlin/src/feature/spindle_laser.cpp +++ b/Marlin/src/feature/spindle_laser.cpp @@ -34,14 +34,16 @@ SpindleLaser cutter; cutter_power_t SpindleLaser::power; // = 0 +#define SPINDLE_LASER_PWM_OFF ((SPINDLE_LASER_PWM_INVERT) ? 255 : 0) + void SpindleLaser::init() { OUT_WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Init spindle to off #if ENABLED(SPINDLE_CHANGE_DIR) OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // Init rotation to clockwise (M3) #endif - #if ENABLED(SPINDLE_LASER_PWM) && PIN_EXISTS(SPINDLE_LASER_PWM) + #if ENABLED(SPINDLE_LASER_PWM) SET_PWM(SPINDLE_LASER_PWM_PIN); - analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_INVERT ? 255 : 0); // set to lowest speed + analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // set to lowest speed #endif } @@ -54,34 +56,34 @@ void SpindleLaser::init() { */ void SpindleLaser::set_ocr(const uint8_t ocr) { WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_HIGH); // turn spindle on (active low) - #if ENABLED(SPINDLE_LASER_PWM) - analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), (SPINDLE_LASER_PWM_INVERT) ? 255 - ocr : ocr); - #endif + analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF); } #endif -void SpindleLaser::update_output() { - const bool ena = enabled(); +void SpindleLaser::apply_power(const cutter_power_t inpow) { + static cutter_power_t last_power_applied = 0; + if (inpow == last_power_applied) return; + last_power_applied = inpow; #if ENABLED(SPINDLE_LASER_PWM) - if (ena) { + if (enabled()) { + #define _scaled(F) ((F - (SPEED_POWER_INTERCEPT)) * inv_slope) constexpr float inv_slope = RECIPROCAL(SPEED_POWER_SLOPE), - min_ocr = (SPEED_POWER_MIN - (SPEED_POWER_INTERCEPT)) * inv_slope, // Minimum allowed - max_ocr = (SPEED_POWER_MAX - (SPEED_POWER_INTERCEPT)) * inv_slope; // Maximum allowed + min_ocr = _scaled(SPEED_POWER_MIN), + max_ocr = _scaled(SPEED_POWER_MAX); int16_t ocr_val; - if (power <= SPEED_POWER_MIN) ocr_val = min_ocr; // Use minimum if set below - else if (power >= SPEED_POWER_MAX) ocr_val = max_ocr; // Use maximum if set above - else ocr_val = (power - (SPEED_POWER_INTERCEPT)) * inv_slope; // Use calculated OCR value - set_ocr(ocr_val & 0xFF); // ...limited to Atmel PWM max + if (inpow <= SPEED_POWER_MIN) ocr_val = min_ocr; // Use minimum if set below + else if (inpow >= SPEED_POWER_MAX) ocr_val = max_ocr; // Use maximum if set above + else ocr_val = _scaled(inpow); // Use calculated OCR value + set_ocr(ocr_val & 0xFF); // ...limited to Atmel PWM max } - else { // Convert RPM to PWM duty cycle - WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off (active low) - analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_INVERT ? 255 : 0); // Only write low byte + else { + WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off (active low) + analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte } #else - WRITE(SPINDLE_LASER_ENA_PIN, ena ? SPINDLE_LASER_ACTIVE_HIGH : !SPINDLE_LASER_ACTIVE_HIGH); + WRITE(SPINDLE_LASER_ENA_PIN, (SPINDLE_LASER_ACTIVE_HIGH) ? enabled() : !enabled()); #endif - power_delay(ena); } #if ENABLED(SPINDLE_CHANGE_DIR) diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 133152a91..80b57be31 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -36,10 +36,10 @@ #define MSG_CUTTER(M) _MSG_CUTTER(M) #if SPEED_POWER_MAX > 255 - #define cutter_power_t uint16_t + typedef uint16_t cutter_power_t; #define CUTTER_MENU_TYPE uint16_5 #else - #define cutter_power_t uint8_t + typedef uint8_t cutter_power_t; #define CUTTER_MENU_TYPE uint8 #endif @@ -51,9 +51,17 @@ public: static inline bool enabled() { return !!power; } - static inline void set_power(const uint8_t pwr) { power = pwr; update_output(); } + static inline void set_power(const cutter_power_t pwr) { power = pwr; } - static inline void set_enabled(const bool enable) { set_power(enable ? 255 : 0); } + static inline void refresh() { apply_power(power); } + + static inline void set_enabled(const bool enable) { + const bool was = enabled(); + set_power(enable ? 255 : 0); + if (was != enable) power_delay(); + } + + static void apply_power(const cutter_power_t inpow); //static bool active() { return READ(SPINDLE_LASER_ENA_PIN) == SPINDLE_LASER_ACTIVE_HIGH; } @@ -61,11 +69,15 @@ public: #if ENABLED(SPINDLE_LASER_PWM) static void set_ocr(const uint8_t ocr); - static inline void set_ocr_power(const uint8_t pwr) { power = pwr; set_ocr(pwr); } + static inline void set_ocr_power(const cutter_power_t pwr) { power = pwr; set_ocr(pwr); } #endif // Wait for spindle to spin up or spin down - static inline void power_delay(const bool on) { safe_delay(on ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY); } + static inline void power_delay() { + #if SPINDLE_LASER_POWERUP_DELAY || SPINDLE_LASER_POWERDOWN_DELAY + safe_delay(enabled() ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY); + #endif + } #if ENABLED(SPINDLE_CHANGE_DIR) static void set_direction(const bool reverse); diff --git a/Marlin/src/gcode/control/M3-M5.cpp b/Marlin/src/gcode/control/M3-M5.cpp index b5e789a92..81e164519 100644 --- a/Marlin/src/gcode/control/M3-M5.cpp +++ b/Marlin/src/gcode/control/M3-M5.cpp @@ -29,10 +29,20 @@ #include "../../module/stepper.h" /** - * M3 - Cutter ON (Clockwise) - * M4 - Cutter ON (Counter-clockwise) + * Laser: * - * S - Set power. S0 turns it off. + * M3 - Laser ON/Power (Ramped power) + * M4 - Laser ON/Power (Continuous power) + * + * S - Set power. S0 will turn the laser off. + * O - Set power and OCR + * + * Spindle: + * + * M3 - Spindle ON (Clockwise) + * M4 - Spindle ON (Counter-clockwise) + * + * S - Set power. S0 will turn the spindle off. * O - Set power and OCR * * If no PWM pin is defined then M3/M4 just turns it on. @@ -61,12 +71,14 @@ */ void GcodeSuite::M3_M4(const bool is_M4) { - planner.synchronize(); // Wait for previous movement commands (G0/G0/G2/G3) to complete before changing power + #if ENABLED(SPINDLE_FEATURE) + planner.synchronize(); // Wait for movement to complete before changing power + #endif cutter.set_direction(is_M4); #if ENABLED(SPINDLE_LASER_PWM) - if (parser.seen('O')) + if (parser.seenval('O')) cutter.set_ocr_power(parser.value_byte()); // The OCR is a value from 0 to 255 (uint8_t) else cutter.set_power(parser.intval('S', 255)); @@ -79,7 +91,9 @@ void GcodeSuite::M3_M4(const bool is_M4) { * M5 - Cutter OFF */ void GcodeSuite::M5() { - planner.synchronize(); + #if ENABLED(SPINDLE_FEATURE) + planner.synchronize(); + #endif cutter.set_enabled(false); } diff --git a/Marlin/src/lcd/menu/menu_spindle_laser.cpp b/Marlin/src/lcd/menu/menu_spindle_laser.cpp index f9add1cb0..1c480577f 100644 --- a/Marlin/src/lcd/menu/menu_spindle_laser.cpp +++ b/Marlin/src/lcd/menu/menu_spindle_laser.cpp @@ -38,7 +38,7 @@ BACK_ITEM(MSG_MAIN); if (cutter.enabled()) { #if ENABLED(SPINDLE_LASER_PWM) - EDIT_ITEM(CUTTER_MENU_TYPE, MSG_CUTTER(POWER), &cutter.power, SPEED_POWER_MIN, SPEED_POWER_MAX, cutter.update_output); + EDIT_ITEM(CUTTER_MENU_TYPE, MSG_CUTTER(POWER), &cutter.power, SPEED_POWER_MIN, SPEED_POWER_MAX); #endif ACTION_ITEM(MSG_CUTTER(OFF), cutter.disable); } diff --git a/Marlin/src/module/planner.cpp b/Marlin/src/module/planner.cpp index d79d96d95..3037ae97f 100644 --- a/Marlin/src/module/planner.cpp +++ b/Marlin/src/module/planner.cpp @@ -100,6 +100,10 @@ #include "../feature/power_loss_recovery.h" #endif +#if HAS_CUTTER + #include "../feature/spindle_laser.h" +#endif + // Delay for delivery of first block to the stepper ISR, if the queue contains 2 or // fewer movements. The delay is measured in milliseconds, and must be less than 250ms #define BLOCK_DELAY_FOR_1ST_MOVE 100 @@ -1220,6 +1224,11 @@ void Planner::check_axes_activity() { #endif } else { + + #if HAS_CUTTER + cutter.refresh(); + #endif + #if FAN_COUNT > 0 FANS_LOOP(i) tail_fan_speed[i] = thermalManager.scaledFanSpeed(i); @@ -1235,6 +1244,9 @@ void Planner::check_axes_activity() { #endif } + // + // Disable inactive axes + // #if ENABLED(DISABLE_X) if (!axis_active.x) disable_X(); #endif @@ -1248,6 +1260,9 @@ void Planner::check_axes_activity() { if (!axis_active.e) disable_e_steppers(); #endif + // + // Update Fan speeds + // #if FAN_COUNT > 0 #if FAN_KICKSTART_TIME > 0 @@ -1841,6 +1856,10 @@ bool Planner::_populate_block(block_t * const block, bool split_move, MIXER_POPULATE_BLOCK(); #endif + #if HAS_CUTTER + block->cutter_power = cutter.power; + #endif + #if FAN_COUNT > 0 FANS_LOOP(i) block->fan_speed[i] = thermalManager.fan_speed[i]; #endif @@ -2354,13 +2373,21 @@ bool Planner::_populate_block(block_t * const block, bool split_move, #endif + #ifdef USE_CACHED_SQRT + #define CACHED_SQRT(N, V) \ + static float saved_V, N; \ + if (V != saved_V) { N = SQRT(V); saved_V = V; } + #else + #define CACHED_SQRT(N, V) const float N = SQRT(V) + #endif + #if HAS_CLASSIC_JERK /** * Adapted from Průša MKS firmware * https://github.com/prusa3d/Prusa-Firmware */ - const float nominal_speed = SQRT(block->nominal_speed_sqr); + CACHED_SQRT(nominal_speed, block->nominal_speed_sqr); // Exit speed limited by a jerk to full halt of a previous last segment static float previous_safe_speed; @@ -2401,7 +2428,8 @@ bool Planner::_populate_block(block_t * const block, bool split_move, // The junction velocity will be shared between successive segments. Limit the junction velocity to their minimum. // Pick the smaller of the nominal speeds. Higher speed shall not be achieved at the junction during coasting. - const float previous_nominal_speed = SQRT(previous_nominal_speed_sqr); + CACHED_SQRT(previous_nominal_speed, previous_nominal_speed_sqr); + vmax_junction = _MIN(nominal_speed, previous_nominal_speed); // Now limit the jerk in all axes. diff --git a/Marlin/src/module/planner.h b/Marlin/src/module/planner.h index c4e576514..8919ae123 100644 --- a/Marlin/src/module/planner.h +++ b/Marlin/src/module/planner.h @@ -51,6 +51,10 @@ #include "../feature/mixing.h" #endif +#if HAS_CUTTER + #include "../feature/spindle_laser.h" +#endif + // Feedrate for manual moves #ifdef MANUAL_FEEDRATE constexpr xyze_feedrate_t manual_feedrate_mm_m = MANUAL_FEEDRATE; @@ -145,6 +149,10 @@ typedef struct block_t { final_rate, // The minimal rate at exit acceleration_steps_per_s2; // acceleration steps/sec^2 + #if HAS_CUTTER + cutter_power_t cutter_power; // Power level for Spindle, Laser, etc. + #endif + #if FAN_COUNT > 0 uint8_t fan_speed[FAN_COUNT]; #endif diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index 12d5820d0..c8e51525f 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -1667,6 +1667,10 @@ uint32_t Stepper::stepper_block_phase_isr() { return interval; // No more queued movements! } + #if HAS_CUTTER + cutter.apply_power(current_block->cutter_power); + #endif + #if ENABLED(POWER_LOSS_RECOVERY) recovery.info.sdpos = current_block->sdpos; #endif