1
0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2024-11-23 12:04:19 +00:00

🎨 Misc. Spindle/Laser (etc.) cleanup

This commit is contained in:
Scott Lahteine 2021-09-06 15:34:12 -05:00
parent 3a83516232
commit 8a4fec9460
5 changed files with 70 additions and 58 deletions

View File

@ -52,9 +52,9 @@ cutter_power_t SpindleLaser::menuPower, // Power s
#endif
#define SPINDLE_LASER_PWM_OFF TERN(SPINDLE_LASER_PWM_INVERT, 255, 0)
//
// Init the cutter to a safe OFF state
//
/**
* Init the cutter to a safe OFF state
*/
void SpindleLaser::init() {
#if ENABLED(SPINDLE_SERVO)
MOVE_SERVO(SPINDLE_SERVO_NR, SPINDLE_SERVO_MIN);
@ -86,6 +86,8 @@ void SpindleLaser::init() {
#if ENABLED(SPINDLE_LASER_PWM)
/**
* Set the cutter PWM directly to the given ocr value
*
* @param ocr Power value
*/
void SpindleLaser::_set_ocr(const uint8_t ocr) {
#if NEEDS_HARDWARE_PWM && SPINDLE_LASER_FREQUENCY
@ -105,11 +107,15 @@ void SpindleLaser::init() {
WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Cutter OFF
_set_ocr(0);
}
#endif
#endif // SPINDLE_LASER_PWM
//
// Set cutter ON/OFF state (and PWM) to the given cutter power value
//
/**
* Apply power for laser/spindle
*
* Apply cutter power value for PWM, Servo, and on/off pin.
*
* @param opwr Power value. Range 0 to MAX. When 0 disable spindle/laser.
*/
void SpindleLaser::apply_power(const uint8_t opwr) {
static uint8_t last_power_applied = 0;
if (opwr == last_power_applied) return;
@ -137,10 +143,10 @@ void SpindleLaser::apply_power(const uint8_t opwr) {
}
#if ENABLED(SPINDLE_CHANGE_DIR)
//
// Set the spindle direction and apply immediately
// Stop on direction change if SPINDLE_STOP_ON_DIR_CHANGE is enabled
//
/**
* Set the spindle direction and apply immediately
* Stop on direction change if SPINDLE_STOP_ON_DIR_CHANGE is enabled
*/
void SpindleLaser::set_reverse(const bool reverse) {
const bool dir_state = (reverse == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
if (TERN0(SPINDLE_STOP_ON_DIR_CHANGE, enabled()) && READ(SPINDLE_DIR_PIN) != dir_state) disable();
@ -149,25 +155,17 @@ void SpindleLaser::apply_power(const uint8_t opwr) {
#endif
#if ENABLED(AIR_EVACUATION)
// Enable / disable Cutter Vacuum or Laser Blower motor
void SpindleLaser::air_evac_enable() { WRITE(AIR_EVACUATION_PIN, AIR_EVACUATION_ACTIVE); } // Turn ON
void SpindleLaser::air_evac_disable() { WRITE(AIR_EVACUATION_PIN, !AIR_EVACUATION_ACTIVE); } // Turn OFF
void SpindleLaser::air_evac_toggle() { TOGGLE(AIR_EVACUATION_PIN); } // Toggle state
#endif // AIR_EVACUATION
#endif
#if ENABLED(AIR_ASSIST)
// Enable / disable air assist
void SpindleLaser::air_assist_enable() { WRITE(AIR_ASSIST_PIN, AIR_ASSIST_PIN); } // Turn ON
void SpindleLaser::air_assist_disable() { WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_PIN); } // Turn OFF
void SpindleLaser::air_assist_toggle() { TOGGLE(AIR_ASSIST_PIN); } // Toggle state
#endif // AIR_ASSIST
#endif
#endif // HAS_CUTTER

View File

@ -132,54 +132,50 @@ public:
public:
static void set_ocr(const uint8_t ocr);
static inline void set_ocr_power(const uint8_t ocr) { power = ocr; set_ocr(ocr); }
static inline void ocr_set_power(const uint8_t ocr) { power = ocr; set_ocr(ocr); }
static void ocr_off();
// Used to update output for power->OCR translation
/**
* Update output for power->OCR translation
*/
static inline uint8_t upower_to_ocr(const cutter_power_t upwr) {
return (
return uint8_t(
#if CUTTER_UNIT_IS(PWM255)
uint8_t(upwr)
upwr
#elif CUTTER_UNIT_IS(PERCENT)
pct_to_ocr(upwr)
#else
uint8_t(pct_to_ocr(cpwr_to_pct(upwr)))
pct_to_ocr(cpwr_to_pct(upwr))
#endif
);
}
// Correct power to configured range
/**
* Correct power to configured range
*/
static inline cutter_power_t power_to_range(const cutter_power_t pwr) {
return power_to_range(pwr, (
#if CUTTER_UNIT_IS(PWM255)
0
#elif CUTTER_UNIT_IS(PERCENT)
1
#elif CUTTER_UNIT_IS(RPM)
2
#else
#error "CUTTER_UNIT_IS(unknown)"
#endif
));
return power_to_range(pwr, _CUTTER_POWER(CUTTER_POWER_UNIT));
}
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 _CUTTER_POWER_PWM255:
upwr = cutter_power_t(
(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 _CUTTER_POWER_PERCENT:
upwr = cutter_power_t(
(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 _CUTTER_POWER_RPM:
upwr = cutter_power_t(
(pwr < SPEED_POWER_MIN) ? SPEED_POWER_MIN // Use minimum if set below
: (pwr > SPEED_POWER_MAX) ? SPEED_POWER_MAX // Use maximum if set above
@ -190,14 +186,34 @@ public:
}
return upwr;
}
#endif // SPINDLE_LASER_PWM
/**
* Enable/Disable spindle/laser
* @param enable true = enable; false = disable
*/
static inline void set_enabled(const bool enable) {
set_power(enable ? TERN(SPINDLE_LASER_PWM, (power ?: (unitPower ? upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP)) : 0)), 255) : 0);
uint8_t value = 0;
if (enable) {
#if ENABLED(SPINDLE_LASER_PWM)
if (power)
value = power;
else if (unitPower)
value = upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP));
#else
value = 255;
#endif
}
set_power(value);
}
// Wait for spindle to spin up or spin down
static inline void disable() { isReady = false; set_enabled(false); }
/**
* Wait for spindle to spin up or spin down
*
* @param on true = state to on; false = state to off.
*/
static inline void power_delay(const bool on) {
#if DISABLED(LASER_POWER_INLINE)
safe_delay(on ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY);
@ -230,8 +246,6 @@ public:
}
#endif
static inline void disable() { isReady = false; set_enabled(false); }
#if HAS_LCD_MENU
static inline void enable_with_dir(const bool reverse) {
isReady = true;
@ -325,7 +339,7 @@ public:
planner.laser_inline.power = ocrpwr;
}
#endif
#endif // LASER_POWER_INLINE
#endif // LASER_POWER_INLINE
static inline void kill() {
TERN_(LASER_POWER_INLINE, inline_disable());

View File

@ -108,7 +108,7 @@ void GcodeSuite::M3_M4(const bool is_M4) {
#if ENABLED(SPINDLE_LASER_PWM)
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)
cutter.ocr_set_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()));

View File

@ -1914,7 +1914,7 @@ uint32_t Stepper::block_phase_isr() {
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_trap.cur_power);
cutter.ocr_set_power(laser_trap.cur_power);
}
}
#else
@ -1923,7 +1923,7 @@ uint32_t Stepper::block_phase_isr() {
else {
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
cutter.ocr_set_power(laser_trap.cur_power); // Cycle efficiency is irrelevant it the last line was many cycles
}
#endif
}
@ -1991,7 +1991,7 @@ uint32_t Stepper::block_phase_isr() {
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_trap.cur_power);
cutter.ocr_set_power(laser_trap.cur_power);
}
}
#else
@ -2000,7 +2000,7 @@ uint32_t Stepper::block_phase_isr() {
else {
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
cutter.ocr_set_power(laser_trap.cur_power); // Cycle efficiency isn't relevant when the last line was many cycles
}
#endif
}
@ -2028,7 +2028,7 @@ uint32_t Stepper::block_phase_isr() {
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);
cutter.ocr_set_power(laser_trap.cur_power);
laser_trap.cruise_set = true;
}
#if ENABLED(LASER_POWER_INLINE_TRAPEZOID_CONT)
@ -2249,14 +2249,14 @@ uint32_t Stepper::block_phase_isr() {
#endif
// Always have PWM in this case
if (stat.isPlanned) { // Planner controls the laser
cutter.set_ocr_power(
cutter.ocr_set_power(
stat.isEnabled ? laser_trap.cur_power : 0 // ON with power or OFF
);
}
#else
if (stat.isPlanned) { // Planner controls the laser
#if ENABLED(SPINDLE_LASER_PWM)
cutter.set_ocr_power(
cutter.ocr_set_power(
stat.isEnabled ? current_block->laser.power : 0 // ON with power or OFF
);
#else
@ -2304,7 +2304,7 @@ uint32_t Stepper::block_phase_isr() {
const power_status_t stat = planner.laser_inline.status;
if (stat.isPlanned) { // Planner controls the laser
#if ENABLED(SPINDLE_LASER_PWM)
cutter.set_ocr_power(
cutter.ocr_set_power(
stat.isEnabled ? planner.laser_inline.power : 0 // ON with power or OFF
);
#else

View File

@ -23,9 +23,9 @@
/**
* Protoneer v3.00 pin assignments
*
*
* This CNC shield has an UNO pinout and fits all Arduino-compatibles.
*
*
* Referenced docs:
* - https://blog.protoneer.co.nz/arduino-cnc-shield-v3-00-assembly-guide/
* - https://blog.protoneer.co.nz/arduino-cnc-shield/