'Name: SEW_Test_4.txt                  Date: 02 Set 2021

Load "Theremino_MicroVideo\Theremino_MicroVideo.exe"
Load "Theremino_HAL\Theremino_Hal.exe"
Load "Theremino_HAL\Theremino_SlotViewer.exe"
Load "Theremino_HAL\Theremino_SignalScope2.exe"

Button 1 Text Home_All
Button Home_All Text "Home ALL"
Button Home_All Color LightBlue

Button 2 Text Home_X
Button Home_X Text "Home X"
Button Home_X Color LightBlue

Button 3 Text Home_Y
Button Home_Y Text "Home Y"
Button Home_Y Color LightBlue

Button 4 Text Home_Z
Button Home_Z Text "Home Z"
Button Home_Z Color LightBlue

Button 6 Text SquareTest
Button 7 Text CircleTest
Button 8 Text CircleZigZag
Button 9 Text ZigZag

Button 11 Text Alva_test
Button Alva_test Text "Alva Test"
Button Alva_test Color Pink

Button 12 Text PunchLimits

Button 13 Text EnableMotors
Button EnableMotors Color Tan
Button 14 Text DisableMotors
Button DisableMotors Color Tan

Button 16 Text AbortSewing

Variable Numeric Slot_MotorX = 1
Variable Numeric Slot_MotorY = 3
Variable Numeric Slot_MotorZ = 5

Variable Numeric Slot_SensorX		=  19
Variable Numeric Slot_SensorY		=  20
Variable Numeric Slot_SensorZ		=  21
Variable Numeric Slot_EnableMotors	=  10
Variable Numeric Slot_SignalScope   =  25

Variable Numeric StopAngle = 90
Variable Numeric StartAngle = 270

Variable Numeric Xmax = 900
Variable Numeric Xmin = 0
Variable Numeric Ymax = 900
Variable Numeric Ymin = 0
Variable Numeric MaxPitch = 4
Variable Numeric MaxPerSec = 20

SewInit Slot_MotorX Slot_MotorY Slot_MotorZ StopAngle StartAngle

SewLimits Xmin Xmax Ymin Ymax MaxPerSec

Gosub Home_All

Stop                                                                   '
' ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

'=====================================================================
'  SIGNAL SCOPE
' ====================================================================
Label SignalScope_ON
	Slot Slot_SignalScope = 1000
Return

Label SignalScope_OFF
	Slot Slot_SignalScope = 0
Return


' ====================================================================
' ENABLE/DISABLE MOTORS
' ====================================================================
Label EnableMotors
	Slot(Slot_EnableMotors) = 0
Return

Label DisableMotors
	Gosub WaitAllMotors
	Slot(Slot_EnableMotors) = 1000
Return

' ====================================================================
'  HOME  X Y Z
' ====================================================================
Label Home_All
	Gosub EnableMotors
	Gosub Home_Z
	Gosub Home_X
	Gosub Home_Y
Return

Label Home_X
	Gosub EnableMotors
	SlotMotor = Slot_MotorX
	SlotSensor = Slot_SensorX
	SpeedSearch = -0.3
	SpeedReturn = +0.05
	DeclaredSwitchPos = 0
	InitialPosition = 1
	Gosub HomeMotor
Return

Label Home_Y
	Gosub EnableMotors
	SlotMotor = Slot_MotorY
	SlotSensor = Slot_SensorY
	SpeedSearch = -0.1
	SpeedReturn = +0.05
	DeclaredSwitchPos = 0
	InitialPosition = 1
	Gosub HomeMotor
Return

Label Home_Z
	Gosub EnableMotors
	SlotMotor = Slot_MotorZ
	SlotSensor = Slot_SensorZ
	SpeedSearch = +0.005
	SpeedReturn = -0.0001
	DeclaredSwitchPos = 0
	InitialPosition = 0
	Gosub HomeMotor
Return

' ====================================================================
'  Home the Motor(SlotMotor) to the Sensor(SlotSensor)
' ====================================================================
Variable Numeric SlotMotor
Variable Numeric SlotSensor
Variable Numeric SpeedSearch
Variable Numeric SpeedReturn
Variable Numeric DeclaredSwitchPos
Variable Numeric InitialPosition

Label HomeMotor
	' ------------------------- detach if sensor is active
	If Slot(SlotSensor) > 500 Or Key("CTRL")
		Slot(SlotMotor) = Slot(SlotMotor) + SpeedReturn
	EndIf
    ' ------------------------- search the sensor
Label SearchHome
	Slot(SlotMotor) = Slot(SlotMotor) + SpeedSearch
	Wait Seconds 0.001
	If Slot(SlotSensor) > 500 Or Key("CTRL")
		Slot(SlotMotor) = Reset
		Wait Seconds 0.1
		Slot(SlotMotor) = 0
		Wait Seconds 0.1
		Goto DetachSensor
	EndIf
Goto SearchHome
    ' ------------------------- detach from the sensor
