hil_task_read_other_write_other End of trail navigation bar

hil_task_read_write

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 reader-writer task.

Description

The hil_task_read_write function 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 hil_task_create_reader_writer. If the requested number of samples is not yet available in the task input buffer then this function will block until the specified number of samples is available. Also, if there's not enough space in the task output buffer, then this function will block until there is space in the task output buffer or the task stops. Since the task both reads the inputs and stores the data in the task input buffer, and removes data from the task output buffer and writes it to the outputs at the sampling rate specified in the call to hil_task_start, this function will never block for longer than the given number of samples times the sampling period.

Note that this function 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 outputs to be updated without interruption, even though the function waits for the inputs to be read.

Because this function 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 hil_task_read_write function may be used to implement system identification, synchronous data streaming and other operations requiring a fixed sampling rate. For data streaming, the num_samples parameter is typically half the number of samples in the task buffers to implement double-buffering. Before starting the task, the directions of the digital I/O lines should be set using the hil_set_digital_directions function.

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 return with a QERR_BUFFER_OVERFLOW error. As a result, hil_task_write should be used to put data into the task output buffer prior to starting the task!

Warning Many cards allow the digital I/O lines to be programmed as inputs or outputs. The digital I/O lines are configured as inputs or outputs using the hil_set_digital_directions function. All the channels which will be used as digital inputs or outputs must be configured accordingly using this function. Failure to configure the digital I/O may result in the hil_task_read_write function failing to read or write the digital I/O as expected.

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 hil_set_pwm_mode function for details.

Prototype

t_error 
hil_task_read_write(t_task task, t_uint32 num_samples,
                    t_double  analog_input_buffer[],
                    t_int32   encoder_input_buffer[],
                    t_boolean digital_input_buffer[],
                    t_double  other_input_buffer[],
                    const t_double  analog_output_buffer[],
                    const t_double  pwm_output_buffer[],
                    const t_boolean digital_output_buffer[],
                    const t_double  other_output_buffer[]);
    

Parameters

t_task task

A handle to the task, as returned by one of the task creation functions.

t_uint32 num_samples

The number of samples to read from the task input buffer and written to the task output buffer. Each "sample" consists of all the input channels and output channels specified when the task was created using hil_task_create_reader_writer . For example, if num_samples is 5 and the task is configured to read 2 encoder channels and write 3 analog channels, then the encoder input buffer must contain at least 10 elements and the analog output buffer must contain at least 15 elements.

t_double [] analog_input_buffer

An array for receiving the voltage values read from the analog inputs. The array must contain num_analog_input_channels * num_samples elements, where num_analog_input_channels 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 analog 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

...

This ordering is equivalent to defining the buffer as:

t_double analog_input_buffer[num_samples][num_analog_input_channels];
            

If the buffer is defined this way then pass the buffer as the analog_input_buffer argument using the syntax: &analog_input_buffer[0][0].

If no analog input channels were specified in the call to hil_task_create_reader_writer then this parameter may be set to NULL.

t_int32 [] encoder_input_buffer

An array for receiving the values read from the encoder inputs. The array must contain num_encoder_input_channels * num_samples elements, where num_encoder_input_channels 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

...

This ordering is equivalent to defining the buffer as:

t_int32 encoder_input_buffer[num_samples][num_encoder_input_channels];
            

If the buffer is defined this way then pass the buffer as the encoder_input_buffer argument using the syntax: &encoder_input_buffer[0][0].

If no encoder input channels were specified in the call to hil_task_create_reader_writer then this parameter may be set to NULL.

t_boolean [] digital_input_buffer

An array for receiving the values read from the digital inputs. The array must contain num_digital_input_channels * num_samples elements, where num_digital_input_channels 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 digital 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

...

This ordering is equivalent to defining the buffer as:

t_boolean digital_input_buffer[num_samples][num_digital_input_channels];
            

If the buffer is defined this way then pass the buffer as the digital_input_buffer argument using the syntax: &digital_input_buffer[0][0].

If no digital input channels were specified in the call to hil_task_create_reader_writer then this parameter may be set to NULL.

t_double [] other_input_buffer

An array for receiving the values read from the other inputs. The array must contain num_other_input_channels * num_samples elements, where num_other_input_channels 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 other 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

