Skip to content

Commit 6399f74

Browse files
authored
Merge pull request #1 from XR-Robotics/dev/hand_tracking
Dev/hand tracking
2 parents 6ecda61 + 9a6d0ee commit 6399f74

File tree

5 files changed

+83
-10
lines changed

5 files changed

+83
-10
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,21 @@ print(f"Left Axis: {left_axis}, Right Axis Clicked: {right_axis_click}")
6868
timestamp = xrt.get_time_stamp_ns()
6969
print(f"Current Timestamp (ns): {timestamp}")
7070

71+
xrt.close()
72+
```
73+
74+
**3. Get hand tracking state**
75+
```python
76+
import xrobotoolkit_sdk as xrt
77+
78+
xrt.init()
79+
80+
# Left Hand State
81+
left_hand_tracking_state = xrt.get_left_hand_tracking_state()
82+
print(f"Left Hand State: {left_hand_state}")
83+
# Right Hand State
84+
right_hand_tracking_state = xrt.get_right_hand_tracking_state()
85+
print(f"Right Hand State: {right_hand_tracking_state}")
86+
7187
xrt.close()
7288
```

bindings/py_bindings.cpp

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ std::array<double, 7> LeftControllerPose;
1515
std::array<double, 7> RightControllerPose;
1616
std::array<double, 7> HeadsetPose;
1717

18+
std::array<std::array<double, 7>, 26> LeftHandTrackingState;
19+
double LeftHandScale = 1.0;
20+
std::array<std::array<double, 7>, 26> RightHandTrackingState;
21+
double RightHandScale = 1.0;
22+
1823
bool LeftMenuButton;
1924
double LeftTrigger;
2025
double LeftGrip;
@@ -37,6 +42,8 @@ std::mutex leftMutex;
3742
std::mutex rightMutex;
3843
std::mutex headsetPoseMutex;
3944
std::mutex timestampMutex;
45+
std::mutex leftHandMutex;
46+
std::mutex rightHandMutex;
4047

4148
std::array<double, 7> stringToPoseArray(const std::string& poseStr) {
4249
std::array<double, 7> result{0};
@@ -60,13 +67,13 @@ void OnPXREAClientCallback(void* context, PXREAClientCallbackType type, int stat
6067
std::cout << "server disconnect\n" << std::endl;
6168
break;
6269
case PXREADeviceFind:
63-
std::cout << "device find " << (const char*)userData << std::endl;
70+
std::cout << "device found\n" << (const char*)userData << std::endl;
6471
break;
6572
case PXREADeviceMissing:
66-
std::cout << "device missing " << (const char*)userData << std::endl;
73+
std::cout << "device missing\n" << (const char*)userData << std::endl;
6774
break;
6875
case PXREADeviceConnect:
69-
std::cout << "device connect " << (const char*)userData << status << std::endl;
76+
std::cout << "device connect\n" << (const char*)userData << status << std::endl;
7077
break;
7178
case PXREADeviceStateJson:
7279
auto& dsj = *((PXREADevStateJson*)userData);
@@ -115,6 +122,27 @@ void OnPXREAClientCallback(void* context, PXREAClientCallbackType type, int stat
115122
std::lock_guard<std::mutex> lock(timestampMutex);
116123
TimeStampNs = value["timeStampNs"].get<int64_t>();
117124
}
125+
if (value["Hand"].contains("leftHand")) {
126+
auto& leftHand = value["Hand"]["leftHand"];
127+
{
128+
std::lock_guard<std::mutex> lock(leftHandMutex);
129+
130+
LeftHandScale = leftHand["scale"].get<double>();
131+
for (int i = 0; i < 26; i++) {
132+
LeftHandTrackingState[i] = stringToPoseArray(leftHand["HandJointLocations"][i]["p"].get<std::string>());
133+
}
134+
}
135+
}
136+
if (value["Hand"].contains("rightHand")) {
137+
auto& rightHand = value["Hand"]["rightHand"];
138+
{
139+
std::lock_guard<std::mutex> lock(rightHandMutex);
140+
RightHandScale = rightHand["scale"].get<double>();
141+
for (int i = 0; i < 26; i++) {
142+
RightHandTrackingState[i] = stringToPoseArray(rightHand["HandJointLocations"][i]["p"].get<std::string>());
143+
}
144+
}
145+
}
118146
}
119147
} catch (const json::exception& e) {
120148
std::cerr << "JSON parsing error: " << e.what() << std::endl;
@@ -224,6 +252,26 @@ int64_t getTimeStampNs() {
224252
return TimeStampNs;
225253
}
226254

255+
std::array<std::array<double, 7>, 26> getLeftHandTrackingState() {
256+
std::lock_guard<std::mutex> lock(leftHandMutex);
257+
return LeftHandTrackingState;
258+
}
259+
260+
int getLeftHandScale() {
261+
std::lock_guard<std::mutex> lock(leftHandMutex);
262+
return LeftHandScale;
263+
}
264+
265+
std::array<std::array<double, 7>, 26> getRightHandTrackingState() {
266+
std::lock_guard<std::mutex> lock(rightHandMutex);
267+
return RightHandTrackingState;
268+
}
269+
270+
int getRightHandScale() {
271+
std::lock_guard<std::mutex> lock(rightHandMutex);
272+
return RightHandScale;
273+
}
274+
227275
PYBIND11_MODULE(xrobotoolkit_sdk, m) {
228276
m.def("init", &init, "Initialize the PXREARobot SDK.");
229277
m.def("close", &deinit, "Deinitialize the PXREARobot SDK.");
@@ -245,5 +293,7 @@ PYBIND11_MODULE(xrobotoolkit_sdk, m) {
245293
m.def("get_Y_button", &getLeftSecondaryButton, "Get the left secondary button state.");
246294
m.def("get_B_button", &getRightSecondaryButton, "Get the right secondary button state.");
247295
m.def("get_time_stamp_ns", &getTimeStampNs, "Get the timestamp in nanoseconds.");
296+
m.def("get_left_hand_tracking_state", &getLeftHandTrackingState, "Get the left hand state.");
297+
m.def("get_right_hand_tracking_state", &getRightHandTrackingState, "Get the right hand state.");
248298
m.doc() = "Python bindings for PXREARobot SDK using pybind11.";
249299
}

python/test_binding.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ def run_tests():
6666
print(f"Y Button (Left Secondary): {y_button}")
6767
print(f"B Button (Right Secondary): {b_button}")
6868

69+
# Left Hand State
70+
left_hand_state = xrt.get_left_hand_tracking_state()
71+
print(f"Left Hand State: {left_hand_state}")
72+
# Right Hand State
73+
right_hand_state = xrt.get_right_hand_tracking_state()
74+
print(f"Right Hand State: {right_hand_state}")
75+
6976
# Timestamp
7077
timestamp = xrt.get_time_stamp_ns()
7178
print(f"Timestamp (ns): {timestamp}")
@@ -79,9 +86,9 @@ def run_tests():
7986
except Exception as e:
8087
print(f"An unexpected error occurred: {e}", file=sys.stderr)
8188
finally:
82-
print("\nDeinitializing SDK...")
89+
print("\nClosing SDK...")
8390
xrt.close()
84-
print("SDK Deinitialized.")
91+
print("SDK closed.")
8592
print("Test finished.")
8693

8794

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ def run(self):
131131

132132
setup(
133133
name="xrobotoolkit_sdk",
134-
version="1.0.0",
135-
author="Zhigen Zhao", # Replace with your name
136-
author_email="[email protected]", # Replace with your email
134+
version="1.0.1",
135+
author="Zhigen Zhao",
136+
author_email="[email protected]",
137137
description="A Python binding for XRobotoolkit PC Service SDK using pybind11 and CMake",
138138
long_description="", # Optionally, load from a README.md file
139139
ext_modules=[CMakeExtension("xrobotoolkit_sdk")],

setup_ubuntu.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# export CC=/usr/bin/gcc
2-
# export CXX=/usr/bin/g++
31
mkdir -p tmp
42
cd tmp
53
git clone https://github.com/XR-Robotics/XRoboToolkit-PC-Service.git
@@ -16,4 +14,6 @@ rm -rf tmp
1614

1715
# Build the project
1816
pip install pybind11
17+
18+
pip uninstall -y xrobotoolkit_sdk
1919
python setup.py install

0 commit comments

Comments
 (0)