Interesting progress on always sending as print.

relates to bug MatterHackers/MCCentral#90
Long G-Code macros do not work
This commit is contained in:
Lars Brubaker 2016-11-03 17:34:36 -07:00
parent 53cdc9b0da
commit 7232620360
3 changed files with 104 additions and 96 deletions

View file

@ -0,0 +1,60 @@
/*
Copyright (c) 2014, Lars Brubaker
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using MatterHackers.Agg;
using MatterHackers.GCodeVisualizer;
using MatterHackers.VectorMath;
using System.Text;
using System.Collections.Generic;
using System.Threading;
namespace MatterHackers.MatterControl.PrinterCommunication.Io
{
public class NotPrintingStream : GCodeStream
{
public NotPrintingStream()
{
}
public override void Dispose()
{
}
public override string ReadLine()
{
Thread.Sleep(100);
return "";
}
public override void SetPrinterPosition(PrinterMove position)
{
}
}
}

View file

@ -175,8 +175,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication
private PrinterMove lastReportedPosition;
private List<string> LinesToWriteQueue = new List<string>();
DataViewGraph sendTimeAfterOkGraph;
private GCodeFile loadedGCode = new GCodeFileLoaded();
@ -219,8 +217,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication
private double[] targetExtruderTemperature = new double[MAX_EXTRUDERS];
private System.Diagnostics.Stopwatch temperatureRequestTimer = new System.Diagnostics.Stopwatch();
private Stopwatch timeHaveBeenWaitingForOK = new Stopwatch();
private Stopwatch timeSinceLastReadAnything = new Stopwatch();
@ -233,8 +229,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication
private Stopwatch timeWaitingForSdProgress = new Stopwatch();
private Stopwatch timeWaitingForTemperature = new Stopwatch();
private double totalSdBytes = 0;
private Stopwatch waitingForPosition = new Stopwatch();
@ -449,7 +443,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication
break;
case CommunicationStates.Connected:
timeWaitingForTemperature.Stop(); // make sure we try again to send temps
SendLineToPrinterNow("M115");
SendLineToPrinterNow("M114");
break;
@ -1071,7 +1064,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
PrinterOutputCache.Instance.Clear();
LinesToWriteQueue.Clear();
//Attempt connecting to a specific printer
this.stopTryingToConnect = false;
firmwareType = FirmwareTypes.Unknown;
@ -1154,7 +1146,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
serialPort = null;
CommunicationState = CommunicationStates.Disconnected;
LinesToWriteQueue.Clear();
}
else
{
@ -1359,39 +1350,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication
{
ReadThread.Start();
}
if (!temperatureRequestTimer.IsRunning)
{
temperatureRequestTimer.Start();
}
if (temperatureRequestTimer.ElapsedMilliseconds > 2000)
{
if (!PrinterIsPrinting
&& MonitorPrinterTemperature
&& (!timeWaitingForTemperature.IsRunning || timeWaitingForTemperature.Elapsed.TotalSeconds > 60))
{
timeWaitingForTemperature.Restart();
SendLineToPrinterNow("M105");
}
if (CommunicationState == CommunicationStates.PrintingFromSd
&& (!timeWaitingForSdProgress.IsRunning || timeWaitingForSdProgress.Elapsed.TotalSeconds > 10))
{
timeWaitingForSdProgress.Restart();
SendLineToPrinterNow("M27"); // : Report SD print status
}
temperatureRequestTimer.Restart();
}
bool waited30SecondsForOk = timeHaveBeenWaitingForOK.Elapsed.TotalSeconds > 30; // waited for more than 30 seconds
bool noResponseFor5Seconds = timeSinceLastReadAnything.Elapsed.TotalSeconds > 5;
while (LinesToWriteQueue.Count > 0 &&
(waited30SecondsForOk || !timeHaveBeenWaitingForOK.IsRunning || noResponseFor5Seconds))
{
WriteNextLineFromQueue();
}
}
public void OnPrintFinished(EventArgs e)
@ -1637,8 +1595,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
while (CommunicationState == CommunicationStates.AttemptingToConnect
|| (PrinterIsConnected && serialPort != null && serialPort.IsOpen && !Disconnecting && readThreadHolder.IsCurrentThread()))
{
if (PrinterIsPrinting
&& PrinterIsConnected
if (PrinterIsConnected
&& CommunicationState != CommunicationStates.PrintingFromSd)
{
TryWriteNextLineFromGCodeFile();
@ -1883,9 +1840,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
}
}
// We read them so we are no longer waiting
timeWaitingForTemperature.Stop();
}
public void RebootBoard()
@ -1988,7 +1942,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
else
{
LinesToWriteQueue.Clear();
pauseHandlingStream1.Resume();
CommunicationState = CommunicationStates.Printing;
}
@ -2041,28 +1994,17 @@ namespace MatterHackers.MatterControl.PrinterCommunication
lineToWrite = lineToWrite.Split(';')[0].Trim();
if (lineToWrite.Trim().Length > 0)
{
if (PrinterIsPrinting && CommunicationState != CommunicationStates.PrintingFromSd
&& !ForceImmediateWrites)
if (CommunicationState == CommunicationStates.PrintingFromSd
|| ForceImmediateWrites)
{
// sometimes we need to send code without buffering (like when we are closing the program).
WriteRawToPrinter(lineToWrite + "\r\n", lineToWrite);
}
else
{
// insert the command into the printing queue at the head
InjectGCode(lineToWrite);
}
else
{
// sometimes we need to send code without buffering (like when we are closing the program).
if (ForceImmediateWrites)
{
WriteToPrinter(lineToWrite + "\r\n", lineToWrite);
}
else
{
// try not to write the exact same command twice (like M105)
if (LinesToWriteQueue.Count == 0 || LinesToWriteQueue[LinesToWriteQueue.Count - 1] != lineToWrite)
{
LinesToWriteQueue.Add(lineToWrite);
}
}
}
}
}
}
@ -2123,7 +2065,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication
waitingForPosition.Stop();
waitingForPosition.Reset();
LinesToWriteQueue.Clear();
ClearQueuedGCode();
activePrintTask = printTaskToUse;
@ -2299,6 +2240,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication
Console.WriteLine("ReadFromPrinter thread created.");
ReadThread.Start();
CreateStreamProcessors(null, false);
// We have to send a line because some printers (like old print-r-bots) do not send anything when connecting and there is no other way to know they are there.
SendLineToPrinterNow("M105");
// We do not need to wait for the M105
@ -2487,28 +2430,39 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
}
private void LoadGCodeToPrint(string gcodeFilename)
void CreateStreamProcessors(string gcodeFilename, bool recoveryEnabled)
{
totalGCodeStream?.Dispose();
loadedGCode = GCodeFile.Load(gcodeFilename);
gCodeFileStream0 = new GCodeFileStream(loadedGCode);
if(ActiveSliceSettings.Instance.GetValue<bool>(SettingsKey.recover_is_enabled)
&& activePrintTask != null) // We are resuming a failed print (do lots of interesting stuff).
GCodeStream firstStream = null;
if (gcodeFilename != null)
{
pauseHandlingStream1 = new PauseHandlingStream(new PrintRecoveryStream(gCodeFileStream0, activePrintTask.PercentDone));
loadedGCode = GCodeFile.Load(gcodeFilename);
gCodeFileStream0 = new GCodeFileStream(loadedGCode);
if (ActiveSliceSettings.Instance.GetValue<bool>(SettingsKey.recover_is_enabled)
&& activePrintTask != null) // We are resuming a failed print (do lots of interesting stuff).
{
pauseHandlingStream1 = new PauseHandlingStream(new PrintRecoveryStream(gCodeFileStream0, activePrintTask.PercentDone));
}
else
{
pauseHandlingStream1 = new PauseHandlingStream(gCodeFileStream0);
}
firstStream = pauseHandlingStream1;
}
else
{
pauseHandlingStream1 = new PauseHandlingStream(gCodeFileStream0);
firstStream = new NotPrintingStream();
}
queuedCommandStream2 = new QueuedCommandsStream(pauseHandlingStream1);
queuedCommandStream2 = new QueuedCommandsStream(firstStream);
relativeToAbsoluteStream3 = new RelativeToAbsoluteStream(queuedCommandStream2);
printLevelingStream4 = new PrintLevelingStream(relativeToAbsoluteStream3, true);
waitForTempStream5 = new WaitForTempStream(printLevelingStream4);
babyStepsStream6 = new BabyStepsStream(waitForTempStream5);
if(activePrintTask != null)
if (activePrintTask != null)
{
// make sure we are in the position we were when we stopped printing
babyStepsStream6.Offset = new Vector3(activePrintTask.PrintingOffsetX, activePrintTask.PrintingOffsetY, activePrintTask.PrintingOffsetZ);
@ -2519,6 +2473,11 @@ namespace MatterHackers.MatterControl.PrinterCommunication
totalGCodeStream = requestTemperaturesStream9;
}
private void LoadGCodeToPrint(string gcodeFilename)
{
CreateStreamProcessors(gcodeFilename, ActiveSliceSettings.Instance.GetValue<bool>(SettingsKey.recover_is_enabled));
}
private void DoneLoadingGCodeToPrint()
{
switch (communicationState)
@ -2771,7 +2730,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
{
if (firstLineToResendIndex < allCheckSumLinesSent.Count)
{
WriteToPrinter(allCheckSumLinesSent[firstLineToResendIndex++] + "\r\n", "resend");
WriteRawToPrinter(allCheckSumLinesSent[firstLineToResendIndex++] + "\r\n", "resend");
}
else
{
@ -2837,7 +2796,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
TurnOffBedAndExtruders();
printWasCanceled = false;
}
else // we finished printing normally
else if(communicationState == CommunicationStates.Printing)// we finished printing normally
{
CommunicationState = CommunicationStates.FinishedPrint;
@ -2846,6 +2805,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication
// never leave the extruder and the bed hot
ReleaseMotors();
TurnOffBedAndExtruders();
// get us back to the no printing setting
CreateStreamProcessors(null, false);
}
}
}
@ -2881,7 +2843,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
//else
{
WriteToPrinter(lineWithChecksum + "\r\n", lineToWrite);
WriteRawToPrinter(lineWithChecksum + "\r\n", lineToWrite);
}
}
@ -2896,22 +2858,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
return lineToWrite;
}
private void WriteNextLineFromQueue()
{
string lineToWrite = LinesToWriteQueue[0];
lock (locker)
{
lineToWrite = RemoveCommentIfAny(lineToWrite);
KeepTrackOfAbsolutePostionAndDestination(lineToWrite);
LinesToWriteQueue.RemoveAt(0); // remove the line first (in case we inject another command)
WriteToPrinter(lineToWrite + "\r\n", lineToWrite);
}
System.Threading.Thread.Sleep(1);
}
private void WriteToPrinter(string lineToWrite, string lineWithoutChecksum)
private void WriteRawToPrinter(string lineToWrite, string lineWithoutChecksum)
{
if (PrinterIsConnected || CommunicationState == CommunicationStates.AttemptingToConnect)
{