Program Examples:Reverse engineering for cam table

From SoftMC-Wiki
Revision as of 06:37, 16 April 2015 by EranKo (talk | contribs) (created)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This is an example of using cam table without no a-priory knowledge on the cam data.

the file buildCam.prg executes motions and builds the cam tables according to the generated profiles.

the file app.prg is the application that uses the cam table. in this example we use tex-el application for a machine that handles textile material.


buildCam.prg (use default configuration with record.lib)

'------------------------------------------------------------------------------
' File:        buildCam.prg
' Purpose:     tex-el motion application
' Version:     1.00
' Author:      Eran Korkidi
' Description:
' History:     13-APR-2015    Created
'------------------------------------------------------------------------------


import record.lib
common shared builtCamTable as CAM

dim shared sCarriageprfRec	as string ="CRG_prf.rec"
dim shared sT2prfRec		as string ="T2_prf.rec"
dim shared sBasculaprfRec	as string ="BSC_prf.rec"
dim shared camTable[10000]	as double
dim shared camTray[10000]	as double

dim shared carriageDistance as double = 562.4
dim shared masterDistance	as double = sqrt(2)*2*562.4 ' = 1590.71
dim shared basculaDistance	as double = 210.0
dim shared carriageVcruise	as double = 424.0
dim shared masterIncrements as double
dim shared T2DistanceTotal	as double = 422.0
dim shared T2DistancePos	as double = 214.45901693934426
dim shared T2DistanceNeg	as double = 207.5409836065574
dim shared T2vcruise		as double = 100.0	'	TBD
dim shared basculaVcruise	as double = 100.0	'	TBD



program

	dim i as long

	call motionRecCarriage
	call motionRecT2
	call motionRecBascula

	call generateRecordingTableCarriage
	StoreCamData CARRIAGE.CAM builtCamTable
'	for i = 1 to builtCamTable.Size
'		Print "MasterData[" i "]=" builtCamTable.MasterData[i] " ,  SlaveData["  i "]=" builtCamTable.SlaveData[i]
'		Sleep 50
'	next
'
	call generateRecordingTableT2
	StoreCamData T2.CAM builtCamTable
'	for i = 1 to builtCamTable.Size
'		Print "MasterData[" i "]=" builtCamTable.MasterData[i] " ,  SlaveData["  i "]=" builtCamTable.SlaveData[i]
'		Sleep 50
'	next

	call generateRecordingTableBascula
	StoreCamData BASCULA.CAM builtCamTable
	for i = 1 to builtCamTable.Size
		Print "MasterData[" i "]=" builtCamTable.MasterData[i] " ,  SlaveData["  i "]=" builtCamTable.SlaveData[i]
		Sleep 10
	next


terminate program


sub motionRecCarriage
	Record$ sCarriageprfRec 10000 Gap=1 RecData = A2.IsMoving, A2.Pcmd, A2.Vfb, A2.Vcmd, A2.Pfb
	Attach A2
		A2.En=1
		Move A2 0 Vcruise=carriageVcruise Abs=TRUE 'Acc=1000 Dec=1000
		while A2.IsMoving
			Sleep 1
		end while
		RecordOn
			Sleep 40
			Move A2 carriageDistance Vcruise=carriageVcruise Abs=TRUE 'Acc=1000 Dec=1000
			while A2.IsMoving
				Sleep 1
			end while
			Move A2 0 Vcruise=carriageVcruise Abs=TRUE StartType=SUPERIMMEDIATE 'Acc=1000 Dec=1000
			while A2.IsMoving
				Sleep 1
			end while
			Sleep 40
		RecordClose
	Detach A2
end sub


sub motionRecT2
	
	Record$ sT2prfRec 10000 Gap=1 RecData = A2.IsMoving, A2.Pcmd, A2.Vfb, A2.Vcmd, A2.Pfb
	Attach A2
		A2.En=1
		Move A2 0 Vcruise=carriageVcruise Abs=TRUE
		while A2.IsMoving
			Sleep 1
		end while
		RecordOn
			Sleep 40
			Move A2 T2DistancePos  Vcruise=T2vcruise Abs=TRUE
			while A2.IsMoving
				Sleep 1
			end while
			Move A2 -T2DistanceNeg Vcruise=T2vcruise Abs=FALSE StartType=SUPERIMMEDIATE
			while A2.IsMoving
				Sleep 1
			end while
			Sleep 40
		RecordClose

		Move A2 0 Vcruise=T2vcruise Abs=TRUE
		while A2.IsMoving
			Sleep 1
		end while

	Detach A2
end sub


