2014-02-05 18:29:58 -08:00
/ *
2014-02-15 18:06:03 -08:00
Copyright ( c ) 2014 , Lars Brubaker
2014-01-29 19:09:30 -08:00
All rights reserved .
Redistribution and use in source and binary forms , with or without
2015-04-08 15:20:10 -07:00
modification , are permitted provided that the following conditions are met :
2014-01-29 19:09:30 -08:00
1. Redistributions of source code must retain the above copyright notice , this
2015-04-08 15:20:10 -07:00
list of conditions and the following disclaimer .
2014-01-29 19:09:30 -08:00
2. Redistributions in binary form must reproduce the above copyright notice ,
this list of conditions and the following disclaimer in the documentation
2015-04-08 15:20:10 -07:00
and / or other materials provided with the distribution .
2014-01-29 19:09:30 -08:00
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
2015-04-08 15:20:10 -07:00
of the authors and should not be interpreted as representing official policies ,
2014-01-29 19:09:30 -08:00
either expressed or implied , of the FreeBSD Project .
* /
using MatterHackers.Agg ;
using MatterHackers.Agg.UI ;
2014-02-05 18:29:58 -08:00
using MatterHackers.Localizations ;
2014-06-11 14:52:58 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2015-08-03 15:48:36 -07:00
using MatterHackers.MatterControl.SlicerConfiguration ;
2015-04-08 15:20:10 -07:00
using MatterHackers.VectorMath ;
using System ;
2016-10-11 14:56:36 -07:00
using System.Collections.Generic ;
2014-01-29 19:09:30 -08:00
2014-06-06 16:05:18 -07:00
namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling
2014-01-29 19:09:30 -08:00
{
2015-04-08 15:20:10 -07:00
public class FirstPageInstructions : InstructionsPage
{
public FirstPageInstructions ( string pageDescription , string instructionsText )
: base ( pageDescription , instructionsText )
{
}
}
public class LastPage3PointInstructions : InstructionsPage
{
2016-10-11 14:56:36 -07:00
private List < ProbePosition > probePositions = new List < ProbePosition > ( 3 )
{
new ProbePosition ( ) , new ProbePosition ( ) , new ProbePosition ( )
} ;
2015-04-08 15:20:10 -07:00
2016-10-11 14:56:36 -07:00
public LastPage3PointInstructions ( string pageDescription , string instructionsText , List < ProbePosition > probePositions )
2015-04-08 15:20:10 -07:00
: base ( pageDescription , instructionsText )
{
this . probePositions = probePositions ;
}
public override void PageIsBecomingActive ( )
{
2016-06-16 06:58:28 -07:00
Vector3 paperWidth = new Vector3 ( 0 , 0 , ActiveSliceSettings . Instance . GetValue < double > ( "manual_probe_paper_width" ) ) ;
2016-04-18 11:31:31 -07:00
2016-07-18 14:15:37 -07:00
PrintLevelingData levelingData = ActiveSliceSettings . Instance . Helpers . GetPrintLevelingData ( ) ;
2016-10-11 14:56:36 -07:00
levelingData . SampledPositions . Clear ( ) ;
levelingData . SampledPositions . Add ( probePositions [ 0 ] . position - paperWidth ) ;
levelingData . SampledPositions . Add ( probePositions [ 1 ] . position - paperWidth ) ;
levelingData . SampledPositions . Add ( probePositions [ 2 ] . position - paperWidth ) ;
2015-08-01 14:44:53 -07:00
2016-04-18 11:31:31 -07:00
// Invoke setter forcing persistence of leveling data
2016-07-18 14:15:37 -07:00
ActiveSliceSettings . Instance . Helpers . SetPrintLevelingData ( levelingData ) ;
2016-04-18 11:31:31 -07:00
2016-07-18 14:15:37 -07:00
ActiveSliceSettings . Instance . Helpers . DoPrintLeveling ( true ) ;
2016-04-18 11:31:31 -07:00
2015-08-01 14:44:53 -07:00
base . PageIsBecomingActive ( ) ;
}
}
2015-08-05 11:12:01 -07:00
public class LastPageRadialInstructions : InstructionsPage
2015-08-01 14:44:53 -07:00
{
2016-10-11 14:56:36 -07:00
private List < ProbePosition > probePositions ;
2015-08-01 14:44:53 -07:00
2016-10-11 14:56:36 -07:00
public LastPageRadialInstructions ( string pageDescription , string instructionsText , List < ProbePosition > probePositions )
2015-08-01 14:44:53 -07:00
: base ( pageDescription , instructionsText )
{
this . probePositions = probePositions ;
}
public override void PageIsBecomingActive ( )
{
2016-07-18 14:15:37 -07:00
PrintLevelingData levelingData = ActiveSliceSettings . Instance . Helpers . GetPrintLevelingData ( ) ;
2016-10-13 14:18:30 -07:00
levelingData . SampledPositions . Clear ( ) ;
2016-10-11 14:56:36 -07:00
2016-06-16 06:58:28 -07:00
Vector3 paperWidth = new Vector3 ( 0 , 0 , ActiveSliceSettings . Instance . GetValue < double > ( "manual_probe_paper_width" ) ) ;
2016-10-11 14:56:36 -07:00
for ( int i = 0 ; i < probePositions . Count ; i + + )
2015-08-01 14:44:53 -07:00
{
levelingData . SampledPositions . Add ( probePositions [ i ] . position - paperWidth ) ;
}
2015-04-08 15:20:10 -07:00
2016-04-18 11:31:31 -07:00
// Invoke setter forcing persistence of leveling data
2016-07-18 14:15:37 -07:00
ActiveSliceSettings . Instance . Helpers . SetPrintLevelingData ( levelingData ) ;
2015-08-01 16:58:25 -07:00
2016-07-18 14:15:37 -07:00
ActiveSliceSettings . Instance . Helpers . DoPrintLeveling ( true ) ;
2015-04-08 15:20:10 -07:00
base . PageIsBecomingActive ( ) ;
}
}
public class GettingThirdPointFor2PointCalibration : InstructionsPage
{
protected Vector3 probeStartPosition ;
private ProbePosition probePosition ;
protected WizardControl container ;
public GettingThirdPointFor2PointCalibration ( WizardControl container , string pageDescription , Vector3 probeStartPosition , string instructionsText , ProbePosition probePosition )
: base ( pageDescription , instructionsText )
{
this . probeStartPosition = probeStartPosition ;
this . probePosition = probePosition ;
this . container = container ;
}
private event EventHandler unregisterEvents ;
public override void OnClosed ( EventArgs e )
{
2016-04-18 11:31:31 -07:00
unregisterEvents ? . Invoke ( this , null ) ;
2015-04-08 15:20:10 -07:00
base . OnClosed ( e ) ;
}
public override void PageIsBecomingActive ( )
{
// first make sure there is no leftover FinishedProbe event
PrinterConnectionAndCommunication . Instance . ReadLine . UnregisterEvent ( FinishedProbe , ref unregisterEvents ) ;
2016-07-18 14:15:37 -07:00
var feedRates = ActiveSliceSettings . Instance . Helpers . ManualMovementSpeeds ( ) ;
2016-06-17 09:29:11 -07:00
PrinterConnectionAndCommunication . Instance . MoveAbsolute ( PrinterConnectionAndCommunication . Axis . Z , probeStartPosition . z , feedRates . z ) ;
PrinterConnectionAndCommunication . Instance . MoveAbsolute ( probeStartPosition , feedRates . x ) ;
2015-04-08 15:20:10 -07:00
PrinterConnectionAndCommunication . Instance . SendLineToPrinterNow ( "G30" ) ;
PrinterConnectionAndCommunication . Instance . ReadLine . RegisterEvent ( FinishedProbe , ref unregisterEvents ) ;
base . PageIsBecomingActive ( ) ;
container . nextButton . Enabled = false ;
container . backButton . Enabled = false ;
}
private void FinishedProbe ( object sender , EventArgs e )
{
StringEventArgs currentEvent = e as StringEventArgs ;
if ( currentEvent ! = null )
{
if ( currentEvent . Data . Contains ( "endstops hit" ) )
{
PrinterConnectionAndCommunication . Instance . ReadLine . UnregisterEvent ( FinishedProbe , ref unregisterEvents ) ;
int zStringPos = currentEvent . Data . LastIndexOf ( "Z:" ) ;
string zProbeHeight = currentEvent . Data . Substring ( zStringPos + 2 ) ;
probePosition . position = new Vector3 ( probeStartPosition . x , probeStartPosition . y , double . Parse ( zProbeHeight ) ) ;
2016-07-18 14:15:37 -07:00
PrinterConnectionAndCommunication . Instance . MoveAbsolute ( probeStartPosition , ActiveSliceSettings . Instance . Helpers . ManualMovementSpeeds ( ) . z ) ;
2015-04-08 15:20:10 -07:00
PrinterConnectionAndCommunication . Instance . ReadPosition ( ) ;
container . nextButton . ClickButton ( new MouseEventArgs ( MouseButtons . Left , 1 , 0 , 0 , 0 ) ) ;
}
}
}
}
public class HomePrinterPage : InstructionsPage
{
public HomePrinterPage ( string pageDescription , string instructionsText )
: base ( pageDescription , instructionsText )
{
}
public override void PageIsBecomingActive ( )
{
PrinterConnectionAndCommunication . Instance . HomeAxis ( PrinterConnectionAndCommunication . Axis . XYZ ) ;
base . PageIsBecomingActive ( ) ;
}
}
public class FindBedHeight : InstructionsPage
{
private Vector3 lastReportedPosition ;
2016-10-11 14:56:36 -07:00
private List < ProbePosition > probePositions ;
int probePositionsBeingEditedIndex ;
2015-04-08 15:20:10 -07:00
private double moveAmount ;
protected JogControls . MoveButton zPlusControl ;
protected JogControls . MoveButton zMinusControl ;
2016-10-11 14:56:36 -07:00
public FindBedHeight ( string pageDescription , string setZHeightCoarseInstruction1 , string setZHeightCoarseInstruction2 , double moveDistance , List < ProbePosition > probePositions , int probePositionsBeingEditedIndex )
2015-04-08 15:20:10 -07:00
: base ( pageDescription , setZHeightCoarseInstruction1 )
{
2016-10-11 14:56:36 -07:00
this . probePositions = probePositions ;
2015-04-08 15:20:10 -07:00
this . moveAmount = moveDistance ;
this . lastReportedPosition = PrinterConnectionAndCommunication . Instance . LastReportedPosition ;
2016-10-11 14:56:36 -07:00
this . probePositionsBeingEditedIndex = probePositionsBeingEditedIndex ;
2015-04-08 15:20:10 -07:00
GuiWidget spacer = new GuiWidget ( 15 , 15 ) ;
topToBottomControls . AddChild ( spacer ) ;
FlowLayoutWidget zButtonsAndInfo = new FlowLayoutWidget ( ) ;
zButtonsAndInfo . HAnchor | = Agg . UI . HAnchor . ParentCenter ;
FlowLayoutWidget zButtons = CreateZButtons ( ) ;
zButtonsAndInfo . AddChild ( zButtons ) ;
zButtonsAndInfo . AddChild ( new GuiWidget ( 15 , 10 ) ) ;
FlowLayoutWidget textFields = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
zButtonsAndInfo . AddChild ( textFields ) ;
topToBottomControls . AddChild ( zButtonsAndInfo ) ;
AddTextField ( setZHeightCoarseInstruction2 , 10 ) ;
}
private event EventHandler unregisterEvents ;
public override void OnClosed ( EventArgs e )
{
if ( unregisterEvents ! = null )
{
unregisterEvents ( this , null ) ;
}
base . OnClosed ( e ) ;
}
public override void PageIsBecomingActive ( )
{
// always make sure we don't have print leveling turned on
2016-07-18 14:15:37 -07:00
ActiveSliceSettings . Instance . Helpers . DoPrintLeveling ( false ) ;
2015-04-08 15:20:10 -07:00
base . PageIsBecomingActive ( ) ;
}
public override void PageIsBecomingInactive ( )
{
2016-10-11 14:56:36 -07:00
probePositions [ probePositionsBeingEditedIndex ] . position = PrinterConnectionAndCommunication . Instance . LastReportedPosition ;
2015-04-08 15:20:10 -07:00
base . PageIsBecomingInactive ( ) ;
}
private FlowLayoutWidget CreateZButtons ( )
{
2015-10-19 15:59:42 -07:00
FlowLayoutWidget zButtons = JogControls . CreateZButtons ( RGBA_Bytes . White , 4 , out zPlusControl , out zMinusControl , true ) ;
2015-04-08 15:20:10 -07:00
// set these to 0 so the button does not do any movements by default (we will handle the movement on our click callback)
zPlusControl . MoveAmount = 0 ;
zMinusControl . MoveAmount = 0 ;
zPlusControl . Click + = new EventHandler ( zPlusControl_Click ) ;
zMinusControl . Click + = new EventHandler ( zMinusControl_Click ) ;
return zButtons ;
}
private void zMinusControl_Click ( object sender , EventArgs mouseEvent )
{
2016-10-10 19:30:48 -07:00
double newPosition = PrinterConnectionAndCommunication . Instance . LastReportedPosition . z - moveAmount ;
bool moveBelow0 = newPosition < 0 ;
if ( moveBelow0 )
2015-04-08 15:20:10 -07:00
{
2016-10-10 19:30:48 -07:00
// increment the z_offset_after_home
2016-10-11 14:56:36 -07:00
double zOffset = ActiveSliceSettings . Instance . GetValue < double > ( SettingsKey . z_offset_after_home ) ;
zOffset + = 1 ;
ActiveSliceSettings . Instance . SetValue ( SettingsKey . z_offset_after_home , zOffset . ToString ( ) ) ;
2016-10-10 19:30:48 -07:00
// adjust all previously sampled points
2016-10-11 14:56:36 -07:00
for ( int i = 0 ; i < probePositions . Count ; i + + )
{
probePositions [ i ] . position = probePositions [ i ] . position + new Vector3 ( 0 , 0 , 1 ) ;
}
2016-10-10 19:30:48 -07:00
// send a G92 z position to the printer to adjust the current z height
2016-10-11 14:56:36 -07:00
PrinterConnectionAndCommunication . Instance . SendLineToPrinterNow ( $"G92 Z{PrinterConnectionAndCommunication.Instance.CurrentDestination.z + 1}" ) ;
2015-04-08 15:20:10 -07:00
}
2016-10-10 19:30:48 -07:00
2016-07-18 14:15:37 -07:00
PrinterConnectionAndCommunication . Instance . MoveRelative ( PrinterConnectionAndCommunication . Axis . Z , - moveAmount , ActiveSliceSettings . Instance . Helpers . ManualMovementSpeeds ( ) . z ) ;
2015-04-08 15:20:10 -07:00
PrinterConnectionAndCommunication . Instance . ReadPosition ( ) ;
}
private void zPlusControl_Click ( object sender , EventArgs mouseEvent )
{
2016-07-18 14:15:37 -07:00
PrinterConnectionAndCommunication . Instance . MoveRelative ( PrinterConnectionAndCommunication . Axis . Z , moveAmount , ActiveSliceSettings . Instance . Helpers . ManualMovementSpeeds ( ) . z ) ;
2015-04-08 15:20:10 -07:00
PrinterConnectionAndCommunication . Instance . ReadPosition ( ) ;
}
}
public class GetCoarseBedHeight : FindBedHeight
{
private static string setZHeightCoarseInstruction1 = LocalizedString . Get ( "Using the [Z] controls on this screen, we will now take a coarse measurement of the extruder height at this position." ) ;
private static string setZHeightCourseInstructTextOne = "Place the paper under the extruder" . Localize ( ) ;
2015-07-31 12:22:32 -07:00
private static string setZHeightCourseInstructTextTwo = "Using the above controls" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
private static string setZHeightCourseInstructTextThree = LocalizedString . Get ( "Press [Z-] until there is resistance to moving the paper" ) ;
private static string setZHeightCourseInstructTextFour = LocalizedString . Get ( "Press [Z+] once to release the paper" ) ;
private static string setZHeightCourseInstructTextFive = LocalizedString . Get ( "Finally click 'Next' to continue." ) ;
private static string setZHeightCoarseInstruction2 = string . Format ( "\t• {0}\n\t• {1}\n\t• {2}\n\t• {3}\n\n{4}" , setZHeightCourseInstructTextOne , setZHeightCourseInstructTextTwo , setZHeightCourseInstructTextThree , setZHeightCourseInstructTextFour , setZHeightCourseInstructTextFive ) ;
protected Vector3 probeStartPosition ;
protected WizardControl container ;
2016-10-11 14:56:36 -07:00
public GetCoarseBedHeight ( WizardControl container , Vector3 probeStartPosition , string pageDescription , List < ProbePosition > probePositions , int probePositionsBeingEditedIndex )
: base ( pageDescription , setZHeightCoarseInstruction1 , setZHeightCoarseInstruction2 , 1 , probePositions , probePositionsBeingEditedIndex )
2015-04-08 15:20:10 -07:00
{
this . container = container ;
this . probeStartPosition = probeStartPosition ;
}
public override void PageIsBecomingActive ( )
{
base . PageIsBecomingActive ( ) ;
2016-07-18 14:15:37 -07:00
var feedRates = ActiveSliceSettings . Instance . Helpers . ManualMovementSpeeds ( ) ;
2016-06-17 09:29:11 -07:00
PrinterConnectionAndCommunication . Instance . MoveAbsolute ( PrinterConnectionAndCommunication . Axis . Z , probeStartPosition . z , feedRates . z ) ;
PrinterConnectionAndCommunication . Instance . MoveAbsolute ( probeStartPosition , feedRates . x ) ;
2015-04-08 15:20:10 -07:00
PrinterConnectionAndCommunication . Instance . ReadPosition ( ) ;
2015-09-01 17:08:59 -07:00
container . backButton . Enabled = false ;
2015-04-08 15:20:10 -07:00
container . nextButton . Enabled = false ;
zPlusControl . Click + = new EventHandler ( zControl_Click ) ;
zMinusControl . Click + = new EventHandler ( zControl_Click ) ;
}
protected void zControl_Click ( object sender , EventArgs mouseEvent )
{
container . nextButton . Enabled = true ;
}
public override void PageIsBecomingInactive ( )
{
2015-09-01 17:08:59 -07:00
container . backButton . Enabled = true ;
2015-04-08 15:20:10 -07:00
container . nextButton . Enabled = true ;
}
}
public class GetFineBedHeight : FindBedHeight
{
private static string setZHeightFineInstruction1 = LocalizedString . Get ( "We will now refine our measurement of the extruder height at this position." ) ;
private static string setZHeightFineInstructionTextOne = LocalizedString . Get ( "Press [Z-] until there is resistance to moving the paper" ) ;
private static string setZHeightFineInstructionTextTwo = LocalizedString . Get ( "Press [Z+] once to release the paper" ) ;
private static string setZHeightFineInstructionTextThree = LocalizedString . Get ( "Finally click 'Next' to continue." ) ;
private static string setZHeightFineInstruction2 = string . Format ( "\t• {0}\n\t• {1}\n\n{2}" , setZHeightFineInstructionTextOne , setZHeightFineInstructionTextTwo , setZHeightFineInstructionTextThree ) ;
2016-10-11 14:56:36 -07:00
public GetFineBedHeight ( string pageDescription , List < ProbePosition > probePositions , int probePositionsBeingEditedIndex )
: base ( pageDescription , setZHeightFineInstruction1 , setZHeightFineInstruction2 , . 1 , probePositions , probePositionsBeingEditedIndex )
2015-04-08 15:20:10 -07:00
{
}
}
public class GetUltraFineBedHeight : FindBedHeight
{
private static string setZHeightFineInstruction1 = LocalizedString . Get ( "We will now finalize our measurement of the extruder height at this position." ) ;
private static string setHeightFineInstructionTextOne = LocalizedString . Get ( "Press [Z-] one click PAST the first hint of resistance" ) ;
private static string setHeightFineInstructionTextTwo = LocalizedString . Get ( "Finally click 'Next' to continue." ) ;
private static string setZHeightFineInstruction2 = string . Format ( "\t• {0}\n\n\n{1}" , setHeightFineInstructionTextOne , setHeightFineInstructionTextTwo ) ;
2016-10-11 14:56:36 -07:00
public GetUltraFineBedHeight ( string pageDescription , List < ProbePosition > probePositions , int probePositionsBeingEditedIndex )
: base ( pageDescription , setZHeightFineInstruction1 , setZHeightFineInstruction2 , . 02 , probePositions , probePositionsBeingEditedIndex )
2015-04-08 15:20:10 -07:00
{
}
private bool haveDrawn = false ;
public override void OnDraw ( Graphics2D graphics2D )
{
haveDrawn = true ;
base . OnDraw ( graphics2D ) ;
}
public override void PageIsBecomingInactive ( )
{
if ( haveDrawn )
{
2016-07-18 14:15:37 -07:00
PrinterConnectionAndCommunication . Instance . MoveRelative ( PrinterConnectionAndCommunication . Axis . Z , 2 , ActiveSliceSettings . Instance . Helpers . ManualMovementSpeeds ( ) . z ) ;
2015-04-08 15:20:10 -07:00
}
base . PageIsBecomingInactive ( ) ;
}
}
}