HEADER
	DEFINE cmdDONE				0
	DEFINE cmdVELOCITY_ON		1
	DEFINE cmdVELOCITY_OFF		2
	DEFINE cmdHALT				3
	DEFINE cmdSTART				4
	DEFINE cmdPVT_INIT_TIME_ABS	5
	DEFINE cmdPVT_INIT_TIME_INC	6
	DEFINE cmdPVT1				7
	DEFINE cmdPVT2				8
	DEFINE cmdPVT3				9
	DEFINE cmdPVT				10
	DEFINE cmdABORT				11
	DEFINE cmdSTARTABORT		12

	DEFINE cmdSCOPEBUFFER		13
	DEFINE cmdSCOPEDATA			14
	DEFINE cmdSCOPESTATUS		15
	DEFINE cmdSCOPETRIG			16
	DEFINE cmdSCOPETRIGPERIOD	17
	DEFINE cmdDRIVEINFO			18
	DEFINE cmdLINEAR			19
	DEFINE cmdDATAACQ_TRIG		20
	DEFINE cmdDATAACQ_INP		21
	DEFINE cmdDATAACQ_ON		22
	DEFINE cmdDATAACQ_OFF		23
	DEFINE cmdDATAACQ_READ		24
	DEFINE cmdDOTRAJECTORY		25

	DEFINE cmdVar	45
	DEFINE iarg1Var	46
	DEFINE iarg2Var	47
	DEFINE iarg3Var	48
	DEFINE iarg4Var	49
	DEFINE darg1Var	1
	DEFINE darg2Var	2
	DEFINE darg3Var	3
	DEFINE darg4Var	4
	DEFINE darg5Var	5
	DEFINE darg6Var	6
	DEFINE darg7Var	7
	DEFINE darg8Var	8
	DEFINE darg9Var	9
	DEFINE numIArg	44
	DEFINE numDArg	43
	DEFINE pvtWaitMSVar 42

	' Numerical values for first arg to scopedata()
	DEFINE sd_PositionCommand	0
	DEFINE sd_PositionFeedback	1
	DEFINE sd_ExternalPosition	2
	DEFINE sd_AxisFault			3
	DEFINE sd_AxisStatus		4
	DEFINE sd_AnalogInput0		5
	DEFINE sd_AnalogInput1		6
	DEFINE sd_AnalogOutput0		7
	DEFINE sd_AnalogOutput1		8
	DEFINE sd_DigitalInput0		9
	DEFINE sd_DigitalInput1		10
	DEFINE sd_DigitalOutput0	11
	DEFINE sd_DigitalOutput1	12
	DEFINE sd_CurrentCommand	13
	DEFINE sd_CurrentFeedback	14
	DEFINE sd_OptionalData1		15
	DEFINE sd_OptionalData2		16
	DEFINE sd_ProgramCounter	17
 
END HEADER

