PSO Fixed Distance Pulse Output (2D)

Demonstrates generating a sequence of pulses each time the vector sum of the tracked axes moves a fixed distance.

// PSO Fixed Distance Pulse Output (2D) Example:
// Demonstrates generating a sequence of pulses each time the vector sum of the tracked axes moves a fixed distance.
// It also demonstrates configuring the SYNC ports to send feedback signals from the second axis to the first (PSO) axis.
// To help you visualize the behavior of the PSO output generated by this configuration, the pulse times used in this program are
// longer than would be typical and this program triggers data collection. You can use the Data Visualizer to collect the
// "OutputActive" bit of the "Pso Status" signal (for $psoAxis) and the Position Feedback signal (for $motionAxes). To see the
// complete output from this program, you must collect data at a period of 1 msec and a time to collect of at least 4000 msec.

program

	var $psoAxis as axis
	var $motionAxes[] as axis = [X, Y]

	// Set the PSO axis to the first motion axis.
	$psoAxis = $motionAxes[0]

	// Set incremental mode.
	SetupTaskTargetMode(TargetMode.Incremental)

	// Enable and home the axis.
	Enable($motionAxes)
	Home($motionAxes)

	// Reset all PSO configuration.
	PsoReset($psoAxis)

	// Configure SYNC port A of the second axis to echo the primary feedback.
	DriveEncoderOutputConfigureInput($motionAxes[1], EncoderOutputChannel.SyncPortA, EncoderInputChannel.PrimaryEncoder)
	DriveEncoderOutputOn($motionAxes[1], EncoderOutputChannel.SyncPortA, EncoderOutputMode.Quadrature)

	// Configure the distance module to track:
	// Primary feedback on the first axis
	// SYNC Port A of the first axis.
	PsoDistanceConfigureInputs($psoAxis, [PsoDistanceInput.XC4PrimaryFeedback, PsoDistanceInput.XC4SyncPortA])

	// Configure the distance module to generate an event every 10 units. This assumes that the counts per unit of both motion axes is the same.
	// To account for differences between position feedback and emulated quadrature resolution caused by the EmulatedQuadratureDivider parameter, divide the distance by the value of this parameter on the first axis.
	PsoDistanceConfigureFixedDistance($psoAxis, Round(UnitsToCounts($motionAxes[0], 10) / ParameterGetAxisValue($motionAxes[0], AxisParameter.PrimaryEmulatedQuadratureDivider)))

	// Enable the distance counter.
	PsoDistanceCounterOn($psoAxis)

	// Enable distance events.
	PsoDistanceEventsOn($psoAxis)

	// Configure waveform module in pulse mode: 20 ms total time, 10 ms on time (50% duty cycle), 3 periods per event.
	PsoWaveformConfigureMode($psoAxis, PsoWaveformMode.Pulse)
	PsoWaveformConfigurePulseFixedTotalTime($psoAxis, 20000)
	PsoWaveformConfigurePulseFixedOnTime($psoAxis, 10000)
	PsoWaveformConfigurePulseFixedCount($psoAxis, 3)
	PsoWaveformApplyPulseConfiguration($psoAxis)
	PsoWaveformOn($psoAxis)

	// Select the waveform module output as the PSO output source.
	PsoOutputConfigureSource($psoAxis, PsoOutputSource.Waveform)

	// Trigger Data Collection.
	AppDataCollectionSnapshot()

	// Move the axes a vector distance of approximately 44 units, expecting 4 events.
	MoveLinear($motionAxes, [20, 40])

	// Disable the axes.
	Disable($motionAxes)

	// Stop Data Collection.
	AppDataCollectionStop()

end