Control spindle RPM through RAMPS

Spindle control through RAMPS board was the hardest nut for me to crack in my MPCNC project.

I saw this post many times in my searches but eventually tracked down a few things that may help others.

I am using two RAMPS boards, 1.4 and 1.6. I discovered that the V1Engineering dual endstop (and also default Marlin code) use pin 4 to PWM control the spindle speed. On my RAMPS boards that is the SECOND set of servo pins from the Reset button.

But I also discovered that alone was not enough.

You also have to enable it in the firmware. Spindle control is disabled by default. This involves uncommenting the line #define SPINDLE_FEATURE in Configuration_adv.h in Marlin 2.0.7.x

And once I was there I saw there was 3 ways to specify the spindle speed…
PWM255 (the default, using M3 S0 through M3 S255 to set from off to full)
PERCENT (using M3 S0 through M3 S100 to set from off to full)
RPM (using M3 S0 through M3 S50000) to set from off to full…)

I opted for RPM since that looked like the obvious… and nothing worked… went back to PWM255 and worked fine so I set my tools to use RPM from 0 to 255 (lazy fix) until I could figure it out…

Well another day of debugging and I did figure it out… its a bug in marlin in 8 bit controllers as far as I can tell. In src>>features>>spindle_laser.h there is a section called class SpindleLaser and in there the following line has an issue:

return unitPower ? round(100 * (cpwr - SPEED_POWER_FLOOR) / (SPEED_POWER_MAX - SPEED_POWER_FLOOR)) : 0;

I found a reference on the Marlin github to the following change that worked for me… its just adding the float (as I commented):

return unitPower ? round(100 * float(cpwr - SPEED_POWER_FLOOR) / (SPEED_POWER_MAX - SPEED_POWER_FLOOR)) : 0; //TJ added float

Hope that saves others the same grief I suffered.