Program Examples:Cam table with shutdown and restart

From SoftMC-Wiki
Jump to: navigation, search
Language: English  • 中文(简体)‎

This is an example of using cam table with system stop. In this example, after system stopped we consider the possibility that the slave pfb was moved, so the master offset is to be calculated by means of linear interpolation.


'************************ cam table continuation ************************ 
' File:		camTest.LIB
' Purpose:	this program executes master-slave motion with relation of cam
'			table. the objective of the task is to allow continuing slave
'			motion even after system stopped: direction and target position
'			in this program we have a chain of slaves:
'			MASTER --[gear]--> INTERMIDIATE --[cam]--> CARRIAGE
' Version:	1.0.0.2
' Author:	Eran Korkidi
' History:	1.MAY.2015		V1.0.0.0  -  Eran Korkidi  -  Created
'***************************************************************************


common shared MASTER			as generic axis = A4
common shared INTERMIDIATE		as generic axis = A5
common shared CARRIAGE			as generic axis = A3
common shared camCarriagePos	as CAM
common shared camCarriageNeg	as CAM
common shared xStop				as long = 0

dim shared CARRIAGE_PREV_POS		as string
dim shared CARRIAGE_PREV_CAM		as string
dim shared CARRIAGE_MASTER_POS		as string

dim shared lMasterHalfPath		as long = 10000
dim shared dMasterPosition		as double = 0.0
dim shared dCarriagePosition	as double = 0.0
dim shared sActiveCam			as string = "camCarriagePos"

program

	dim i						as long
	dim dPositionDifferenace	as double = 10.0
	dim ltableSize				as long

	Attach MASTER			'	allow task to control MASTER       axis
	Attach INTERMIDIATE		'	allow task to control INTERMIDIATE axis
	Attach CARRIAGE			'	allow task to control CARRIAGE     axis

		try
			Load tex_el.prg	'	get data from previous run (positions and active cam table)
			catch else
			Print "tex-el.prg file doesnt exist, assigning default parameters"
			CARRIAGE_PREV_POS	= "0.0"
			CARRIAGE_MASTER_POS	= "0.0"
			CARRIAGE_PREV_CAM	= "camCarriagePos"
		end try

		dCarriagePosition =  Val(CARRIAGE_PREV_POS)	'	convert prev position from string to double
'		if Abs(dCarriagePosition - CARRIAGE.pfb) > dPositionDifferenace then ' TBD threshold
			dCarriagePosition = CARRIAGE.pfb	'	get current position
			call calcMasterOffset	'	calculate the position of the master according to the position of the slave (carriage)
'		end if

		Sleep 100
		dMasterPosition = Val(CARRIAGE_MASTER_POS) MOD (2 * lMasterHalfPath)	'	find the position of MASTER inside the cycle of MASTER in the cam table. eg, 2100345.678 MOD 2000 = 345.678
		sActiveCam = CARRIAGE_PREV_CAM	'	assign the same camtable that was undergoing before system stop
		try
			ltableSize = lMasterHalfPath	'	in this example we have distance of 10000 shared by 10000 master data entries
			CreateCamData ltableSize camCarriagePos	'	build cam table of size ltableSize = 10000
			CreateCamData ltableSize camCarriageNeg	'	build cam table of size ltableSize = 10000
			LoadCamData CRG_POS.CAM camCarriagePos	'	get data of cam table from previous run
			LoadCamData CRG_NEG.CAM camCarriageNeg	'	get data of cam table from previous run
			catch 8042
			catch 3077
			catch else
				for i = 1 to ltableSize													'	in case the cam tables don't exist - built them
					camCarriagePos.MasterData[i] = i-1									'	[0   , ...... , 10000]
					camCarriagePos.SlaveData[i] = (i-1)*0.01							'	[0.0 , ...... , 100.0]
				next
				camCarriagePos.MasterData[ltableSize] = lMasterHalfPath
				camCarriagePos.SlaveData[ltableSize] = lMasterHalfPath * 0.01
				for i = 1 to ltableSize
					camCarriageNeg.MasterData[i] = lMasterHalfPath + (i - 1)			'	[10000 , ...... , 20000]
					camCarriageNeg.SlaveData[i] = (lMasterHalfPath - (i - 1)) * 0.01	'	[100.0 , ...... , 0.0  ]
				next
				camCarriageNeg.MasterData[ltableSize] = 2 * lMasterHalfPath
				camCarriageNeg.SlaveData[ltableSize] = 0.0
				StoreCamData CRG_POS.CAM camCarriagePos							'	keep cam table for next run
				StoreCamData CRG_NEG.CAM camCarriageNeg							'	keep cam table for next run
		end try

		camCarriagePos.Previous = camCarriageNeg		'	   /
		camCarriageNeg.Previous = camCarriagePos		'	  /	camCarriageNeg --> camCarriagePos --> camCarriageNeg --> camCarriagePos ....
		camCarriagePos.Next = camCarriageNeg			'	 /
		camCarriageNeg.Next = camCarriagePos			'	/

		INTERMIDIATE.MasterSource = MASTER.PositionFeedback
		INTERMIDIATE.GearRatio = 1
		INTERMIDIATE.Slave = GEAR
		INTERMIDIATE.En = 1

		CARRIAGE.MasterSource = INTERMIDIATE.Pcmd
		CARRIAGE.GearRatio = 1.0
		CARRIAGE.CamOffset = dMasterPosition					'	here we assign the offset on the masterdata inside the range (masterData[1] .... masterData[activeCamTable.Size] )
		if Ucase$(sActiveCam) = Ucase$("camCarriagePos") then	'	set the correct active cam
			CARRIAGE.FirstCam  = camCarriagePos
		else
			CARRIAGE.FirstCam  = camCarriageNeg
		end if			
		CARRIAGE.Slave = CAM
		CARRIAGE.En = 1       

		MASTER.En = 1
		Jog MASTER 1	'	start run MASTER. at this point the CARRIAGE will move back n forth

		while NOT xStop
			Sleep 1
		end while

		Stop MASTER

		while MASTER.IsMoving ' wait for MASTER full stop
			Sleep 1
		end while

		CARRIAGE_PREV_POS = Str$(CARRIAGE.Pfb)	'	store CARRIAGE position in file
		CARRIAGE_MASTER_POS = Str$(MASTER.Pfb)	'	store MASTER position in file
		CARRIAGE_PREV_CAM = CARRIAGE.ActiveCam
		Save File="tex_el.prg" type=All VariableName="CARRIAGE_*" Mode=New

		Proceed MASTER	'	because we dont want problems with applying another move command
		CARRIAGE.Slave = FALSE ' otherwise next run the camtable init will fail

	Detach MASTER
	Detach INTERMIDIATE
	Detach CARRIAGE

