TrackerManager Programmer Notes - HipsterSloth/PSVRTracker GitHub Wiki

TrackerManager

The TrackerManager class is responsible for managing all connected Sony tracking cameras that PSMoveService cares about (i.e. the PS4Camera). On a regular interval it polls all connected tracking cameras and extracts any tracking projections it can find. These tracking projections are used to compute an optical tracking pose for each controller. Each tracker is an instance of the ServerTrackerView class which does all of the heavy lifting for processing the video frame and extracting the tracking projection.

Members

TrackerManagerConfig

The class contains the following configuration parameters:

  • version
    • If the version id loaded from the config file is older than the current version of the TrackerManagerConfig structure, we ignore the config data and fall back to defaults.
    • Bump this value when there are breaking changes to the tracker manager config
  • default_tracker_profile
    • The default hue, saturation and value filters for each tracking color
    • This is used by the color calibration tool when applying or setting the default
  • global_forward_degrees
    • The angle from the global +X axis in an OpenGL left handed coordinate system that's considered "forward" in the psmoveservice tracking system.
    • The default is 270 degrees (down -Z)
  • optical_tracking_timeout
    • The number of ms without seeing a tracking projection before we consider the controller no longer optically tracked
  • use_bgr_to_hsv_lookup_table
    • Flag to enable BGR to HSV lookup table optimization (eats ~64MB of memory)
    • Enabled by default
  • exclude_opposed_cameras
    • Flag to disable triangulation between pairs of nearly opposing cameras
  • min_valid_projection_area
    • The minimum controller tracking projection size (in px^2) that's considered valid
  • disable_roi
    • Flag to disable the "Region-of-Interest" optimization
    • This optimization only processes the video frame around the area we last saw the controller

Functions

closeAllTrackers()

Force close all trackers and the refresh the tracker list. Normally we don't refresh the list of connected trackers while PSVRService is running.

ServerTrackerView

The ServerTrackerView class is an abstraction layer for tracker device types derived from the ServerDeviceView class. It also holds data computed from captured video frames, namely controller tracking projections and optical poses.

Members

Shared Video Frame Buffer

Helper class used to write video frames to the shared memory buffer.

Shared Memory Video Stream Count

The number of clients currently reading from the shared memory buffer.

OpenCV Buffer State

Helper class that contains OpenCV buffers and functions used to extract tracking projections from captured video frames

Device

An ITrackerInterface pointer to the underlying video capture device class.

Functions

open()

This function attempts to open a tracker using the given DeviceEnumerator. This is typically performed when the open tracker list has changed.

See Device Enumerator Programmer Notes for more details.

startSharedMemoryVideoStream() / stopSharedMemoryVideoStream()

Starts or stops streaming of the video feed to the shared memory buffer. Keep a ref count of how many clients are following the stream.

poll()

Fetch the next video frame and copy to shared memory.

getDevice()

Return the ITrackerInterface pointer to the underlying tracker device implementation.

// Returns what type of tracker this tracker view represents
CommonDeviceState::eDeviceType getTrackerDeviceType() const;

// Returns what type of driver this tracker uses
ITrackerInterface::eDriverType getTrackerDriverType() const;

// Returns the full usb device path for the controller
std::string getUSBDevicePath() const;

// Returns the name of the shared memory block video frames are written to
std::string getSharedMemoryStreamName() const;

void loadSettings();
void saveSettings();

double getFrameWidth() const;
void setFrameWidth(double value, bool bUpdateConfig);

double getFrameHeight() const;
void setFrameHeight(double value, bool bUpdateConfig);

double getFrameRate() const;
void setFrameRate(double value, bool bUpdateConfig);

double getExposure() const;
void setExposure(double value, bool bUpdateConfig);

double getGain() const;
void setGain(double value, bool bUpdateConfig);

bool computePoseForProjection(
    const struct CommonDeviceTrackingProjection *projection,
    const struct CommonDeviceTrackingShape *tracking_shape,
    const struct CommonDevicePose *pose_guess,
    struct ControllerOpticalPoseEstimation *out_pose_estimate);
