'   Name: Micrometric_Measure_5.txt     9 September 2019
'  Autor: Leonardo DE PALO Ph.D.
'  Scope: Measure repetitivity after zero operation using 5V inductive
'         sensor. Stepper motor NEMA 57BYGH436B.
'		  Stepper driver WXM256
'
' ========== VARIABLE ASSIGNEMENT =====================================
' v1 to Count loop
' v2 be assigned
' v3 be assigned
' v4 be assigned
' v5 be assigned
' v6 be assigned
' v7 be assigned
' v8 be assigned
' v9 be assigned

' =========== MICROSTEP HARDWARE SET UP WXM256 ========================
' Motor M2 current 250 mA
' Microstep "ABC"  32
' Microstep "A C" 128


'Load xxxxxx.jpg

Option Speed 9
Button 1 Text "Home M2(Z)"
Button 2 Text "Move to zero"
Button 3 Text "Move to 10"
Button 4 Text "100 times"
Button 5 Text "100B subloop"

Button 7 Text "Home 100times"

Button 8 Text DisableMotor

Load Theremino_HAL\Theremino_HAL.exe
Load Theremino_HAL\Theremino_SlotViewer.exe
Load Theremino_HAL\Theremino_Terminal.exe
'Load Theremino_HAL\Theremino_SlotsToSpeech.exe
Load Theremino_HAL\Theremino_MicroVideo.exe
Load Theremino_HAL\Theremino_SignalScope.exe

s2 = LogTest.txt
v3 = 99
Key Pageup		Gosub MoveUp
Key Pagedown	Gosub MoveDown

Variable Numeric pippo

Gosub DisableMotor
Stop


' =============== HOMING MOTOR 2 (Z) ==================================
Label Home M2(Z)
	Gosub EnableMotor
	Label Homing_M2_1
		Slot(5) = Slot(5) + 1		' direction and speed goto switch
		Wait Seconds 0.05			' pulse up time
		If Slot(21) > 500 			' limit switch
			Goto Homing_M2_2
			Wait Seconds 0.3		' stabilize before inverse direction
		EndIf
	Goto Homing_M2_1
Goto Homing_M2_2
'
Label Homing_M2_2
	Slot(5) = Slot(5) - 0.01		' direction/speed return from sensor
	Wait Seconds 0.05
	If Slot (21) < 500				' limit switch
		Goto Homing_M2_3
	EndIf
Goto Homing_M2_2
'
Label Homing_M2_3
	Wait Seconds 1					' JUST TO SEE THE ZERO BY EYES
	Slot 5 = Reset					' Reset the slot value
	Wait Seconds 0.05
	Slot 5 = 0   					' declared initial position
	Wait Seconds 0.05
	Slot 5 = -5 					' move desidere initial position
Return

' =============== MOVE TO ZERO ========================================
Label Move to zero
	Slot 5 = 0   					' declared initial position
	Wait Seconds 0.05
Return

' =============== MOVE TO POSITION 10 =================================
Label Move to 10
	Slot 5 = -10   					' declared initial position
	Wait Seconds 0.05
Return

' =============== MOVE 100 TIMES ======================================
Label  100 times
	Gosub Home M2(Z)
	v1 = 0
	Label Loop
		v1 = v1 + 1
		If v1 > 100
			Gosub Home M2(Z)
			Return
		EndIf
		'
		Wait Seconds 1
		Slot 5 = -10
		Gosub WaitM2
		Wait Seconds 0.8
		Slot 5 = 0
		Gosub WaitM2
		Wait Seconds 0.8
		v2 = Slot(26)
		Slot 25 = v2
		s1 = Now
		s1 = s1 + Chars(" ", 4) + "v1=" + v1
		s1 = s1 + Chars(" ", 4) + "v2=" + Format(v2, "0.000")
		Print s1
		Save StringToFile s1 s2
	Goto Loop
Return

