trs_interface package¶
The trs_interface
package is responsible for the host (computer) side of communications
and control of the TRSpectrometer interface hardware.
The most important part of the package is the TRSInterface
class.
This will handle all of a typical user’s needs when communicating with the device.
Example usage:
from trs_interface import TRSInterface
trsi = TRSInterface()
# Initialise other associated hardware devices
# delay = ... motorised delay stage
# chopper = ... optical chopper wheel
# camera = ... detector
# Do any required configuration
trsi.set_chopper_divider = 4
trsi.set_chopper_delay = 50 # == 1.2 us (units of 1/42 MHz, ~24 ns)
trsi.set_camera_sync_delay = 75 # == 1.8 us
trsi.set_camera_sync_duration = 100 # == 2.4 us
# Set up a handler for when data is received
data = (None, None)
def data_handler(quad_pos, chop_state):
global data
print(f"Received {data[0].shape[0]} data points.")
data = (quad_pos, chop_state)
trsi.register_data_callback(data_handler)
# Set up a handler to be notified immediately if the connection fails
def error_handler(message):
print(f"Error: {message}")
trsi.register_error_callback(error_handler)
# Move delay to starting position
delay.move_absolute(0)
# Arm the device so it's waiting for start trigger from delay
trsi.arm()
# Start the move
delay.move_absolute(12345678)
# ... wait
# Do something with the data
print(data)
The status of the device can be queried using the status
property.
This is a dictionary containing the fields:
"connected"
"laser_sync_polarity"
"chopper_syncin_polarity"
"chopper_syncout_polarity"
"delay_trig_polarity"
"camera_trig_polarity"
"quadrature_polarity"
"quadrature_direction"
"chopper_sync_delay"
"chopper_sync_duration"
"camera_sync_delay"
"camera_sync_duration"
"chopper_divider"
"quadrature_value"
"laser_sync_period"
Class properties are also implemented so that the values can be queried or set easily, for example:
# Print the current quadrature value
print(trsi.quadrature_value)
# Set the camera sync pulse duration to 2.952 us (units of 1/42 MHz, ~24 ns)
trsi.camera_sync_duration = 123;
To configure signal polarities or quadrature directions, some enumerations are defined in the protocol
package:
from trs_interface.protocol import Polarity, Direction
# Laser input sync on falling edge of signal
trsi.laser_sync_polarity = Polarity.FALLING
# Delay input signal active low
trsi.delay_trig_polarity = Polarity.LOW
# Invert quadrature counting direction
trsi.quadrature_direction = Direction.REVERSE
Once all parameters are configured, to save the settings so they are used as defaults when first powered on:
trsi.store_settings()
- class trs_interface.TRSInterface(serial_port=None, location='')[source]¶
Bases:
object
Initialise and open serial device for the TRSInterface device.
If the
serial_port
parameter isNone
(default), then an attempt to detect the device will be performed. The first device found will be returned. If multiple devices are attached to the system, thelocation
parameter may be used to select the correct device. Location refers to the USB bus and port identifier the device is plugged in to, for example “1-14:1.0”. This is a regular expression match, for examplelocation="14"
would match devices with “14” anywhere in the location string, whilelocation=".*14$"
would match locations ending in 14.- Parameters
serial_port – Serial port device the device is connected to.
location – Regular expression matching the serial number of device to search for.
- arm()[source]¶
Arm the device to start collecting data on hardware trigger signal from the delay stage.
- register_data_callback(callback_function)[source]¶
Register a function to be called when data is received from the device.
The function passed in should have the signature
callback_function(quadrature, chopper)
, wherequadrature
is the value for the quadrature encoder as a numpy array of signed integers (np.uint32) andchopper
is the state of the chopper (“on” or “off”) as a numpy array of boolean values.- Parameters
callback_function – Function to call when data is received.
- register_error_callback(callback_function)[source]¶
Register a function to be called if there is an error communicating with the device.
The function passed in should have the signature
callback_function(message)
, wheremessage
is a string describing the error.- Parameters
callback_function – Function to call when a connection error occurs.
- status_formatted()[source]¶
Get a human-readable interpretation of the device status.
- Returns
String of status values.
- store_settings()[source]¶
Instruct the device to store its current settings to flash memory.
The settings will be used as defaults when powered on in the future.
- trigger()[source]¶
Begin sending camera trigger pulses, but do not return chopper or delay positions.
The triggering can be stopped with
stop()
. Status updates will still occur while triggering is occurring.
- unregister_data_callback(callback_function)[source]¶
Unregister a previously registered callback function.
The function passed in should have been previously registered using
register_data_callback()
.- Parameters
callback_function – Function to unregister.
- unregister_error_callback(callback_function)[source]¶
Unregister a previously registered error callback function.
The function passed in should have been previously registered using
register_error_callback()
.- Parameters
callback_function – Function to unregister.
- read_interval¶
Time to wait between read attempts on the serial port, in seconds.
- status¶
Dictionary of status properties for the device.
- update_interval¶
Time to wait between read queries of the device status, such as current quadrature decoder value.
- trs_interface.find_device(location='')[source]¶
Search attached serial ports for a TRSInterface device.
The first device found will be returned. If multiple devices are attached to the system, the
location
parameter may be used to select the correct device. Location refers to the USB bus and port identifier the device is plugged in to, for example “1-14:1.0”. This is a regular expression match, for examplelocation="14"
would match devices with “14” anywhere in the location string, whilelocation=".*14$"
would match locations ending in 14.- Parameters
location – Regular expression to match a device USB location.
- trs_interface.format_status(status_dict)[source]¶
Convert a status dictionary to a readable format suitable for printing.
- Parameters
status_dict – Status dictionary.
- Returns
String of human-readable status values.