Write Functions
- HIL.task_create_analog_writer(samples_in_buffer, channels, num_channels)
Creates a task for writing to the specified analog output channels. The task allows other operations to be performed while the analog outputs are being written “in the background”. The data written to the analog outputs is read from an internal circular “task buffer”. This data may be written into the task buffer at any time using the task_write_analog function. The size of this task buffer is determined by the samples_in_buffer parameter.
The task does not actually start reading the data from the task buffer and writing it to the analog outputs until the task_start function is called. In order for data to be available in the task buffer as soon as the task starts, store data in the buffer using task_write_analog prior to starting the task. Since the task writes to the analog outputs at the sampling rate specified when the task is started, it will be reading data from the task buffer at that rate. Thus, task_write_analog must be called to add more data to the task buffer before all the data in the buffer is depleted. Otherwise, the task will have no data to write to the analog outputs and will return with a QERR_BUFFER_OVERFLOW error the next time task_write_analog is called.
The task must be deleted when it is no longer in use using the task_delete function, in order to free the system and hardware resources used by the task.
- Parameters
samples_in_buffer (int) – The number of samples in the task buffer. The task_write_analog function cannot write more samples than this in a single call. If the task buffer underflows because task_write_analog has not been called in time to add data to the task buffer, then the next call to task_write_analog will return a`HIL_BUFFER_OVERFLOW` error.
channels (array_like) – An array containing the channel numbers of the analog outputs to be written to by the task.
num_channels (int) – The number of channels specified in the channels array.
- Returns
A handle to the created task.
- Return type
handle
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Create a task to write to the first four analog output channels.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> samples_in_buffer = 1000 >>> channels = array('I', [0, 1, 2, 3]) >>> num_channels = len(channels) >>> card.task_create_analog_writer(samples_in_buffer, channels, num_channels) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> samples_in_buffer = 1000 >>> channels = np.array([0, 1, 2, 3], dtype=np.uint32) >>> num_channels = len(channels) >>> card.task_create_analog_writer(samples_in_buffer, channels, num_channels) >>> # ... ... >>> finally: >>> card.close()
- HIL.task_create_pwm_writer(samples_in_buffer, channels, num_channels)
Creates a task for writing to the specified PWM output channels. The task allows other operations to be performed while the PWM outputs are being written “in the background”. The data written to the PWM outputs is read from an internal circular “task buffer”. This data may be written into the task buffer at any time using the task_write_pwm function. The size of this task buffer is determined by the samples_in_buffer parameter.
The task does not actually start reading the data from the task buffer and writing it to the PWM outputs until the task_start function is called. In order for data to be available in the task buffer as soon as the task starts, store data in the buffer using task_write_pwm prior to starting the task. Since the task writes to the PWM outputs at the sampling rate specified when the task is started, it will be reading data from the task buffer at that rate. Thus, task_write_pwm must be called to add more data to the task buffer before all the data in the buffer is depleted. Otherwise, the task will have no data to write to the PWM outputs and will return with a QERR_BUFFER_OVERFLOW error the next time task_write_pwm is called.
The task must be deleted when it is no longer in use using the task_delete function, in order to free the system and hardware resources used by the task.
- Parameters
samples_in_buffer (int) – The number of samples in the task buffer. The task_write_pwm function cannot write more samples than this in a single call. If the task buffer underflows because task_write_pwm has not been called in time to add data to the task buffer, then the next call to task_write_pwm will return a HIL_BUFFER_OVERFLOW error.
channels (array_like) – An array containing the channel numbers of the PWM outputs to be written to by the task.
num_channels (int) – The number of channels specified in the channels array.
- Returns
A handle to the created task.
- Return type
handle
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Create a task to write to the first two PWM output channels.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> samples_in_buffer = 1000 >>> channels = array('I', [0, 1]) >>> num_channels = len(channels) >>> card.task_create_pwm_writer(samples_in_buffer, channels, num_channels) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> samples_in_buffer = 1000 >>> channels = np.array([0, 1], dtype=np.uint32) >>> num_channels = len(channels) >>> card.task_create_pwm_writer(samples_in_buffer, channels, num_channels) >>> # ... ... >>> finally: >>> card.close()
- HIL.task_create_digital_writer(samples_in_buffer, channels, num_channels)
Creates a task for writing to the specified digital output channels. The task allows other operations to be performed while the digital outputs are being written “in the background”. The data written to the digital outputs is read from an internal circular “task buffer”. This data may be written into the task buffer at any time using the hil_task_write_digital function. The size of this task buffer is determined by the samples_in_buffer parameter.
The task does not actually start reading the data from the task buffer and writing it to the digital outputs until the task_start function is called. Before starting the task, the directions of the digital I/O lines should be set using the set_digital_directions function. Also, in order for data to be available in the task buffer as soon as the task starts, store data in the buffer using task_write_digital prior to starting the task. Since the task writes to the digital outputs at the sampling rate specified when the task is started, it will be reading data from the task buffer at that rate. Thus, task_write_digital must be called to add more data to the task buffer before all the data in the buffer is depleted. Otherwise the task will have no data to write to the digital outputs and will return with a QERR_BUFFER_OVERFLOW error the next time task_write_digital is called.
The task must be deleted when it is no longer in use using the task_delete function, in order to free the system and hardware resources used by the task.
- Parameters
samples_in_buffer (int) – The number of samples in the task buffer. The task_write_digital function cannot write more samples than this in a single call. If the task buffer underflows because task_write_digital has not been called in time to add data to the task buffer then the next call to task_write_digital will return an HIL_BUFFER_OVERFLOW error.
channels (array_like) – An array containing the channel numbers of the digital outputs to be written to by the task.
num_channels (int) – The number of channels specified in the channels array.
- Returns
A handle to the created task.
- Return type
handle
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
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 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_digital function failing to write those outputs.
Examples
Create a task to write to the first four digital output channels.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> samples_in_buffer = 1000 >>> channels = array('I', [0, 1, 2, 3]) >>> num_channels = len(channels) >>> card.task_create_digital_writer(samples_in_buffer, channels, num_channels) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> samples_in_buffer = 1000 >>> channels = np.array([0, 1, 2, 3], dtype=np.uint32) >>> num_channels = len(channels) >>> card.task_create_digital_writer(samples_in_buffer, channels, num_channels) >>> # ... ... >>> finally: >>> card.close()
- HIL.task_create_other_writer(samples_in_buffer, channels, num_channels)
Creates a task for writing to the specified other output channels. The task allows other operations to be performed while the other outputs are being written “in the background”. The data written to the other outputs is read from an internal circular “task buffer”. This data may be written into the task buffer at any time using the task_write_other function. The size of this task buffer is determined by the samples_in_buffer parameter.
The task does not actually start reading the data from the task buffer and writing it to the other outputs until the task_start function is called. In order for data to be available in the task buffer as soon as the task starts, store data in the buffer using task_write_other prior to starting the task. Since the task writes to the other outputs at the sampling rate specified when the task is started, it will be reading data from the task buffer at that rate. Thus, task_write_other must be called to add more data to the task buffer before all the data in the buffer is depleted. Otherwise, the task will have no data to write to the other outputs and will return with a QERR_BUFFER_OVERFLOW error the next time task_write_other is called.
The task must be deleted when it is no longer in use using the task_delete function, in order to free the system and hardware resources used by the task.
- Parameters
samples_in_buffer (int) – The number of samples in the task buffer. The task_write_other function cannot write more samples than this in a single call. If the task buffer underflows because task_write_other has not been called in time to add data to the task buffer, then the next call to task_write_other will return a`HIL_BUFFER_OVERFLOW` error.
channels (array_like) – An array containing the channel numbers of the other outputs to be written to by the task.
num_channels (int) – The number of channels specified in the channels array.
- Returns
A handle to the created task.
- Return type
handle
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Create a task to write to the first four other output channels.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> samples_in_buffer = 1000 >>> channels = array('I', [0, 1, 2, 3]) >>> num_channels = len(channels) >>> card.task_create_other_writer(samples_in_buffer, channels, num_channels) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> samples_in_buffer = 1000 >>> channels = np.array([0, 1, 2, 3], dtype=np.uint32) >>> num_channels = len(channels) >>> card.task_create_other_writer(samples_in_buffer, channels, num_channels) >>> # ... ... >>> finally: >>> card.close()
- HIL.task_create_writer(samples_in_buffer, analog_channels, num_analog_channels, pwm_channels, num_pwm_channels, digital_channels, num_digital_channels, other_channels, num_other_channels)
Creates a task for writing to the specified output channels. The task allows other operations to be performed while the outputs are being written “in the background”. The data written to the outputs is read from an internal circular “task buffer”. This data may be written into the task buffer at any time using the task_write function. The size of this task buffer is determined by the samples_in_buffer parameter. Before starting the task, the directions of the digital I/O lines should be set using the set_digital_directions function.
The task does not actually start reading the data from the task buffer and writing it to the outputs until the task_start function is called. In order for data to be available in the task buffer as soon as the task starts, store data in the buffer using task_write prior to starting the task. Since the task writes to the outputs at the sampling rate specified when the task is started, it will be reading data from the task buffer at that rate. Thus, task_write must be called to add more data to the task buffer before all the data in the buffer is depleted. Otherwise, the task will have no data to write to the outputs and will return with a QERR_BUFFER_OVERFLOW error the next time task_write is called.
The task must be deleted when it is no longer in use using the task_delete function, in order to free the system and hardware resources used by the task.
- Parameters
samples_in_buffer (int) – The number of samples in the task buffer. The task_write function cannot write more samples than this in a single call. If the task buffer overflows because task_write has not been called in time to remove the data from the task buffer, then the next call to task_write will return a HIL_BUFFER_OVERFLOW error.
analog_channels (array_like or None) – An array containing the channel numbers of the analog outputs to be written by the task. If no analog channels are required, then this parameter may be
None
. In this case, num_analog_channels must be zero.num_analog_channels (int) – The number of channels specified in the analog_channels array.
pwm_channels (array_like or None) – An array containing the channel numbers of the PWM outputs to be write by the task. If no encoder channels are required, then this parameter may be
None
. In this case, num_encoder_channels must be zero.num_pwm_channels (int) – The number of channels specified in the encoder_channels array.
digital_channels (array_like or None) – An array containing the channel numbers of the digital outputs to be write by the task. If no digital channels are required, then this parameter may be
None
. In this case, num_digital_channels must be zero.num_digital_channels (int) – The number of channels specified in the digital_channels array.
other_channels (array_like or None) – An array containing the channel numbers of the other outputs to be write by the task. If no other channels are required, then this parameter may be
None
. In this case, num_other_channels must be zero.num_other_channels (int) – The number of channels specified in the other_channels array.
- Returns
A handle to the created task.
- Return type
handle
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
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 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.
Examples
Create a task to write to the first four analog output channels and the first two PWM output channels.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> samples_in_buffer = 1000 >>> analog_channels = array('I', [0, 1, 2, 3]) >>> encoder_channels = array('I', [0, 1]) >>> num_analog_channels = len(analog_channels) >>> num_encoder_channels = len(encoder_channels) >>> card.task_create_writer(samples_in_buffer, ... analog_channels, num_analog_channels ... encoder_channels, num_encoder_channels, ... None, 0, ... None, 0) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> samples_in_buffer = 1000 >>> analog_channels = np.array([0, 1, 2, 3], dtype=np.uint32) >>> encoder_channels = np.array([0, 1], dtype=np.uint32) >>> num_analog_channels = len(analog_channels) >>> num_encoder_channels = len(encoder_channels) >>> card.task_create_writer(samples_in_buffer, ... analog_channels, num_analog_channels ... encoder_channels, num_encoder_channels, ... None, 0, ... None, 0) >>> # ... ... >>> finally: >>> card.close()
- HIL.task_write_analog(task, num_samples, buffer)
Writes the specified number of samples to the task buffer of a task created using 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 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, 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.
- Parameters
task (handle) – A handle to the task, as returned by one of the task creation functions.
num_samples (int) – 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 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.
buffer (array_like) – 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, then the data must appear in the array as follows, where the numbers correspond to channel numbers: [0, 1, 3, 0, 1, 3, …].
- Returns
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 task_start.
Note that successive calls to task_write_analog can write more samples in total then the total number of samples specified in task_start. However, only the number of samples specified in task_start will actually be processed and written to the hardware.
- Return type
int
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Writes 5000 samples at 1 kHz to the first four analog output channels, using
SYSTEM_CLOCK_1
.Using array:
>>> from array import array >>> from quanser.hardware import HIL, Clock >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 1, 2, 3]) >>> num_channels = len(channels) >>> frequency = 1000.0 >>> samples = 5000 >>> samples_in_buffer = int(frequency) >>> samples_to_write = 1 >>> buffer = array('d', [0.0] * num_channels) >>> task = card.task_create_analog_writer(samples_in_buffer, channels, num_channels) >>> card.task_start(task, Clock.SYSTEM_CLOCK_1, frequency, samples) >>> for index in range(samples): >>> card.task_write_analog(task, samples_to_write, buffer) >>> # ... ... >>> card.task_stop(task) >>> card.task_delete(task) >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL, Clock >>> card = HIL("q8_usb", "0") >>> try: >>> channels = np.array([0, 1, 2, 3], dtype=np.uint32) >>> num_channels = len(channels) >>> frequency = 1000.0 >>> samples = 5000 >>> samples_in_buffer = int(frequency) >>> samples_to_write = 1 >>> buffer = np.zeros(num_channels, dtype=np.float64) >>> task = card.task_create_analog_writer(samples_in_buffer, channels, num_channels) >>> card.task_start(task, Clock.SYSTEM_CLOCK_1, frequency, samples) >>> for index in range(samples): >>> card.task_write_analog(task, samples_to_write, buffer) >>> # ... ... >>> card.task_stop(task) >>> card.task_delete(task) >>> finally: >>> card.close()
- HIL.task_write_pwm(task, num_samples, buffer)
Writes the specified number of samples to the task buffer of a task created using task_create_pwm_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 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, task_write_pwm 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.
The interpretation of the PWM samples to be written depends upon the PWM mode. Typically, the data is interpreted as a duty cycle, in which a magnitude of 0.0 denotes a 0% duty cycle and magnitude of 1.0 indicates a 100% duty cycle. The sign determines the polarity of the output for those boards supporting bidirectional PWM outputs. However, other PWM modes are possible with some boards. Refer to the set_pwm_mode function for details.
- Parameters
task (handle) – A handle to the task, as returned by one of the task creation functions.
num_samples (int) – 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 task_create_pwm_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.
buffer (array_like) – An array containing the values to write to the PWM outputs. How these values are interpreted depends on the PWM mode. The PWM mode is configured using the et_pwm_mode function. 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 PWM output channels 0, 1 and 3 are being written, then the data must appear in the array as follows, where the numbers correspond to channel numbers: [0, 1, 3, 0, 1, 3, …].
- Returns
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 task_start.
Note that successive calls to task_write_pwm can write more samples in total then the total number of samples specified in task_start. However, only the number of samples specified in task_start will actually be processed and written to the hardware.
- Return type
int
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Writes 5000 samples at 1 kHz to the first two PWM output channels, using
SYSTEM_CLOCK_1
.Using array:
>>> from array import array >>> from quanser.hardware import HIL, Clock >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 1]) >>> num_channels = len(channels) >>> frequency = 1000.0 >>> samples = 5000 >>> samples_in_buffer = int(frequency) >>> samples_to_write = 1 >>> buffer = array('d', [0.0] * num_channels) >>> task = card.task_create_pwm_writer(samples_in_buffer, channels, num_channels) >>> card.task_start(task, Clock.SYSTEM_CLOCK_1, frequency, samples) >>> for index in range(samples): >>> card.task_write_pwm(task, samples_to_write, buffer) >>> # ... ... >>> card.task_stop(task) >>> card.task_delete(task) >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL, Clock >>> card = HIL("q8_usb", "0") >>> try: >>> channels = np.array([0, 1], dtype=np.uint32) >>> num_channels = len(channels) >>> frequency = 1000.0 >>> samples = 5000 >>> samples_in_buffer = int(frequency) >>> samples_to_write = 1 >>> buffer = np.zeros(num_channels, dtype=np.float64) >>> task = card.task_create_pwm_writer(samples_in_buffer, channels, num_channels) >>> card.task_start(task, Clock.SYSTEM_CLOCK_1, frequency, samples) >>> for index in range(samples): >>> card.task_write_pwm(task, samples_to_write, buffer) >>> # ... ... >>> card.task_stop(task) >>> card.task_delete(task) >>> finally: >>> card.close()
- HIL.task_write_digital(task, num_samples, buffer)
Writes the specified number of samples to the task buffer of a task created using task_create_digital_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 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, task_write_digital 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.
- Parameters
task (handle) – A handle to the task, as returned by one of the task creation functions.
num_samples (int) – The number of samples to write to the task buffer. Each “sample” consists of all the digital output channels specified when the task was created using task_create_digital_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.
buffer (array_like) – An array containing the values to write to the digital 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 digital output channels 0, 1 and 3 are being written, then the data must appear in the array as follows, where the numbers correspond to channel numbers: [0, 1, 3, 0, 1, 3, …].
- Returns
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 task_start.
Note that successive calls to task_write_digital can write more samples in total then the total number of samples specified in task_start. However, only the number of samples specified in task_start will actually be processed and written to the hardware.
- Return type
int
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
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 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 watchdog_set_digital_expiration_state function failing to write those outputs when the watchdog expires.
Examples
Writes 5000 samples at 1 kHz to the first four digital output channels, using
SYSTEM_CLOCK_1
.Using array:
>>> from array import array >>> from quanser.hardware import HIL, Clock >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 1, 2, 3]) >>> num_channels = len(channels) >>> frequency = 1000.0 >>> samples = 5000 >>> samples_in_buffer = int(frequency) >>> samples_to_write = 1 >>> buffer = array('b', [0] * num_channels) >>> task = card.task_create_digital_writer(samples_in_buffer, channels, num_channels) >>> card.task_start(task, Clock.SYSTEM_CLOCK_1, frequency, samples) >>> for index in range(samples): >>> card.task_write_digital(task, samples_to_write, buffer) >>> # ... ... >>> card.task_stop(task) >>> card.task_delete(task) >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL, Clock >>> card = HIL("q8_usb", "0") >>> try: >>> channels = np.array([0, 1, 2, 3], dtype=np.uint32) >>> num_channels = len(channels) >>> frequency = 1000.0 >>> samples = 5000 >>> samples_in_buffer = int(frequency) >>> samples_to_write = 1 >>> buffer = np.zeros(num_channels, dtype=np.int8) >>> task = card.task_create_digital_writer(samples_in_buffer, channels, num_channels) >>> card.task_start(task, Clock.SYSTEM_CLOCK_1, frequency, samples) >>> for index in range(samples): >>> card.task_write_digital(task, samples_to_write, buffer) >>> # ... ... >>> card.task_stop(task) >>> card.task_delete(task) >>> finally: >>> card.close()
- HIL.task_write_other(task, num_samples, buffer)
Writes the specified number of samples to the task buffer of a task created using task_create_other_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 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, task_write_other 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.
- Parameters
task (handle) – A handle to the task, as returned by one of the task creation functions.
num_samples (int) – The number of samples to write to the task buffer. Each “sample” consists of all the other output channels specified when the task was created using task_create_other_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.
buffer (array_like) – An array containing the values to write to the other 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 other output channels 0, 1 and 3 are being written, then the data must appear in the array as follows, where the numbers correspond to channel numbers: [0, 1, 3, 0, 1, 3, …].
- Returns
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 task_start.
Note that successive calls to task_write_other can write more samples in total then the total number of samples specified in task_start. However, only the number of samples specified in task_start will actually be processed and written to the hardware.
- Return type
int
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Writes 5000 samples at 1 kHz to the first four other output channels, using
SYSTEM_CLOCK_1
.Using array:
>>> from array import array >>> from quanser.hardware import HIL, Clock >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 1, 2, 3]) >>> num_channels = len(channels) >>> frequency = 1000.0 >>> samples = 5000 >>> samples_in_buffer = int(frequency) >>> samples_to_write = 1 >>> buffer = array('d', [0.0] * num_channels) >>> task = card.task_create_other_writer(samples_in_buffer, channels, num_channels) >>> card.task_start(task, Clock.SYSTEM_CLOCK_1, frequency, samples) >>> for index in range(samples): >>> card.task_write_other(task, samples_to_write, buffer) >>> # ... ... >>> card.task_stop(task) >>> card.task_delete(task) >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL, Clock >>> card = HIL("q8_usb", "0") >>> try: >>> channels = np.array([0, 1, 2, 3], dtype=np.uint32) >>> num_channels = len(channels) >>> frequency = 1000.0 >>> samples = 5000 >>> samples_in_buffer = int(frequency) >>> samples_to_write = 1 >>> buffer = np.zeros(num_channels, dtype=np.float64) >>> task = card.task_create_other_writer(samples_in_buffer, channels, num_channels) >>> card.task_start(task, Clock.SYSTEM_CLOCK_1, frequency, samples) >>> for index in range(samples): >>> card.task_write_other(task, samples_to_write, buffer) >>> # ... ... >>> card.task_stop(task) >>> card.task_delete(task) >>> finally: >>> card.close()
- HIL.task_write(task, num_samples, analog_buffer, pwm_buffer, digital_buffer, other_buffer)
writes the specified number of samples to the task buffer of a task created using task_create_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, task_write 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.
The interpretation of the PWM data to be written depends upon the PWM mode. Typically, the data is interpreted as a duty cycle, in which a magnitude of 0.0 denotes a 0% duty cycle and magnitude of 1.0 indicates a 100% duty cycle. The sign determines the polarity of the output for those boards supporting bidirectional PWM outputs. However, other PWM modes are possible with some boards. Refer to the set_pwm_mode function for details.
- Parameters
task (handle) – A handle to the task, as returned by one of the task creation functions.
num_samples (int) – The number of samples to write to the task buffer. Each “sample” consists of all the output channels specified when the task was created using task_create_writer. For example, if num_samples is 5 and the task is configured to write 3 analog channels and 2 PWM channels, then the analog input buffer will contain at least 15 elements and the PWM input buffer must contain at least 10 elements.
analog_buffer (array_like or None) – An array containing the voltage values to write to the analog outputs. The array must contain num_analog_channels * num_samples elements, where num_analog_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, then the data must appear in the array as follows, where the numbers correspond to channel numbers: [0, 1, 3, 0, 1, 3, …]. If no analog channels were specified in the call to task_create_writer, then this parameter may be
None
.pwm_buffer (array_like or None) – An array containing the values to write to the PWM outputs. How these values are interpreted depends on the PWM mode. The PWM mode is configured using the set_pwm_mode function. The array must contain num_pwm_channels * num_samples elements, where num_pwm_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 PWM output channels 0, 1 and 3 are being written, then the data must appear in the array as follows, where the numbers correspond to channel numbers: [0, 1, 3, 0, 1, 3, …]. If no PWM channels were specified in the call to task_create_writer, then this parameter may be
None
.digital_buffer (array_like or None) – An array containing the values to write to the digital outputs. The array must contain num_digital_channels * num_samples elements, where num_digital_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 digital output channels 0, 1 and 3 are being written, then the data must appear in the array as follows, where the numbers correspond to channel numbers: [0, 1, 3, 0, 1, 3, …]. If no digital channels were specified in the call to task_create_writer, then this parameter may be
None
.other_buffer (array_like or None) – An array containing the values to write to the other outputs. The array must contain num_other_channels * num_samples elements, where num_other_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 other output channels 0, 1 and 3 are being written, then the data must appear in the array as follows, where the numbers correspond to channel numbers: [0, 1, 3, 0, 1, 3, …]. If no other channels were specified in the call to task_create_writer, then this parameter may be
None
.
- Returns
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 is stopped or has finished processing the total number of samples indicated in the call to task_start.
Note that successive calls to task_write can write more samples in total then the total number of samples specified in task_start. However, only the number of samples specified in task_start will actually be processed and written to the hardware.
- Return type
int
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
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 set_digital_directions function. All the channels which will be used as digital inputs or outputs must be configured accordingly using this function. Failure to configure the digital I/O may result in the task_read_digital function failing to read or write the digital I/O as expected.
Examples
Writes 5000 samples at 1 kHz from the first four analog input channels and the first two encoder input channels, using
SYSTEM_CLOCK_1
.Using array:
>>> from array import array >>> from quanser.hardware import HIL, Clock >>> card = HIL("q8_usb", "0") >>> try: >>> analog_channels = array('I', [0, 1, 2, 3]) >>> encoder_channels = array('I', [0, 1]) >>> num_analog_channels = len(analog_channels) >>> num_encoder_channels = len(encoder_channels) >>> frequency = 1000.0 >>> samples = 5000 >>> samples_in_buffer = int(frequency) >>> samples_to_write = 1 >>> analog_buffer = array('d', [0.0] * num_analog_channels) >>> encoder_buffer = array('i', [0] * num_encoder_channels) >>> task = card.task_create_writer(samples_in_buffer, >>> analog_channels, num_analog_channels, >>> encoder_channels, num_encoder_channels, >>> None, 0, >>> None, 0) >>> card.task_start(task, Clock.SYSTEM_CLOCK_1, frequency, samples) >>> for index in range(samples): >>> card.task_write(task, samples_to_write, analog_buffer, encoder_buffer, None, None) >>> # ... ... >>> card.task_stop(task) >>> card.task_delete(task) >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL, Clock >>> card = HIL("q8_usb", "0") >>> try: >>> analog_channels = np.array([0, 1, 2, 3], dtype=np.uint32) >>> encoder_channels = np.array([0, 1], dtype=np.uint32) >>> num_analog_channels = len(analog_channels) >>> num_encoder_channels = len(encoder_channels) >>> frequency = 1000.0 >>> samples = 5000 >>> samples_in_buffer = int(frequency) >>> samples_to_write = 1 >>> analog_buffer = np.zeros(num_analog_channels, dtype=np.float64) >>> encoder_buffer = np.zeros(num_encoder_channels, dtype=np.int32) >>> task = card.task_create_writer(samples_in_buffer, >>> analog_channels, num_analog_channels, >>> encoder_channels, num_encoder_channels, >>> None, 0, >>> None, 0) >>> card.task_start(task, Clock.SYSTEM_CLOCK_1, frequency, samples) >>> for index in range(samples): >>> card.task_write(task, samples_to_write, analog_buffer, encoder_buffer, None, None) >>> # ... ... >>> card.task_stop(task) >>> card.task_delete(task) >>> finally: >>> card.close()