2019-06-27 23:06:49 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2019-06-27 23:06:49 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2019-06-27 23:06:49 -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
|
2020-07-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-06-27 23:06:49 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* feature/spindle_laser.cpp
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
|
|
|
#if HAS_CUTTER
|
|
|
|
|
|
|
|
#include "spindle_laser.h"
|
|
|
|
|
2020-10-31 17:42:05 -05:00
|
|
|
#if ENABLED(SPINDLE_SERVO)
|
|
|
|
#include "../module/servo.h"
|
|
|
|
#endif
|
|
|
|
|
2019-06-27 23:06:49 -05:00
|
|
|
SpindleLaser cutter;
|
2020-06-08 00:47:31 -05:00
|
|
|
uint8_t SpindleLaser::power;
|
2021-01-10 06:01:25 -06:00
|
|
|
#if ENABLED(LASER_FEATURE)
|
|
|
|
cutter_test_pulse_t SpindleLaser::testPulse = 50; // Test fire Pulse time ms value.
|
|
|
|
#endif
|
2020-06-08 00:47:31 -05:00
|
|
|
bool SpindleLaser::isReady; // Ready to apply power setting from the UI to OCR
|
|
|
|
cutter_power_t SpindleLaser::menuPower, // Power set via LCD menu in PWM, PERCENT, or RPM
|
|
|
|
SpindleLaser::unitPower; // LCD status power in PWM, PERCENT, or RPM
|
2019-06-27 23:06:49 -05:00
|
|
|
|
2020-04-02 19:31:08 -05:00
|
|
|
#if ENABLED(MARLIN_DEV_MODE)
|
2020-08-21 23:13:43 -05:00
|
|
|
cutter_frequency_t SpindleLaser::frequency; // PWM frequency setting; range: 2K - 50K
|
2020-04-02 19:31:08 -05:00
|
|
|
#endif
|
2020-12-26 19:01:54 -06:00
|
|
|
#define SPINDLE_LASER_PWM_OFF TERN(SPINDLE_LASER_PWM_INVERT, 255, 0)
|
2019-10-15 16:10:20 -05:00
|
|
|
|
2020-04-02 19:31:08 -05:00
|
|
|
//
|
|
|
|
// Init the cutter to a safe OFF state
|
|
|
|
//
|
2019-06-27 23:06:49 -05:00
|
|
|
void SpindleLaser::init() {
|
2020-10-31 17:42:05 -05:00
|
|
|
#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
|
2019-06-27 23:06:49 -05:00
|
|
|
#if ENABLED(SPINDLE_CHANGE_DIR)
|
2020-06-08 00:47:31 -05:00
|
|
|
OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // Init rotation to clockwise (M3)
|
2019-06-27 23:06:49 -05:00
|
|
|
#endif
|
2019-10-15 16:10:20 -05:00
|
|
|
#if ENABLED(SPINDLE_LASER_PWM)
|
2019-06-27 23:06:49 -05:00
|
|
|
SET_PWM(SPINDLE_LASER_PWM_PIN);
|
2020-08-21 23:13:43 -05:00
|
|
|
analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Set to lowest speed
|
2019-06-27 23:06:49 -05:00
|
|
|
#endif
|
2020-04-02 19:31:08 -05:00
|
|
|
#if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY)
|
|
|
|
set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
|
2020-04-22 16:35:03 -05:00
|
|
|
TERN_(MARLIN_DEV_MODE, frequency = SPINDLE_LASER_FREQUENCY);
|
2020-04-02 19:31:08 -05:00
|
|
|
#endif
|
2019-06-27 23:06:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(SPINDLE_LASER_PWM)
|
|
|
|
/**
|
2020-06-08 00:47:31 -05:00
|
|
|
* Set the cutter PWM directly to the given ocr value
|
|
|
|
*/
|
2020-12-26 19:01:54 -06:00
|
|
|
void SpindleLaser::_set_ocr(const uint8_t ocr) {
|
2020-08-02 20:16:12 -05:00
|
|
|
#if NEEDS_HARDWARE_PWM && SPINDLE_LASER_FREQUENCY
|
2020-12-26 19:01:54 -06:00
|
|
|
set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), TERN(MARLIN_DEV_MODE, frequency, SPINDLE_LASER_FREQUENCY));
|
2020-08-02 20:16:12 -05:00
|
|
|
set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
|
2020-12-26 19:01:54 -06:00
|
|
|
#else
|
|
|
|
analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
|
2020-08-06 00:49:15 -05:00
|
|
|
#endif
|
2019-06-27 23:06:49 -05:00
|
|
|
}
|
2020-12-26 19:01:54 -06:00
|
|
|
|
|
|
|
void SpindleLaser::set_ocr(const uint8_t ocr) {
|
|
|
|
WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_STATE); // Cutter ON
|
|
|
|
_set_ocr(ocr);
|
|
|
|
}
|
|
|
|
|
2020-06-08 00:47:31 -05:00
|
|
|
void SpindleLaser::ocr_off() {
|
2020-12-26 19:01:54 -06:00
|
|
|
WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Cutter OFF
|
|
|
|
_set_ocr(0);
|
2020-06-08 00:47:31 -05:00
|
|
|
}
|
2019-06-27 23:06:49 -05:00
|
|
|
#endif
|
|
|
|
|
2020-04-02 19:31:08 -05:00
|
|
|
//
|
2020-06-08 00:47:31 -05:00
|
|
|
// Set cutter ON/OFF state (and PWM) to the given cutter power value
|
2020-04-02 19:31:08 -05:00
|
|
|
//
|
2020-06-08 00:47:31 -05:00
|
|
|
void SpindleLaser::apply_power(const uint8_t opwr) {
|
|
|
|
static uint8_t last_power_applied = 0;
|
|
|
|
if (opwr == last_power_applied) return;
|
|
|
|
last_power_applied = opwr;
|
|
|
|
power = opwr;
|
2019-06-27 23:06:49 -05:00
|
|
|
#if ENABLED(SPINDLE_LASER_PWM)
|
2020-06-08 00:47:31 -05:00
|
|
|
if (cutter.unitPower == 0 && CUTTER_UNIT_IS(RPM)) {
|
|
|
|
ocr_off();
|
|
|
|
isReady = false;
|
|
|
|
}
|
2020-12-26 19:01:54 -06:00
|
|
|
else if (ENABLED(CUTTER_POWER_RELATIVE) || enabled()) {
|
2020-06-08 00:47:31 -05:00
|
|
|
set_ocr(power);
|
|
|
|
isReady = true;
|
|
|
|
}
|
2019-10-15 16:10:20 -05:00
|
|
|
else {
|
2020-06-08 00:47:31 -05:00
|
|
|
ocr_off();
|
|
|
|
isReady = false;
|
2019-06-27 23:06:49 -05:00
|
|
|
}
|
2020-10-31 17:42:05 -05:00
|
|
|
#elif ENABLED(SPINDLE_SERVO)
|
|
|
|
MOVE_SERVO(SPINDLE_SERVO_NR, power);
|
2019-06-27 23:06:49 -05:00
|
|
|
#else
|
2020-08-21 23:13:43 -05:00
|
|
|
WRITE(SPINDLE_LASER_ENA_PIN, enabled() ? SPINDLE_LASER_ACTIVE_STATE : !SPINDLE_LASER_ACTIVE_STATE);
|
2020-06-08 00:47:31 -05:00
|
|
|
isReady = true;
|
2019-06-27 23:06:49 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(SPINDLE_CHANGE_DIR)
|
2020-04-02 19:31:08 -05:00
|
|
|
//
|
|
|
|
// Set the spindle direction and apply immediately
|
|
|
|
// Stop on direction change if SPINDLE_STOP_ON_DIR_CHANGE is enabled
|
|
|
|
//
|
2020-12-09 04:29:33 -06:00
|
|
|
void SpindleLaser::set_reverse(const bool reverse) {
|
2019-06-27 23:06:49 -05:00
|
|
|
const bool dir_state = (reverse == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
|
2020-04-22 16:35:03 -05:00
|
|
|
if (TERN0(SPINDLE_STOP_ON_DIR_CHANGE, enabled()) && READ(SPINDLE_DIR_PIN) != dir_state) disable();
|
2019-06-27 23:06:49 -05:00
|
|
|
WRITE(SPINDLE_DIR_PIN, dir_state);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // HAS_CUTTER
|