Assembly: Quanser.Hardware.Hil (in Quanser.Hardware.Hil.dll)
Syntax
Visual Basic (Declaration) |
---|
Public Function TaskCreateEncoderReaderPwmWriter ( _ samplesInBuffer As Integer, _ inputChannels As Integer(), _ outputChannels As Integer() _ ) As Hil..::.Task |
C# |
---|
public Hil..::.Task TaskCreateEncoderReaderPwmWriter( int samplesInBuffer, int[] inputChannels, int[] outputChannels ) |
Visual C++ |
---|
public: Hil..::.Task^ TaskCreateEncoderReaderPwmWriter( int samplesInBuffer, array<int>^ inputChannels, array<int>^ outputChannels ) |
JavaScript |
---|
function taskCreateEncoderReaderPwmWriter(samplesInBuffer, inputChannels, outputChannels); |
Parameters
- samplesInBuffer
- Type: System..::.Int32
The number of samples in the task buffers. The ReadEncoderWritePwm(Int32, array<Int32>[]()[], array<Double>[]()[]) method cannot read or write more samples than this in a single call. If the task input buffer overflows because ReadEncoderWritePwm(Int32, array<Int32>[]()[], array<Double>[]()[]) has not been called in time to remove the data from the task buffer then the next call to ReadEncoderWritePwm(Int32, array<Int32>[]()[], array<Double>[]()[]) will throw an HilException exception. Likewise, if the task output buffer underflows because ReadEncoderWritePwm(Int32, array<Int32>[]()[], array<Double>[]()[]) has not been called in time to add data to the task output buffer then the next call to ReadEncoderWritePwm(Int32, array<Int32>[]()[], array<Double>[]()[]) will also throw an HilException exception. See Hil..::.Task for more information on task buffers.
- inputChannels
- Type: array<
System..::.Int32
>[]()[]
An array containing the numbers of the encoder input channels to be read by the task. Channel numbers are zero-based. Thus, channel 0 is the first channel, channel 1 the second channel, etc.
Select a board type from the list for board-specific details: .
- outputChannels
- Type: array<
System..::.Int32
>[]()[]
An array containing the numbers of the PWM output channels to be written by the task. Channel numbers are zero-based. Thus, channel 0 is the first channel, channel 1 the second channel, etc.
Select a board type from the list for board-specific details: .
Return Value
Returns a Hil..::.Task interface for manipulating the task, including starting and stopping the task, and for reading and writing the samples for the task.Remarks
The TaskCreateEncoderReaderPwmWriter method creates a task for reading from the specified encoder input channels and writing to the specified PWM output channels at the same time. The task allows other operations to be performed while the encoder inputs are being read and the PWM outputs are being written "in the background". The data read from the encoder inputs is stored in an internal circular "task input buffer". The data written to the PWM outputs is read from an internal circular "task output buffer". The application may read the data from the task input buffer and write data to the task output buffer at any time using the ReadEncoderWritePwm(Int32, array<Int32>[]()[], array<Double>[]()[]) method. Data may also be read separately from the task input buffer using the ReadAnalog(Int32, array<Double>[]()[]) method, and data may also be written into the task output buffer using the WritePwm(Int32, array<Double>[]()[]) method. The size of this task buffer is determined by the samplesInBuffer parameter.
The WritePwm(Int32, array<Double>[]()[]) method is typically called prior to starting the task in order to put the initial samples in the the task output buffer. After the task is started, ReadEncoderWritePwm(Int32, array<Int32>[]()[], array<Double>[]()[]) is typically called to read the encoder input values from the task input buffer and to put more data into the task output buffer for the PWM outputs.
The task does not actually start reading from the encoder inputs and storing the data in the task input buffer or reading the data from the task output buffer and writing it to the PWM outputs until the Start(Hil..::.Clock, Double, Int32) method is called. In order for data to be available in the task buffer as soon as the task starts, store data in the buffer using WritePwm(Int32, array<Double>[]()[]) prior to starting the task.
Each sampling instant, the task first reads from the encoder input channels selected and stores the data in the task input buffer. Then, in the same sampling instant, the task extracts one sample from the task output buffer and writes to the selected PWM output channels. This synchronization of input and output is particularly useful for system identification because the time at which a sample is read and a sample is written is known, so no postprocessing is required to determine the response delay in the system been identified.
Since the task writes to the PWM outputs at the sampling rate specified when the task is started, it will be reading data from the task output buffer at that rate. Thus, ReadEncoderWritePwm(Int32, array<Int32>[]()[], array<Double>[]()[]) or WritePwm(Int32, array<Double>[]()[]) must be called to add more data to the task output buffer before all the data in the buffer is depleted. Otherwise the task will have no data to write to the PWM outputs and will return with an HilException exception the next time ReadEncoderWritePwm(Int32, array<Int32>[]()[], array<Double>[]()[]) or WritePwm(Int32, array<Double>[]()[]) is called. See Hil..::.Task for more information on tasks.
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
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
Exception | Condition |
---|---|
Quanser.Hardware..::.HilException | If the task cannot be created then an exception is thrown. This situtation typically arises if the board does not support encoder inputs or PWM outputs or tasks. |