hil_task_create_other_reader_other_writer hil_task_read_analog_write_analog navigation bar

Table of Contents

hil_task_create_reader_writer

Creates a task for reading from analog, encoder, digital and/or other inputs and writing to analog, PWM, digital and/or other outputs at the same time.

Description

The hil_task_create_reader_writer function creates a task for reading from the specified input channels and writing to the specified output channels at the same time. The task allows other operations to be performed while the inputs are being read and the outputs are being written "in the background". The data read from the inputs is stored in an internal circular "task input buffer". The data written to the 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 hil_task_read_write function. Data may also be read separately from the task input buffer using the hil_task_read function, and data may also be written into the task output buffer using the hil_task_write function. The size of both the task input buffer and the task output buffer is determined by the samples_in_buffer parameter.

The hil_task_write function 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, hil_task_read_write is typically called to read the input values from the task input buffer and to put more data into the task output buffer for the outputs. Before starting the task, the directions of the digital I/O lines should be set using the hil_set_digital_directions function.

The task does not actually start reading from the inputs and storing the data in the task input buffer or reading the data from the task output buffer and writing it to the outputs until the hil_task_start function 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 hil_task_write prior to starting the task.

Each sampling instant, the task first reads from the 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 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 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, hil_task_read_write or hil_task_write 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 outputs and will return with a QERR_BUFFER_OVERFLOW error the next time hil_task_read_write or hil_task_write is called.

The task must be deleted when it is no longer in use using the hil_task_delete function, in order to free the system and hardware resources used by the task. See Tasks for more information on tasks.

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.

Prototype

t_error hil_task_create_reader_writer(t_card card, t_uint32 samples_in_buffer,
                                      const t_uint32 analog_input_channels[],  t_uint32 num_analog_input_channels,
                                      const t_uint32 encoder_input_channels[], t_uint32 num_encoder_input_channels,
                                      const t_uint32 digital_input_lines[],    t_uint32 num_digital_input_lines,
                                      const t_uint32 other_input_channels[],   t_uint32 num_other_input_channels,
                                      const t_uint32 analog_output_channels[],  t_uint32 num_analog_output_channels,
                                      const t_uint32 pwm_output_channels[],     t_uint32 num_pwm_output_channels,
                                      const t_uint32 digital_output_lines[],    t_uint32 num_digital_output_lines,
                                      const t_uint32 other_output_channels[],   t_uint32 num_other_output_channels,
                                      t_task *task);
    

Parameters

t_card card

A handle to the board, as returned by hil_open.

t_uint32 samples_in_buffer

The number of samples in the task buffers. The hil_task_read_write function cannot read or write more samples than this in a single call. If the task input buffer overflows because hil_task_read_write has not been called in time to remove data from the task input buffer then the next call to hil_task_read_write will return an HIL_BUFFER_OVERFLOW error. Likewise, if the task output buffer underflows because hil_task_read_write has not been called in time to add data to the task output buffer then the next call to hil_task_read_write will also return an HIL_BUFFER_OVERFLOW error. See Tasks for more information on task buffers.

const t_uint32 [] analog_input_channels

An array containing the channel numbers of the analog inputs to be read by the task.

Select a board type from the list for board-specific details: .

If no analog input channels are required then this parameter may be NULL. In this case, num_analog_input_channels must be zero.

t_uint32 num_analog_input_channels

t_uint32

The number of channels specified in the analog_input_channels array. This parameter may be zero.

const t_uint32 [] encoder_input_channels

An array containing the channel numbers of the encoder inputs to be read by the task.

Select a board type from the list for board-specific details: .

If no encoder input channels are required then this parameter may be NULL. In this case, num_encoder_input_channels must be zero.

t_uint32 num_encoder_input_channels

The number of channels specified in the encoder_input_channels array. This parameter may be zero.

const t_uint32 [] digital_input_channels

An array containing the channel numbers of the digital inputs to be read by the task.

Select a board type from the list for board-specific details: .

If no digital input channels are required then this parameter may be NULL. In this case, num_digital_input_channels must be zero.

t_uint32 num_digital_input_channels

The number of channels specified in the digital_input_channels array. This parameter may be zero.

const t_uint32 [] other_input_channels

An array containing the channel numbers of the other inputs to be read by the task.

Select a board type from the list for board-specific details: .

If no other input channels are required then this parameter may be NULL. In this case, num_other_input_channels must be zero.

t_uint32 num_other_input_channels

The number of channels specified in the other_input_channels array. This parameter may be zero.

const t_uint32 [] analog_output_channels

An array containing the channel numbers of the analog outputs to be written to by the task.

Select a board type from the list for board-specific details: .

If no analog output channels are required then this parameter may be NULL. In this case, num_analog_output_channels must be zero.

t_uint32 num_analog_output_channels

The number of channels specified in the analog_output_channels array. This parameter may be zero.

const t_uint32 [] pwm_output_channels

An array containing the channel numbers of the PWM outputs to be written to by the task.

Select a board type from the list for board-specific details: .

If no PWM output channels are required then this parameter may be NULL. In this case, num_pwm_output_channels must be zero.

t_uint32 num_pwm_output_channels

The number of channels specified in the pwm_output_channels array. This parameter may be zero.

const t_uint32 [] digital_output_channels

An array containing the channel numbers of the digital outputs to be written to by the task.

Select a board type from the list for board-specific details: .

If no digital output channels are required then this parameter may be NULL. In this case, num_digital_output_channels must be zero.

t_uint32 num_digital_output_channels

The number of channels specified in the digital_output_channels array. This parameter may be zero.

const t_uint32 [] other_output_channels

An array containing the channel numbers of the other outputs to be written to by the task.

