Skip to content

Commit

Permalink
🔧 SPINDLE_LASER_PWM => SPINDLE_LASER_USE_PWM
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Oct 3, 2021
1 parent 59ad935 commit de4eed3
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 49 deletions.
30 changes: 18 additions & 12 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -3323,11 +3323,13 @@
//#define SPINDLE_FEATURE
//#define LASER_FEATURE
#if EITHER(SPINDLE_FEATURE, LASER_FEATURE)
#define SPINDLE_LASER_ACTIVE_STATE LOW // Set to "HIGH" 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 false // Set to "true" if the speed/power goes up when you want it to go slower
#define SPINDLE_LASER_ACTIVE_STATE LOW // Set to "HIGH" if SPINDLE_LASER_ENA_PIN is active HIGH

#define SPINDLE_LASER_FREQUENCY 2500 // (Hz) Spindle/laser frequency (only on supported HALs: AVR and LPC)
#define SPINDLE_LASER_USE_PWM // Enable if your controller supports setting the speed/power
#if ENABLED(SPINDLE_LASER_USE_PWM)
#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)
#endif

//#define AIR_EVACUATION // Cutter Vacuum / Laser Blower motor control with G-codes M10-M11
#if ENABLED(AIR_EVACUATION)
Expand Down Expand Up @@ -3383,17 +3385,21 @@
* 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_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)
#if ENABLED(SPINDLE_LASER_USE_PWM)
#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)
#endif

#else

#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)
#if ENABLED(SPINDLE_LASER_USE_PWM)
#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)
#endif

// Define the minimum and maximum test pulse time values for a laser test fire function
#define LASER_TEST_PULSE_MIN 1 // Used with Laser Control Menu
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/HAL/AVR/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
/**
* Sanity checks for Spindle / Laser PWM
*/
#if ENABLED(SPINDLE_LASER_PWM)
#if ENABLED(SPINDLE_LASER_USE_PWM)
#include "../ServoTimers.h" // Needed to check timer availability (_useTimer3)
#if SPINDLE_LASER_PWM_PIN == 4 || WITHIN(SPINDLE_LASER_PWM_PIN, 11, 13)
#error "Counter/Timer for SPINDLE_LASER_PWM_PIN is used by a system interrupt."
#elif NUM_SERVOS > 0 && defined(_useTimer3) && (WITHIN(SPINDLE_LASER_PWM_PIN, 2, 3) || SPINDLE_LASER_PWM_PIN == 5)
#error "Counter/Timer for SPINDLE_LASER_PWM_PIN is used by the servo system."
#endif
#elif defined(SPINDLE_LASER_FREQUENCY)
#error "SPINDLE_LASER_FREQUENCY requires SPINDLE_LASER_PWM."
#error "SPINDLE_LASER_FREQUENCY requires SPINDLE_LASER_USE_PWM."
#endif

/**
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/LINUX/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/

// Emulating RAMPS
#if ENABLED(SPINDLE_LASER_PWM) && !(SPINDLE_LASER_PWM_PIN == 4 || SPINDLE_LASER_PWM_PIN == 6 || SPINDLE_LASER_PWM_PIN == 11)
#if ENABLED(SPINDLE_LASER_USE_PWM) && !(SPINDLE_LASER_PWM_PIN == 4 || SPINDLE_LASER_PWM_PIN == 6 || SPINDLE_LASER_PWM_PIN == 11)
#error "SPINDLE_LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector"
#endif

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/LPC1768/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static_assert(!(NUM_SERVOS && ENABLED(FAST_PWM_FAN)), "BLTOUCH and Servos are in
* Test LPC176x-specific configuration values for errors at compile-time.
*/

//#if ENABLED(SPINDLE_LASER_PWM) && !(SPINDLE_LASER_PWM_PIN == 4 || SPINDLE_LASER_PWM_PIN == 6 || SPINDLE_LASER_PWM_PIN == 11)
//#if ENABLED(SPINDLE_LASER_USE_PWM) && !(SPINDLE_LASER_PWM_PIN == 4 || SPINDLE_LASER_PWM_PIN == 6 || SPINDLE_LASER_PWM_PIN == 11)
// #error "SPINDLE_LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector"
//#endif

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/NATIVE_SIM/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/

