PST SDK  5.2.0.0-0eac0f6
listener.py

This example can be found in Development\examples\python\listener.py.

This example shows how to implement the PST SDK using the pstech.pstsdk.tracker.Tracker class and how to receive data by implementing a callback function. The example initializes the PST Tracker and grabs 100 datapoints.

In order to be able to run this example, the PST Tracker has to be initialized first. This can be done by starting the PST-Server and the PST-Client application and making sure the calibration files have been downloaded and a tracking target is available. The tracking target can be the default Reference target or a newly trained or imported target. For more information, please see the Initialization section or check the PST Manual.

When compiling and running this example, please make sure that the required dependencies are copied from the Development\Redist directory into the build directory.

"""Listener example of the PST SDK
This example shows how to implement the PST SDK using the Tracker class
and how to receive data by implementing a callback function. The example
initializes the PST Tracker and grabs 100 data points.
In order to be able to run this example, the PST Tracker has to be initialized first.
This can be done by starting the PST-Server and the PST-Client application and making
sure the calibration files have been downloaded and a tracking target is available.
The tracking target can be the default Reference target or a newly trained or imported
target. For more information, please see the Initialization section of the PST SDK manual
or check the PST Manual.
Copyright PS-Tech B.V. All Rights Reserved.
"""
import context
import time
import sys
import pstech.pstsdk.tracker as pst
import pstech.pstsdk.errors as psterrors
# Control variable for main loop
running = True
# Number of data points to grab before application termination
max_samples = 100
# Global number of samples
samples = 0
"""Helper function for clear printing of 4x4 matrices."""
def print_matrix(matrix):
idx = 0
for y in range(4):
for x in range(4):
print(str(matrix[x + y * 4]), end="\t")
print("")
print("\n")
"""Implementation of a tracker callback function
Implementation of a tracker callback function. The callback_function
receives the data as soon as it becomes available and prints the tracking
target pose to the command line.
Args:
tracker_data: Object containing tracking information retrieved from tracker
status_message: Status message reported by the tracker.
See Also:
pstech.pstdk.trackerdata.TrackerData
pstech.pstsdk.errors.EStatusMessage
"""
def callback_function(tracker_data, status_message):
global samples
global running
if samples >= max_samples:
running = False
if len(tracker_data.targetlist) > 0:
for target_pose in tracker_data.targetlist:
print("Pose for " + target_pose.name)
print_matrix(target_pose.pose)
samples += 1
"""Helper function to register the exit handler with the application"""
def register_exit_handler():
if sys.platform.startswith("linux"):
import signal
signal.signal(signal.SIGTERM, exit_handler)
signal.signal(signal.SIGHUP, exit_handler)
signal.signal(signal.SIGQUIT, exit_handler)
signal.signal(signal.SIGINT, exit_handler)
elif sys.platform.startswith("win"):
import win32api
win32api.SetConsoleCtrlHandler(exit_handler, True)
"""Implement the exit handler to shut-down the PST Tracker connection on application termination."""
def exit_handler(*args):
global running
pst.Tracker.shutdown()
running = False
return True
def main():
if(len(sys.argv) < 2):
print("\nConfiguration Error: A camera configuration file needs to be specified. This file can be found in the Redist folder of your installation. "
"See the documentation of the Python bindings for more information.")
exit(0)
# Register exit_handler for proper shutdown
register_exit_handler()
try:
# Use Context Manager to prevent improper Tracker shutdown on errors.
# Create an instance of the Tracker object using the default configuration path and file names.
with pst.Tracker("", "","", sys.argv[1]) as tracker:
# Check if calibration information is available for all cameras. When this is not the case, provide a warning and exit.
if(len(tracker.get_uncalibrated_camera_urls()) > 0):
print("\nNo calibration information could be found in the configuration directory.\n"
"Please use the PST Server and PST Client application to initialize the PST Tracker and create/import a tracking target.\n"
"More information can be found in the Initialization section of the PST SDK manual and the PST Manual.\n"
)
sys.exit()
# Print version number of the tracker server being used.
print("Running PST Server version " + tracker.get_version_info())
# Register the listener object to the tracker server.
tracker.add_tracker_listener(callback_function)
print("Put the Reference card in front of the PST in order to see tracking results.\n")
# Start the tracker server.
tracker.start()
# Perform a system check to see if the tracker server is running OK and print the result.
print("System check: " + str(tracker.system_check()))
# Set the frame rate to 30 Hz.
tracker.set_framerate(30)
# Print the new frame rate to see if it was set correctly. Note that for PST HD and Pico
# trackers the frame rate actually being set can differ from the value provided to Tracker.set_framerate().
print("Frame rate set to: " + str(tracker.get_framerate()))
# Main loop, wait for auto-termination.
while running:
time.sleep(0.1)
except psterrors.TrackerError as err:
# Catch TrackerError and print error messages.
print(err.message)
if __name__ == "__main__":
main()
pstech.pstsdk.tracker
Definition: tracker.py:1
pstech.pstsdk.errors
Definition: errors.py:1