Skip to content

Commit 4fbf399

Browse files
authored
Merge pull request #4 from XR-Robotics/dev/motion_tracker
added individual motion tracker
2 parents 3c87def + d638c04 commit 4fbf399

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

bindings/py_bindings.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ std::array<int64_t, 24> BodyJointsTimestamp; // IMU timestamp for each joint
3030
int64_t BodyTimeStampNs = 0; // Body data timestamp
3131
bool BodyDataAvailable = false; // Flag to indicate if body data is available
3232

33+
std::array<std::array<double, 7>, 3> MotionTrackerPose; // Position and rotation for each joint
34+
std::array<std::array<double, 6>, 3> MotionTrackerVelocity; // Velocity and angular velocity for each joint
35+
std::array<std::array<double, 6>, 3> MotionTrackerAcceleration; // Acceleration and angular acceleration for each joint
36+
std::array<std::string, 3> MotionTrackerSerialNumbers; // Serial numbers of the motion trackers
37+
int64_t MotionTimeStampNs = 0; // Motion data timestamp
38+
int NumMotionDataAvailable = 0; // number of motion trackers
39+
40+
3341
bool LeftMenuButton;
3442
double LeftTrigger;
3543
double LeftGrip;
@@ -55,6 +63,7 @@ std::mutex timestampMutex;
5563
std::mutex leftHandMutex;
5664
std::mutex rightHandMutex;
5765
std::mutex bodyMutex; // Mutex for body tracking data
66+
std::mutex motionMutex;
5867

