From cd89fa141b45dd84644543321994b9b83f159ec4 Mon Sep 17 00:00:00 2001 From: Alexander Semion Date: Sun, 1 Nov 2020 01:42:05 +0300 Subject: [PATCH] Add SPINDLE_SERVO option (#19971) --- Marlin/Configuration_adv.h | 7 +++++++ Marlin/src/feature/spindle_laser.cpp | 12 +++++++++++- Marlin/src/feature/spindle_laser.h | 5 ++++- Marlin/src/gcode/control/M3-M5.cpp | 8 +++++++- Marlin/src/inc/Conditionals_adv.h | 6 +++++- Marlin/src/inc/SanityCheck.h | 8 ++++---- 6 files changed, 38 insertions(+), 8 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 951aab780..b39e9e172 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -2943,11 +2943,18 @@ #define SPINDLE_LASER_FREQUENCY 2500 // (Hz) Spindle/laser frequency (only on supported HALs: AVR and LPC) + //#define SPINDLE_SERVO // A servo converting an angle to spindle power + #ifdef SPINDLE_SERVO + #define SPINDLE_SERVO_NR 0 // Index of servo used for spindle control + #define SPINDLE_SERVO_MIN 10 // Minimum angle for servo spindle + #endif + /** * Speed / Power can be set ('M3 S') and displayed in terms of: * - PWM255 (S0 - S255) * - PERCENT (S0 - S100) * - RPM (S0 - S50000) Best for use with a spindle + * - SERVO (S0 - S180) */ #define CUTTER_POWER_UNIT PWM255 diff --git a/Marlin/src/feature/spindle_laser.cpp b/Marlin/src/feature/spindle_laser.cpp index bc387a933..3b28b61b4 100644 --- a/Marlin/src/feature/spindle_laser.cpp +++ b/Marlin/src/feature/spindle_laser.cpp @@ -30,6 +30,10 @@ #include "spindle_laser.h" +#if ENABLED(SPINDLE_SERVO) + #include "../module/servo.h" +#endif + SpindleLaser cutter; uint8_t SpindleLaser::power; bool SpindleLaser::isReady; // Ready to apply power setting from the UI to OCR @@ -45,7 +49,11 @@ cutter_power_t SpindleLaser::menuPower, // Power s // Init the cutter to a safe OFF state // void SpindleLaser::init() { - OUT_WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Init spindle to off + #if ENABLED(SPINDLE_SERVO) + MOVE_SERVO(SPINDLE_SERVO_NR, SPINDLE_SERVO_MIN); + #else + OUT_WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Init spindle to off + #endif #if ENABLED(SPINDLE_CHANGE_DIR) OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // Init rotation to clockwise (M3) #endif @@ -97,6 +105,8 @@ void SpindleLaser::apply_power(const uint8_t opwr) { ocr_off(); isReady = false; } + #elif ENABLED(SPINDLE_SERVO) + MOVE_SERVO(SPINDLE_SERVO_NR, power); #else WRITE(SPINDLE_LASER_ENA_PIN, enabled() ? SPINDLE_LASER_ACTIVE_STATE : !SPINDLE_LASER_ACTIVE_STATE); isReady = true; diff --git a/Marlin/src/feature/spindle_laser.h b/Marlin/src/feature/spindle_laser.h index 50e92fe66..74d06634a 100644 --- a/Marlin/src/feature/spindle_laser.h +++ b/Marlin/src/feature/spindle_laser.h @@ -35,6 +35,7 @@ #endif #define PCT_TO_PWM(X) ((X) * 255 / 100) +#define PCT_TO_SERVO(X) ((X) * 180 / 100) #ifndef SPEED_POWER_INTERCEPT #define SPEED_POWER_INTERCEPT 0 @@ -60,7 +61,7 @@ public: } // Convert a cpower (e.g., SPEED_POWER_STARTUP) to unit power (upwr, upower), - // which can be PWM, Percent, or RPM (rel/abs). + // which can be PWM, Percent, Servo angle, 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 = ( #if ENABLED(SPINDLE_FEATURE) @@ -69,6 +70,8 @@ public: cpwr // to RPM #elif CUTTER_UNIT_IS(PERCENT) // to PCT cpwr_to_pct(cpwr) + #elif CUTTER_UNIT_IS(SERVO) // to SERVO angle + PCT_TO_SERVO(cpwr_to_pct(cpwr)) #else // to PWM PCT_TO_PWM(cpwr_to_pct(cpwr)) #endif diff --git a/Marlin/src/gcode/control/M3-M5.cpp b/Marlin/src/gcode/control/M3-M5.cpp index 1326c3066..6f2409f69 100644 --- a/Marlin/src/gcode/control/M3-M5.cpp +++ b/Marlin/src/gcode/control/M3-M5.cpp @@ -69,9 +69,13 @@ void GcodeSuite::M3_M4(const bool is_M4) { auto get_s_power = [] { if (parser.seenval('S')) { const float spwr = parser.value_float(); - cutter.unitPower = TERN(SPINDLE_LASER_PWM, + #if ENABLED(SPINDLE_SERVO) + cutter.unitPower = spwr; + #else + cutter.unitPower = TERN(SPINDLE_LASER_PWM, cutter.power_to_range(cutter_power_t(round(spwr))), spwr > 0 ? 255 : 0); + #endif } else cutter.unitPower = cutter.cpwr_to_upwr(SPEED_POWER_STARTUP); @@ -108,6 +112,8 @@ void GcodeSuite::M3_M4(const bool is_M4) { } else cutter.set_power(cutter.upower_to_ocr(get_s_power())); + #elif ENABLED(SPINDLE_SERVO) + cutter.set_power(get_s_power()); #else cutter.set_enabled(true); #endif diff --git a/Marlin/src/inc/Conditionals_adv.h b/Marlin/src/inc/Conditionals_adv.h index 05935be54..2a898d608 100644 --- a/Marlin/src/inc/Conditionals_adv.h +++ b/Marlin/src/inc/Conditionals_adv.h @@ -33,7 +33,7 @@ // Determine NUM_SERVOS if none was supplied #ifndef NUM_SERVOS #define NUM_SERVOS 0 - #if ANY(CHAMBER_VENT, HAS_Z_SERVO_PROBE, SWITCHING_EXTRUDER, SWITCHING_NOZZLE) + #if ANY(HAS_Z_SERVO_PROBE, CHAMBER_VENT, SWITCHING_TOOLHEAD, SWITCHING_EXTRUDER, SWITCHING_NOZZLE, SPINDLE_SERVO) #if NUM_SERVOS <= Z_PROBE_SERVO_NR #undef NUM_SERVOS #define NUM_SERVOS (Z_PROBE_SERVO_NR + 1) @@ -62,6 +62,10 @@ #undef NUM_SERVOS #define NUM_SERVOS (SWITCHING_EXTRUDER_E23_SERVO_NR + 1) #endif + #if NUM_SERVOS <= SPINDLE_SERVO_NR + #undef NUM_SERVOS + #define NUM_SERVOS (SPINDLE_SERVO_NR + 1) + #endif #endif #endif diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index 657e05524..cadf84abd 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -3007,8 +3007,8 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2) #if HAS_CUTTER #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." + #elif !CUTTER_UNIT_IS(PWM255) && !CUTTER_UNIT_IS(PERCENT) && !CUTTER_UNIT_IS(RPM) && !CUTTER_UNIT_IS(SERVO) + #error "CUTTER_POWER_UNIT must be PWM255, PERCENT, RPM, or SERVO. Please update your Configuration_adv.h." #endif #if ENABLED(LASER_POWER_INLINE) @@ -3047,8 +3047,8 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2) #define _PIN_CONFLICT(P) (PIN_EXISTS(P) && P##_PIN == SPINDLE_LASER_PWM_PIN) #if BOTH(SPINDLE_FEATURE, LASER_FEATURE) #error "Enable only one of SPINDLE_FEATURE or LASER_FEATURE." - #elif !PIN_EXISTS(SPINDLE_LASER_ENA) - #error "(SPINDLE|LASER)_FEATURE requires SPINDLE_LASER_ENA_PIN." + #elif !PIN_EXISTS(SPINDLE_LASER_ENA) && DISABLED(SPINDLE_SERVO) + #error "(SPINDLE|LASER)_FEATURE requires SPINDLE_LASER_ENA_PIN or SPINDLE_SERVO to control the power." #elif ENABLED(SPINDLE_CHANGE_DIR) && !PIN_EXISTS(SPINDLE_DIR) #error "SPINDLE_DIR_PIN is required for SPINDLE_CHANGE_DIR." #elif ENABLED(SPINDLE_LASER_PWM)