PST SDK  6.0.0.0-272350a
images.cpp
#ifdef WIN32
#include <windows.h>
#else
#include <csignal>
#endif
/*
* Define handler functions required to ensure a clean shutdown of the PST Tracker when the
* application is terminated.
*/
static void Exithandler(int sig);
#ifdef WIN32
BOOL WINAPI ConsoleHandler(DWORD CEvent)
{
Exithandler(CEvent);
return TRUE;
}
#endif
/* End of handler functions */
#include <iostream>
#include <thread>
#include <chrono>
#include <atomic>
#include "pstsdk_cpp.h"
/* Control variable for main loop */
static std::atomic<bool> running(true);
/* Number of data points to grab before application termination */
static const uint32_t numberOfSamplesToGrab = 1000;
/*
* Implementation of the PSTech::pstsdk::Listener class to receive tracking data.
* The OnTrackerData() callback function receives the data as soon as it becomes
* available.
*/
class MyListener : public PSTech::pstsdk::Listener
{
{
static uint32_t samplesGrabbed = 0;
if (samplesGrabbed++ >= numberOfSamplesToGrab)
running = false;
// Do something with the received data.
}
} listener;
/*
* Implement the exit handler to shut-down the PST Tracker connection on application termination.
*/
static void Exithandler(int sig)
{
exit(0);
}
int main(int argc, char *argv[])
{
// Register the exit handler with the application
#ifdef WIN32
SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler, TRUE);
#else
signal(SIGTERM, Exithandler);
signal(SIGKILL, Exithandler);
signal(SIGQUIT, Exithandler);
signal(SIGINT, Exithandler);
#endif
// 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.
#ifdef WIN32
#else
// On Linux, specify the type of grabber that needs to be used as the last parameter:
// "basler_ace" for PST HD or "basler_dart" for PST Pico
PSTech::pstsdk::Tracker pst("","config.cfg","models.db",argv[1]);
#endif
// Print version number of the tracker server being used.
std::cout << "Running PST Server version " << pst.GetVersionInfo() << "\n";
// Register the listener object to the tracker server.
pst.AddTrackerListener(&listener);
// Start the tracker server.
pst.Start();
// Perform a system check to see if the tracker server is running OK and print the result.
std::cout << "System check: " << (int)pst.Systemcheck() << "\n\n";
std::cout << "***************************\n\n";
// Set the frame rate to 60 Hz.
pst.SetFramerate(60);
std::cout << "Current frame rate: " << pst.GetFramerate() << " Hz\n\n";
// In order to start receiving images, enable image transfer. When image transfer is disabled,
// the vector of images returned by GetImage will be empty.
// The standard PST trackers will run at a reduced frame rate of 30 Hz when image transfer is enabled.
// However, since this frame rate is temporary for as long as image transfer is enabled, that frame rate
// will not be reported as the current frame rate.
std::cout << "Enabled image transfer. Current frame rate: " << pst.GetFramerate() << " Hz\n\n";
std::cout << "***************************\n\n";
// Try to capture 100 images.
for (int i = 0; i < 100; ++i)
{
// Get a reference to the last grabbed image in the PSTech::pstsdk::Image struct.
// Note that enabling image transfer takes some time. While image transfer is being enabled,
// the images vector in the PSTech::pstsdk::Image struct will be empty.
bool succes = pst.GetImage(images);
std::cout << "Retrieval operation " << ((succes) ? "successful!\n" : "unsuccessful!\n\n");
if (succes)
{
std::cout << "Retrieved " << images.images.size() << " image(s) of size: " << images.width << " X " << images.height << "\n\n";
// Do something with the image.
}
// Don't request images too fast, wait for around 1/60 seconds
std::this_thread::sleep_for(std::chrono::milliseconds(17));
}
// Wait for 5 seconds, since this is > 4 seconds, image transfer will be disabled automatically.
std::cout << "Waiting 5 seconds for image transfer to be automatically disabled...\n\n";
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
// Try to grab one image. Since image retrieval timed out, it should return an empty image vector.
bool succes = pst.GetImage(images);
std::cout << "Retrieval operation " << ((succes) ? "successful!\n" : "unsuccessful!");
if (succes)
{
std::cout << "Retrieved " << images.images.size() << " image(s) of size: " << images.width << " X " << images.height << "\n\n";
}
// Main loop, wait for auto-termination.
while (running)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
{
// Catch PSTech::TrackerException exceptions and print error messages.
std::cout << e.full_description() << "\n";
}
// Make sure that the connection to the PST Tracker is shut down properly.
// Pause command line to see results.
std::cout << "Press enter to continue...\n";
std::cin.get();
return 0;
}
PSTech::pstsdk::Tracker
Main PST SDK class implementing tracker communication.
Definition: pstsdk_cpp.h:87
PSTech::pstsdk::Tracker::GetVersionInfo
Utils::PstString GetVersionInfo() const
Get version information of the SDK.
PSTech::pstsdk::Image::images
Utils::PstVector< unsigned char * > images
Definition: PstTypes.h:31
PSTech::pstsdk::TrackerData
Tracking information retrieved from tracker.
Definition: PstTypes.h:157
PSTech::pstsdk::Listener
Abstract listener class for receiving tracking information and tracking mode updates.
Definition: pstsdk_cpp.h:35
PSTech::pstsdk::Tracker::Shutdown
static void Shutdown()
Shutdown the tracking system, stopping tracking.
PSTech::pstsdk::Image::width
unsigned int width
Definition: PstTypes.h:29
PSTech::pstsdk::Tracker::EnableImageTransfer
void EnableImageTransfer()
Enable image transfer from the PST Tracker.
PSTech::pstsdk::Tracker::GetFramerate
double GetFramerate() const
Get current frame rate.
PSTech::pstsdk::Tracker::AddTrackerListener
void AddTrackerListener(Listener *listener)
Add a listener for receiving tracker data and tracking mode updates.
PSTech::pstsdk::Tracker::GetImage
bool GetImage(Image &image) const
Retrieve images from the connected PST Tracker.
PSTech::pstsdk::Tracker::Systemcheck
StatusMessage Systemcheck() const
Check if the tracker is running correctly.
PSTech::pstsdk::Tracker::Start
void Start()
Start tracking.
pstsdk_cpp.h
PSTech::pstsdk::Image
Images retrieved from the tracker.
Definition: PstTypes.h:27
PSTech::Utils::PstVector::size
size_type size() const
PSTech::pstsdk::Tracker::SetFramerate
void SetFramerate(double fps)
Set tracker frame rate.
PSTech::pstsdk::Listener::OnTrackerData
virtual void OnTrackerData(const TrackerData &data)
Callback function receiving tracking information from the tracker.
Definition: pstsdk_cpp.h:47
TrackerExceptions.h
PSTech::PSTException::full_description
virtual const char * full_description() const
PSTech::TrackerException
Definition: TrackerExceptions.h:49
PSTech::pstsdk::Image::height
unsigned int height
Definition: PstTypes.h:30
PstStringIoStream.h