| PST SDK 6.0.0.0-272350a

Using PST SDK

The PST SDK can be used to receive tracking results and images from a PST Tracker. Implementing the SDK is done by creating an instance of the PSTech::pstsdk::Tracker class. This class provides access to settings on the PST Tracker and enables its user to receive tracking data.

# Setup Windows

In order to start working with the PST SDK on Windows, create a Visual Studio (2015 or newer) project and add the `include` directory to the include path. When the PST SDK has been installed through the PST Software Suite installer the include folder can be found in the Development folder. Include the header file `pstsdk_cpp.h` in your project. To handle exceptions add the `TrackerExceptions.h` header file as well. The application is dynamically linked using the `pstsdk_cpp.lib` import library and the `pstsdk_cpp.dll` shared library which can be found in the `Redist` directory. When the PST SDK has been installed through the PST Software Suite installer the Redist folder can be found in the Development folder. In order to run the application, copy all required binary dependencies from the `Redist` directory to the location of the application. A list of files to use for specific use-cases can be found in the Redistribution section of the PST SDK overview.

# Setup Linux

In order to start working with the PST SDK on Linux, create a Makefile for your project comparable to the ones found in the examples directory. Supply the linker with an rpath in order to point your executable to the location of the shared libraries (`-Wl,-rpath=/opt/ps-tech/pst/Development/Redist` or `-Wl,-rpath=/opt/ps-tech/pst/Redist`). Link your executable to libpstsdk using `-lpstsdk`. Copy the appropriate camera configuration XML file from the `Redist` directory to the working directory of the executable in order to be able to connect to the PST. It might be necessary to change the path on line 5 of the XML file to the directory containing the `libbaslercameraplugin.so` file.

# Initialization

Before using the PST Tracker for object tracking, it must be initialized with calibration and tracking target information. This information can be provided through the PST SDK directly, or using several alternative methods.

## Calibration

Each time a PST Tracker is connected to a new computer, tracker calibration information needs to be provided. When the PSTech::pstsdk::Tracker object is initialized it checks whether or not calibration information can be found in the specified configuration directory. If no calibration information can be found, a message specifying the download locations of the required files will be printed to the standard output, which is usually the command line. It is also possible to call PSTech::pstsdk::Tracker::GetUncalibratedCameraUrls() or PSTech::pstsdk::Tracker::GetConnectedCameraUrls() to receive a vector of download URLs. A single PST Tracker requires two calibration files. The download URL of these files takes the following form: http://www.ps-tech.com/pstsw/calibrations/iris_####.cam, where #### is the identification number of each camera. After downloading the calibration files and making them available on the local system, they can be uploaded to the configuration directory and loaded into the system using PSTech::pstsdk::Tracker::LoadCalibrationFromLocalPath(). When the PST Tracker that is going to be used is known on forehand, tracker calibration files can also be provided by copying the right files from any medium into the configuration directory manually.

## Tracking Target Setup

A new installation of the PST software will not contain any tracking targets. In order to start tracking, tracking targets need to be added to the model database. When a tracking target model definition is available in the JSON format described in Appendix A of the PST Manual, it can be added to the model database using PSTech::pstsdk::Tracker::ImportJSONModel(). This function takes a JSON-formatted string describing the target model. After calling this function, the model will be available for tracking.

There are also a number of alternative ways to get tracking target models into the model database.

1. Copy an existing tracking target model database (models.db) into the configuration directory.
2. Train a new tracking target model using the 'New device model' tab in the PST-Client.
3. Import an existing tracking target model (JSON or PSM formatted) using the 'Edit device model' tab in the PST-Client.

When tracking target models are added using the PST-Client, a shared memory pipeline needs to be available to connect the PST-Client to the PST Tracker. Shared memory access can be enabled using the C++ API by calling PSTech::pstsdk::Tracker::EnableSharedMemory(). Implementing this function allows the PST-Client to directly connect to an application. This way, tracking target model data will directly be saved to the model database file set in the application.

Alternatively, the PST-Server application can be started to allow the PST-Client to connect to the PST Tracker.

Note
  On Windows, an application that implements shared memory access must be run with administrator privileges.
  The PST Interface Service will not be available when the PST SDK has been installed.
  The PST-Server and the application implementing the PST SDK can not be active at the same time, since only one application can connect to the PST Tracker at any given time.

The PST-Server will store its model database at `C:\ProgramData\PS-Tech\PST Iris\models.db` on Windows systems and `~/.pstiris/models.db` on Linux systems. The application implementing the PST SDK can either use the same configuration directory and model database file, or the model database file can be copied to the configuration directory used by the application.

# Usage

A clear example of how to use the PST SDK can be found in the Examples section of this documentation. The first step of connecting to the PST Tracker is creating an instance of the PSTech::pstsdk::Tracker class. It is recommended to instantiate the object on the stack or, when heap allocation is required, as a smart pointer. This will make it possible to ensure safe and clean termination of the PST Tracker on program exit. More on this topic will be discussed in the section Safe Tracker Termination.

## Receiving tracker data

