'===================================================================
'  RECOLOR SPEED TESTER - 450 LINES
'===================================================================

' ------- ColorAllLines_StartingWith                     470 mS
' ------- ColorAllLines_StartingWith_And_Containing      280 mS
Label pippo
Return

' ------- ColorAllLines_StartingWith                     160 mS
For v1 = 0 To 9
Next

' ------- ColorAllLines_StartingWith                     120 mS
Variable String Info2     'test result
Variable String Result

' ------- ColorAllLines_StartingWith                     200 mS
If v1 = v2
EndIf


' ==================================================================
'     Name:	WXM_Measure1_V5.05.2.txt           Date: 02 October 2019
'    Autor: Leonardo DE PALO Ph.D.
'    Scope: Automatic measurement
' Hardware: Cartesian System.
' ==================================================================
'
'Load BM_Foundation0.jpg
'================ GENERATING THE LOG FILE NAME =====================
's2 = "Measurement.txt"
'S9 = "- Date - " + " - TIME - " + " Cycle " + " RESULT "
'Save StringToFile s9 s2
'===================================================================
'
'Load BM_Foundation0.jpg
'================ GENERATING THE LOG FILE NAME =====================
's2 = "Measurement.txt"
'S9 = "- Date - " + " - TIME - " + " Cycle " + " RESULT "
'Save StringToFile s9 s2
'===================================================================

Load Toc.wav
Option Speed 9
Button 1 Text "HOME All"
Button 2 Text "HOUSE"

Button 4 Text "MEASUREMENT"

Button 5 Text "Home M0(X)"
Button 6 Text "Home M1(Y)"
Button 7 Text "Home M2(Z)"

Button 7 Text "LASER"

Load Theremino_HAL\Theremino_HAL.exe
Load Theremino_HAL\Theremino_SlotViewer.exe
'Load Theremino_HAL\Theremino_MediaPlayer.exe
Load Theremino_HAL\Theremino_Terminal.exe
'Load Theremino_HAL\Theremino_SlotsToSpeech.exe
'Load Theremino_HAL\\Theremino_MicroVideo_V1.4_CAM\Theremino_MicroVideo\Theremino_MicroVideo.exe
'Load Theremino_HAL\\Theremino_MicroVideo_V1.4_Video\Theremino_MicroVideo\Theremino_MicroVideo.exe'Load Theremino_HAL\Theremino_SignalScope.exe
Load Theremino_HAL\Theremino_SignalScope.exe

Variable Numeric DigitTime
Variable Numeric NumTest
Variable Numeric NumCycles
Variable Numeric Current

Variable Numeric Coord_X
Variable Numeric Coord_Y
Variable Numeric Coord_Z
Variable Numeric Coord_A
Variable Numeric LowSpeed

Variable Numeric NumMeasure
Variable Numeric Measure

Variable Numeric SlotMotor
Variable Numeric SlotSensor
Variable Numeric SpeedSearch
Variable Numeric SpeedReturn
Variable Numeric DeclaredZero
Variable Numeric InitialPosition


Variable String Info
Variable String Point



Key Right	 	Gosub MoveRightM0
Key Left 		Gosub MoveLeftM0
Key Up 			Gosub MoveFarM1
Key Down 		Gosub MoveNearM1
Key Pageup		Gosub MoveUpM2
Key Pagedown	Gosub MoveDownM2

Gosub RED
Stop

Label "MEASUREMENT"
	Gosub BLK
	s2 = "Measure_repetition_2.txt"
	Save StringToFile "--------========== START ==========-------" s2
	S9 = "- Date - " + " - TIME - " + " Cycle " + "  Measure  " + "Point"
	Save StringToFile s9 s2
	'NumTest = Input "Tell me how many cycle:"
	NumTest = 3000
	Gosub "HOME ALL"
	For NumMeasure = 1 To NumTest
		Gosub MeasureScrew
		Gosub SaveData
		Gosub MeasureFlat
		Gosub SaveData
		Gosub MeasureBorder
		Gosub SaveData
		Gosub MeasureLowerLeft
		Gosub SaveData
		Gosub MeasureUpperLeft
		Gosub SaveData
		Gosub MeasureUpperMiddle
		Gosub SaveData
		Gosub MeasureUpperRigth
		Gosub SaveData
		Gosub MeasureLowerRigth
		Gosub SaveData
		Gosub MeasureLowerMiddle
		Gosub SaveData
		Gosub "HOUSE"
	Next
	Gosub BLU
Return