end program


'****************************************************************************
' function Name:		calcMasterOffset
' Description:			calculates the position of master offset for cam table
' Called From:			local
' Author:				Eran Korkidi
' Input Parameters:		N/A
' Output Parameters:	CARRIAGE_MASTER_POS
' Algorithm:			finds the interpolated position of slave between two closest points
'						assign the interpolated position of the master between relevant points
'****************************************************************************
sub calcMasterOffset

	dim tableSize as long
	dim i as long
	dim interpolationRatio as double

	if Instr(Ucase$(CARRIAGE_PREV_CAM), "POS") then

		tableSize = camCarriagePos.size
		if dCarriagePosition = camCarriagePos.slaveData[1] then
			CARRIAGE_MASTER_POS = Str$(camCarriagePos.masterData[1])	'	 it might be the case that the machine stopped in the start of the path 
		else
			if dCarriagePosition = camCarriagePos.slaveData[tableSize] then
				CARRIAGE_MASTER_POS = Str$(camCarriagePos.masterData[tableSize])	'	 it might be the case that the machine stopped in the end of the path
			else
				for i = 1 to tableSize-1
					if ( dCarriagePosition >= camCarriagePos.slaveData[i] AND dCarriagePosition <= camCarriagePos.slaveData[i+1] ) then
						' find two closest points
						interpolationRatio = (dCarriagePosition - camCarriagePos.slaveData[i])/(camCarriagePos.slaveData[i+1]-camCarriagePos.slaveData[i])
						' calculate linear deviation between the two slave points
						CARRIAGE_MASTER_POS = Str$(camCarriagePos.masterData[i] + interpolationRatio*(camCarriagePos.masterData[i+1] - camCarriagePos.masterData[i]))
						'	assign new master position according to the linear interpolation of the slave
					end if
				next
			end if
		end if

	else

		tableSize = camCarriagePos.size
		if dCarriagePosition = camCarriageNeg.slaveData[1] then
			CARRIAGE_MASTER_POS = Str$(camCarriageNeg.masterData[1])	'	 it might be the case that the machine stopped in the start of the path
		else
			if dCarriagePosition = camCarriageNeg.slaveData[tableSize] then
				CARRIAGE_MASTER_POS = Str$(camCarriageNeg.masterData[tableSize])	'	 it might be the case that the machine stopped in the end of the path
			else
				for i = 1 to tableSize-1
					if ( dCarriagePosition <= camCarriageNeg.slaveData[i] AND dCarriagePosition >= camCarriageNeg.slaveData[i+1] ) then
						' find two closest points
						interpolationRatio = (dCarriagePosition - camCarriageNeg.slaveData[i+1])/(camCarriageNeg.slaveData[i]-camCarriageNeg.slaveData[i+1])
						' calculate linear deviation between the two slave points
						CARRIAGE_MASTER_POS = Str$(camCarriageNeg.masterData[i] - interpolationRatio*(camCarriageNeg.masterData[i] - camCarriageNeg.masterData[i+1]))
						'	assign new master position according to the linear interpolation of the slave
					end if
				next
			end if
		end if

	end if

end sub