From 6fbc87acf07c5ce086e8b6b766f5d0001fdad80a Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Wed, 22 May 2019 10:28:27 -0700 Subject: [PATCH] fixed baby step stream to have the right offsets refactoring for clarity fixing warnings issue: MatterHackers/MatterControl#4560 Cannot move up in z calibration wizard --- .../Library/Export/GCodeExport.cs | 2 +- .../Io/BabyStepsStream.cs | 41 +++++++++---------- .../Io/PrintLevelingStream.cs | 32 +++++++-------- .../PrinterCommunication/PrinterConnection.cs | 2 +- 4 files changed, 36 insertions(+), 41 deletions(-) diff --git a/MatterControlLib/Library/Export/GCodeExport.cs b/MatterControlLib/Library/Export/GCodeExport.cs index 007f55f46..68cfc97ec 100644 --- a/MatterControlLib/Library/Export/GCodeExport.cs +++ b/MatterControlLib/Library/Export/GCodeExport.cs @@ -307,7 +307,7 @@ namespace MatterHackers.MatterControl.Library.Export if (levelingEnabled && !LevelingValidation.NeedsToBeRun(printer)) { - accumulatedStream = new PrintLevelingStream(printer, accumulatedStream, false); + accumulatedStream = new PrintLevelingStream(printer, accumulatedStream); } if (printer.Settings.GetValue(SettingsKey.emulate_endstops)) diff --git a/MatterControlLib/PrinterCommunication/Io/BabyStepsStream.cs b/MatterControlLib/PrinterCommunication/Io/BabyStepsStream.cs index ecb47dcc4..1738c7415 100644 --- a/MatterControlLib/PrinterCommunication/Io/BabyStepsStream.cs +++ b/MatterControlLib/PrinterCommunication/Io/BabyStepsStream.cs @@ -38,9 +38,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io public class BabyStepsStream : GCodeStreamProxy { private int extruderIndex = 0; - private Vector3[] extruderOffsets = new Vector3[4]; + private readonly Vector3[] extruderOffsets = new Vector3[4]; - public PrinterMove lastDestination = PrinterMove.Unknown; + private PrinterMove lastInputDestination = PrinterMove.Unknown; public Vector3 BabbyStepOffset { get; private set; } = Vector3.Zero; @@ -56,7 +56,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io ReadExtruderOffsets(); } - private void ReadExtruderOffsets() { for (int i = 0; i < 4; i++) @@ -69,36 +68,36 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io { get { - return $"Last Destination = {lastDestination}"; + return $"Last Destination = {lastInputDestination}"; } } - void Printer_SettingChanged(object s, StringEventArgs e) + private void Printer_SettingChanged(object s, StringEventArgs e) { if (e?.Data == SettingsKey.baby_step_z_offset) { var currentOffset = BabbyStepOffset.Z; BabbyStepOffset = new Vector3(0, 0, printer.Settings.GetValue(SettingsKey.baby_step_z_offset)); - lastDestination.position.Z = lastDestination.position.Z - currentOffset + BabbyStepOffset.Z; } - // if the offsets change update them (unless we are actively printing) else if (e?.Data == SettingsKey.extruder_offset && !printer.Connection.Printing && !printer.Connection.Paused) { + // if the offsets change update them (unless we are actively printing) ReadExtruderOffsets(); } } public override void SetPrinterPosition(PrinterMove position) { - this.lastDestination.CopyKnowSettings(position); - if (extruderIndex < 4) - { - lastDestination.position -= BabbyStepOffset; - lastDestination.position += extruderOffsets[extruderIndex]; - } - internalStream.SetPrinterPosition(lastDestination); + lastInputDestination.CopyKnowSettings(position); + + // calculate our offset to pass on to internal streams + var offestDestination = lastInputDestination; + offestDestination.position -= BabbyStepOffset; + offestDestination.position += extruderOffsets[Math.Min(extruderIndex, 4)]; + + internalStream.SetPrinterPosition(offestDestination); } public override void Dispose() @@ -131,20 +130,18 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io if (lineToSend != null && LineIsMovement(lineToSend)) { - PrinterMove currentMove = GetPosition(lineToSend, lastDestination); + PrinterMove currentMove = GetPosition(lineToSend, lastInputDestination); PrinterMove moveToSend = currentMove; - if (extruderIndex < 4) - { - moveToSend.position += BabbyStepOffset; - moveToSend.position -= extruderOffsets[extruderIndex]; - } + moveToSend.position += BabbyStepOffset; + moveToSend.position -= extruderOffsets[Math.Min(extruderIndex, 4)]; if (moveToSend.HaveAnyPosition) { - lineToSend = CreateMovementLine(moveToSend, lastDestination); + lineToSend = CreateMovementLine(moveToSend, lastInputDestination); } - lastDestination = currentMove; + + lastInputDestination = currentMove; return lineToSend; } diff --git a/MatterControlLib/PrinterCommunication/Io/PrintLevelingStream.cs b/MatterControlLib/PrinterCommunication/Io/PrintLevelingStream.cs index 420039d3b..fa280fabe 100644 --- a/MatterControlLib/PrinterCommunication/Io/PrintLevelingStream.cs +++ b/MatterControlLib/PrinterCommunication/Io/PrintLevelingStream.cs @@ -35,34 +35,31 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io { public class PrintLevelingStream : GCodeStreamProxy { - private PrinterMove _lastDestination = PrinterMove.Unknown; - private bool activePrinting; private LevelingFunctions currentLevelingFunctions = null; private Vector3 currentProbeZOffset; private bool wroteLevelingStatus = false; private bool gcodeAlreadyLeveled = false; - public PrintLevelingStream(PrinterConfig printer, GCodeStream internalStream, bool activePrinting) + public PrintLevelingStream(PrinterConfig printer, GCodeStream internalStream) : base(printer, internalStream) { // always reset this when we construct AllowLeveling = true; - this.activePrinting = activePrinting; } public override string DebugInfo { get { - return $"Last Destination = {LastDestination}"; + return $"Last Destination = {lastUnleveledDestination}"; } } public bool AllowLeveling { get; set; } - public PrinterMove LastDestination => _lastDestination; + private PrinterMove lastUnleveledDestination = PrinterMove.Unknown; - bool LevelingActive + private bool LevelingActive { get { @@ -74,7 +71,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io public override string ReadLine() { - if(!wroteLevelingStatus && LevelingActive) + if (!wroteLevelingStatus && LevelingActive) { wroteLevelingStatus = true; return "; Software Leveling Applied"; @@ -99,12 +96,12 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io { if (LineIsMovement(lineToSend)) { - PrinterMove currentDestination = GetPosition(lineToSend, LastDestination); - var leveledLine = GetLeveledPosition(lineToSend, currentDestination); + PrinterMove currentUnleveledDestination = GetPosition(lineToSend, lastUnleveledDestination); + var leveledLine = GetLeveledPosition(lineToSend, currentUnleveledDestination); // TODO: clamp to 0 - baby stepping - extruder z-offset, so we don't go below the bed (for the active extruder) - _lastDestination = currentDestination; + lastUnleveledDestination = currentUnleveledDestination; return leveledLine; } @@ -120,6 +117,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io public override void SetPrinterPosition(PrinterMove position) { + this.lastUnleveledDestination.CopyKnowSettings(position); + if (LevelingActive && position.PositionFullyKnown) { @@ -129,17 +128,16 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io PrinterMove leveledDestination = GetPosition(leveledPosition, PrinterMove.Unknown); PrinterMove deltaToLeveledPosition = leveledDestination - position; - PrinterMove withoutLevelingOffset = position - deltaToLeveledPosition; + PrinterMove withLevelingOffset = position - deltaToLeveledPosition; - _lastDestination = withoutLevelingOffset; - _lastDestination.extrusion = position.extrusion; - _lastDestination.feedRate = position.feedRate; + // clean up settings that we don't want to be subtracted + withLevelingOffset.extrusion = position.extrusion; + withLevelingOffset.feedRate = position.feedRate; - internalStream.SetPrinterPosition(_lastDestination); + internalStream.SetPrinterPosition(withLevelingOffset); } else { - this._lastDestination.CopyKnowSettings(position); internalStream.SetPrinterPosition(position); } } diff --git a/MatterControlLib/PrinterCommunication/PrinterConnection.cs b/MatterControlLib/PrinterCommunication/PrinterConnection.cs index 53bf2a915..77487ff97 100644 --- a/MatterControlLib/PrinterCommunication/PrinterConnection.cs +++ b/MatterControlLib/PrinterCommunication/PrinterConnection.cs @@ -2318,7 +2318,7 @@ You will then need to logout and log back in to the computer for the changes to if (!LevelingValidation.NeedsToBeRun(Printer)) { - accumulatedStream = printLevelingStream = new PrintLevelingStream(Printer, accumulatedStream, true); + accumulatedStream = printLevelingStream = new PrintLevelingStream(Printer, accumulatedStream); } accumulatedStream = waitForTempStream = new WaitForTempStream(Printer, accumulatedStream);