// Emulating RAMPS
#if ENABLED(SPINDLE_LASER_PWM) && !(SPINDLE_LASER_PWM_PIN == 4 || SPINDLE_LASER_PWM_PIN == 6 || SPINDLE_LASER_PWM_PIN == 11)
#if ENABLED(SPINDLE_LASER_USE_PWM) && !(SPINDLE_LASER_PWM_PIN == 4 || SPINDLE_LASER_PWM_PIN == 6 || SPINDLE_LASER_PWM_PIN == 11)
#error "SPINDLE_LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector"
#endif

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Test STM32-specific configuration values for errors at compile-time.
*/
//#if ENABLED(SPINDLE_LASER_PWM) && !(SPINDLE_LASER_PWM_PIN == 4 || SPINDLE_LASER_PWM_PIN == 6 || SPINDLE_LASER_PWM_PIN == 11)
//#if ENABLED(SPINDLE_LASER_USE_PWM) && !(SPINDLE_LASER_PWM_PIN == 4 || SPINDLE_LASER_PWM_PIN == 6 || SPINDLE_LASER_PWM_PIN == 11)
// #error "SPINDLE_LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector"
//#endif

Expand Down
8 changes: 4 additions & 4 deletions Marlin/src/feature/spindle_laser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void SpindleLaser::init() {
#if ENABLED(SPINDLE_CHANGE_DIR)
OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // Init rotation to clockwise (M3)
#endif
#if ENABLED(SPINDLE_LASER_PWM)
#if ENABLED(SPINDLE_LASER_USE_PWM)
SET_PWM(SPINDLE_LASER_PWM_PIN);
analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Set to lowest speed
#endif
Expand All @@ -83,7 +83,7 @@ void SpindleLaser::init() {
#endif
}

#if ENABLED(SPINDLE_LASER_PWM)
#if ENABLED(SPINDLE_LASER_USE_PWM)
/**
* Set the cutter PWM directly to the given ocr value
*
Expand All @@ -107,7 +107,7 @@ void SpindleLaser::init() {
WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Cutter OFF
_set_ocr(0);
}
#endif // SPINDLE_LASER_PWM
#endif // SPINDLE_LASER_USE_PWM

/**
* Apply power for laser/spindle
Expand All @@ -121,7 +121,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_USE_PWM)
if (cutter.unitPower == 0 && CUTTER_UNIT_IS(RPM)) {
ocr_off();
isReady = false;
Expand Down
16 changes: 8 additions & 8 deletions Marlin/src/feature/spindle_laser.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class SpindleLaser {
FORCE_INLINE static void refresh() { apply_power(power); }
FORCE_INLINE static void set_power(const uint8_t upwr) { power = upwr; refresh(); }

#if ENABLED(SPINDLE_LASER_PWM)
#if ENABLED(SPINDLE_LASER_USE_PWM)

private:

Expand Down Expand Up @@ -186,7 +186,7 @@ class SpindleLaser {
}
return upwr;
}
#endif // SPINDLE_LASER_PWM
#endif // SPINDLE_LASER_USE_PWM

/**
* Enable/Disable spindle/laser
Expand All @@ -195,7 +195,7 @@ class SpindleLaser {
static inline void set_enabled(const bool enable) {
uint8_t value = 0;
if (enable) {
#if ENABLED(SPINDLE_LASER_PWM)
#if ENABLED(SPINDLE_LASER_USE_PWM)
if (power)
value = power;
else if (unitPower)
Expand Down Expand Up @@ -249,7 +249,7 @@ class SpindleLaser {
#if HAS_LCD_MENU
static inline void enable_with_dir(const bool reverse) {
isReady = true;
const uint8_t ocr = TERN(SPINDLE_LASER_PWM, upower_to_ocr(menuPower), 255);
const uint8_t ocr = TERN(SPINDLE_LASER_USE_PWM, upower_to_ocr(menuPower), 255);
if (menuPower)
power = ocr;
else
Expand All @@ -262,7 +262,7 @@ class SpindleLaser {
FORCE_INLINE static void enable_reverse() { enable_with_dir(true); }
FORCE_INLINE static void enable_same_dir() { enable_with_dir(is_reverse()); }

#if ENABLED(SPINDLE_LASER_PWM)
#if ENABLED(SPINDLE_LASER_USE_PWM)
static inline void update_from_mpower() {
if (isReady) power = upower_to_ocr(menuPower);
unitPower = menuPower;
Expand Down Expand Up @@ -308,14 +308,14 @@ class SpindleLaser {
isReady = false;
unitPower = menuPower = 0;
planner.laser_inline.status.isPlanned = false;
TERN(SPINDLE_LASER_PWM, inline_ocr_power, inline_power)(0);
TERN(SPINDLE_LASER_USE_PWM, inline_ocr_power, inline_power)(0);
}
}

// Set the power for subsequent movement blocks
static void inline_power(const cutter_power_t upwr) {
unitPower = menuPower = upwr;
#if ENABLED(SPINDLE_LASER_PWM)
#if ENABLED(SPINDLE_LASER_USE_PWM)
#if ENABLED(SPEED_POWER_RELATIVE) && !CUTTER_UNIT_IS(RPM) // relative mode does not turn laser off at 0, except for RPM
planner.laser_inline.status.isEnabled = true;
planner.laser_inline.power = upower_to_ocr(upwr);
Expand All @@ -332,7 +332,7 @@ class SpindleLaser {

static inline void inline_direction(const bool) { /* never */ }

