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

K2API: (4) Updating tracker's data & position

公彦赤屋先 edited this page Aug 30, 2021 · 2 revisions

Updating tracker's position (Directly and with K2TrackerBase object)

After you have successfully created and connected your tracker, you're ready to update its position.
You can either create a new pose object or just cast one at the function all.

ktvr::update_tracker_pose(id,
		ktvr::K2TrackerPose( // Constructed at the call itself
			Eigen::Quaternionf(0.707, 0.0, 0.707, 0.0), // same as 0,PI/2,0
			Eigen::Vector3f(1, 1, 0)));

This function will return the tracker's ID if it was successful or -1 for failure.
As said before, you can also construct some object to update the tracker later:

auto pose = ktvr::K2TrackerPose(
    Eigen::Quaternionf(0.707, 0.0, 0.707, 0.0), // Orienatation
    Eigen::Vector3f(1, 1, 0)); // Position

// Note: this may fail to compile if converting constructor isn't registered
ktvr::update_tracker_pose(id, pose); // Now update

Anyway, you can also create a ktvr::K2TrackerBase and overwrite its internal position and orientation,
and when needed you can poll data from it, straight to the function call.

auto tracker = ktvr::K2TrackerBase();

// Some time later
tracker.pose.position = Eigen::Vector3f(1, 1, 0);
tracker.pose.orientation = Eigen::Quaternionf(0.707, 0.0, 0.707, 0.0);

// Some time later
// Note: this may fail to compile if converting constructor isn't registered
ktvr::update_tracker_pose(id, tracker.pose);

// Note: you may also use converting constructors if they're registered
ktvr::update_tracker_pose(id, tracker);

At the end, you can also construct a ktvr::K2PosePacket and give it a time offset in which the tracker should update.

ktvr::K2PosePacket packet;
packet.orientation = Eigen::Quaternionf(1.f, 0.f, 0.f, 0.f);
packet.position = Eigen::Vector3f(0.f, 0.f, 0.f);
packet.millisFromNow = 1000;

Here, after calling ktvr::update_tracker_pose, the tracker will update its pose right after waiting 1000 milliseconds.

Updating tracker's data (Directly and with K2TrackerBase object)

After you have successfully created your tracker, you're ready to update its data.
This way, you can also change prepended trackers' serials and roles.
You can either create a new data object or just cast one at the function all.
Although, you can only do this until you spawn your new tracker by setting its state to true!

ktvr::update_tracker_data(id,
		ktvr::K2TrackerData( // Constructed at the call itself
			"LHR-000000", 
			Tracker_Handed));

This function will return the tracker's ID if it was successful or -1 for failure.
As said before, you can also construct some object to update the tracker later:

auto data = ktvr::K2TrackerData(
			"LHR-000000", // Serial
			Tracker_Handed); // Tracker's role, see other pages

ktvr::update_tracker_data(id, data); // Now update

Anyway, you can also create a ktvr::K2TrackerBase and overwrite its internal data,
and when needed you can poll data from it, straight to the function call.

auto tracker = ktvr::K2TrackerBase();

// Some time later
tracker.data.serial = "LHR-000000";
tracker.data.role = Tracker_Handed;

// Some time later
ktvr::update_tracker_data(id, tracker.data);

At the end, you can also construct a ktvr::K2DataPacket and give it a time offset in which the tracker should update.

ktvr::K2DataPacket packet;
packet.serial = "LHR-000000";
packet.role = Tracker_Handed;
packet.millisFromNow = 1000;

Here, after calling ktvr::update_tracker_data, the tracker will update its data right after waiting 1000 milliseconds.

Updating whole tracker (Directly and with K2TrackerBase object)

After you have successfully created and connected your tracker, or either if you haven't connected it yet, you still can update it.
You should use a base object for this. It will have its ID set.

auto tracker = ktvr::K2TrackerBase();

// Add the tracker, K2API will overwrite it's id
ktvr::add_tracker(tracker);

// Change its pose
tracker.pose.position = Eigen::Vector3f(1, 1, 0);
tracker.pose.orientation = Eigen::Quaternionf(0.707, 0.0, 0.707, 0.0);

// Change its data, will happen only if it's not added yet
tracker.data.serial = "LHR-000000";
tracker.data.role = Tracker_Handed;

// Change its data, can happen normally, let's say we want to turn it off
tracker.data.isActive = false;

// Update it
ktvr::update_tracker_data(tracker);

Here, tracker will update everything it can.
Such an action should take about 2ms, since it will send 2 messages.

Of course, you can check if the action succeed.
After calling any command, you can check if it returned success with
checking the success bool. You may also check the type.

// Let's say we were updating a tracker.
auto response = ktvr::update_tracker_data(tracker);

// Check if there was an error
std::cout << response.success ? "Success updating the tracker!" :
    "Failure updating the tracker!";

// Check for the message type
std::cout << response.messageType == ktvr::K2ResponseMessage_Invalid ? 
    "Failure updating the tracker, the response had invalid type!" :
    "Message type was not invalid, type code: " + std::to_string(response.messageType);