' =============== MOVE 100 TIMES WITH SUBLOOP =========================
Label 100B subloop
	'Gosub Homing_M2
	v1 = 0
	Label LoopB
		v1 = v1 + 1
		If v1 > 100
			Gosub Home M2(Z)
			Return
		EndIf
		'
		Wait Seconds 1
		Gosub MoveMotor1mm0mm
		Gosub MoveMotor1mm0mm
		Gosub MoveMotor1mm0mm
		Gosub MoveMotor1mm0mm
		Gosub MoveMotor1mm0mm
		Gosub MoveMotor1mm0mm
		Gosub MoveMotor1mm0mm
		Gosub MoveMotor1mm0mm
		Gosub MoveMotor1mm0mm
		Gosub MoveMotor1mm0mm
		'
		Wait Seconds 0.8
		v2 = Slot(26)
		Slot 25 = v2
		s1 = Now
		s1 = s1 + Chars(" ", 4) + "v1=" + v1
		s1 = s1 + Chars(" ", 4) + "v2=" + Format(v2, "0.000")
		Print s1
		Save StringToFile s1 s2
	Goto LoopB
Return

Label MoveMotor1mm0mm
	Slot 5 = -1
	Gosub WaitM2
	Slot 5 = 0
	Gosub WaitM2
Return

' =============== HOMING MOTOR 2(Z) MULTIPLE TIME =====================
Label Home 100times
	Gosub EnableMotor
	v1 = 0
	Label LoopMulti
		v1 = v1 + 1
		If v1 > 100
			Return
		EndIf
		Label HomeMulti_1
			Slot(5) = Slot(5) + 0.1	' direction and speed goto switch
			Wait Seconds 0.01			' pulse up time
			If Slot(21) > 500 			' limit switch
				Goto HomeMulti_2
				Wait Seconds 0.3		' stabilize before inverse direction
			EndIf
			Goto HomeMulti_1
Label HomeMulti_2
	Slot(5) = Slot(5) - 0.005		' direction/speed return from sensor
	Wait Seconds 0.01
	If Slot (21) < 500				' limit switch
		Goto HomeMulti_3
	EndIf
	Goto HomeMulti_2
Label HomeMulti_3
	Wait Seconds 1					' JUST TO SEE THE ZERO BY EYES
	Slot 5 = Reset					' Reset the slot value
	Wait Seconds 0.05
	Slot 5 = 0   					' declared initial position
	Gosub WaitM2
	Wait Seconds 0.05
	v3 = Slot(26)
	Slot 5 = -5 					' move desidere initial position
	Gosub WaitM2
	Wait Seconds 1
	v2 = Slot(26)
	Slot 25 = v2
	s1 = Now
	s1 = s1 + Chars(" ", 4) + "v1=" + v1
	s1 = s1 + Chars(" ", 4) + "v2=" + Format(v2, "0.000")
	s1 = s1 + Chars(" ", 4) + "v3=" + Format(V3, "0.000")
	Print s1
	Save StringToFile s1 s2
Goto LoopMulti
Return



'---------------------------------------------------------------------------------
'----- subroutine area -----------------------------------------------------------
'---------------------------------------------------------------------------------
' =============== ENABLE / DISABLE MOTORS ==========================
Label EnableMotor
	Slot 11 = 1000
	Wait Seconds 0.05
Return

Label DisableMotor
	Slot 11 = 1000					'inverted on the HAL
	Wait Seconds 0.05
Return

' =============== MOVE UP AND DOWN WITH KEYBOARD ======================
Label MoveUp
	Gosub EnableMotor			'Breakpoint
	Slot(5) = Slot(5) + 1
Return

Label MoveDown
	Gosub EnableMotor			'Breakpoint
	Slot(5) = Slot(5) - 1
Return


' =============== W A I T   M O T O R  -2- (Z) =====================
Label WaitM2
' ------------------ this Wait ensures that the motors are really started
	Wait Seconds 0.1
	Label WaitM2_2
		If Abs(Slot(6)) < 0.01
			Return
		EndIf
		Wait Seconds 0.01
	Goto WaitM2_2
' ------------------ this line is required to indent correctly
Goto WaitM2


'====== DUMMY AREA ===============================================================

'====== TO DO AREA ===============================================================

'====== SET UP AREA ===============================================================
' StepperDriver V2 board jumper set up with WXM006:
' Motor M2(Z): '
' Microstep "ABC"  32
' Microstep "A C" 128

' ENABLE stepper motors:
' from center to ON = always on
' from center to Out2 = SW controlled
' Gear ratio for M0 and M1 1:4   Step x mm = 800 x 360 / 360 = 59.556 ???????????
' Leadscrew: 4mm for one turn




' Inductive sensor: Blue=GND  Brown=+5VDC  Black=output


'WATCH New watch
'WATCH New watch
'WATCH v1
'WATCH s1
'WATCH Now
'WATCH Print Now
'WATCH Now