bool computePoseForHMD(
    const class ServerHMDView* tracked_hmd,
    const CommonDevicePose *tracker_pose_guess,
    struct HMDOpticalPoseEstimation *out_pose_estimate);

std::vector<CommonDeviceScreenLocation> projectTrackerRelativePositions(
                            const std::vector<CommonDevicePosition> &objectPositions) const;

CommonDeviceScreenLocation projectTrackerRelativePosition(const CommonDevicePosition *trackerRelativePosition) const;

CommonDevicePosition computeWorldPosition(const CommonDevicePosition *tracker_relative_position) const;
CommonDeviceQuaternion computeWorldOrientation(const CommonDeviceQuaternion *tracker_relative_orientation) const;

CommonDevicePosition computeTrackerPosition(const CommonDevicePosition *world_relative_position) const;
CommonDeviceQuaternion computeTrackerOrientation(const CommonDeviceQuaternion *world_relative_orientation) const;

/// Given a single screen location on two different trackers, compute the triangulated world space location
static CommonDevicePosition triangulateWorldPosition(
    const ServerTrackerView *tracker, const CommonDeviceScreenLocation *screen_location,
    const ServerTrackerView *other_tracker, const CommonDeviceScreenLocation *other_screen_location);

/// Given a set of screen locations on two different trackers, compute the triangulated world space locations
static void triangulateWorldPositions(
    const ServerTrackerView *tracker, 
    const CommonDeviceScreenLocation *screen_locations,
    const ServerTrackerView *other_tracker,
    const CommonDeviceScreenLocation *other_screen_locations,
    const int screen_location_count,
    CommonDevicePosition *out_result);

/// Given screen projections on two different trackers, compute the triangulated world space location
static CommonDevicePose triangulateWorldPose(
    const ServerTrackerView *tracker, const CommonDeviceTrackingProjection *tracker_relative_projection,
    const ServerTrackerView *other_tracker, const CommonDeviceTrackingProjection *other_tracker_relative_projection);

void getCameraIntrinsics(
    float &outFocalLengthX, float &outFocalLengthY,
    float &outPrincipalX, float &outPrincipalY,
    float &outDistortionK1, float &outDistortionK2, float &outDistortionK3,
    float &outDistortionP1, float &outDistortionP2) const;
void setCameraIntrinsics(
    float focalLengthX, float focalLengthY,
    float principalX, float principalY,
    float distortionK1, float distortionK2, float distortionK3,
    float distortionP1, float distortionP2);

CommonDevicePose getTrackerPose() const;
void setTrackerPose(const struct CommonDevicePose *pose);

void getPixelDimensions(float &outWidth, float &outHeight) const;
void getFOV(float &outHFOV, float &outVFOV) const;
void getZRange(float &outZNear, float &outZFar) const;

void gatherTrackerOptions(PSMoveProtocol::Response_ResultTrackerSettings* settings) const;
bool setOptionIndex(const std::string &option_name, int option_index);
bool getOptionIndex(const std::string &option_name, int &out_option_index) const;

void gatherTrackingColorPresets(const class ServerControllerView *controller, PSMoveProtocol::Response_ResultTrackerSettings* settings) const;
void gatherTrackingColorPresets(const class ServerHMDView *hmd, PSMoveProtocol::Response_ResultTrackerSettings* settings) const;

void setControllerTrackingColorPreset(const class ServerControllerView *controller, eCommonTrackingColorID color, const CommonHSVColorRange *preset);
void getControllerTrackingColorPreset(const class ServerControllerView *controller, eCommonTrackingColorID color, CommonHSVColorRange *out_preset) const;

void setHMDTrackingColorPreset(const class ServerHMDView *controller, eCommonTrackingColorID color, const CommonHSVColorRange *preset);
void getHMDTrackingColorPreset(const class ServerHMDView *controller, eCommonTrackingColorID color, CommonHSVColorRange *out_preset) const;

ITrackerInterface

PS3EyeTracker