PST SDK 7.0.0.0-ebe6e713
Loading...
Searching...
No Matches
trackingtarget.cs

This example can be found in examples\csharp\trackingtarget\trackingtarget.cs. When the PST SDK has been installed through the PST Software Suite installer the examples can be found in the Development folder.

This example shows how to work with tracking targets using the PST SDK. Note that at this moment tracking targets can not be trained or imported using the PST SDK. In order to add new tracking targets, please use the PST Client together with the PSTech.Pstsdk.Tracker.EnableSharedMemory() fuction, or use the stand-alone PST Server to configure the tracking targets.

When compiling and running this example, please make sure that the required dependencies can be found by the executable (e.g. by copying the Redist directory into the build directory. When the PST SDK has been installed through the PST Software Suite installer the Redist folder can be found in the Development folder.).

// Copyright PS-Tech B.V. All Rights Reserved.
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace PsTech.TrackingTarget
{
public class TrackingTarget
{
//Registering control handler function.
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(ConsoleEventHandler handler, bool add);
private delegate bool ConsoleEventHandler(CtrlType sig);
static ConsoleEventHandler ConsoleCtrlEventHandler;
public enum CtrlType
{
CtrlCEvent = 0,
CtrlBreakEvent = 1,
CtrlCloseEvent = 2,
CtrlLogOffEvent = 5,
CtrlShutdownEvent = 6,
}
private static bool ConsoleHandler(CtrlType sig)
{
Exithandler((int)sig);
return true;
}
// Control variable for main loop.
static bool Running = true;
// Number of data points to grab before application termination.
static int NumberOfSamplesToGrab = 100;
static int SamplesGrabbed = 0;
class MyTrackerListener : TrackerListener
{
public override void OnTrackerData(TrackerData data, ErrorStatus status)
{
if (SamplesGrabbed++ >= NumberOfSamplesToGrab)
{
Running = false;
}
foreach (TargetPose pose in data.TargetPoseList)
{
Console.Write("Detected " + pose.Target.Name + "\n");
}
// Do something with the received data.
}
}
static void Exithandler(int sig)
{
Tracker.Shutdown(); // Cleans up native resources only; does NOT remove listeners
Running = false;
}
static int Main(string[] args)
{
// Register the exit handler with the application
ConsoleCtrlEventHandler += new ConsoleEventHandler(ConsoleHandler);
SetConsoleCtrlHandler(ConsoleCtrlEventHandler, true);
// Implement error handling of PSTech.TrackerException exceptions to prevent
// improper PST Tracker shutdown on errors.
try
{
// Create an instance of the Tracker object using the default configuration path and file names.
// Wrap the Tracker in a using statement to ensure Dispose() is called automatically
using (Tracker tracker = new Tracker())
{
// Print version number of the tracker server being used.
Console.Write("Running PST Server version " + tracker.GetVersionInfo() + "\n");
//Create tracker listener with callback functions for data and/or mode updates.
TrackerListener listener = new MyTrackerListener();
// Register the TrackerListener object to the tracker server.
tracker.AddTrackerListener(listener);
Console.Write("Put the Reference card in front of the PST in order to see tracking results.\n\n");
// Start the tracker server.
tracker.StartTracker();
// Perform a system check to see if the tracker server is running OK and print the result.
Console.Write("System check: " + tracker.Systemcheck() + "\n\n");
// Retrieve the list of registered tracking targets and print their names and current status (active or not).
TargetStatus[] targets = tracker.GetTargetList();
Console.Write("Found " + targets.Length + " tracking targets:\n");
foreach (TargetStatus t in targets)
{
Console.Write(t.Target.Name + "\t" + t.Status + "\n");
}
Console.Write("\n");
// Enable the Reference target. Note that this will cause a PSTech.TrackerException in case the Reference
// target has not been created. The Reference target can be created using the PST Client.
tracker.SetTargetStatus("Reference", true);
// Get the 3D marker positions making up the Reference device and display them.
// Note that this will cause a PSTech.TrackerException in case the Reference target has not been created.
TargetMarker[] markers = tracker.GetTargetMarkers("Reference");
Console.Write("3D marker positions making up the Reference target:\n");
foreach (TargetMarker marker in markers)
{
Console.Write("x: " + marker.Coordinates[0] + "\t" + "y: " + marker.Coordinates[1] + "\t" + "z: " + marker.Coordinates[2] + "\n");
}
Console.Write("\n");
// Main loop, wait for auto-termination.
while (Running)
{
Thread.Sleep(100);
}
}
}
catch (Exception e)
{
// Catch PSTech.TrackerException exceptions and print error messages.
Console.Write(e + "\n");
}
// Pause command line to see results.
Console.Write("Press enter to continue...\n");
Console.Read();
return 0;
}
}
}
Target Target
Definition TargetPose.cs:41
Tracking information retrieved from tracker.
Definition TrackerData.cs:25
TargetPose[] TargetPoseList
Definition TrackerData.cs:58
void StartTracker()
Start tracking.
Definition Tracker.cs:524
StatusMessage Systemcheck()
Check if the tracker is running correctly.
Definition Tracker.cs:568
void AddTrackerListener(TrackerListener listener)
Add a listener for tracker data and/or mode change reporting.
Definition Tracker.cs:476
void SetTargetStatus(string targetName, bool active)
Set status of a single tracking Target.
Definition Tracker.cs:842
TargetMarker[] GetTargetMarkers(string targetName)
Get 3D marker positions of stored tracking Target.
Definition Tracker.cs:869
TargetStatus[] GetTargetList()
Get TargetStatuses object containing all tracking targets and their status.
Definition Tracker.cs:806
string GetVersionInfo()
Get version information of the SDK.
Definition Tracker.cs:400
Implements the TrackerListener class to receive tracking information and tracking mode updates.
Definition Tracker.cs:166
Definition CApi.cs:9
ErrorStatus
Tracker error messages enum class.
Definition ErrorHandler.cs:17
string Name
User given name of the tracking target as listed in the PST-Client software.
Definition Tracker.cs:37
float[] Coordinates
3D marker position of a tracking Target.
Definition Tracker.cs:93
Target Target
Tracking target information.
Definition Tracker.cs:79
bool Status
Status of the tracking target (active or inactive) as listed in the PST-Client software.
Definition Tracker.cs:82