Configuration
- HIL.open(card_type, card_identifier='0')
Opens a particular HIL board.
- Parameters
card_type (string) – A string indicating the type of HIL board being configured. For example, for the Q8 board the board type is “q8”. Some other valid board types are “q4” and “qpid_e”.
card_identifier (string) – A string identifying the particular board among all the boards of the same type. This parameter is only used when there is more than one board of the given type in the computer. Typically, this parameter is “0”, indicating the first board of the given type. Subsequent boards are typically numbered sequentially. Hence, the second board in the system is board “1”.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Notes
This function is called automatically if the
HIL
object is constructed with parameters.Example
>>> from quanser.hardware import HIL >>> card = HIL() >>> card.open("q8_usb", "0") >>> try: >>> # ... ... >>> finally: >>> card.close()
- HIL.close()
Closes the HIL board.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Example
>>> from quanser.hardware import HIL >>> card = HIL() >>> card.open("q8_usb", "0") >>> try: >>> # ... ... >>> finally: >>> card.close()
- static HIL.close_all()
Closes all HIL boards opened by the current process.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Example
>>> from quanser.hardware import HIL >>> card1 = HIL() >>> card2 = HIL() >>> try: >>> card1.open("q8_usb", "0") >>> card2.open("qube_servo2_usb", "0") >>> # ... ... >>> finally: >>> HIL.close_all()
- HIL.set_analog_input_configuration(channels, num_channels, configs)
Sets the configuration of the specified analog input channels. Only configurations supported by the board may be specified. Invalid configurations will generate an error. Refer to the documentation for your card for details on the features supported by the card.
- Parameters
channels (array_like) – An array containing the numbers of the analog input channels whose configurations will be changed. Channel numbers are zero-based. Thus, channel 0 is the first channel, channel 1 the second channel, etc.
num_channels (int) – The number of channels specified in channels array.
configs (array_like) – An array containing the configuration of the analog inputs. The array must contain num_channels elements. Each element in the configs array corresponds to the same element in the channels array.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Set the analog input configuration of channel 0 to
DIFF
and channel 3 toPDIFF
.Using array:
>>> from array import array >>> from quanser.hardware import HIL, AnalogInputConfiguration >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 3]) >>> num_channels = len(channels) >>> configs = array('i', [AnalogInputConfiguration.DIFF, AnalogInputConfiguration.PDIFF]) >>> card.set_analog_input_configuration(channels, num_channels, configs) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL, AnalogInputConfiguration >>> card = HIL("q8_usb", "0") >>> channels = np.array([0, 3], dtype=np.uint32) >>> num_channels = len(channels) >>> configs = np.array([AnalogInputConfiguration.DIFF, AnalogInputConfiguration.PDIFF], dtype=np.int32) >>> card.set_analog_input_configuration(channels, num_channels, configs) >>> # ... ... >>> card.close()
- HIL.set_analog_input_ranges(channels, num_channels, minima, maxima)
Sets the ranges of the analog inputs. Not all cards support programmable input ranges. Some cards only allow the ranges of all inputs to be set to the same value, rather than allowing each analog input to have a separate input range. Refer to the documentation for your card for details on the features supported by the card.
The units for the minimum and maximum values are typically Volts, but may be Amps or a unit appropriate to the specific card.
- Parameters
channels (array_like) – An array containing the numbers of the analog input channels whose ranges will be configured. Channel numbers are zero-based. Thus, channel 0 is the first channel, channel 1 the second channel, etc.
num_channels (int) – The number of channels specified in the channels, minima, and `maxima arrays.
minima (array_like) – An array of doubles containing the minimum value of the input range for the corresponding channel in the channels array. This array must be the same size as the channels array.
maxima (array_like) – An array of doubles containing the maximum value of the input range for the corresponding channel in the channels array. This array must be the same size as the channels array.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Set the range of analog input #0 to +/-10V, the range of analog input #3 to 0..5V and the range of input #5 to +/-5V.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 3, 5]) >>> num_channels = len(channels) >>> minima = array('i', [-10, 0, -5]) >>> maxima = array('i', [+10, 5, +5]) >>> card.set_analog_input_ranges(channels, num_channels, minima, maxima) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> channels = np.array([0, 3, 5], dtype=np.uint32) >>> num_channels = len(channels) >>> minima = np.array([-10, 0, -5], dtype=np.int32) >>> maxima = np.array([+10, 5, +5], dtype=np.int32) >>> card.set_analog_input_ranges(channels, num_channels, minima, maxima) >>> # ... ... >>> finally: >>> card.close()
- HIL.set_analog_output_ranges(channels, num_channels, minima, maxima)
Sets the ranges of the analog outputs. Not all cards support programmable output ranges. Some cards only allow the ranges of all outputs to be set to the same value, rather than allowing each analog output to have a separate output range. Refer to the documentation for your card for details on the features supported by the card.
The units for the minimum and maximum values are typically Volts, but may be Amps or a unit appropriate to the specific card.
- Parameters
channels (array_like) – An array containing the numbers of the analog output channels whose ranges will be configured. Channel numbers are zero-based. Thus, channel 0 is the first channel, channel 1 the second channel, etc.
num_channels (int) – The number of channels specified in the channels, minima, and `maxima arrays.
minima (array_like) – An array of doubles containing the minimum value of the output range for the corresponding channel in the channels array. This array must be the same size as the channels array.
maxima (array_like) – An array of doubles containing the maximum value of the output range for the corresponding channel in the channels array. This array must be the same size as the channels array.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Set the range of analog output #0 to +/-10V, the range of analog output #3 to 0..5V and the range of output #5 to +/-5V.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 3, 5]) >>> num_channels = len(channels) >>> minima = array('i', [-10, 0, -5]) >>> maxima = array('i', [+10, 5, +5]) >>> card.set_analog_output_ranges(channels, num_channels, minima, maxima) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> channels = np.array([0, 3, 5], dtype=np.uint32) >>> num_channels = len(channels) >>> minima = np.array([-10, 0, -5], dtype=np.int32) >>> maxima = np.array([+10, 5, +5], dtype=np.int32) >>> card.set_analog_output_ranges(channels, num_channels, minima, maxima) >>> # ... ... >>> finally: >>> card.close()
- HIL.set_clock_mode(clocks, num_clocks, clock_modes)
Sets the mode of the hardware clocks. This function is used with hardware clocks that are multipurpose. Some boards, like the Q8, allow the hardware clocks to be configured as timebases (the default) or as PWM outputs. This function allows the appropriate mode to be selected.
- Parameters
clocks (array_like) – An array containing the clocks whose mode will be changed. System clocks do not support different modes, so only hardware clocks, such as
HARDWARE_CLOCK_0
, should be specified.num_clocks (int) – The number of clocks specified in the clocks and clock_modes arrays.
clock_modes (array_like) – An array of clock modes to which to set the clocks. Each element in the clock_modes array corresponds to the same element in the clocks array. Hence, there must be as many elements in the clock_modes array as there are clocks.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Configure HARDWARE_CLOCK_0 as a PWM output.
Using array:
>>> from array import array >>> from quanser.hardware import HIL, Clock, ClockMode >>> card = HIL("q8_usb", "0") >>> try: >>> clocks = array('i', [Clock.HARDWARE_CLOCK_0]) >>> num_clocks = len(clocks) >>> modes = array('i', [ClockMode.PWM]) >>> card.set_clock_mode(clocks, num_clocks, modes) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL, Clock, ClockMode >>> card = HIL("q8_usb", "0") >>> try: >>> clocks = np.array([Clock.HARDWARE_CLOCK_0], dtype=np.uint32) >>> num_clocks = len(clocks) >>> modes = np.array([ClockMode.PWM], dtype=np.int32) >>> card.set_clock_mode(clocks, num_clocks, modes) >>> # ... ... >>> finally: >>> card.close()
- HIL.set_digital_directions(input_channels, num_input_channels, output_channels, num_output_channels)
Configures digital I/O channels as either inputs or outputs. In general, a digital I/O line cannot be configured as an input and an output at the same time.
- Parameters
input_channels (array_like) – An array containing the numbers of the digital I/O channels which will be configured as inputs.
num_input_channels (int) – The number of channels specified in the input_channels array. This parameter may be zero.
output_channels (array_like) – An array containing the numbers of the digital I/O channels which will be configured as outputs.
num_output_channels (int) – The number of channels specified in the output_channels array. This parameter may be zero.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Set the directions for the first two digital channels. Channel 1 is configured as an output while channel 0 is configured as an input.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL() >>> card.open("q2_usb", "0") >>> try: >>> input_channels = array('I', [1]) >>> output_channels = array('I', [0]) >>> num_input_channels = len(input_channels) >>> num_output_channels = len(output_channels) >>> card.set_digital_directions(input_channels, num_input_channels, output_channels, num_output_channels) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL() >>> card.open("q2_usb", "0") >>> try: >>> input_channels = np.array([1], dtype=np.uint32) >>> output_channels = np.array([0], dtype=np.uint32) >>> num_input_channels = len(input_channels) >>> num_output_channels = len(output_channels) >>> card.set_digital_directions(input_channels, num_input_channels, output_channels, num_output_channels) >>> card.close() >>> # ... ... >>> finally: >>> card.close()
- HIL.set_digital_output_configuration(channels, num_channels, configurations)
Sets the configuration of digital output lines.
- Two output configurations are possible:
DigitalConfiguration.TOTEM_POLE = use a totem-pole output (active high and active low) DigitalConfiguration.OPEN_COLLECTOR = use an open-collector output (passive high, active low)
Cards which have totem-pole outputs normally drive the output high or low actively. However, these outputs can simulate open-collector outputs by making the output tristate for a “high” output and active low for a “low” output. This function allows this emulation to be configured. Some cards allow the configuration to be programmed on a per-channel basis.
- Parameters
channels (array_like) – An array containing the digital output channels which will be configured.
num_channels (int) – The number of channels specified in the channels array. This parameter may be zero.
configurations (array_like) – An array containing the configuration of each digital output channel. Each element in the configurations array corresponds to the same element in the channels array. Hence, there must be as many elements in the configurations array as there are channels.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Set the configurations for the first two digital output channels. Channel 0 is configured as a totem-pole output while channel 1 is configured as an open-collector output.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL() >>> card.open("qpid_e", "0") >>> try: >>> channels = array('I', [1]) >>> num_channels = len(channels) >>> configurations = array('i', [DigitalConfiguration.TOTEM_POLE, DigitalConfiguration.OPEN_COLLECTOR]) >>> card.set_digital_output_configuration(channels, num_channels, configurations) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL() >>> card.open("qpid_e", "0") >>> try: >>> channels = np.array([1], dtype=np.uint32) >>> num_channels = len(channels) >>> configurations = np.array([DigitalConfiguration.TOTEM_POLE, DigitalConfiguration.OPEN_COLLECTOR], dtype=np.int32) >>> card.set_digital_output_configuration(channels, num_channels, configurations) >>> card.close() >>> # ... ... >>> finally: >>> card.close()
- HIL.set_encoder_counts(channels, num_channels, counts)
Sets the count values for the encoder counters. This function is typically used to initialize the encoder counters to zero when the board is first opened.
- Parameters
channels (array_like) – An array containing the channel numbers of the encoder inputs whose counters will be set.
num_channels (int) – The number of channels specified in the channels array.
counts (array_like) – An array of count values to which to set the encoder counters. Each element in the counts array corresponds to the same element in the channels array. Hence, there must be as many elements in the counts array as there are channels.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Set the encoder counts for the first three encoder channels.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 1, 2]) >>> num_channels = len(channels) >>> counts = array('i', [1000, -1000, 0]) >>> card.set_encoder_counts(channels, num_channels, counts) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> channels = np.array([0, 1, 2], dtype=np.uint32) >>> num_channels = len(channels) >>> counts = np.array([1000, -1000, 0], dtype=np.int32) >>> card.set_encoder_counts(channels, num_channels, counts) >>> # ... ... >>> finally: >>> card.close()
- HIL.set_encoder_filter_frequency(channels, num_channels, frequencies)
Some cards support filtering of their encoder inputs. This function sets the filter frequency of the encoder inputs on the card. Note that many cards do not support encoders. Cards which do provide encoders may not support filtering or different filter frequencies, or may not support different filter frequencies for each channel.
- Parameters
channels (array_like) – An array containing the channel numbers of the encoder inputs whose filter frequencies will be set. Channel numbers are zero-based. Thus, channel 0 is the first channel, channel 1 the second channel, etc.
num_channels (int) – The number of channels specified in the channels array.
frequencies (array_like) – An array of doubles containing the new encoder filter frequency for each channel in Hertz. There must be one element for each encoder channel specified in the channels array.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Set the filter frequencies of channels 0 and 4 to 8.33 MHz and set channel 2 to 4.17 MHz.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 2, 4]) >>> num_channels = len(channels) >>> frequencies = array('d', [1 / (120e-9 * 1), 1 / (120e-9 * 2), 1 / (120e-9 * 1)]) >>> card.set_encoder_filter_frequency(channels, num_channels, frequencies) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> channels = np.array([0, 2, 4], dtype=np.uint32) >>> num_channels = len(channels) >>> frequencies = np.array([1 / (120e-9 * 1), 1 / (120e-9 * 2), 1 / (120e-9 * 1)], dtype=np.float64) >>> card.set_encoder_filter_frequency(channels, num_channels, frequencies) >>> # ... ... >>> finally: >>> card.close()
- HIL.set_encoder_quadrature_mode(channels, num_channels, modes)
Sets the quadrature mode of the encoder inputs on the card. Many cards do not support encoders. Cards which do provide encoders may not support different quadrature modes, or may not support different quadrature modes for each channel.
- Parameters
channels (array_like) – An array containing the channel numbers of the encoder inputs whose quadrature modes will be set. Channel numbers are zero-based. Thus, channel 0 is the first channel, channel 1 the second channel, etc.
num_channels (int) – The number of channels specified in the channels array.
modes (array_like) – An array of
EncoderQuadritureMode
constants containing the new encoder quadrature mode for each channel. There must be one element for each encoder channel specified in the channels array.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Set the quadrature mode of channels 0 and 2 to 4X quadrature and set channel 1 to non-quadrature mode.
Using array:
>>> from array import array >>> from quanser.hardware import HIL, EncoderQuadratureMode >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 1, 2]) >>> num_channels = len(channels) >>> modes = array('i', [EncoderQuadratureMode.X4, EncoderQuadratureMode.NONE, EncoderQuadratureMode.X4]) >>> card.set_encoder_quadrature_mode(channels, num_channels, modes) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL, EncoderQuadratureMode >>> card = HIL("q8_usb", "0") >>> try: >>> channels = np.array([0, 2, 4], dtype=np.uint32) >>> num_channels = len(channels) >>> modes = np.array([EncoderQuadratureMode.X4, EncoderQuadratureMode.NONE, EncoderQuadratureMode.X4], dtype=np.int32) >>> card.set_encoder_quadrature_mode(channels, num_channels, modes) >>> # ... ... >>> finally: >>> card.close()
- HIL.set_pwm_configuration(channels, num_channels, configurations, alignments, polarities)
Sets the configuration of the PWM output channels.
Most cards do not support different configurations for their PWM channels but the Quanser QPID does. A PWM output configuration determines whether each PWM channel is an independent unipolar output, a bipolar output, or whether the outputs are paired to make complementary outputs. It also determines whether the PWM signals aligned to either the leading or trailing edge of the PWM period, or aligned to the center of the PWM period. Finally, the configuration also determines the polarity of the output (whether the duty cycle refers to the high pulse or low pulse width).
The actual PWM output mode is not changed until the PWM output is used in one of the following functions: write_pwm, write, read_encoder_write_pwm, read_write, write_pwm_buffer, write_buffer, read_encoder_write_pwm_buffer, read_write_buffer, task_start.
- Parameters
channels (array_like) – An array containing the channel numbers of the PWM outputs which are being configured.
num_channels (int) – The number of channels specified in the channels array.
configurations (array_like) – An array containing the configurations of the PWM outputs, as PWMConfiguration values. The array must contain num_channels elements. Each element in the configurations array corresponds to the same element in the channels array.
alignments (array_like) – An array containing the alignments of the PWM outputs, as PWMAlignment values. The array must contain num_channels elements. Each element in the alignments array corresponds to the same element in the channels array.
polarities (array_like) – An array containing the polarities of the PWM outputs, as PWMPolarity values. The array must contain num_channels elements. Each element in the polarities array corresponds to the same element in the channels array.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Configure PWM output 0 to be
UNIPOLAR
and output 2 to beCOMPLEMENTARY
.Using array:
>>> from array import array >>> from quanser.hardware import HIL, PWMconfiguration, PWMAlignment, PWMPolarity >>> card = HIL("qpid_e", "0") >>> try: >>> channels = array('I', [0, 2]) >>> num_channels = len(channels) >>> configurations = array('i', [PWMConfiguration.UNIPOLAR, PWMConfiguration.COMPLEMENTARY]) >>> alignments = array('i', [PWMAlignment.LEADING_EDGE_ALIGNED, PWMAlignment.LEADING_EDGE_ALIGNED]) >>> polarities = array('i', [PWMPolarity.ACTIVE_HIGH, PWMPolarity.ACTIVE_HIGH]) >>> card.set_pwm_configuration(channels, num_channels, configurations, alignments, polarities) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL, PWMMode >>> card = HIL("qpid_e", "0") >>> try: >>> channels = np.array([0, 2], dtype=np.uint32) >>> num_channels = len(channels) >>> configurations = np.array([PWMConfiguration.UNIPOLAR, PWMConfiguration.COMPLEMENTARY], dtype=np.int32) >>> alignments = np.array([PWMAlignment.LEADING_EDGE_ALIGNED, PWMAlignment.LEADING_EDGE_ALIGNED], dtype=np.int32) >>> polarities = np.array([PWMPolarity.ACTIVE_HIGH, PWMPolarity.ACTIVE_HIGH], dtype=np.int32) >>> card.set_pwm_configuration(channels, num_channels, configurations, alignments, polarities) >>> # ... ... >>> finally: >>> card.close()
- HIL.set_pwm_deadband(channels, num_channels, leading_edge_deadband, trailing_edge_deadband)
Sets the deadband of paired or complementary PWM output channels.
When two PWM outputs are configured as complementary by the set_pwm_configuration function, the secondary channel outputs the inverse of the primary channel. Hence, when the primary channel is high, the secondary channel is low, and vice versa. However, a “deadband” may be introduced in the transition from high-to-low or low-to-high, such that the secondary output does not change immediately, as soon as the primary channel changes state. Such “deadband” is necessary when the PWM outputs are being used to drive an H-bridge or other transistor configurations to avoid two transistors in an H-bridge from being turned on at the same time, albeit briefly, and causing a current surge (since power would essentially be shorted to ground for a brief instant).
This function allows the deadband to be specified. In fact, a different deadband may be specified for when the primary channel transitions from high-to-low or low-to-high, since transistor switching times may not be symmetric.
The deadband may be set for any channels that are configured as bipolar, paired or complementary. Channels which are configured as unipolar are ignored (i.e., hil_set_pwm_deadband may be called for those channels but the deadband value is ignored and success is returned). Only the primary channel should have its deadband set. The secondary channel is ignored since its behaviour is governed entirely by the primary channel.
The actual PWM output mode is not changed until the PWM output is used in one of the following functions: write_pwm, write, read_encoder_write_pwm, read_write, write_pwm_buffer, write_buffer, read_encoder_write_pwm_buffer, read_write_buffer, task_start.
- Parameters
channels (array_like) – An array containing the channel numbers of the PWM outputs which are being configured.
num_channels (int) – The number of channels specified in the channels array.
leading_edge_deadband (array_like) – An array containing the leading-edge deadband for the PWM outputs, as real values. The array must contain num_channels elements. Each element in the leading_edge_deadband array corresponds to the same element in the channels array.
trailing_edge_deadband (array_like) – An array containing the trailing-edge deadband for the PWM outputs, as real values. The array must contain num_channels elements. Each element in the trailing_edge_deadband array corresponds to the same element in the channels array.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Set the deadbands of PWM outputs 0, 1 and 2.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL("qpid_e", "0") >>> try: >>> channels = array('I', [0, 2]) >>> num_channels = len(channels) >>> leading_edge = array('d', [50e-9, 0, 100e-9]) >>> trailing_edge = array('d', [100e-9, 0, 200e-9]) >>> card.set_pwm_deadband(channels, num_channels, leading_edge, trailing_edge) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL, PWMMode >>> card = HIL("qpid_e", "0") >>> try: >>> channels = np.array([0, 2], dtype=np.uint32) >>> num_channels = len(channels) >>> leading_edge = np.array([50e-9, 0, 100e-9], dtype=np.double) >>> trailing_edge = np.array([100e-9, 0, 200e-9], dtype=np.double) >>> card.set_pwm_deadband(channels, num_channels, leading_edge, trailing_edge) >>> # ... ... >>> finally: >>> card.close()
- HIL.set_pwm_mode(channels, num_channels, modes)
Sets the mode that will be used for a PWM output. The mode determines how the values written to a PWM output are interpreted. The actual PWM output mode is not changed until the PWM output is used in one of the following functions: write_pwm, write, read_encoder_write_pwm, read_write, write_pwm_buffer, write_buffer, read_encoder_write_pwm_buffer, read_write_buffer, task_start.
- Parameters
channels (array_like) – An array containing the channel numbers of the PWM outputs whose modes are being set.
num_channels (int) – The number of channels specified in the channels array.
modes (array_like) – An array containing the modes of the PWM outputs. The array must contain num_channels elements. Each element in the modes array corresponds to the same element in the channels array.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Set the PWM mode to
DutyCycle
for PWM output channel 0 andFREQUENCY
for PWM output channel 1.Using array:
>>> from array import array >>> from quanser.hardware import HIL, PWMMode >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 1]) >>> num_channels = len(channels) >>> modes = array('i', [PWMMode.DUTY_CYCLE, PWMMode.FREQUENCY]) >>> card.set_pwm_mode(channels, num_channels, modes) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL, PWMMode >>> card = HIL("q8_usb", "0") >>> try: >>> channels = np.array([0, 1], dtype=np.uint32) >>> num_channels = len(channels) >>> modes = np.array([PWMMode.DUTY_CYCLE, PWMMode.FREQUENCY], dtype=np.int32) >>> card.set_pwm_mode(channels, num_channels, modes) >>> # ... ... >>> finally: >>> card.close()
- HIL.set_pwm_frequency(channels, num_channels, frequencies)
Sets the frequency that will be used for a PWM output. This function is typically used only in
PWMMode.DutyCycle
(the default). The actual PWM output frequency is not changed until the PWM output is used in one of the following functions: write_pwm, write, read_encoder_write_pwm, read_write, write_pwm_buffer, write_buffer, read_encoder_write_pwm_buffer, read_write_buffer, task_start.- Parameters
channels (array_like) – An array containing the channel numbers of the PWM outputs whose frequencies are being set.
num_channels (int) – The number of channels specified in the channels array.
frequencies (array_like) – An array containing the frequencies in Hertz of the PWM outputs. The array must contain num_channels elements. Each element in the frequencies array corresponds to the same element in the channels array.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Set the PWM frequency to 10 kHz for PWM output channel 0 and 20 kHz for PWM output channel 1.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 1]) >>> num_channels = len(channels) >>> frequencies = array('d', [10000.0, 20000.0]) >>> card.set_pwm_frequency(channels, num_channels, frequencies) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> channels = np.array([0, 1], dtype=np.uint32) >>> num_channels = len(channels) >>> frequencies = np.array([10000.0, 20000.0], dtype=np.float64) >>> card.set_pwm_frequency(channels, num_channels, frequencies) >>> # ... ... >>> finally: >>> card.close()
- HIL.set_pwm_duty_cycle(channels, num_channels, duty_cycles)
Sets the duty cycle that will be used for a PWM output. This function is typically used only in
PWM_FREQUENCY_MODE
orPWM_PERIOD_MODE
. The actual PWM output duty cycle is not changed until the PWM output is used in one of the following functions: write_pwm, write, read_encoder_write_pwm, read_write, write_pwm_buffer, write_buffer, read_encoder_write_pwm_buffer, read_write_buffer, task_start.- Parameters
channels (array_like) – An array containing the channel numbers of the PWM outputs whose duty cycles are being set.
num_channels (int) – The number of channels specified in the channels array.
duty_cycles (array_like) – An array containing the duty cycle values for the PWM outputs. Duty cycle values must be fractions between 0.0 and 1.0, where 0.0 indicates 0% and 1.0 denotes 100%. The value may be signed for those boards which support bidirectional PWM outputs. The array must contain num_channels elements. Each element in the duty_cycles array corresponds to the same element in the channels array.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Examples
Set the PWM duty cycle to 50% for PWM output channel 0 and 75% for PWM output channel 1.
Using array:
>>> from array import array >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> channels = array('I', [0, 1]) >>> num_channels = len(channels) >>> duty_cycles = array('d', [0.5, 0.75]) >>> card.set_pwm_duty_cycle(channels, num_channels, duty_cycles) >>> # ... ... >>> finally: >>> card.close()
Using numpy:
>>> import numpy as np >>> from quanser.hardware import HIL >>> card = HIL("q8_usb", "0") >>> try: >>> channels = np.array([0, 1], dtype=np.uint32) >>> num_channels = len(channels) >>> duty_cycles = np.array([10000.0, 20000.0], dtype=np.float64) >>> card.set_pwm_duty_cycle(channels, num_channels, duty_cycles) >>> # ... ... >>> finally: >>> card.close()
- HIL.set_card_specific_options(options, size)
Sets options specific to a particular card. The options parameter is a string containing the card-specific options. The string typically takes the form:
<name>=<value>,<name>=<value>,…
where <name> is the name of an option and <value> is the option’s value. In general, spaces should be avoided. Spaces may be contained in values if the value is enclosed in double quotes. For example, in the hypothetical options string below, the value of the vendor option contains a space, so it is enclosed in quotes:
options = “vendor=”National Instruments”,terminal_board=e_series”
Refer to the card’s documentation for details on the options supported by the card. Many cards do not support any options, since the standard HIL API functions cover most, if not all, of their functionality.
- Parameters
options (string) – A string containing the options. See the standard format of this argument in the discussion above. The string should be null terminated, although the options_size argument may be used to circumvent this requirement by providing the actual length of the string.
size (int) – The maximum number of characters that will be used from the options string. If the options string is null-terminated, then this argument may be set to
MAX_STRING_LENGTH
.
- Raises
HILError – On non-zero return code. A suitable error message may be retrieved using get_error_message.
Example
Configure a National Instruments PCI-6259 card for use with a Quanser E-Series terminal board.
>>> from quanser.hardware import HIL, MAX_STRING_LENGTH >>> card = HIL() >>> card.open("ni_pci_6259", "0") >>> try: >>> card.set_card_specific_options("terminal_board=e_series", MAX_STRING_LENGTH) >>> # ... ... >>> finally: >>> card.close()