Skip to content

Commit 74796ec

Browse files
committed
Make exception handling consistent across examples
1 parent ef5f878 commit 74796ec

File tree

7 files changed

+313
-332
lines changed

7 files changed

+313
-332
lines changed

examples/lerobot/record.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ def main():
183183

184184
frame_count += 1
185185
time.sleep(0.016) # ~60 FPS
186-
187186
except KeyboardInterrupt:
188-
print("\nInterrupted by user")
187+
print("\nKeyboardInterrupt received, stopping recording early.")
189188

190189
# STEP 4: Save episode
191190
print(f"\nSaving episode with {frame_count} frames...")

examples/native_openxr/xdev_list/main.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <openxr/openxr.h>
88

99
#include <XR_MNDX_xdev_space.h>
10+
#include <exception>
1011
#include <iostream>
12+
#include <stdexcept>
1113
#include <string>
1214
#include <vector>
1315

@@ -179,6 +181,19 @@ class XDevListApp : public HeadlessApp
179181

180182
int main(int argc, char* argv[])
181183
{
182-
XDevListApp app;
183-
return doHeadless(app);
184+
try
185+
{
186+
XDevListApp app;
187+
return doHeadless(app);
188+
}
189+
catch (const std::exception& e)
190+
{
191+
std::cerr << "Error: " << e.what() << std::endl;
192+
return 1;
193+
}
194+
catch (...)
195+
{
196+
std::cerr << "Unknown error occurred" << std::endl;
197+
return 1;
198+
}
184199
}

examples/oxr/python/modular_example.py

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -58,65 +58,61 @@ def main():
5858
frame_count = 0
5959
start_time = time.time()
6060

