| PST SDK 6.0.0.0-272350a

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