From db6ff0dca3971529f3d4b8f6a0b730162435d6fd Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Fri, 26 Apr 2019 10:01:02 -0700 Subject: [PATCH] fixing warnings --- .../GCode/GCodeMemoryFile.cs | 2 +- .../PrinterCommunication/Io/GCodeStream.cs | 2 +- .../Io/QueuedCommandsStream.cs | 3 +- .../Io/ToolChangeStream.cs | 40 +++++---- .../MatterControl/ToolChangeTests.cs | 83 ++++++++++++++++++- 5 files changed, 111 insertions(+), 19 deletions(-) diff --git a/MatterControl.Printing/GCode/GCodeMemoryFile.cs b/MatterControl.Printing/GCode/GCodeMemoryFile.cs index fe0792ae7..addfcbed5 100644 --- a/MatterControl.Printing/GCode/GCodeMemoryFile.cs +++ b/MatterControl.Printing/GCode/GCodeMemoryFile.cs @@ -463,7 +463,7 @@ namespace MatterControl.Printing var length = endIndex - startIndex; if (length > 0) { - return deltaFromStart / (double)(length); + return deltaFromStart / (double)length; } } diff --git a/MatterControlLib/PrinterCommunication/Io/GCodeStream.cs b/MatterControlLib/PrinterCommunication/Io/GCodeStream.cs index ed2595a6f..ce30f4434 100644 --- a/MatterControlLib/PrinterCommunication/Io/GCodeStream.cs +++ b/MatterControlLib/PrinterCommunication/Io/GCodeStream.cs @@ -47,7 +47,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io public abstract void Dispose(); - bool useG0ForMovement = false; + private readonly bool useG0ForMovement = false; protected PrinterConfig printer { get; } diff --git a/MatterControlLib/PrinterCommunication/Io/QueuedCommandsStream.cs b/MatterControlLib/PrinterCommunication/Io/QueuedCommandsStream.cs index 0eb7dde49..e3a90180c 100644 --- a/MatterControlLib/PrinterCommunication/Io/QueuedCommandsStream.cs +++ b/MatterControlLib/PrinterCommunication/Io/QueuedCommandsStream.cs @@ -93,7 +93,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io lineIn = lineIn.Replace("\\n", "\n"); } - //Check line for line breaks, split and process separate if necessary + // Check line for line breaks, split and process separate if necessary if (lineIn.Contains("\n")) { string[] linesToWrite = lineIn.Split(new string[] { "\n" }, StringSplitOptions.None); @@ -105,6 +105,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io this.Add(line); } } + return; } diff --git a/MatterControlLib/PrinterCommunication/Io/ToolChangeStream.cs b/MatterControlLib/PrinterCommunication/Io/ToolChangeStream.cs index 8662c43e9..76841d8ce 100644 --- a/MatterControlLib/PrinterCommunication/Io/ToolChangeStream.cs +++ b/MatterControlLib/PrinterCommunication/Io/ToolChangeStream.cs @@ -47,8 +47,15 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io private Vector3 preSwitchPosition; private QueuedCommandsStream queuedCommandsStream; private int requestedTool; - enum SendStates { Normal, WaitingForMove, SendingBefore } - private SendStates SendState = SendStates.Normal; + + private enum SendStates + { + Normal, + WaitingForMove, + SendingBefore + } + + private SendStates sendState = SendStates.Normal; private double[] targetTemps = new double[4]; private Queue queuedCommands = new Queue(); @@ -64,14 +71,14 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io public override string ReadLine() { - if(queuedCommands.Count > 0) + if (queuedCommands.Count > 0) { return queuedCommands.Dequeue(); } string lineToSend = base.ReadLine(); - if(lineToSend == null) + if (lineToSend == null) { return null; } @@ -105,7 +112,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io if (lineToSend == completedBeforeGCodeString) { activeTool = requestedTool; - SendState = SendStates.Normal; + sendState = SendStates.Normal; QueueAfterGCode(); } @@ -120,6 +127,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io queuedCommands.Enqueue($"T{activeTool}"); return $"{lineToSend.Substring(0, 4)} T{requestedToolForTempChange} S{targetTemps[requestedToolForTempChange]}"; } + // if we are waiting to switch to the next tool else if (activeTool != requestedTool) { @@ -128,12 +136,14 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io { queuedCommands.Enqueue($"T{requestedTool}"); } + // For smoothie, switch back to the extrude we were using before the temp change (smoothie switches to the specified extruder, marlin repetier do not) queuedCommands.Enqueue($"T{activeTool}"); // then send the heat command return lineToSend; } } + // if this is a tool change request else if (lineToSend.StartsWith("T")) { @@ -142,11 +152,11 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io { if (changeCommandTool == activeTool) { - if(SendState == SendStates.WaitingForMove) + if (sendState == SendStates.WaitingForMove) { // we have switch back to our starting tool without a move // change back to normal processing and don't change tools - SendState = SendStates.Normal; + sendState = SendStates.Normal; var lastRequestedTool = requestedTool; // set the requested tool requestedTool = changeCommandTool; @@ -156,9 +166,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io } else // we are switching tools { - if (SendState == SendStates.Normal) + if (sendState == SendStates.Normal) { - SendState = SendStates.WaitingForMove; + sendState = SendStates.WaitingForMove; // set the requested tool requestedTool = changeCommandTool; // don't queue the tool change until after the before gcode has been sent @@ -167,12 +177,13 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io } } } + // if it is only an extrusion move - if (SendState == SendStates.WaitingForMove + if (sendState == SendStates.WaitingForMove && activeTool != requestedTool // is different than the last extruder set && (lineNoComment.StartsWith("G0 ") || lineNoComment.StartsWith("G1 ")) // is a G1 or G0 && lineNoComment.Contains("E") // it is an extrusion move - // and have no other position information + // and have no other position information && !lineNoComment.Contains("X") && !lineNoComment.Contains("Y") && !lineNoComment.Contains("Z")) @@ -185,13 +196,14 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io queuedCommands.Enqueue($"T{requestedTool}"); // if we know the current E position before the switch - // set the E value to the previous E value. + // set the E value to the previous E value. if (lastDestination.extrusion != double.PositiveInfinity) { // On Marlin E position is share between extruders and this code has no utility // On Smoothie E is stored per extruder and this makes it behave the same as Marlin queuedCommands.Enqueue($"G92 E{lastDestination.extrusion}"); } + // send the extrusion queuedCommands.Enqueue(lineNoComment + " ; NO_PROCESSING"); // switch back @@ -360,7 +372,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io private bool QueueBeforeIfNeedToSwitchExtruders(string lineIn, string lineNoComment) { // check if there is a travel - if (SendState == SendStates.WaitingForMove + if (sendState == SendStates.WaitingForMove && activeTool != requestedTool // is different than the last extruder set && (lineNoComment.StartsWith("G0 ") || lineNoComment.StartsWith("G1 ")) // is a G1 or G0 && (lineNoComment.Contains("X") || lineNoComment.Contains("Y") || lineNoComment.Contains("Z"))) // has a move axis in it @@ -401,7 +413,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io queuedCommandsStream.Add(gcode.ToString()); - SendState = SendStates.SendingBefore; + sendState = SendStates.SendingBefore; return true; } diff --git a/Tests/MatterControl.Tests/MatterControl/ToolChangeTests.cs b/Tests/MatterControl.Tests/MatterControl/ToolChangeTests.cs index 132cd977a..c6f42e552 100644 --- a/Tests/MatterControl.Tests/MatterControl/ToolChangeTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/ToolChangeTests.cs @@ -237,8 +237,6 @@ namespace MatterControl.Tests.MatterControl.ToolChanges G1 X10 Y10 Z10 E0 T0 G1 X10 Y10 Z10 E0"); - // now do the same thing with a long enough print to cause - // cooling and heating); // Validate var expectedLines = new string[] @@ -274,6 +272,87 @@ namespace MatterControl.Tests.MatterControl.ToolChanges Assert.AreEqual(expectedLines, sentLines); } + [Test] + public async Task ToolChangeWithCoolDown() + { + var printer = ToolChangeTests.CreatePrinter(); + + // set cool down temp and time so the extruder will do a cool down on switch + printer.Settings.SetValue(SettingsKey.inactive_cool_down, $"{30}"); + printer.Settings.SetValue(SettingsKey.seconds_to_reheat, $"{1}"); + + // Collect gcode sent through stream processors + var sentLines = await printer.RunSimulatedPrint( + @"T0 + ; tell the printer to heat up + M104 T1 S240 ; start with T0 to test smoothie temp change code + M104 T0 S230 + ; send some movement commands with tool switching + ; the printer is moving normally + G1 X10 Y10 Z10 E0 F2500 + T1 + G1 X20 Y10 Z10 E10 F10 ; a long move with extrusion, will need to cool T0 + T0 + G1 X30 Y10 Z10 E10 F10 ; a long move with extrusion, will need to cool T1 + T1 + G1 X10 Y10 Z10 E0"); + + // Validate + var expectedLines = new string[] + { + "M114", + "T0", + "M114", + "M104 T1 S240", // T1 to 240 + "T0", + "M114", + "M104 T1 S240", + "T0", + "M114", + "M104 T0 S230", // T0 to 230 + "G1 X10 Y10 Z10 F2500", + "G1 Y111", + "M114", + "M104 T0 S200", // T0 to cool down temp + "T0", + "M114", + "T1", + "M114", + "M104 T1 S240", + "G1 Y222", + "M114", + "G1 X19 Y8 F3000", + "G1 Z7 F315", + "G1 F2500", + "G1 E10 F10", + "G1 X111", + "M114", + "M104 T1 S210", // T1 to cool down temp 210 + "T1", + "M114", + "T0", + "M114", + "M104 T0 S230", // T0 back up to 230 + "G1 X222", + "M114", + "G1 X30 Y10 F3000", + "G1 Z10 F315", + "G1 F10", + "G1 Y111", + "M114", + "T1", + "M114", + "M104 T1 S240", // T1 back up to 240 + "G1 Y222", + "M114", + "G1 X9 Y8 F3000", + "G1 Z7 F315", + "G1 F10", + "G1 E0", + }; + Assert.AreEqual(expectedLines, sentLines); + } + [Test] public async Task ToolChangeHeatOnlyT0() {