2019-06-28 04:06:49 +00:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 14:00:57 +00:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2019-06-28 04:06:49 +00:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 04:57:50 +00:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2019-06-28 04:06:49 +00: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-23 03:20:14 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-06-28 04:06:49 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/**
|
|
|
|
* feature/spindle_laser.h
|
|
|
|
* Support for Laser Power or Spindle Power & Direction
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
2020-04-03 00:31:08 +00:00
|
|
|
#include "spindle_laser_types.h"
|
|
|
|
|
2021-01-10 12:01:25 +00:00
|
|
|
#if USE_BEEPER
|
|
|
|
#include "../libs/buzzer.h"
|
|
|
|
#endif
|
|
|
|
|
2020-04-03 00:31:08 +00:00
|
|
|
#if ENABLED(LASER_POWER_INLINE)
|
|
|
|
#include "../module/planner.h"
|
2019-06-28 04:06:49 +00:00
|
|
|
#endif
|
|
|
|
|
2020-06-08 05:47:31 +00:00
|
|
|
#define PCT_TO_PWM(X) ((X) * 255 / 100)
|
2020-10-31 22:42:05 +00:00
|
|
|
#define PCT_TO_SERVO(X) ((X) * 180 / 100)
|
2020-06-08 05:47:31 +00:00
|
|
|
|
|
|
|
#ifndef SPEED_POWER_INTERCEPT
|
|
|
|
#define SPEED_POWER_INTERCEPT 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// #define _MAP(N,S1,S2,D1,D2) ((N)*_MAX((D2)-(D1),0)/_MAX((S2)-(S1),1)+(D1))
|
|
|
|
|
2019-06-28 04:06:49 +00:00
|
|
|
class SpindleLaser {
|
|
|
|
public:
|
2020-06-08 05:47:31 +00:00
|
|
|
static constexpr float
|
2020-10-31 21:28:33 +00:00
|
|
|
min_pct = TERN(CUTTER_POWER_RELATIVE, 0, TERN(SPINDLE_FEATURE, round(100.0f * (SPEED_POWER_MIN) / (SPEED_POWER_MAX)), SPEED_POWER_MIN)),
|
2020-10-31 18:45:10 +00:00
|
|
|
max_pct = TERN(SPINDLE_FEATURE, 100, SPEED_POWER_MAX);
|
2019-06-28 04:06:49 +00:00
|
|
|
|
2020-06-08 05:47:31 +00:00
|
|
|
static const inline uint8_t pct_to_ocr(const float pct) { return uint8_t(PCT_TO_PWM(pct)); }
|
|
|
|
|
2020-10-31 21:28:33 +00:00
|
|
|
// cpower = configured values (e.g., SPEED_POWER_MAX)
|
|
|
|
|
|
|
|
// Convert configured power range to a percentage
|
|
|
|
static const inline uint8_t cpwr_to_pct(const cutter_cpower_t cpwr) {
|
|
|
|
constexpr cutter_cpower_t power_floor = TERN(CUTTER_POWER_RELATIVE, SPEED_POWER_MIN, 0),
|
|
|
|
power_range = SPEED_POWER_MAX - power_floor;
|
2020-12-09 10:29:33 +00:00
|
|
|
return cpwr ? round(100.0f * (cpwr - power_floor) / power_range) : 0;
|
2020-04-03 00:31:08 +00:00
|
|
|
}
|
2019-06-28 04:06:49 +00:00
|
|
|
|
2020-10-31 21:28:33 +00:00
|
|
|
// Convert a cpower (e.g., SPEED_POWER_STARTUP) to unit power (upwr, upower),
|
2020-10-31 22:42:05 +00:00
|
|
|
// which can be PWM, Percent, Servo angle, or RPM (rel/abs).
|
2020-06-08 05:47:31 +00:00
|
|
|
static const inline cutter_power_t cpwr_to_upwr(const cutter_cpower_t cpwr) { // STARTUP power to Unit power
|
|
|
|
const cutter_power_t upwr = (
|
|
|
|
#if ENABLED(SPINDLE_FEATURE)
|
|
|
|
// Spindle configured values are in RPM
|
|
|
|
#if CUTTER_UNIT_IS(RPM)
|
|
|
|
cpwr // to RPM
|
|
|
|
#elif CUTTER_UNIT_IS(PERCENT) // to PCT
|
|
|
|
cpwr_to_pct(cpwr)
|
2020-10-31 22:42:05 +00:00
|
|
|
#elif CUTTER_UNIT_IS(SERVO) // to SERVO angle
|
|
|
|
PCT_TO_SERVO(cpwr_to_pct(cpwr))
|
2020-06-08 05:47:31 +00:00
|
|
|
#else // to PWM
|
|
|
|
PCT_TO_PWM(cpwr_to_pct(cpwr))
|
|
|
|
#endif
|
2020-04-03 00:31:08 +00:00
|
|
|
#else
|
2020-06-08 05:47:31 +00:00
|
|
|
// Laser configured values are in PCT
|
|
|
|
#if CUTTER_UNIT_IS(PWM255)
|
|
|
|
PCT_TO_PWM(cpwr)
|
|
|
|
#else
|
|
|
|
cpwr // to RPM/PCT
|
|
|
|
#endif
|
2020-04-03 00:31:08 +00:00
|
|
|
#endif
|
2020-06-08 05:47:31 +00:00
|
|
|
);
|
|
|
|
return upwr;
|
2019-10-15 21:10:20 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 05:47:31 +00:00
|
|
|
static const cutter_power_t mpower_min() { return cpwr_to_upwr(SPEED_POWER_MIN); }
|
|
|
|
static const cutter_power_t mpower_max() { return cpwr_to_upwr(SPEED_POWER_MAX); }
|
|
|
|
|
2021-01-10 12:01:25 +00:00
|
|
|
#if ENABLED(LASER_FEATURE)
|
|
|
|
static cutter_test_pulse_t testPulse; // Test fire Pulse ms value
|
|
|
|
#endif
|
|
|
|
|
2020-06-08 05:47:31 +00:00
|
|
|
static bool isReady; // Ready to apply power setting from the UI to OCR
|
|
|
|
static uint8_t power;
|
|
|
|
|
|
|
|
#if ENABLED(MARLIN_DEV_MODE)
|
|
|
|
static cutter_frequency_t frequency; // Set PWM frequency; range: 2K-50K
|
|
|
|
#endif
|
|
|
|
|
2020-06-09 00:53:39 +00:00
|
|
|
static cutter_power_t menuPower, // Power as set via LCD menu in PWM, Percentage or RPM
|
|
|
|
unitPower; // Power as displayed status in PWM, Percentage or RPM
|
2020-06-08 05:47:31 +00:00
|
|
|
|
2020-04-03 00:31:08 +00:00
|
|
|
static void init();
|
|
|
|
|
|
|
|
#if ENABLED(MARLIN_DEV_MODE)
|
|
|
|
static inline void refresh_frequency() { set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency); }
|
|
|
|
#endif
|
2020-06-08 05:47:31 +00:00
|
|
|
|
|
|
|
// Modifying this function should update everywhere
|
|
|
|
static inline bool enabled(const cutter_power_t opwr) { return opwr > 0; }
|
|
|
|
static inline bool enabled() { return enabled(power); }
|
|
|
|
|
|
|
|
static void apply_power(const uint8_t inpow);
|
2019-06-28 04:06:49 +00:00
|
|
|
|
2020-04-03 00:31:08 +00:00
|
|
|
FORCE_INLINE static void refresh() { apply_power(power); }
|
2020-06-08 05:47:31 +00:00
|
|
|
FORCE_INLINE static void set_power(const uint8_t upwr) { power = upwr; refresh(); }
|
2019-06-28 04:06:49 +00:00
|
|
|
|
|
|
|
#if ENABLED(SPINDLE_LASER_PWM)
|
2020-06-08 05:47:31 +00:00
|
|
|
|
2020-12-27 01:01:54 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
static void _set_ocr(const uint8_t ocr);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2019-06-28 04:06:49 +00:00
|
|
|
static void set_ocr(const uint8_t ocr);
|
2020-06-08 05:47:31 +00:00
|
|
|
static inline void set_ocr_power(const uint8_t ocr) { power = ocr; set_ocr(ocr); }
|
|
|
|
static void ocr_off();
|
|
|
|
// Used to update output for power->OCR translation
|
|
|
|
static inline uint8_t upower_to_ocr(const cutter_power_t upwr) {
|
|
|
|
return (
|
|
|
|
#if CUTTER_UNIT_IS(PWM255)
|
|
|
|
uint8_t(upwr)
|
|
|
|
#elif CUTTER_UNIT_IS(PERCENT)
|
|
|
|
pct_to_ocr(upwr)
|
|
|
|
#else
|
|
|
|
uint8_t(pct_to_ocr(cpwr_to_pct(upwr)))
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-12-27 01:01:54 +00:00
|
|
|
#error "CUTTER_UNIT_IS(???)"
|
2020-06-08 05:47:31 +00:00
|
|
|
#endif
|
|
|
|
));
|
|
|
|
}
|
|
|
|
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
|
2020-06-16 20:49:32 +00:00
|
|
|
upwr = cutter_power_t(
|
2020-06-08 05:47:31 +00:00
|
|
|
(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
|
2020-06-16 20:49:32 +00:00
|
|
|
upwr = cutter_power_t(
|
2020-06-08 05:47:31 +00:00
|
|
|
(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
|
2020-06-16 20:49:32 +00:00
|
|
|
upwr = cutter_power_t(
|
2020-06-08 05:47:31 +00:00
|
|
|
(pwr < SPEED_POWER_MIN) ? SPEED_POWER_MIN // Use minimum if set below
|
|
|
|
: (pwr > SPEED_POWER_MAX) ? SPEED_POWER_MAX // Use maximum if set above
|
|
|
|
: pwr // Calculate OCR value
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return upwr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SPINDLE_LASER_PWM
|
2019-06-28 04:06:49 +00:00
|
|
|
|
2020-06-16 20:49:32 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-06-28 04:06:49 +00:00
|
|
|
// Wait for spindle to spin up or spin down
|
2020-04-03 00:31:08 +00:00
|
|
|
static inline void power_delay(const bool on) {
|
|
|
|
#if DISABLED(LASER_POWER_INLINE)
|
|
|
|
safe_delay(on ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY);
|
2019-10-15 21:10:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
2019-06-28 04:06:49 +00:00
|
|
|
|
|
|
|
#if ENABLED(SPINDLE_CHANGE_DIR)
|
2020-12-09 10:29:33 +00:00
|
|
|
static void set_reverse(const bool reverse);
|
|
|
|
static bool is_reverse() { return READ(SPINDLE_DIR_PIN) == SPINDLE_INVERT_DIR; }
|
2019-06-28 04:06:49 +00:00
|
|
|
#else
|
2020-12-09 10:29:33 +00:00
|
|
|
static inline void set_reverse(const bool) {}
|
|
|
|
static bool is_reverse() { return false; }
|
2019-06-28 04:06:49 +00:00
|
|
|
#endif
|
|
|
|
|
2020-06-08 05:47:31 +00:00
|
|
|
static inline void disable() { isReady = false; set_enabled(false); }
|
|
|
|
|
2020-04-03 00:31:08 +00:00
|
|
|
#if HAS_LCD_MENU
|
2020-06-08 05:47:31 +00:00
|
|
|
|
|
|
|
static inline void enable_with_dir(const bool reverse) {
|
|
|
|
isReady = true;
|
2020-06-16 20:49:32 +00:00
|
|
|
const uint8_t ocr = TERN(SPINDLE_LASER_PWM, upower_to_ocr(menuPower), 255);
|
2020-06-08 05:47:31 +00:00
|
|
|
if (menuPower)
|
|
|
|
power = ocr;
|
|
|
|
else
|
|
|
|
menuPower = cpwr_to_upwr(SPEED_POWER_STARTUP);
|
|
|
|
unitPower = menuPower;
|
2020-12-09 10:29:33 +00:00
|
|
|
set_reverse(reverse);
|
2020-06-08 05:47:31 +00:00
|
|
|
set_enabled(true);
|
|
|
|
}
|
|
|
|
FORCE_INLINE static void enable_forward() { enable_with_dir(false); }
|
|
|
|
FORCE_INLINE static void enable_reverse() { enable_with_dir(true); }
|
2020-12-09 10:29:33 +00:00
|
|
|
FORCE_INLINE static void enable_same_dir() { enable_with_dir(is_reverse()); }
|
2020-06-08 05:47:31 +00:00
|
|
|
|
|
|
|
#if ENABLED(SPINDLE_LASER_PWM)
|
|
|
|
static inline void update_from_mpower() {
|
|
|
|
if (isReady) power = upower_to_ocr(menuPower);
|
|
|
|
unitPower = menuPower;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-10 12:01:25 +00:00
|
|
|
#if ENABLED(LASER_FEATURE)
|
|
|
|
/**
|
|
|
|
* Test fire the laser using the testPulse ms duration
|
|
|
|
* Also fires with any PWM power that was previous set
|
|
|
|
* If not set defaults to 80% power
|
|
|
|
*/
|
|
|
|
static inline void test_fire_pulse() {
|
|
|
|
enable_forward(); // Turn Laser on (Spindle speak but same funct)
|
|
|
|
TERN_(USE_BEEPER, buzzer.tone(30, 3000));
|
|
|
|
delay(testPulse); // Delay for time set by user in pulse ms menu screen.
|
|
|
|
disable(); // Turn laser off
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // HAS_LCD_MENU
|
2019-06-28 04:06:49 +00:00
|
|
|
|
2020-04-03 00:31:08 +00:00
|
|
|
#if ENABLED(LASER_POWER_INLINE)
|
2020-06-08 05:47:31 +00:00
|
|
|
/**
|
|
|
|
* Inline power adds extra fields to the planner block
|
|
|
|
* to handle laser power and scale to movement speed.
|
|
|
|
*/
|
|
|
|
|
2020-04-03 00:31:08 +00:00
|
|
|
// Force disengage planner power control
|
2020-09-20 23:24:05 +00:00
|
|
|
static inline void inline_disable() {
|
2020-06-08 05:47:31 +00:00
|
|
|
isReady = false;
|
|
|
|
unitPower = 0;
|
2020-06-09 00:53:39 +00:00
|
|
|
planner.laser_inline.status.isPlanned = false;
|
|
|
|
planner.laser_inline.status.isEnabled = false;
|
2020-06-08 05:47:31 +00:00
|
|
|
planner.laser_inline.power = 0;
|
|
|
|
}
|
2020-04-03 00:31:08 +00:00
|
|
|
|
|
|
|
// Inline modes of all other functions; all enable planner inline power control
|
2020-06-08 05:47:31 +00:00
|
|
|
static inline void set_inline_enabled(const bool enable) {
|
2020-06-09 00:53:39 +00:00
|
|
|
if (enable)
|
2020-10-31 21:12:35 +00:00
|
|
|
inline_power(255);
|
2020-06-09 00:53:39 +00:00
|
|
|
else {
|
|
|
|
isReady = false;
|
|
|
|
unitPower = menuPower = 0;
|
|
|
|
planner.laser_inline.status.isPlanned = false;
|
|
|
|
TERN(SPINDLE_LASER_PWM, inline_ocr_power, inline_power)(0);
|
|
|
|
}
|
2020-06-08 05:47:31 +00:00
|
|
|
}
|
2020-04-03 00:31:08 +00:00
|
|
|
|
2020-06-08 05:47:31 +00:00
|
|
|
// Set the power for subsequent movement blocks
|
|
|
|
static void inline_power(const cutter_power_t upwr) {
|
2020-06-09 00:53:39 +00:00
|
|
|
unitPower = menuPower = upwr;
|
2020-04-03 00:31:08 +00:00
|
|
|
#if ENABLED(SPINDLE_LASER_PWM)
|
2020-06-08 05:47:31 +00:00
|
|
|
#if ENABLED(SPEED_POWER_RELATIVE) && !CUTTER_UNIT_IS(RPM) // relative mode does not turn laser off at 0, except for RPM
|
2020-06-09 00:53:39 +00:00
|
|
|
planner.laser_inline.status.isEnabled = true;
|
2020-06-08 05:47:31 +00:00
|
|
|
planner.laser_inline.power = upower_to_ocr(upwr);
|
2020-06-09 00:53:39 +00:00
|
|
|
isReady = true;
|
2020-06-08 05:47:31 +00:00
|
|
|
#else
|
2020-06-09 00:53:39 +00:00
|
|
|
inline_ocr_power(upower_to_ocr(upwr));
|
2020-06-08 05:47:31 +00:00
|
|
|
#endif
|
2020-04-03 00:31:08 +00:00
|
|
|
#else
|
2020-06-09 00:53:39 +00:00
|
|
|
planner.laser_inline.status.isEnabled = enabled(upwr);
|
|
|
|
planner.laser_inline.power = upwr;
|
2020-06-08 05:47:31 +00:00
|
|
|
isReady = enabled(upwr);
|
2020-04-03 00:31:08 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-06-08 05:47:31 +00:00
|
|
|
static inline void inline_direction(const bool) { /* never */ }
|
2020-04-03 00:31:08 +00:00
|
|
|
|
|
|
|
#if ENABLED(SPINDLE_LASER_PWM)
|
2020-06-08 05:47:31 +00:00
|
|
|
static inline void inline_ocr_power(const uint8_t ocrpwr) {
|
2020-06-09 00:53:39 +00:00
|
|
|
isReady = ocrpwr > 0;
|
|
|
|
planner.laser_inline.status.isEnabled = ocrpwr > 0;
|
2020-06-08 05:47:31 +00:00
|
|
|
planner.laser_inline.power = ocrpwr;
|
2020-04-03 00:31:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-06-08 05:47:31 +00:00
|
|
|
#endif // LASER_POWER_INLINE
|
2020-04-03 00:31:08 +00:00
|
|
|
|
|
|
|
static inline void kill() {
|
2020-04-22 21:35:03 +00:00
|
|
|
TERN_(LASER_POWER_INLINE, inline_disable());
|
2020-04-03 00:31:08 +00:00
|
|
|
disable();
|
|
|
|
}
|
2019-06-28 04:06:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern SpindleLaser cutter;
|