Label DetachSensor
	Slot(SlotMotor) = Slot(SlotMotor) + SpeedReturn
	Wait Seconds 0.001
	If Slot (SlotSensor) < 500
		Slot(SlotMotor) = Reset
		Wait Seconds 0.1
		Slot(SlotMotor) = DeclaredSwitchPos
		Wait Seconds 0.1
		Goto SetFinalValues
	EndIf
Goto DetachSensor
    ' ------------------------- reset and move to InitialPos
Label SetFinalValues
	Slot(SlotMotor) = InitialPosition
	Gosub WaitMotor
	'Gosub EnableAllButtons
	'Button RunSequence Disabled
Return

' ====================================================================
'   Wait until the motor(SlotMotor) is stopped
' ====================================================================
Variable Numeric WaitMot1
Label WaitMotor
	Wait Seconds 0.1
	For WaitMot1 = 1 To 9e99
		If Abs(Slot(SlotMotor + 1)) < 0.01
			Exit
		EndIf
		Wait Seconds 0.005
	Next
Return


' ====================================================================
'   Wait until all motors are stopped
' ====================================================================
Label WaitAllMotors
	Wait Seconds 0.05
	Variable Numeric WM1
	For WM1 = 1 To 9e99
		If Abs(Slot(Slot_MotorX + 1)) < 0.01
			If Abs(Slot(Slot_MotorY + 1)) < 0.01
				If Abs(Slot(Slot_MotorZ + 1)) < 0.01
					Exit
				EndIf
			EndIf
		EndIf
		Wait Seconds 0.001
	Next
Return

' ===============================================================
'  SEWING FUNCTIONS
' ===============================================================
Label WaitSewing
	For v1 = 1 To Infinity
		If  SewProgress < 0
			Exit
		Else
			Print Int(SewProgress * 100) + " %"
		EndIf
	Next
Return

Label AbortSewing
	SewAbort
Stop


Label WaitSewProgress
	Variable Numeric wsp1
	For wsp1 = 1 To Infinity
		If SewProgress < 0
			wsp1 = SewProgress
			Exit
		EndIf
		Print Int(Abs(SewProgress) * 100) + "%"
		'Print Mod(Slot(Slot_MotorZ),1)
	Next
Return


' ===============================================================
'  SEQUENCE  - SQUARE
' ===============================================================
Label SquareTest
	Gosub SignalScope_ON
	'       x    y  mm/s st/s   flags
	SewTo  10   10  100    0    ""            ' jump to begin the square
	Gosub WaitSewProgress
	SewTo  10   44   20    3    ""            ' left vertical
	Gosub WaitSewProgress
	SewTo  50   50   20    3    ""            ' top horizontal
	Gosub WaitSewProgress
	SewTo  50   10   20    3    ""            ' right vertical
	Gosub WaitSewProgress
	SewTo  10   10   20    3    ""            ' low horizontal
	Gosub WaitSewProgress
	
	SewTo  50   10  100    0    ""            ' jump to edge
	Gosub WaitSewProgress
	SewTo  10   50   20    3    ""            ' diagonal
	Gosub WaitSewProgress
	
	SewTo  50   50  100    0    ""            ' jump to edge
	Gosub WaitSewProgress
	SewTo  10   10   20    3    ""            ' diagonal
	Gosub WaitSewProgress
	
	SewTo  90   10  100    0    ""            ' jump to begin the square
	Gosub WaitSewProgress
	SewTo  90   50   20    3    ""            ' left vertical
	Gosub WaitSewProgress
	SewTo 130   50   20    3    ""            ' top horizontal
	Gosub WaitSewProgress
	SewTo 130   10   20    3    ""            ' right vertical
	Gosub WaitSewProgress
	SewTo  90   10   20    3    ""            ' low horizontal
	Gosub WaitSewProgress
	
	SewTo 130   50  100    0    ""            ' jump to edge
	Gosub WaitSewProgress
	SewTo  90   10   20    3    ""            ' diagonal
	Gosub WaitSewProgress
	
	SewTo  90   50  100    0    ""            ' jump to edge
	Gosub WaitSewProgress
	SewTo 130   10   20    3    ""            ' diagonal
	Gosub WaitSewProgress
	
	
	SewTo  70   30  100    0    ""            ' jump to center circle
	Gosub WaitSewProgress
	SewTo  70   30   20    3    "Radius=12"   ' Circle
	Gosub WaitSewProgress
	
	SewTo   1    1  100    0    ""            ' jump to home
	Gosub WaitSewProgress
	Gosub SignalScope_OFF
Return

Label CircleTest
	SewTo 1 1 500 0 ""
	Gosub WaitSewProgress
	
	SewTo 80 30 100  0   ""
	Gosub WaitSewProgress
	
	v1 = ElapsedTime
	SewTo 80 30  10  3   "Radius=15.92"
	Gosub WaitSewProgress
	v2 = ElapsedTime
	
	SewTo 1 1 500 0 ""
	Gosub WaitSewProgress
	
	Print Format(v2 - v1, "0.000") + " sec."
Return

