2019-06-28 06:06:49 +02:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 15:00:57 +01:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2019-06-28 06:06:49 +02:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2019-06-28 06:06:49 +02: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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/**
|
|
|
|
* feature/spindle_laser.h
|
|
|
|
* Support for Laser Power or Spindle Power & Direction
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
2020-04-03 02:31:08 +02:00
|
|
|
#include "spindle_laser_types.h"
|
|
|
|
|
|
|
|
#if ENABLED(LASER_POWER_INLINE)
|
|
|
|
#include "../module/planner.h"
|
2019-06-28 06:06:49 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
class SpindleLaser {
|
|
|
|
public:
|
2020-04-03 02:31:08 +02:00
|
|
|
static bool isOn; // state to determine when to apply setPower to power
|
2019-06-28 06:06:49 +02:00
|
|
|
static cutter_power_t power;
|
2020-04-03 02:31:08 +02:00
|
|
|
static cutter_setPower_t setPower; // spindle/laser menu set power; in PWM, Percentage or RPM
|
|
|
|
#if ENABLED(MARLIN_DEV_MODE)
|
|
|
|
static cutter_frequency_t frequency; // set PWM frequency; range: 2K-50K
|
|
|
|
#endif
|
2019-06-28 06:06:49 +02:00
|
|
|
|
2020-04-03 02:31:08 +02:00
|
|
|
static cutter_setPower_t interpret_power(const float pwr) { // convert speed/power to configured PWM, Percentage or RPM in relative or normal range
|
|
|
|
#if CUTTER_DISPLAY_IS(PERCENT)
|
|
|
|
return (pwr / SPEED_POWER_MAX) * 100; // to percent
|
|
|
|
#elif CUTTER_DISPLAY_IS(RPM) // to RPM is unaltered
|
|
|
|
return pwr;
|
|
|
|
#else // to PWM
|
|
|
|
#if ENABLED(CUTTER_POWER_RELATIVE)
|
|
|
|
return (pwr - SPEED_POWER_MIN) / (SPEED_POWER_MAX - SPEED_POWER_MIN) * 255; // using rpm range as relative percentage
|
|
|
|
#else
|
|
|
|
return (pwr / SPEED_POWER_MAX) * 255;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Translate speed/power --> percentage --> PWM value
|
|
|
|
**/
|
|
|
|
static cutter_power_t translate_power(const float pwr) {
|
|
|
|
float pwrpc;
|
|
|
|
#if CUTTER_DISPLAY_IS(PERCENT)
|
|
|
|
pwrpc = pwr;
|
|
|
|
#elif CUTTER_DISPLAY_IS(RPM) // RPM to percent
|
|
|
|
#if ENABLED(CUTTER_POWER_RELATIVE)
|
|
|
|
pwrpc = (pwr - SPEED_POWER_MIN) / (SPEED_POWER_MAX - SPEED_POWER_MIN) * 100;
|
|
|
|
#else
|
|
|
|
pwrpc = pwr / SPEED_POWER_MAX * 100;
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
return pwr; // PWM
|
|
|
|
#endif
|
2019-06-28 06:06:49 +02:00
|
|
|
|
2020-04-03 02:31:08 +02:00
|
|
|
#if ENABLED(SPINDLE_FEATURE)
|
|
|
|
#if ENABLED(CUTTER_POWER_RELATIVE)
|
|
|
|
constexpr float spmin = 0;
|
|
|
|
#else
|
|
|
|
constexpr float spmin = SPEED_POWER_MIN / SPEED_POWER_MAX * 100; // convert to percentage
|
|
|
|
#endif
|
|
|
|
constexpr float spmax = 100;
|
|
|
|
#else
|
|
|
|
constexpr float spmin = SPEED_POWER_MIN;
|
|
|
|
constexpr float spmax = SPEED_POWER_MAX;
|
|
|
|
#endif
|
2019-10-15 23:10:20 +02:00
|
|
|
|
2020-04-03 02:31:08 +02:00
|
|
|
constexpr float inv_slope = RECIPROCAL(SPEED_POWER_SLOPE),
|
|
|
|
min_ocr = (spmin - (SPEED_POWER_INTERCEPT)) * inv_slope, // Minimum allowed
|
|
|
|
max_ocr = (spmax - (SPEED_POWER_INTERCEPT)) * inv_slope; // Maximum allowed
|
|
|
|
float ocr_val;
|
|
|
|
if (pwrpc < spmin) ocr_val = min_ocr; // Use minimum if set below
|
|
|
|
else if (pwrpc > spmax) ocr_val = max_ocr; // Use maximum if set above
|
|
|
|
else ocr_val = (pwrpc - (SPEED_POWER_INTERCEPT)) * inv_slope; // Use calculated OCR value
|
|
|
|
return ocr_val; // ...limited to Atmel PWM max
|
2019-10-15 23:10:20 +02:00
|
|
|
}
|
|
|
|
|
2020-04-03 02:31:08 +02:00
|
|
|
static void init();
|
|
|
|
|
|
|
|
// Modifying this function should update everywhere
|
|
|
|
static inline bool enabled(const cutter_power_t pwr) { return pwr > 0; }
|
|
|
|
static inline bool enabled() { return enabled(power); }
|
|
|
|
#if ENABLED(MARLIN_DEV_MODE)
|
|
|
|
static inline void refresh_frequency() { set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency); }
|
|
|
|
#endif
|
2019-10-15 23:10:20 +02:00
|
|
|
static void apply_power(const cutter_power_t inpow);
|
2019-06-28 06:06:49 +02:00
|
|
|
|
2020-04-03 02:31:08 +02:00
|
|
|
FORCE_INLINE static void refresh() { apply_power(power); }
|
|
|
|
FORCE_INLINE static void set_power(const cutter_power_t pwr) { power = pwr; refresh(); }
|
2019-06-28 06:06:49 +02:00
|
|
|
|
2020-04-03 02:31:08 +02:00
|
|
|
static inline void set_enabled(const bool enable) { set_power(enable ? (power ?: interpret_power(SPEED_POWER_STARTUP)) : 0); }
|
2019-07-06 01:39:27 +02:00
|
|
|
|
2019-06-28 06:06:49 +02:00
|
|
|
#if ENABLED(SPINDLE_LASER_PWM)
|
|
|
|
static void set_ocr(const uint8_t ocr);
|
2020-04-03 02:31:08 +02:00
|
|
|
static inline void set_ocr_power(const uint8_t pwr) { power = pwr; set_ocr(pwr); }
|
|
|
|
// static uint8_t translate_power(const cutter_power_t pwr); // Used by update output for power->OCR translation
|
2019-06-28 06:06:49 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Wait for spindle to spin up or spin down
|
2020-04-03 02:31:08 +02: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 23:10:20 +02:00
|
|
|
#endif
|
|
|
|
}
|
2019-06-28 06:06:49 +02:00
|
|
|
|
|
|
|
#if ENABLED(SPINDLE_CHANGE_DIR)
|
|
|
|
static void set_direction(const bool reverse);
|
|
|
|
#else
|
2019-10-01 04:44:07 +02:00
|
|
|
static inline void set_direction(const bool) {}
|
2019-06-28 06:06:49 +02:00
|
|
|
#endif
|
|
|
|
|
2020-04-03 02:31:08 +02:00
|
|
|
static inline void disable() { isOn = false; set_enabled(false); }
|
|
|
|
#if HAS_LCD_MENU
|
|
|
|
static inline void enable_forward() { isOn = true; setPower ? (power = setPower) : (setPower = interpret_power(SPEED_POWER_STARTUP)); set_direction(false); set_enabled(true); }
|
|
|
|
static inline void enable_reverse() { isOn = true; setPower ? (power = setPower) : (setPower = interpret_power(SPEED_POWER_STARTUP)); set_direction(true); set_enabled(true); }
|
|
|
|
#endif
|
2019-06-28 06:06:49 +02:00
|
|
|
|
2020-04-03 02:31:08 +02:00
|
|
|
#if ENABLED(LASER_POWER_INLINE)
|
|
|
|
// Force disengage planner power control
|
2020-05-12 07:09:18 +02:00
|
|
|
static inline void inline_disable() { planner.laser.status = 0; planner.laser.power = 0; isOn = false;}
|
2020-04-03 02:31:08 +02:00
|
|
|
|
|
|
|
// Inline modes of all other functions; all enable planner inline power control
|
|
|
|
static inline void inline_enabled(const bool enable) { enable ? inline_power(SPEED_POWER_STARTUP) : inline_ocr_power(0); }
|
|
|
|
|
|
|
|
static void inline_power(const cutter_power_t pwr) {
|
|
|
|
#if ENABLED(SPINDLE_LASER_PWM)
|
|
|
|
inline_ocr_power(translate_power(pwr));
|
|
|
|
#else
|
2020-05-12 07:09:18 +02:00
|
|
|
planner.laser.status = enabled(pwr) ? 0x03 : 0x01;
|
|
|
|
planner.laser.power = pwr;
|
2020-04-03 02:31:08 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void inline_direction(const bool reverse) { UNUSED(reverse); } // TODO is this ever going to be needed
|
|
|
|
|
|
|
|
#if ENABLED(SPINDLE_LASER_PWM)
|
|
|
|
static inline void inline_ocr_power(const uint8_t pwr) {
|
2020-05-12 07:09:18 +02:00
|
|
|
planner.laser.status = pwr ? 0x03 : 0x01;
|
|
|
|
planner.laser.power = pwr;
|
2020-04-03 02:31:08 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline void kill() {
|
2020-04-22 23:35:03 +02:00
|
|
|
TERN_(LASER_POWER_INLINE, inline_disable());
|
2020-04-03 02:31:08 +02:00
|
|
|
disable();
|
|
|
|
}
|
2019-06-28 06:06:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern SpindleLaser cutter;
|