Skip to content

Commit d638c04

Browse files
committed
added individual motion tracker
1 parent 5485b4e commit d638c04

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
@@ -28,6 +28,14 @@ std::array<int64_t, 24> BodyJointsTimestamp; // IMU timestamp for each joint
2828
int64_t BodyTimeStampNs = 0; // Body data timestamp
2929
bool BodyDataAvailable = false; // Flag to indicate if body data is available
3030

31+
std::array<std::array<double, 7>, 3> MotionTrackerPose; // Position and rotation for each joint
32+
std::array<std::array<double, 6>, 3> MotionTrackerVelocity; // Velocity and angular velocity for each joint
33+
std::array<std::array<double, 6>, 3> MotionTrackerAcceleration; // Acceleration and angular acceleration for each joint
34+
std::array<std::string, 3> MotionTrackerSerialNumbers; // Serial numbers of the motion trackers
35+
int64_t MotionTimeStampNs = 0; // Motion data timestamp
36+
int NumMotionDataAvailable = 0; // number of motion trackers
37+
38+
3139
bool LeftMenuButton;
3240
double LeftTrigger;
3341
double LeftGrip;
@@ -53,6 +61,7 @@ std::mutex timestampMutex;
5361
std::mutex leftHandMutex;
5462
std::mutex rightHandMutex;
5563
std::mutex bodyMutex; // Mutex for body tracking data
64+
std::mutex motionMutex;
5665

5766
std::array<double, 7> stringToPoseArray(const std::string& poseStr) {
5867
std::array<double, 7> result{0};
@@ -205,6 +214,44 @@ void OnPXREAClientCallback(void* context, PXREAClientCallbackType type, int stat
205214
}
206215
}
207216
}
217+
//parse individual tracker data
218+
if (value.contains("Motion")) {
219+
auto& motion = value["Motion"];
220+
{
221+
std::lock_guard<std::mutex> lock(motionMutex);
222+
if (motion.contains("timeStampNs")) {
223+
MotionTimeStampNs = motion["timeStampNs"].get<int64_t>();
224+
}
225+
if (motion.contains("joints") && motion["joints"].is_array()) {
226+
auto joints = motion["joints"];
227+
NumMotionDataAvailable = std::min(static_cast<int>(joints.size()), 3);
228+
229+
for (int i = 0; i < NumMotionDataAvailable; i++) {
230+
auto& joint = joints[i];
231+
232+
// Parse pose (position and rotation)
233+
if (joint.contains("p")) {
234+
MotionTrackerPose[i] = stringToPoseArray(joint["p"].get<std::string>());
235+
}
236+
237+
// Parse velocity and angular velocity
238+
if (joint.contains("va")) {
239+
MotionTrackerVelocity[i] = stringToVelocityArray(joint["va"].get<std::string>());
240+
}
241+
242+
// Parse acceleration and angular acceleration
243+
if (joint.contains("wva")) {
244+
MotionTrackerAcceleration[i] = stringToVelocityArray(joint["wva"].get<std::string>());
245+
}
246+
247+
if (joint.contains("sn")) {
248+
MotionTrackerSerialNumbers[i] = joint["sn"].get<std::string>();
249+
}
250+
}
251+
252+
}
253+
}
254+
}
208255
}
209256
} catch (const json::exception& e) {
210257
std::cerr << "JSON parsing error: " << e.what() << std::endl;
@@ -365,6 +412,53 @@ int64_t getBodyTimeStampNs() {
365412
return BodyTimeStampNs;
366413
}
367414

415+
int numMotionDataAvailable() {
416+
std::lock_guard<std::mutex> lock(motionMutex);
417+
return NumMotionDataAvailable;
418+
}
419+
420+
std::vector<std::array<double, 7>> getMotionTrackerPose() {
421+
std::lock_guard<std::mutex> lock(motionMutex);
422+
std::vector<std::array<double, 7>> result;
423+
for (int i = 0; i < NumMotionDataAvailable; i++) {
424+
result.push_back(MotionTrackerPose[i]);
425+
}
426+
return result;
427+
}
428+
429+
std::vector<std::array<double, 6>> getMotionTrackerVelocity() {
430+
std::lock_guard<std::mutex> lock(motionMutex);
431+
std::vector<std::array<double, 6>> result;
432+
for (int i = 0; i < NumMotionDataAvailable; i++) {
433+
result.push_back(MotionTrackerVelocity[i]);
434+
}
435+
return result;
436+
}
437+
438+
std::vector<std::array<double, 6>> getMotionTrackerAcceleration() {
439+
std::lock_guard<std::mutex> lock(motionMutex);
440+
std::vector<std::array<double, 6>> result;
441+
for (int i = 0; i < NumMotionDataAvailable; i++) {
442+
result.push_back(MotionTrackerAcceleration[i]);
443+
}
444+
return result;
445+
}
446+
447+
std::vector<std::string> getMotionTrackerSerialNumbers() {
448+
std::lock_guard<std::mutex> lock(motionMutex);
449+
std::vector<std::string> result;
450+
for (int i = 0; i < NumMotionDataAvailable; i++) {
451+
result.push_back(MotionTrackerSerialNumbers[i]);
452+
}
453+
return result;
454+
}
455+
456+
int64_t getMotionTimeStampNs() {
457+
std::lock_guard<std::mutex> lock(motionMutex);
458+
return MotionTimeStampNs;
459+
}
460+
461+
368462
PYBIND11_MODULE(xrobotoolkit_sdk, m) {
369463
m.def("init", &init, "Initialize the PXREARobot SDK.");
370464
m.def("close", &deinit, "Deinitialize the PXREARobot SDK.");
@@ -396,6 +490,14 @@ PYBIND11_MODULE(xrobotoolkit_sdk, m) {
396490
m.def("get_body_joints_acceleration", &getBodyJointsAcceleration, "Get the body joints acceleration data (24 joints, 6 values each: ax,ay,az,wax,way,waz).");
397491
m.def("get_body_joints_timestamp", &getBodyJointsTimestamp, "Get the body joints IMU timestamp data (24 joints).");
398492
m.def("get_body_timestamp_ns", &getBodyTimeStampNs, "Get the body data timestamp in nanoseconds.");
493+
494+
// Motion tracker functions
495+
m.def("num_motion_data_available", &numMotionDataAvailable, "Check if motion tracker data is available.");
496+
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).");
497+
m.def("get_motion_tracker_velocity", &getMotionTrackerVelocity, "Get the motion tracker velocity data (3 trackers, 6 values each: vx,vy,vz,wx,wy,wz).");
498+
m.def("get_motion_tracker_acceleration", &getMotionTrackerAcceleration, "Get the motion tracker acceleration data (3 trackers, 6 values each: ax,ay,az,wax,way,waz).");
499+
m.def("get_motion_tracker_serial_numbers", &getMotionTrackerSerialNumbers, "Get the serial numbers of the motion trackers.");
500+
m.def("get_motion_timestamp_ns", &getMotionTimeStampNs, "Get the motion data timestamp in nanoseconds.");
399501

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

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)