Skip to content

Commit

Permalink
🩹 Fix non-PWM cutter compile (#23169)
Browse files Browse the repository at this point in the history
  • Loading branch information
descipher authored and thinkyhead committed Dec 25, 2021
1 parent 7123b15 commit c3b8b3e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
12 changes: 4 additions & 8 deletions Marlin/src/feature/spindle_laser.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,10 @@
#define PCT_TO_PWM(X) ((X) * 255 / 100)
#define PCT_TO_SERVO(X) ((X) * 180 / 100)

#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))

class SpindleLaser {
public:
static constexpr float
min_pct = TERN(CUTTER_POWER_RELATIVE, 0, TERN(SPINDLE_FEATURE, round(100.0f * (SPEED_POWER_MIN) / (SPEED_POWER_MAX)), SPEED_POWER_MIN)),
max_pct = TERN(SPINDLE_FEATURE, 100, SPEED_POWER_MAX);

static const inline uint8_t pct_to_ocr(const_float_t pct) { return uint8_t(PCT_TO_PWM(pct)); }

// cpower = configured values (e.g., SPEED_POWER_MAX)
Expand Down Expand Up @@ -158,6 +150,9 @@ class SpindleLaser {
}

static inline cutter_power_t power_to_range(const cutter_power_t pwr, const uint8_t pwrUnit) {
static constexpr float
min_pct = TERN(CUTTER_POWER_RELATIVE, 0, TERN(SPINDLE_FEATURE, round(100.0f * (SPEED_POWER_MIN) / (SPEED_POWER_MAX)), SPEED_POWER_MIN)),
max_pct = TERN(SPINDLE_FEATURE, 100, SPEED_POWER_MAX);
if (pwr <= 0) return 0;
cutter_power_t upwr;
switch (pwrUnit) {
Expand Down Expand Up @@ -186,6 +181,7 @@ class SpindleLaser {
}
return upwr;
}

#endif // SPINDLE_LASER_USE_PWM

/**
Expand Down
24 changes: 23 additions & 1 deletion Marlin/src/feature/spindle_laser_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,34 @@

#include "../inc/MarlinConfigPre.h"

#define MSG_CUTTER(M) _MSG_CUTTER(M)

#ifndef SPEED_POWER_INTERCEPT
#define SPEED_POWER_INTERCEPT 0
#endif
#if ENABLED(SPINDLE_FEATURE)
#define _MSG_CUTTER(M) MSG_SPINDLE_##M
#ifndef SPEED_POWER_MIN
#define SPEED_POWER_MIN 5000
#endif
#ifndef SPEED_POWER_MAX
#define SPEED_POWER_MAX 30000
#endif
#ifndef SPEED_POWER_STARTUP
#define SPEED_POWER_STARTUP 25000
#endif
#else
#define _MSG_CUTTER(M) MSG_LASER_##M
#ifndef SPEED_POWER_MIN
#define SPEED_POWER_MIN 0
#endif
#ifndef SPEED_POWER_MAX
#define SPEED_POWER_MAX 255
#endif
#ifndef SPEED_POWER_STARTUP
#define SPEED_POWER_STARTUP 255
#endif
#endif
#define MSG_CUTTER(M) _MSG_CUTTER(M)

typedef IF<(SPEED_POWER_MAX > 255), uint16_t, uint8_t>::type cutter_cpower_t;

Expand Down
32 changes: 17 additions & 15 deletions Marlin/src/gcode/control/M3-M5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,23 @@
* PWM duty cycle goes from 0 (off) to 255 (always on).
*/
void GcodeSuite::M3_M4(const bool is_M4) {
auto get_s_power = [] {
if (parser.seenval('S')) {
const float spwr = parser.value_float();
#if ENABLED(SPINDLE_SERVO)
cutter.unitPower = spwr;
#else
cutter.unitPower = TERN(SPINDLE_LASER_USE_PWM,
cutter.power_to_range(cutter_power_t(round(spwr))),
spwr > 0 ? 255 : 0);
#endif
}
else
cutter.unitPower = cutter.cpwr_to_upwr(SPEED_POWER_STARTUP);
return cutter.unitPower;
};
#if EITHER(SPINDLE_LASER_USE_PWM, SPINDLE_SERVO)
auto get_s_power = [] {
if (parser.seenval('S')) {
const float spwr = parser.value_float();
#if ENABLED(SPINDLE_SERVO)
cutter.unitPower = spwr;
#else
cutter.unitPower = TERN(SPINDLE_LASER_USE_PWM,
cutter.power_to_range(cutter_power_t(round(spwr))),
spwr > 0 ? 255 : 0);
#endif
}
else
cutter.unitPower = cutter.cpwr_to_upwr(SPEED_POWER_STARTUP);
return cutter.unitPower;
};
#endif

#if ENABLED(LASER_POWER_INLINE)
if (parser.seen('I') == DISABLED(LASER_POWER_INLINE_INVERT)) {
Expand Down
6 changes: 6 additions & 0 deletions buildroot/tests/mega1280
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ opt_enable SPINDLE_FEATURE ULTIMAKERCONTROLLER LCD_BED_LEVELING \
EXTERNAL_CLOSED_LOOP_CONTROLLER POWER_MONITOR_CURRENT POWER_MONITOR_VOLTAGE
exec_test $1 $2 "Spindle, MESH_BED_LEVELING, closed loop, Power Monitor, and LCD" "$3"

#
# ...and without PWM
#
opt_disable SPINDLE_LASER_USE_PWM
exec_test $1 $2 "(No PWM)" "$3"

#
# Test DUAL_X_CARRIAGE
#
Expand Down

0 comments on commit c3b8b3e

Please sign in to comment.