61-
try:
62-
while time.time() - start_time < 10.0:
63-
# Update session and all trackers
64-
if not session.update():
65-
print("Update failed")
66-
break
67-
68-
# Print every 60 frames (~1 second)
69-
if frame_count % 60 == 0:
70-
elapsed = time.time() - start_time
71-
print(f"[{elapsed:4.1f}s] Frame {frame_count}")
72-
73-
# Get hand data
74-
left_tracked: schema.HandPoseTrackedT = (
75-
hand_tracker.get_left_hand(session)
61+
while time.time() - start_time < 10.0:
62+
# Update session and all trackers
63+
if not session.update():
64+
print("Update failed")
65+
break
66+
67+
# Print every 60 frames (~1 second)
68+
if frame_count % 60 == 0:
69+
elapsed = time.time() - start_time
70+
print(f"[{elapsed:4.1f}s] Frame {frame_count}")
71+
72+
# Get hand data
73+
left_tracked: schema.HandPoseTrackedT = hand_tracker.get_left_hand(
74+
session
75+
)
76+
right_tracked: schema.HandPoseTrackedT = (
77+
hand_tracker.get_right_hand(session)
78+
)
79+
80+
if left_tracked.data is not None:
81+
pos = left_tracked.data.joints.poses(
82+
deviceio.JOINT_WRIST
83+
).pose.position
84+
print(
85+
f" Left wrist: [{pos.x:6.3f}, {pos.y:6.3f}, {pos.z:6.3f}]"
7686
)
77-
right_tracked: schema.HandPoseTrackedT = (
78-
hand_tracker.get_right_hand(session)
87+
else:
88+
print(" Left hand: inactive")
89+
90+
if right_tracked.data is not None:
91+
pos = right_tracked.data.joints.poses(
92+
deviceio.JOINT_WRIST
93+
).pose.position
94+
print(
95+
f" Right wrist: [{pos.x:6.3f}, {pos.y:6.3f}, {pos.z:6.3f}]"
7996
)
80-
81-
if left_tracked.data is not None:
82-
pos = left_tracked.data.joints.poses(
83-
deviceio.JOINT_WRIST
84-
).pose.position
85-
print(
86-
f" Left wrist: [{pos.x:6.3f}, {pos.y:6.3f}, {pos.z:6.3f}]"
87-
)
88-
else:
89-
print(" Left hand: inactive")
90-
91-
if right_tracked.data is not None:
92-
pos = right_tracked.data.joints.poses(
93-
deviceio.JOINT_WRIST
94-
).pose.position
95-
print(
96-
f" Right wrist: [{pos.x:6.3f}, {pos.y:6.3f}, {pos.z:6.3f}]"
97-
)
98-
else:
99-
print(" Right hand: inactive")
100-
101-
# Get head data
102-
head_tracked: schema.HeadPoseTrackedT = head_tracker.get_head(
103-
session
97+
else:
98+
print(" Right hand: inactive")
99+
100+
# Get head data
101+
head_tracked: schema.HeadPoseTrackedT = head_tracker.get_head(
102+
session
103+
)
104+
if head_tracked.data is not None:
105+
pos = head_tracked.data.pose.position
106+
print(
107+
f" Head pos: [{pos.x:6.3f}, {pos.y:6.3f}, {pos.z:6.3f}]"
104108
)
105-
if head_tracked.data is not None:
106-
pos = head_tracked.data.pose.position
107-
print(
108-
f" Head pos: [{pos.x:6.3f}, {pos.y:6.3f}, {pos.z:6.3f}]"
109-
)
110-
else:
111-
print(" Head: inactive")
112-
113-
print()
109+
else:
110+
print(" Head: inactive")
114111

115-
frame_count += 1
116-
time.sleep(0.016) # ~60 FPS
112+
print()
117113

118-
except KeyboardInterrupt:
119-
print("\nInterrupted by user")
114+
frame_count += 1
115+
time.sleep(0.016) # ~60 FPS
120116

121117
# Cleanup
122118
print(f"\nProcessed {frame_count} frames")

examples/oxr/python/test_controller_tracker.py

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -103,48 +103,44 @@ def assert_trackers_consistent(label, ta, tb):
103103
start_time = time.time()
104104
last_status_print = start_time
105105

106-
try:
107-
while time.time() - start_time < 10.0:
108-
if not session.update():
109-
print("Update failed")
110-
break
111-
112-
current_time = time.time()
113-
if current_time - last_status_print >= 0.5:
114-
elapsed = current_time - start_time
115-
left_tracked = controller_tracker_a.get_left_controller(session)
116-
right_tracked = controller_tracker_a.get_right_controller(session)
117-
118-
print(f" [{elapsed:5.2f}s] Frame {frame_count:4d}")
119-
120-
left_data = left_tracked.data
121-
if left_data is not None:
122-
li = left_data.inputs
123-
print(
124-
f" L: Trig={li.trigger_value:.2f} Sq={li.squeeze_value:.2f}"
125-
f" Stick=({li.thumbstick_x:+.2f},{li.thumbstick_y:+.2f})"
126-
f" Btn=[{int(li.primary_click)}{int(li.secondary_click)}{int(li.thumbstick_click)}]"
127-
)
128-
else:
129-
print(" L: INACTIVE")
130-
131-
right_data = right_tracked.data
132-
if right_data is not None:
133-
ri = right_data.inputs
134-
print(
135-
f" R: Trig={ri.trigger_value:.2f} Sq={ri.squeeze_value:.2f}"
136-
f" Stick=({ri.thumbstick_x:+.2f},{ri.thumbstick_y:+.2f})"
137-
f" Btn=[{int(ri.primary_click)}{int(ri.secondary_click)}{int(ri.thumbstick_click)}]"
138-
)
139-
else:
140-
print(" R: INACTIVE")
141-
last_status_print = current_time
142-
143-
frame_count += 1
144-
time.sleep(0.016) # ~60 FPS
145-
146-
except KeyboardInterrupt:
147-
print("\n⚠️ Interrupted by user")
106+
while time.time() - start_time < 10.0:
107+
if not session.update():
108+
print("Update failed")
109+
break
110+
111+
current_time = time.time()
112+
if current_time - last_status_print >= 0.5:
113+
elapsed = current_time - start_time
114+
left_tracked = controller_tracker_a.get_left_controller(session)
115+
right_tracked = controller_tracker_a.get_right_controller(session)
116+
117+
print(f" [{elapsed:5.2f}s] Frame {frame_count:4d}")
118+
119+
left_data = left_tracked.data
120+
if left_data is not None:
121+
li = left_data.inputs
122+
print(
123+
f" L: Trig={li.trigger_value:.2f} Sq={li.squeeze_value:.2f}"
124+
f" Stick=({li.thumbstick_x:+.2f},{li.thumbstick_y:+.2f})"
125+
f" Btn=[{int(li.primary_click)}{int(li.secondary_click)}{int(li.thumbstick_click)}]"
126+
)
127+
else:
128+
print(" L: INACTIVE")
129+
130+
right_data = right_tracked.data
131+
if right_data is not None:
132+
ri = right_data.inputs
133+
print(
134+
f" R: Trig={ri.trigger_value:.2f} Sq={ri.squeeze_value:.2f}"
135+
f" Stick=({ri.thumbstick_x:+.2f},{ri.thumbstick_y:+.2f})"
136+
f" Btn=[{int(ri.primary_click)}{int(ri.secondary_click)}{int(ri.thumbstick_click)}]"
137+
)
138+
else:
139+
print(" R: INACTIVE")
140+
last_status_print = current_time
141+
142+
frame_count += 1
143+
time.sleep(0.016) # ~60 FPS
148144

149145
print()
150146
print(f"✓ Processed {frame_count} frames")

0 commit comments

Comments
 (0)