From 11d9c425812efdeb66071528de2d362f79ab4305 Mon Sep 17 00:00:00 2001 From: Michael Lincoln Date: Sat, 9 Mar 2019 10:25:39 +0000 Subject: [PATCH] Make spindle speed sticky so you can restart at the last used speed using m3/m4 without a S parameter. --- src/GCodes/GCodes2.cpp | 25 +++++++++++++++++-------- src/Spindle.cpp | 13 ++++++++++--- src/Spindle.h | 1 + 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/GCodes/GCodes2.cpp b/src/GCodes/GCodes2.cpp index a269257fea..0eb1bff994 100644 --- a/src/GCodes/GCodes2.cpp +++ b/src/GCodes/GCodes2.cpp @@ -393,13 +393,11 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) break; case 3: // Spin spindle clockwise - if (gb.Seen('S')) { switch (machineType) { case MachineType::cnc: { - const float rpm = gb.GetFValue(); const uint32_t slot = gb.Seen('P') ? gb.GetUIValue() : 0; if (slot >= MaxSpindles) { @@ -408,18 +406,25 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) } else { - platform.AccessSpindle(slot).SetRpm(rpm); + if (gb.Seen('S')) + { + const float rpm = gb.GetFValue(); + platform.AccessSpindle(slot).SetRpm(rpm); + } + platform.AccessSpindle(slot).TurnOn(); } } break; case MachineType::laser: - platform.SetLaserPwm(ConvertLaserPwm(gb.GetFValue())); + if (gb.Seen('S')) { + platform.SetLaserPwm(ConvertLaserPwm(gb.GetFValue())); + } break; default: #if SUPPORT_ROLAND - if (reprap.GetRoland()->Active()) + if (gb.Seen('S') && reprap.GetRoland()->Active()) { result = reprap.GetRoland()->ProcessSpindle(gb.GetFValue()); } @@ -434,11 +439,9 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) break; case 4: // Spin spindle counter clockwise - if (gb.Seen('S')) { if (machineType == MachineType::cnc) { - const float rpm = gb.GetFValue(); const uint32_t slot = gb.Seen('P') ? gb.GetUIValue() : 0; if (slot >= MaxSpindles) { @@ -447,7 +450,13 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) } else { - platform.AccessSpindle(slot).SetRpm(-rpm); + if (gb.Seen('S')) + { + const float rpm = gb.GetFValue(); + platform.AccessSpindle(slot).SetRpm(-rpm); + } + platform.AccessSpindle(slot).TurnOn(); + } } else diff --git a/src/Spindle.cpp b/src/Spindle.cpp index 7c4f531bdc..9bf61a30ef 100644 --- a/src/Spindle.cpp +++ b/src/Spindle.cpp @@ -33,8 +33,13 @@ void Spindle::SetPwmFrequency(float freq) void Spindle::SetRpm(float rpm) { - const float pwm = abs(rpm / maxRpm); - if (rpm >= 0.0) + configuredRpm = rpm; +} + +void Spindle::TurnOn() +{ + const float pwm = abs(configuredRpm / maxRpm); + if (configuredRpm >= 0.0) { spindleReversePort.WriteAnalog(0.0); spindleForwardPort.WriteAnalog(pwm); @@ -44,7 +49,7 @@ void Spindle::SetRpm(float rpm) spindleReversePort.WriteAnalog(pwm); spindleForwardPort.WriteAnalog(0.0); } - currentRpm = configuredRpm = rpm; + currentRpm = configuredRpm; } void Spindle::TurnOff() @@ -54,4 +59,6 @@ void Spindle::TurnOff() currentRpm = 0.0; } + + // End diff --git a/src/Spindle.h b/src/Spindle.h index 5e05993cf0..08a1469bbb 100644 --- a/src/Spindle.h +++ b/src/Spindle.h @@ -35,6 +35,7 @@ class Spindle float GetRpm() const { return configuredRpm; } void SetRpm(float rpm); + void TurnOn(); void TurnOff(); };