hil_task_read_analog_write_analog hil_task_read_digital_write_digital navigation bar

Table of Contents

hil_task_read_encoder_write_pwm

Deprecated

The HIL API MATLAB functions have been deprecated in favour of the new quanser.hardware.hil class, which has support for code generation for Quanser targets. Hence, with the new hil class it is possible to run MATLAB scripts in real-time on Quanser targets, while making use of the Quanser hardware.

Reads the specified number of encoder samples and writes the specified number of PWM samples to the task's internal buffer.

Syntax

[counts, samples_read, err] = hil_task_read_encoder_write_pwm(task, num_samples, duty_cycles)
    

Description

Reads the specified number of encoder samples and writes the specified number of PWM samples to the task's internal buffer. This function will block until the requested number of samples have been read or can be written or the task is stopped. Note that the written samples are only transferred to the internal buffer. The samples may not appear at the outputs until later. Use hil_task_flush to wait for all samples to be written to the 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 handle returned by hil_task_create_encoder_reader_pwm_writer.

num_samples

The number of samples to read and write. This number must be less than or equal to the internal buffer size assigned to the task in the hil_task_create_encoder_reader_pwm_writer call.

duty_cycles

A matrix of PWM duty cycles to write. There must be one row for each channel and one column for each sample.

Outputs

counts

A matrix of encoder counts read, as int32s. There is one row for each channel and one column for each sample.

samples_read

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

err

A negative error code or zero on success.

Examples

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

% Uniformly distributed random values between 0 and 1.
duty = rand(1,500);

% Writes 500 samples to PWM channel 5. Won't block.
hil_task_write_pwm(task, duty);

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

for i=1:10
    % Uniformly distributed random values between 0 and 1.
    duty = rand(1,500);

    % Reads 500 samples from encoder channels 0 and 3 and writes 500 samples to PWM channel 5.
    [v, n] = hil_task_read_encoder_write_pwm(task, 500, duty);
    ...
end;

% Make sure any samples remaining in the internal buffer have been written to the outputs.
hil_task_flush(task);
hil_task_stop(task);
hil_task_delete(task);
    

See Also

 

navigation bar