In order to be able to run this example, the PST Tracker has to be initialized first. This can be done by starting the PST-Server and the PST-Client applications and making sure the calibration files have been downloaded and a tracking target is available. The tracking target can be the default Reference target or a newly trained or imported target. For more information, please see the Initialization section or check the PST Manual.
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.Listener
{
public class Listener
{
[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;
}
public static void PrintMatrix(float[] matrix)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write(matrix[j + i * 4] + "\t");
}
Console.WriteLine();
}
}
static bool Running = true;
static bool Restart = false;
static int NumberOfSamplesToGrab = 100;
static int SamplesGrabbed = 0;
class MyTrackerListener : TrackerListener
{
public override void OnTrackerData(TrackerData data,
ErrorStatus status)
{
if (SamplesGrabbed++ >= NumberOfSamplesToGrab)
{
Running = false;
}
for (int d = 0; d < data.TargetPoseList.Length; ++d)
{
var mat = data.TargetPoseList[d].PoseMatrix;
Console.Write("Pose for " + data.TargetPoseList[d].Target.Name + "\n");
PrintMatrix(mat);
}
}
{
switch (mode)
{
Console.WriteLine("Tracker tracking");
break;
Console.WriteLine("Tracker paused");
break;
Console.WriteLine("Tracker disconnected");
break;
Console.WriteLine("Tracker reconnected");
Restart = true;
break;
default:
Console.WriteLine("Mode " + (int) mode);
break;
}
}
}
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();
if (tracker.GetUncalibratedCameraUrls().Length > 0)
{
Console.Write("\nNo calibration information could be found in the configuration directory. " +
"Please use the PST Server and PST Client application to initialize the PST Tracker and create/import a tracking target. " +
"More information can be found in the Initialization section of the PST SDK manual and the PST Manual.\n\n");
Console.Write("Press enter to continue...\n");
Console.Read();
return 1;
}
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");
tracker.SetFramerate(30);
Console.Write("Frame rate set to " + tracker.GetFramerate() + "\n");
while (Running)
{
if (Restart)
{
tracker.StartTracker();
Restart = false;
}
Thread.Sleep(100); ;
}
}
{
Console.Write(e + "\n");
Console.Write("Press enter to continue...\n");
Console.Read();
}
finally
{
Tracker.Shutdown();
}
return 0;
}
}
}