PROGRAM

	DIM axis1Number AS Integer
	DIM axis2Number AS Integer
	DIM axis3Number AS Integer
	DIM axis4Number AS Integer
	DIM keepon as Integer
	DIM iarg as integer
	DIM pvtWaitMS as integer
	dim pos as double
	dim vel as double
	dim timeval as double
	dim timeIsAbs as integer
	dim timeInitialized as integer
	dim numpoints as integer
	dim i as integer

	wait mode nowait
	ABS
	keepon = 1
	pvtWaitMS = IGLOBAL(pvtWaitMSVar)
	IF pvtWaitMS < 1 THEN
		pvtWaitMS = 50
	END IF
	WHILE keepon
	    IF IGLOBAL(cmdVar) = cmdPVT1 THEN
			axis1Number = IGLOBAL(iarg1Var)
			pos = DGLOBAL(darg1Var)
			vel = DGLOBAL(darg2Var)
			timeval = DGLOBAL(darg3Var)
			if timeIsAbs and not timeInitialized then
				PVT INIT TIME ABS
				timeInitialized = 1
			end if
	        PVT @axis1Number pos, vel TIME timeval
		ELSEIF IGLOBAL(cmdVar) = cmdVELOCITY_ON THEN
	        VELOCITY ON
	    ELSEIF IGLOBAL(cmdVar) = cmdVELOCITY_OFF THEN
	        VELOCITY OFF
	    ELSEIF IGLOBAL(cmdVar) = cmdHALT THEN
	        HALT
		ELSEIF IGLOBAL(cmdVar) = cmdSTARTABORT THEN
			START
			ABORT @0
			acknowledgeall
			enable @0
			keepon = 0
	    ELSEIF IGLOBAL(cmdVar) = cmdSTART THEN
	        START
			keepon = 0
	    ELSEIF IGLOBAL(cmdVar) = cmdPVT  THEN
			axis1Number = IGLOBAL(iarg1Var)
			axis2Number = IGLOBAL(iarg2Var)
			axis3Number = IGLOBAL(iarg3Var)
			axis4Number = IGLOBAL(iarg4Var)
			if IGLOBAL(numIArg) = 1 then
				PVT @axis1Number DGLOBAL(darg1Var), DGLOBAL(darg2Var) TIME DGLOBAL(darg3Var)
			elseif IGLOBAL(numIArg) = 2 then
				PVT @axis1Number DGLOBAL(darg1Var), DGLOBAL(darg2Var) @axis2Number DGLOBAL(darg3Var), DGLOBAL(darg4Var) TIME DGLOBAL(darg5Var)
			elseif IGLOBAL(numIArg) = 3 then
				PVT @axis1Number DGLOBAL(darg1Var), DGLOBAL(darg2Var) @axis2Number DGLOBAL(darg3Var), DGLOBAL(darg4Var) @axis3Number DGLOBAL(darg5Var), DGLOBAL(darg6Var) TIME DGLOBAL(darg7Var)
			elseif IGLOBAL(numIArg) = 4 then
				PVT @axis1Number DGLOBAL(darg1Var), DGLOBAL(darg2Var) @axis2Number DGLOBAL(darg3Var), DGLOBAL(darg4Var) @axis3Number DGLOBAL(darg5Var), DGLOBAL(darg6Var) @axis4Number DGLOBAL(darg7Var), DGLOBAL(darg8Var) TIME DGLOBAL(darg9Var)
			end if
		ELSEIF IGLOBAL(cmdVar) = cmdPVT_INIT_TIME_ABS THEN
			'PVT INIT TIME ABS
			timeIsAbs = 1
			timeInitialized = 0
	    ELSEIF IGLOBAL(cmdVar) = cmdPVT_INIT_TIME_INC THEN
			timeIsAbs = 0
			PVT INIT TIME INC
	    ELSEIF IGLOBAL(cmdVar) = cmdABORT THEN
			axis1Number = IGLOBAL(iarg1Var)
			axis2Number = IGLOBAL(iarg2Var)
			axis3Number = IGLOBAL(iarg3Var)
			axis4Number = IGLOBAL(iarg4Var)
			if IGLOBAL(numIArg) = 1 then
	        	ABORT @axis1Number
			elseif IGLOBAL(numIArg) = 2 then
	        	ABORT @axis1Number @axis2Number
			elseif IGLOBAL(numIArg) = 3 then
				ABORT @axis1Number @axis2Number @axis3Number
			elseif IGLOBAL(numIArg) = 4 then
				ABORT @axis1Number @axis2Number @axis3Number @axis4Number
			end if
			keepon = 0
		ELSEIF IGLOBAL(cmdVar) = cmdSCOPEBUFFER THEN
			iarg = IGLOBAL(iarg1Var)
			SCOPEBUFFER iarg
		ELSEIF IGLOBAL(cmdVar) = cmdSCOPEDATA THEN
			iarg = IGLOBAL(iarg1Var)
			if iarg = sd_PositionCommand then
				DGLOBAL(darg1Var) = SCOPEDATA(PositionCommand, IGLOBAL(iarg2Var))
			elseif iarg = sd_PositionFeedback then
				DGLOBAL(darg1Var) = SCOPEDATA(PositionFeedback, IGLOBAL(iarg2Var))
			elseif iarg = sd_CurrentFeedback then
				DGLOBAL(darg1Var) = SCOPEDATA(CurrentFeedback, IGLOBAL(iarg2Var))
			else
				DGLOBAL(darg1Var) = -1.2345654321
			end if
		ELSEIF IGLOBAL(cmdVar) = cmdSCOPESTATUS THEN
			IGLOBAL(iarg1Var) = SCOPESTATUS(IGLOBAL(iarg1Var))
		ELSEIF IGLOBAL(cmdVar) = cmdSCOPETRIG THEN
			if IGLOBAL(iarg1Var) = 1 then
				SCOPETRIG STOP
			ELSE
				SCOPETRIG
			END IF
		ELSEIF IGLOBAL(cmdVar) = cmdSCOPETRIGPERIOD THEN
			iarg = IGLOBAL(iarg1Var)
			'SCOPETRIGPERIOD iarg
			SCOPETRIGPERIOD iarg*1.0
	' Not until version 4.03
	'	ELSEIF IGLOBAL(cmdVar) = cmdDRIVEINFO THEN
	'		IGLOBAL(iarg1Var) = DRIVEINFO(X, 59)
		ELSEIF IGLOBAL(cmdVar) = cmdLINEAR THEN
			axis1Number = IGLOBAL(iarg1Var)
			LINEAR @axis1Number DGLOBAL(darg1Var) F DGLOBAL(darg2Var)
		ELSEIF IGLOBAL(cmdVar) = cmdDATAACQ_TRIG THEN
			axis1Number = IGLOBAL(iarg1Var)
			DATAACQ @axis1Number TRIGGER IGLOBAL(iarg2Var)
		ELSEIF IGLOBAL(cmdVar) = cmdDATAACQ_INP THEN
			axis1Number = IGLOBAL(iarg1Var)
			DATAACQ @axis1Number INPUT IGLOBAL(iarg2Var)
		ELSEIF IGLOBAL(cmdVar) = cmdDATAACQ_ON THEN
			axis1Number = IGLOBAL(iarg1Var)
			DATAACQ @axis1Number ON IGLOBAL(iarg2Var)
		ELSEIF IGLOBAL(cmdVar) = cmdDATAACQ_OFF THEN
			axis1Number = IGLOBAL(iarg1Var)
			DATAACQ @axis1Number OFF
		ELSEIF IGLOBAL(cmdVar) = cmdDATAACQ_READ THEN
			axis1Number = IGLOBAL(iarg1Var)
			DATAACQ @axis1Number READ IGLOBAL(iarg2Var), IGLOBAL(iarg3Var)
		ELSEIF IGLOBAL(cmdVar) = cmdDOTRAJECTORY THEN
			axis1Number = IGLOBAL(iarg1Var)
			numpoints = IGLOBAL(iarg2Var)
			VELOCITY ON
			PVT INIT TIME ABS
			for i = 0 to numpoints-1 step 1
				pos = dglobal(3*i)
				vel = dglobal(3*i+1)
				timeVal = dglobal(3*i+2)
				if i = numpoints-2 then
					VELOCITY OFF
				end if
				PVT @axis1Number pos, vel time timeVal
				dwell pvtWaitMS/1000
			next i
		elseif IGLOBAL(cmdVar) = cmdDONE then
			' do nothing
	'	else
	'		IGLOBAL(cmdVar) = 1000
		END IF
		
		IF IGLOBAL(cmdVar) = cmdPVT1 THEN
			D = pvtWaitMS/1000
		ELSE
			D = .001
		END IF
		if IGLOBAL(cmdVar) > 0 then
	    	IGLOBAL(cmdVar) = -(IGLOBAL(cmdVar))
		end if
		DWELL D
	WEND

END PROGRAM
