hil_write_buffer hil_read_encoder_write_pwm_buffer navigation bar

Table of Contents

hil_read_analog_write_analog_buffer

Reads the specified number of samples from the analog input channels and writes the specified number of samples to the analog output channels at the indicated sampling rate.

Description

The hil_read_analog_write_analog_buffer function reads the specified number of samples from the analog input channels and writes to the specified analog output channels at the given sampling rate in a single function call. Each sampling instant, the write operation occurs immediately following the read operation. Since the read-write operation occurs at the lowest level the read and write occur virtually concurrently. The function does not return until all the data has been read and written. This function is particularly useful for system identification since the read and write operations are synchronized. In particular, the value read in one sampling instant is the result of the write operation in the previous sampling instant. See the Buffered I/O overview for more details.

Prototype

t_error 
hil_read_analog_write_analog_buffer(t_card card, t_clock clock, t_double frequency, t_uint32 num_samples,
                                    const t_uint32 analog_input_channels[],  t_uint32 num_analog_input_channels, 
                                    const t_uint32 analog_output_channels[], t_uint32 num_analog_output_channels, 
                                    t_double       analog_input_buffer[],
                                    const t_double analog_output_buffer[]);
    

Parameters

t_card card

A handle to the board, as returned by hil_open

t_clock clock

The clock used to time the operation. Note that some clocks allow faster sampling rates than others. See Clocks for more information on clocks.

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

t_double frequency

The frequency in Hertz at which to read from the analog input channels and write to the analog output channels. For example, if frequency is set to 1000, then the hil_read_analog_write_analog_buffer function will read all the input channels and write all the output channels every millisecond.

t_uint32 num_samples

The number of samples to process. Each "sample" consists of all the analog input channels and all the analog output channel specified. For example, if frequency is set to 1000 and num_samples is set to 5000, then the hil_read_analog_write_analog_buffer function will return after 5 seconds, having read 5000 samples and written 5000 samples. If 3 input channels have been selected, then the input buffer will contain 15,000 elements. If 2 output channels have been selected, then the output buffer must contain 10,000 elements.

const t_uint32 [] analog_input_channels

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

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

t_uint32 num_analog_input_channels

The number of channels specified in the analog_input_channels array.

const t_uint32 [] analog_output_channels

An array containing the channel numbers of the analog outputs to which to write.

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

t_uint32 num_analog_output_channels

The number of channels specified in the analog_output_channels array.

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. 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 buffer[num_samples][num_channels];
            

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

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. The array must be organized as a linear array of samples, with each sample consisting of a group of channels. Refer to the analog_input_buffer parameter for an example.

Return value

The return value is the number of samples successfully read and written. 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_READ_ANALOG_WRITE_ANALOG_BUFFER_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_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_RESOURCE_IN_USE

The analog-to-digital converter on the HIL board is currently in use by another operation.

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_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 first four analog input channels and writes at the same time
* to the first two analog output channels, using SYSTEM_CLOCK_1.
*/

t_double frequency = 1000;
t_uint32 samples   = 5000;
t_error result;
int i, j;

t_uint32 input_channels[]  = { 0, 1, 2, 3 };
t_uint32 output_channels[] = { 0, 1 };

static t_double input_buffer[5000][4];
static t_double output_buffer[5000][2];

for (i=0; i < samples; i++) {
double time = i / frequency;
for (j=0; j < ARRAY_LENGTH(output_channels); j++)
    output_buffer[i][j] = (j + 7.0) * sin(2*M_PI*time);
}

result = hil_read_analog_write_analog_buffer(board, SYSTEM_CLOCK_1, frequency, samples
    , input_channels,  ARRAY_LENGTH(input_channels)
    , output_channels, ARRAY_LENGTH(output_channels)
    , &input_buffer[0][0]
    , &output_buffer[0][0]
    );
    

 

navigation bar