#if ENABLED(SPINDLE_LASER_PWM)
#if ENABLED(SPINDLE_LASER_USE_PWM)
static inline void inline_ocr_power(const uint8_t ocrpwr) {
isReady = ocrpwr > 0;
planner.laser_inline.status.isEnabled = ocrpwr > 0;
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/gcode/control/M3-M5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void GcodeSuite::M3_M4(const bool is_M4) {
#if ENABLED(SPINDLE_SERVO)
cutter.unitPower = spwr;
#else
cutter.unitPower = TERN(SPINDLE_LASER_PWM,
cutter.unitPower = TERN(SPINDLE_LASER_USE_PWM,
cutter.power_to_range(cutter_power_t(round(spwr))),
spwr > 0 ? 255 : 0);
#endif
Expand All @@ -86,7 +86,7 @@ void GcodeSuite::M3_M4(const bool is_M4) {
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 ENABLED(SPINDLE_LASER_USE_PWM)
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)
Expand All @@ -105,7 +105,7 @@ void GcodeSuite::M3_M4(const bool is_M4) {
planner.synchronize(); // Wait for previous movement commands (G0/G0/G2/G3) to complete before changing power
cutter.set_reverse(is_M4);

#if ENABLED(SPINDLE_LASER_PWM)
#if ENABLED(SPINDLE_LASER_USE_PWM)
if (parser.seenval('O')) {
cutter.unitPower = cutter.power_to_range(parser.value_byte(), 0);
cutter.ocr_set_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t)
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/gcode/gcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void GcodeSuite::get_destination_from_command() {
// Set the laser power in the planner to configure this move
if (parser.seen('S')) {
const float spwr = parser.value_float();
cutter.inline_power(TERN(SPINDLE_LASER_PWM, cutter.power_to_range(cutter_power_t(round(spwr))), spwr > 0 ? 255 : 0));
cutter.inline_power(TERN(SPINDLE_LASER_USE_PWM, cutter.power_to_range(cutter_power_t(round(spwr))), spwr > 0 ? 255 : 0));
}
else if (ENABLED(LASER_MOVE_G0_OFF) && parser.codenum == 0) // G0
cutter.set_inline_enabled(false);
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/inc/Conditionals_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@
#endif

// Add features that need hardware PWM here
#if ANY(FAST_PWM_FAN, SPINDLE_LASER_PWM)
#if ANY(FAST_PWM_FAN, SPINDLE_LASER_USE_PWM)
#define NEEDS_HARDWARE_PWM 1
#endif

Expand Down
18 changes: 10 additions & 8 deletions Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,8 @@
#error "ARC_SUPPORT no longer uses ARC_SEGMENTS_PER_R."
#elif ENABLED(ARC_SUPPORT) && (!defined(MIN_ARC_SEGMENT_MM) || !defined(MAX_ARC_SEGMENT_MM))
#error "ARC_SUPPORT now requires MIN_ARC_SEGMENT_MM and MAX_ARC_SEGMENT_MM."
#elif defined(SPINDLE_LASER_PWM)
#error "SPINDLE_LASER_PWM (true) is now set with SPINDLE_LASER_USE_PWM (enabled)."
#endif

#if MOTHERBOARD == BOARD_DUE3DOM_MINI && PIN_EXISTS(TEMP_2) && DISABLED(TEMP_SENSOR_BOARD)
Expand Down Expand Up @@ -3558,8 +3560,8 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2)
#error "LASER_MOVE_G0_OFF requires LASER_MOVE_POWER."
#endif
#if ENABLED(LASER_POWER_INLINE_TRAPEZOID)
#if DISABLED(SPINDLE_LASER_PWM)
#error "LASER_POWER_INLINE_TRAPEZOID requires SPINDLE_LASER_PWM to function."
#if DISABLED(SPINDLE_LASER_USE_PWM)
#error "LASER_POWER_INLINE_TRAPEZOID requires SPINDLE_LASER_USE_PWM to function."
#elif ENABLED(S_CURVE_ACCELERATION)
//#ifndef LASER_POWER_INLINE_S_CURVE_ACCELERATION_WARN
// #define LASER_POWER_INLINE_S_CURVE_ACCELERATION_WARN
Expand Down Expand Up @@ -3591,21 +3593,21 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2)
#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)
#elif ENABLED(SPINDLE_LASER_USE_PWM)
#if !defined(SPINDLE_LASER_PWM_PIN) || SPINDLE_LASER_PWM_PIN < 0
#error "SPINDLE_LASER_PWM_PIN is required for SPINDLE_LASER_PWM."
#error "SPINDLE_LASER_PWM_PIN is required for SPINDLE_LASER_USE_PWM."
#elif !_TEST_PWM(SPINDLE_LASER_PWM_PIN)
#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_INTERCEPT) && defined(SPEED_POWER_MIN) && defined(SPEED_POWER_MAX) && defined(SPEED_POWER_STARTUP))
#error "SPINDLE_LASER_PWM equation constant(s) missing."
#error "SPINDLE_LASER_USE_PWM equation constant(s) missing."
#elif _PIN_CONFLICT(X_MIN)
#error "SPINDLE_LASER_PWM pin conflicts with X_MIN_PIN."
#error "SPINDLE_LASER_USE_PWM pin conflicts with X_MIN_PIN."
#elif _PIN_CONFLICT(X_MAX)
#error "SPINDLE_LASER_PWM pin conflicts with X_MAX_PIN."
#error "SPINDLE_LASER_USE_PWM pin conflicts with X_MAX_PIN."
#elif _PIN_CONFLICT(Z_STEP)
#error "SPINDLE_LASER_PWM pin conflicts with Z_STEP_PIN."
#error "SPINDLE_LASER_USE_PWM pin conflicts with Z_STEP_PIN."
#elif _PIN_CONFLICT(CASE_LIGHT)
#error "SPINDLE_LASER_PWM_PIN conflicts with CASE_LIGHT_PIN."
#elif _PIN_CONFLICT(E0_AUTO_FAN)
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/menu/menu_spindle_laser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
START_MENU();
BACK_ITEM(MSG_MAIN);

#if ENABLED(SPINDLE_LASER_PWM)
#if ENABLED(SPINDLE_LASER_USE_PWM)
// Change the cutter's "current power" value without turning the cutter on or off
// 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,
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/module/stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,7 @@ uint32_t Stepper::block_phase_isr() {
}
#else
if (stat.isPlanned) { // Planner controls the laser
#if ENABLED(SPINDLE_LASER_PWM)
#if ENABLED(SPINDLE_LASER_USE_PWM)
cutter.ocr_set_power(
stat.isEnabled ? current_block->laser.power : 0 // ON with power or OFF
);
Expand Down Expand Up @@ -2303,7 +2303,7 @@ uint32_t Stepper::block_phase_isr() {
// This should mean ending file with 'M5 I' will stop the laser; thus the inline flag isn't needed
const power_status_t stat = planner.laser_inline.status;
if (stat.isPlanned) { // Planner controls the laser
#if ENABLED(SPINDLE_LASER_PWM)
#if ENABLED(SPINDLE_LASER_USE_PWM)
cutter.ocr_set_power(
stat.isEnabled ? planner.laser_inline.power : 0 // ON with power or OFF
);
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/module/stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,11 @@ class Stepper {
uint8_t cur_power; // Current laser power
bool cruise_set; // Power set up for cruising?

#if DISABLED(LASER_POWER_INLINE_TRAPEZOID_CONT)
#if ENABLED(LASER_POWER_INLINE_TRAPEZOID_CONT)
uint16_t till_update; // Countdown to the next update
#else
uint32_t last_step_count, // Step count from the last update
acc_step_count; // Bresenham counter for laser accel/decel
#else
uint16_t till_update; // Countdown to the next update
#endif
} stepper_laser_t;

Expand Down

0 comments on commit de4eed3

Please sign in to comment.