quanser.hardware.task.write_other quanser.hardware.task.read_analog_write_analog navigation bar

Table of Contents

quanser.hardware.task.write

Writes the specified samples to the task's internal buffer.

Syntax

[sample_written, err] = task.write(analog_voltages, pwm_duty_cycles, digital_values, other_values)
    

Description

Writes the specified samples to the task's internal buffer. The number of samples is determined by the number of columns in the matrix parameters, and must be less than or equal to the internal buffer size assigned to the task in the quanser.hardware.hil.task_create_writer or quanser.hardware.hil.task_create_reader_writer call. This function will block until the requested number of samples can be written or the task is stopped. Note that the samples are only transferred to the internal buffer. The samples may not appear at the outputs until later. Use task.flush to wait for all samples to be written to the outputs.

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 quanser.hardware.hil.set_digital_directions function. All the channels which will be used as digital outputs must be configured as outputs using this function. Failure to configure the digital I/O may result in the task.write function failing to write those outputs.

If no err output is provided then it throws an exception if an error occurs. In generated code it prints the error message. Use hil_get_error_message to get the message associated with an error code.

Parameters

task

Task instance returned by quanser.hardware.hil.task_create_writer or quanser.hardware.hil.task_create_reader_writer.

analog_voltages

A matrix of analog voltages. There must be one row for each channel and one column for each sample. If no analog channels were specified in the call to quanser.hardware.hil.task_create_writer then this parameter may be an empty matrix, [].

pwm_duty_cycles

In PWM duty cycle mode, this argument is a matrix of PWM duty cycles. Values may range from -1.0 to +1.0. Sign indicates direction for bipolar PWM outputs.

In PWM frequency mode, this argument is a matrix of PWM frequencies in Hertz. Sign indicates direction for bipolar PWM outputs.

In PWM period mode, this argument is a matrix of PWM periods in seconds. Sign indicates direction for bipolar PWM outputs.

There must be one row for each PWM output channel and one column for each sample. If no PWM channels were specified in the call to hil_task_create_writer then this parameter may be an empty matrix, [].

digital_values

A matrix of logical values. Values may be 0 or 1.There must be one row for each channel and one column for each sample. If no digital channels were specified in the call to quanser.hardware.hil.task_create_writer then this parameter may be an empty matrix, [].

other_values

A matrix of other values. There must be one row for each channel and one column for each sample. If no other channels were specified in the call to quanser.hardware.hil.task_create_writer then this parameter may be an empty matrix, [].

Outputs

samples_written

The number of samples written. This number will be equal to the number of samples specified unless the total number of samples specified in task.start has expired or the task has been stopped.

err

A negative error code or zero on success.

Examples

% Creates a task to write analog channels 0 and 3 and digital channel 5, with an internal buffer of 1000 samples.
task = board.task_create_writer(1000, [0 3], [], 5, []);

volts = 5*(rand(2,500)-0.5)*2;
bits  = (rand(1,500) >= 0.5);

task.write(volts, [], bits, []);

% Start the task using SYSTEM_CLOCK_1 at 1 kHz. Collect up to 5000 samples.
task.start(-1, 1000, 5000);

for i=1:10
    % Uniformly distributed random voltages between -5V and +5V.
    volts = 5*(rand(2,500)-0.5)*2;

    % Random logical values.
    bits  = (rand(1,500) >= 0.5);

    % Writes 500 samples to analog channels 0 and 3 and digital channel 5. Won't block until internal buffer full.
    n = task.write(volts, [], bits, []);
    ...
end;

% Make sure any samples remaining in the internal buffer have been written to the outputs.
task.flush;
task.stop;
task.close;
    

See Also

 

navigation bar