Select a board type from the list for board-specific details: .

If no other output channels are required then this parameter may be NULL. In this case, num_other_output_channels must be zero.

t_uint32 num_other_output_channels

The number of channels specified in the other_output_channels array. This parameter may be zero.

t_task * task

A handle to the task is returned in the t_task variable passed in this parameter. This argument cannot be NULL. Pass the address of a variable of type t_task.

Return value

The return value is 0 if the task is created successfully. 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 .

Error codes

QERR_HIL_TASK_CREATE_READER_WRITER_NOT_SUPPORTED

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

QERR_INVALID_CARD_HANDLE

An invalid card handle was passed as an argument. Once a card has been closed using hil_close the card handle is invalid.

QERR_TASK_ARGUMENT_IS_NULL

The task argument is NULL. A pointer to a t_task variable must be supplied because this variable must be passed to the other HIL task functions to refer to the task.

QERR_MISSING_ANALOG_INPUTS

The analog input channels argument is NULL when the number of analog inputs specified is non-zero.

QERR_TOO_MANY_ANALOG_INPUT_CHANNELS

Too many analog input channels were specified.

QERR_INVALID_ANALOG_INPUT_CHANNEL

One of the analog input channels that was specified is not a valid channel number. Channel numbers range from 0 to one less than the number of channels.

QERR_ANALOG_INPUT_CHANNELS_NOT_SUPPORTED

Analog input channels are not supported by this board.

QERR_MISSING_ENCODER_INPUTS

The encoder input channels argument is NULL when the number of encoder inputs specified is non-zero.

QERR_TOO_MANY_ENCODER_INPUT_CHANNELS

Too many encoder input channels were specified.

QERR_INVALID_ENCODER_INPUT_CHANNEL

One of the encoder input channels that was specified is not a valid channel number. Channel numbers range from 0 to one less than the number of channels.

QERR_ENCODER_INPUT_CHANNELS_NOT_SUPPORTED

Encoder input channels are not supported by this board.

QERR_MISSING_DIGITAL_INPUTS

The digital input channels argument is NULL when the number of digital inputs specified is non-zero.

QERR_TOO_MANY_DIGITAL_INPUT_CHANNELS

Too many digital input channels were specified.

QERR_INVALID_DIGITAL_INPUT_CHANNEL

One of the digital input channels that was specified is not a valid channel number. Channel numbers range from 0 to one less than the number of channels.

QERR_DIGITAL_INPUT_CHANNELS_NOT_SUPPORTED

Digital input channels are not supported by this board.

QERR_MISSING_OTHER_INPUTS

The other input channels argument is NULL when the number of other inputs specified is non-zero.

QERR_TOO_MANY_OTHER_INPUT_CHANNELS

Too many other input channels were specified.

QERR_INVALID_OTHER_INPUT_CHANNEL

One of the other input channels that was specified is not a valid channel number. Channel numbers range from 0 to one less than the number of channels.

QERR_OTHER_INPUT_CHANNELS_NOT_SUPPORTED

Other input channels are not supported by this board.

QERR_MISSING_ANALOG_OUTPUTS

The analog output channels argument is NULL when the number of analog outputs specified is non-zero.

QERR_TOO_MANY_ANALOG_OUTPUT_CHANNELS

Too many analog output channels were specified.

QERR_INVALID_ANALOG_OUTPUT_CHANNEL

One of the analog output channels that was specified is not a valid channel number. Channel numbers range from 0 to one less than the number of channels.

QERR_ANALOG_OUTPUT_CHANNELS_NOT_SUPPORTED

Analog output channels are not supported by this board.

QERR_MISSING_PWM_OUTPUTS

The PWM output channels argument is NULL when the number of PWM outputs specified is non-zero.

QERR_TOO_MANY_PWM_OUTPUT_CHANNELS

Too many PWM output channels were specified.

QERR_INVALID_PWM_OUTPUT_CHANNEL

One of the PWM output channels that was specified is not a valid channel number. Channel numbers range from 0 to one less than the number of channels.

QERR_PWM_OUTPUT_CHANNELS_NOT_SUPPORTED

PWM output channels are not supported by this board.

QERR_MISSING_DIGITAL_OUTPUTS

The digital output channels argument is NULL when the number of digital outputs specified is non-zero.

QERR_TOO_MANY_DIGITAL_OUTPUT_CHANNELS

Too many digital output channels were specified.

QERR_INVALID_DIGITAL_OUTPUT_CHANNEL

One of the digital output channels that was specified is not a valid channel number. Channel numbers range from 0 to one less than the number of channels.

QERR_DIGITAL_OUTPUT_CHANNELS_NOT_SUPPORTED

Digital output channels are not supported by this board.

QERR_MISSING_OTHER_OUTPUTS

The other output channels argument is NULL when the number of other outputs specified is non-zero.

QERR_TOO_MANY_OTHER_OUTPUT_CHANNELS

Too many other output channels were specified.

QERR_INVALID_OTHER_OUTPUT_CHANNEL

One of the other output channels that was specified is not a valid channel number. Channel numbers range from 0 to one less than the number of channels.

QERR_OTHER_OUTPUT_CHANNELS_NOT_SUPPORTED

Other output channels are not supported by this board.

QERR_INVALID_BOARD_HANDLE

An invalid board handle was passed as an argument to the board-specific HIL driver. Once a card has been closed using hil_close the board handle is invalid.

QERR_INVALID_BUFFER_HANDLE

An invalid buffer handle was passed to a board-specific HIL driver function.

QERR_OPERATION_ARGUMENT_IS_NULL

The operation argument to a board-specific HIL driver is NULL. This situation should never occur unless the user is calling the board-specific driver directly or memory has been corrupted.

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