...

This ordering is equivalent to defining the buffer as:

t_double other_input_buffer[num_samples][num_other_input_channels];
            

If the buffer is defined this way then pass the buffer as the other_input_buffer argument using the syntax: &other_input_buffer[0][0].

If no other input channels were specified in the call to hil_task_create_reader_writer then this parameter may be set to NULL.

const t_double [] analog_output_buffer

An array containing the voltage values to write to the analog outputs. The array must contain num_analog_output_channels * num_samples elements, where num_analog_output_channels 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 analog 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

...

This ordering is equivalent to defining the buffer as:

t_double analog_output_buffer[num_samples][num_analog_output_channels];
            

If the buffer is defined this way then pass the buffer as the analog_output_buffer argument using the syntax: &analog_output_buffer[0][0].

If no analog output channels were specified in the call to hil_task_create_reader_writer then this parameter may be set to NULL.

const t_double [] pwm_output_buffer

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 hil_set_pwm_mode function. The array must contain num_pwm_output_channels * num_samples elements, where num_pwm_output_channels 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

...

This ordering is equivalent to defining the buffer as:

t_double pwm_output_buffer[num_samples][num_pwm_output_channels];
            

If the buffer is defined this way then pass the buffer as the pwm_output_buffer argument using the syntax: &pwm_output_buffer[0][0].

If no PWM output channels were specified in the call to hil_task_create_reader_writer then this parameter may be set to NULL.

const t_boolean [] digital_output_buffer

An array containing the values to write to the digital outputs. The array must contain num_digital_output_channels * num_samples elements, where num_digital_output_channels 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 digital 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

...

This ordering is equivalent to defining the buffer as:

t_boolean digital_output_buffer[num_samples][num_digital_output_channels];
            

If the buffer is defined this way then pass the buffer as the digital_output_buffer argument using the syntax: &digital_output_buffer[0][0].

If no digital output channels were specified in the call to hil_task_create_reader_writer then this parameter may be set to NULL.

const t_double [] other_output_buffer

An array containing the values to write to the other outputs. The array must contain num_other_output_channels * num_samples elements, where num_other_output_channels 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 other 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

...

This ordering is equivalent to defining the buffer as:

t_double other_output_buffer[num_samples][num_other_output_channels];
            

If the buffer is defined this way then pass the buffer as the other_output_buffer argument using the syntax: &other_output_buffer[0][0].

If no other output channels were specified in the call to hil_task_create_reader_writer then this parameter may be set to NULL.

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 hil_task_start. Otherwise a negative error code is returned. Error codes are defined in quanser_errors.h. A suitable error message may be retrieved using msg_get_error_message.

Note that successive calls to hil_task_read_write can never read more samples in total than the total number of samples specified in hil_task_start. It is possible to write more samples than this to the task output buffer using hil_task_write but only the number of samples specified in hil_task_start will actually be processed and written to the hardware.

Error codes

QERR_HIL_TASK_READ_WRITE_NOT_SUPPORTED

This function is not supported by the board-specific HIL driver for this board type.

QERR_INVALID_TASK_HANDLE

An invalid task handle was passed as an argument. Once a task has been deleted using hil_task_delete the task handle is invalid.

QERR_INVALID_OPERATION_HANDLE

An invalid operation handle was passed as an argument to the board-specific HIL driver. Once a task has been deleted using hil_task_delete the operation handle is invalid.

QERR_TOO_MANY_SAMPLES_FOR_BUFFER

The number of samples requested in the read or write operation is more than the number of samples being buffered by the task. Increase the buffer size for the task or read or write fewer samples.

QERR_MISSING_ANALOG_INPUT_BUFFER

Analog input channels have been specified but no analog input buffer has been provided for the read operation.

QERR_MISSING_ENCODER_INPUT_BUFFER

Encoder input channels have been specified but no encoder input buffer has been provided for the read operation.

QERR_MISSING_DIGITAL_INPUT_BUFFER

Digital input channels have been specified but no digital input buffer has been provided for the read operation.

QERR_MISSING_OTHER_INPUT_BUFFER

Other input channels have been specified but no other input buffer has been provided for the read operation.

