Make sure we dispose delegates correctly

Option to turn on run out sensor diagnostics
This commit is contained in:
Lars Brubaker 2020-09-21 08:55:25 -07:00
parent 46a67928d2
commit a29ec6ea42
8 changed files with 101 additions and 62 deletions

View file

@ -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,

View file

@ -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);

View file

@ -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(),

View file

@ -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();

View file

@ -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

View file

@ -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<bool>(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<bool>(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;
}
}

View file

@ -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

@ -1 +1 @@
Subproject commit 88e95955719ab1dba80cb563a4911637c551e19e
Subproject commit bfa7354469fdf525200ed6f0cefb12ada2c36940