Loading...
Searching...
No Matches
trackingtarget.c

This example can be found in `examples\c\trackingtarget\trackingtarget.c`.

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 pst_sdk_enable_shared_memory() 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.).

#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#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 "pstsdk_c.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <signal.h>
/* Control variable for main loop */
static sig_atomic_t running = 1;
/* Number of data points to grab before application termination */
static const uint32_t numberOfSamplesToGrab = 100;
/*
* Implementation of a tracker callback function.
* The OnTrackerData() callback function receives the data as soon as it becomes
* available and prints the tracking target pose to the command line.
*/
void OnTrackerData(const PstTrackerData* tracker_data, EPstErrorStatus status)
{
static uint32_t samplesGrabbed = 0;
if (samplesGrabbed++ >= numberOfSamplesToGrab)
{
running = 0;
}
for (int d = 0; d < tracker_data->number_of_targets; ++d)
{
printf("Detected %s \n", tracker_data->targetlist[d].target.name);
}
}
/*
* Implement the exit handler to shut-down the PST Tracker connection on application termination.
*/
static void Exithandler(int sig)
{
pst_sdk_shutdown();
running = 0;
}
// Print the last error message.
void PrintLastErrorMessage()
{
char* last_error_message = NULL;
EPstErrorStatus error_status = pst_alloc_and_get_last_error_message(&last_error_message);
if (error_status != PST_ERROR_STATUS_OK)
{
last_error_message = "Failed to allocate memory error.";
}
printf("last error message: %s \n", last_error_message);
pst_free(last_error_message);
}
// Check error status and shutdown tracker upon error.
void CheckErrorCode(EPstErrorStatus status)
{
if (status != PST_ERROR_STATUS_OK)
{
PrintLastErrorMessage();
pst_sdk_shutdown();
exit(status);
}
}
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
PstTracker ctracker;
#ifdef WIN32
// Create an instance of the Tracker object using the default configuration path and file names.
CheckErrorCode(pst_tracker_init(&ctracker));
#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
CheckErrorCode(pst_tracker_init4(&ctracker, "", "config.cfg", "models.db", argv[1]));
#endif
char* version_string;
CheckErrorCode(pst_tracker_alloc_and_get_version_info(&ctracker, &version_string));
// Print version number of the tracker server being used.
printf("Running PST Server version %s \n", version_string);
pst_free(version_string);
// Register the OnTrackerData callback function to the tracker server.
CheckErrorCode(pst_tracker_add_tracker_data_callback(&ctracker, &OnTrackerData));
printf("Put the Reference card in front of the PST in order to see tracking results.\n\n");
// Start the tracker server.
CheckErrorCode(pst_tracker_start(&ctracker));
// Perform a system check to see if the tracker server is running OK and print the result.
printf("System check: %i \n", pst_tracker_system_check(&ctracker));
// Retrieve the list of registered tracking targets and print their names and current status (active or not).
PstTargetStatus* statuses;
size_t number_of_statuses;
CheckErrorCode(pst_tracker_alloc_and_get_target_list(&ctracker, &statuses, &number_of_statuses));
printf("Found %zu tracking targets:\n", number_of_statuses);
for (int i = 0; i < number_of_statuses; ++i)
{
printf("%s\t%s\n", statuses[i].target.name, statuses[i].status ? "true" : "false");
}
printf("\n");
pst_free(statuses);
// Enable the Reference target. Note that this will return an error in case the Reference
// target has not been created. The Reference target can be created using the PST Client.
CheckErrorCode(pst_tracker_set_target_status(&ctracker, "Reference", true));
PstTargetMarkers marker_list;
//Make sure to initialize the PstImage with ::PstTargetMarkers before using pst_tracker_get_target_markers function.
pst_target_markers_init(&marker_list);
// Get the 3D marker positions making up the Reference device and display them.
// Note that this will cause an error in case the Reference target has not been created.
CheckErrorCode(pst_tracker_get_target_markers(&ctracker, "Reference", &marker_list));
printf("3D marker positions making up the Reference target:\n");
for (int i = 0; i < marker_list.number_of_markers; ++i)
{
printf("x: %f\ty: %f\tz: %f\n", marker_list.markers[i].coordinates[0], marker_list.markers[i].coordinates[1], marker_list.markers[i].coordinates[2]);
}
printf("\n");
//Make sure to destroy PstTargetMarkers after using it.
pst_target_markers_destroy(&marker_list);
// Main loop, wait for auto-termination.
while (running == 1)
{
#ifdef WIN32
Sleep(100);
#else
usleep(100000);
#endif
}
// Make sure that the connection to the PST Tracker is shut down properly.
pst_tracker_destroy(&ctracker);
// Pause command line to see results.
printf("Press enter to continue...\n");
getchar();
return 0;
}
pst_tracker_get_target_markers
EPstErrorStatus pst_tracker_get_target_markers(const PstTracker *ctracker, const char *name, PstTargetMarkers *marker_list)
Get 3D marker positions of stored tracking target.
pst_tracker_alloc_and_get_target_list
EPstErrorStatus pst_tracker_alloc_and_get_target_list(const PstTracker *ctracker, PstTargetStatus **statuses, size_t *number_of_statuses)
Get TargetStatuses object containing all tracking targets and their status.
pst_tracker_init
EPstErrorStatus pst_tracker_init(PstTracker *ctracker)
This function initializes the tracker object.
pst_alloc_and_get_last_error_message
EPstErrorStatus pst_alloc_and_get_last_error_message(char **cstring)
This function returns the last error message that was recorded by the system.
pst_tracker_init4
EPstErrorStatus pst_tracker_init4(PstTracker *ctracker, const char path[], const char config_file[], const char db_file[], const char grabber_name[])
This function initializes the tracker object.
pst_tracker_destroy
void pst_tracker_destroy(PstTracker *ctracker)
pst_sdk_shutdown
void pst_sdk_shutdown()
Shutdown the tracking system, stopping tracking.
pst_tracker_alloc_and_get_version_info
EPstErrorStatus pst_tracker_alloc_and_get_version_info(const PstTracker *ctracker, char **version_string)
Get version information of the SDK.
pst_tracker_set_target_status
EPstErrorStatus pst_tracker_set_target_status(PstTracker *ctracker, const char *name, bool set_active)
Set status of a single tracking Target.
pst_free
void pst_free(void *data)
Free data allocated by the PST SDK.
pst_tracker_start
EPstErrorStatus pst_tracker_start(PstTracker *ctracker)
Start tracking.
pst_target_markers_init
void pst_target_markers_init(PstTargetMarkers *marker_list)
pst_target_markers_destroy
void pst_target_markers_destroy(PstTargetMarkers *marker_list)
pst_tracker_add_tracker_data_callback
EPstErrorStatus pst_tracker_add_tracker_data_callback(PstTracker *ctracker, void(*on_tracker_data)(const PstTrackerData *, EPstErrorStatus))
Add a callback function for tracker data.
pst_tracker_system_check
EPstStatusMessage pst_tracker_system_check(const PstTracker *ctracker)
Check if the tracker is running correctly.
EPstErrorStatus
EPstErrorStatus
Tracker error messages enum class.
Definition pstsdk_c.h:44
PST_ERROR_STATUS_OK
@ PST_ERROR_STATUS_OK
Definition pstsdk_c.h:45
pstsdk_c.h
PstTarget::name
char name[128]
Definition pstsdk_c.h:134
PstTargetMarker::coordinates
float coordinates[3]
Definition pstsdk_c.h:217
PstTargetMarkers
Collection struct for PstTargetMarker.
Definition pstsdk_c.h:229
PstTargetMarkers::number_of_markers
size_t number_of_markers
Definition pstsdk_c.h:231
PstTargetMarkers::markers
PstTargetMarker * markers
Definition pstsdk_c.h:230
PstTargetPose::target
PstTarget target
Definition pstsdk_c.h:200
PstTargetStatus
Tracking target status.
Definition pstsdk_c.h:152
PstTrackerData
Tracking information retrieved from tracker.
Definition pstsdk_c.h:248
PstTrackerData::number_of_targets
size_t number_of_targets
Definition pstsdk_c.h:251
PstTrackerData::targetlist
PstTargetPose * targetlist
Definition pstsdk_c.h:253
PstTracker
Main PST SDK struct for tracker communication.
Definition pstsdk_c.h:264