Tracking data can be received by implementing the PSTech::pstsdk::Listener class and registering it as a listener to the instantiated PSTech::pstsdk::Tracker object using PSTech::pstsdk::Tracker::AddTrackerListener(). After enabling the required tracking targets using PSTech::pstsdk::Tracker::SetTargetStatus() and calling PSTech::pstsdk::Tracker::Start(), the PSTech::pstsdk::Listener::OnTrackerData() callback function will start receiving tracking data in the form of PSTech::pstsdk::TrackerData structs. These structs contain the time stamp of the recorded data, a recording id, a list of all detected 3D points that could not be matched to any of the activated tracking targets and a list of detected tracking targets and their poses.

While tracking is in progress, settings like the frame rate and exposure can be adjusted using their corresponding functions. It is also possible to change which tracking targets are active.

Note
  When building an application based on the PST SDK, make sure all dependencies are available to the compiled application. Either copy the files from the `Redist` folder to the application's build folder or use the `Redist` folder as the working directory. When the PST SDK has been installed through the PST Software Suite installer the Redist folder can be found in the Development folder.

## Filtering

Settings controlling the amount of filtering that is applied to the tracking data can be adjusted at all times. The filtering strength for the position and orientation can be adjusted independently using PSTech::pstsdk::Tracker::SetPositionFilter() and PSTech::pstsdk::Tracker::SetOrientationFilter() respectively. Filtering using the DESP filter is enabled by default while the tremor filter is disabled by default. When data filtering is part of the end-user application it is recommended to disable PST SDK filtering using PSTech::pstsdk::Tracker::DisableFiltering() to prevent double filtering and over smoothing the data.

Note
  When shared memory is used to connect the PST-Client to the PST SDK, all filtering on the side of the PST SDK will be disabled since the PST-Client performs its own filtering. After closing the PST-Client, filtering should be re-enabled manually by calling PSTech::pstsdk::Tracker::EnableFiltering() and PSTech::pstsdk::Tracker::EnableTremorFilter().

## Camera Images

It is possible to retrieve image data from the cameras inside the PST Tracker. In order to do so, image transfer has to be enabled on the PST Tracker by calling PSTech::pstsdk::Tracker::EnableImageTransfer().

When image transfer has been enabled, images can be requested by calling PSTech::pstsdk::Tracker::GetImage(). Images are provided as a PSTech::pstsdk::Image struct containing the width and height of the images in pixels and a vector containing pointers to the first pixel of the left and right camera image. Images are gray scale and provided as a unsigned char array without memory alignment.

In order to keep receiving images, a call to PSTech::pstsdk::Tracker::GetImage() has to be made at least every 4 seconds. For performance reasons, image transfer will be automatically disabled after no images have been requested for 4 seconds.

Note
  Enabling image transfer when using a standard PST Iris or PST Base will reduce the PST Tracker's frame rate to 30 Hz for as long as images are being requested. When image transfer is disabled, either automatically or manually by calling PSTech::pstsdk::Tracker::DisableImageTransfer(), the frame rate of the PST Tracker will be reset to its original value.

## Reference System

Tracking results are reported relative to a predefined right-handed Cartesian coordinate system, called the reference system. The default reference system is located at 1 meter from the PST Tracker. It is oriented such that the Z-axis points away from the PST tracker and the X-axis is parallel to the PST tracker. The reference system can be changed by the user and is stored as a transformation matrix in a file called `otracker.ref` located in the configuration directory. Setting the reference system can be done in several ways.

1. Use the PSTech::pstsdk::Tracker::SetReference() function to supply a relative or absolute reference system.
2. Use the pose of a tracking target on the 'Reference System' tab in the PST-Client.
3. Enter a relative or absolute reference system transformation on the 'Reference System' tab in the PST-Client.
4. Copy an existing otracker.ref reference system file into the configuration directory.

The PST-Client can be used to set the reference system according to the methods described in section Tracking Target Setup.

## Safe Tracker Termination

The PST Tracking systems are sensitive hardware. Therefore, it is important that the PST Tracker is correctly terminated every time an application connecting to the PST Tracker is closed. Failure to do so might result in unpredictable system behavior and could lead to computer crashes.

In order to safely terminate the connection to the PST Tracker, it is important that the PSTech::pstsdk::Tracker::Shutdown() function is called before the application closes. The destructor of the PSTech::pstsdk::Tracker class will call PSTech::pstsdk::Tracker::Shutdown() automatically, but there are cases in which the destructor might not be called properly. This could happen in case of an application crash, or because the console running the application is closed.

The first cause can be mitigated in most cases by using a try-catch block around the code. Be aware that a number of PST SDK functions can throw PSTech::TrackerException type errors that might need to be caught.

The second case should be caught by implementing the console exit handler. On Windows, this is done by implementing the `SetConsoleCtrlHandler()` function from the Windows API. The callback function registered as the console handler should call PSTech::pstsdk::Tracker::Shutdown(). Since PSTech::pstsdk::Tracker::Shutdown() is a static member function it can be called from any context and does not require an instance of PSTech::pstsdk::Tracker to be available.

Taking the above mentioned precautions should take care of most cases where disconnecting from the PST Tracker can result in unwanted behavior.