Singleton for cutting tools (#14429)
This commit is contained in:
@ -22,23 +22,23 @@
|
||||
|
||||
#include "../../inc/MarlinConfig.h"
|
||||
|
||||
#if ENABLED(SPINDLE_LASER_ENABLE)
|
||||
#if HAS_CUTTER
|
||||
|
||||
#include "../gcode.h"
|
||||
#include "../../feature/spindle_laser.h"
|
||||
#include "../../module/stepper.h"
|
||||
|
||||
uint8_t spindle_laser_power; // = 0
|
||||
|
||||
/**
|
||||
* M3: Spindle Clockwise
|
||||
* M4: Spindle Counter-clockwise
|
||||
* M3 - Cutter ON (Clockwise)
|
||||
* M4 - Cutter ON (Counter-clockwise)
|
||||
*
|
||||
* S0 turns off spindle.
|
||||
* S<power> - Set power. S0 turns it off.
|
||||
* O<ocr> - Set power and OCR
|
||||
*
|
||||
* If no speed PWM output is defined then M3/M4 just turns it on.
|
||||
* If no PWM pin is defined then M3/M4 just turns it on.
|
||||
*
|
||||
* At least 12.8KHz (50Hz * 256) is needed for spindle PWM.
|
||||
* Hardware PWM is required. ISRs are too slow.
|
||||
* At least 12.8KHz (50Hz * 256) is needed for Spindle PWM.
|
||||
* Hardware PWM is required on AVR. ISRs are too slow.
|
||||
*
|
||||
* NOTE: WGM for timers 3, 4, and 5 must be either Mode 1 or Mode 5.
|
||||
* No other settings give a PWM signal that goes from 0 to 5 volts.
|
||||
@ -59,114 +59,28 @@ uint8_t spindle_laser_power; // = 0
|
||||
*
|
||||
* PWM duty cycle goes from 0 (off) to 255 (always on).
|
||||
*/
|
||||
|
||||
// Wait for spindle to come up to speed
|
||||
inline void delay_for_power_up() { safe_delay(SPINDLE_LASER_POWERUP_DELAY); }
|
||||
|
||||
// Wait for spindle to stop turning
|
||||
inline void delay_for_power_down() { safe_delay(SPINDLE_LASER_POWERDOWN_DELAY); }
|
||||
|
||||
/**
|
||||
* ocr_val_mode() is used for debugging and to get the points needed to compute the RPM vs ocr_val line
|
||||
*
|
||||
* it accepts inputs of 0-255
|
||||
*/
|
||||
|
||||
inline void set_spindle_laser_ocr(const uint8_t ocr) {
|
||||
WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ENABLE_INVERT); // turn spindle on (active low)
|
||||
#if ENABLED(SPINDLE_LASER_PWM)
|
||||
analogWrite(SPINDLE_LASER_PWM_PIN, (SPINDLE_LASER_PWM_INVERT) ? 255 - ocr : ocr);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if ENABLED(SPINDLE_LASER_PWM)
|
||||
|
||||
void update_spindle_laser_power() {
|
||||
if (spindle_laser_power == 0) {
|
||||
WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ENABLE_INVERT); // turn spindle off (active low)
|
||||
analogWrite(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // only write low byte
|
||||
delay_for_power_down();
|
||||
}
|
||||
else { // Convert RPM to PWM duty cycle
|
||||
constexpr float inv_slope = 1.0f / (SPEED_POWER_SLOPE),
|
||||
min_ocr = (SPEED_POWER_MIN - (SPEED_POWER_INTERCEPT)) * inv_slope, // Minimum allowed
|
||||
max_ocr = (SPEED_POWER_MAX - (SPEED_POWER_INTERCEPT)) * inv_slope; // Maximum allowed
|
||||
int16_t ocr_val;
|
||||
if (spindle_laser_power <= SPEED_POWER_MIN) ocr_val = min_ocr; // Use minimum if set below
|
||||
else if (spindle_laser_power >= SPEED_POWER_MAX) ocr_val = max_ocr; // Use maximum if set above
|
||||
else ocr_val = (spindle_laser_power - (SPEED_POWER_INTERCEPT)) * inv_slope; // Use calculated OCR value
|
||||
set_spindle_laser_ocr(ocr_val & 0xFF); // ...limited to Atmel PWM max
|
||||
delay_for_power_up();
|
||||
}
|
||||
}
|
||||
|
||||
#endif // SPINDLE_LASER_PWM
|
||||
|
||||
bool spindle_laser_enabled() {
|
||||
return !!spindle_laser_power; // READ(SPINDLE_LASER_ENA_PIN) == SPINDLE_LASER_ENABLE_INVERT;
|
||||
}
|
||||
|
||||
void set_spindle_laser_enabled(const bool enable) {
|
||||
// Enabled by PWM setting elsewhere
|
||||
spindle_laser_power = enable ? 255 : 0;
|
||||
#if ENABLED(SPINDLE_LASER_PWM)
|
||||
update_spindle_laser_power();
|
||||
#else
|
||||
if (enable) {
|
||||
WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ENABLE_INVERT);
|
||||
delay_for_power_up();
|
||||
}
|
||||
else {
|
||||
WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ENABLE_INVERT);
|
||||
delay_for_power_down();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if SPINDLE_DIR_CHANGE
|
||||
|
||||
void set_spindle_direction(const bool reverse_dir) {
|
||||
const bool dir_state = (reverse_dir == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
|
||||
if (SPINDLE_STOP_ON_DIR_CHANGE && spindle_laser_enabled() && READ(SPINDLE_DIR_PIN) != dir_state)
|
||||
set_spindle_laser_enabled(false);
|
||||
WRITE(SPINDLE_DIR_PIN, dir_state);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void GcodeSuite::M3_M4(const bool is_M4) {
|
||||
|
||||
planner.synchronize(); // wait until previous movement commands (G0/G0/G2/G3) have completed before playing with the spindle
|
||||
planner.synchronize(); // Wait for previous movement commands (G0/G0/G2/G3) to complete before changing power
|
||||
|
||||
#if SPINDLE_DIR_CHANGE
|
||||
set_spindle_direction(is_M4);
|
||||
#endif
|
||||
cutter.set_direction(is_M4);
|
||||
|
||||
/**
|
||||
* Our final value for ocr_val is an unsigned 8 bit value between 0 and 255 which usually means uint8_t.
|
||||
* Went to uint16_t because some of the uint8_t calculations would sometimes give 1000 0000 rather than 1111 1111.
|
||||
* Then needed to AND the uint16_t result with 0x00FF to make sure we only wrote the byte of interest.
|
||||
*/
|
||||
#if ENABLED(SPINDLE_LASER_PWM)
|
||||
if (parser.seen('O')) {
|
||||
spindle_laser_power = parser.value_byte();
|
||||
set_spindle_laser_ocr(spindle_laser_power);
|
||||
}
|
||||
else {
|
||||
spindle_laser_power = parser.intval('S', 255);
|
||||
update_spindle_laser_power();
|
||||
}
|
||||
if (parser.seen('O'))
|
||||
cutter.set_ocr_power(parser.value_byte()); // The OCR is a value from 0 to 255 (uint8_t)
|
||||
else
|
||||
cutter.set_power(parser.intval('S', 255));
|
||||
#else
|
||||
set_spindle_laser_enabled(true);
|
||||
cutter.set_enabled(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* M5 turn off spindle
|
||||
* M5 - Cutter OFF
|
||||
*/
|
||||
void GcodeSuite::M5() {
|
||||
planner.synchronize();
|
||||
set_spindle_laser_enabled(false);
|
||||
cutter.set_enabled(false);
|
||||
}
|
||||
|
||||
#endif // SPINDLE_LASER_ENABLE
|
||||
#endif // HAS_CUTTER
|
||||
|
@ -322,7 +322,7 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
|
||||
case 1: M0_M1(); break; // M1: Conditional stop - Wait for user button press on LCD
|
||||
#endif
|
||||
|
||||
#if ENABLED(SPINDLE_LASER_ENABLE)
|
||||
#if HAS_CUTTER
|
||||
case 3: M3_M4(false); break; // M3: Turn ON Laser | Spindle (clockwise), set Power | Speed
|
||||
case 4: M3_M4(true ); break; // M4: Turn ON Laser | Spindle (counter-clockwise), set Power | Speed
|
||||
case 5: M5(); break; // M5: Turn OFF Laser | Spindle
|
||||
|
@ -75,9 +75,9 @@
|
||||
*
|
||||
* M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
|
||||
* M1 -> M0
|
||||
* M3 - Turn ON Laser | Spindle (clockwise), set Power | Speed. (Requires SPINDLE_LASER_ENABLE)
|
||||
* M4 - Turn ON Laser | Spindle (counter-clockwise), set Power | Speed. (Requires SPINDLE_LASER_ENABLE)
|
||||
* M5 - Turn OFF Laser | Spindle. (Requires SPINDLE_LASER_ENABLE)
|
||||
* M3 - Turn ON Laser | Spindle (clockwise), set Power | Speed. (Requires SPINDLE_FEATURE or LASER_FEATURE)
|
||||
* M4 - Turn ON Laser | Spindle (counter-clockwise), set Power | Speed. (Requires SPINDLE_FEATURE or LASER_FEATURE)
|
||||
* M5 - Turn OFF Laser | Spindle. (Requires SPINDLE_FEATURE or LASER_FEATURE)
|
||||
* M7 - Turn mist coolant ON. (Requires COOLANT_CONTROL)
|
||||
* M8 - Turn flood coolant ON. (Requires COOLANT_CONTROL)
|
||||
* M9 - Turn coolant OFF. (Requires COOLANT_CONTROL)
|
||||
@ -453,7 +453,7 @@ private:
|
||||
static void M0_M1();
|
||||
#endif
|
||||
|
||||
#if ENABLED(SPINDLE_LASER_ENABLE)
|
||||
#if HAS_CUTTER
|
||||
static void M3_M4(const bool is_M4);
|
||||
static void M5();
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user