PST SDK  6.0.0.0-272350a
trackingtarget.py
1 """Tracking target example of the PST SDK
2 
3 This example shows how to work with tracking targets using the PST SDK.
4 Note that at this moment tracking targets can not be trained or
5 imported using the PST SDK. In order to add new tracking targets, please
6 use the PST Client together with the pstech.pstsdk.tracker.Tracker.enable_shared_memory()
7 function, or use the stand-alone PST Server to configure the tracking targets.
8 
9 Copyright PS-Tech B.V. All Rights Reserved.
10 """
11 import context
12 import time
13 import sys
14 import pstech.pstsdk.tracker as pst
15 import pstech.pstsdk.errors as psterrors
16 
17 # Control variable for main loop
18 running = True
19 
20 # Number of data points to grab before application termination
21 max_samples = 100
22 
23 # Global number of samples
24 samples = 0
25 
26 """Implementation of the pst.Listener class to receive tracking data and mode changes."""
27 class MyListener(pst.Listener):
28 
29  """Implementation of a tracker data callback function
30 
31  Implementation of a tracker data callback function. The on_tracker_data
32  function receives the data as soon as it becomes available.
33 
34  Args:
35  tracker_data: Object containing tracking information retrieved from tracker
36  status_message: Status message reported by the tracker.
37 
38  See Also:
39  pstech.pstdk.trackerdata.TrackerData
40  pstech.pstsdk.errors.EStatusMessage
41  """
42  def on_tracker_data(self, tracker_data, status_message):
43  # Do something with the tracker data
44  global samples
45  global running
46 
47  if samples >= max_samples:
48  running = False
49 
50  for target_pose in tracker_data.targetlist:
51  print("Detected " + target_pose.name)
52 
53  samples += 1
54 
55 """Helper function to register the exit handler with the application"""
56 def register_exit_handler():
57  if sys.platform.startswith("linux"):
58  import signal
59  signal.signal(signal.SIGTERM, exit_handler)
60  signal.signal(signal.SIGHUP, exit_handler)
61  signal.signal(signal.SIGQUIT, exit_handler)
62  signal.signal(signal.SIGINT, exit_handler)
63  elif sys.platform.startswith("win"):
64  import win32api
65  win32api.SetConsoleCtrlHandler(exit_handler, True)
66 
67 """Implement the exit handler to shut-down the PST Tracker connection on application termination."""
68 def exit_handler(*args):
69  global running
70  pst.Tracker.shutdown()
71  running = False
72  return True
73 
74 def main():
75  if(len(sys.argv) < 2):
76  print("\nConfiguration Error: A camera configuration file needs to be specified. This file can be found in the Redist folder of your installation. "
77  "See the documentation of the Python bindings for more information.")
78  exit(0)
79 
80  # Register exit_handler for proper shutdown
81  register_exit_handler()
82 
83  try:
84  # Use Context Manager to prevent improper Tracker shutdown on errors.
85  # Create an instance of the Tracker object using the default configuration path and file names.
86  with pst.Tracker("", "","", sys.argv[1]) as tracker:
87 
88  # Print version number of the tracker server being used.
89  print("Running PST Server version " + tracker.get_version_info())
90 
91  # Create listener with callback functions for data and/or mode updates.
92  listener = MyListener()
93 
94  # Register the listener object to the tracker server.
95  tracker.add_tracker_listener(listener)
96 
97  print("Put the Reference card in front of the PST in order to see tracking results.\n")
98 
99  # Start the tracker server.
100  tracker.start()
101 
102  # Perform a system check to see if the tracker server is running OK and print the result.
103  print("System check: " + str(tracker.system_check()))
104 
105  # Retrieve the list of registered tracking targets and print their names and current status (active or not).
106  targets = tracker.get_target_list()
107  print("Found " + str(len(targets)) + " tracking targets:")
108  for target_status in targets:
109  print(target_status.name + "\t" + str(target_status.status))
110  print("")
111 
112  # Enable the Reference target. Note that this will cause a TrackerError in case the Reference
113  # target has not been created. The Reference target can be created using the PST Client.
114  tracker.set_target_status("Reference", True)
115 
116  # Get the 3D marker positions making up the Reference device and display them.
117  # Note that this will cause a TrackerError in case the Reference target has not been created.
118  markers_list = tracker.get_target_markers("Reference")
119  print("3D marker positions making up the Reference target:")
120  for marker in markers_list:
121  print("x: " + str(marker[0]) + "\ty: " + str(marker[1]) + "\tz: " + str(marker[2]))
122  print("")
123 
124  # Main loop, wait for auto-termination.
125  while running:
126  time.sleep(0.1)
127 
128  except psterrors.TrackerError as err:
129  # Catch TrackerError exceptions and print error messages.
130  print(err.message)
131 
132 if __name__ == "__main__":
133  main()