QERR_MISSING_ANALOG_OUTPUT_BUFFER

Analog output channels have been specified but no analog output buffer has been provided for the write operation.

QERR_MISSING_PWM_OUTPUT_BUFFER

PWM output channels have been specified but no PWM output buffer has been provided for the write operation.

QERR_MISSING_DIGITAL_OUTPUT_BUFFER

Digital output channels have been specified but no digital output buffer has been provided for the write operation.

QERR_MISSING_OTHER_OUTPUT_BUFFER

Other output channels have been specified but no other output buffer has been provided for the write operation.

QERR_READING_FROM_WRITE_ONLY_TASK

An attempt was made to read from a write-only task.

QERR_WRITING_TO_READ_ONLY_TASK

An attempt was made to write to a read-only task.

QERR_BUFFER_OVERFLOW

For a read operation, the buffer has overflowed. For a write operation, there is no more data left in the buffer. The sampling frequency is too fast for the rate at which data is being read from or written to the buffer.

QERR_DRIVER_INCOMPATIBLE_WITH_BOARD_DLL

The board-specific HIL driver passed an invalid parameter to the operating system specific kernel-level driver for the board. The board-specific HIL driver is likely not compatible with the operating system specific kernel-level driver for the board. Make sure both are up-to-date and compatible versions.

QERR_INTERNAL_BUFFER_TOO_SMALL

The board-specific HIL driver used an internal buffer that was too small for the operating system specific kernel-level driver for the board. The board-specific HIL driver is likely not compatible with the operating system specific kernel-level driver for the board. Make sure both are up-to-date and compatible versions.

QERR_OUT_OF_REQUIRED_SYSTEM_RESOURCES

There are not enough system resources to perform the requested operation. Try rebooting, requesting fewer samples, or adding more memory to your machine.

QERR_OUT_OF_MEMORY

There is not enough memory to perform the operation.

Requirements

Include Files

Libraries

hil.h

hil.lib;quanser_runtime.lib;quanser_common.lib

Examples


/*
* Reads 5000 samples at 1 kHz from the analog input channels 1 and 3 and encoder input channels 0 and 2
* while writing at the same time to analog output channels 2 and 3 and PWM output channel 0,
* using SYSTEM_CLOCK_1. Return values are ignored for simplicity.
*/

t_uint32 analog_input_channels[]  = { 1, 3 };
t_uint32 encoder_input_channels[] = { 0, 2 };
t_uint32 analog_output_channels[] = { 2, 3 };
t_uint32 pwm_output_channels[]    = { 0 };
t_double frequency                = 1000;
t_uint32 samples                  = 5000;
t_uint32 samples_in_buffer        = frequency;
t_uint32 samples_to_read_write    = 1;

static t_double analog_input_buffer[2];
static t_int32  encoder_input_buffer[2];
static t_double analog_output_buffer[2];
static t_double pwm_output_buffer[1];
t_task task;

/* Fill output buffer */
...
hil_task_create_reader_writer(board, samples_in_buffer,
     analog_input_channels,  ARRAY_LENGTH(analog_input_channels), 
     encoder_input_channels, ARRAY_LENGTH(encoder_input_channels), 
     NULL,                   0,
     NULL,                   0,
     analog_output_channels, ARRAY_LENGTH(analog_output_channels), 
     pwm_output_channels,    ARRAY_LENGTH(pwm_output_channels), 
     NULL,                   0,
     NULL,                   0,
     &task);
hil_task_write(task, samples_to_read_write, /* pre-fill the task output buffer prior to starting the task */
               analog_output_buffer, pwm_output_buffer, NULL, NULL); 
hil_task_start(task, SYSTEM_CLOCK_1, frequency, samples);
for (int index = 0; index < samples; index += samples_to_read_write) {
    /* Fill output buffer */
    ...
    hil_task_read_write(task, samples_to_write,                                 /* waits for data to be read from hardware */
                        analog_input_buffer,  encoder_input_buffer, NULL, NULL  /* and for space in the task output buffer */
                        analog_output_buffer, pwm_output_buffer,    NULL, NULL);
    ...
}
hil_task_stop(task);
hil_task_delete(task);
    

 

navigation bar