1 """Listener example of the PST SDK 3 This example shows how to implement the PST SDK using the Tracker class 4 and how to receive data by implementing the Listener class. The example 5 initializes the PST Tracker and grabs 100 data points. 7 In order to be able to run this example, the PST Tracker has to be initialized first. 8 This can be done by starting the PST-Server and the PST-Client application and making 9 sure the calibration files have been downloaded and a tracking target is available. 10 The tracking target can be the default Reference target or a newly trained or imported 11 target. For more information, please see the Initialization section of the PST SDK manual 12 or check the PST Manual. 14 Copyright PS-Tech B.V. All Rights Reserved. 34 """Helper function for clear printing of 4x4 matrices.""" 35 def print_matrix(matrix):
39 print(str(matrix[x + y * 4]), end=
"\t")
43 """Implementation of the pst.Listener class to receive tracking data and mode changes.""" 44 class MyListener(pst.Listener):
46 """Implementation of a tracker data callback function 48 Implementation of a tracker data callback function. The on_tracker_data 49 function receives the data as soon as it becomes available and prints the 50 tracking target pose to the command line. 53 tracker_data: Object containing tracking information retrieved from tracker 54 status_message: Status message reported by the tracker. 57 pstech.pstdk.trackerdata.TrackerData 58 pstech.pstsdk.errors.EStatusMessage 60 def on_tracker_data(self, tracker_data, status_message):
64 if samples >= max_samples:
67 if len(tracker_data.targetlist) > 0:
68 for target_pose
in tracker_data.targetlist:
69 print(
"Pose for " + target_pose.name)
70 print_matrix(target_pose.pose)
74 """Implementation of a tracker mode callback function 76 Implementation of a tracker mode callback function. The on_tracker_mode 77 function receives any mode update as soon as it become available and prints it. 80 mode: Current mode reported by the tracker. 83 pstech.pstsdk.tracker.ETrackerMode 85 def on_tracker_mode(self, mode):
87 if mode == pst.ETrackerMode.MODE_TRACKING:
88 print(
"Tracker tracking")
89 elif mode == pst.ETrackerMode.MODE_LOWPOWER:
90 print(
"Tracker paused")
91 elif mode == pst.ETrackerMode.MODE_DISCONNECT:
92 print(
"Tracker disconnected")
93 elif mode == pst.ETrackerMode.MODE_RECONNECT:
94 print(
"Tracker reconnected")
97 print(
"Mode " + str(mode))
99 """Helper function to register the exit handler with the application""" 100 def register_exit_handler():
101 if sys.platform.startswith(
"linux"):
103 signal.signal(signal.SIGTERM, exit_handler)
104 signal.signal(signal.SIGHUP, exit_handler)
105 signal.signal(signal.SIGQUIT, exit_handler)
106 signal.signal(signal.SIGINT, exit_handler)
107 elif sys.platform.startswith(
"win"):
109 win32api.SetConsoleCtrlHandler(exit_handler,
True)
111 """Implement the exit handler to shut-down the PST Tracker connection on application termination.""" 112 def exit_handler(*args):
114 pst.Tracker.shutdown()
119 if(len(sys.argv) < 2):
120 print(
"\nConfiguration Error: A camera configuration file needs to be specified. This file can be found in the Redist folder of your installation. " 121 "See the documentation of the Python bindings for more information.")
125 register_exit_handler()
130 with pst.Tracker(
"",
"",
"", sys.argv[1])
as tracker:
133 if(len(tracker.get_uncalibrated_camera_urls()) > 0):
134 print(
"\nNo calibration information could be found in the configuration directory.\n" 135 "Please use the PST Server and PST Client application to initialize the PST Tracker and create/import a tracking target.\n" 136 "More information can be found in the Initialization section of the PST SDK manual and the PST Manual.\n" 141 print(
"Running PST Server version " + tracker.get_version_info())
144 listener = MyListener()
147 tracker.add_tracker_listener(listener)
149 print(
"Put the Reference card in front of the PST in order to see tracking results.\n")
155 print(
"System check: " + str(tracker.system_check()))
158 tracker.set_framerate(30)
162 print(
"Frame rate set to: " + str(tracker.get_framerate()))
174 except psterrors.TrackerError
as err:
178 if __name__ ==
"__main__":