Skip to content
This repository was archived by the owner on Nov 20, 2022. It is now read-only.

K2API: (3) Creating and spawning a new tracker

公彦赤屋先 edited this page Aug 30, 2021 · 1 revision

Creating a new tracker with ktvr::K2TrackerBase

You're now ready to create and connect your fist tracker with K2API!
You should create a new ktvr::K2TrackerBase or use its constructor.

ktvr::K2TrackerBase base_tracker;

This one will create us a tracker with empty serial, role and 0 position and orientation.
Although, if you want to create nonepmty tracker you can do:

ktvr::K2TrackerBase base_tracker(
		ktvr::K2TrackerPose(
			Eigen::Quaternionf(0.707, 0.707, 0.0, 0.0), // same as PI/2,0,0
			Eigen::Vector3f(1, 0, 1)), 
		ktvr::K2TrackerData(
			"LHR-000000", 
			ktvr::Tracker_Handed));

This tracker will be turned +90 degrees up-down and one meter to the side + one meter to the front.
Furthermore, we gave it a superior serial number and role Tracker_Handed! And all this just at the construction.
(You can also not be constructing an object like that and pass tracker right to the creating function - see lower)

Adding new tracker to SteamVR

When you finally have your tracker object, you can add it to the trackers poll.
Simply call this, and K2Driver will register it right away inside itself.

ktvr::add_tracker(base_tracker);

Remember to grab the output from this function, it's our new tracker's id!
You can also add the new tracker all alone without the tracker object,
let's assume you want to add the previous LHR-000000 just by itself,
without creating an additional object.

ktvr::add_tracker(
    ktvr::K2TrackerBase(
		ktvr::K2TrackerPose(
			glm::quat(glm::vec3(glm::radians(90), 0, 0)), 
			glm::vec3(1, 0, 1)), 
		ktvr::K2TrackerData(
			"LHR-000000", 
			Tracker_Handed)));

Yeah, we've just done the same but without creating another const object you won't use later.
(Actually, you can use it for initializing multiple trackers, and it's kinda useful for maintaining more than one tracker.
For example, you can create a poll of trackers and call update functions just referring to those objects
while updating them somewhere in the background, that may be kind of useful later, huh?)

Tracker's id will be greater than -1 when it was successfully added.
When you've tried to add a tracker with the same serial number,
it would be rejected and the function would return -1 to you.

If everything went okay, the server should output something like this in its log:

New tracker added! Serial: LHR-000000 Role: TrackerRole_Handed

Activating the tracker

Yay! You've just added a new tracker. Beautiful, isn't it? NO - you can't even see it right now...
The first time you're activating a tracker, K2Driver automatically adds it to SteamVR's device list.
To activate your tracker you'll need its id, which you have grabbed at its construction.

ktvr::connect_tracker(tracker_id);

This function should return true if the operation was successful or false if it has failed.
Now your tracker is visible. You may proceed to updating it.

If everything went okay, the server should output something like this in its log:

Tracker id: 0 state has been set to: 1

A new tracker with path /devices/htc/vive_tracker/LHR-000000 will be added.
The server combines your serial and HTC's default device path in: https://github.com/KinectToVR/k2vr-application/blob/master/driver_KinectToVR/K2Tracker.cpp#L258
It is intentional, as otherwise SteamVR may think you're not using a real Puck.

Prepended trackers

Actually, it's sometimes better to use the prepended trackers instead of dynamic ones.
It is used e.g. in KTVR, where dynamically adding or downloading trackers may result in some weird situations.
(This will never happen on high-end CPUs and will happen probably every time on slow ones)

K2Driver has provided 3 default trackers:

        K2Tracker( // WAIST TRACKER
			ktvr::K2TrackerBase(
			ktvr::K2TrackerPose(), // Default pose
			ktvr::K2TrackerData(
				"LHR-CB9AD1T0", // Serial
				ktvr::Tracker_Waist, // Role
				false // AutoAdd
			)
		)),

		K2Tracker( // LEFT FOOT TRACKER
			ktvr::K2TrackerBase(
			ktvr::K2TrackerPose(), // Default pose 
			ktvr::K2TrackerData(
				"LHR-CB9AD1T1", // Serial
				ktvr::Tracker_LeftFoot, // Role
				false // AutoAdd
			)
		)),

		K2Tracker( // RIGHT FOOT TRACKER
			ktvr::K2TrackerBase(
			ktvr::K2TrackerPose(), // Default pose 
			ktvr::K2TrackerData(
				"LHR-CB9AD1T2", // Serial
				ktvr::Tracker_RightFoot, // Role
				false // AutoAdd
			)
		))

you can find them prepended here and checked here.
You can access them by respective IDs: Waist is 0, Left is 1 and Right is 2.
Note that if 2 apps use them at the same time, they'll just go insane. (≧﹏≦)

Clone this wiki locally