Various Laser / Spindle improvements (#15335)
This commit is contained in:
@ -32,10 +32,17 @@
|
||||
|
||||
SpindleLaser cutter;
|
||||
|
||||
cutter_power_t SpindleLaser::power; // = 0
|
||||
|
||||
cutter_power_t SpindleLaser::power;
|
||||
bool SpindleLaser::isOn; // state to determine when to apply setPower to power
|
||||
cutter_setPower_t SpindleLaser::setPower = interpret_power(SPEED_POWER_MIN); // spindle/laser speed/power control in PWM, Percentage or RPM
|
||||
#if ENABLED(MARLIN_DEV_MODE)
|
||||
cutter_frequency_t SpindleLaser::frequency; // setting PWM frequency; range: 2K - 50K
|
||||
#endif
|
||||
#define SPINDLE_LASER_PWM_OFF ((SPINDLE_LASER_PWM_INVERT) ? 255 : 0)
|
||||
|
||||
//
|
||||
// Init the cutter to a safe OFF state
|
||||
//
|
||||
void SpindleLaser::init() {
|
||||
OUT_WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Init spindle to off
|
||||
#if ENABLED(SPINDLE_CHANGE_DIR)
|
||||
@ -45,41 +52,39 @@ void SpindleLaser::init() {
|
||||
SET_PWM(SPINDLE_LASER_PWM_PIN);
|
||||
analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // set to lowest speed
|
||||
#endif
|
||||
#if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY)
|
||||
set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
|
||||
#if ENABLED(MARLIN_DEV_MODE)
|
||||
frequency = SPINDLE_LASER_FREQUENCY;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
#if ENABLED(SPINDLE_LASER_PWM)
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
* Set the cutter PWM directly to the given ocr value
|
||||
**/
|
||||
void SpindleLaser::set_ocr(const uint8_t ocr) {
|
||||
WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_HIGH); // turn spindle on (active low)
|
||||
WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_HIGH); // turn spindle on
|
||||
analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
//
|
||||
// Set cutter ON state (and PWM) to the given cutter power value
|
||||
//
|
||||
void SpindleLaser::apply_power(const cutter_power_t inpow) {
|
||||
static cutter_power_t last_power_applied = 0;
|
||||
if (inpow == last_power_applied) return;
|
||||
last_power_applied = inpow;
|
||||
#if ENABLED(SPINDLE_LASER_PWM)
|
||||
if (enabled()) {
|
||||
#define _scaled(F) ((F - (SPEED_POWER_INTERCEPT)) * inv_slope)
|
||||
constexpr float inv_slope = RECIPROCAL(SPEED_POWER_SLOPE),
|
||||
min_ocr = _scaled(SPEED_POWER_MIN),
|
||||
max_ocr = _scaled(SPEED_POWER_MAX);
|
||||
int16_t ocr_val;
|
||||
if (inpow <= SPEED_POWER_MIN) ocr_val = min_ocr; // Use minimum if set below
|
||||
else if (inpow >= SPEED_POWER_MAX) ocr_val = max_ocr; // Use maximum if set above
|
||||
else ocr_val = _scaled(inpow); // Use calculated OCR value
|
||||
set_ocr(ocr_val & 0xFF); // ...limited to Atmel PWM max
|
||||
}
|
||||
if (enabled())
|
||||
set_ocr(translate_power(inpow));
|
||||
else {
|
||||
WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off (active low)
|
||||
analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte
|
||||
WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_HIGH); // Turn spindle off
|
||||
analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Only write low byte
|
||||
}
|
||||
#else
|
||||
WRITE(SPINDLE_LASER_ENA_PIN, (SPINDLE_LASER_ACTIVE_HIGH) ? enabled() : !enabled());
|
||||
@ -88,6 +93,10 @@ void SpindleLaser::apply_power(const cutter_power_t inpow) {
|
||||
|
||||
#if ENABLED(SPINDLE_CHANGE_DIR)
|
||||
|
||||
//
|
||||
// Set the spindle direction and apply immediately
|
||||
// Stop on direction change if SPINDLE_STOP_ON_DIR_CHANGE is enabled
|
||||
//
|
||||
void SpindleLaser::set_direction(const bool reverse) {
|
||||
const bool dir_state = (reverse == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
|
||||
#if ENABLED(SPINDLE_STOP_ON_DIR_CHANGE)
|
||||
|
@ -28,55 +28,98 @@
|
||||
|
||||
#include "../inc/MarlinConfig.h"
|
||||
|
||||
#if ENABLED(SPINDLE_FEATURE)
|
||||
#define _MSG_CUTTER(M) MSG_SPINDLE_##M
|
||||
#else
|
||||
#define _MSG_CUTTER(M) MSG_LASER_##M
|
||||
#endif
|
||||
#define MSG_CUTTER(M) _MSG_CUTTER(M)
|
||||
#include "spindle_laser_types.h"
|
||||
|
||||
#if SPEED_POWER_MAX > 255
|
||||
typedef uint16_t cutter_power_t;
|
||||
#define CUTTER_MENU_TYPE uint16_5
|
||||
#else
|
||||
typedef uint8_t cutter_power_t;
|
||||
#define CUTTER_MENU_TYPE uint8
|
||||
#if ENABLED(LASER_POWER_INLINE)
|
||||
#include "../module/planner.h"
|
||||
#endif
|
||||
|
||||
class SpindleLaser {
|
||||
public:
|
||||
static bool isOn; // state to determine when to apply setPower to power
|
||||
static cutter_power_t power;
|
||||
static inline uint8_t powerPercent(const uint8_t pp) { return ui8_to_percent(pp); } // for display
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
#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
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
static void init();
|
||||
|
||||
static inline bool enabled() { return !!power; }
|
||||
|
||||
static inline void set_power(const cutter_power_t pwr) { power = pwr; }
|
||||
|
||||
static inline void refresh() { apply_power(power); }
|
||||
|
||||
static inline void set_enabled(const bool enable) {
|
||||
const bool was = enabled();
|
||||
set_power(enable ? 255 : 0);
|
||||
if (was != enable) power_delay();
|
||||
}
|
||||
|
||||
// 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
|
||||
static void apply_power(const cutter_power_t inpow);
|
||||
|
||||
//static bool active() { return READ(SPINDLE_LASER_ENA_PIN) == SPINDLE_LASER_ACTIVE_HIGH; }
|
||||
FORCE_INLINE static void refresh() { apply_power(power); }
|
||||
FORCE_INLINE static void set_power(const cutter_power_t pwr) { power = pwr; refresh(); }
|
||||
|
||||
static void update_output();
|
||||
static inline void set_enabled(const bool enable) { set_power(enable ? (power ?: interpret_power(SPEED_POWER_STARTUP)) : 0); }
|
||||
|
||||
#if ENABLED(SPINDLE_LASER_PWM)
|
||||
static void set_ocr(const uint8_t ocr);
|
||||
static inline void set_ocr_power(const cutter_power_t pwr) { power = pwr; set_ocr(pwr); }
|
||||
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
|
||||
#endif
|
||||
|
||||
// Wait for spindle to spin up or spin down
|
||||
static inline void power_delay() {
|
||||
#if SPINDLE_LASER_POWERUP_DELAY || SPINDLE_LASER_POWERDOWN_DELAY
|
||||
safe_delay(enabled() ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY);
|
||||
static inline void power_delay(const bool on) {
|
||||
#if DISABLED(LASER_POWER_INLINE)
|
||||
safe_delay(on ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -86,10 +129,44 @@ public:
|
||||
static inline void set_direction(const bool) {}
|
||||
#endif
|
||||
|
||||
static inline void disable() { set_enabled(false); }
|
||||
static inline void enable_forward() { set_direction(false); set_enabled(true); }
|
||||
static inline void enable_reverse() { set_direction(true); set_enabled(true); }
|
||||
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
|
||||
|
||||
#if ENABLED(LASER_POWER_INLINE)
|
||||
// Force disengage planner power control
|
||||
static inline void inline_disable() { planner.settings.laser.status = 0; planner.settings.laser.power = 0; isOn = false;}
|
||||
|
||||
// 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
|
||||
planner.settings.laser.status = enabled(pwr) ? 0x03 : 0x01;
|
||||
planner.settings.laser.power = pwr;
|
||||
#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) {
|
||||
planner.settings.laser.status = pwr ? 0x03 : 0x01;
|
||||
planner.settings.laser.power = pwr;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static inline void kill() {
|
||||
#if ENABLED(LASER_POWER_INLINE)
|
||||
inline_disable();
|
||||
#endif
|
||||
disable();
|
||||
}
|
||||
};
|
||||
|
||||
extern SpindleLaser cutter;
|
||||
|
50
Marlin/src/feature/spindle_laser_types.h
Normal file
50
Marlin/src/feature/spindle_laser_types.h
Normal file
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Marlin 3D Printer Firmware
|
||||
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||
*
|
||||
* Based on Sprinter and grbl.
|
||||
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
||||
*
|
||||
* 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_types.h
|
||||
* Support for Laser Power or Spindle Power & Direction
|
||||
*/
|
||||
|
||||
#include "../inc/MarlinConfigPre.h"
|
||||
|
||||
#if ENABLED(SPINDLE_FEATURE)
|
||||
#define _MSG_CUTTER(M) MSG_SPINDLE_##M
|
||||
#else
|
||||
#define _MSG_CUTTER(M) MSG_LASER_##M
|
||||
#endif
|
||||
#define MSG_CUTTER(M) _MSG_CUTTER(M)
|
||||
#if CUTTER_DISPLAY_IS(RPM) && SPEED_POWER_MAX > 255
|
||||
#define cutter_power_t uint16_t
|
||||
#define cutter_setPower_t uint16_t
|
||||
#define CUTTER_MENU_POWER_TYPE uint16_5
|
||||
#else
|
||||
#define cutter_power_t uint8_t
|
||||
#define cutter_setPower_t uint8_t
|
||||
#define CUTTER_MENU_POWER_TYPE uint8
|
||||
#endif
|
||||
|
||||
#if ENABLED(MARLIN_DEV_MODE)
|
||||
#define cutter_frequency_t uint16_t
|
||||
#define CUTTER_MENU_FREQUENCY_TYPE uint16_5
|
||||
#endif
|
Reference in New Issue
Block a user