From cae6a7679b1b9733926cc19295f76cd59289a825 Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Mon, 25 Mar 2019 17:55:52 -0700 Subject: [PATCH] Got one of the dual extruder switching tests passing Tool change stream tracking requested temps Made printer use settings for settings rather than keeping a copy Moved getting a line without checksum to GCodeFile so it can be re-used --- MatterControl.Printing/GCode/GCodeFile.cs | 19 +++++ .../ApplicationView/PrinterConfig.cs | 61 ---------------- .../Io/RequestTemperaturesStream.cs | 2 +- .../Io/ToolChangeStream.cs | 39 +++++------ .../Io/WaitForTempStream.cs | 7 +- .../PrinterCommunication/PrinterConnection.cs | 48 ++++++++----- .../TerminalWindow/TerminalWidget.cs | 22 ++---- .../MatterControl/GCodeStreamTests.cs | 70 +++++++++---------- 8 files changed, 112 insertions(+), 156 deletions(-) diff --git a/MatterControl.Printing/GCode/GCodeFile.cs b/MatterControl.Printing/GCode/GCodeFile.cs index 1990a68c0..2aebdede0 100644 --- a/MatterControl.Printing/GCode/GCodeFile.cs +++ b/MatterControl.Printing/GCode/GCodeFile.cs @@ -131,6 +131,25 @@ namespace MatterControl.Printing return GetFirstNumberAfter(stringToCheckAfter, stringWithNumber, ref readValue, out _, startIndex, stopCheckingString); } + public static string GetLineWithoutChecksum(string inLine) + { + if (inLine.StartsWith("N")) + { + int lineNumber = 0; + if (GCodeFile.GetFirstNumberAfter("N", inLine, ref lineNumber, out int numberEnd)) + { + var outLine = inLine.Substring(numberEnd).Trim(); + int checksumStart = outLine.IndexOf('*'); + if (checksumStart != -1) + { + return outLine.Substring(0, checksumStart); + } + } + } + + return inLine; + } + public static bool GetFirstNumberAfter(string stringToCheckAfter, string stringWithNumber, ref int readValue, out int numberEnd, int startIndex = 0, string stopCheckingString = ";") { double doubleValue = readValue; diff --git a/MatterControlLib/ApplicationView/PrinterConfig.cs b/MatterControlLib/ApplicationView/PrinterConfig.cs index eff3225ce..fd4cd344f 100644 --- a/MatterControlLib/ApplicationView/PrinterConfig.cs +++ b/MatterControlLib/ApplicationView/PrinterConfig.cs @@ -95,20 +95,6 @@ namespace MatterHackers.MatterControl this.Bed.InvalidateBedMesh(); this.Settings.SettingChanged += Printer_SettingChanged; - - if (!string.IsNullOrEmpty(this.Settings.GetValue(SettingsKey.baud_rate))) - { - this.Connection.BaudRate = this.Settings.GetValue(SettingsKey.baud_rate); - } - - this.Connection.ConnectGCode = this.Settings.GetValue(SettingsKey.connect_gcode); - this.Connection.CancelGCode = this.Settings.GetValue(SettingsKey.cancel_gcode); - this.Connection.EnableNetworkPrinting = this.Settings.GetValue(SettingsKey.enable_network_printing); - this.Connection.AutoReleaseMotors = this.Settings.GetValue(SettingsKey.auto_release_motors); - this.Connection.RecoveryIsEnabled = this.Settings.GetValue(SettingsKey.recover_is_enabled); - this.Connection.ExtruderCount = this.Settings.GetValue(SettingsKey.extruder_count); - this.Connection.SendWithChecksum = this.Settings.GetValue(SettingsKey.send_with_checksum); - this.Connection.ReadLineReplacementString = this.Settings.GetValue(SettingsKey.read_regex); } private void InitMacroReplacements() @@ -426,53 +412,6 @@ namespace MatterHackers.MatterControl this.ReloadBedSettings(); this.Bed.InvalidateBedMesh(); } - - // Sync settings changes to printer connection - switch(stringEvent.Data) - { - case SettingsKey.feedrate_ratio: - this.Connection.FeedRateRatio = this.Settings.GetValue(SettingsKey.feedrate_ratio); - break; - - case SettingsKey.baud_rate: - if (!string.IsNullOrEmpty(this.Settings.GetValue(SettingsKey.baud_rate))) - { - this.Connection.BaudRate = this.Settings.GetValue(SettingsKey.baud_rate); - } - break; - - case SettingsKey.connect_gcode: - this.Connection.ConnectGCode = this.Settings.GetValue(SettingsKey.connect_gcode); - break; - - case SettingsKey.cancel_gcode: - this.Connection.CancelGCode = this.Settings.GetValue(SettingsKey.cancel_gcode); - break; - - case SettingsKey.enable_network_printing: - this.Connection.EnableNetworkPrinting = this.Settings.GetValue(SettingsKey.enable_network_printing); - break; - - case SettingsKey.auto_release_motors: - this.Connection.AutoReleaseMotors = this.Settings.GetValue(SettingsKey.auto_release_motors); - break; - - case SettingsKey.recover_is_enabled: - this.Connection.RecoveryIsEnabled = this.Settings.GetValue(SettingsKey.recover_is_enabled); - break; - - case SettingsKey.extruder_count: - this.Connection.ExtruderCount = this.Settings.GetValue(SettingsKey.extruder_count); - break; - - case SettingsKey.send_with_checksum: - this.Connection.SendWithChecksum = this.Settings.GetValue(SettingsKey.send_with_checksum); - break; - - case SettingsKey.read_regex: - this.Connection.ReadLineReplacementString = this.Settings.GetValue(SettingsKey.read_regex); - break; - } } } diff --git a/MatterControlLib/PrinterCommunication/Io/RequestTemperaturesStream.cs b/MatterControlLib/PrinterCommunication/Io/RequestTemperaturesStream.cs index 8704a9238..074c0216f 100644 --- a/MatterControlLib/PrinterCommunication/Io/RequestTemperaturesStream.cs +++ b/MatterControlLib/PrinterCommunication/Io/RequestTemperaturesStream.cs @@ -49,7 +49,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io if (!printer.Connection.WaitingForPositionRead && nextReadTimeMs < UiThread.CurrentTimerMs && printer.Connection.IsConnected - && (printer.Connection.Printing || printer.Connection.MonitorPrinterTemperature)) + && printer.Connection.MonitorPrinterTemperature) { nextReadTimeMs = UiThread.CurrentTimerMs + 1000; return "M105"; diff --git a/MatterControlLib/PrinterCommunication/Io/ToolChangeStream.cs b/MatterControlLib/PrinterCommunication/Io/ToolChangeStream.cs index bf9323920..e809bbe5c 100644 --- a/MatterControlLib/PrinterCommunication/Io/ToolChangeStream.cs +++ b/MatterControlLib/PrinterCommunication/Io/ToolChangeStream.cs @@ -49,6 +49,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io private int requestedTool; enum SendStates { Normal, WaitingForMove, SendingBefore } private SendStates SendState = SendStates.Normal; + private double[] targetTemps = new double[4]; public ToolChangeStream(PrinterConfig printer, GCodeStream internalStream, QueuedCommandsStream queuedCommandsStream) : base(printer, internalStream) @@ -80,6 +81,15 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io return lineToSend; } + if (lineToSend.StartsWith("M109") || lineToSend.StartsWith("M104")) + { + int toolTemp = 0; + int toolIndex = activeTool; + GCodeFile.GetFirstNumberAfter("T", lineToSend, ref toolIndex); + GCodeFile.GetFirstNumberAfter("S", lineToSend, ref toolTemp); + targetTemps[toolIndex] = toolTemp; + } + // check if any of the heaters we will be switching to need to start heating ManageReHeating(lineToSend); @@ -173,31 +183,16 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io // If there is enough time before we will use this tool again, lower the temp by the inactive_cool_down else if (nextTimeThisTool > timeToReheat) { - var targetTemp = PrintingTemperature(activeTool); + var targetTemp = targetTemps[activeTool]; targetTemp = Math.Max(0, targetTemp - printer.Settings.GetValue(SettingsKey.inactive_cool_down)); - gcode.AppendLine($"M104 T{activeTool} S{targetTemp}"); + if (targetTemp != printer.Connection.GetTargetHotendTemperature(activeTool)) + { + gcode.AppendLine($"M104 T{activeTool} S{targetTemp}"); + } } } } - double PrintingTemperature(int toolIndex) - { - switch(toolIndex) - { - default: - return printer.Settings.GetValue(SettingsKey.temperature); - - case 1: - return printer.Settings.GetValue(SettingsKey.temperature1); - - case 2: - return printer.Settings.GetValue(SettingsKey.temperature2); - - case 3: - return printer.Settings.GetValue(SettingsKey.temperature3); - } - } - private void ManageReHeating(string line) { var timeToReheat = printer.Settings.GetValue(SettingsKey.seconds_to_reheat); @@ -207,7 +202,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io for (int i = 0; i < extruderCount; i++) { var nextToolChange = printer.Connection.NextToolChange(i); - var targetTemp = printer.Settings.Helpers.ExtruderTargetTemperature(i); + var targetTemp = targetTemps[i]; if (nextToolChange.toolIndex >= 0 && nextToolChange.time < timeToReheat && printer.Connection.GetTargetHotendTemperature(i) != targetTemp) @@ -248,7 +243,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io var gcode = new StringBuilder(); // If the printer is heating, make sure we are at temp before switching extruders - var nextToolTargetTemp = PrintingTemperature(requestedTool); + var nextToolTargetTemp = targetTemps[requestedTool]; var currentPrinterTargeTemp = printer.Connection.GetTargetHotendTemperature(requestedTool); if (currentPrinterTargeTemp > 0 && printer.Connection.GetActualHotendTemperature(requestedTool) < nextToolTargetTemp - 3) diff --git a/MatterControlLib/PrinterCommunication/Io/WaitForTempStream.cs b/MatterControlLib/PrinterCommunication/Io/WaitForTempStream.cs index f5c7876d0..928d6a4ca 100644 --- a/MatterControlLib/PrinterCommunication/Io/WaitForTempStream.cs +++ b/MatterControlLib/PrinterCommunication/Io/WaitForTempStream.cs @@ -59,7 +59,12 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io } private enum State - { Passthrough, WaitingForBedTemp, WaitingForT0Temp, WaitingForT1Temp }; + { + Passthrough, + WaitingForBedTemp, + WaitingForT0Temp, + WaitingForT1Temp + }; public bool HeatingBed { get { return state == State.WaitingForBedTemp; } } public bool HeatingT0 { get { return state == State.WaitingForT0Temp; } } diff --git a/MatterControlLib/PrinterCommunication/PrinterConnection.cs b/MatterControlLib/PrinterCommunication/PrinterConnection.cs index fc6b55bcf..a4dfc1f0e 100644 --- a/MatterControlLib/PrinterCommunication/PrinterConnection.cs +++ b/MatterControlLib/PrinterCommunication/PrinterConnection.cs @@ -101,7 +101,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication public event EventHandler BedTemperatureRead; public event EventHandler CommunicationStateChanged; - + public event EventHandler DetailedPrintingStateChanged; public event EventHandler ConnectionFailed; @@ -423,23 +423,34 @@ namespace MatterHackers.MatterControl.PrinterCommunication // PrinterSettings/Options {{ - public int BaudRate { get; set; } = 250000; + public int BaudRate + { + get + { + if (!string.IsNullOrEmpty(Printer.Settings.GetValue(SettingsKey.baud_rate))) + { + return 250000; + } - public double FeedRateRatio { get; set; } = 1; + return Printer.Settings.GetValue(SettingsKey.baud_rate); + } + } - public string ConnectGCode { get; set; } = ""; + public double FeedRateRatio => Printer.Settings.GetValue(SettingsKey.feedrate_ratio); - public string CancelGCode { get; set; } = ""; + public string ConnectGCode => Printer.Settings.GetValue(SettingsKey.connect_gcode); - public int ExtruderCount { get; set; } + public string CancelGCode => Printer.Settings.GetValue(SettingsKey.cancel_gcode); - public bool SendWithChecksum { get; set; } + public int ExtruderCount => Printer.Settings.GetValue(SettingsKey.extruder_count); - public bool EnableNetworkPrinting { get; set; } + public bool SendWithChecksum => Printer.Settings.GetValue(SettingsKey.send_with_checksum); - public bool AutoReleaseMotors { get; set; } + public bool EnableNetworkPrinting => Printer.Settings.GetValue(SettingsKey.enable_network_printing); - public bool RecoveryIsEnabled { get; set; } + public bool AutoReleaseMotors => Printer.Settings.GetValue(SettingsKey.auto_release_motors); + + public bool RecoveryIsEnabled => Printer.Settings.GetValue(SettingsKey.recover_is_enabled); public string LastPrintedItemName { get; private set; } = ""; public string PrintingItemName { get; set; } = ""; @@ -448,12 +459,12 @@ namespace MatterHackers.MatterControl.PrinterCommunication private string _readLineReplacementString = ""; public string ReadLineReplacementString { - get => _readLineReplacementString; - set + get { - if (value != _readLineReplacementString) + var readRegEx = Printer.Settings.GetValue(SettingsKey.read_regex); + if(readRegEx != _readLineReplacementString) { - _readLineReplacementString = value; + _readLineReplacementString = readRegEx; // Clear and rebuild the replacement list readLineReplacements.Clear(); @@ -469,6 +480,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication } } } + + return _readLineReplacementString; } } @@ -527,7 +540,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication if (communicationState != value) { - LineSent?.Invoke(this, string.Format("Communication State: {0}\n", value)); + LineSent?.Invoke(this, string.Format("Communication State: {0}", value)); switch (communicationState) { @@ -564,7 +577,10 @@ namespace MatterHackers.MatterControl.PrinterCommunication // Set this early as we always want our functions to know the state we are in. communicationState = value; timePrinting.Stop(); - PrintFinished?.Invoke(this, new PrintFinishedEventArgs(Printer.Bed.EditContext.SourceItem.Name)); + if (Printer.Bed?.EditContext?.SourceItem.Name != null) + { + PrintFinished?.Invoke(this, new PrintFinishedEventArgs(Printer.Bed.EditContext.SourceItem.Name)); + } } else { diff --git a/MatterControlLib/PrinterControls/TerminalWindow/TerminalWidget.cs b/MatterControlLib/PrinterControls/TerminalWindow/TerminalWidget.cs index 689522f8e..ed0c4644f 100644 --- a/MatterControlLib/PrinterControls/TerminalWindow/TerminalWidget.cs +++ b/MatterControlLib/PrinterControls/TerminalWindow/TerminalWidget.cs @@ -111,26 +111,14 @@ namespace MatterHackers.MatterControl textScrollWidget.LineFilterFunction = lineData => { var line = lineData.line; - var lineWithoutChecksum = line; var outputLine = line; - if (lineWithoutChecksum.StartsWith("N")) + var lineWithoutChecksum = GCodeFile.GetLineWithoutChecksum(line); + + // and set this as the output if desired + if (!UserSettings.Instance.Fields.GetBool(UserSettingsKey.TerminalShowChecksum, true)) { - int lineNumber = 0; - if (GCodeFile.GetFirstNumberAfter("N", lineWithoutChecksum, ref lineNumber, out int numberEnd)) - { - lineWithoutChecksum = lineWithoutChecksum.Substring(numberEnd).Trim(); - int checksumStart = lineWithoutChecksum.IndexOf('*'); - if (checksumStart != -1) - { - lineWithoutChecksum = lineWithoutChecksum.Substring(0, checksumStart); - // and set this as the output if desired - if(!UserSettings.Instance.Fields.GetBool(UserSettingsKey.TerminalShowChecksum, true)) - { - outputLine = lineWithoutChecksum; - } - } - } + outputLine = lineWithoutChecksum; } if (lineWithoutChecksum.StartsWith("ok") diff --git a/Tests/MatterControl.Tests/MatterControl/GCodeStreamTests.cs b/Tests/MatterControl.Tests/MatterControl/GCodeStreamTests.cs index 3030486a4..4d32af3ad 100644 --- a/Tests/MatterControl.Tests/MatterControl/GCodeStreamTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/GCodeStreamTests.cs @@ -565,10 +565,11 @@ namespace MatterControl.Tests.MatterControl } [Test, Category("GCodeStream")] - public void ToolChangeNoHeatNoExtrusion() + public Task ToolChangeNoHeatNoExtrusion() { string[] inputLines = new string[] { + "T0", // send some movement comands with tool switching "; the printer is moving normally", "G1 X10 Y10 Z10 E0 F2500", @@ -583,37 +584,33 @@ namespace MatterControl.Tests.MatterControl string[] expected = new string[] { - "; the printer is moving normally", - "G1 X10 Y10 Z10 E0 F2500", - // the code to switch to t1 - "; waiting for move on T1", - "", - "; simulated before toolchange 1 gcode", - "T1", - "; COMPLEATED_BEFORE_GCODE", - "; simulated after toolchange 1 gcode", - "G1 X9 Y8 Z7 F3000", // the F comes from the x movement speed - "G1 X9 Y8 Z7 F315", // the F comes from the z movement speed - "G1 X9 Y8 Z7 F2500", // restore the feedrate - "G1 X9 Y8 Z7", - // the code to switch back to t0 - "; waiting for move on T0", - "", - "; simulated before toolchange gcode", + "M114", "T0", - "; COMPLEATED_BEFORE_GCODE", - "; simulated after toolchange gcode", - "G1 F3000", // the F comes from the x movement speed - "G1 F315", // the F comes from the z movement speed - "G1 F2500", // restore the feedrate + "M114", + "G1 X10 Y10 Z10 F2500", + "G1 Y111", + "T1", + "M114", // after a tool change we inject an M114 + "G1 Y220", + "G1 X9 Y8 F3000", + "G1 Z7 F315", + "G1 F2500", // 11 "G1", + "G1 X332", + "T0", // 14 + "M114", + "G1 X444", + "G1 X10 Y10 F3000", + "G1 Z10 F315", + "G1 F2500", + "G1", + "Communication State: FinishedPrint", null, }; PrinterConfig printer = SetupToolChangeSettings(); - - var testStream = GCodeExport.GetExportStream(printer, new TestGCodeStream(printer, inputLines), true); - ValidateStreamResponse(expected, testStream); + ValidateStreamResponseWhilePrintingAsync(expected, printer, inputLines).GetAwaiter().GetResult(); + return Task.CompletedTask; } [Test, Category("GCodeStream")] @@ -669,9 +666,7 @@ namespace MatterControl.Tests.MatterControl }; PrinterConfig printer = SetupToolChangeSettings(); - ValidateStreamResponseWhilePrintingAsync(expected, printer, inputLines).GetAwaiter().GetResult(); - return Task.CompletedTask; } @@ -688,11 +683,11 @@ namespace MatterControl.Tests.MatterControl printer.Settings.SetValue(SettingsKey.enable_line_splitting, "0"); - printer.Settings.SetValue(SettingsKey.toolchange_gcode, "; simulated after toolchange gcode"); - printer.Settings.SetValue(SettingsKey.toolchange_gcode_1, "; simulated after toolchange 1 gcode"); + printer.Settings.SetValue(SettingsKey.before_toolchange_gcode, "G1 X333"); + printer.Settings.SetValue(SettingsKey.toolchange_gcode, "G1 X444"); - printer.Settings.SetValue(SettingsKey.before_toolchange_gcode, "; simulated before toolchange gcode"); - printer.Settings.SetValue(SettingsKey.before_toolchange_gcode_1, "; simulated before toolchange 1 gcode"); + printer.Settings.SetValue(SettingsKey.before_toolchange_gcode_1, "G1 Y111"); + printer.Settings.SetValue(SettingsKey.toolchange_gcode_1, "G1 Y222"); // set some data for T1 printer.Settings.Helpers.SetExtruderOffset(1, new Vector3(1, 2, 3)); @@ -766,7 +761,8 @@ namespace MatterControl.Tests.MatterControl if (printer.Connection.Printing) { string expectedLine = expected[expectedIndex++]; - Assert.AreEqual(expectedLine, actualLine, "Unexpected response from testStream"); + var actualLineWithoutChecksum = GCodeFile.GetLineWithoutChecksum(actualLine); + Assert.AreEqual(expectedLine, actualLineWithoutChecksum, "Unexpected response from testStream"); } }; @@ -788,15 +784,13 @@ namespace MatterControl.Tests.MatterControl // wait for the print to finish (or 3 minutes to pass) time = Stopwatch.StartNew(); - while(//printer.Connection.Printing - //&& - time.ElapsedMilliseconds < (1000 * 60 * 3)) + while(printer.Connection.Printing + && time.ElapsedMilliseconds < (1000 * 60 * 3)) { Thread.Sleep(1000); - //printer.Connection.upda } - Assert.AreEqual(expectedIndex, expected.Length, "We should have seen all the expected lines"); + Assert.AreEqual(expectedIndex + 1, expected.Length, "We should have seen all the expected lines"); } private static void Connection_LineReceived(object sender, string e)