Skip to content

Commit

Permalink
✨ M3 / M4 O for laser/spindle (MarlinFirmware#26883)
Browse files Browse the repository at this point in the history
  • Loading branch information
HoverClub authored Aug 15, 2024
1 parent 43d9d1c commit 23d9020
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Marlin/src/feature/spindle_laser.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@
// Inline laser power
#include "../module/planner.h"

#define RPM_TO_PWM(X) ((X) * 255 / (SPEED_POWER_MAX))
#define PWM_TO_RPM(X) ((X) * (SPEED_POWER_MAX) / 255)
#define PCT_TO_PWM(X) ((X) * 255 / 100)
#define PWM_TO_PCT(X) ((X) * 100 / 255)
#define PCT_TO_SERVO(X) ((X) * 180 / 100)
#define CUTTER_PWM_TO_SPWR(X) (CUTTER_UNIT_IS(PERCENT) ? PWM_TO_PCT(X) : (CUTTER_UNIT_IS(RPM) ? PWM_TO_RPM(X) : X))

// Laser/Cutter operation mode
enum CutterMode : int8_t {
Expand Down
7 changes: 6 additions & 1 deletion Marlin/src/gcode/control/M3-M5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
*
* Parameters:
* S<power> - Set power. S0 will turn the spindle/laser off.
* O<power> - Set power in PWM units 0-255
*
* If no PWM pin is defined then M3/M4 just turns it on or off.
*
Expand Down Expand Up @@ -91,7 +92,11 @@ void GcodeSuite::M3_M4(const bool is_M4) {
auto get_s_power = [] {
if (parser.seenval('S')) {
const float v = parser.value_float();
cutter.menuPower = cutter.unitPower = TERN(LASER_POWER_TRAP, v, cutter.power_to_range(v));
cutter.menuPower = cutter.unitPower = TERN(LASER_POWER_TRAP, constrain( v, 0, CUTTER_POWER_MAX), cutter.power_to_range(v));
}
else if (parser.seenval('O')) { // pwr in PWM units
const float v = parser.value_float();
cutter.menuPower = cutter.unitPower = CUTTER_PWM_TO_SPWR(constrain(v, 0, 255));
}
else if (cutter.cutter_mode == CUTTER_MODE_STANDARD)
cutter.menuPower = cutter.unitPower = cutter.cpwr_to_upwr(SPEED_POWER_STARTUP);
Expand Down

0 comments on commit 23d9020

Please sign in to comment.