From 0ab445a652a3a69a43c16d524644ba9c6595b02f Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 9 May 2020 11:20:02 -0500 Subject: [PATCH 01/17] AVR Timer 5 reference code --- Marlin/src/HAL/AVR/fastio.cpp | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Marlin/src/HAL/AVR/fastio.cpp b/Marlin/src/HAL/AVR/fastio.cpp index 9f70b50f7177..01350488265a 100644 --- a/Marlin/src/HAL/AVR/fastio.cpp +++ b/Marlin/src/HAL/AVR/fastio.cpp @@ -234,5 +234,55 @@ uint8_t extDigitalRead(const int8_t pin) { } } +#if 0 +/** + * Set Timer 5 PWM frequency in Hz, from 3.8Hz up to ~16MHz + * with a minimum resolution of 100 steps. + * + * DC values -1.0 to 1.0. Negative duty cycle inverts the pulse. + */ +uint16_t set_pwm_frequency_hz(const float &hz, const float dca, const float dcb, const float dcc) { + float count = 0; + if (hz > 0 && (dca || dcb || dcc)) { + count = float(F_CPU) / hz; // 1x prescaler, TOP for 16MHz base freq. + uint16_t prescaler; // Range of 30.5Hz (65535) 64.5KHz (>31) + + if (count >= 255. * 256.) { prescaler = 1024; SET_CS(5, PRESCALER_1024); } + else if (count >= 255. * 64.) { prescaler = 256; SET_CS(5, PRESCALER_256); } + else if (count >= 255. * 8.) { prescaler = 64; SET_CS(5, PRESCALER_64); } + else if (count >= 255.) { prescaler = 8; SET_CS(5, PRESCALER_8); } + else { prescaler = 1; SET_CS(5, PRESCALER_1); } + + count /= float(prescaler); + const float pwm_top = round(count); // Get the rounded count + + ICR5 = (uint16_t)pwm_top - 1; // Subtract 1 for TOP + OCR5A = pwm_top * ABS(dca); // Update and scale DCs + OCR5B = pwm_top * ABS(dcb); + OCR5C = pwm_top * ABS(dcc); + _SET_COM(5, A, dca ? (dca < 0 ? COM_SET_CLEAR : COM_CLEAR_SET) : COM_NORMAL); // Set compare modes + _SET_COM(5, B, dcb ? (dcb < 0 ? COM_SET_CLEAR : COM_CLEAR_SET) : COM_NORMAL); + _SET_COM(5, C, dcc ? (dcc < 0 ? COM_SET_CLEAR : COM_CLEAR_SET) : COM_NORMAL); + + SET_WGM(5, FAST_PWM_ICRn); // Fast PWM with ICR5 as TOP + + //SERIAL_ECHOLNPGM("Timer 5 Settings:"); + //SERIAL_ECHOLNPAIR(" Prescaler=", prescaler); + //SERIAL_ECHOLNPAIR(" TOP=", ICR5); + //SERIAL_ECHOLNPAIR(" OCR5A=", OCR5A); + //SERIAL_ECHOLNPAIR(" OCR5B=", OCR5B); + //SERIAL_ECHOLNPAIR(" OCR5C=", OCR5C); + } + else { + // Restore the default for Timer 5 + SET_WGM(5, PWM_PC_8); // PWM 8-bit (Phase Correct) + SET_COMS(5, NORMAL, NORMAL, NORMAL); // Do nothing + SET_CS(5, PRESCALER_64); // 16MHz / 64 = 250KHz + OCR5A = OCR5B = OCR5C = 0; + } + return round(count); +} +#endif + #endif // FASTIO_EXT_START #endif // __AVR__ From e200273dff686c84217ae0eca9288980c0369f7c Mon Sep 17 00:00:00 2001 From: shitcreek Date: Wed, 22 Apr 2020 18:51:56 -0500 Subject: [PATCH 02/17] use max power for non pwm, etc. --- Marlin/src/feature/spindle_laser.cpp | 47 ++++++++++++++++++++++++++-- Marlin/src/feature/spindle_laser.h | 42 ++----------------------- Marlin/src/module/planner.cpp | 2 +- Marlin/src/module/stepper.cpp | 11 ++++--- 4 files changed, 54 insertions(+), 48 deletions(-) diff --git a/Marlin/src/feature/spindle_laser.cpp b/Marlin/src/feature/spindle_laser.cpp index 83b6dd05f7e6..b4558741235e 100644 --- a/Marlin/src/feature/spindle_laser.cpp +++ b/Marlin/src/feature/spindle_laser.cpp @@ -34,7 +34,11 @@ SpindleLaser cutter; cutter_power_t SpindleLaser::power; bool SpindleLaser::isOn; // state to determine when to apply setPower to power -cutter_setPower_t SpindleLaser::setPower = interpret_power(SPEED_POWER_MIN); // spindle/laser speed/power control in PWM, Percentage or RPM +#if ENABLED(SPINDLE_LASER_PWM) +cutter_setPower_t SpindleLaser::setPower = interpret_power(SPEED_POWER_MIN); // spindle/laser speed/power control in PWM, Percentage or RPM +#else + cutter_setPower_t SpindleLaser::setPower = interpret_power(SPEED_POWER_STARTUP); // spindle/laser speed/power control in PWM, Percentage or RPM +#endif #if ENABLED(MARLIN_DEV_MODE) cutter_frequency_t SpindleLaser::frequency; // setting PWM frequency; range: 2K - 50K #endif @@ -68,13 +72,52 @@ void SpindleLaser::init() { analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF); } +cutter_power_t SpindleLaser::translate_power(const float pwr) + { + float pwrpc; + #if CUTTER_DISPLAY_IS(PERCENT) + pwrpc = pwr; + #elif CUTTER_DISPLAY_IS(RPM) // RPM to percent + #if ENABLED(CUTTER_POWER_RELATIVE) + pwrpc = (pwr - SPEED_POWER_MIN) / (SPEED_POWER_MAX - SPEED_POWER_MIN) * 100; + #else + pwrpc = pwr / SPEED_POWER_MAX * 100; + #endif + #else + return pwr; // PWM + #endif + + #if ENABLED(SPINDLE_FEATURE) + #if ENABLED(CUTTER_POWER_RELATIVE) + constexpr float spmin = 0; + #else + constexpr float spmin = SPEED_POWER_MIN / SPEED_POWER_MAX * 100; // convert to percentage + #endif + constexpr float spmax = 100; + #else + constexpr float spmin = SPEED_POWER_MIN; + constexpr float spmax = SPEED_POWER_MAX; + #endif + + constexpr float inv_slope = RECIPROCAL(SPEED_POWER_SLOPE), + min_ocr = (spmin - (SPEED_POWER_INTERCEPT)) * inv_slope, // Minimum allowed + max_ocr = (spmax - (SPEED_POWER_INTERCEPT)) * inv_slope; // Maximum allowed + float ocr_val; + if (pwrpc < spmin) + ocr_val = min_ocr; // Use minimum if set below + else if (pwrpc > spmax) + ocr_val = max_ocr; // Use maximum if set above + else + ocr_val = (pwrpc - (SPEED_POWER_INTERCEPT)) * inv_slope; // Use calculated OCR value + return ocr_val; // ...limited to Atmel PWM max +} #endif // // Set cutter ON state (and PWM) to the given cutter power value // +static cutter_power_t last_power_applied = 0; 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) diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 0c2e569aa6f5..6aff8585627a 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -56,44 +56,6 @@ class SpindleLaser { #endif #endif } - /** - * Translate speed/power --> percentage --> PWM value - **/ - static cutter_power_t translate_power(const float pwr) { - float pwrpc; - #if CUTTER_DISPLAY_IS(PERCENT) - pwrpc = pwr; - #elif CUTTER_DISPLAY_IS(RPM) // RPM to percent - #if ENABLED(CUTTER_POWER_RELATIVE) - pwrpc = (pwr - SPEED_POWER_MIN) / (SPEED_POWER_MAX - SPEED_POWER_MIN) * 100; - #else - pwrpc = pwr / SPEED_POWER_MAX * 100; - #endif - #else - return pwr; // PWM - #endif - - #if ENABLED(SPINDLE_FEATURE) - #if ENABLED(CUTTER_POWER_RELATIVE) - constexpr float spmin = 0; - #else - constexpr float spmin = SPEED_POWER_MIN / SPEED_POWER_MAX * 100; // convert to percentage - #endif - constexpr float spmax = 100; - #else - constexpr float spmin = SPEED_POWER_MIN; - constexpr float spmax = SPEED_POWER_MAX; - #endif - - constexpr float inv_slope = RECIPROCAL(SPEED_POWER_SLOPE), - min_ocr = (spmin - (SPEED_POWER_INTERCEPT)) * inv_slope, // Minimum allowed - max_ocr = (spmax - (SPEED_POWER_INTERCEPT)) * inv_slope; // Maximum allowed - float ocr_val; - if (pwrpc < spmin) ocr_val = min_ocr; // Use minimum if set below - else if (pwrpc > spmax) ocr_val = max_ocr; // Use maximum if set above - else ocr_val = (pwrpc - (SPEED_POWER_INTERCEPT)) * inv_slope; // Use calculated OCR value - return ocr_val; // ...limited to Atmel PWM max - } static void init(); @@ -113,7 +75,7 @@ class SpindleLaser { #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 uint8_t translate_power(const cutter_power_t pwr); // Used by update output for power->OCR translation + static cutter_power_t translate_power(const float pwr); // Used by update output for power->OCR translation #endif // Wait for spindle to spin up or spin down @@ -140,7 +102,7 @@ class SpindleLaser { static inline void inline_disable() { planner.settings.laser.status = 0; planner.settings.laser.power = 0; isOn = false;} // Inline modes of all other functions; all enable planner inline power control - static inline void inline_enabled(const bool enable) { enable ? inline_power(SPEED_POWER_STARTUP) : inline_ocr_power(0); } + static inline void inline_enabled(const bool enable) { enable ? inline_power(SPEED_POWER_STARTUP) : inline_power(0); } static void inline_power(const cutter_power_t pwr) { #if ENABLED(SPINDLE_LASER_PWM) diff --git a/Marlin/src/module/planner.cpp b/Marlin/src/module/planner.cpp index 9c1071c6fb39..8816c17cae3e 100644 --- a/Marlin/src/module/planner.cpp +++ b/Marlin/src/module/planner.cpp @@ -1680,7 +1680,7 @@ bool Planner::_buffer_steps(const xyze_long_t &target * fr_mm_s - (target) speed of the move * extruder - target extruder * - * Returns true is movement is acceptable, false otherwise + * Returns true if movement is acceptable, false otherwise */ bool Planner::_populate_block(block_t * const block, bool split_move, const abce_long_t &target diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index 92ee753392c8..07e7b5d8a74a 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -1873,8 +1873,9 @@ uint32_t Stepper::block_phase_isr() { if (!(current_block = planner.get_current_block())) return interval; // No more queued movements! } - - TERN_(HAS_CUTTER, cutter.apply_power(current_block->cutter_power)); + #if DISABLED(LASER_POWER_INLINE) + TERN_(HAS_CUTTER, cutter.apply_power(current_block->cutter_power)); + #endif TERN_(POWER_LOSS_RECOVERY, recovery.info.sdpos = current_block->sdpos); @@ -2043,7 +2044,7 @@ uint32_t Stepper::block_phase_isr() { if (TEST(stat, 1)) // Laser is on cutter.set_ocr_power(laser.cur_power); else - cutter.set_power(0); + cutter.set_ocr_power(0); } #else if (TEST(stat, 0)) { // Planner controls the laser @@ -2051,7 +2052,7 @@ uint32_t Stepper::block_phase_isr() { if (TEST(stat, 1)) // Laser is on cutter.set_ocr_power(current_block->laser.power); else - cutter.set_power(0); + cutter.set_ocr_power(0); #else cutter.set_enabled(TEST(stat, 1)); #endif @@ -2100,7 +2101,7 @@ uint32_t Stepper::block_phase_isr() { if (TEST(stat, 1)) // Laser is on cutter.set_ocr_power(planner.settings.laser.power); else - cutter.set_power(0); + cutter.set_ocr_power(0); #else cutter.set_enabled(TEST(stat, 1)); #endif From bdc6eace2ff0ce8ddab034090a61a8b45f062d2b Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 8 May 2020 02:56:09 -0500 Subject: [PATCH 03/17] Clarify and fix --- Marlin/Configuration_adv.h | 40 +++--- Marlin/src/feature/spindle_laser.cpp | 70 +++------- Marlin/src/feature/spindle_laser.h | 147 ++++++++++++++++----- Marlin/src/feature/spindle_laser_types.h | 17 +-- Marlin/src/gcode/control/M3-M5.cpp | 26 ++-- Marlin/src/gcode/gcode.cpp | 8 +- Marlin/src/lcd/menu/menu_spindle_laser.cpp | 6 +- Marlin/src/module/planner.h | 6 +- Marlin/src/module/stepper.cpp | 6 +- 9 files changed, 186 insertions(+), 140 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 25bd3391697e..468a8482a64a 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -2748,10 +2748,14 @@ #define CUTTER_POWER_DISPLAY PWM255 /** - * Relative mode uses relative range (SPEED_POWER_MIN to SPEED_POWER_MAX) instead of normal range (0 to SPEED_POWER_MAX) - * Best use with SuperPID router controller where for example S0 = 5,000 RPM and S255 = 30,000 RPM + * Relative Cutter Power + * Normally, 'M3 O' sets + * OCR power is relative to the range SPEED_POWER_MIN...SPEED_POWER_MAX. + * so input powers of 0...255 correspond to SPEED_POWER_MIN...SPEED_POWER_MAX + * instead of normal range (0 to SPEED_POWER_MAX). + * Best used with (e.g.) SuperPID router controller: S0 = 5,000 RPM and S255 = 30,000 RPM */ - //#define CUTTER_POWER_RELATIVE // Set speed proportional to [SPEED_POWER_MIN...SPEED_POWER_MAX] instead of directly + //#define CUTTER_POWER_RELATIVE // Set speed proportional to [SPEED_POWER_MIN...SPEED_POWER_MAX] #if ENABLED(SPINDLE_FEATURE) //#define SPINDLE_CHANGE_DIR // Enable if your spindle controller can change spindle direction @@ -2762,25 +2766,27 @@ #define SPINDLE_LASER_POWERDOWN_DELAY 5000 // (ms) Delay to allow the spindle to stop /** - * M3/M4 uses the following equation to convert speed/power to PWM duty cycle - * Power = ((DC / 255 * 100) - SPEED_POWER_INTERCEPT)) * (1 / SPEED_POWER_SLOPE) - * where PWM DC varies from 0 to 255 + * M3/M4 Power Equation * - * Set these required parameters for your controller + * Each tool uses different value ranges for speed / power control. + * These parameters are used to convert between tool power units and PWM. + * + * Speed/Power = (PWMDC / 255 * 100 - SPEED_POWER_INTERCEPT) / SPEED_POWER_SLOPE + * PWMDC = (spdpwr - SPEED_POWER_MIN) / (SPEED_POWER_MAX - SPEED_POWER_MIN) / SPEED_POWER_SLOPE */ - #define SPEED_POWER_SLOPE 118.4 // SPEED_POWER_SLOPE = SPEED_POWER_MAX / 255 - #define SPEED_POWER_INTERCEPT 0 - #define SPEED_POWER_MIN 5000 - #define SPEED_POWER_MAX 30000 // SuperPID router controller 0 - 30,000 RPM - #define SPEED_POWER_STARTUP 25000 // The default value for speed power when M3 is called without arguments + #define SPEED_POWER_SLOPE (float(SPEED_POWER_MAX) / 255) + #define SPEED_POWER_INTERCEPT 0 // (%) 0-100 i.e., Minimum power percentage + #define SPEED_POWER_MIN 5000 // (RPM) + #define SPEED_POWER_MAX 30000 // (RPM) SuperPID router controller 0 - 30,000 RPM + #define SPEED_POWER_STARTUP 25000 // (RPM) M3/M4 speed/power default (with no arguments) #else - #define SPEED_POWER_SLOPE 0.3922 // SPEED_POWER_SLOPE = SPEED_POWER_MAX / 255 - #define SPEED_POWER_INTERCEPT 0 - #define SPEED_POWER_MIN 0 - #define SPEED_POWER_MAX 100 // 0-100% - #define SPEED_POWER_STARTUP 80 // The default value for speed power when M3 is called without arguments + #define SPEED_POWER_SLOPE (float(SPEED_POWER_MAX) / 255) + #define SPEED_POWER_INTERCEPT 0 // (%) 0-100 i.e., Minimum power percentage + #define SPEED_POWER_MIN 0 // (%) 0-100 + #define SPEED_POWER_MAX 100 // (%) 0-100 + #define SPEED_POWER_STARTUP 80 // (%) M3/M4 speed/power default (with no arguments) /** * Enable inline laser power to be handled in the planner / stepper routines. diff --git a/Marlin/src/feature/spindle_laser.cpp b/Marlin/src/feature/spindle_laser.cpp index b4558741235e..4deb32e8db44 100644 --- a/Marlin/src/feature/spindle_laser.cpp +++ b/Marlin/src/feature/spindle_laser.cpp @@ -33,12 +33,10 @@ SpindleLaser cutter; cutter_power_t SpindleLaser::power; -bool SpindleLaser::isOn; // state to determine when to apply setPower to power -#if ENABLED(SPINDLE_LASER_PWM) -cutter_setPower_t SpindleLaser::setPower = interpret_power(SPEED_POWER_MIN); // spindle/laser speed/power control in PWM, Percentage or RPM -#else - cutter_setPower_t SpindleLaser::setPower = interpret_power(SPEED_POWER_STARTUP); // spindle/laser speed/power control in PWM, Percentage or RPM -#endif +bool SpindleLaser::isOn; // state to determine when to apply displayPower to power +cutter_displayPower_t SpindleLaser::displayPower = + cutter.upower_to_dpower(TERN(SPINDLE_LASER_PWM, SPEED_POWER_MIN, SPEED_POWER_STARTUP)); // spindle/laser speed/power control in PWM, Percentage or RPM + #if ENABLED(MARLIN_DEV_MODE) cutter_frequency_t SpindleLaser::frequency; // setting PWM frequency; range: 2K - 50K #endif @@ -65,70 +63,32 @@ void SpindleLaser::init() { #if ENABLED(SPINDLE_LASER_PWM) /** - * Set the cutter PWM directly to the given ocr value - **/ + * Set the cutter PWM directly to the given ocr value + */ void SpindleLaser::set_ocr(const uint8_t ocr) { WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_HIGH); // turn spindle on analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF); } -cutter_power_t SpindleLaser::translate_power(const float pwr) - { - float pwrpc; - #if CUTTER_DISPLAY_IS(PERCENT) - pwrpc = pwr; - #elif CUTTER_DISPLAY_IS(RPM) // RPM to percent - #if ENABLED(CUTTER_POWER_RELATIVE) - pwrpc = (pwr - SPEED_POWER_MIN) / (SPEED_POWER_MAX - SPEED_POWER_MIN) * 100; - #else - pwrpc = pwr / SPEED_POWER_MAX * 100; - #endif - #else - return pwr; // PWM - #endif - - #if ENABLED(SPINDLE_FEATURE) - #if ENABLED(CUTTER_POWER_RELATIVE) - constexpr float spmin = 0; - #else - constexpr float spmin = SPEED_POWER_MIN / SPEED_POWER_MAX * 100; // convert to percentage - #endif - constexpr float spmax = 100; - #else - constexpr float spmin = SPEED_POWER_MIN; - constexpr float spmax = SPEED_POWER_MAX; - #endif - - constexpr float inv_slope = RECIPROCAL(SPEED_POWER_SLOPE), - min_ocr = (spmin - (SPEED_POWER_INTERCEPT)) * inv_slope, // Minimum allowed - max_ocr = (spmax - (SPEED_POWER_INTERCEPT)) * inv_slope; // Maximum allowed - float ocr_val; - if (pwrpc < spmin) - ocr_val = min_ocr; // Use minimum if set below - else if (pwrpc > spmax) - ocr_val = max_ocr; // Use maximum if set above - else - ocr_val = (pwrpc - (SPEED_POWER_INTERCEPT)) * inv_slope; // Use calculated OCR value - return ocr_val; // ...limited to Atmel PWM max -} #endif // // Set cutter ON state (and PWM) to the given cutter power value // -static cutter_power_t last_power_applied = 0; -void SpindleLaser::apply_power(const cutter_power_t inpow) { - if (inpow == last_power_applied) return; - last_power_applied = inpow; +void SpindleLaser::apply_power(const cutter_power_t upwr) { + static cutter_power_t last_power_applied = 0; + if (upwr == last_power_applied) return; + last_power_applied = upwr; + power = upwr; #if ENABLED(SPINDLE_LASER_PWM) if (enabled()) - set_ocr(translate_power(inpow)); + set_ocr(unit_power_to_ocr(upwr)); else { - WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off - analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte + WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off + analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte } #else - WRITE(SPINDLE_LASER_ENA_PIN, (SPINDLE_LASER_ACTIVE_HIGH) ? enabled() : !enabled()); + WRITE(SPINDLE_LASER_ENA_PIN, enabled() == SPINDLE_LASER_ACTIVE_HIGH); #endif } diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 6aff8585627a..91df4c08e79d 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -34,29 +34,77 @@ #include "../module/planner.h" #endif +#ifndef SPEED_POWER_INTERCEPT + #define SPEED_POWER_INTERCEPT 0 +#endif + class SpindleLaser { public: - static bool isOn; // state to determine when to apply setPower to power - static cutter_power_t power; - static cutter_setPower_t setPower; // spindle/laser menu set power; in PWM, Percentage or RPM - #if ENABLED(MARLIN_DEV_MODE) - static cutter_frequency_t frequency; // set PWM frequency; range: 2K-50K - #endif + static constexpr float + min_pct = 100.0f * TERN(CUTTER_POWER_RELATIVE, 0, (SPEED_POWER_MIN)) / (SPEED_POWER_MAX), + max_pct = TERN(SPINDLE_FEATURE, 100, SPEED_POWER_MAX); + static constexpr float pct_to_ocr(float pct) { + return (pct - (SPEED_POWER_INTERCEPT)) * RECIPROCAL(SPEED_POWER_SLOPE); + } + static constexpr float min_ocr() { return pct_to_ocr(min_pct); } // Minimum allowed + static constexpr float max_ocr() { return pct_to_ocr(max_pct); } // Maximum allowed + + static constexpr float upower_to_percent(const float upwr) { + return ( + #if CUTTER_DISPLAY_IS(PERCENT) + upwr + #elif ENABLED(CUTTER_POWER_RELATIVE) // RPM / PWM below here + 100 * (upwr - (SPEED_POWER_MIN)) / ((SPEED_POWER_MAX) - (SPEED_POWER_MIN)) + #else + 100 * upwr / (SPEED_POWER_MAX) + #endif + ); + } - static cutter_setPower_t interpret_power(const float pwr) { // convert speed/power to configured PWM, Percentage or RPM in relative or normal range - #if CUTTER_DISPLAY_IS(PERCENT) - return (pwr / SPEED_POWER_MAX) * 100; // to percent - #elif CUTTER_DISPLAY_IS(RPM) // to RPM is unaltered - return pwr; - #else // to PWM - #if ENABLED(CUTTER_POWER_RELATIVE) - return (pwr - SPEED_POWER_MIN) / (SPEED_POWER_MAX - SPEED_POWER_MIN) * 255; // using rpm range as relative percentage + // Convert a given speed/power from the native unit + // to the display/edit unit: PWM, Percent, or RPM (rel/abs). + static constexpr + cutter_displayPower_t upower_to_dpower(const float upwr) { + return ( + #if CUTTER_DISPLAY_IS(RPM) // RPM is also the native unit + upwr + #elif CUTTER_DISPLAY_IS(PERCENT) // Anything to percent + 100 * upwr / (SPEED_POWER_MAX) + #elif ENABLED(CUTTER_POWER_RELATIVE) // Anything to PWM / OCR + map(upwr, SPEED_POWER_MIN, SPEED_POWER_MAX, 0, 255) // Scale allowed range to PWM #else - return (pwr / SPEED_POWER_MAX) * 255; + 255 * upwr / (SPEED_POWER_MAX) // Scale direct to PWM <= 255 #endif - #endif + ); + } + + static constexpr + cutter_power_t dpower_to_upower(const float dpwr) { + return ( + #if CUTTER_DISPLAY_IS(RPM) // RPM is also the native unit + dpwr + #elif CUTTER_DISPLAY_IS(PERCENT) // Percent to anything + dpwr * (SPEED_POWER_MAX) / 100 + #elif ENABLED(CUTTER_POWER_RELATIVE) // PWM / OCR to anything + map(dpwr, 0, 255, SPEED_POWER_MIN, SPEED_POWER_MAX) // Scale to allowed range + #else + dpwr * (SPEED_POWER_MAX) / 255 // Scaled PWM <= 255 + #endif + ); } + static constexpr cutter_displayPower_t dpower_min() { return upower_to_dpower(SPEED_POWER_MIN); } + static constexpr cutter_displayPower_t dpower_max() { return upower_to_dpower(SPEED_POWER_MAX); } + + static bool isOn; // State to determine when to apply power to OCR + static cutter_power_t power; + + #if ENABLED(MARLIN_DEV_MODE) + static cutter_frequency_t frequency; // Set PWM frequency; range: 2K-50K + #endif + + static cutter_displayPower_t displayPower; // Power as displayed in PWM, Percentage or RPM + static void init(); // Modifying this function should update everywhere @@ -70,13 +118,26 @@ class SpindleLaser { FORCE_INLINE static void refresh() { apply_power(power); } FORCE_INLINE static void set_power(const cutter_power_t pwr) { power = pwr; refresh(); } - static inline void set_enabled(const bool enable) { set_power(enable ? (power ?: interpret_power(SPEED_POWER_STARTUP)) : 0); } + static inline void set_enabled(const bool enable) { set_power(enable ? (power ?: SPEED_POWER_STARTUP) : 0); } #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 cutter_power_t translate_power(const float pwr); // Used by update output for power->OCR translation - #endif + + // Used to update output for power->OCR translation + static inline constexpr + uint8_t unit_power_to_ocr(const float &pwr) { + #if CUTTER_DISPLAY_IS(PWM255) + return pwr; + #else + return (upower_to_percent(pwr) < min_pct) ? min_ocr() // Use minimum if set below + : (upower_to_percent(pwr) > max_pct) ? max_ocr() // Use maximum if set above + : pct_to_ocr(upower_to_percent(pwr)); // Calculate OCR value + #endif + } + + #endif // SPINDLE_LASER_PWM // Wait for spindle to spin up or spin down static inline void power_delay(const bool on) { @@ -92,33 +153,59 @@ class SpindleLaser { #endif static inline void disable() { isOn = false; set_enabled(false); } + #if HAS_LCD_MENU - static inline void enable_forward() { isOn = true; setPower ? (power = setPower) : (setPower = interpret_power(SPEED_POWER_STARTUP)); set_direction(false); set_enabled(true); } - static inline void enable_reverse() { isOn = true; setPower ? (power = setPower) : (setPower = interpret_power(SPEED_POWER_STARTUP)); set_direction(true); set_enabled(true); } + static inline void enable_with_dir(const bool reverse) { + isOn = true; + const cutter_power_t upwr = dpower_to_upower(displayPower); + if (upwr) + power = upwr; + else + displayPower = upower_to_dpower(SPEED_POWER_STARTUP); + set_direction(reverse); + set_enabled(true); + } + FORCE_INLINE static void enable_forward() { enable_with_dir(false); } + FORCE_INLINE static void enable_reverse() { enable_with_dir(true); } #endif #if ENABLED(LASER_POWER_INLINE) + /** + * Inline power adds extra fields to the planner block + * to handle laser power and scale to movement speed. + */ + // Force disengage planner power control - static inline void inline_disable() { planner.settings.laser.status = 0; planner.settings.laser.power = 0; isOn = false;} + static inline void inline_disable() { + isOn = false; + planner.settings.laser.status = 0; + planner.settings.laser.power = 0; + } // Inline modes of all other functions; all enable planner inline power control - static inline void inline_enabled(const bool enable) { enable ? inline_power(SPEED_POWER_STARTUP) : inline_power(0); } + static inline void set_inline_enabled(const bool enable) { + if (enable) + inline_power(unit_power_to_ocr(SPEED_POWER_STARTUP)); + else + TERN(SPINDLE_LASER_PWM, inline_ocr_power, inline_power)(0); + } - static void inline_power(const cutter_power_t pwr) { + // Set the power for subsequent movement blocks + static void inline_power(const cutter_power_t upwr) { #if ENABLED(SPINDLE_LASER_PWM) - inline_ocr_power(translate_power(pwr)); + inline_ocr_power(unit_power_to_ocr(upwr)); #else - planner.settings.laser.status = enabled(pwr) ? 0x03 : 0x01; - planner.settings.laser.power = pwr; + planner.settings.laser.status = enabled(upwr) ? 0x03 : 0x01; + planner.settings.laser.power = upwr; #endif } static inline void inline_direction(const bool reverse) { UNUSED(reverse); } // TODO is this ever going to be needed #if ENABLED(SPINDLE_LASER_PWM) - static inline void inline_ocr_power(const uint8_t pwr) { - planner.settings.laser.status = pwr ? 0x03 : 0x01; - planner.settings.laser.power = pwr; + static inline void inline_ocr_power(const uint8_t ocrpwr) { + planner.settings.laser.status = ocrpwr ? 0x03 : 0x01; + planner.settings.laser.power = ocrpwr; } #endif #endif diff --git a/Marlin/src/feature/spindle_laser_types.h b/Marlin/src/feature/spindle_laser_types.h index 9e3f2bae48a8..2548383d5f28 100644 --- a/Marlin/src/feature/spindle_laser_types.h +++ b/Marlin/src/feature/spindle_laser_types.h @@ -34,17 +34,18 @@ #define _MSG_CUTTER(M) MSG_LASER_##M #endif #define MSG_CUTTER(M) _MSG_CUTTER(M) + +typedef IF<(SPEED_POWER_MAX>255), uint16_t, uint8_t>::type cutter_power_t; + #if CUTTER_DISPLAY_IS(RPM) && SPEED_POWER_MAX > 255 - #define cutter_power_t uint16_t - #define cutter_setPower_t uint16_t - #define CUTTER_MENU_POWER_TYPE uint16_5 + typedef uint16_t cutter_displayPower_t; + #define CUTTER_MENU_POWER_TYPE uint16_5 #else - #define cutter_power_t uint8_t - #define cutter_setPower_t uint8_t - #define CUTTER_MENU_POWER_TYPE uint8 + typedef uint8_t cutter_displayPower_t; + #define CUTTER_MENU_POWER_TYPE uint8 #endif #if ENABLED(MARLIN_DEV_MODE) - #define cutter_frequency_t uint16_t - #define CUTTER_MENU_FREQUENCY_TYPE uint16_5 + typedef uint16_t cutter_frequency_t; + #define CUTTER_MENU_FREQUENCY_TYPE uint16_5 #endif diff --git a/Marlin/src/gcode/control/M3-M5.cpp b/Marlin/src/gcode/control/M3-M5.cpp index 9c897abf01ae..a69b9af130e3 100644 --- a/Marlin/src/gcode/control/M3-M5.cpp +++ b/Marlin/src/gcode/control/M3-M5.cpp @@ -28,28 +28,18 @@ #include "../../feature/spindle_laser.h" #include "../../module/stepper.h" -inline cutter_power_t get_s_power() { - return cutter_power_t( - parser.intval('S', cutter.interpret_power(SPEED_POWER_STARTUP)) - ); -} - /** * Laser: - * * 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 + * Parameters: + * S - Set power. S0 will turn the spindle/laser off. + * O - Set power and OCR (oscillator count register) * * If no PWM pin is defined then M3/M4 just turns it on. * @@ -77,11 +67,16 @@ inline cutter_power_t get_s_power() { */ void GcodeSuite::M3_M4(const bool is_M4) { + auto get_s_power = []{ + return cutter_power_t( + parser.intval('S', cutter.upower_to_dpower(SPEED_POWER_STARTUP)) + ); + }; + #if ENABLED(LASER_POWER_INLINE) if (parser.seen('I') == DISABLED(LASER_POWER_INLINE_INVERT)) { // Laser power in inline mode cutter.inline_direction(is_M4); // Should always be unused - #if ENABLED(SPINDLE_LASER_PWM) if (parser.seen('O')) cutter.inline_ocr_power(parser.value_byte()); // The OCR is a value from 0 to 255 (uint8_t) @@ -97,7 +92,6 @@ void GcodeSuite::M3_M4(const bool is_M4) { #endif planner.synchronize(); // Wait for previous movement commands (G0/G0/G2/G3) to complete before changing power - cutter.set_direction(is_M4); #if ENABLED(SPINDLE_LASER_PWM) @@ -116,7 +110,7 @@ void GcodeSuite::M3_M4(const bool is_M4) { void GcodeSuite::M5() { #if ENABLED(LASER_POWER_INLINE) if (parser.seen('I') == DISABLED(LASER_POWER_INLINE_INVERT)) { - cutter.inline_enabled(false); // Laser power in inline mode + cutter.set_inline_enabled(false); // Laser power in inline mode return; } // Non-inline, standard case diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp index ac5a60ed9364..f672b5ef5170 100644 --- a/Marlin/src/gcode/gcode.cpp +++ b/Marlin/src/gcode/gcode.cpp @@ -181,12 +181,8 @@ void GcodeSuite::get_destination_from_command() { // Set the laser power in the planner to configure this move if (parser.seen('S')) cutter.inline_power(parser.value_int()); - else { - #if ENABLED(LASER_MOVE_G0_OFF) - if (parser.codenum == 0) // G0 - cutter.inline_enabled(false); - #endif - } + else if (ENABLED(LASER_MOVE_G0_OFF) && parser.codenum == 0) // G0 + cutter.set_inline_enabled(false); #endif } diff --git a/Marlin/src/lcd/menu/menu_spindle_laser.cpp b/Marlin/src/lcd/menu/menu_spindle_laser.cpp index 26cac933839e..fcf754c9f02b 100644 --- a/Marlin/src/lcd/menu/menu_spindle_laser.cpp +++ b/Marlin/src/lcd/menu/menu_spindle_laser.cpp @@ -40,9 +40,9 @@ BACK_ITEM(MSG_MAIN); #if ENABLED(SPINDLE_LASER_PWM) - EDIT_ITEM_FAST( CUTTER_MENU_POWER_TYPE, MSG_CUTTER(POWER), &cutter.setPower - , cutter.interpret_power(SPEED_POWER_MIN), cutter.interpret_power(SPEED_POWER_MAX) - , []{ if (cutter.isOn) cutter.power = cutter.setPower; } + EDIT_ITEM_FAST(CUTTER_MENU_POWER_TYPE, MSG_CUTTER(POWER), &cutter.displayPower + , cutter.dpower_min(), cutter.dpower_max() + , []{ if (cutter.isOn) cutter.power = cutter.displayPower; } ); #endif diff --git a/Marlin/src/module/planner.h b/Marlin/src/module/planner.h index f2b90cd99434..e5a9f7dd5c17 100644 --- a/Marlin/src/module/planner.h +++ b/Marlin/src/module/planner.h @@ -223,10 +223,10 @@ typedef struct block_t { uint8_t status; /** * Laser power: 0 or 255 in case of PWM-less laser, - * or the OCR value; + * or the OCR (oscillator count register) value; * - * Using OCR instead of raw power, - * as it avoids floating points during move loop + * Using OCR instead of raw power, because it avoids + * floating point operations during the move loop. */ uint8_t power; } settings_laser_t; diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index 07e7b5d8a74a..03ac345344eb 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -1873,8 +1873,10 @@ uint32_t Stepper::block_phase_isr() { if (!(current_block = planner.get_current_block())) return interval; // No more queued movements! } - #if DISABLED(LASER_POWER_INLINE) - TERN_(HAS_CUTTER, cutter.apply_power(current_block->cutter_power)); + + // For non-inline cutter, grossly apply power + #if HAS_CUTTER && DISABLED(LASER_POWER_INLINE) + cutter.apply_power(current_block->cutter_power); #endif TERN_(POWER_LOSS_RECOVERY, recovery.info.sdpos = current_block->sdpos); From a0da32e91e5458857c3e64c23bcb176364bb1808 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 9 May 2020 20:56:37 -0500 Subject: [PATCH 04/17] Other tweaks --- Marlin/src/lcd/dogm/status_screen_DOGM.cpp | 2 +- Marlin/src/lcd/menu/menu_spindle_laser.cpp | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Marlin/src/lcd/dogm/status_screen_DOGM.cpp b/Marlin/src/lcd/dogm/status_screen_DOGM.cpp index 8c67ff10ab32..042d10c0bed6 100644 --- a/Marlin/src/lcd/dogm/status_screen_DOGM.cpp +++ b/Marlin/src/lcd/dogm/status_screen_DOGM.cpp @@ -542,7 +542,7 @@ void MarlinUI::draw_status_screen() { // Laser / Spindle #if DO_DRAW_CUTTER if (cutter.power && PAGE_CONTAINS(STATUS_CUTTER_TEXT_Y - INFO_FONT_ASCENT, STATUS_CUTTER_TEXT_Y - 1)) { - lcd_put_u8str(STATUS_CUTTER_TEXT_X, STATUS_CUTTER_TEXT_Y, i16tostr3rj(cutter.power)); + lcd_put_u8str(STATUS_CUTTER_TEXT_X, STATUS_CUTTER_TEXT_Y, i16tostr3rj(cutter.upower_to_dpower(cutter.power))); #if CUTTER_DISPLAY_IS(PERCENT) lcd_put_wchar('%'); #elif CUTTER_DISPLAY_IS(RPM) diff --git a/Marlin/src/lcd/menu/menu_spindle_laser.cpp b/Marlin/src/lcd/menu/menu_spindle_laser.cpp index fcf754c9f02b..19ffb4289634 100644 --- a/Marlin/src/lcd/menu/menu_spindle_laser.cpp +++ b/Marlin/src/lcd/menu/menu_spindle_laser.cpp @@ -34,19 +34,21 @@ void menu_spindle_laser() { - const bool can_disable = cutter.enabled() && cutter.isOn; + const bool is_enabled = cutter.enabled() && cutter.isOn; START_MENU(); BACK_ITEM(MSG_MAIN); #if ENABLED(SPINDLE_LASER_PWM) + // Change the cutter's "current power" value without turning the cutter on or off + // Power is displayed in units and range according to config EDIT_ITEM_FAST(CUTTER_MENU_POWER_TYPE, MSG_CUTTER(POWER), &cutter.displayPower , cutter.dpower_min(), cutter.dpower_max() - , []{ if (cutter.isOn) cutter.power = cutter.displayPower; } + , []{ if (cutter.isOn) cutter.power = dpower_to_upower(cutter.displayPower); } ); #endif - if (can_disable) + if (is_enabled) ACTION_ITEM(MSG_CUTTER(OFF), cutter.disable); else { ACTION_ITEM(MSG_CUTTER(ON), cutter.enable_forward); @@ -57,7 +59,7 @@ #if ENABLED(MARLIN_DEV_MODE) #if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY) - EDIT_ITEM_FAST(CUTTER_MENU_FREQUENCY_TYPE, MSG_CUTTER_FREQUENCY, &cutter.frequency, 2000, 50000,[]{ cutter.refresh_frequency();}); + EDIT_ITEM_FAST(CUTTER_MENU_FREQUENCY_TYPE, MSG_CUTTER_FREQUENCY, &cutter.frequency, 2000, 50000,[]{ cutter.refresh_frequency(); }); #endif #endif END_MENU(); From 423ecfddbb28a0f063e806cf11031f3ec06cbcde Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 9 May 2020 21:07:46 -0500 Subject: [PATCH 05/17] Last tweaks --- Marlin/src/feature/spindle_laser.h | 38 ++++++++++++++++-------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 91df4c08e79d..b879c2df21d4 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -64,31 +64,33 @@ class SpindleLaser { // Convert a given speed/power from the native unit // to the display/edit unit: PWM, Percent, or RPM (rel/abs). static constexpr - cutter_displayPower_t upower_to_dpower(const float upwr) { + cutter_power_t dpower_to_upower(const float dpwr) { return ( #if CUTTER_DISPLAY_IS(RPM) // RPM is also the native unit - upwr - #elif CUTTER_DISPLAY_IS(PERCENT) // Anything to percent - 100 * upwr / (SPEED_POWER_MAX) - #elif ENABLED(CUTTER_POWER_RELATIVE) // Anything to PWM / OCR - map(upwr, SPEED_POWER_MIN, SPEED_POWER_MAX, 0, 255) // Scale allowed range to PWM + dpwr + #elif CUTTER_DISPLAY_IS(PERCENT) // Percent to anything + dpwr * (SPEED_POWER_MAX) / 100 + #elif ENABLED(CUTTER_POWER_RELATIVE) // PWM / OCR to anything + map(dpwr, 0, 255, SPEED_POWER_MIN, SPEED_POWER_MAX) // Scale to allowed range #else - 255 * upwr / (SPEED_POWER_MAX) // Scale direct to PWM <= 255 + dpwr * (SPEED_POWER_MAX) / 255 // Scaled PWM <= 255 #endif ); } + // Convert a given display/edit value to native unit, + // which can also be PWM, Percent, or RPM (rel/abs). static constexpr - cutter_power_t dpower_to_upower(const float dpwr) { + cutter_displayPower_t upower_to_dpower(const float upwr) { return ( #if CUTTER_DISPLAY_IS(RPM) // RPM is also the native unit - dpwr - #elif CUTTER_DISPLAY_IS(PERCENT) // Percent to anything - dpwr * (SPEED_POWER_MAX) / 100 - #elif ENABLED(CUTTER_POWER_RELATIVE) // PWM / OCR to anything - map(dpwr, 0, 255, SPEED_POWER_MIN, SPEED_POWER_MAX) // Scale to allowed range + upwr + #elif CUTTER_DISPLAY_IS(PERCENT) // Anything to percent + 100 * upwr / (SPEED_POWER_MAX) + #elif ENABLED(CUTTER_POWER_RELATIVE) // Anything to PWM / OCR + map(upwr, SPEED_POWER_MIN, SPEED_POWER_MAX, 0, 255) // Scale allowed range to PWM #else - dpwr * (SPEED_POWER_MAX) / 255 // Scaled PWM <= 255 + 255 * upwr / (SPEED_POWER_MAX) // Scale direct to PWM <= 255 #endif ); } @@ -96,14 +98,14 @@ class SpindleLaser { static constexpr cutter_displayPower_t dpower_min() { return upower_to_dpower(SPEED_POWER_MIN); } static constexpr cutter_displayPower_t dpower_max() { return upower_to_dpower(SPEED_POWER_MAX); } - static bool isOn; // State to determine when to apply power to OCR + static bool isOn; // State to determine when to apply power to OCR static cutter_power_t power; #if ENABLED(MARLIN_DEV_MODE) - static cutter_frequency_t frequency; // Set PWM frequency; range: 2K-50K + static cutter_frequency_t frequency; // Set PWM frequency; range: 2K-50K #endif - static cutter_displayPower_t displayPower; // Power as displayed in PWM, Percentage or RPM + static cutter_displayPower_t displayPower; // Power as displayed in PWM, Percentage or RPM static void init(); @@ -200,7 +202,7 @@ class SpindleLaser { #endif } - static inline void inline_direction(const bool reverse) { UNUSED(reverse); } // TODO is this ever going to be needed + static inline void inline_direction(const bool) { /* never */ } #if ENABLED(SPINDLE_LASER_PWM) static inline void inline_ocr_power(const uint8_t ocrpwr) { From e0a1073ba0f8db14ec5dc57cbfb9ba9d66e6d408 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 9 May 2020 23:57:02 -0500 Subject: [PATCH 06/17] Final bits, for sure --- Marlin/src/feature/spindle_laser.h | 6 ++++++ Marlin/src/lcd/menu/menu_spindle_laser.cpp | 5 ++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index b879c2df21d4..aa18d5244791 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -157,6 +157,7 @@ class SpindleLaser { static inline void disable() { isOn = false; set_enabled(false); } #if HAS_LCD_MENU + static inline void enable_with_dir(const bool reverse) { isOn = true; const cutter_power_t upwr = dpower_to_upower(displayPower); @@ -169,6 +170,11 @@ class SpindleLaser { } FORCE_INLINE static void enable_forward() { enable_with_dir(false); } FORCE_INLINE static void enable_reverse() { enable_with_dir(true); } + + #if ENABLED(SPINDLE_LASER_PWM) + static inline update_from_dpower() { if (isOn) power = dpower_to_upower(displayPower); } + #endif + #endif #if ENABLED(LASER_POWER_INLINE) diff --git a/Marlin/src/lcd/menu/menu_spindle_laser.cpp b/Marlin/src/lcd/menu/menu_spindle_laser.cpp index 19ffb4289634..6746670115d2 100644 --- a/Marlin/src/lcd/menu/menu_spindle_laser.cpp +++ b/Marlin/src/lcd/menu/menu_spindle_laser.cpp @@ -42,9 +42,8 @@ #if ENABLED(SPINDLE_LASER_PWM) // Change the cutter's "current power" value without turning the cutter on or off // Power is displayed in units and range according to config - EDIT_ITEM_FAST(CUTTER_MENU_POWER_TYPE, MSG_CUTTER(POWER), &cutter.displayPower - , cutter.dpower_min(), cutter.dpower_max() - , []{ if (cutter.isOn) cutter.power = dpower_to_upower(cutter.displayPower); } + EDIT_ITEM_FAST(CUTTER_MENU_POWER_TYPE, MSG_CUTTER(POWER), &cutter.displayPower, + cutter.dpower_min(), cutter.dpower_max(), cutter.update_from_dpower() ); #endif From 8d586d3b6a679158aa223faf771ad5dd3323a5e3 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 11 May 2020 01:19:59 -0500 Subject: [PATCH 07/17] Fix M3 S with L_P_INLINE + S_L_PWM --- Marlin/src/feature/spindle_laser.h | 33 ++++++++-------------- Marlin/src/gcode/control/M3-M5.cpp | 2 +- Marlin/src/inc/SanityCheck.h | 2 +- Marlin/src/lcd/menu/menu_spindle_laser.cpp | 4 +-- 4 files changed, 16 insertions(+), 25 deletions(-) diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index aa18d5244791..0af18c2f867d 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -37,6 +37,9 @@ #ifndef SPEED_POWER_INTERCEPT #define SPEED_POWER_INTERCEPT 0 #endif +#define SPEED_POWER_FLOOR TERN(CUTTER_POWER_RELATIVE, SPEED_POWER_MIN, 0) + +#define _MAP(N,S1,S2,D1,D2) ((N)*_MAX((D2)-(D1),0)/_MAX((S2)-(S1),1)+(D1)) class SpindleLaser { public: @@ -50,15 +53,7 @@ class SpindleLaser { static constexpr float max_ocr() { return pct_to_ocr(max_pct); } // Maximum allowed static constexpr float upower_to_percent(const float upwr) { - return ( - #if CUTTER_DISPLAY_IS(PERCENT) - upwr - #elif ENABLED(CUTTER_POWER_RELATIVE) // RPM / PWM below here - 100 * (upwr - (SPEED_POWER_MIN)) / ((SPEED_POWER_MAX) - (SPEED_POWER_MIN)) - #else - 100 * upwr / (SPEED_POWER_MAX) - #endif - ); + return _MAP(upwr, SPEED_POWER_FLOOR, SPEED_POWER_MAX, 0, 100); } // Convert a given speed/power from the native unit @@ -69,11 +64,9 @@ class SpindleLaser { #if CUTTER_DISPLAY_IS(RPM) // RPM is also the native unit dpwr #elif CUTTER_DISPLAY_IS(PERCENT) // Percent to anything - dpwr * (SPEED_POWER_MAX) / 100 - #elif ENABLED(CUTTER_POWER_RELATIVE) // PWM / OCR to anything - map(dpwr, 0, 255, SPEED_POWER_MIN, SPEED_POWER_MAX) // Scale to allowed range - #else - dpwr * (SPEED_POWER_MAX) / 255 // Scaled PWM <= 255 + _MAP(dpwr, 0, 100, 0, SPEED_POWER_MAX) + #else // PWM / OCR to allowed range + _MAP(dpwr, 0, 255, SPEED_POWER_FLOOR, SPEED_POWER_MAX) #endif ); } @@ -86,11 +79,9 @@ class SpindleLaser { #if CUTTER_DISPLAY_IS(RPM) // RPM is also the native unit upwr #elif CUTTER_DISPLAY_IS(PERCENT) // Anything to percent - 100 * upwr / (SPEED_POWER_MAX) - #elif ENABLED(CUTTER_POWER_RELATIVE) // Anything to PWM / OCR - map(upwr, SPEED_POWER_MIN, SPEED_POWER_MAX, 0, 255) // Scale allowed range to PWM - #else - 255 * upwr / (SPEED_POWER_MAX) // Scale direct to PWM <= 255 + _MAP(dpwr, 0, SPEED_POWER_MAX, 0, 100) + #else // Allowed range to PWM / OCR + _MAP(upwr, SPEED_POWER_MIN, SPEED_POWER_MAX, 0, 255) #endif ); } @@ -172,7 +163,7 @@ class SpindleLaser { FORCE_INLINE static void enable_reverse() { enable_with_dir(true); } #if ENABLED(SPINDLE_LASER_PWM) - static inline update_from_dpower() { if (isOn) power = dpower_to_upower(displayPower); } + static inline void update_from_dpower() { if (isOn) power = dpower_to_upower(displayPower); } #endif #endif @@ -193,7 +184,7 @@ class SpindleLaser { // Inline modes of all other functions; all enable planner inline power control static inline void set_inline_enabled(const bool enable) { if (enable) - inline_power(unit_power_to_ocr(SPEED_POWER_STARTUP)); + inline_power(SPEED_POWER_STARTUP); else TERN(SPINDLE_LASER_PWM, inline_ocr_power, inline_power)(0); } diff --git a/Marlin/src/gcode/control/M3-M5.cpp b/Marlin/src/gcode/control/M3-M5.cpp index a69b9af130e3..b421a5d95644 100644 --- a/Marlin/src/gcode/control/M3-M5.cpp +++ b/Marlin/src/gcode/control/M3-M5.cpp @@ -81,7 +81,7 @@ void GcodeSuite::M3_M4(const bool is_M4) { if (parser.seen('O')) cutter.inline_ocr_power(parser.value_byte()); // The OCR is a value from 0 to 255 (uint8_t) else - cutter.inline_power(get_s_power()); + cutter.inline_power(dpower_to_upower(get_s_power())); #else cutter.inline_enabled(true); #endif diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index c420740125fa..f9b0b452b9a9 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -2844,7 +2844,7 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2) #error "SPINDLE_LASER_PWM_PIN not assigned to a PWM pin." #elif !defined(SPINDLE_LASER_PWM_INVERT) #error "SPINDLE_LASER_PWM_INVERT is required for (SPINDLE|LASER)_FEATURE." - #elif !defined(SPEED_POWER_SLOPE) || !defined(SPEED_POWER_INTERCEPT) || !defined(SPEED_POWER_MIN) || !defined(SPEED_POWER_MAX) || !defined(SPEED_POWER_STARTUP) + #elif !(defined(SPEED_POWER_SLOPE) && defined(SPEED_POWER_INTERCEPT) && defined(SPEED_POWER_MIN) && defined(SPEED_POWER_MAX) && defined(SPEED_POWER_STARTUP)) #error "SPINDLE_LASER_PWM equation constant(s) missing." #elif _PIN_CONFLICT(X_MIN) #error "SPINDLE_LASER_PWM pin conflicts with X_MIN_PIN." diff --git a/Marlin/src/lcd/menu/menu_spindle_laser.cpp b/Marlin/src/lcd/menu/menu_spindle_laser.cpp index 6746670115d2..e7504122b616 100644 --- a/Marlin/src/lcd/menu/menu_spindle_laser.cpp +++ b/Marlin/src/lcd/menu/menu_spindle_laser.cpp @@ -43,7 +43,7 @@ // Change the cutter's "current power" value without turning the cutter on or off // Power is displayed in units and range according to config EDIT_ITEM_FAST(CUTTER_MENU_POWER_TYPE, MSG_CUTTER(POWER), &cutter.displayPower, - cutter.dpower_min(), cutter.dpower_max(), cutter.update_from_dpower() + cutter.dpower_min(), cutter.dpower_max(), cutter.update_from_dpower ); #endif @@ -58,7 +58,7 @@ #if ENABLED(MARLIN_DEV_MODE) #if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY) - EDIT_ITEM_FAST(CUTTER_MENU_FREQUENCY_TYPE, MSG_CUTTER_FREQUENCY, &cutter.frequency, 2000, 50000,[]{ cutter.refresh_frequency(); }); + EDIT_ITEM_FAST(CUTTER_MENU_FREQUENCY_TYPE, MSG_CUTTER_FREQUENCY, &cutter.frequency, 2000, 50000, cutter.refresh_frequency); #endif #endif END_MENU(); From f5b4dba6c7193eadd174de1704b2fd60f0ddd4d3 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Tue, 12 May 2020 00:19:47 -0500 Subject: [PATCH 08/17] oops --- Marlin/src/feature/spindle_laser.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 4e450322776c..4b49fcc46031 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -79,7 +79,7 @@ class SpindleLaser { #if CUTTER_DISPLAY_IS(RPM) // RPM is also the native unit upwr #elif CUTTER_DISPLAY_IS(PERCENT) // Anything to percent - _MAP(dpwr, 0, SPEED_POWER_MAX, 0, 100) + _MAP(upwr, 0, SPEED_POWER_MAX, 0, 100) #else // Allowed range to PWM / OCR _MAP(upwr, SPEED_POWER_MIN, SPEED_POWER_MAX, 0, 255) #endif From da31ff44b026fd0f54e4f20e210f013dad684165 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Tue, 12 May 2020 00:27:15 -0500 Subject: [PATCH 09/17] "upower" etc. --- Marlin/src/feature/spindle_laser.cpp | 2 +- Marlin/src/feature/spindle_laser.h | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Marlin/src/feature/spindle_laser.cpp b/Marlin/src/feature/spindle_laser.cpp index 4deb32e8db44..c420c8f25a33 100644 --- a/Marlin/src/feature/spindle_laser.cpp +++ b/Marlin/src/feature/spindle_laser.cpp @@ -82,7 +82,7 @@ void SpindleLaser::apply_power(const cutter_power_t upwr) { power = upwr; #if ENABLED(SPINDLE_LASER_PWM) if (enabled()) - set_ocr(unit_power_to_ocr(upwr)); + set_ocr(upower_to_ocr(upwr)); else { WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 4b49fcc46031..20d86a3e593b 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -46,7 +46,7 @@ class SpindleLaser { static constexpr float min_pct = 100.0f * TERN(CUTTER_POWER_RELATIVE, 0, (SPEED_POWER_MIN)) / (SPEED_POWER_MAX), max_pct = TERN(SPINDLE_FEATURE, 100, SPEED_POWER_MAX); - static constexpr float pct_to_ocr(float pct) { + static constexpr float pct_to_ocr(const float pct) { return (pct - (SPEED_POWER_INTERCEPT)) * RECIPROCAL(SPEED_POWER_SLOPE); } static constexpr float min_ocr() { return pct_to_ocr(min_pct); } // Minimum allowed @@ -101,32 +101,32 @@ class SpindleLaser { static void init(); // Modifying this function should update everywhere - static inline bool enabled(const cutter_power_t pwr) { return pwr > 0; } + static inline bool enabled(const cutter_power_t upwr) { return upwr > 0; } static inline bool enabled() { return enabled(power); } #if ENABLED(MARLIN_DEV_MODE) static inline void refresh_frequency() { set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency); } #endif - static void apply_power(const cutter_power_t inpow); + static void apply_power(const cutter_power_t upwr); FORCE_INLINE static void refresh() { apply_power(power); } - FORCE_INLINE static void set_power(const cutter_power_t pwr) { power = pwr; refresh(); } + FORCE_INLINE static void set_power(const cutter_power_t upwr) { power = upwr; refresh(); } static inline void set_enabled(const bool enable) { set_power(enable ? (power ?: SPEED_POWER_STARTUP) : 0); } #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 uint8_t ocr) { power = ocr; set_ocr(ocr); } // Used to update output for power->OCR translation static inline constexpr - uint8_t unit_power_to_ocr(const float &pwr) { + uint8_t upower_to_ocr(const float &upwr) { #if CUTTER_DISPLAY_IS(PWM255) - return pwr; + return upwr; #else - return (upower_to_percent(pwr) < min_pct) ? min_ocr() // Use minimum if set below - : (upower_to_percent(pwr) > max_pct) ? max_ocr() // Use maximum if set above - : pct_to_ocr(upower_to_percent(pwr)); // Calculate OCR value + return (upower_to_percent(upwr) < min_pct) ? min_ocr() // Use minimum if set below + : (upower_to_percent(upwr) > max_pct) ? max_ocr() // Use maximum if set above + : pct_to_ocr(upower_to_percent(upwr)); // Calculate OCR value #endif } @@ -192,7 +192,7 @@ class SpindleLaser { // Set the power for subsequent movement blocks static void inline_power(const cutter_power_t upwr) { #if ENABLED(SPINDLE_LASER_PWM) - inline_ocr_power(unit_power_to_ocr(upwr)); + inline_ocr_power(upower_to_ocr(upwr)); #else planner.laser.status = enabled(upwr) ? 0x03 : 0x01; planner.laser.power = upwr; From 505fe85fd11b51b7d1e799a6bd7876c7d7a65b01 Mon Sep 17 00:00:00 2001 From: shitcreek Date: Sat, 23 May 2020 16:58:57 -0500 Subject: [PATCH 10/17] Laser / Spindle fixes cpower = power set in config_adv ie SPEED_POWER_MAX | for spindle it is in RPM, for laser it is in percent. upower = unit power = set by CUTTER_POWER_UNIT in PWM, percent or RPM power_to_range will take any value and puts it within the min and max power range. In Relative mode, M3 S0 will not turn off the spndle/laser except if unit power is in RPM. Menu power = laser/spindle control via LCD menu. Note: The dial can set the laser/spindle in the off state. Whe the laser is on, it can adjust the power, and turn off the power when dialed to zero. --- Marlin/Configuration_adv.h | 6 +- Marlin/src/feature/spindle_laser.cpp | 57 ++++--- Marlin/src/feature/spindle_laser.h | 190 +++++++++++++-------- Marlin/src/feature/spindle_laser_types.h | 8 +- Marlin/src/gcode/control/M3-M5.cpp | 44 +++-- Marlin/src/gcode/gcode.cpp | 5 +- Marlin/src/inc/Conditionals_adv.h | 10 +- Marlin/src/inc/SanityCheck.h | 10 +- Marlin/src/lcd/dogm/status_screen_DOGM.cpp | 13 +- Marlin/src/lcd/menu/menu_main.cpp | 2 +- Marlin/src/lcd/menu/menu_spindle_laser.cpp | 7 +- Marlin/src/module/stepper.cpp | 2 +- 12 files changed, 211 insertions(+), 143 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 468a8482a64a..8d9c4a8296eb 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -2735,7 +2735,7 @@ #if EITHER(SPINDLE_FEATURE, LASER_FEATURE) #define SPINDLE_LASER_ACTIVE_HIGH false // Set to "true" if the on/off function is active HIGH #define SPINDLE_LASER_PWM true // Set to "true" if your controller supports setting the speed/power - #define SPINDLE_LASER_PWM_INVERT true // Set to "true" if the speed/power goes up when you want it to go slower + #define SPINDLE_LASER_PWM_INVERT false // Set to "true" if the speed/power goes up when you want it to go slower #define SPINDLE_LASER_FREQUENCY 2500 // (Hz) Spindle/laser frequency (only on supported HALs: AVR and LPC) @@ -2745,7 +2745,7 @@ * - PERCENT (S0 - S100) * - RPM (S0 - S50000) Best for use with a spindle */ - #define CUTTER_POWER_DISPLAY PWM255 + #define CUTTER_POWER_UNIT PWM255 /** * Relative Cutter Power @@ -2774,7 +2774,6 @@ * Speed/Power = (PWMDC / 255 * 100 - SPEED_POWER_INTERCEPT) / SPEED_POWER_SLOPE * PWMDC = (spdpwr - SPEED_POWER_MIN) / (SPEED_POWER_MAX - SPEED_POWER_MIN) / SPEED_POWER_SLOPE */ - #define SPEED_POWER_SLOPE (float(SPEED_POWER_MAX) / 255) #define SPEED_POWER_INTERCEPT 0 // (%) 0-100 i.e., Minimum power percentage #define SPEED_POWER_MIN 5000 // (RPM) #define SPEED_POWER_MAX 30000 // (RPM) SuperPID router controller 0 - 30,000 RPM @@ -2782,7 +2781,6 @@ #else - #define SPEED_POWER_SLOPE (float(SPEED_POWER_MAX) / 255) #define SPEED_POWER_INTERCEPT 0 // (%) 0-100 i.e., Minimum power percentage #define SPEED_POWER_MIN 0 // (%) 0-100 #define SPEED_POWER_MAX 100 // (%) 0-100 diff --git a/Marlin/src/feature/spindle_laser.cpp b/Marlin/src/feature/spindle_laser.cpp index 4deb32e8db44..9534fbea60f7 100644 --- a/Marlin/src/feature/spindle_laser.cpp +++ b/Marlin/src/feature/spindle_laser.cpp @@ -31,14 +31,13 @@ #include "spindle_laser.h" SpindleLaser cutter; - -cutter_power_t SpindleLaser::power; -bool SpindleLaser::isOn; // state to determine when to apply displayPower to power -cutter_displayPower_t SpindleLaser::displayPower = - cutter.upower_to_dpower(TERN(SPINDLE_LASER_PWM, SPEED_POWER_MIN, SPEED_POWER_STARTUP)); // spindle/laser speed/power control in PWM, Percentage or RPM +uint8_t SpindleLaser::power; +bool SpindleLaser::isOn; // state to determine when to apply menuPower to power +cutter_power_t SpindleLaser::menuPower; // Power set via LCD menu in PWM, PERCENT, or RPM +cutter_power_t SpindleLaser::unitPower; // LCD status power in PWM, PERCENT, or RPM #if ENABLED(MARLIN_DEV_MODE) - cutter_frequency_t SpindleLaser::frequency; // setting PWM frequency; range: 2K - 50K + cutter_frequency_t SpindleLaser::frequency; // setting PWM frequency; range: 2K - 50K #endif #define SPINDLE_LASER_PWM_OFF ((SPINDLE_LASER_PWM_INVERT) ? 255 : 0) @@ -46,13 +45,13 @@ cutter_displayPower_t SpindleLaser::displayPower = // Init the cutter to a safe OFF state // void SpindleLaser::init() { - OUT_WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Init spindle to off + 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) + OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // Init rotation to clockwise (M3) #endif #if ENABLED(SPINDLE_LASER_PWM) SET_PWM(SPINDLE_LASER_PWM_PIN); - analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // set to lowest speed + analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // set to lowest speed #endif #if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY) set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY); @@ -61,39 +60,48 @@ void SpindleLaser::init() { } #if ENABLED(SPINDLE_LASER_PWM) - /** * Set the cutter PWM directly to the given ocr value */ void SpindleLaser::set_ocr(const uint8_t ocr) { - WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_HIGH); // turn spindle on + WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_HIGH); // turn spindle on analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF); } - + void SpindleLaser::ocr_off() { + WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off + analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte + } #endif // -// Set cutter ON state (and PWM) to the given cutter power value +// Set cutter ON/OFF state (and PWM) to the given cutter power value // -void SpindleLaser::apply_power(const cutter_power_t upwr) { - static cutter_power_t last_power_applied = 0; - if (upwr == last_power_applied) return; - last_power_applied = upwr; - power = upwr; - #if ENABLED(SPINDLE_LASER_PWM) - if (enabled()) - set_ocr(unit_power_to_ocr(upwr)); +void SpindleLaser::apply_power(const uint8_t opwr) { + static uint8_t last_power_applied = 0; + if (opwr == last_power_applied) return; + last_power_applied = opwr; + power = opwr; + #if ENABLED(SPINDLE_LASER_PWM) + if (cutter.unitPower == 0 && CUTTER_UNIT_IS(RPM)){ + ocr_off(); + isOn = false; + } + else if (enabled() || (ENABLED(CUTTER_POWER_RELATIVE))){ + set_ocr(power); + isOn = true; + } else { - WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off - analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte + ocr_off(); + isOn = false; } #else + SERIAL_ECHO(" -- D - "); WRITE(SPINDLE_LASER_ENA_PIN, enabled() == SPINDLE_LASER_ACTIVE_HIGH); + isOn = true; #endif } #if ENABLED(SPINDLE_CHANGE_DIR) - // // Set the spindle direction and apply immediately // Stop on direction change if SPINDLE_STOP_ON_DIR_CHANGE is enabled @@ -103,7 +111,6 @@ void SpindleLaser::apply_power(const cutter_power_t upwr) { if (TERN0(SPINDLE_STOP_ON_DIR_CHANGE, enabled()) && READ(SPINDLE_DIR_PIN) != dir_state) disable(); WRITE(SPINDLE_DIR_PIN, dir_state); } - #endif #endif // HAS_CUTTER diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 0af18c2f867d..14f5b96bed57 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -39,95 +39,135 @@ #endif #define SPEED_POWER_FLOOR TERN(CUTTER_POWER_RELATIVE, SPEED_POWER_MIN, 0) -#define _MAP(N,S1,S2,D1,D2) ((N)*_MAX((D2)-(D1),0)/_MAX((S2)-(S1),1)+(D1)) +// #define _MAP(N,S1,S2,D1,D2) ((N)*_MAX((D2)-(D1),0)/_MAX((S2)-(S1),1)+(D1)) class SpindleLaser { public: static constexpr float - min_pct = 100.0f * TERN(CUTTER_POWER_RELATIVE, 0, (SPEED_POWER_MIN)) / (SPEED_POWER_MAX), - max_pct = TERN(SPINDLE_FEATURE, 100, SPEED_POWER_MAX); - static constexpr float pct_to_ocr(float pct) { - return (pct - (SPEED_POWER_INTERCEPT)) * RECIPROCAL(SPEED_POWER_SLOPE); - } - static constexpr float min_ocr() { return pct_to_ocr(min_pct); } // Minimum allowed - static constexpr float max_ocr() { return pct_to_ocr(max_pct); } // Maximum allowed + min_pct = round(TERN(CUTTER_POWER_RELATIVE, 0, (100 * float(SPEED_POWER_MIN) / TERN(SPINDLE_FEATURE, float(SPEED_POWER_MAX), 100)))), + max_pct = round(TERN(SPINDLE_FEATURE, 100, float(SPEED_POWER_MAX))); - static constexpr float upower_to_percent(const float upwr) { - return _MAP(upwr, SPEED_POWER_FLOOR, SPEED_POWER_MAX, 0, 100); - } + static const inline uint8_t pct_to_ocr(const float pct) { return uint8_t(pct * 255 / 100); } - // Convert a given speed/power from the native unit - // to the display/edit unit: PWM, Percent, or RPM (rel/abs). - static constexpr - cutter_power_t dpower_to_upower(const float dpwr) { - return ( - #if CUTTER_DISPLAY_IS(RPM) // RPM is also the native unit - dpwr - #elif CUTTER_DISPLAY_IS(PERCENT) // Percent to anything - _MAP(dpwr, 0, 100, 0, SPEED_POWER_MAX) - #else // PWM / OCR to allowed range - _MAP(dpwr, 0, 255, SPEED_POWER_FLOOR, SPEED_POWER_MAX) - #endif - ); + // cpower = configured values (ie SPEED_POWER_MAX) + static const inline uint8_t cpwr_to_pct(const cutter_cpower_t cpwr) { // configured value to pct + uint8_t pct = (unitPower == 0) ? 0 : round(100 * (cpwr - SPEED_POWER_FLOOR) / (SPEED_POWER_MAX - SPEED_POWER_FLOOR)); + return pct; } - // Convert a given display/edit value to native unit, - // which can also be PWM, Percent, or RPM (rel/abs). - static constexpr - cutter_displayPower_t upower_to_dpower(const float upwr) { - return ( - #if CUTTER_DISPLAY_IS(RPM) // RPM is also the native unit - upwr - #elif CUTTER_DISPLAY_IS(PERCENT) // Anything to percent - _MAP(dpwr, 0, SPEED_POWER_MAX, 0, 100) - #else // Allowed range to PWM / OCR - _MAP(upwr, SPEED_POWER_MIN, SPEED_POWER_MAX, 0, 255) + // Convert a configured value (cpower)(ie SPEED_POWER_STARTUP) to unit power (upwr, upower), + // which can be PWM, Percent, or RPM (rel/abs). + static const inline cutter_power_t cpwr_to_upwr(const cutter_cpower_t cpwr){ // STARTUP power to Unit power + const cutter_power_t upwr = ( + // Spindle configured values are in RPM + #if ENABLED(SPINDLE_FEATURE) + #if CUTTER_UNIT_IS(RPM) + cpwr // to RPM + #elif CUTTER_UNIT_IS(PERCENT) // to PCT + cpwr_to_pct(cpwr) + #else // to PWM + cpwr_to_pct(cpwr) * 255 / 100 + #endif + // Laser configured values are in PCT + #else + #if CUTTER_UNIT_IS(PWM255) + cpwr * 255 / 100 + #else + cpwr // to RPM/PCT + #endif #endif ); + return upwr; } - static constexpr cutter_displayPower_t dpower_min() { return upower_to_dpower(SPEED_POWER_MIN); } - static constexpr cutter_displayPower_t dpower_max() { return upower_to_dpower(SPEED_POWER_MAX); } + static const cutter_power_t mpower_min() { return cpwr_to_upwr(SPEED_POWER_MIN); } + static const cutter_power_t mpower_max() { return cpwr_to_upwr(SPEED_POWER_MAX); } static bool isOn; // State to determine when to apply power to OCR - static cutter_power_t power; + static uint8_t power; #if ENABLED(MARLIN_DEV_MODE) static cutter_frequency_t frequency; // Set PWM frequency; range: 2K-50K #endif - static cutter_displayPower_t displayPower; // Power as displayed in PWM, Percentage or RPM + static cutter_power_t menuPower; // Power as set via LCD menu in PWM, Percentage or RPM + static cutter_power_t unitPower; // Power as displayed status in PWM, Percentage or RPM static void init(); - // Modifying this function should update everywhere - static inline bool enabled(const cutter_power_t pwr) { return pwr > 0; } - static inline bool enabled() { return enabled(power); } #if ENABLED(MARLIN_DEV_MODE) static inline void refresh_frequency() { set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency); } #endif - static void apply_power(const cutter_power_t inpow); + + // Modifying this function should update everywhere + static inline bool enabled(const cutter_power_t opwr) { return opwr > 0; } + static inline bool enabled() { return enabled(power); } + + static void apply_power(const uint8_t inpow); FORCE_INLINE static void refresh() { apply_power(power); } - FORCE_INLINE static void set_power(const cutter_power_t pwr) { power = pwr; refresh(); } + FORCE_INLINE static void set_power(const uint8_t upwr) { power = upwr; refresh(); } - static inline void set_enabled(const bool enable) { set_power(enable ? (power ?: SPEED_POWER_STARTUP) : 0); } + static inline void set_enabled(const bool enable) { set_power(enable ? (power ?: (unitPower == 0 ? 0 : upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP)))) : 0);} #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 uint8_t ocr) { power = ocr; set_ocr(ocr); } + static void ocr_off(); // Used to update output for power->OCR translation - static inline constexpr - uint8_t unit_power_to_ocr(const float &pwr) { - #if CUTTER_DISPLAY_IS(PWM255) - return pwr; - #else - return (upower_to_percent(pwr) < min_pct) ? min_ocr() // Use minimum if set below - : (upower_to_percent(pwr) > max_pct) ? max_ocr() // Use maximum if set above - : pct_to_ocr(upower_to_percent(pwr)); // Calculate OCR value - #endif + static inline uint8_t upower_to_ocr(const cutter_power_t upwr) { + return ( + #if CUTTER_UNIT_IS(PWM255) + uint8_t(upwr) + #elif CUTTER_UNIT_IS(PERCENT) + pct_to_ocr(upwr) + #else + uint8_t(pct_to_ocr(cpwr_to_pct(upwr))) + #endif + ); + } + + // Correct power to configured range + static inline cutter_power_t power_to_range(const cutter_power_t pwr) { + return ( + #if CUTTER_UNIT_IS(PWM255) + power_to_range(pwr, 0) + #elif CUTTER_UNIT_IS(PERCENT) + power_to_range(pwr, 1) + #elif CUTTER_UNIT_IS(RPM) + power_to_range(pwr, 2) + #endif + ); + } + static inline cutter_power_t power_to_range(const cutter_power_t pwr , const uint8_t pwrUnit) { + if (pwr <= 0) return 0; + cutter_power_t upwr; + switch (pwrUnit) { + case 0: // PWM + upwr = ( + (pwr < pct_to_ocr(min_pct)) ? pct_to_ocr(min_pct) // Use minimum if set below + : (pwr > pct_to_ocr(max_pct)) ? pct_to_ocr(max_pct) // Use maximum if set above + : pwr + ); + break; + case 1: // PERCENT + upwr = ( + (pwr < min_pct) ? min_pct // Use minimum if set below + : (pwr > max_pct) ? max_pct // Use maximum if set above + : pwr // PCT + ); + break; + case 2: // RPM + upwr = ( + (pwr < SPEED_POWER_MIN) ? SPEED_POWER_MIN // Use minimum if set below + : (pwr > SPEED_POWER_MAX) ? SPEED_POWER_MAX // Use maximum if set above + : pwr // Calculate OCR value + ); + break; + default: break; + } + return upwr; } #endif // SPINDLE_LASER_PWM @@ -151,11 +191,12 @@ class SpindleLaser { static inline void enable_with_dir(const bool reverse) { isOn = true; - const cutter_power_t upwr = dpower_to_upower(displayPower); - if (upwr) - power = upwr; + const uint8_t ocr = upower_to_ocr(menuPower); + if (menuPower) + power = ocr; else - displayPower = upower_to_dpower(SPEED_POWER_STARTUP); + menuPower = cpwr_to_upwr(SPEED_POWER_STARTUP); + unitPower = menuPower; set_direction(reverse); set_enabled(true); } @@ -163,7 +204,8 @@ class SpindleLaser { FORCE_INLINE static void enable_reverse() { enable_with_dir(true); } #if ENABLED(SPINDLE_LASER_PWM) - static inline void update_from_dpower() { if (isOn) power = dpower_to_upower(displayPower); } + static inline void update_from_mpower() { if (isOn) power = upower_to_ocr(menuPower); unitPower = menuPower; + } #endif #endif @@ -175,27 +217,37 @@ class SpindleLaser { */ // Force disengage planner power control - static inline void inline_disable() { + static inline void inline_disable() + { isOn = false; + unitPower = 0; planner.settings.laser.status = 0; planner.settings.laser.power = 0; } // Inline modes of all other functions; all enable planner inline power control static inline void set_inline_enabled(const bool enable) { - if (enable) - inline_power(SPEED_POWER_STARTUP); - else - TERN(SPINDLE_LASER_PWM, inline_ocr_power, inline_power)(0); + if (enable) {inline_power(cpwr_to_upwr(SPEED_POWER_STARTUP));} + else { unitPower = 0; isOn = false; menuPower = 0; TERN(SPINDLE_LASER_PWM, inline_ocr_power, inline_power)(0);} } // Set the power for subsequent movement blocks static void inline_power(const cutter_power_t upwr) { + unitPower = upwr; + menuPower = unitPower; #if ENABLED(SPINDLE_LASER_PWM) - inline_ocr_power(unit_power_to_ocr(upwr)); + isOn = true; + #if ENABLED(SPEED_POWER_RELATIVE) && !CUTTER_UNIT_IS(RPM) // relative mode does not turn laser off at 0, except for RPM + planner.settings.laser.status = 0x03; + planner.settings.laser.power = upower_to_ocr(upwr); + #else + if (upwr > 0) + inline_ocr_power(upower_to_ocr(upwr)); + #endif #else planner.settings.laser.status = enabled(upwr) ? 0x03 : 0x01; planner.settings.laser.power = upwr; + isOn = enabled(upwr); #endif } @@ -204,10 +256,10 @@ class SpindleLaser { #if ENABLED(SPINDLE_LASER_PWM) static inline void inline_ocr_power(const uint8_t ocrpwr) { planner.settings.laser.status = ocrpwr ? 0x03 : 0x01; - planner.settings.laser.power = ocrpwr; + planner.settings.laser.power = ocrpwr; } #endif - #endif + #endif // LASER_POWER_INLINE static inline void kill() { TERN_(LASER_POWER_INLINE, inline_disable()); diff --git a/Marlin/src/feature/spindle_laser_types.h b/Marlin/src/feature/spindle_laser_types.h index 2548383d5f28..ad20d4c88a66 100644 --- a/Marlin/src/feature/spindle_laser_types.h +++ b/Marlin/src/feature/spindle_laser_types.h @@ -35,13 +35,13 @@ #endif #define MSG_CUTTER(M) _MSG_CUTTER(M) -typedef IF<(SPEED_POWER_MAX>255), uint16_t, uint8_t>::type cutter_power_t; +typedef IF<(SPEED_POWER_MAX > 255), uint16_t, uint8_t>::type cutter_cpower_t; -#if CUTTER_DISPLAY_IS(RPM) && SPEED_POWER_MAX > 255 - typedef uint16_t cutter_displayPower_t; +#if CUTTER_UNIT_IS(RPM) && SPEED_POWER_MAX > 255 + typedef uint16_t cutter_power_t; #define CUTTER_MENU_POWER_TYPE uint16_5 #else - typedef uint8_t cutter_displayPower_t; + typedef uint8_t cutter_power_t; #define CUTTER_MENU_POWER_TYPE uint8 #endif diff --git a/Marlin/src/gcode/control/M3-M5.cpp b/Marlin/src/gcode/control/M3-M5.cpp index b421a5d95644..00021c1945f1 100644 --- a/Marlin/src/gcode/control/M3-M5.cpp +++ b/Marlin/src/gcode/control/M3-M5.cpp @@ -38,7 +38,7 @@ * M4 - Spindle ON (Counter-clockwise) * * Parameters: - * S - Set power. S0 will turn the spindle/laser off. + * S - Set power. S0 will turn the spindle/laser off, except in relative mode. * O - Set power and OCR (oscillator count register) * * If no PWM pin is defined then M3/M4 just turns it on. @@ -66,24 +66,28 @@ * PWM duty cycle goes from 0 (off) to 255 (always on). */ void GcodeSuite::M3_M4(const bool is_M4) { - - auto get_s_power = []{ - return cutter_power_t( - parser.intval('S', cutter.upower_to_dpower(SPEED_POWER_STARTUP)) - ); + auto get_s_power = [] { + if (parser.seen('S')) + cutter.unitPower = cutter.power_to_range(cutter_power_t(round(parser.value_float()))); + else + cutter.unitPower = cutter.cpwr_to_upwr(SPEED_POWER_STARTUP); + return cutter.unitPower; }; - #if ENABLED(LASER_POWER_INLINE) +#if ENABLED(LASER_POWER_INLINE) if (parser.seen('I') == DISABLED(LASER_POWER_INLINE_INVERT)) { // Laser power in inline mode cutter.inline_direction(is_M4); // Should always be unused #if ENABLED(SPINDLE_LASER_PWM) - if (parser.seen('O')) - cutter.inline_ocr_power(parser.value_byte()); // The OCR is a value from 0 to 255 (uint8_t) - else - cutter.inline_power(dpower_to_upower(get_s_power())); + if (parser.seen('O')){ + cutter.unitPower = cutter.power_to_range(parser.value_byte(), 0); + cutter.inline_ocr_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t) + } + else { + cutter.inline_power(cutter.upower_to_ocr(get_s_power())); + } #else - cutter.inline_enabled(true); + cutter.inline_enabled(true); #endif return; } @@ -95,13 +99,17 @@ void GcodeSuite::M3_M4(const bool is_M4) { cutter.set_direction(is_M4); #if ENABLED(SPINDLE_LASER_PWM) - 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(get_s_power()); + if (parser.seenval('O')){ + cutter.unitPower = cutter.power_to_range(parser.value_byte(), 0); + cutter.set_ocr_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t) + } + else { + cutter.set_power(cutter.upower_to_ocr(get_s_power())); + } #else cutter.set_enabled(true); #endif + cutter.menuPower = cutter.unitPower; } /** @@ -112,12 +120,12 @@ void GcodeSuite::M5() { if (parser.seen('I') == DISABLED(LASER_POWER_INLINE_INVERT)) { cutter.set_inline_enabled(false); // Laser power in inline mode return; - } - // Non-inline, standard case + } // Non-inline, standard case cutter.inline_disable(); // Prevent future blocks re-setting the power #endif planner.synchronize(); cutter.set_enabled(false); + cutter.menuPower = cutter.unitPower; } #endif // HAS_CUTTER diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp index f672b5ef5170..abfd8ba69b68 100644 --- a/Marlin/src/gcode/gcode.cpp +++ b/Marlin/src/gcode/gcode.cpp @@ -179,8 +179,9 @@ void GcodeSuite::get_destination_from_command() { #if ENABLED(LASER_MOVE_POWER) // Set the laser power in the planner to configure this move - if (parser.seen('S')) - cutter.inline_power(parser.value_int()); + if (parser.seen('S')){ + cutter.inline_power(cutter.power_to_range(cutter_power_t(round(parser.value_float())))); + } else if (ENABLED(LASER_MOVE_G0_OFF) && parser.codenum == 0) // G0 cutter.set_inline_enabled(false); #endif diff --git a/Marlin/src/inc/Conditionals_adv.h b/Marlin/src/inc/Conditionals_adv.h index 2dd9f95d8187..88d0f9252e78 100644 --- a/Marlin/src/inc/Conditionals_adv.h +++ b/Marlin/src/inc/Conditionals_adv.h @@ -139,11 +139,11 @@ // #if EITHER(SPINDLE_FEATURE, LASER_FEATURE) #define HAS_CUTTER 1 - #define _CUTTER_DISP_PWM255 1 - #define _CUTTER_DISP_PERCENT 2 - #define _CUTTER_DISP_RPM 3 - #define _CUTTER_DISP(V) _CAT(_CUTTER_DISP_, V) - #define CUTTER_DISPLAY_IS(V) (_CUTTER_DISP(CUTTER_POWER_DISPLAY) == _CUTTER_DISP(V)) + #define _CUTTER_POWER_PWM255 1 + #define _CUTTER_POWER_PERCENT 2 + #define _CUTTER_POWER_RPM 3 + #define _CUTTER_POWER(V) _CAT(_CUTTER_POWER_, V) + #define CUTTER_UNIT_IS(V) (_CUTTER_POWER(CUTTER_POWER_UNIT) == _CUTTER_POWER(V)) #endif // Add features that need hardware PWM here diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index f9b0b452b9a9..e35526b30dd1 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -2791,10 +2791,10 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2) #endif #if HAS_CUTTER - #ifndef CUTTER_POWER_DISPLAY - #error "CUTTER_POWER_DISPLAY is required with a spindle or laser. Please update your Configuration_adv.h." - #elif !CUTTER_DISPLAY_IS(PWM255) && !CUTTER_DISPLAY_IS(PERCENT) && !CUTTER_DISPLAY_IS(RPM) - #error "CUTTER_POWER_DISPLAY must be PWM255, PERCENT, or RPM. Please update your Configuration_adv.h." + #ifndef CUTTER_POWER_UNIT + #error "CUTTER_POWER_UNIT is required with a spindle or laser. Please update your Configuration_adv.h." + #elif !CUTTER_UNIT_IS(PWM255) && !CUTTER_UNIT_IS(PERCENT) && !CUTTER_UNIT_IS(RPM) + #error "CUTTER_POWER_UNIT must be PWM255, PERCENT, or RPM. Please update your Configuration_adv.h." #endif #if ENABLED(LASER_POWER_INLINE) @@ -2844,7 +2844,7 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2) #error "SPINDLE_LASER_PWM_PIN not assigned to a PWM pin." #elif !defined(SPINDLE_LASER_PWM_INVERT) #error "SPINDLE_LASER_PWM_INVERT is required for (SPINDLE|LASER)_FEATURE." - #elif !(defined(SPEED_POWER_SLOPE) && defined(SPEED_POWER_INTERCEPT) && defined(SPEED_POWER_MIN) && defined(SPEED_POWER_MAX) && defined(SPEED_POWER_STARTUP)) + #elif !(defined(SPEED_POWER_INTERCEPT) && defined(SPEED_POWER_MIN) && defined(SPEED_POWER_MAX) && defined(SPEED_POWER_STARTUP)) #error "SPINDLE_LASER_PWM equation constant(s) missing." #elif _PIN_CONFLICT(X_MIN) #error "SPINDLE_LASER_PWM pin conflicts with X_MIN_PIN." diff --git a/Marlin/src/lcd/dogm/status_screen_DOGM.cpp b/Marlin/src/lcd/dogm/status_screen_DOGM.cpp index 042d10c0bed6..a57ca9e13503 100644 --- a/Marlin/src/lcd/dogm/status_screen_DOGM.cpp +++ b/Marlin/src/lcd/dogm/status_screen_DOGM.cpp @@ -541,12 +541,15 @@ void MarlinUI::draw_status_screen() { // Laser / Spindle #if DO_DRAW_CUTTER - if (cutter.power && PAGE_CONTAINS(STATUS_CUTTER_TEXT_Y - INFO_FONT_ASCENT, STATUS_CUTTER_TEXT_Y - 1)) { - lcd_put_u8str(STATUS_CUTTER_TEXT_X, STATUS_CUTTER_TEXT_Y, i16tostr3rj(cutter.upower_to_dpower(cutter.power))); - #if CUTTER_DISPLAY_IS(PERCENT) + if (cutter.isOn && PAGE_CONTAINS(STATUS_CUTTER_TEXT_Y - INFO_FONT_ASCENT, STATUS_CUTTER_TEXT_Y - 1)) { + #if CUTTER_UNIT_IS(PERCENT) + lcd_put_u8str(STATUS_CUTTER_TEXT_X, STATUS_CUTTER_TEXT_Y, i16tostr3rj(cutter.unitPower)); lcd_put_wchar('%'); - #elif CUTTER_DISPLAY_IS(RPM) - lcd_put_wchar('K'); + #elif CUTTER_UNIT_IS(RPM) + lcd_put_u8str(STATUS_CUTTER_TEXT_X - 2, STATUS_CUTTER_TEXT_Y, ftostr51rj(float(cutter.unitPower) / 1000)); + lcd_put_wchar('K'); + #else + lcd_put_u8str(STATUS_CUTTER_TEXT_X, STATUS_CUTTER_TEXT_Y, i16tostr3rj(cutter.unitPower)); #endif } #endif diff --git a/Marlin/src/lcd/menu/menu_main.cpp b/Marlin/src/lcd/menu/menu_main.cpp index f882fe68688b..d8842137ddf0 100644 --- a/Marlin/src/lcd/menu/menu_main.cpp +++ b/Marlin/src/lcd/menu/menu_main.cpp @@ -73,7 +73,7 @@ void menu_configuration(); #endif #if HAS_CUTTER - #include "../../feature/spindle_laser.h" + // #include "../../feature/spindle_laser.h" void menu_spindle_laser(); #endif diff --git a/Marlin/src/lcd/menu/menu_spindle_laser.cpp b/Marlin/src/lcd/menu/menu_spindle_laser.cpp index e7504122b616..0b3d95defd68 100644 --- a/Marlin/src/lcd/menu/menu_spindle_laser.cpp +++ b/Marlin/src/lcd/menu/menu_spindle_laser.cpp @@ -41,10 +41,9 @@ #if ENABLED(SPINDLE_LASER_PWM) // Change the cutter's "current power" value without turning the cutter on or off - // Power is displayed in units and range according to config - EDIT_ITEM_FAST(CUTTER_MENU_POWER_TYPE, MSG_CUTTER(POWER), &cutter.displayPower, - cutter.dpower_min(), cutter.dpower_max(), cutter.update_from_dpower - ); + // Power is displayed and set in units and range according to CUTTER_POWER_UNIT + EDIT_ITEM_FAST(CUTTER_MENU_POWER_TYPE, MSG_CUTTER(POWER), &cutter.menuPower, + cutter.mpower_min(), cutter.mpower_max(), cutter.update_from_mpower); #endif if (is_enabled) diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index 03ac345344eb..b43f459c7c99 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -1875,7 +1875,7 @@ uint32_t Stepper::block_phase_isr() { } // For non-inline cutter, grossly apply power - #if HAS_CUTTER && DISABLED(LASER_POWER_INLINE) + #if HAS_CUTTER && DISABLED(LASER_POWER_INLINE) && ENABLED(LASER_FEATURE) cutter.apply_power(current_block->cutter_power); #endif From 6d12709a0fd81fffd10b30bfd69f7df1eb7d9b72 Mon Sep 17 00:00:00 2001 From: shitcreek Date: Sat, 23 May 2020 19:14:50 -0500 Subject: [PATCH 11/17] Update spindle_laser.h --- Marlin/src/feature/spindle_laser.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 33efc5519e33..3c6b8280ce83 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -256,7 +256,6 @@ class SpindleLaser { static inline void inline_ocr_power(const uint8_t ocrpwr) { planner.laser.status = ocrpwr ? 0x03 : 0x01; planner.laser.power = ocrpwr; - } #endif #endif // LASER_POWER_INLINE From d456e450687befc7d1dee09a8e9c45bdbe5fc0d9 Mon Sep 17 00:00:00 2001 From: shitcreek Date: Sun, 24 May 2020 19:14:44 -0500 Subject: [PATCH 12/17] Add turn off laser on G28 homing --- Marlin/Configuration_adv.h | 4 ++++ Marlin/src/gcode/calibrate/G28.cpp | 9 +++++++++ Marlin/src/gcode/gcode.cpp | 3 +-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index b8da2a54e799..35a315f45b52 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -2871,6 +2871,10 @@ // Turn off the laser on G0 moves with no power parameter. // If a power parameter is provided, use that instead. //#define LASER_MOVE_G0_OFF + + // Turn off the laser on G28 homing. + //#define LASER_MOVE_G28_OFF + #endif /** diff --git a/Marlin/src/gcode/calibrate/G28.cpp b/Marlin/src/gcode/calibrate/G28.cpp index e44d94fc555b..b0ff5adb4898 100644 --- a/Marlin/src/gcode/calibrate/G28.cpp +++ b/Marlin/src/gcode/calibrate/G28.cpp @@ -51,6 +51,10 @@ #include "../../libs/L64XX/L64XX_Marlin.h" #endif +#if ENABLED(LASER_MOVE_G28_OFF) +#include "../../feature/spindle_laser.h" +#endif + #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) #include "../../core/debug_out.h" @@ -195,6 +199,11 @@ * */ void GcodeSuite::G28() { + +#if ENABLED(LASER_MOVE_G28_OFF) + cutter.set_inline_enabled(false); // turn off laser +#endif + if (DEBUGGING(LEVELING)) { DEBUG_ECHOLNPGM(">>> G28"); log_machine_info(); diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp index 5b071d726642..5dee317ca71c 100644 --- a/Marlin/src/gcode/gcode.cpp +++ b/Marlin/src/gcode/gcode.cpp @@ -179,9 +179,8 @@ void GcodeSuite::get_destination_from_command() { #if ENABLED(LASER_MOVE_POWER) // Set the laser power in the planner to configure this move - if (parser.seen('S')){ + if (parser.seen('S')) cutter.inline_power(cutter.power_to_range(cutter_power_t(round(parser.value_float())))); - } else if (ENABLED(LASER_MOVE_G0_OFF) && parser.codenum == 0) // G0 cutter.set_inline_enabled(false); #endif From ce0c50a9b7308754ce5c4fb976ddd5a74d335d67 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 7 Jun 2020 20:57:47 -0500 Subject: [PATCH 13/17] Minor cleanups --- Marlin/src/feature/spindle_laser.cpp | 6 +-- Marlin/src/feature/spindle_laser.h | 66 +++++++++++++++------------- Marlin/src/inc/SanityCheck.h | 2 + 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/Marlin/src/feature/spindle_laser.cpp b/Marlin/src/feature/spindle_laser.cpp index 5a80e1fd1845..967dbafc8b0b 100644 --- a/Marlin/src/feature/spindle_laser.cpp +++ b/Marlin/src/feature/spindle_laser.cpp @@ -33,8 +33,8 @@ SpindleLaser cutter; uint8_t SpindleLaser::power; bool SpindleLaser::isOn; // state to determine when to apply menuPower to power -cutter_power_t SpindleLaser::menuPower; // Power set via LCD menu in PWM, PERCENT, or RPM -cutter_power_t SpindleLaser::unitPower; // LCD status power in PWM, PERCENT, or RPM +cutter_power_t SpindleLaser::menuPower, // Power set via LCD menu in PWM, PERCENT, or RPM + SpindleLaser::unitPower; // LCD status power in PWM, PERCENT, or RPM #if ENABLED(MARLIN_DEV_MODE) cutter_frequency_t SpindleLaser::frequency; // setting PWM frequency; range: 2K - 50K @@ -81,7 +81,7 @@ void SpindleLaser::apply_power(const uint8_t opwr) { if (opwr == last_power_applied) return; last_power_applied = opwr; power = opwr; - #if ENABLED(SPINDLE_LASER_PWM) + #if ENABLED(SPINDLE_LASER_PWM) if (cutter.unitPower == 0 && CUTTER_UNIT_IS(RPM)){ ocr_off(); isOn = false; diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 3c6b8280ce83..becf97fc8552 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -34,6 +34,8 @@ #include "../module/planner.h" #endif +#define PCT_TO_PWM(X) ((X) * 255 / 100) + #ifndef SPEED_POWER_INTERCEPT #define SPEED_POWER_INTERCEPT 0 #endif @@ -47,7 +49,7 @@ class SpindleLaser { min_pct = round(TERN(CUTTER_POWER_RELATIVE, 0, (100 * float(SPEED_POWER_MIN) / TERN(SPINDLE_FEATURE, float(SPEED_POWER_MAX), 100)))), max_pct = round(TERN(SPINDLE_FEATURE, 100, float(SPEED_POWER_MAX))); - static const inline uint8_t pct_to_ocr(const float pct) { return uint8_t(pct * 255 / 100); } + static const inline uint8_t pct_to_ocr(const float pct) { return uint8_t(PCT_TO_PWM(pct)); } // cpower = configured values (ie SPEED_POWER_MAX) static const inline uint8_t cpwr_to_pct(const cutter_cpower_t cpwr) { // configured value to pct @@ -57,21 +59,21 @@ class SpindleLaser { // Convert a configured value (cpower)(ie SPEED_POWER_STARTUP) to unit power (upwr, upower), // which can be PWM, Percent, or RPM (rel/abs). - static const inline cutter_power_t cpwr_to_upwr(const cutter_cpower_t cpwr){ // STARTUP power to Unit power + static const inline cutter_power_t cpwr_to_upwr(const cutter_cpower_t cpwr) { // STARTUP power to Unit power const cutter_power_t upwr = ( - // Spindle configured values are in RPM #if ENABLED(SPINDLE_FEATURE) + // Spindle configured values are in RPM #if CUTTER_UNIT_IS(RPM) cpwr // to RPM #elif CUTTER_UNIT_IS(PERCENT) // to PCT cpwr_to_pct(cpwr) #else // to PWM - cpwr_to_pct(cpwr) * 255 / 100 + PCT_TO_PWM(cpwr_to_pct(cpwr)) #endif - // Laser configured values are in PCT #else + // Laser configured values are in PCT #if CUTTER_UNIT_IS(PWM255) - cpwr * 255 / 100 + PCT_TO_PWM(cpwr) #else cpwr // to RPM/PCT #endif @@ -83,15 +85,15 @@ class SpindleLaser { static const cutter_power_t mpower_min() { return cpwr_to_upwr(SPEED_POWER_MIN); } static const cutter_power_t mpower_max() { return cpwr_to_upwr(SPEED_POWER_MAX); } - static bool isOn; // State to determine when to apply power to OCR + static bool isOn; // State to determine when to apply power to OCR static uint8_t power; #if ENABLED(MARLIN_DEV_MODE) - static cutter_frequency_t frequency; // Set PWM frequency; range: 2K-50K + static cutter_frequency_t frequency; // Set PWM frequency; range: 2K-50K #endif - static cutter_power_t menuPower; // Power as set via LCD menu in PWM, Percentage or RPM - static cutter_power_t unitPower; // Power as displayed status in PWM, Percentage or RPM + static cutter_power_t menuPower; // Power as set via LCD menu in PWM, Percentage or RPM + static cutter_power_t unitPower; // Power as displayed status in PWM, Percentage or RPM static void init(); @@ -127,42 +129,44 @@ class SpindleLaser { #endif ); } - + // Correct power to configured range static inline cutter_power_t power_to_range(const cutter_power_t pwr) { - return ( + return power_to_range(pwr, ( #if CUTTER_UNIT_IS(PWM255) - power_to_range(pwr, 0) + 0 #elif CUTTER_UNIT_IS(PERCENT) - power_to_range(pwr, 1) + 1 #elif CUTTER_UNIT_IS(RPM) - power_to_range(pwr, 2) + 2 + #else + #error "???" #endif - ); + )); } static inline cutter_power_t power_to_range(const cutter_power_t pwr , const uint8_t pwrUnit) { if (pwr <= 0) return 0; cutter_power_t upwr; switch (pwrUnit) { - case 0: // PWM + case 0: // PWM upwr = ( - (pwr < pct_to_ocr(min_pct)) ? pct_to_ocr(min_pct) // Use minimum if set below - : (pwr > pct_to_ocr(max_pct)) ? pct_to_ocr(max_pct) // Use maximum if set above - : pwr + (pwr < pct_to_ocr(min_pct)) ? pct_to_ocr(min_pct) // Use minimum if set below + : (pwr > pct_to_ocr(max_pct)) ? pct_to_ocr(max_pct) // Use maximum if set above + : pwr ); break; - case 1: // PERCENT + case 1: // PERCENT upwr = ( - (pwr < min_pct) ? min_pct // Use minimum if set below - : (pwr > max_pct) ? max_pct // Use maximum if set above - : pwr // PCT + (pwr < min_pct) ? min_pct // Use minimum if set below + : (pwr > max_pct) ? max_pct // Use maximum if set above + : pwr // PCT ); break; - case 2: // RPM + case 2: // RPM upwr = ( - (pwr < SPEED_POWER_MIN) ? SPEED_POWER_MIN // Use minimum if set below - : (pwr > SPEED_POWER_MAX) ? SPEED_POWER_MAX // Use maximum if set above - : pwr // Calculate OCR value + (pwr < SPEED_POWER_MIN) ? SPEED_POWER_MIN // Use minimum if set below + : (pwr > SPEED_POWER_MAX) ? SPEED_POWER_MAX // Use maximum if set above + : pwr // Calculate OCR value ); break; default: break; @@ -204,7 +208,9 @@ class SpindleLaser { FORCE_INLINE static void enable_reverse() { enable_with_dir(true); } #if ENABLED(SPINDLE_LASER_PWM) - static inline void update_from_mpower() { if (isOn) power = upower_to_ocr(menuPower); unitPower = menuPower; + static inline void update_from_mpower() { + if (isOn) power = upower_to_ocr(menuPower); + unitPower = menuPower; } #endif @@ -226,7 +232,7 @@ class SpindleLaser { // Inline modes of all other functions; all enable planner inline power control static inline void set_inline_enabled(const bool enable) { - if (enable) {inline_power(cpwr_to_upwr(SPEED_POWER_STARTUP));} + if (enable) { inline_power(cpwr_to_upwr(SPEED_POWER_STARTUP)); } else { unitPower = 0; isOn = false; menuPower = 0; TERN(SPINDLE_LASER_PWM, inline_ocr_power, inline_power)(0);} } diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index 01f03e25eae2..329f094e47cc 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -421,6 +421,8 @@ #error "SPINDLE_STOP_ON_DIR_CHANGE is now SPINDLE_CHANGE_DIR_STOP. Please update your Configuration_adv.h." #elif defined(SPINDLE_LASER_ENABLE_INVERT) #error "SPINDLE_LASER_ENABLE_INVERT is now SPINDLE_LASER_ACTIVE_HIGH. Please update your Configuration_adv.h." +#elif defined(CUTTER_POWER_DISPLAY) + #error "CUTTER_POWER_DISPLAY is now CUTTER_POWER_UNIT. Please update your Configuration_adv.h." #elif defined(CHAMBER_HEATER_PIN) #error "CHAMBER_HEATER_PIN is now HEATER_CHAMBER_PIN. Please update your configuration and/or pins." #elif defined(TMC_Z_CALIBRATION) From 91f84b8379b35a10f6a7be86a192c8100a7ca9fb Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 7 Jun 2020 21:06:49 -0500 Subject: [PATCH 14/17] Some laser renames --- Marlin/src/feature/spindle_laser.cpp | 30 +++++++++++----------- Marlin/src/feature/spindle_laser.h | 16 ++++++------ Marlin/src/lcd/dogm/status_screen_DOGM.cpp | 2 +- Marlin/src/lcd/menu/menu_spindle_laser.cpp | 2 +- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Marlin/src/feature/spindle_laser.cpp b/Marlin/src/feature/spindle_laser.cpp index 967dbafc8b0b..17fee5ef2645 100644 --- a/Marlin/src/feature/spindle_laser.cpp +++ b/Marlin/src/feature/spindle_laser.cpp @@ -32,12 +32,12 @@ SpindleLaser cutter; uint8_t SpindleLaser::power; -bool SpindleLaser::isOn; // state to determine when to apply menuPower to power -cutter_power_t SpindleLaser::menuPower, // Power set via LCD menu in PWM, PERCENT, or RPM - SpindleLaser::unitPower; // LCD status power in PWM, PERCENT, or RPM +bool SpindleLaser::isReady; // Ready to apply power setting from the UI to OCR +cutter_power_t SpindleLaser::menuPower, // Power set via LCD menu in PWM, PERCENT, or RPM + SpindleLaser::unitPower; // LCD status power in PWM, PERCENT, or RPM #if ENABLED(MARLIN_DEV_MODE) - cutter_frequency_t SpindleLaser::frequency; // setting PWM frequency; range: 2K - 50K + cutter_frequency_t SpindleLaser::frequency; // setting PWM frequency; range: 2K - 50K #endif #define SPINDLE_LASER_PWM_OFF ((SPINDLE_LASER_PWM_INVERT) ? 255 : 0) @@ -45,13 +45,13 @@ cutter_power_t SpindleLaser::menuPower, // Power // Init the cutter to a safe OFF state // void SpindleLaser::init() { - OUT_WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Init spindle to off + 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) + OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // Init rotation to clockwise (M3) #endif #if ENABLED(SPINDLE_LASER_PWM) SET_PWM(SPINDLE_LASER_PWM_PIN); - analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // set to lowest speed + analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // set to lowest speed #endif #if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY) set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY); @@ -64,11 +64,11 @@ void SpindleLaser::init() { * Set the cutter PWM directly to the given ocr value */ void SpindleLaser::set_ocr(const uint8_t ocr) { - WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_HIGH); // turn spindle on + WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_HIGH); // turn spindle on analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF); } void SpindleLaser::ocr_off() { - WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off + WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte } #endif @@ -82,21 +82,21 @@ void SpindleLaser::apply_power(const uint8_t opwr) { last_power_applied = opwr; power = opwr; #if ENABLED(SPINDLE_LASER_PWM) - if (cutter.unitPower == 0 && CUTTER_UNIT_IS(RPM)){ + if (cutter.unitPower == 0 && CUTTER_UNIT_IS(RPM)) { ocr_off(); - isOn = false; + isReady = false; } - else if (enabled() || (ENABLED(CUTTER_POWER_RELATIVE))){ + else if (enabled() || ENABLED(CUTTER_POWER_RELATIVE)) { set_ocr(power); - isOn = true; + isReady = true; } else { ocr_off(); - isOn = false; + isReady = false; } #else WRITE(SPINDLE_LASER_ENA_PIN, enabled() == SPINDLE_LASER_ACTIVE_HIGH); - isOn = true; + isReady = true; #endif } diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index becf97fc8552..3d19be2bc8a1 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -85,7 +85,7 @@ class SpindleLaser { static const cutter_power_t mpower_min() { return cpwr_to_upwr(SPEED_POWER_MIN); } static const cutter_power_t mpower_max() { return cpwr_to_upwr(SPEED_POWER_MAX); } - static bool isOn; // State to determine when to apply power to OCR + static bool isReady; // Ready to apply power setting from the UI to OCR static uint8_t power; #if ENABLED(MARLIN_DEV_MODE) @@ -189,12 +189,12 @@ class SpindleLaser { static inline void set_direction(const bool) {} #endif - static inline void disable() { isOn = false; set_enabled(false); } + static inline void disable() { isReady = false; set_enabled(false); } #if HAS_LCD_MENU static inline void enable_with_dir(const bool reverse) { - isOn = true; + isReady = true; const uint8_t ocr = upower_to_ocr(menuPower); if (menuPower) power = ocr; @@ -209,7 +209,7 @@ class SpindleLaser { #if ENABLED(SPINDLE_LASER_PWM) static inline void update_from_mpower() { - if (isOn) power = upower_to_ocr(menuPower); + if (isReady) power = upower_to_ocr(menuPower); unitPower = menuPower; } #endif @@ -224,7 +224,7 @@ class SpindleLaser { // Force disengage planner power control static inline void inline_disable() { - isOn = false; + isReady = false; unitPower = 0; planner.laser.status = 0; planner.laser.power = 0; @@ -233,7 +233,7 @@ class SpindleLaser { // Inline modes of all other functions; all enable planner inline power control static inline void set_inline_enabled(const bool enable) { if (enable) { inline_power(cpwr_to_upwr(SPEED_POWER_STARTUP)); } - else { unitPower = 0; isOn = false; menuPower = 0; TERN(SPINDLE_LASER_PWM, inline_ocr_power, inline_power)(0);} + else { unitPower = 0; isReady = false; menuPower = 0; TERN(SPINDLE_LASER_PWM, inline_ocr_power, inline_power)(0);} } // Set the power for subsequent movement blocks @@ -241,7 +241,7 @@ class SpindleLaser { unitPower = upwr; menuPower = unitPower; #if ENABLED(SPINDLE_LASER_PWM) - isOn = true; + isReady = true; #if ENABLED(SPEED_POWER_RELATIVE) && !CUTTER_UNIT_IS(RPM) // relative mode does not turn laser off at 0, except for RPM planner.laser.status = 0x03; planner.laser.power = upower_to_ocr(upwr); @@ -252,7 +252,7 @@ class SpindleLaser { #else planner.laser.status = enabled(upwr) ? 0x03 : 0x01; planner.laser.power = upwr; - isOn = enabled(upwr); + isReady = enabled(upwr); #endif } diff --git a/Marlin/src/lcd/dogm/status_screen_DOGM.cpp b/Marlin/src/lcd/dogm/status_screen_DOGM.cpp index c74ea39f5e6d..6fc6b2f614ce 100644 --- a/Marlin/src/lcd/dogm/status_screen_DOGM.cpp +++ b/Marlin/src/lcd/dogm/status_screen_DOGM.cpp @@ -541,7 +541,7 @@ void MarlinUI::draw_status_screen() { // Laser / Spindle #if DO_DRAW_CUTTER - if (cutter.isOn && PAGE_CONTAINS(STATUS_CUTTER_TEXT_Y - INFO_FONT_ASCENT, STATUS_CUTTER_TEXT_Y - 1)) { + if (cutter.isReady && PAGE_CONTAINS(STATUS_CUTTER_TEXT_Y - INFO_FONT_ASCENT, STATUS_CUTTER_TEXT_Y - 1)) { #if CUTTER_UNIT_IS(PERCENT) lcd_put_u8str(STATUS_CUTTER_TEXT_X, STATUS_CUTTER_TEXT_Y, cutter_power2str(cutter.unitPower)); lcd_put_wchar('%'); diff --git a/Marlin/src/lcd/menu/menu_spindle_laser.cpp b/Marlin/src/lcd/menu/menu_spindle_laser.cpp index 0b3d95defd68..c2a0e9fc0cd8 100644 --- a/Marlin/src/lcd/menu/menu_spindle_laser.cpp +++ b/Marlin/src/lcd/menu/menu_spindle_laser.cpp @@ -34,7 +34,7 @@ void menu_spindle_laser() { - const bool is_enabled = cutter.enabled() && cutter.isOn; + const bool is_enabled = cutter.enabled() && cutter.isReady; START_MENU(); BACK_ITEM(MSG_MAIN); From d97bbb53a7a78e44fdd58f348400a6929d44d21f Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 23 May 2020 23:01:50 -0500 Subject: [PATCH 15/17] Distinct names for laser data --- Marlin/src/feature/spindle_laser.h | 16 +++--- Marlin/src/module/planner.cpp | 6 +- Marlin/src/module/planner.h | 2 +- Marlin/src/module/stepper.cpp | 88 +++++++++++++++--------------- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 3d19be2bc8a1..3118e95c2337 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -226,8 +226,8 @@ class SpindleLaser { static inline void inline_disable() { isReady = false; unitPower = 0; - planner.laser.status = 0; - planner.laser.power = 0; + planner.laser_inline.status = 0; + planner.laser_inline.power = 0; } // Inline modes of all other functions; all enable planner inline power control @@ -243,15 +243,15 @@ class SpindleLaser { #if ENABLED(SPINDLE_LASER_PWM) isReady = true; #if ENABLED(SPEED_POWER_RELATIVE) && !CUTTER_UNIT_IS(RPM) // relative mode does not turn laser off at 0, except for RPM - planner.laser.status = 0x03; - planner.laser.power = upower_to_ocr(upwr); + planner.laser_inline.status = 0x03; + planner.laser_inline.power = upower_to_ocr(upwr); #else if (upwr > 0) inline_ocr_power(upower_to_ocr(upwr)); #endif #else - planner.laser.status = enabled(upwr) ? 0x03 : 0x01; - planner.laser.power = upwr; + planner.laser_inline.status = enabled(pwr) ? 0x03 : 0x01; + planner.laser_inline.power = pwr; isReady = enabled(upwr); #endif } @@ -260,8 +260,8 @@ class SpindleLaser { #if ENABLED(SPINDLE_LASER_PWM) static inline void inline_ocr_power(const uint8_t ocrpwr) { - planner.laser.status = ocrpwr ? 0x03 : 0x01; - planner.laser.power = ocrpwr; + planner.laser_inline.status = ocrpwr ? 0x03 : 0x01; + planner.laser_inline.power = ocrpwr; } #endif #endif // LASER_POWER_INLINE diff --git a/Marlin/src/module/planner.cpp b/Marlin/src/module/planner.cpp index 784cc194328d..f04c63060dd8 100644 --- a/Marlin/src/module/planner.cpp +++ b/Marlin/src/module/planner.cpp @@ -129,7 +129,7 @@ uint8_t Planner::delay_before_delivering; // This counter delays delivery planner_settings_t Planner::settings; // Initialized by settings.load() #if ENABLED(LASER_POWER_INLINE) - laser_state_t Planner::laser; // Current state for blocks + laser_state_t Planner::laser_inline; // Current state for blocks #endif uint32_t Planner::max_acceleration_steps_per_s2[XYZE_N]; // (steps/s^2) Derived from mm_per_s2 @@ -1803,8 +1803,8 @@ bool Planner::_populate_block(block_t * const block, bool split_move, // Update block laser power #if ENABLED(LASER_POWER_INLINE) - block->laser.status = laser.status; - block->laser.power = laser.power; + block->laser.status = laser_inline.status; + block->laser.power = laser_inline.power; #endif // Number of steps for each axis diff --git a/Marlin/src/module/planner.h b/Marlin/src/module/planner.h index 94be0d0b06dc..8caa9d6e1111 100644 --- a/Marlin/src/module/planner.h +++ b/Marlin/src/module/planner.h @@ -332,7 +332,7 @@ class Planner { static planner_settings_t settings; #if ENABLED(LASER_POWER_INLINE) - static laser_state_t laser; + static laser_state_t laser_inline; #endif static uint32_t max_acceleration_steps_per_s2[XYZE_N]; // (steps/s^2) Derived from mm_per_s2 diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index d2f009a78afd..cfca7d29a3c0 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -244,8 +244,8 @@ xyze_long_t Stepper::count_position{0}; xyze_int8_t Stepper::count_direction{0}; #if ENABLED(LASER_POWER_INLINE_TRAPEZOID) - Stepper::stepper_laser_t Stepper::laser = { - .trap_en = false, + Stepper::stepper_laser_t Stepper::laser_trap = { + .enabled = false, .cur_power = 0, .cruise_set = false, #if DISABLED(LASER_POWER_INLINE_TRAPEZOID_CONT) @@ -1845,28 +1845,28 @@ uint32_t Stepper::block_phase_isr() { // Update laser - Accelerating #if ENABLED(LASER_POWER_INLINE_TRAPEZOID) - if (laser.trap_en) { + if (laser_trap.enabled) { #if DISABLED(LASER_POWER_INLINE_TRAPEZOID_CONT) if (current_block->laser.entry_per) { - laser.acc_step_count -= step_events_completed - laser.last_step_count; - laser.last_step_count = step_events_completed; + laser_trap.acc_step_count -= step_events_completed - laser_trap.last_step_count; + laser_trap.last_step_count = step_events_completed; // Should be faster than a divide, since this should trip just once - if (laser.acc_step_count < 0) { - while (laser.acc_step_count < 0) { - laser.acc_step_count += current_block->laser.entry_per; - if (laser.cur_power < current_block->laser.power) laser.cur_power++; + if (laser_trap.acc_step_count < 0) { + while (laser_trap.acc_step_count < 0) { + laser_trap.acc_step_count += current_block->laser.entry_per; + if (laser_trap.cur_power < current_block->laser.power) laser_trap.cur_power++; } - cutter.set_ocr_power(laser.cur_power); + cutter.set_ocr_power(laser_trap.cur_power); } } #else - if (laser.till_update) - laser.till_update--; + if (laser_trap.till_update) + laser_trap.till_update--; else { - laser.till_update = LASER_POWER_INLINE_TRAPEZOID_CONT_PER; - laser.cur_power = (current_block->laser.power * acc_step_rate) / current_block->nominal_rate; - cutter.set_ocr_power(laser.cur_power); // Cycle efficiency is irrelevant it the last line was many cycles + laser_trap.till_update = LASER_POWER_INLINE_TRAPEZOID_CONT_PER; + laser_trap.cur_power = (current_block->laser.power * acc_step_rate) / current_block->nominal_rate; + cutter.set_ocr_power(laser_trap.cur_power); // Cycle efficiency is irrelevant it the last line was many cycles } #endif } @@ -1922,28 +1922,28 @@ uint32_t Stepper::block_phase_isr() { // Update laser - Decelerating #if ENABLED(LASER_POWER_INLINE_TRAPEZOID) - if (laser.trap_en) { + if (laser_trap.enabled) { #if DISABLED(LASER_POWER_INLINE_TRAPEZOID_CONT) if (current_block->laser.exit_per) { - laser.acc_step_count -= step_events_completed - laser.last_step_count; - laser.last_step_count = step_events_completed; + laser_trap.acc_step_count -= step_events_completed - laser_trap.last_step_count; + laser_trap.last_step_count = step_events_completed; // Should be faster than a divide, since this should trip just once - if (laser.acc_step_count < 0) { - while (laser.acc_step_count < 0) { - laser.acc_step_count += current_block->laser.exit_per; - if (laser.cur_power > current_block->laser.power_exit) laser.cur_power--; + if (laser_trap.acc_step_count < 0) { + while (laser_trap.acc_step_count < 0) { + laser_trap.acc_step_count += current_block->laser.exit_per; + if (laser_trap.cur_power > current_block->laser.power_exit) laser_trap.cur_power--; } - cutter.set_ocr_power(laser.cur_power); + cutter.set_ocr_power(laser_trap.cur_power); } } #else - if (laser.till_update) - laser.till_update--; + if (laser_trap.till_update) + laser_trap.till_update--; else { - laser.till_update = LASER_POWER_INLINE_TRAPEZOID_CONT_PER; - laser.cur_power = (current_block->laser.power * step_rate) / current_block->nominal_rate; - cutter.set_ocr_power(laser.cur_power); // Cycle efficiency isn't relevant when the last line was many cycles + laser_trap.till_update = LASER_POWER_INLINE_TRAPEZOID_CONT_PER; + laser_trap.cur_power = (current_block->laser.power * step_rate) / current_block->nominal_rate; + cutter.set_ocr_power(laser_trap.cur_power); // Cycle efficiency isn't relevant when the last line was many cycles } #endif } @@ -1968,16 +1968,16 @@ uint32_t Stepper::block_phase_isr() { // Update laser - Cruising #if ENABLED(LASER_POWER_INLINE_TRAPEZOID) - if (laser.trap_en) { - if (!laser.cruise_set) { - laser.cur_power = current_block->laser.power; - cutter.set_ocr_power(laser.cur_power); - laser.cruise_set = true; + if (laser_trap.enabled) { + if (!laser_trap.cruise_set) { + laser_trap.cur_power = current_block->laser.power; + cutter.set_ocr_power(laser_trap.cur_power); + laser_trap.cruise_set = true; } #if ENABLED(LASER_POWER_INLINE_TRAPEZOID_CONT) - laser.till_update = LASER_POWER_INLINE_TRAPEZOID_CONT_PER; + laser_trap.till_update = LASER_POWER_INLINE_TRAPEZOID_CONT_PER; #else - laser.last_step_count = step_events_completed; + laser_trap.last_step_count = step_events_completed; #endif } #endif @@ -2177,19 +2177,19 @@ uint32_t Stepper::block_phase_isr() { #if ENABLED(LASER_POWER_INLINE) const uint8_t stat = current_block->laser.status; #if ENABLED(LASER_POWER_INLINE_TRAPEZOID) - laser.trap_en = (stat & 0x03) == 0x03; - laser.cur_power = current_block->laser.power_entry; // RESET STATE - laser.cruise_set = false; + laser_trap.enabled = (stat & 0x03) == 0x03; + laser_trap.cur_power = current_block->laser.power_entry; // RESET STATE + laser_trap.cruise_set = false; #if DISABLED(LASER_POWER_INLINE_TRAPEZOID_CONT) - laser.last_step_count = 0; - laser.acc_step_count = current_block->laser.entry_per / 2; + laser_trap.last_step_count = 0; + laser_trap.acc_step_count = current_block->laser.entry_per / 2; #else - laser.till_update = 0; + laser_trap.till_update = 0; #endif // Always have PWM in this case if (TEST(stat, 0)) { // Planner controls the laser if (TEST(stat, 1)) // Laser is on - cutter.set_ocr_power(laser.cur_power); + cutter.set_ocr_power(laser_trap.cur_power); else cutter.set_ocr_power(0); } @@ -2242,11 +2242,11 @@ uint32_t Stepper::block_phase_isr() { #if ENABLED(LASER_POWER_INLINE_CONTINUOUS) else { // No new block found; so apply inline laser parameters // This should mean ending file with 'M5 I' will stop the laser; thus the inline flag isn't needed - const uint8_t stat = planner.laser.status; + const uint8_t stat = planner.laser_inline.status; if (TEST(stat, 0)) { // Planner controls the laser #if ENABLED(SPINDLE_LASER_PWM) if (TEST(stat, 1)) // Laser is on - cutter.set_ocr_power(planner.laser.power); + cutter.set_ocr_power(planner.laser_inline.power); else cutter.set_ocr_power(0); #else From 9fced793c055bcb43922b86d84f2e721c9f76725 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 7 Jun 2020 21:39:21 -0500 Subject: [PATCH 16/17] And, more cleanup --- Marlin/src/feature/spindle_laser.h | 7 ++--- Marlin/src/module/planner.h | 18 ++++++----- Marlin/src/module/stepper.cpp | 49 ++++++++++++------------------ 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 3118e95c2337..e3aa4023c029 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -53,8 +53,7 @@ class SpindleLaser { // cpower = configured values (ie SPEED_POWER_MAX) static const inline uint8_t cpwr_to_pct(const cutter_cpower_t cpwr) { // configured value to pct - uint8_t pct = (unitPower == 0) ? 0 : round(100 * (cpwr - SPEED_POWER_FLOOR) / (SPEED_POWER_MAX - SPEED_POWER_FLOOR)); - return pct; + return unitPower ? round(100 * (cpwr - SPEED_POWER_FLOOR) / (SPEED_POWER_MAX - SPEED_POWER_FLOOR)) : 0; } // Convert a configured value (cpower)(ie SPEED_POWER_STARTUP) to unit power (upwr, upower), @@ -110,7 +109,7 @@ class SpindleLaser { FORCE_INLINE static void refresh() { apply_power(power); } FORCE_INLINE static void set_power(const uint8_t upwr) { power = upwr; refresh(); } - static inline void set_enabled(const bool enable) { set_power(enable ? (power ?: (unitPower == 0 ? 0 : upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP)))) : 0);} + static inline void set_enabled(const bool enable) { set_power(enable ? (power ?: (unitPower ? upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP)) : 0)) : 0); } #if ENABLED(SPINDLE_LASER_PWM) @@ -144,7 +143,7 @@ class SpindleLaser { #endif )); } - static inline cutter_power_t power_to_range(const cutter_power_t pwr , const uint8_t pwrUnit) { + static inline cutter_power_t power_to_range(const cutter_power_t pwr, const uint8_t pwrUnit) { if (pwr <= 0) return 0; cutter_power_t upwr; switch (pwrUnit) { diff --git a/Marlin/src/module/planner.h b/Marlin/src/module/planner.h index 8caa9d6e1111..228b3c9a1cbe 100644 --- a/Marlin/src/module/planner.h +++ b/Marlin/src/module/planner.h @@ -117,8 +117,15 @@ enum BlockFlag : char { #if ENABLED(LASER_POWER_INLINE) typedef struct { - uint8_t status, // See planner settings for meaning - power; // Ditto; When in trapezoid mode this is nominal power + bool isPlanned:1; + bool isEnabled:1; + bool dir:1; + bool Reserved:6; + } power_status_t; + + typedef struct { + power_status_t status; // See planner settings for meaning + uint8_t power; // Ditto; When in trapezoid mode this is nominal power #if ENABLED(LASER_POWER_INLINE_TRAPEZOID) uint8_t power_entry; // Entry power for the laser #if DISABLED(LASER_POWER_INLINE_TRAPEZOID_CONT) @@ -234,12 +241,9 @@ typedef struct block_t { #if ENABLED(LASER_POWER_INLINE) typedef struct { /** - * Laser status bitmask; most bits are unused; - * 0: Planner buffer enable - * 1: Laser enable - * 2: Reserved for direction + * Laser status flags */ - uint8_t status; + power_status_t status; /** * Laser power: 0 or 255 in case of PWM-less laser, * or the OCR (oscillator count register) value; diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index cfca7d29a3c0..03a77f17f089 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -2155,15 +2155,9 @@ uint32_t Stepper::block_phase_isr() { else LA_isr_rate = LA_ADV_NEVER; #endif - if ( - #if HAS_L64XX - true // Always set direction for L64xx (This also enables the chips) - #else - current_block->direction_bits != last_direction_bits - #if DISABLED(MIXING_EXTRUDER) - || stepper_extruder != last_moved_extruder - #endif - #endif + if ( ENABLED(HAS_L64XX) // Always set direction for L64xx (Also enables the chips) + || current_block->direction_bits != last_direction_bits + || TERN(MIXING_EXTRUDER, false, stepper_extruder != last_moved_extruder) ) { last_direction_bits = current_block->direction_bits; #if EXTRUDERS > 1 @@ -2175,9 +2169,9 @@ uint32_t Stepper::block_phase_isr() { } #if ENABLED(LASER_POWER_INLINE) - const uint8_t stat = current_block->laser.status; + const power_status_t stat = current_block->laser.status; #if ENABLED(LASER_POWER_INLINE_TRAPEZOID) - laser_trap.enabled = (stat & 0x03) == 0x03; + laser_trap.enabled = stat.isPlanned && stat.isEnabled; laser_trap.cur_power = current_block->laser.power_entry; // RESET STATE laser_trap.cruise_set = false; #if DISABLED(LASER_POWER_INLINE_TRAPEZOID_CONT) @@ -2187,21 +2181,19 @@ uint32_t Stepper::block_phase_isr() { laser_trap.till_update = 0; #endif // Always have PWM in this case - if (TEST(stat, 0)) { // Planner controls the laser - if (TEST(stat, 1)) // Laser is on - cutter.set_ocr_power(laser_trap.cur_power); - else - cutter.set_ocr_power(0); + if (stat.isPlanned) { // Planner controls the laser + cutter.set_ocr_power( + stat.isEnabled ? laser_trap.cur_power : 0 // ON with power or OFF + ); } #else - if (TEST(stat, 0)) { // Planner controls the laser + if (stat.isPlanned) { // Planner controls the laser #if ENABLED(SPINDLE_LASER_PWM) - if (TEST(stat, 1)) // Laser is on - cutter.set_ocr_power(current_block->laser.power); - else - cutter.set_ocr_power(0); + cutter.set_ocr_power( + stat.isEnabled ? current_block->laser.power : 0 // ON with power or OFF + ); #else - cutter.set_enabled(TEST(stat, 1)); + cutter.set_enabled(stat.isEnabled); #endif } #endif @@ -2242,15 +2234,14 @@ uint32_t Stepper::block_phase_isr() { #if ENABLED(LASER_POWER_INLINE_CONTINUOUS) else { // No new block found; so apply inline laser parameters // This should mean ending file with 'M5 I' will stop the laser; thus the inline flag isn't needed - const uint8_t stat = planner.laser_inline.status; - if (TEST(stat, 0)) { // Planner controls the laser + const power_status_t stat = planner.laser_inline.status; + if (stat.isPlanned) { // Planner controls the laser #if ENABLED(SPINDLE_LASER_PWM) - if (TEST(stat, 1)) // Laser is on - cutter.set_ocr_power(planner.laser_inline.power); - else - cutter.set_ocr_power(0); + cutter.set_ocr_power( + stat.isEnabled ? planner.laser_inline.power : 0 // ON with power or OFF + ); #else - cutter.set_enabled(TEST(stat, 1)); + cutter.set_enabled(stat.isEnabled); #endif } } From 619e591b1b727fa8572c2f6d0020e2132dbc01b9 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 7 Jun 2020 21:51:24 -0500 Subject: [PATCH 17/17] Cleanup --- Marlin/src/gcode/calibrate/G28.cpp | 2 +- Marlin/src/gcode/control/M3-M5.cpp | 21 ++++++++++----------- Marlin/src/lcd/menu/menu_main.cpp | 1 - Marlin/src/module/stepper.cpp | 2 +- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G28.cpp b/Marlin/src/gcode/calibrate/G28.cpp index caf83c0831cc..8720b1a929e6 100644 --- a/Marlin/src/gcode/calibrate/G28.cpp +++ b/Marlin/src/gcode/calibrate/G28.cpp @@ -52,7 +52,7 @@ #endif #if ENABLED(LASER_MOVE_G28_OFF) -#include "../../feature/spindle_laser.h" + #include "../../feature/spindle_laser.h" #endif #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) diff --git a/Marlin/src/gcode/control/M3-M5.cpp b/Marlin/src/gcode/control/M3-M5.cpp index 00021c1945f1..0ef385221be7 100644 --- a/Marlin/src/gcode/control/M3-M5.cpp +++ b/Marlin/src/gcode/control/M3-M5.cpp @@ -74,20 +74,19 @@ void GcodeSuite::M3_M4(const bool is_M4) { return cutter.unitPower; }; -#if ENABLED(LASER_POWER_INLINE) + #if ENABLED(LASER_POWER_INLINE) if (parser.seen('I') == DISABLED(LASER_POWER_INLINE_INVERT)) { // Laser power in inline mode cutter.inline_direction(is_M4); // Should always be unused #if ENABLED(SPINDLE_LASER_PWM) - if (parser.seen('O')){ + if (parser.seen('O')) { cutter.unitPower = cutter.power_to_range(parser.value_byte(), 0); cutter.inline_ocr_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t) - } - else { - cutter.inline_power(cutter.upower_to_ocr(get_s_power())); } + else + cutter.inline_power(cutter.upower_to_ocr(get_s_power())); #else - cutter.inline_enabled(true); + cutter.inline_enabled(true); #endif return; } @@ -99,17 +98,16 @@ void GcodeSuite::M3_M4(const bool is_M4) { cutter.set_direction(is_M4); #if ENABLED(SPINDLE_LASER_PWM) - if (parser.seenval('O')){ + if (parser.seenval('O')) { cutter.unitPower = cutter.power_to_range(parser.value_byte(), 0); cutter.set_ocr_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t) } - else { + else cutter.set_power(cutter.upower_to_ocr(get_s_power())); - } #else cutter.set_enabled(true); #endif - cutter.menuPower = cutter.unitPower; + cutter.menuPower = cutter.unitPower; } /** @@ -120,7 +118,8 @@ void GcodeSuite::M5() { if (parser.seen('I') == DISABLED(LASER_POWER_INLINE_INVERT)) { cutter.set_inline_enabled(false); // Laser power in inline mode return; - } // Non-inline, standard case + } + // Non-inline, standard case cutter.inline_disable(); // Prevent future blocks re-setting the power #endif planner.synchronize(); diff --git a/Marlin/src/lcd/menu/menu_main.cpp b/Marlin/src/lcd/menu/menu_main.cpp index 3974b61dd1de..3bfb6003386c 100644 --- a/Marlin/src/lcd/menu/menu_main.cpp +++ b/Marlin/src/lcd/menu/menu_main.cpp @@ -73,7 +73,6 @@ void menu_configuration(); #endif #if HAS_CUTTER - // #include "../../feature/spindle_laser.h" void menu_spindle_laser(); #endif diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index 03a77f17f089..86d85cc7234b 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -2003,7 +2003,7 @@ uint32_t Stepper::block_phase_isr() { } // For non-inline cutter, grossly apply power - #if HAS_CUTTER && DISABLED(LASER_POWER_INLINE) && ENABLED(LASER_FEATURE) + #if ENABLED(LASER_FEATURE) && DISABLED(LASER_POWER_INLINE) cutter.apply_power(current_block->cutter_power); #endif