Put a try catch around the printer on idle call

Refactor / renaming
This commit is contained in:
Lars Brubaker 2015-03-25 18:01:57 -07:00
parent fd699981ec
commit 53ded760e9
8 changed files with 66 additions and 53 deletions

View file

@ -244,8 +244,11 @@ namespace MatterHackers.MatterControl.ActionBar
void onConfirmCancelPrint(bool messageBoxResponse)
{
if (messageBoxResponse)
{
CancelPrinting();
{
UiThread.RunOnIdle((state) =>
{
CancelPrinting();
});
}
}

View file

@ -289,9 +289,9 @@ namespace MatterHackers.MatterControl
PrintLevelingData levelingData = PrintLevelingData.GetForPrinter(ActivePrinterProfile.Instance.ActivePrinter);
if (levelingData != null)
{
for (int i = 0; i < unleveledGCode.Count; i++)
for (int lineIndex = 0; lineIndex < unleveledGCode.LineCount; lineIndex++)
{
PrinterMachineInstruction instruction = unleveledGCode.Instruction(i);
PrinterMachineInstruction instruction = unleveledGCode.Instruction(lineIndex);
List<string> linesToWrite = null;
switch (levelingData.levelingSystem)
@ -312,7 +312,7 @@ namespace MatterHackers.MatterControl
foreach(string line in linesToWrite)
{
PrinterMachineInstruction newInstruction = new PrinterMachineInstruction(line);
unleveledGCode.Insert(++i, newInstruction);
unleveledGCode.Insert(++lineIndex, newInstruction);
}
}
}

View file

@ -346,7 +346,17 @@ namespace MatterHackers.MatterControl
void CheckOnPrinter(object state)
{
PrinterConnectionAndCommunication.Instance.OnIdle();
try
{
PrinterConnectionAndCommunication.Instance.OnIdle();
}
catch(Exception e)
{
Debug.Print(e.Message);
#if DEBUG
throw e;
#endif
}
UiThread.RunOnIdle(CheckOnPrinter);
}

View file

@ -875,7 +875,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
if (gcodeViewWidget != null
&& gcodeViewWidget.LoadedGCode != null
&& gcodeViewWidget.LoadedGCode.Count > 0)
&& gcodeViewWidget.LoadedGCode.LineCount > 0)
{
CreateOptionsContent();
buttonRightPanel.Visible = true;

View file

@ -244,13 +244,13 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
void SetInitalLayer()
{
activeLayerIndex = 0;
if (loadedGCode.Count > 0)
if (loadedGCode.LineCount > 0)
{
int firstExtrusionIndex = 0;
Vector3 lastPosition = loadedGCode.Instruction(0).Position;
double ePosition = loadedGCode.Instruction(0).EPosition;
// let's find the first layer that has extrusion if possible and go to that
for (int i = 1; i < loadedGCode.Count; i++)
for (int i = 1; i < loadedGCode.LineCount; i++)
{
PrinterMachineInstruction currentInstruction = loadedGCode.Instruction(i);
if (currentInstruction.EPosition > ePosition && lastPosition != currentInstruction.Position)

View file

@ -38,7 +38,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
public class PrinterIoGCodeFile : PrinterIoBase
{
GCodeFile loadedGCode;
int printerCommandQueueIndex;
int printerCommandQueueLineIndex;
public PrinterIoGCodeFile(GCodeFile loadedGCode)
{
this.loadedGCode = loadedGCode;
@ -48,7 +48,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
{
get
{
if (loadedGCode.Count > 0)
if (loadedGCode.LineCount > 0)
{
return loadedGCode.Instruction(0).secondsToEndFromHere;
}
@ -62,9 +62,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
{
get
{
int currentIndex = printerCommandQueueIndex - backupAmount;
int currentIndex = printerCommandQueueLineIndex - backupAmount;
if (currentIndex >= 0
&& currentIndex < loadedGCode.Count)
&& currentIndex < loadedGCode.LineCount)
{
for (int zIndex = 0; zIndex < loadedGCode.NumChangesInZ; zIndex++)
{
@ -101,19 +101,19 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
{
get
{
int currentIndex = printerCommandQueueIndex - backupAmount;
if (currentIndex >= 0
&& currentIndex < loadedGCode.Count)
int currentLineIndex = printerCommandQueueLineIndex - backupAmount;
if (currentLineIndex >= 0
&& currentLineIndex < loadedGCode.LineCount)
{
int currentLayer = CurrentlyPrintingLayer;
int startIndex = loadedGCode.GetInstructionIndexAtLayer(currentLayer);
int endIndex = loadedGCode.Count - 1;
int endIndex = loadedGCode.LineCount - 1;
if (currentLayer < loadedGCode.NumChangesInZ - 2)
{
endIndex = loadedGCode.GetInstructionIndexAtLayer(currentLayer + 1) - 1;
}
int deltaFromStart = Math.Max(0, currentIndex - startIndex);
int deltaFromStart = Math.Max(0, currentLineIndex - startIndex);
return deltaFromStart / (double)(endIndex - startIndex);
}
@ -127,11 +127,11 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
{
if (NumberOfInstruction > 0)
{
if (printerCommandQueueIndex >= 0
&& printerCommandQueueIndex < loadedGCode.Count
&& loadedGCode.Instruction(printerCommandQueueIndex).secondsToEndFromHere != 0)
if (printerCommandQueueLineIndex >= 0
&& printerCommandQueueLineIndex < loadedGCode.LineCount
&& loadedGCode.Instruction(printerCommandQueueLineIndex).secondsToEndFromHere != 0)
{
return loadedGCode.Instruction(printerCommandQueueIndex).secondsToEndFromHere;
return loadedGCode.Instruction(printerCommandQueueLineIndex).secondsToEndFromHere;
}
}

View file

@ -357,7 +357,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
private PrintItemWrapper activePrintItem;
int lastRemainingSecondsReported = 0;
int printerCommandQueueIndex = -1;
int printerCommandQueueLineIndex = -1;
Vector3 currentDestination;
double currentFeedRate;
@ -702,7 +702,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
{
get
{
return loadedGCode.Count;
return loadedGCode.LineCount;
}
}
@ -710,7 +710,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
{
get
{
if (loadedGCode.Count > 0)
if (loadedGCode.LineCount > 0)
{
if (FeedRateRatio != 0)
{
@ -729,7 +729,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
{
get
{
int instructionIndex = printerCommandQueueIndex - backupAmount;
int instructionIndex = printerCommandQueueLineIndex - backupAmount;
return loadedGCode.GetLayerIndex(instructionIndex);
}
}
@ -754,7 +754,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
{
get
{
int instructionIndex = printerCommandQueueIndex - backupAmount;
int instructionIndex = printerCommandQueueLineIndex - backupAmount;
return loadedGCode.Ratio0to1IntoContainedLayer(instructionIndex);
}
}
@ -766,13 +766,13 @@ namespace MatterHackers.MatterControl.PrinterCommunication
{
if (NumberOfLinesInCurrentPrint > 0)
{
if (printerCommandQueueIndex >= 0
&& printerCommandQueueIndex < loadedGCode.Count
&& loadedGCode.Instruction(printerCommandQueueIndex).secondsToEndFromHere != 0)
if (printerCommandQueueLineIndex >= 0
&& printerCommandQueueLineIndex < loadedGCode.LineCount
&& loadedGCode.Instruction(printerCommandQueueLineIndex).secondsToEndFromHere != 0)
{
if (FeedRateRatio != 0)
{
lastRemainingSecondsReported = (int)(loadedGCode.Instruction(printerCommandQueueIndex).secondsToEndFromHere / FeedRateRatio);
lastRemainingSecondsReported = (int)(loadedGCode.Instruction(printerCommandQueueLineIndex).secondsToEndFromHere / FeedRateRatio);
}
}
@ -817,7 +817,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
else if (NumberOfLinesInCurrentPrint > 0
&& loadedGCode != null)
{
return loadedGCode.PercentComplete(printerCommandQueueIndex);
return loadedGCode.PercentComplete(printerCommandQueueLineIndex);
}
else
{
@ -855,7 +855,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
if (CommunicationState == CommunicationStates.PrintingFromSd
&& (!timeWaitingForSdProgress.IsRunning || timeWaitingForSdProgress.Elapsed.TotalSeconds > 60))
&& (!timeWaitingForSdProgress.IsRunning || timeWaitingForSdProgress.Elapsed.TotalSeconds > 10))
{
timeWaitingForSdProgress.Restart();
SendLineToPrinterNow("M27"); // : Report SD print status
@ -1889,12 +1889,12 @@ namespace MatterHackers.MatterControl.PrinterCommunication
if (PrinterIsPrinting && CommunicationState != CommunicationStates.PrintingFromSd)
{
// insert the command into the printing queue at the head
if (printerCommandQueueIndex >= 0
&& printerCommandQueueIndex < loadedGCode.Count - 1)
if (printerCommandQueueLineIndex >= 0
&& printerCommandQueueLineIndex < loadedGCode.LineCount - 1)
{
if (!loadedGCode.Instruction(printerCommandQueueIndex + 1).Line.Contains(lineToWrite))
if (!loadedGCode.Instruction(printerCommandQueueLineIndex + 1).Line.Contains(lineToWrite))
{
loadedGCode.Insert(printerCommandQueueIndex + 1, new PrinterMachineInstruction(lineToWrite, loadedGCode.Instruction(printerCommandQueueIndex)));
loadedGCode.Insert(printerCommandQueueLineIndex + 1, new PrinterMachineInstruction(lineToWrite, loadedGCode.Instruction(printerCommandQueueLineIndex)));
}
}
}
@ -2135,7 +2135,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
double MaxTimeToMoveForSentInstructions()
{
double maxTime = 0;
for(int i = Math.Max(0, printerCommandQueueIndex - backupAmount); i < printerCommandQueueIndex; i++)
for(int i = Math.Max(0, printerCommandQueueLineIndex - backupAmount); i < printerCommandQueueLineIndex; i++)
{
maxTime = Math.Max(maxTime, loadedGCode.Instruction(i).secondsThisLine);
}
@ -2154,10 +2154,10 @@ namespace MatterHackers.MatterControl.PrinterCommunication
using (TimedLock.Lock(this, "WriteNextLineFromGCodeFile1"))
{
// we are still sending commands
if (printerCommandQueueIndex > 0 && printerCommandQueueIndex < loadedGCode.Count - 1)
if (printerCommandQueueLineIndex > 0 && printerCommandQueueLineIndex < loadedGCode.LineCount - 1)
{
// the last instruction was a move
PrinterMachineInstruction lastInstruction = loadedGCode.Instruction(printerCommandQueueIndex - 1);
PrinterMachineInstruction lastInstruction = loadedGCode.Instruction(printerCommandQueueLineIndex - 1);
double epectedSecondsToWait = Math.Max(5, MaxTimeToMoveForSentInstructions());
bool wasMoveAndNoOK = (lastInstruction.Line.Contains("G0 ") || lastInstruction.Line.Contains("G1 "))
&& timeHaveBeenWaitingForOK.Elapsed.TotalSeconds > epectedSecondsToWait;
@ -2190,7 +2190,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
bool pauseRequested = false;
using (TimedLock.Lock(this, "WriteNextLineFromGCodeFile2"))
{
if (printerCommandQueueIndex < loadedGCode.Count)
if (printerCommandQueueLineIndex < loadedGCode.LineCount)
{
if (firstLineToResendIndex < allCheckSumLinesSent.Count)
{
@ -2205,7 +2205,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
return;
}
string lineToWrite = loadedGCode.Instruction(printerCommandQueueIndex).Line;
string lineToWrite = loadedGCode.Instruction(printerCommandQueueLineIndex).Line;
string[] splitOnSemicolon = lineToWrite.Split(';');
string trimedLine = splitOnSemicolon[0].Trim().ToUpper();
@ -2222,7 +2222,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
else if (lineToWrite == "M226" || lineToWrite == "@pause")
{
RequestPause(printerCommandQueueIndex + 1);
RequestPause(printerCommandQueueLineIndex + 1);
}
else
{
@ -2231,7 +2231,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
firstLineToResendIndex++;
}
printerCommandQueueIndex++;
printerCommandQueueLineIndex++;
}
}
else if (printWasCanceled)
@ -2244,7 +2244,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
else
{
if (printerCommandQueueIndex == loadedGCode.Count)
if (printerCommandQueueLineIndex == loadedGCode.LineCount)
{
CommunicationState = CommunicationStates.FinishedPrint;
@ -2271,7 +2271,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
{
if (injectionStartIndex == 0)
{
injectionStartIndex = printerCommandQueueIndex;
injectionStartIndex = printerCommandQueueLineIndex;
}
if (PrinterIsPrinting)
@ -2323,7 +2323,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
}
private int InjectGCode(string codeToInject, int indexToStartInjection)
private int InjectGCode(string codeToInject, int lineIndexToStartInjection)
{
codeToInject = GCodeProcessing.ReplaceMacroValues(codeToInject);
@ -2339,9 +2339,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication
{
trimedLine = ReplacePrinterMacros(trimedLine);
if (loadedGCode.Count > indexToStartInjection)
if (loadedGCode.LineCount > lineIndexToStartInjection)
{
loadedGCode.Insert(indexToStartInjection, new PrinterMachineInstruction(trimedLine, loadedGCode.Instruction(indexToStartInjection)));
loadedGCode.Insert(lineIndexToStartInjection, new PrinterMachineInstruction(trimedLine, loadedGCode.Instruction(lineIndexToStartInjection)));
}
else
{
@ -2351,7 +2351,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
}
return indexToStartInjection + linesAdded;
return lineIndexToStartInjection + linesAdded;
}
private string ReplacePrinterMacros(string trimedLine)
@ -2403,7 +2403,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
void ClearQueuedGCode()
{
loadedGCode.Clear();
printerCommandQueueIndex = 0;
printerCommandQueueLineIndex = 0;
lastRemainingSecondsReported = 0;
allCheckSumLinesSent.Clear();
@ -2439,7 +2439,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication
if (cancelGCode.Trim() != "")
{
// add any gcode we want to print while canceling
InjectGCode(cancelGCode, printerCommandQueueIndex);
InjectGCode(cancelGCode, printerCommandQueueLineIndex);
}
// let the process know we canceled not ended normaly.
printWasCanceled = true;

View file

@ -124,7 +124,7 @@ namespace MatterHackers.MatterControl
public void ApplyLeveling(GCodeFile unleveledGCode)
{
for(int i=0; i<unleveledGCode.Count; i++)
for (int i = 0; i < unleveledGCode.LineCount; i++)
{
PrinterMachineInstruction instruction = unleveledGCode.Instruction(i);
Vector3 currentDestination = instruction.Position;