Reads the specified number of samples from the task input buffer and writes the same number of samples to the task output buffer of an encoder reader-PWM writer task.

Namespace:  Quanser.Hardware
Assembly:  Quanser.Hardware.Hil (in Quanser.Hardware.Hil.dll)

Syntax

Visual Basic (Declaration)
Function ReadEncoderWritePwm ( _
	numSamples As Integer, _
	encoderInputBuffer As Integer(), _
	pwmOutputBuffer As Double() _
) As Integer
C#
int ReadEncoderWritePwm(
	int numSamples,
	int[] encoderInputBuffer,
	double[] pwmOutputBuffer
)
Visual C++
int ReadEncoderWritePwm(
	int numSamples, 
	array<int>^ encoderInputBuffer, 
	array<double>^ pwmOutputBuffer
)
JavaScript
function readEncoderWritePwm(numSamples, encoderInputBuffer, pwmOutputBuffer);

Parameters

numSamples
Type: System..::.Int32
The number of samples to read from the task input buffer and to write to the task output buffer. Each "sample" consists of all the encoder input channels and PWM output channels specified when the task was created using TaskCreateEncoderReaderPwmWriter(Int32, array<Int32>[]()[], array<Int32>[]()[]). For example, if numSamples is 5 and the task is configured to read two channels and write three channels, then the input buffer must contain at least 10 elements and the output buffer must contain at least 15 elements.
encoderInputBuffer
Type: array< System..::.Int32 >[]()[]

An array for receiving the count values read from the encoder inputs. The array must contain numInputChannels * numSamples elements, where numInputChannels is the number of input channels specified when the task was created. The array is organized as a linear array of samples, with each sample consisting of a group of channels. For example, if encoder input channels 0, 1 and 3 are being read, than the data appears in the array as follows, where the numbers correspond to channel numbers:

0 1 3 0 1 3 ...
pwmOutputBuffer
Type: array< System..::.Double >[]()[]

An array containing the values to write to the PWM outputs. How these values are interpreted depends on the PWM mode. The PWM mode is configured using the SetPwmMode(array<Int32>[]()[], array<Hil..::.PwmMode>[]()[]) method. The array must contain numOutputChannels * numSamples elements, where numOutputChannels is the number of output channels specified when the task was created. The array must be organized as a linear array of samples, with each sample consisting of a group of channels. For example, if PWM output channels 0, 1 and 3 are being written, than the data must appear in the array as follows, where the numbers correspond to channel numbers:

0 1 3 0 1 3 ...

Return Value

The return value is the number of samples read from the task input buffer and written to the task output buffer. This value may be less than the requested number of samples (including 0) if the task is stopped or has finished processing the total number of samples indicated in the call to Start(Hil..::.Clock, Double, Int32).

Note that successive calls to ReadEncoderWritePwm can never read more samples in total than the total number of samples specified in the call to Start(Hil..::.Clock, Double, Int32). It is possible to write more samples than this to the task output buffer using WritePwm(Int32, array<Double>[]()[]) but only the number of samples specified in Start(Hil..::.Clock, Double, Int32) will actually be processed and written to the hardware.

Remarks

The ReadEncoderWritePwm method reads the specified number of samples from the task input buffer and writes the same number of samples to the task output buffer of a task created using TaskCreateEncoderReaderPwmWriter(Int32, array<Int32>[]()[], array<Int32>[]()[]). If the requested number of samples is not yet available in the task input buffer then this method will block until the specified number of samples is available. Also, if there's not enough space in the task output buffer, then this method will block until there is space in the task output buffer or the task stops. Since the task both reads the encoder inputs and stores the data in the task input buffer, and removes data from the task output buffer and writes it to the PWM outputs at the sampling rate specified in the call to Start(Hil..::.Clock, Double, Int32), this method will never block for longer than the given number of samples times the sampling period.

Note that this method first reads as many samples as it can from the task input buffer and writes as many samples as it can to the task output buffer without blocking. Only then does it block if necessary to complete the operation. Thus, it will put as much data as it can in the task output buffer before waiting for the task input buffer to be filled with the requisite number of samples. These semantics allow the PWM outputs to be updated without interruption, even though the method waits for the encoder inputs to be read.

Because this method blocks until enough input data is available and the task input buffer is filled at a given sampling rate, calling this function synchronizes the caller to that sampling rate (provided the task input buffer is not being filled faster than we can read the data). Thus, the ReadEncoderWritePwm method may be used to implement system identification, synchronous data streaming and other operations requiring a fixed sampling rate. For data streaming, the numSamples parameter is typically half the number of samples in the task buffers to implement double-buffering.

The task output buffer is depleted at the same sampling rate. Hence, data must be written to the task buffer before the task buffer is completely depleted or else the next attempt to write to the task buffer will throw a HilExceptionBufferOverflow exception. As a result, WritePwm(Int32, array<Double>[]()[]) should be used to put data into the task output buffer prior to starting the task!.

Internally, samples are always read from the hardware prior to writing to the hardware. Hence, in a loopback test, the values read always reflect the samples written in the previous sampling instant. These semantics help to ensure that the inputs are not sampled while the outputs are transitioning to their new states.

