hil_stream_from_disk_example.m
Open this M-file

HIL Stream From Disk Example

This example demonstrates the double buffering capabilities of the QUARC HIL commands. It writes continuously to analog output channels 0, 1, and 2, at a sampling rate of 1kHz. The data is read from a file as it is output. An oscilloscope must be used to observe the output sine waves.

A task is created to handle writing to the analog outputs in the background using quanser.hardware.hil.task_create_analog_writer. This task is started using the quanser.hardware.task.start function. The data to be written to the analog outputs is stored in the task's internal buffer in one second intervals (1000 samples), using the quanser.hardware.task.write_analog function.

The example will stop after 10 seconds.

Warning

Do NOT press Ctrl+C to stop the script or the example will not stop the motor and will not shut down the hardware correctly! Use the quanser.hardware.hil.close_all command in order to shut down the hardware if you have pressed Ctrl+C.

System Requirements

This example requires hardware supported by the HIL command set, such as the Q4 or Q8 hardware-in-the-loop card. Furthermore, the card must support task-based writes to an analog channel.

Configuring the example

To set up the example for your data acquisition card, edit the M-file and change the board_type to the type of board being used. If you have more than one of these data acquisition cards in your machine, then also change the board_identifier variable to refer to desire board. Board identifiers are typically equal to the board number, with the first board being board '0', the next board being board '1', etc.

If your board does not support three analog output channels, then change the analog_channels variable to an appropriate vector of channels.

Running the example

Simply type the name of the example, hil_stream_from_disk_example, at the Matlab command prompt to run the example. It begins by generating 10 seconds worth of data to a file called 'sample_data.txt', so that there is a file to read for the double-buffering example. It then reads that file in one second increments (1000 lines at a time) while writing the data to the analog outputs. The 1000 samples read from the file are written to the task buffer and the task then writes those samples to the analog outputs at a sampling frequency of 1 kHz in the background. Use an oscilloscope to observe the sine waves that are output from analog output channels 0-2.

Building code from the example

To build real-time code from the example for the QUARC Win64 target, execute the following command in the MATLAB Command Window:

qc_build_script('hil_stream_from_disk_example', 'win64');

The command may also be entered in this form:

qc_build_script('hil_stream_from_disk_example.rt-win64');

The qc_build_script command generates a build script called 'hil_stream_from_disk_example_build.m' and a main C file called 'hil_stream_from_disk_example_main.c'. It then invokes the build script to generate C code for the MATLAB script and to compile and link it into a QUARC executable called 'hil_stream_from_disk_example.rt-win64'. Running qc_build_script again will not overwrite the generated files, so they may be modified and the changes will be incorporated when qc_build_script is run again.

Running the generated executable

Before running the generated executable, open a QUARC Console so that the output of the executable may be seen. The QUARC Console need only be opened once. Use the command:

qc_script_console('hil_stream_from_disk_example.rt-win64', 'all');

The QUARC Console shows the standard output from any QUARC executable that is run on the target (since the 'all' option was specified).

To run the generated executable, type the following command in the MATLAB Command Window:

qc_run_script('hil_stream_from_disk_example.rt-win64');

The output from the executable will be seen in the QUARC Console:

QUARC Console output