Label CircleZigZag
	SewTo 1 1 500 0 ""
	Gosub WaitSewProgress
	
	SewTo 50 30 100  0   ""
	Gosub WaitSewProgress
	
	v1 = ElapsedTime
	SewTo 50 30  10  2   "Radius=15.92 zigzag=2"
	Gosub WaitSewProgress
	v2 = ElapsedTime
	
	SewTo 1 1 500 0 ""
	Gosub WaitSewProgress
	
	Print Format(v2 - v1, "0.000") + " sec."
Return

Label ZigZag
	'       x    y  mm/s st/s   flags
	SewTo  10   10  100    0   ""            ' jump to begin the square
	Gosub WaitSewProgress
	
	v1 = ElapsedTime
	SewTo  10   50   20    8   ""    ' left vertical
	Gosub WaitSewProgress
	SewTo  50   50   20    6   ""    ' top horizontal
	Gosub WaitSewProgress
	SewTo  50   10   20    4   ""    ' right vertical
	Gosub WaitSewProgress
	SewTo  10   10   20    2   ""    ' low horizontal
	Gosub WaitSewProgress
	v2 = ElapsedTime
	
	'SewTo  10   50   20    3   "zigzag=2"    ' left vertical
	'Gosub WaitSewProgress
	'SewTo  50   50   20    3   "zigzag=2"    ' top horizontal
	'Gosub WaitSewProgress
	'SewTo  50   10   20    3   "zigzag=2"    ' right vertical
	'Gosub WaitSewProgress
	'SewTo  10   10   20    3   "zigzag=2"    ' low horizontal
	'Gosub WaitSewProgress
	
	SewTo   1    1  500    0   ""			 ' jump to home
	Gosub WaitSewProgress
	
	Print Format(v2 - v1, "0.000") + " sec."
Return

Label Alva_test
	'       x    y  mm/s st/s   flags
	SewTo 105   15  100    0   ""    ' jump to begin the square
	Gosub WaitSewProgress
	
	'square
	SewTo 105   55   20    6   ""    ' left vertical
	Gosub WaitSewProgress
	SewTo 145   55   20    5   ""    ' top horizontal
	Gosub WaitSewProgress
	SewTo 145   15   20    4   ""    ' right vertical
	Gosub WaitSewProgress
	SewTo 105   15   20    3   ""    ' low horizontal
	Gosub WaitSewProgress
	
	'triangle
	'       x    y  mm/s point   flags
	SewTo  10   10  100    0   ""     ' jump to begin the square
	Gosub WaitSewProgress
	
    ' triangle
	SewTo  30   20   20    5   ""
	Gosub WaitSewProgress
	SewTo  50   10   20    5   ""
	Gosub WaitSewProgress
	SewTo  10   10   20    5   ""
	Gosub WaitSewProgress
	
	'1st arrow
	SewTo  10   10  100    0   ""     ' jump to begin the square
	Gosub WaitSewProgress
	SewTo  30   30   20    5   ""
	Gosub WaitSewProgress
	SewTo  50   10   20    5   ""
	Gosub WaitSewProgress
	
	'2nd arrow
	SewTo  10   10  100    0   ""     ' jump to begin the square
	Gosub WaitSewProgress
	SewTo  30   40   20    5   ""
	Gosub WaitSewProgress
	SewTo  50   10   20    5   ""
	Gosub WaitSewProgress
	
	' 3rd arrow
	SewTo  10   10  100    0   ""     ' jump to begin the square
	Gosub WaitSewProgress
	SewTo  30   50   20    5   ""
	Gosub WaitSewProgress
	SewTo  50   10   20    5   ""
	Gosub WaitSewProgress
	
	'4 arrow
	SewTo  10   10  100    0   ""     ' jump to begin the square
	Gosub WaitSewProgress
	SewTo  30   60   20    5   ""
	Gosub WaitSewProgress
	SewTo  50   10   20    5   ""
	Gosub WaitSewProgress
	
	' circles
	SewTo  75   35  100    0   ""           ' jump to center
	Gosub WaitSewProgress
	SewTo  75   35   10    3   "Radius=20"
	Gosub WaitSewProgress
	
	SewTo  75   35   10    3   "Radius=15"
	Gosub WaitSewProgress
	
	SewTo  75   35   10    3   "Radius=10"
	Gosub WaitSewProgress
	
	SewTo   1    1  500    0   ""		   ' jump to home
	Gosub WaitSewProgress
	
	Gosub PunchLimits
Return


Label PunchLimits
	'       x    y  mm/s point  flags
	SewTo   0    0   200    0   ""         ' jump to begin the square
	Gosub WaitSewProgress
	
	SewTo   1    1    60    5   ""         ' lower left
	Gosub WaitSewProgress
	
	SewTo 149    1    60    5   ""         ' lower right
	Gosub WaitSewProgress
	
	SewTo 150   64    60    5   ""         ' upper right
	Gosub WaitSewProgress
	'
	SewTo   1   64    60    5   ""         ' upper left
	Gosub WaitSewProgress
	
	SewTo   1    1    60    5   ""         ' lower left
	Gosub WaitSewProgress
	
	SewTo   1    1   200    0   ""		   ' jump to home
	Gosub WaitSewProgress
Return
' -----------


'WATCH SewTo_Speed
'EXEC Button 5 Text Button5