Label "MeasureScrew"
	Point = A
	Coord_X =  48.156
	Coord_Y = 181
	Coord_Z =  34.14
	Gosub "EXEC MOVE"
	Wait Seconds 1.5
	Measure = Slot(37)
Return

Label "MeasureFlat"
	Point = B
	Coord_X =  56.5
	Coord_Y = 170
	Coord_Z =  34.14
	Gosub "EXEC MOVE"
	Measure = Slot(37)
	Wait Seconds 1
Return

Label "MeasureBorder"
	Point = C
	Coord_X =  46.5
	Coord_Y = 170
	Coord_Z =  34.14
	Gosub "EXEC MOVE"
	Measure = Slot(37)
	Wait Seconds 1
Return

Label "MeasureLowerLeft"
	Point = D
	Coord_X =  60
	Coord_Y = 180.5
	Coord_Z =  34.14
	Gosub "EXEC MOVE"
	Measure = Slot(37)
	Wait Seconds 1
Return

Label "MeasureUpperLeft"
	Point = E
	Coord_X =  60.5
	Coord_Y = 164
	Coord_Z =  34.14
	Gosub "EXEC MOVE"
	Measure = Slot(37)
	Wait Seconds 1
Return

Label "MeasureUpperMiddle"
	Point = F
	Coord_X =  87
	Coord_Y = 165
	Coord_Z =  34.14
	Gosub "EXEC MOVE"
	Measure = Slot(37)
	Wait Seconds 1
Return

Label "MeasureUpperRigth"
	Point = G
	Coord_X = 121.5
	Coord_Y = 164.5
	Coord_Z =  34.14
	Gosub "EXEC MOVE"
	Measure = Slot(37)
	Wait Seconds 1
Return

Label "MeasureLowerRigth"
	Point = H
	Coord_X = 120.5
	Coord_Y = 181
	Coord_Z =  34.14
	Gosub "EXEC MOVE"
	Measure = Slot(37)
	Wait Seconds 1
Return

Label "MeasureLowerMiddle"
	Point = I
	Coord_X =  97.5
	Coord_Y = 178
	Coord_Z =  34.14
	Gosub "EXEC MOVE"
	Measure = Slot(37)
	Wait Seconds 1
Return

Label "EXEC MOVE"
	Gosub GRE
	Gosub WaitAllMotors
	Slot(1) = Coord_X
	Gosub WaitAllMotors
	Slot(3) = Coord_Y
	Gosub WaitAllMotors
	Slot(5) = Coord_Z
	Gosub WaitAllMotors
	For LowSpeed = 100 To 0 Step -1
		Wait Seconds 0.01
		Slot(5) = Slot(5) - 0.1
	Next
Return

Label "SaveData"
	s1 = ""
	s1 = Now
	s1 = s1 + "  " + Format(NumMeasure, "00000") + "   " + Format(Measure, "000.000")
	s1 = s1 + "    " + Point
	Print s1
	Save StringToFile s1 s2
	Slot(5)	= 50			'Raise Z to safe area
Return


'----------------------------------------------------------------------
'----- S U B R O U T I N E   A R E A  ---------------------------------
'----------------------------------------------------------------------
' =============== HOME ALL MOTORs M0, M1, M2 ==========================
Label "HOME All"
	Gosub BLU
	Slot(2) = Slot(2) - 15				'Raise Z for safety
	Gosub "Home M3(A)"
	Gosub "Home M2(Z)"
	Gosub "Home M1(Y)"
	Gosub "Home M0(X)"
Return

Label "Home M0(X)"
	SlotMotor = 1
	SlotSensor = 19
	SpeedSearch = -0.1
	SpeedReturn = +0.005
	DeclaredZero = 0
	InitialPosition = 1
	Gosub "Home"
Return

Label "Home M1(Y)"
	SlotMotor = 3
	SlotSensor = 20
	SpeedSearch = -0.1
	SpeedReturn = +0.05
	DeclaredZero = 0
	InitialPosition = 1
	Gosub "Home"
Return

Label "Home M2(Z)"
	SlotMotor = 5
	SlotSensor = 21
	SpeedSearch = +0.1
	SpeedReturn = -0.005
	DeclaredZero = 103
	InitialPosition = 100
	Gosub "Home"
Return

Label "Home M3(A)"
	SlotMotor = 25
	SlotSensor = 31
	SpeedSearch = -0.08
	SpeedReturn = +0.05
	DeclaredZero = 0
	InitialPosition = 1
	Gosub "Home"
Return

