2017-09-06 06:28:31 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-06 06:28:31 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-09-06 06:28:31 -05:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-09-16 01:07:00 -05:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
|
|
|
|
2019-06-27 23:06:49 -05:00
|
|
|
#if HAS_CUTTER
|
2017-09-16 01:07:00 -05:00
|
|
|
|
|
|
|
#include "../gcode.h"
|
2019-06-27 23:06:49 -05:00
|
|
|
#include "../../feature/spindle_laser.h"
|
2017-09-16 01:07:00 -05:00
|
|
|
#include "../../module/stepper.h"
|
|
|
|
|
2017-09-06 06:28:31 -05:00
|
|
|
/**
|
2019-10-15 16:10:20 -05:00
|
|
|
* Laser:
|
|
|
|
* M3 - Laser ON/Power (Ramped power)
|
|
|
|
* M4 - Laser ON/Power (Continuous power)
|
|
|
|
*
|
|
|
|
* Spindle:
|
|
|
|
* M3 - Spindle ON (Clockwise)
|
|
|
|
* M4 - Spindle ON (Counter-clockwise)
|
|
|
|
*
|
2020-06-08 00:47:31 -05:00
|
|
|
* Parameters:
|
|
|
|
* S<power> - Set power. S0 will turn the spindle/laser off, except in relative mode.
|
|
|
|
* O<ocr> - Set power and OCR (oscillator count register)
|
2017-09-06 06:28:31 -05:00
|
|
|
*
|
2019-06-27 23:06:49 -05:00
|
|
|
* If no PWM pin is defined then M3/M4 just turns it on.
|
2017-09-06 06:28:31 -05:00
|
|
|
*
|
2019-06-27 23:06:49 -05:00
|
|
|
* At least 12.8KHz (50Hz * 256) is needed for Spindle PWM.
|
|
|
|
* Hardware PWM is required on AVR. ISRs are too slow.
|
2017-09-06 06:28:31 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The system automatically sets WGM to Mode 1, so no special
|
|
|
|
* initialization is needed.
|
|
|
|
*
|
|
|
|
* WGM bits for timer 2 are automatically set by the system to
|
|
|
|
* Mode 1. This produces an acceptable 0 to 5 volt signal.
|
|
|
|
* No special initialization is needed.
|
|
|
|
*
|
|
|
|
* NOTE: A minimum PWM frequency of 50 Hz is needed. All prescaler
|
|
|
|
* factors for timers 2, 3, 4, and 5 are acceptable.
|
|
|
|
*
|
2019-04-08 17:52:33 -05:00
|
|
|
* SPINDLE_LASER_ENA_PIN needs an external pullup or it may power on
|
2017-09-06 06:28:31 -05:00
|
|
|
* the spindle/laser during power-up or when connecting to the host
|
|
|
|
* (usually goes through a reset which sets all I/O pins to tri-state)
|
|
|
|
*
|
|
|
|
* PWM duty cycle goes from 0 (off) to 255 (always on).
|
|
|
|
*/
|
2019-01-20 23:41:47 -06:00
|
|
|
void GcodeSuite::M3_M4(const bool is_M4) {
|
2020-06-08 00:47:31 -05:00
|
|
|
auto get_s_power = [] {
|
2020-06-16 15:49:32 -05:00
|
|
|
if (parser.seenval('S')) {
|
|
|
|
const float spwr = parser.value_float();
|
|
|
|
cutter.unitPower = TERN(SPINDLE_LASER_PWM,
|
|
|
|
cutter.power_to_range(cutter_power_t(round(spwr))),
|
|
|
|
spwr > 0 ? 255 : 0);
|
|
|
|
}
|
2020-06-08 00:47:31 -05:00
|
|
|
else
|
|
|
|
cutter.unitPower = cutter.cpwr_to_upwr(SPEED_POWER_STARTUP);
|
|
|
|
return cutter.unitPower;
|
|
|
|
};
|
2019-01-20 23:41:47 -06:00
|
|
|
|
2020-04-02 19:31:08 -05:00
|
|
|
#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)
|
2020-06-08 00:47:31 -05:00
|
|
|
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)
|
|
|
|
}
|
2020-04-02 19:31:08 -05:00
|
|
|
else
|
2020-06-08 00:47:31 -05:00
|
|
|
cutter.inline_power(cutter.upower_to_ocr(get_s_power()));
|
2020-04-02 19:31:08 -05:00
|
|
|
#else
|
2020-06-16 15:49:32 -05:00
|
|
|
cutter.set_inline_enabled(true);
|
2020-04-02 19:31:08 -05:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Non-inline, standard case
|
|
|
|
cutter.inline_disable(); // Prevent future blocks re-setting the power
|
2019-10-15 16:10:20 -05:00
|
|
|
#endif
|
2019-01-20 23:41:47 -06:00
|
|
|
|
2020-04-02 19:31:08 -05:00
|
|
|
planner.synchronize(); // Wait for previous movement commands (G0/G0/G2/G3) to complete before changing power
|
2019-06-27 23:06:49 -05:00
|
|
|
cutter.set_direction(is_M4);
|
2017-09-06 06:28:31 -05:00
|
|
|
|
|
|
|
#if ENABLED(SPINDLE_LASER_PWM)
|
2020-06-08 00:47:31 -05:00
|
|
|
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)
|
|
|
|
}
|
2019-06-27 23:06:49 -05:00
|
|
|
else
|
2020-06-08 00:47:31 -05:00
|
|
|
cutter.set_power(cutter.upower_to_ocr(get_s_power()));
|
2017-09-06 06:28:31 -05:00
|
|
|
#else
|
2019-06-27 23:06:49 -05:00
|
|
|
cutter.set_enabled(true);
|
2017-09-06 06:28:31 -05:00
|
|
|
#endif
|
2020-06-08 00:47:31 -05:00
|
|
|
cutter.menuPower = cutter.unitPower;
|
2017-09-06 06:28:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-02 19:31:08 -05:00
|
|
|
* M5 - Cutter OFF (when moves are complete)
|
2017-09-16 01:07:00 -05:00
|
|
|
*/
|
|
|
|
void GcodeSuite::M5() {
|
2020-04-02 19:31:08 -05:00
|
|
|
#if ENABLED(LASER_POWER_INLINE)
|
|
|
|
if (parser.seen('I') == DISABLED(LASER_POWER_INLINE_INVERT)) {
|
2020-06-08 00:47:31 -05:00
|
|
|
cutter.set_inline_enabled(false); // Laser power in inline mode
|
2020-04-02 19:31:08 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Non-inline, standard case
|
|
|
|
cutter.inline_disable(); // Prevent future blocks re-setting the power
|
2019-10-15 16:10:20 -05:00
|
|
|
#endif
|
2020-04-02 19:31:08 -05:00
|
|
|
planner.synchronize();
|
2019-06-27 23:06:49 -05:00
|
|
|
cutter.set_enabled(false);
|
2020-06-08 00:47:31 -05:00
|
|
|
cutter.menuPower = cutter.unitPower;
|
2017-09-06 06:28:31 -05:00
|
|
|
}
|
2017-09-16 01:07:00 -05:00
|
|
|
|
2019-06-27 23:06:49 -05:00
|
|
|
#endif // HAS_CUTTER
|