| PST SDK 6.0.0.0-272350a

sharedmemory.py
1 """Shared memory example of the PST SDK
2
3 This example shows how to activate the shared memory communication pipeline
4 that enables communication of the PST Client application through the PST SDK.
5 Note that for the shared memory pipeline to function, the application has to
6 run with elevated access (administrator rights). After enabling shared memory,
7 the PST Client application can be used to download calibration information and
8 manage tracking targets.
9
10 Copyright PS-Tech B.V. All Rights Reserved.
11 """
12 import context
13 import time
14 import sys
15 import ctypes
16 import pstech.pstsdk.tracker as pst
17 import pstech.pstsdk.errors as psterrors
18
19 """Helper function to register the exit handler with the application"""
20 def register_exit_handler():
21 if sys.platform.startswith("linux"):
22 import signal
23 signal.signal(signal.SIGTERM, exit_handler)
24 signal.signal(signal.SIGHUP, exit_handler)
25 signal.signal(signal.SIGQUIT, exit_handler)
26 signal.signal(signal.SIGINT, exit_handler)
27 elif sys.platform.startswith("win"):
28 import win32api
29 win32api.SetConsoleCtrlHandler(exit_handler, True)
30
31 """Implement the exit handler to shut-down the PST Tracker connection on application termination."""
32 def exit_handler(*args):
33 pst.Tracker.disable_shared_memory()
34 pst.Tracker.shutdown()
35
36 """Check if user is an admin."""
37 def is_admin():
38 if sys.platform.startswith("win"):
39 try:
40 return ctypes.windll.shell32.IsUserAnAdmin()
41 except:
42 return False
43 elif sys.platform.startswith("linux"):
44 return True
45
46 def main():
47 if is_admin():
48 if(len(sys.argv) < 2):
49 print("\nConfiguration Error: A camera configuration file needs to be specified. This file can be found in the Redist folder of your installation. "
50 "See the documentation of the Python bindings for more information.")
51 exit(0)
52
53 # Register exit_handler for proper shutdown
54 register_exit_handler()
55
56 try:
57 # Use Context Manager to prevent improper Tracker shutdown on errors.
58 # Create an instance of the Tracker object using the default configuration path and file names.
59 with pst.Tracker("", "","", sys.argv[1]) as tracker:
60 # Start the tracker server.
61 tracker.start()
62
63 # Enable the shared memory pipeline to enable client connections.
64 tracker.enable_shared_memory()
65
66 print("Shared memory server initialized. Start the PST Client application to create a connection.\n")
67
68 # Wait for one minute before terminating this application.
69 time.sleep(60)
70
71 # Disable the shared memory pipeline. If the PST Client is still connected at this point,
72 # it will try to reconnect and otherwise exit.
73 tracker.disable_shared_memory()
74 except psterrors.TrackerError as err:
75 # Catch TrackerError and print error messages.
76 print(err.message)
77 else:
78 # Re-run the program with admin rights
79 ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
80
81 if __name__ == "__main__":
82 main()