sub generateRecordingTableCarriage

	dim sCRSfile as string
	dim handleAddress as long = 1
	dim sCurrentLine  as string 
	dim dA2pcmd 	as double
	dim lA2ismoving as double
	dim lCommaLoc as long
	dim tableCounter as long = 0
	dim i as long

	tableCounter = 0
	
	if recconvert(sCarriageprfRec) <> 1 then
		Print "convert failed"
	else
		sCRSfile = Left$(sCarriageprfRec,Len(sCarriageprfRec)-4) + ".CSR"
		Open sCRSfile Mode="r" as #handleAddress
			sCurrentLine = input$(#handleAddress)
			while sCurrentLine <> ""
				lCommaLoc = Instr(sCurrentLine,",")
				lA2ismoving = Val(Left$(sCurrentLine, lCommaLoc-1))
				sCurrentLine = Right$(sCurrentLine,Len(sCurrentLine)- lCommaLoc)
				dA2pcmd = Val(Left$(sCurrentLine, lCommaLoc-1))
				if lA2ismoving then
					tableCounter = tableCounter + 1 
					'Print "dA2ismoving="lA2ismoving, " , dA2pcmd="dA2pcmd
					camTable[tableCounter] = dA2pcmd
				end if
				sCurrentLine = input$(#handleAddress)
			end while
		close #handleAddress
		masterIncrements = masterDistance/(tableCounter+1)
		createcamdata tableCounter+2 builtCamTable
		builtCamTable.MasterData[1] = 0.0
		builtCamTable.SlaveData[1]  = 0.0
		for i = 1 to builtCamTable.Size-2
			builtCamTable.MasterData[i+1] = masterIncrements * i
			builtCamTable.SlaveData[i+1]  = camTable[i]
		next
		builtCamTable.MasterData[builtCamTable.Size] = masterDistance
		builtCamTable.SlaveData[builtCamTable.Size]  = 0.0
	end if

end sub


sub generateRecordingTableT2

	dim sCRSfile		as string
	dim handleAddress	as long = 1
	dim sCurrentLine	as string 
	dim dA2pcmd			as double
	dim lA2ismoving		as double
	dim lCommaLoc		as long
	dim tableCounter	as long = 0
	dim i				as long

	tableCounter = 0
	
	if recconvert(sT2prfRec) <> 1 then
		Print "convert failed"
	else
		sCRSfile = Left$(sT2prfRec,Len(sT2prfRec)-4) + ".CSR"
		Open sCRSfile Mode="r" as #handleAddress
			sCurrentLine = input$(#handleAddress)
			while sCurrentLine <> ""
				lCommaLoc = Instr(sCurrentLine,",")
				lA2ismoving = Val(Left$(sCurrentLine, lCommaLoc-1))
				sCurrentLine = Right$(sCurrentLine,Len(sCurrentLine)- lCommaLoc)
				dA2pcmd = Val(Left$(sCurrentLine, lCommaLoc-1))
				if lA2ismoving then
					tableCounter = tableCounter + 1 
					'Print "dA2ismoving="lA2ismoving, " , dA2pcmd="dA2pcmd
					camTable[tableCounter] = dA2pcmd
				end if
				sCurrentLine = input$(#handleAddress)
			end while
		close #handleAddress
		masterIncrements = T2DistanceTotal/tableCounter
		createcamdata tableCounter+1 builtCamTable
		builtCamTable.MasterData[1] = 0.0
		builtCamTable.SlaveData[1]  = 0.0
		for i = 1 to builtCamTable.Size-1
			builtCamTable.MasterData[i+1] = masterIncrements * i
			builtCamTable.SlaveData[i+1]  = camTable[i]
		next
	end if

end sub



sub motionRecBascula
	Record$ sBasculaprfRec 10000 Gap=1 RecData = A2.IsMoving, A2.Pcmd', A2.Vfb, A2.Vcmd, A2.Pfb
	Attach A2
		A2.En=1
		Move A2 0 Vcruise=basculaVcruise Abs=TRUE
		while A2.IsMoving
			Sleep 1
		end while
		RecordOn
			Sleep 40
			Move A2 basculaDistance Vcruise=basculaVcruise Abs=TRUE
			while A2.IsMoving
				Sleep 1
			end while
			Move A2 0 Vcruise=basculaVcruise Abs=TRUE StartType=SUPERIMMEDIATE
			while A2.IsMoving
				Sleep 1
			end while
			Sleep 40
		RecordClose
	Detach A2
end sub




sub generateRecordingTableBascula

	dim sCRSfile as string
	dim handleAddress as long = 1
	dim sCurrentLine  as string 
	dim dA2pcmd 	as double
	dim lA2ismoving as double
	dim lCommaLoc as long
	dim tableCounter as long = 0
	dim i as long

	tableCounter = 0
	
	if recconvert(sBasculaprfRec) <> 1 then
		Print "convert failed"
	else
		sCRSfile = Left$(sBasculaprfRec,Len(sBasculaprfRec)-4) + ".CSR"
		Open sCRSfile Mode="r" as #handleAddress
			sCurrentLine = input$(#handleAddress)
			while sCurrentLine <> ""
				lCommaLoc = Instr(sCurrentLine,",")
				lA2ismoving = Val(Left$(sCurrentLine, lCommaLoc-1))
				sCurrentLine = Right$(sCurrentLine,Len(sCurrentLine)- lCommaLoc)
				dA2pcmd = Val(Left$(sCurrentLine, lCommaLoc-1))
				if lA2ismoving then
					tableCounter = tableCounter + 1 
					'Print "dA2ismoving="lA2ismoving, " , dA2pcmd="dA2pcmd
					camTable[tableCounter] = dA2pcmd
				end if
				sCurrentLine = input$(#handleAddress)
			end while
		close #handleAddress
		masterIncrements = T2DistanceTotal/(tableCounter+1)
		createcamdata tableCounter+2 builtCamTable
		builtCamTable.MasterData[1] = 0.0
		builtCamTable.SlaveData[1]  = 0.0
		for i = 1 to builtCamTable.Size-2
			builtCamTable.MasterData[i+1] = masterIncrements * i
			builtCamTable.SlaveData[i+1]  = camTable[i]
		next
		builtCamTable.MasterData[builtCamTable.Size] = T2DistanceTotal
		builtCamTable.SlaveData[builtCamTable.Size]  = 0.0
	end if

end sub



app.prg (use default configuration, preferred after running buildCam.prg)

'------------------------------------------------------------------------------
' File:        CamDemo.prg
' Purpose:     tex-el motion application
' Version:     1.00
' Author:      Eran Korkidi
' Description:
' History:     13-APR-2015    Created
'------------------------------------------------------------------------------

common shared camCariage	as CAM
common shared camT2			as CAM
common shared camBascula	as CAM
common shared MASTER 	as generic axis
common shared CARRIAGE 	as generic axis
common shared BASCULA	as generic axis
common shared T2		as generic axis

program

	call setaxis(A5,1000)
	call setaxis(A4,1000)
	call setaxis(A3,1000)
	call setaxis(A2,1000)
	call SetAxis(A1,1000)
	try
		LoadCamData CARRIAGE.CAM camCariage
		catch 3063
			Load buildCam.prg
			Stas buildCam.prg
			Sleep 20*1000
			LoadCamData CARRIAGE.CAM camCariage
	end try
	try
		LoadCamData T2.CAM camT2
		catch 3063
			Load buildCam.prg
			Stas buildCam.prg
			Sleep 20*1000
			LoadCamData T2.CAM camT2
	end try
	try
		LoadCamData BASCULA.CAM camBascula
		catch 3063
			Load buildCam.prg
			Stas buildCam.prg
			Sleep 20*1000
			LoadCamData BASCULA.CAM camBascula
	end try
	camCariage.Cycle = -1
	camT2.Cycle = -1
	camBascula.Cycle = -1

	Attach A2
		A2.MasterSource = A1.PositionFeedback
		A2.GearRatio = 1'0.5
		A2.Slave = GEAR
		A2.En=1
	Detach A2

	Attach A3
		a3.MasterSource = a2.Pcmd
		a3.GearRatio = 1.0       
		a3.CamOffset = camCariage.MasterData[1]
		a3.FirstCam  = camCariage
		a3.Slave     = CAM       
		A3.En = 1                
	Detach A3
	
	Attach A4
		a4.MasterSource = a2.Pcmd
		a4.GearRatio = 1.0       
		a4.CamOffset = camT2.MasterData[1]
		a4.FirstCam  = camT2
		a4.Slave     = CAM       
		A4.En = 1
	Detach A4

	Attach A5
		A5.MasterSource = a2.Pcmd
		A5.GearRatio = 1.0       
		A5.CamOffset = camBascula.MasterData[1]
		A5.FirstCam  = camBascula
		A5.Slave     = CAM       
		A5.En = 1                
	Detach A5
	
	MASTER	 = A1
	CARRIAGE = A3
	T2		 = A4
	BASCULA  = A5
	
	Attach A1
		A1.En=1
		Record crg_T3.REC 20000 Gap=1 RecData = MASTER.Vcmd, CARRIAGE.Pcmd, CARRIAGE.Vcmd, T2.Pcmd, T2.Vcmd, BASCULA.Pcmd, BASCULA.Vcmd
		RecordOn
		Sleep 10
		Jog A1 100
		Sleep 24000
		RecordClose
		while 1
			Sleep 10
		end while		
	Detach A1


end program

 
sub SetAxis(ax as generic axis, byval vvv as double)

	dim omega	as double	= 15
	dim profile	as long		= -1

	with ax
    	Attach
	 		Slave = 0
      		En = 0
      		Sleep 10
      		Simulated = 1
      		Pfac = 1E6/5
      		Vfac = pfac/1000
      		Afac = vfac/1000 
      		Jfac = afac/1000 
      		Vmax = vvv
      		Amax = omega*Vmax
      		Dmax = omega*Vmax
      		Jmax = omega*Amax
      		Vcruise = Vmax
      		Acc = Amax
      		Dec = Dmax
      		Jerk = Jmax
      		prftype = profile
      		Smooth = -1
      		Vospd               = 2*Vmax 
      		VelocitySafetyLimit = 100*Vmax
      		Positionerrorsettle = 5
      		PeMax  = 10
      		PmaxEn = 1
      		PminEn = 1
      		Disp = 0
      		Absolute = 1
      		Positionrolloverenable = 0
      		Simulated = 1
      		Print "axis "; ElementName ; " set."
		Detach
	end with

end sub