fixed baby step stream to have the right offsets

refactoring for clarity
fixing warnings
issue: MatterHackers/MatterControl#4560
Cannot move up in z calibration wizard
This commit is contained in:
Lars Brubaker 2019-05-22 10:28:27 -07:00
parent bc76fec428
commit 6fbc87acf0
4 changed files with 36 additions and 41 deletions

View file

@ -307,7 +307,7 @@ namespace MatterHackers.MatterControl.Library.Export
if (levelingEnabled if (levelingEnabled
&& !LevelingValidation.NeedsToBeRun(printer)) && !LevelingValidation.NeedsToBeRun(printer))
{ {
accumulatedStream = new PrintLevelingStream(printer, accumulatedStream, false); accumulatedStream = new PrintLevelingStream(printer, accumulatedStream);
} }
if (printer.Settings.GetValue<bool>(SettingsKey.emulate_endstops)) if (printer.Settings.GetValue<bool>(SettingsKey.emulate_endstops))

View file

@ -38,9 +38,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
public class BabyStepsStream : GCodeStreamProxy public class BabyStepsStream : GCodeStreamProxy
{ {
private int extruderIndex = 0; private int extruderIndex = 0;
private Vector3[] extruderOffsets = new Vector3[4]; private readonly Vector3[] extruderOffsets = new Vector3[4];
public PrinterMove lastDestination = PrinterMove.Unknown; private PrinterMove lastInputDestination = PrinterMove.Unknown;
public Vector3 BabbyStepOffset { get; private set; } = Vector3.Zero; public Vector3 BabbyStepOffset { get; private set; } = Vector3.Zero;
@ -56,7 +56,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
ReadExtruderOffsets(); ReadExtruderOffsets();
} }
private void ReadExtruderOffsets() private void ReadExtruderOffsets()
{ {
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
@ -69,36 +68,36 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
{ {
get get
{ {
return $"Last Destination = {lastDestination}"; return $"Last Destination = {lastInputDestination}";
} }
} }
void Printer_SettingChanged(object s, StringEventArgs e) private void Printer_SettingChanged(object s, StringEventArgs e)
{ {
if (e?.Data == SettingsKey.baby_step_z_offset) if (e?.Data == SettingsKey.baby_step_z_offset)
{ {
var currentOffset = BabbyStepOffset.Z; var currentOffset = BabbyStepOffset.Z;
BabbyStepOffset = new Vector3(0, 0, printer.Settings.GetValue<double>(SettingsKey.baby_step_z_offset)); BabbyStepOffset = new Vector3(0, 0, printer.Settings.GetValue<double>(SettingsKey.baby_step_z_offset));
lastDestination.position.Z = lastDestination.position.Z - currentOffset + BabbyStepOffset.Z;
} }
// if the offsets change update them (unless we are actively printing)
else if (e?.Data == SettingsKey.extruder_offset else if (e?.Data == SettingsKey.extruder_offset
&& !printer.Connection.Printing && !printer.Connection.Printing
&& !printer.Connection.Paused) && !printer.Connection.Paused)
{ {
// if the offsets change update them (unless we are actively printing)
ReadExtruderOffsets(); ReadExtruderOffsets();
} }
} }
public override void SetPrinterPosition(PrinterMove position) public override void SetPrinterPosition(PrinterMove position)
{ {
this.lastDestination.CopyKnowSettings(position); lastInputDestination.CopyKnowSettings(position);
if (extruderIndex < 4)
{ // calculate our offset to pass on to internal streams
lastDestination.position -= BabbyStepOffset; var offestDestination = lastInputDestination;
lastDestination.position += extruderOffsets[extruderIndex]; offestDestination.position -= BabbyStepOffset;
} offestDestination.position += extruderOffsets[Math.Min(extruderIndex, 4)];
internalStream.SetPrinterPosition(lastDestination);
internalStream.SetPrinterPosition(offestDestination);
} }
public override void Dispose() public override void Dispose()
@ -131,20 +130,18 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
if (lineToSend != null if (lineToSend != null
&& LineIsMovement(lineToSend)) && LineIsMovement(lineToSend))
{ {
PrinterMove currentMove = GetPosition(lineToSend, lastDestination); PrinterMove currentMove = GetPosition(lineToSend, lastInputDestination);
PrinterMove moveToSend = currentMove; PrinterMove moveToSend = currentMove;
if (extruderIndex < 4) moveToSend.position += BabbyStepOffset;
{ moveToSend.position -= extruderOffsets[Math.Min(extruderIndex, 4)];
moveToSend.position += BabbyStepOffset;
moveToSend.position -= extruderOffsets[extruderIndex];
}
if (moveToSend.HaveAnyPosition) if (moveToSend.HaveAnyPosition)
{ {
lineToSend = CreateMovementLine(moveToSend, lastDestination); lineToSend = CreateMovementLine(moveToSend, lastInputDestination);
} }
lastDestination = currentMove;
lastInputDestination = currentMove;
return lineToSend; return lineToSend;
} }

View file

@ -35,34 +35,31 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
{ {
public class PrintLevelingStream : GCodeStreamProxy public class PrintLevelingStream : GCodeStreamProxy
{ {
private PrinterMove _lastDestination = PrinterMove.Unknown;
private bool activePrinting;
private LevelingFunctions currentLevelingFunctions = null; private LevelingFunctions currentLevelingFunctions = null;
private Vector3 currentProbeZOffset; private Vector3 currentProbeZOffset;
private bool wroteLevelingStatus = false; private bool wroteLevelingStatus = false;
private bool gcodeAlreadyLeveled = false; private bool gcodeAlreadyLeveled = false;
public PrintLevelingStream(PrinterConfig printer, GCodeStream internalStream, bool activePrinting) public PrintLevelingStream(PrinterConfig printer, GCodeStream internalStream)
: base(printer, internalStream) : base(printer, internalStream)
{ {
// always reset this when we construct // always reset this when we construct
AllowLeveling = true; AllowLeveling = true;
this.activePrinting = activePrinting;
} }
public override string DebugInfo public override string DebugInfo
{ {
get get
{ {
return $"Last Destination = {LastDestination}"; return $"Last Destination = {lastUnleveledDestination}";
} }
} }
public bool AllowLeveling { get; set; } public bool AllowLeveling { get; set; }
public PrinterMove LastDestination => _lastDestination; private PrinterMove lastUnleveledDestination = PrinterMove.Unknown;
bool LevelingActive private bool LevelingActive
{ {
get get
{ {
@ -74,7 +71,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
public override string ReadLine() public override string ReadLine()
{ {
if(!wroteLevelingStatus && LevelingActive) if (!wroteLevelingStatus && LevelingActive)
{ {
wroteLevelingStatus = true; wroteLevelingStatus = true;
return "; Software Leveling Applied"; return "; Software Leveling Applied";
@ -99,12 +96,12 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
{ {
if (LineIsMovement(lineToSend)) if (LineIsMovement(lineToSend))
{ {
PrinterMove currentDestination = GetPosition(lineToSend, LastDestination); PrinterMove currentUnleveledDestination = GetPosition(lineToSend, lastUnleveledDestination);
var leveledLine = GetLeveledPosition(lineToSend, currentDestination); var leveledLine = GetLeveledPosition(lineToSend, currentUnleveledDestination);
// TODO: clamp to 0 - baby stepping - extruder z-offset, so we don't go below the bed (for the active extruder) // TODO: clamp to 0 - baby stepping - extruder z-offset, so we don't go below the bed (for the active extruder)
_lastDestination = currentDestination; lastUnleveledDestination = currentUnleveledDestination;
return leveledLine; return leveledLine;
} }
@ -120,6 +117,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
public override void SetPrinterPosition(PrinterMove position) public override void SetPrinterPosition(PrinterMove position)
{ {
this.lastUnleveledDestination.CopyKnowSettings(position);
if (LevelingActive if (LevelingActive
&& position.PositionFullyKnown) && position.PositionFullyKnown)
{ {
@ -129,17 +128,16 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
PrinterMove leveledDestination = GetPosition(leveledPosition, PrinterMove.Unknown); PrinterMove leveledDestination = GetPosition(leveledPosition, PrinterMove.Unknown);
PrinterMove deltaToLeveledPosition = leveledDestination - position; PrinterMove deltaToLeveledPosition = leveledDestination - position;
PrinterMove withoutLevelingOffset = position - deltaToLeveledPosition; PrinterMove withLevelingOffset = position - deltaToLeveledPosition;
_lastDestination = withoutLevelingOffset; // clean up settings that we don't want to be subtracted
_lastDestination.extrusion = position.extrusion; withLevelingOffset.extrusion = position.extrusion;
_lastDestination.feedRate = position.feedRate; withLevelingOffset.feedRate = position.feedRate;
internalStream.SetPrinterPosition(_lastDestination); internalStream.SetPrinterPosition(withLevelingOffset);
} }
else else
{ {
this._lastDestination.CopyKnowSettings(position);
internalStream.SetPrinterPosition(position); internalStream.SetPrinterPosition(position);
} }
} }

View file

@ -2318,7 +2318,7 @@ You will then need to logout and log back in to the computer for the changes to
if (!LevelingValidation.NeedsToBeRun(Printer)) if (!LevelingValidation.NeedsToBeRun(Printer))
{ {
accumulatedStream = printLevelingStream = new PrintLevelingStream(Printer, accumulatedStream, true); accumulatedStream = printLevelingStream = new PrintLevelingStream(Printer, accumulatedStream);
} }
accumulatedStream = waitForTempStream = new WaitForTempStream(Printer, accumulatedStream); accumulatedStream = waitForTempStream = new WaitForTempStream(Printer, accumulatedStream);