hil_task_create_writer hil_task_write_pwm navigation bar

Table of Contents

hil_task_write_analog

Writes the specified number of samples to the task buffer of an analog writer task.

Description

The hil_task_write_analog function writes the specified number of samples to the task buffer of a task created using hil_task_create_analog_writer. If there's not enough space in the task buffer, then this function will block until there is space in the task buffer or the task stops. Since the task removes data from the task buffer and writes it to the hardware 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 only blocks until there is enough space available in the task buffer. Because the task buffer is depleted at a given sampling rate, calling this function only synchronizes the caller to that sampling rate if the task buffer is kept full. 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_analog should be used to put data into the task buffer prior to starting the task!

Writer tasks are typically used to stream data to HIL hardware. In this case the num_samples parameter is typically half the number of samples in the task buffer to implement double-buffering.

Prototype

t_error
hil_task_write_analog(t_task task, t_uint32 num_samples, const t_double analog_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 write to the task buffer. Each "sample" consists of all the analog output channels specified when the task was created using hil_task_create_analog_writer . For example, if num_samples is 5 and the task is configured to write 3 channels, then the input buffer must contain at least 15 elements.

const t_double [] analog_buffer

An array containing the voltage values to write to the analog outputs. The array must contain num_channels * num_samples elements, where num_channels is the number of 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_buffer[num_samples][num_channels];
            

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

Return value

The return value is the number of samples written to the task buffer. This value may be less than the requested number of samples (including 0) if the task buffer does not have sufficient space and 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_write_analog can write more samples in total then the total number of samples specified in hil_task_start. However, only the number of samples specified in hil_task_start will actually be processed and written to the hardware.

Error codes

QERR_HIL_TASK_WRITE_ANALOG_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_OUTPUT_BUFFER

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

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


/*
* Writes 5000 samples at 1 kHz to the first four analog output channels, using SYSTEM_CLOCK_1.
* Return values are ignored for simplicity.
*/

t_uint32 channels[]        = { 0, 1, 2, 3 };
t_double frequency         = 1000;
t_uint32 samples           = 5000;
t_uint32 samples_in_buffer = frequency;
t_uint32 samples_to_write  = 100;

static t_double buffer[100][4];
t_task task;

/* Fill buffer */
...
hil_task_create_analog_writer(board, samples_in_buffer, channels, ARRAY_LENGTH(channels), &task);
hil_task_write_analog(task, samples_to_write, buffer); /* pre-fill the task buffer prior to starting the task */
hil_task_start(task, SYSTEM_CLOCK_1, frequency, samples);
for (int index = samples_to_write; index < samples; index += samples_to_write) {
    /* Fill buffer */
    ...
    hil_task_write_analog(task, samples_to_write, buffer); /* does not wait for data to be written to the hardware, */
    ...                                                    /* only waits for space in the task buffer */
}
hil_task_flush(task); /* make sure all data has been written to the hardware */
hil_task_stop(task);
hil_task_delete(task);
    

 

navigation bar