
Load "Theremino_HAL\Theremino_SignalScope.exe"
Load "Theremino_HAL\Theremino_Hal.exe"
Load "Theremino_HAL\Theremino_SlotViewer.exe"

Button 2 Text AbortSewing

Variable Numeric SlotX = 1
Variable Numeric SlotY = 3
Variable Numeric SlotZ = 5
Variable Numeric SlotSewScopeRestart = 29
Variable Numeric SlotSignalScopeRun = 99

' StopAngle could be used in the future versions
Variable Numeric StopAngle = 90

' StartAngle can be set from 270 to 350 depending from HAL accellerations
Variable Numeric StartAngle = 270+80

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

SewInit SlotX SlotY SlotZ StopAngle StartAngle

SewLimits Xmin Xmax Ymin Ymax MaxPerSec

StartScopes
'TestCircleLeo
'RectangleFast
'Rectangle
'Circle
TestBzz

StopScopes

Stop

Label AbortSewing
	SewAbort
Stop

Label StartScopes
	Slot SlotSignalScopeRun = 1000
	Slot SlotSewScopeRestart = 1000
	Wait Seconds 0.1
	Slot SlotSewScopeRestart = 0
Return

Label StopScopes
	Slot SlotSignalScopeRun = 0
Return

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

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
	Gosub WaitAllMotors
Return

Label Circle
	'Slot(SlotZ) = 0
	'
	'SewTo 1 1 500 0 ""
	'Gosub WaitSewProgress
	'
	'v1 = ElapsedTime
	
	'SewTo 101 81 20 5 "zigzag=2"
	'Gosub WaitSewProgress
	
	SewTo 50 30 100 0 ""
	Gosub WaitSewProgress
	'SewTo 50 50 20 3 "Radius=15.92"
	SewTo 50 30 30 3 "Radius=5"' zigzag=4"
	Gosub WaitSewProgress
	
	'v2 = ElapsedTime
	'
	'SewTo 1 1 500 0 ""
	'Gosub WaitSewProgress
	'
	'Print Format(v2 - v1, "0.000") + " sec."
	
	SewTo 3 3 100 0 ""
	Gosub WaitSewProgress
Return

Label Rectangle
	SewTo 10 10 100 0 ""
	Gosub WaitSewProgress
	
	SewTo 12 10 20 2 ""
	Gosub WaitSewProgress
	SewTo 10 10 20 2 ""
	Gosub WaitSewProgress
	SewTo 12 10 20 2 ""
	Gosub WaitSewProgress
	SewTo 10 10 20 2 ""
	Gosub WaitSewProgress
	
	SewTo 40 10 20 4 ""
	Gosub WaitSewProgress
	SewTo 40 30 20 4 ""
	Gosub WaitSewProgress
	SewTo 10 30 20 4 ""
	Gosub WaitSewProgress
	SewTo 10 10 20 4 ""
	Gosub WaitSewProgress
	
	SewTo 10 12 20 2 ""
	Gosub WaitSewProgress
	SewTo 10 10 20 2 ""
	Gosub WaitSewProgress
	SewTo 10 12 20 2 ""
	Gosub WaitSewProgress
	SewTo 10 10 20 2 ""
	Gosub WaitSewProgress
	
	SewTo 3 3 100 0 ""
	Gosub WaitSewProgress
Return

Label RectangleFast
	'SewTo 10 10 100 0 ""
	'Gosub WaitSewProgress
	
	SewTo 20 10 10 1 ""
	Gosub WaitSewProgress
	SewTo 20 20 20 2 ""
	Gosub WaitSewProgress
	SewTo 10 20 40 4 ""
	Gosub WaitSewProgress
	SewTo 10 10 20 5 ""
	Gosub WaitSewProgress
	
	'SewTo 3 3 100 0 ""
	'Gosub WaitSewProgress
Return

' ===========================================
'  SPEED LIMITS
' ===========================================
'  WITH XY acceleration = 6000
'   AND  Z acceleration =  320
' -------------------------------------------
'  The max linear speed is 20 or 30 mm/s
'  The max stitch speed is 10 or 15 st./s
' ===========================================

' ===========================================
'  SIMPLIFIED TABLE
' ===========================================
' Step    Speed Max    Speed Max with errors
' -------------------------------------------
'  1         10         15
'  2         20         30
'  >2        20         30
' ===========================================


Label TestCircleLeo
	'SewTo  70   60   100    0    ""    'jump to center
	'SewTo  70   35   100    0    ""    'jump to center
	WaitSewProgress
	SewTo  70   35    20    4    "Radius=25"
	WaitSewProgress
	SewTo  70   35    20    4    "Radius=20"
	WaitSewProgress
	SewTo  70   35    20    4    "Radius=15"
	WaitSewProgress
	SewTo  70   35    20    4    "Radius=10"
	WaitSewProgress
	SewTo  70   35    20    4    "Radius=5"
	WaitSewProgress
	'
	SewTo       3    3   100    0    ""     'jump to safe place
	WaitSewProgress
Return

Label TestBzz
	SewTo       3    3   100    0    ""     'jump to safe place
	WaitSewProgress
	
	SewTo  70   35    20    4   ""
	WaitSewProgress
Return


'WATCH SewTo_Speed
'EXEC Button 5 Text Button5
