PST SDK  5.2.0.0-0eac0f6
listener.py
1 """Listener example of the PST SDK
2 
3 This example shows how to implement the PST SDK using the Tracker class
4 and how to receive data by implementing a callback function. The example
5 initializes the PST Tracker and grabs 100 data points.
6 
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.
13 
14 Copyright PS-Tech B.V. All Rights Reserved.
15 """
16 import context
17 import time
18 import sys
19 import pstech.pstsdk.tracker as pst
20 import pstech.pstsdk.errors as psterrors
21 
22 # Control variable for main loop
23 running = True
24 
25 # Number of data points to grab before application termination
26 max_samples = 100
27 
28 # Global number of samples
29 samples = 0
30 
31 """Helper function for clear printing of 4x4 matrices."""
32 def print_matrix(matrix):
33  idx = 0
34  for y in range(4):
35  for x in range(4):
36  print(str(matrix[x + y * 4]), end="\t")
37  print("")
38  print("\n")
39 
40 """Implementation of a tracker callback function
41 
42 Implementation of a tracker callback function. The callback_function
43 receives the data as soon as it becomes available and prints the tracking
44 target pose to the command line.
45 
46 Args:
47  tracker_data: Object containing tracking information retrieved from tracker
48  status_message: Status message reported by the tracker.
49 
50 See Also:
51  pstech.pstdk.trackerdata.TrackerData
52  pstech.pstsdk.errors.EStatusMessage
53 """
54 def callback_function(tracker_data, status_message):
55  global samples
56  global running
57 
58  if samples >= max_samples:
59  running = False
60 
61  if len(tracker_data.targetlist) > 0:
62  for target_pose in tracker_data.targetlist:
63  print("Pose for " + target_pose.name)
64  print_matrix(target_pose.pose)
65 
66  samples += 1
67 
68 """Helper function to register the exit handler with the application"""
69 def register_exit_handler():
70  if sys.platform.startswith("linux"):
71  import signal
72  signal.signal(signal.SIGTERM, exit_handler)
73  signal.signal(signal.SIGHUP, exit_handler)
74  signal.signal(signal.SIGQUIT, exit_handler)
75  signal.signal(signal.SIGINT, exit_handler)
76  elif sys.platform.startswith("win"):
77  import win32api
78  win32api.SetConsoleCtrlHandler(exit_handler, True)
79 
80 """Implement the exit handler to shut-down the PST Tracker connection on application termination."""
81 def exit_handler(*args):
82  global running
83  pst.Tracker.shutdown()
84  running = False
85  return True
86 
87 def main():
88  if(len(sys.argv) < 2):
89  print("\nConfiguration Error: A camera configuration file needs to be specified. This file can be found in the Redist folder of your installation. "
90  "See the documentation of the Python bindings for more information.")
91  exit(0)
92 
93  # Register exit_handler for proper shutdown
94  register_exit_handler()
95 
96  try:
97  # Use Context Manager to prevent improper Tracker shutdown on errors.
98  # Create an instance of the Tracker object using the default configuration path and file names.
99  with pst.Tracker("", "","", sys.argv[1]) as tracker:
100 
101  # Check if calibration information is available for all cameras. When this is not the case, provide a warning and exit.
102  if(len(tracker.get_uncalibrated_camera_urls()) > 0):
103  print("\nNo calibration information could be found in the configuration directory.\n"
104  "Please use the PST Server and PST Client application to initialize the PST Tracker and create/import a tracking target.\n"
105  "More information can be found in the Initialization section of the PST SDK manual and the PST Manual.\n"
106  )
107  sys.exit()
108 
109  # Print version number of the tracker server being used.
110  print("Running PST Server version " + tracker.get_version_info())
111 
112  # Register the listener object to the tracker server.
113  tracker.add_tracker_listener(callback_function)
114 
115  print("Put the Reference card in front of the PST in order to see tracking results.\n")
116 
117  # Start the tracker server.
118  tracker.start()
119 
120  # Perform a system check to see if the tracker server is running OK and print the result.
121  print("System check: " + str(tracker.system_check()))
122 
123  # Set the frame rate to 30 Hz.
124  tracker.set_framerate(30)
125 
126  # Print the new frame rate to see if it was set correctly. Note that for PST HD and Pico
127  # trackers the frame rate actually being set can differ from the value provided to Tracker.set_framerate().
128 
129  print("Frame rate set to: " + str(tracker.get_framerate()))
130 
131  # Main loop, wait for auto-termination.
132  while running:
133  time.sleep(0.1)
134 
135  except psterrors.TrackerError as err:
136  # Catch TrackerError and print error messages.
137  print(err.message)
138 
139 if __name__ == "__main__":
140  main()