PST SDK  5.2.0.0-0eac0f6
exposure.py
1 """Exposure example of the PST SDK
2 
3 This example shows how to adjust exposure settings using the PST SDK. It shows how to
4 change exposure settings based on frame rate and the available exposure range for a
5 certain PST Tracker at a certain frame rate.
6 
7 Copyright PS-Tech B.V. All Rights Reserved.
8 """
9 import context
10 import time
11 import sys
12 import pstech.pstsdk.tracker as pst
13 import pstech.pstsdk.errors as psterrors
14 
15 # Control variable for main loop
16 running = True
17 
18 # Number of data points to grab before application termination
19 max_samples = 100
20 
21 # Global number of samples
22 samples = 0
23 
24 """Implementation of a tracker callback function
25 
26 Implementation of a tracker callback function. The callback_function
27 receives the data as soon as it becomes available.
28 
29 Args:
30  tracker_data: Object containing tracking information retrieved from tracker
31  status_message: Status message reported by the tracker.
32 
33 See Also:
34  pstech.pstdk.trackerdata.TrackerData
35  pstech.pstsdk.errors.EStatusMessage
36 """
37 def callback_function(tracker_data, status_message):
38  global samples
39  global running
40 
41  if samples >= max_samples:
42  running = False
43 
44  samples += 1
45  # Do something here with the received data
46 
47 """Helper function to register the exit handler with the application"""
48 def register_exit_handler():
49  if sys.platform.startswith("linux"):
50  import signal
51  signal.signal(signal.SIGTERM, exit_handler)
52  signal.signal(signal.SIGHUP, exit_handler)
53  signal.signal(signal.SIGQUIT, exit_handler)
54  signal.signal(signal.SIGINT, exit_handler)
55  elif sys.platform.startswith("win"):
56  import win32api
57  win32api.SetConsoleCtrlHandler(exit_handler, True)
58 
59 
60 """Implement the exit handler to shut-down the PST Tracker connection on application termination."""
61 def exit_handler(*args):
62  global running
63  pst.Tracker.shutdown()
64  running = False
65  return True
66 
67 def main():
68  if(len(sys.argv) < 2):
69  print("\nConfiguration Error: A camera configuration file needs to be specified. This file can be found in the Redist folder of your installation. "
70  "See the documentation of the Python bindings for more information.")
71  exit(0)
72 
73  # Register exit_handler for proper shutdown
74  register_exit_handler()
75 
76  try:
77  # Use Context Manager to prevent improper Tracker shutdown on errors.
78  # Create an instance of the Tracker object using the default configuration path and file names.
79  with pst.Tracker("", "","", sys.argv[1]) as tracker:
80 
81  # Print version number of the tracker server being used.
82  print("Running PST Server version " + tracker.get_version_info())
83 
84  # Register the callback function to the tracker server.
85  tracker.add_tracker_listener(callback_function)
86 
87  # Start the tracker server.
88  tracker.start()
89 
90  # Perform a system check to see if the tracker server is running OK and print the result.
91  print("System check: " + str(tracker.system_check()))
92  print("***************************\n")
93 
94  # Set the frame rate to 30 Hz.
95  tracker.set_framerate(30)
96 
97  # Print the new frame rate to see if it was set correctly. Note that for PST HD and Pico
98  # trackers the frame rate actually being set can differ from the value provided to SetFramerate().
99  print("Frame rate set to: " + str(tracker.get_framerate()) + " Hz\n")
100 
101  # Query available exposure range for the current frame rate and try setting the maximum exposure value.
102  min, max = tracker.get_exposure_range()
103  print("Exposure range: " + str(min) + " s - " + str(max) + " s")
104  print("Set Exposure to " + str(max))
105  tracker.set_exposure(max)
106  print("Check new exposure: " + str(tracker.get_exposure()) + " s")
107  print("***************************\n")
108 
109 
110  # Increase frame rate and check exposure value. For PST HD and PST Pico trackers, maximum exposure
111  # depends on frame rate. Exposure will be automatically decreased when necessary.
112  print("Set frame rate to 60 Hz")
113  tracker.set_framerate(60)
114  print("Frame rate set to: " + str(tracker.get_framerate()) + " Hz\n")
115  print("Check exposure: " + str(tracker.get_exposure()) + " s")
116 
117  # Check new exposure range
118  min, max = tracker.get_exposure_range()
119  print("New exposure range: " + str(min) + " s - " + str(max) + " s")
120  print("***************************\n")
121 
122  # Set exposure half-way
123  exposure_half = min + (max - min) / 2.0
124  print("Set exposure half way: " + str(exposure_half) + " s")
125  tracker.set_exposure(exposure_half)
126  print("Check new exposure: " + str(tracker.get_exposure()) + " s")
127  print("***************************\n")
128 
129  while running:
130  time.sleep(0.1)
131 
132  except psterrors.TrackerError as err:
133  # Catch TrackerError and print error message
134  print(err.message)
135 
136 if __name__ == "__main__":
137  main()