The interpretation of the PWM samples to be written depends upon the PWM mode. Typically the data is interpreted as a duty cycle, in which a magnitude of 0.0 denotes a 0% duty cycle and magnitude of 1.0 indicates a 100% duty cycle. The sign determines the polarity of the output for those boards supporting bidirectional PWM outputs. However, other PWM modes are possible with some boards. Refer to the SetPwmMode(array<Int32>[]()[], array<Hil..::.PwmMode>[]()[]) method for details.

Examples

This example illustrates how to read encoder inputs and write PWM outputs at the same time using a task. The task reads encoder input channels 2-3 while writing to PWM output channels 0-1 every millisecond using a hardware clock. The data may be processed each sampling instant. It runs for 5 seconds before stopping. Exceptions are ignored for simplicity.
C# Copy Code
int [] inputChannels      = { 2, 3 };
int [] outputChannels     = { 0, 1 };
double frequency          = 1000;
int    samples            = 5000;
int    samplesInBuffer    = frequency;
int    samplesToReadWrite = 1;

int []    inputBuffer  = new int    [samplesToReadWrite * inputChannels.length];
double [] outputBuffer = new double [samplesToReadWrite * outputChannels.length];
Hil.Task task;

/* ... fill output buffer with samplesToReadWrite samples to write ... */

task = card.TaskCreateEncoderReaderPwmWriter(samplesInBuffer, inputChannels, outputChannels);

/* Preload task output buffer with first samplesToReadWrite samples prior to starting task */
task.WritePwm(samplesToReadWrite, outputBuffer);

/* Start task */
task.Start(Hil.Clock.Hardware0, frequency, samples);
for (int index = 0; index < samples; index += samplesToReadWrite) {
    /* ... fill output buffer with next samplesToReadWrite samples to write ... */

    /* 
        Block (if necessary) waiting to read next samplesToReadWrite samples from
        the hardware and for space in the task output buffer.
    */
    task.ReadEncoderWritePwm(samplesToReadWrite, inputBuffer, outputBuffer);

    /* ... process samplesToReadWrite samples read ... */
}
task.Stop();
Visual Basic Copy Code
Dim inputChannels() As Integer = {2, 3}
Dim outputChannels() As Integer = {0, 1}
Dim frequency as Double = 1000
Dim samples As Integer = 5000
Dim samplesInBuffer As Integer = frequency
Dim samplesToReadWrite As Integer = 1

Dim inputBuffer(samplesToReadWrite * inputChannels.Length - 1) As Integer
Dim outputBuffer(samplesToReadWrite * outputChannels.Length - 1) As Double
Dim task As Hil.Task
Dim index As Integer

' ... fill output buffer with samplesToReadWrite samples to write ...

' Create task
task = card.TaskCreateEncoderReaderPwmWriter(samplesInBuffer, inputChannels, outputChannels)

' Preload task buffer with first samplesToReadWrite samples prior to starting task
task.WritePwm(samplesToWrite, outputBuffer)

' Start task
task.Start(Hil.Clock.Hardware0, frequency, samples)
For index = 0 To samples - 1 Step samplesToReadWrite
    ' ... fill buffer with next samplesToReadWrite samples to write ...

    ' Block (if necessary) waiting to read next samplesToReadWrite samples
    ' from the hardware and for space in the task output buffer.
    task.ReadEncoderWritePwm(samplesToReadWrite, inputBuffer, outputBuffer)

    ' ... process samplesToReadWrite samples read ...
Next
task.Stop()
Visual C++ Copy Code
array<int>^ inputChannels      = { 2, 3 };
array<int>^ outputChannels     = { 0, 1 };
double      frequency          = 1000;
int         samples            = 5000;
int         samplesInBuffer    = frequency;
int         samplesToReadWrite = 1;

array<int>^    inputBuffer  = gcnew array<int>(samplesToReadWrite * inputChannels->Length);
array<double>^ outputBuffer = gcnew array<double>(samplesToReadWrite * outputChannels->Length);
Hil::Task^ task;

/* ... fill output buffer with samplesToReadWrite samples to write ... */

/* Create task */
task = card->TaskCreateEncoderReaderPwmWriter(samplesInBuffer, channels);

/* Preload task buffer with first samplesToWrite samples prior to starting task */
task->WritePwm(samplesToWrite, buffer);

/* Start task */
task->Start(Hil::Clock::Hardware0, frequency, samples);
for (int index = 0; index < samples; index += samplesToReadWrite) {
    /* ... fill output buffer with next samplesToReadWrite samples to write ... */

    /* 
        Block (if necessary) waiting to read next samplesToReadWrite samples from
        the hardware and for space in the task output buffer.
    */
    task->ReadEncoderWritePwm(samplesToReadWrite, inputBuffer, outputBuffer);

    /* ... process samplesToReadWrite samples read ... */
}
task->Stop();

Exceptions

ExceptionCondition
Quanser.Hardware..::.HilException If the read or write cannot be performed then an exception is thrown. This situtation typically arises if the task output buffer underflowed (ran out of data) or the task input buffer overflowed (ran out of space) after the last call to this method.

See Also