' =============== HOUSE ALL MOTORS M0, M1, M2 ========================
Label "HOUSE"
	Gosub BLU
	Slot(5) =  98	 	'Z Raise
	Gosub WaitAllMotors
	Slot 1 =  2     	'X go to HOUSE
	Slot 3 =  2     	'Y go to HOUSE
	Gosub WaitAllMotors
	Gosub WHI
Return

' =============== HOME MOTOR ==========================================
Label "Home"
	Gosub Blu
	If Slot(SlotSensor) > 500      ' detach If sensor active
		Slot(SlotMotor) = Slot(SlotMotor) + SpeedReturn
	EndIf
'
Label SearchHome
	Slot(SlotMotor) = Slot(SlotMotor) + SpeedSearch
	Wait Seconds 0.001
	If Slot(SlotSensor) > 500 	' limit switch
		Gosub WaitMotor			' WAIT UNTIL MOTOR STOP
		Wait Seconds 0.1		' stabilize mechanically
		Goto DetachSensor
	EndIf
Goto SearchHome
'
Label DetachSensor
	Slot(SlotMotor) = Slot(SlotMotor) + SpeedReturn ' direction/speed return
	Wait Seconds 0.001
	If Slot (SlotSensor) < 500
		Gosub WaitMotor
		Goto AssignZero
	EndIf
Goto DetachSensor
'
Label AssignZero
	Slot(SlotMotor) = Reset			' Reset the Slot value
	Wait Seconds 0.08		  		' DO NOT TOUCH, needed to the Master!!!!
	Slot(SlotMotor) = DeclaredZero
	Gosub WaitMotor
	Slot(SlotMotor) = InitialPosition
	Gosub WHI
Return

' =============== W A I T until the motor (Slot Motor) is stopped ========
Label WaitMotor
' ------------------ this Wait ensures that the motors ia running
	Wait Seconds 0.1
	Label Wait1
		If Abs(Slot(SlotMotor + 1)) < 0.01
			Return			' now the motor is not running
		EndIf
		Wait Seconds 0.005
	Goto Wait1
' ------------------ this line is required to indent correctly
Goto WaitMotor

' =============== W A I T  all MOTORS are STOPPED =======================
Label WaitAllMotors
' ------------------ this Wait ensures that the motors are really started
	Wait Seconds 0.1
	Label WaitAllMotors2
		If Abs(Slot(2)) < 0.01 And Abs(Slot(4)) < 0.01 And Abs(Slot(6)) < 0.01 And Abs(Slot(26)) < 0.01
			Return
		EndIf
		Wait Seconds 0.01
	Goto WaitAllMotors2
' ------------------ this line is required to indent correctly
Goto WaitAllMotors

' =============== ENABLE / DISABLE MOTORS =============================
'Label EnableMotor
'	Slot 11 = 0
'	Wait Seconds 0.2
'Return
'
'Label DisableMotor
'	Slot 11 = 1000
'	Wait Seconds 0.2
'Return

' =============== TRAFFIC LIGHT COLOR =================================
Label RED
	Slot 16 =  600		'RED
	Slot 17 =    0		'GRE
	Slot 18 =    0		'BLU
Return

Label GRE
	Slot 16 =    0		'RED
	Slot 17 =  500		'GRE
	Slot 18 =    0		'BLU
Return

Label BLU
	Slot 16 =    0		'RED
	Slot 17 =    0		'GRE
	Slot 18 =  500		'BLU
Return

Label WHI
	Slot 16 =  450		'RED
	Slot 17 =  450		'GRE
	Slot 18 =  450		'BLU
Return

Label BLK
	Slot 16 =    0		'RED
	Slot 17 =    0		'GRE
	Slot 18 =    0		'BLU
Return


' =============== MOVE USING KEYBOARD ================================
Label MoveRightM0
	Slot(1) = Slot(1) + 0.5
Return

Label MoveLeftM0
	Slot(1) = Slot(1) - 0.5
Return

Label MoveFarM1
	Slot(3) = Slot(3) + 0.5
Return

Label MoveNearM1
	Slot(3) = Slot(3) - 0.5
Return

Label MoveUpM2
	Slot(5) = Slot(5) + 0.5
Return

Label MoveDownM2
	Slot(5) = Slot(5) - 0.5
Return


'====== DUMMY AREA ====================================================
'====== TO DO AREA ====================================================
'====== SET UP AREA ===================================================


'WATCH SlotMotor
'WATCH SpeedSearch
'WATCH Measure
'EXEC Slot 1 = 3
