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.).
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace PsTech.TrackingTarget
{
public class TrackingTarget
{
[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;
}
static bool Running = true;
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");
}
}
}
static void Exithandler(int sig)
{
Tracker.Shutdown();
Running = false;
}
static int Main(string[] args)
{
ConsoleCtrlEventHandler += new ConsoleEventHandler(ConsoleHandler);
SetConsoleCtrlHandler(ConsoleCtrlEventHandler, true);
try
{
Tracker tracker = new Tracker();
Console.Write("Running PST Server version " + tracker.GetVersionInfo() + "\n");
TrackerListener listener = new MyTrackerListener();
tracker.AddTrackerListener(ref listener);
Console.Write("Put the Reference card in front of the PST in order to see tracking results.\n\n");
tracker.StartTracker();
Console.Write("System check: " + tracker.Systemcheck() + "\n\n");
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");
tracker.SetTargetStatus("Reference", true);
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");
while (Running)
{
Thread.Sleep(100);
}
}
{
Console.Write(e + "\n");
}
finally
{
Tracker.Shutdown();
}
Console.Write("Press enter to continue...\n");
Console.Read();
return 0;
}
}
}