5968
std::array<double, 7> stringToPoseArray(const std::string& poseStr) {
6069
std::array<double, 7> result{0};
@@ -209,6 +218,44 @@ void OnPXREAClientCallback(void* context, PXREAClientCallbackType type, int stat
209218
}
210219
}
211220
}
221+
//parse individual tracker data
222+
if (value.contains("Motion")) {
223+
auto& motion = value["Motion"];
224+
{
225+
std::lock_guard<std::mutex> lock(motionMutex);
226+
if (motion.contains("timeStampNs")) {
227+
MotionTimeStampNs = motion["timeStampNs"].get<int64_t>();
228+
}
229+
if (motion.contains("joints") && motion["joints"].is_array()) {
230+
auto joints = motion["joints"];
231+
NumMotionDataAvailable = std::min(static_cast<int>(joints.size()), 3);
232+
233+
for (int i = 0; i < NumMotionDataAvailable; i++) {
234+
auto& joint = joints[i];
235+
236+
// Parse pose (position and rotation)
237+
if (joint.contains("p")) {
238+
MotionTrackerPose[i] = stringToPoseArray(joint["p"].get<std::string>());
239+
}
240+
241+
// Parse velocity and angular velocity
242+
if (joint.contains("va")) {
243+
MotionTrackerVelocity[i] = stringToVelocityArray(joint["va"].get<std::string>());
244+
}
245+
246+
// Parse acceleration and angular acceleration
247+
if (joint.contains("wva")) {
248+
MotionTrackerAcceleration[i] = stringToVelocityArray(joint["wva"].get<std::string>());
249+
}
250+
251+
if (joint.contains("sn")) {
252+
MotionTrackerSerialNumbers[i] = joint["sn"].get<std::string>();
253+
}
254+
}
255+
256+
}
257+
}
258+
}
212259
}
213260
} catch (const json::exception& e) {
214261
std::cerr << "JSON parsing error: " << e.what() << std::endl;
@@ -379,6 +426,53 @@ int64_t getBodyTimeStampNs() {
379426
return BodyTimeStampNs;
380427
}
381428

429+
int numMotionDataAvailable() {
430+
std::lock_guard<std::mutex> lock(motionMutex);
431+
return NumMotionDataAvailable;
432+
}
433+
434+
std::vector<std::array<double, 7>> getMotionTrackerPose() {
435+
std::lock_guard<std::mutex> lock(motionMutex);
436+
std::vector<std::array<double, 7>> result;
437+
for (int i = 0; i < NumMotionDataAvailable; i++) {
438+
result.push_back(MotionTrackerPose[i]);
439+
}
440+
return result;
441+
}
442+
443+
std::vector<std::array<double, 6>> getMotionTrackerVelocity() {
444+
std::lock_guard<std::mutex> lock(motionMutex);
445+
std::vector<std::array<double, 6>> result;
446+
for (int i = 0; i < NumMotionDataAvailable; i++) {
447+
result.push_back(MotionTrackerVelocity[i]);
448+
}
449+
return result;
450+
}
451+
452+
std::vector<std::array<double, 6>> getMotionTrackerAcceleration() {
453+
std::lock_guard<std::mutex> lock(motionMutex);
454+
std::vector<std::array<double, 6>> result;
455+
for (int i = 0; i < NumMotionDataAvailable; i++) {
456+
result.push_back(MotionTrackerAcceleration[i]);
457+
}
458+
return result;
459+
}
460+
461+
std::vector<std::string> getMotionTrackerSerialNumbers() {
462+
std::lock_guard<std::mutex> lock(motionMutex);
463+
std::vector<std::string> result;
464+
for (int i = 0; i < NumMotionDataAvailable; i++) {
465+
result.push_back(MotionTrackerSerialNumbers[i]);
466+
}
467+
return result;
468+
}
469+
470+
int64_t getMotionTimeStampNs() {
471+
std::lock_guard<std::mutex> lock(motionMutex);
472+
return MotionTimeStampNs;
473+
}
474+
475+
382476
PYBIND11_MODULE(xrobotoolkit_sdk, m) {
383477
m.def("init", &init, "Initialize the PXREARobot SDK.");
384478
m.def("close", &deinit, "Deinitialize the PXREARobot SDK.");
@@ -412,6 +506,14 @@ PYBIND11_MODULE(xrobotoolkit_sdk, m) {
412506
m.def("get_body_joints_acceleration", &getBodyJointsAcceleration, "Get the body joints acceleration data (24 joints, 6 values each: ax,ay,az,wax,way,waz).");
413507
m.def("get_body_joints_timestamp", &getBodyJointsTimestamp, "Get the body joints IMU timestamp data (24 joints).");
414508
m.def("get_body_timestamp_ns", &getBodyTimeStampNs, "Get the body data timestamp in nanoseconds.");
509+
510+
// Motion tracker functions
511+
m.def("num_motion_data_available", &numMotionDataAvailable, "Check if motion tracker data is available.");
512+
m.def("get_motion_tracker_pose", &getMotionTrackerPose, "Get the motion tracker pose data (3 trackers, 7 values each: x,y,z,qx,qy,qz,qw).");
513+
m.def("get_motion_tracker_velocity", &getMotionTrackerVelocity, "Get the motion tracker velocity data (3 trackers, 6 values each: vx,vy,vz,wx,wy,wz).");
514+
m.def("get_motion_tracker_acceleration", &getMotionTrackerAcceleration, "Get the motion tracker acceleration data (3 trackers, 6 values each: ax,ay,az,wax,way,waz).");
515+
m.def("get_motion_tracker_serial_numbers", &getMotionTrackerSerialNumbers, "Get the serial numbers of the motion trackers.");
516+
m.def("get_motion_timestamp_ns", &getMotionTimeStampNs, "Get the motion data timestamp in nanoseconds.");
415517

416518
m.doc() = "Python bindings for PXREARobot SDK using pybind11.";
417519
}

examples/example_motion_tracker.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import xrobotoolkit_sdk as xrt
2+
3+
xrt.init()
4+
num_motion_data = xrt.num_motion_data_available()
5+
print(f"Number of Motion Trackers: {num_motion_data}")
6+
if num_motion_data > 0:
7+
motion_tracker_pose = xrt.get_motion_tracker_pose()
8+
motion_tracker_velocity = xrt.get_motion_tracker_velocity()
9+
motion_tracker_acceleration = xrt.get_motion_tracker_acceleration()
10+
motion_tracker_serial_numbers = xrt.get_motion_tracker_serial_numbers()
11+
motion_timestamp_ns = xrt.get_motion_timestamp_ns()
12+
13+
print(f"Motion Tracker Pose: {motion_tracker_pose}")
14+
print(f"Motion Tracker Velocity: {motion_tracker_velocity}")
15+
print(f"Motion Tracker Acceleration: {motion_tracker_acceleration}")
16+
print(f"Motion Tracker Serial Numbers: {motion_tracker_serial_numbers}")
17+
print(f"Motion Timestamp (ns): {motion_timestamp_ns}")

examples/run_binding_continuous.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ def run_tests():
7777
timestamp = xrt.get_time_stamp_ns()
7878
print(f"Timestamp (ns): {timestamp}")
7979

80+
num_motion_data = xrt.num_motion_data_available()
81+
print(f"Number of Motion Trackers: {num_motion_data}")
82+
if num_motion_data > 0:
83+
motion_tracker_pose = xrt.get_motion_tracker_pose()
84+
motion_tracker_velocity = xrt.get_motion_tracker_velocity()
85+
motion_tracker_acceleration = xrt.get_motion_tracker_acceleration()
86+
motion_tracker_serial_numbers = xrt.get_motion_tracker_serial_numbers()
87+
motion_timestamp_ns = xrt.get_motion_timestamp_ns()
88+
89+
print(f"Motion Tracker Pose: {motion_tracker_pose}")
90+
print(f"Motion Tracker Velocity: {motion_tracker_velocity}")
91+
print(f"Motion Tracker Acceleration: {motion_tracker_acceleration}")
92+
print(f"Motion Tracker Serial Numbers: {motion_tracker_serial_numbers}")
93+
print(f"Motion Timestamp (ns): {motion_timestamp_ns}")
94+
8095
time.sleep(0.5) # Wait for 0.5 seconds before the next iteration
8196

8297
print("\nAll iterations complete.")

setup_ubuntu.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
if [[ "$CONDA_DEFAULT_ENV" != "" ]]; then
2+
conda install -c conda-forge libstdcxx-ng -y
3+
fi
4+
15
mkdir -p tmp
26
cd tmp
37
git clone https://github.com/XR-Robotics/XRoboToolkit-PC-Service.git

0 commit comments

Comments
 (0)