From a29ec6ea4278ff2fa4e203e399b95acadda11cc3 Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Mon, 21 Sep 2020 08:55:25 -0700 Subject: [PATCH] Make sure we dispose delegates correctly Option to turn on run out sensor diagnostics --- .../Settings/PrinterSettings.cs | 1 + .../Settings/SettingsKey.cs | 1 + .../Settings/SliceSettingsFields.cs | 12 ++ MatterControlLib/EeProm/EePromMarlinWindow.cs | 5 + .../EeProm/EePromRepetierWindow.cs | 6 + .../Io/PauseHandlingStream.cs | 134 ++++++++++-------- StaticData/SliceSettings/Layouts.txt | 2 + Submodules/MatterSlice | 2 +- 8 files changed, 101 insertions(+), 62 deletions(-) diff --git a/MatterControl.Printing/Settings/PrinterSettings.cs b/MatterControl.Printing/Settings/PrinterSettings.cs index 047894397..5b6ed7987 100644 --- a/MatterControl.Printing/Settings/PrinterSettings.cs +++ b/MatterControl.Printing/Settings/PrinterSettings.cs @@ -179,6 +179,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration SettingsKey.filament_has_been_loaded, SettingsKey.filament_1_has_been_loaded, SettingsKey.filament_runout_sensor, + SettingsKey.report_runout_sensor_data, SettingsKey.firmware_type, SettingsKey.has_c_axis, SettingsKey.has_fan, diff --git a/MatterControl.Printing/Settings/SettingsKey.cs b/MatterControl.Printing/Settings/SettingsKey.cs index 2ff390f86..cfe67a367 100644 --- a/MatterControl.Printing/Settings/SettingsKey.cs +++ b/MatterControl.Printing/Settings/SettingsKey.cs @@ -100,6 +100,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public const string filament_diameter = nameof(filament_diameter); public const string filament_has_been_loaded = nameof(filament_has_been_loaded); public const string filament_runout_sensor = nameof(filament_runout_sensor); + public const string report_runout_sensor_data = nameof(report_runout_sensor_data); public const string fill_angle = nameof(fill_angle); public const string fill_density = nameof(fill_density); public const string fill_pattern = nameof(fill_pattern); diff --git a/MatterControl.Printing/Settings/SliceSettingsFields.cs b/MatterControl.Printing/Settings/SliceSettingsFields.cs index bdbc46e8f..72276ed64 100644 --- a/MatterControl.Printing/Settings/SliceSettingsFields.cs +++ b/MatterControl.Printing/Settings/SliceSettingsFields.cs @@ -959,6 +959,18 @@ namespace MatterHackers.MatterControl.SlicerConfiguration RebuildGCodeOnChange = false }, new SliceSettingData() + { + SlicerConfigName = SettingsKey.report_runout_sensor_data, + PresentationName = "Report Filament Runout Sensor Data".Localize(), + HelpText = "Report the data analysis of the run out sensor data each time it is read.".Localize(), + DataEditType = DataEditTypes.CHECK_BOX, + ShowAsOverride = true, + ShowIfSet = "!sla_printer&filament_runout_sonsor", + ResetAtEndOfPrint = false, + DefaultValue = "0", + RebuildGCodeOnChange = false + }, + new SliceSettingData() { SlicerConfigName = SettingsKey.probe_has_been_calibrated, PresentationName = "Probe Has Been Calibrated".Localize(), diff --git a/MatterControlLib/EeProm/EePromMarlinWindow.cs b/MatterControlLib/EeProm/EePromMarlinWindow.cs index c2f42e9eb..f56a8c386 100644 --- a/MatterControlLib/EeProm/EePromMarlinWindow.cs +++ b/MatterControlLib/EeProm/EePromMarlinWindow.cs @@ -174,6 +174,11 @@ namespace MatterHackers.MatterControl.EeProm printer.Connection.LineReceived += currentEePromSettings.Add; + this.Closed += (s, e) => + { + printer.Connection.LineReceived -= currentEePromSettings.Add; + }; + // and ask the printer to send the settings currentEePromSettings.Update(); diff --git a/MatterControlLib/EeProm/EePromRepetierWindow.cs b/MatterControlLib/EeProm/EePromRepetierWindow.cs index b54632402..7179e393b 100644 --- a/MatterControlLib/EeProm/EePromRepetierWindow.cs +++ b/MatterControlLib/EeProm/EePromRepetierWindow.cs @@ -191,6 +191,12 @@ namespace MatterHackers.MatterControl.EeProm currentEePromSettings.SettingAdded += NewSettingReadFromPrinter; currentEePromSettings.AskPrinterForSettings(printer.Connection); + this.Closed += (s, e) => + { + printer.Connection.LineReceived -= currentEePromSettings.Add; + currentEePromSettings.SettingAdded -= NewSettingReadFromPrinter; + }; + #if SIMULATE_CONNECTION UiThread.RunOnIdle(AddSimulatedItems); #endif diff --git a/MatterControlLib/PrinterCommunication/Io/PauseHandlingStream.cs b/MatterControlLib/PrinterCommunication/Io/PauseHandlingStream.cs index 12cc65930..77a3080c7 100644 --- a/MatterControlLib/PrinterCommunication/Io/PauseHandlingStream.cs +++ b/MatterControlLib/PrinterCommunication/Io/PauseHandlingStream.cs @@ -61,73 +61,85 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io public override string DebugInfo => ""; + void LineReceived(object sender, string line) + { + if (line != null) + { + if (line.Contains("ros_")) + { + if (line.Contains("TRIGGERED")) + { + readOutOfFilament = true; + } + } + + if (line.Contains("pos_")) + { + double sensorDistance = 0; + double stepperDistance = 0; + if (GCodeFile.GetFirstNumberAfter("SENSOR:", line, ref sensorDistance)) + { + if (sensorDistance < -1 || sensorDistance > 1) + { + printer.Connection.FilamentPositionSensorDetected = true; + } + + if (printer.Connection.FilamentPositionSensorDetected) + { + GCodeFile.GetFirstNumberAfter("STEPPER:", line, ref stepperDistance); + + var stepperDelta = Math.Abs(stepperDistance - positionSensorData.LastStepperDistance); + + // if we think we should have move the filament by more than 1mm + if (stepperDelta > 1) + { + var sensorDelta = Math.Abs(sensorDistance - positionSensorData.LastSensorDistance); + var deltaRatio = sensorDelta / stepperDelta; + + if (printer.Settings.GetValue(SettingsKey.report_runout_sensor_data)) + { + // report the position for debugging + printer.Connection.TerminalLog.WriteLine($"RUNOUT ({positionSensorData.ExtrusionDiscrepency}): Sensor ({sensorDelta:#.0}) / Stepper ({stepperDelta:#.0}) = {deltaRatio:#.00}"); + } + + // check if the sensor data is within a tolerance of the stepper data + if (deltaRatio < .5 || deltaRatio > 2) + { + // we have a discrepancy set a runout state + positionSensorData.ExtrusionDiscrepency++; + if (positionSensorData.ExtrusionDiscrepency > 2) + { + readOutOfFilament = true; + positionSensorData.ExtrusionDiscrepency = 0; + } + } + else + { + positionSensorData.ExtrusionDiscrepency = 0; + } + + // and record this position + positionSensorData.LastSensorDistance = sensorDistance; + positionSensorData.LastStepperDistance = stepperDistance; + } + } + } + } + } + } + + public override void Dispose() + { + printer.Connection.LineReceived -= LineReceived; + } + public PauseHandlingStream(PrinterConfig printer, GCodeStream internalStream) : base(printer, internalStream) { // if we have a runout sensor, register to listen for lines to check it if (printer.Settings.GetValue(SettingsKey.filament_runout_sensor)) { - printer.Connection.LineReceived += (s, line) => - { - if (line != null) - { - if (line.Contains("ros_")) - { - if (line.Contains("TRIGGERED")) - { - readOutOfFilament = true; - } - } - - if (line.Contains("pos_")) - { - double sensorDistance = 0; - double stepperDistance = 0; - if (GCodeFile.GetFirstNumberAfter("SENSOR:", line, ref sensorDistance)) - { - if (sensorDistance < -1 || sensorDistance > 1) - { - printer.Connection.FilamentPositionSensorDetected = true; - } - - if (printer.Connection.FilamentPositionSensorDetected) - { - GCodeFile.GetFirstNumberAfter("STEPPER:", line, ref stepperDistance); - - var stepperDelta = Math.Abs(stepperDistance - positionSensorData.LastStepperDistance); - - // if we think we should have move the filament by more than 1mm - if (stepperDelta > 1) - { - var sensorDelta = Math.Abs(sensorDistance - positionSensorData.LastSensorDistance); - // check if the sensor data is within a tolerance of the stepper data - - var deltaRatio = sensorDelta / stepperDelta; - if (deltaRatio < .5 || deltaRatio > 2) - { - printer.Connection.TerminalLog.WriteLine($"RUNNOUT ({positionSensorData.ExtrusionDiscrepency}): Sensor ({sensorDelta:#.0}) / Stepper ({stepperDelta:#.0}) = {deltaRatio:#.00}"); - // we have a discrepancy set a runout state - positionSensorData.ExtrusionDiscrepency++; - if (positionSensorData.ExtrusionDiscrepency > 2) - { - readOutOfFilament = true; - positionSensorData.ExtrusionDiscrepency = 0; - } - } - else - { - positionSensorData.ExtrusionDiscrepency = 0; - } - - // and record this position - positionSensorData.LastSensorDistance = sensorDistance; - positionSensorData.LastStepperDistance = stepperDistance; - } - } - } - } - } - }; + printer.Connection.LineReceived += LineReceived; } } diff --git a/StaticData/SliceSettings/Layouts.txt b/StaticData/SliceSettings/Layouts.txt index 5acebc8d6..98a3f9066 100644 --- a/StaticData/SliceSettings/Layouts.txt +++ b/StaticData/SliceSettings/Layouts.txt @@ -169,6 +169,8 @@ Printer progress_reporting include_firmware_updater backup_firmware_before_update + Diagnostics + report_runout_sensor_data Hardware firmware_type show_reset_connection diff --git a/Submodules/MatterSlice b/Submodules/MatterSlice index 88e959557..bfa735446 160000 --- a/Submodules/MatterSlice +++ b/Submodules/MatterSlice @@ -1 +1 @@ -Subproject commit 88e95955719ab1dba80cb563a4911637c551e19e +Subproject commit bfa7354469fdf525200ed6f0cefb12ada2c36940