-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
1487 lines (1291 loc) · 67.7 KB
/
Copy pathnode.py
File metadata and controls
1487 lines (1291 loc) · 67.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
FoundationPose ROS2 node: subscribes to compressed RGB and depth images,
runs object detection (YOLO) and 6-DoF pose estimation (register + track).
Requires: ROS2 (rclpy), sensor_msgs, geometry_msgs, message_filters.
Run from workspace: python run_demo_ros2.py
(or: ros2 run <your_pkg> run_demo_ros2.py if installed as a package)
Subscriptions:
- /camera/color/image_raw/compressed (sensor_msgs/CompressedImage)
- /camera/depth/image_raw/compressed (sensor_msgs/CompressedImage)
- /camera/color/camera_info (sensor_msgs/CameraInfo) for intrinsics K
- /orchestrator/pose/toggle_fp (std_msgs/Bool) to enable/disable the node
- /orchestrator/pose/target_object (std_msgs/String) to set the target object class at runtime
Publishes:
- object_pose (geometry_msgs/PoseStamped)
- object_marker (visualization_msgs/Marker) mesh marker at pose
"""
import os
import time
import threading
from typing import Optional, Sequence
import cv2
import numpy as np
import rclpy
from rclpy.node import Node
from rclpy.qos import QoSProfile, ReliabilityPolicy, HistoryPolicy, DurabilityPolicy
from sensor_msgs.msg import CompressedImage, CameraInfo
from geometry_msgs.msg import PoseStamped
from std_msgs.msg import Bool, String
from visualization_msgs.msg import Marker
from message_filters import Subscriber, ApproximateTimeSynchronizer
from foundationpose.estimater import *
from ultralytics import YOLO
from ultralytics.models.sam import SAM3SemanticPredictor
from scipy.spatial.transform import Rotation
def _quat_normalize_xyzw(q: np.ndarray) -> np.ndarray:
q = np.asarray(q, dtype=np.float64).reshape(4)
n = float(np.linalg.norm(q))
if n <= 0.0:
return np.array([0.0, 0.0, 0.0, 1.0], dtype=np.float64)
return q / n
def _quat_slerp_xyzw(q0: np.ndarray, q1: np.ndarray, t: float) -> np.ndarray:
"""
Spherical linear interpolation between quaternions in xyzw convention.
Ensures shortest-path by flipping q1 if needed.
"""
q0 = _quat_normalize_xyzw(q0)
q1 = _quat_normalize_xyzw(q1)
dot = float(np.dot(q0, q1))
if dot < 0.0:
q1 = -q1
dot = -dot
dot = float(np.clip(dot, -1.0, 1.0))
if dot > 0.9995:
# Nearly identical: fall back to lerp
return _quat_normalize_xyzw(q0 + t * (q1 - q0))
theta_0 = float(np.arccos(dot))
sin_theta_0 = float(np.sin(theta_0))
theta = theta_0 * float(t)
sin_theta = float(np.sin(theta))
s0 = float(np.sin(theta_0 - theta) / sin_theta_0)
s1 = float(sin_theta / sin_theta_0)
return _quat_normalize_xyzw((s0 * q0) + (s1 * q1))
class PoseFilter:
"""
Position: Kalman filter (constant velocity), state [pos(3), vel(3)].
Orientation: exponential smoothing via SLERP.
"""
def __init__(self):
self.initialized = False
self.pos = np.zeros(3, dtype=np.float64)
self.vel = np.zeros(3, dtype=np.float64)
self.quat_xyzw = np.array([0.0, 0.0, 0.0, 1.0], dtype=np.float64)
self.P = np.eye(6, dtype=np.float64)
def reset(self):
self.initialized = False
def update(
self,
dt: float,
meas_pos: np.ndarray,
meas_quat_xyzw: np.ndarray,
process_noise: float = 0.1,
meas_noise: float = 0.05,
slerp_factor: float = 0.15,
max_dt_reinit: float = 1.0,
):
meas_pos = np.asarray(meas_pos, dtype=np.float64).reshape(3)
meas_quat_xyzw = _quat_normalize_xyzw(meas_quat_xyzw)
dt = float(dt)
if (not self.initialized) or (dt > max_dt_reinit):
# (Re)initialize when starting, or when the time gap suggests tracking was lost.
# We trust the measurement fully and reset velocity to 0.
self.pos = meas_pos.copy()
self.vel = np.zeros(3, dtype=np.float64)
self.quat_xyzw = meas_quat_xyzw.copy()
self.P = np.eye(6, dtype=np.float64)
self.initialized = True
return
# --- Position KF (Constant Velocity) ---
# State is x = [p, v]^T with p in meters and v in meters/second.
# Motion model assumes constant velocity between frames:
# p_k = p_{k-1} + v_{k-1} * dt
# v_k = v_{k-1}
# So the state transition is:
# x_k = F x_{k-1} + w, with w ~ N(0, Q)
F = np.eye(6, dtype=np.float64)
F[:3, 3:] = np.eye(3, dtype=np.float64) * dt
# Q: process noise covariance.
Q = np.eye(6, dtype=np.float64) * float(process_noise)
x_pred = np.concatenate([self.pos, self.vel], axis=0).reshape(6, 1)
x_pred = F @ x_pred
P_pred = (F @ self.P @ F.T) + Q
# H maps state -> measured components (extract p from [p, v]).
H = np.zeros((3, 6), dtype=np.float64)
H[:3, :3] = np.eye(3, dtype=np.float64)
# R: measurement noise covariance.
Rm = np.eye(3, dtype=np.float64) * float(meas_noise)
S = (H @ P_pred @ H.T) + Rm
K = P_pred @ H.T @ np.linalg.inv(S)
# Innovation/residual y = z - H x_pred, then correction x_upd = x_pred + K y.
y = (meas_pos.reshape(3, 1) - (H @ x_pred))
x_upd = x_pred + (K @ y)
self.P = (np.eye(6, dtype=np.float64) - (K @ H)) @ P_pred
self.pos = x_upd[:3, 0]
self.vel = x_upd[3:, 0]
# --- Orientation Smoothing (SLERP) ---
# Orientation is not filtered with a full EKF here; we just smooth the measured quaternion.
# slerp_factor in (0,1]: lower = more smoothing/lag, higher = more responsive.
self.quat_xyzw = _quat_slerp_xyzw(self.quat_xyzw, meas_quat_xyzw, float(slerp_factor))
DET_NAMES = {
0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus',
6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant',
11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog',
17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra',
23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase',
29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite',
34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard',
38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork',
43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich',
49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut',
55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table',
61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard',
67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink',
72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors',
77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush',
}
def decode_compressed_color(msg: CompressedImage) -> np.ndarray:
"""Decode CompressedImage to RGB (H, W, 3) uint8."""
buf = np.frombuffer(msg.data, dtype=np.uint8)
img = cv2.imdecode(buf, cv2.IMREAD_COLOR)
if img is None:
raise ValueError("Failed to decode color CompressedImage")
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
def decode_compressed_depth(msg: CompressedImage, scale: float = 0.001) -> np.ndarray:
# Skip the header (first 12 bytes)
# https://github.com/ros-perception/image_transport_plugins
depth_header_size = 12
raw_data = msg.data[depth_header_size:]
# Decode PNG (this is where uint16 is preserved)
np_arr = np.frombuffer(raw_data, np.uint8)
depth_img = cv2.imdecode(np_arr, cv2.IMREAD_UNCHANGED)
if depth_img is None:
raise RuntimeError("Failed to decode compressed depth image")
if depth_img.dtype != np.uint16:
raise RuntimeError(f"Expected uint16, got {depth_img.dtype}")
return depth_img * scale
def symmetry_tfs_from_z_angles(z_angles):
symmetry_tfs = []
for z_angle in z_angles:
# Pure Z rotation for Z-up meshes
r = Rotation.from_euler("z", z_angle, degrees=True)
tf = np.eye(4)
tf[:3, :3] = r.as_matrix()
symmetry_tfs.append(tf)
return np.array(symmetry_tfs)
def _parse_angles_str(angles_str):
"""Parse a 'a1,a2,...' string of degrees into a list of floats, or None if empty."""
if angles_str is None or angles_str == "":
return None
return [float(a) for a in angles_str.split(",")]
def symmetry_tfs_from_angles(x_angles_str=None, y_angles_str=None, z_angles_str=None):
"""
Build the set of 4x4 symmetry transforms from per-axis angle strings (degrees).
Each argument is a comma-separated string like "0,90,180,270" (or "" / None for no
symmetry on that axis). The returned set is the Cartesian product of the per-axis
rotations, composed as R = Rz @ Ry @ Rx (scipy "xyz" intrinsic order). When only
z-angles are given this reproduces the previous pure-Z behavior.
Returns None if no symmetry is requested on any axis.
"""
xa = _parse_angles_str(x_angles_str)
ya = _parse_angles_str(y_angles_str)
za = _parse_angles_str(z_angles_str)
if xa is None and ya is None and za is None:
return None
xs = xa if xa is not None else [0.0]
ys = ya if ya is not None else [0.0]
zs = za if za is not None else [0.0]
symmetry_tfs = []
for x_angle in xs:
for y_angle in ys:
for z_angle in zs:
r = Rotation.from_euler("xyz", [x_angle, y_angle, z_angle], degrees=True)
tf = np.eye(4)
tf[:3, :3] = r.as_matrix()
symmetry_tfs.append(tf)
return np.array(symmetry_tfs)
def _orthonormal_basis_from_z(z_axis: np.ndarray) -> np.ndarray:
"""
Right-handed rotation matrix [x, y, z] with given z column (object z in camera frame).
Convention (camera frame): z-forward (depth), x-right, y-down (typical optical frame).
- z column is the estimated object z-axis in camera coords.
- x column is camera-forward (camera z) projected onto the plane orthogonal to object z.
- y column completes the basis: y = z × x.
"""
z = np.asarray(z_axis, dtype=np.float64).reshape(3)
z /= np.linalg.norm(z) + 1e-12
cam_fwd = np.array([0.0, 0.0, 1.0], dtype=np.float64) # camera Z axis (depth)
x_axis = cam_fwd - np.dot(cam_fwd, z) * z # project forward into plane normal to z
nx = np.linalg.norm(x_axis)
if nx < 1e-8:
# If object z is (near) parallel to camera forward, fall back to camera X then Y.
cam_x = np.array([1.0, 0.0, 0.0], dtype=np.float64)
x_axis = cam_x - np.dot(cam_x, z) * z
nx = np.linalg.norm(x_axis)
if nx < 1e-8:
cam_y = np.array([0.0, 1.0, 0.0], dtype=np.float64)
x_axis = cam_y - np.dot(cam_y, z) * z
nx = np.linalg.norm(x_axis)
x_axis /= nx + 1e-12
y_axis = np.cross(z, x_axis)
y_axis /= np.linalg.norm(y_axis) + 1e-12
return np.stack([x_axis, y_axis, z], axis=1)
def _orthonormal_basis_from_x(x_axis: np.ndarray) -> np.ndarray:
"""
Right-handed rotation matrix [x, y, z] with given x column (object x in camera frame).
Convention (camera frame): z-forward (depth), x-right, y-down (typical optical frame).
- x column is the estimated object x-axis in camera coords.
- z column is camera-forward (camera z) projected onto the plane orthogonal to object x.
- y column completes the basis: y = z × x; then re-orthogonalize z = x × y.
"""
x = np.asarray(x_axis, dtype=np.float64).reshape(3)
x /= np.linalg.norm(x) + 1e-12
cam_fwd = np.array([0.0, 0.0, 1.0], dtype=np.float64) # camera Z axis (depth)
z_axis = cam_fwd - np.dot(cam_fwd, x) * x # project forward into plane normal to x
nz = np.linalg.norm(z_axis)
if nz < 1e-8:
# If object x is (near) parallel to camera forward, fall back to camera X then Y.
cam_x = np.array([1.0, 0.0, 0.0], dtype=np.float64)
z_axis = cam_x - np.dot(cam_x, x) * x
nz = np.linalg.norm(z_axis)
if nz < 1e-8:
cam_y = np.array([0.0, 1.0, 0.0], dtype=np.float64)
z_axis = cam_y - np.dot(cam_y, x) * x
nz = np.linalg.norm(z_axis)
z_axis /= nz + 1e-12
y_axis = np.cross(z_axis, x)
y_axis /= np.linalg.norm(y_axis) + 1e-12
z_axis = np.cross(x, y_axis)
z_axis /= np.linalg.norm(z_axis) + 1e-12
return np.stack([x, y_axis, z_axis], axis=1)
def _orthonormal_basis_from_y(y_axis: np.ndarray) -> np.ndarray:
"""
Right-handed rotation matrix [x, y, z] with given y column (object y in camera frame).
Convention (camera frame): z-forward (depth), x-right, y-down (typical optical frame).
- y column is the estimated object y-axis in camera coords.
- z column is camera-forward (camera z) projected onto the plane orthogonal to object y.
- x column completes the basis: x = y × z; then re-orthogonalize z = x × y.
"""
y = np.asarray(y_axis, dtype=np.float64).reshape(3)
y /= np.linalg.norm(y) + 1e-12
cam_fwd = np.array([0.0, 0.0, 1.0], dtype=np.float64) # camera Z axis (depth)
z_axis = cam_fwd - np.dot(cam_fwd, y) * y # project forward into plane normal to y
nz = np.linalg.norm(z_axis)
if nz < 1e-8:
# If object y is (near) parallel to camera forward, fall back to camera X then Y.
cam_x = np.array([1.0, 0.0, 0.0], dtype=np.float64)
z_axis = cam_x - np.dot(cam_x, y) * y
nz = np.linalg.norm(z_axis)
if nz < 1e-8:
cam_y = np.array([0.0, 1.0, 0.0], dtype=np.float64)
z_axis = cam_y - np.dot(cam_y, y) * y
nz = np.linalg.norm(z_axis)
z_axis /= nz + 1e-12
x_axis = np.cross(y, z_axis)
x_axis /= np.linalg.norm(x_axis) + 1e-12
z_axis = np.cross(x_axis, y)
z_axis /= np.linalg.norm(z_axis) + 1e-12
return np.stack([x_axis, y, z_axis], axis=1)
def apply_rotate_z_in(R: np.ndarray, apply_z_in: Optional[Sequence[float]]) -> np.ndarray:
"""
Keep the estimated object z-axis (column 2 of R); rebuild x,y in the plane orthogonal to z.
apply_z_in:
- None or (): leave R unchanged.
- [0]: yaw around z fixed to 0 (x aligned with camera-forward as much as possible).
- [0, P] with P in {90, 180, ...}: yaw angle (deg) reduced with np.mod(psi, P) into [0, P),
e.g. P=90 → 115° → 25°; then R' = R_ref @ Rz(psi_reduced).
"""
if apply_z_in is None:
return np.asarray(R, dtype=np.float64).copy()
rz = list(apply_z_in)
if len(rz) == 0:
return np.asarray(R, dtype=np.float64).copy()
R = np.asarray(R, dtype=np.float64)
z_axis = R[:, 2]
R_ref = _orthonormal_basis_from_z(z_axis)
x_ref, y_ref = R_ref[:, 0], R_ref[:, 1]
if len(rz) == 1:
if float(rz[0]) != 0.0:
raise ValueError(f"apply_z_in with one element must be [0], got {apply_z_in!r}")
return R_ref
if len(rz) == 2:
lo, hi = float(rz[0]), float(rz[1])
if lo != 0.0:
raise ValueError(f"apply_z_in two-element form must be [0, period], got {apply_z_in!r}")
period = hi
if period <= 0:
raise ValueError(f"apply_z_in period must be > 0, got {period}")
v = R[:, 0]
psi_rad = np.arctan2(np.dot(v, y_ref), np.dot(v, x_ref))
psi_deg = np.degrees(psi_rad)
psi_rem = float(np.mod(psi_deg, period))
Rz = Rotation.from_euler("z", psi_rem, degrees=True).as_matrix()
return R_ref @ Rz
raise ValueError(f"apply_z_in must be [0], [0, period], or empty/None; got {apply_z_in!r}")
def apply_rotate_x_in(R: np.ndarray, apply_x_in: Optional[Sequence[float]]) -> np.ndarray:
"""
Keep the estimated object x-axis (column 0 of R); rebuild y,z in the plane orthogonal to x.
apply_x_in:
- None or (): leave R unchanged.
- [0]: roll around x fixed to 0 (z aligned with camera-forward as much as possible).
- [0, P] with P in {90, 180, ...}: roll angle (deg) reduced with np.mod(psi, P) into [0, P),
then R' = R_ref @ Rx(psi_reduced).
"""
if apply_x_in is None:
return np.asarray(R, dtype=np.float64).copy()
rx = list(apply_x_in)
if len(rx) == 0:
return np.asarray(R, dtype=np.float64).copy()
R = np.asarray(R, dtype=np.float64)
x_axis = R[:, 0]
R_ref = _orthonormal_basis_from_x(x_axis)
y_ref, z_ref = R_ref[:, 1], R_ref[:, 2]
if len(rx) == 1:
if float(rx[0]) != 0.0:
raise ValueError(f"apply_x_in with one element must be [0], got {apply_x_in!r}")
return R_ref
if len(rx) == 2:
lo, hi = float(rx[0]), float(rx[1])
if lo != 0.0:
raise ValueError(f"apply_x_in two-element form must be [0, period], got {apply_x_in!r}")
period = hi
if period <= 0:
raise ValueError(f"apply_x_in period must be > 0, got {period}")
v = R[:, 2] # current z axis
psi_rad = np.arctan2(np.dot(v, y_ref), np.dot(v, z_ref))
psi_deg = np.degrees(psi_rad)
psi_rem = float(np.mod(psi_deg, period))
Rx = Rotation.from_euler("x", psi_rem, degrees=True).as_matrix()
return R_ref @ Rx
raise ValueError(f"apply_x_in must be [0], [0, period], or empty/None; got {apply_x_in!r}")
def apply_rotate_y_in(R: np.ndarray, apply_y_in: Optional[Sequence[float]]) -> np.ndarray:
"""
Keep the estimated object y-axis (column 1 of R); rebuild x,z in the plane orthogonal to y.
apply_y_in:
- None or (): leave R unchanged.
- [0]: pitch around y fixed to 0 (z aligned with camera-forward as much as possible).
- [0, P] with P in {90, 180, ...}: pitch angle (deg) reduced with np.mod(psi, P) into [0, P),
then R' = R_ref @ Ry(psi_reduced).
"""
if apply_y_in is None:
return np.asarray(R, dtype=np.float64).copy()
ry = list(apply_y_in)
if len(ry) == 0:
return np.asarray(R, dtype=np.float64).copy()
R = np.asarray(R, dtype=np.float64)
y_axis = R[:, 1]
R_ref = _orthonormal_basis_from_y(y_axis)
x_ref, z_ref = R_ref[:, 0], R_ref[:, 2]
if len(ry) == 1:
if float(ry[0]) != 0.0:
raise ValueError(f"apply_y_in with one element must be [0], got {apply_y_in!r}")
return R_ref
if len(ry) == 2:
lo, hi = float(ry[0]), float(ry[1])
if lo != 0.0:
raise ValueError(f"apply_y_in two-element form must be [0, period], got {apply_y_in!r}")
period = hi
if period <= 0:
raise ValueError(f"apply_y_in period must be > 0, got {period}")
v = R[:, 2] # current z axis
psi_rad = np.arctan2(np.dot(v, x_ref), np.dot(v, z_ref))
psi_deg = np.degrees(psi_rad)
psi_rem = float(np.mod(psi_deg, period))
Ry = Rotation.from_euler("y", psi_rem, degrees=True).as_matrix()
return R_ref @ Ry
raise ValueError(f"apply_y_in must be [0], [0, period], or empty/None; got {apply_y_in!r}")
# OBJECT_KEYS_TO_PARAMETERS = {
# # "mustard": {"mesh_file": "./assets/hackathon2/mustard/mustard.obj", "symmetry_z_angles": "0,180", "target_object": "yellow bottle", "rotate_z_in":[0,180]},
# # "juice": {"mesh_file": "./assets/hackathon2/juice/juice.obj", "symmetry_z_angles": "0,90,180,270", "target_object": "bottle", "rotate_z_in":[0,90]},
# # "milk": {"mesh_file": "./assets/hackathon2/milk/milk.obj", "symmetry_z_angles": "0,30,60,90,120,150,180,210,240,270,300,330", "target_object": "white bottle", "rotate_z_in": [0]},
# # "plate": {"mesh_file": "./assets/hackathon2/plate/plate.obj", "symmetry_z_angles": "0,90,180,270", "target_object": "red plate", "rotate_z_in": [0]},
# # "gavottes": {"mesh_file": "./assets/hackathon2/gavottes/gavottes.obj", "symmetry_z_angles": "0,180", "target_object": "biscuit box", "rotate_z_in": [0,180]},
# # "bowl": {"mesh_file": "./assets/hackathon2/bowl/bowl.obj", "symmetry_z_angles": "0,30,60,90,120,150,180,210,240,270,300,330", "target_object": "green bowl", "rotate_z_in": [0]},
# }
OBJECT_KEYS_TO_PARAMETERS = {
# flips on x and y at 180 !
"baguette" : {"mesh_file": "./assets/hackathon3/baguette/baguette.obj",
"symmetry_x_angles": "0,180",
"symmetry_y_angles": "0,180",
"symmetry_z_angles": "0,30,60,90,120,150,180,210,240,270,300,330",
"target_object": "bread",
"constraint_yaw_in": 0,
"constraint_pitch_in": [0,180],
"constraint_roll_in": [0,180],
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
# seems okay and does not flip
"banana" : {"mesh_file": "./assets/hackathon3/banana/banana.obj",
"symmetry_x_angles": "",
"symmetry_y_angles": "",
"symmetry_z_angles": "",
"target_object": "banana",
"constraint_yaw_in": None,
"constraint_pitch_in": None,
"constraint_roll_in": None,
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
# still flips up and down, adjusted size to retry
"coffeecan" : {"mesh_file": "./assets/hackathon3/coffeecan/coffeecan.obj",
"symmetry_x_angles": "",
"symmetry_y_angles": "",
"symmetry_z_angles": "",
"target_object": "blue container",
"constraint_yaw_in": 0,
"constraint_pitch_in": None,
"constraint_roll_in": None,
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
# lots of flipping difficult to stabilize
"egg" : {"mesh_file": "./assets/hackathon3/egg/egg.obj",
"symmetry_x_angles": "0,180",
"symmetry_y_angles": "0,180",
"symmetry_z_angles": "0,30,60,90,120,150,180,210,240,270,300,330",
"target_object": "egg",
"constraint_yaw_in": 0,
"constraint_pitch_in": 0,
"constraint_roll_in": 0,
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
# good it seems that does not flip up/down but the handle does not always get oriented correctly
"flowercup" : {"mesh_file": "./assets/hackathon3/flowercup/flowercup.obj",
"symmetry_x_angles": "",
"symmetry_y_angles": "",
"symmetry_z_angles": "",
"target_object": "yellow mug",
"constraint_yaw_in": None,
"constraint_pitch_in": None,
"constraint_roll_in": None,
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
# not detected with keyword jam
"jam" : {"mesh_file": "./assets/hackathon3/jam/jam.obj",
"symmetry_x_angles": "",
"symmetry_y_angles": "",
"symmetry_z_angles": "",
"target_object": "orange jam",
"constraint_yaw_in": None,
"constraint_pitch_in": None,
"constraint_roll_in": None,
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
# ok
"milk" : {"mesh_file": "./assets/hackathon3/milk/milk.obj",
"symmetry_x_angles": "",
"symmetry_y_angles": "",
"symmetry_z_angles": "0,30,60,90,120,150,180,210,240,270,300,330",
"target_object": "white bottle",
"constraint_yaw_in": 0,
"constraint_pitch_in": None,
"constraint_roll_in": None,
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
# hard to find but seems good when found... investigate, maybe a bigger one !
"minicheese" : {"mesh_file": "./assets/hackathon3/minicheese/minicheese.obj",
"symmetry_x_angles": "",
"symmetry_y_angles": "",
"symmetry_z_angles": "",
"target_object": "triangle cheese",
"constraint_yaw_in": None,
"constraint_pitch_in": None,
"constraint_roll_in": None,
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
# seems robust could be a good anchor point
"pan" : {"mesh_file": "./assets/hackathon3/pan/pan.obj",
"symmetry_x_angles": "",
"symmetry_y_angles": "",
"symmetry_z_angles": "",
"target_object": "pan",
"constraint_yaw_in": None,
"constraint_pitch_in": None,
"constraint_roll_in": None,
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
# problem gets a lot of multiple objects + rotations
"redapple" : {"mesh_file": "./assets/hackathon3/redapple/redapple.obj",
"symmetry_x_angles": "0,180",
"symmetry_y_angles": "0,180",
"symmetry_z_angles": "0,30,60,90,120,150,180,210,240,270,300,330",
"target_object": "red apple",
"constraint_yaw_in": 0,
"constraint_pitch_in": 0,
"constraint_roll_in": 0,
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
"smallmilk" : {"mesh_file": "./assets/hackathon3/smallmilk/smallmilk.obj",
"symmetry_x_angles": "",
"symmetry_y_angles": "",
"symmetry_z_angles": "0,30,60,90,120,150,180,210,240,270,300,330",
"target_object": "white bottle",
"constraint_yaw_in": 0,
"constraint_pitch_in": None,
"constraint_roll_in": None,
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
"smallsanpellegrino" : {"mesh_file": "./assets/hackathon3/smallsanpellegrino/smallsanpellegrino.obj",
"symmetry_x_angles": "",
"symmetry_y_angles": "",
"symmetry_z_angles": "0,30,60,90,120,150,180,210,240,270,300,330",
"target_object": "green bottle",
"constraint_yaw_in": 0,
"constraint_pitch_in": None,
"constraint_roll_in": None,
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
# hard to detect and flips on several axes
"spam" : {"mesh_file": "./assets/hackathon3/spam/spam.obj",
"symmetry_x_angles": "",
"symmetry_y_angles": "",
"symmetry_z_angles": "",
"target_object": "blue container",
"constraint_yaw_in": [0,180],
"constraint_pitch_in": [0,180],
"constraint_roll_in": [0,180],
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
# okay but flips on one axis
"ycbmustard" : {"mesh_file": "./assets/hackathon3/ycbmustard/ycbmustard.obj",
"symmetry_x_angles": "",
"symmetry_y_angles": "",
"symmetry_z_angles": "",
"target_object": "yellow bottle",
"constraint_yaw_in": [0,180],
"constraint_pitch_in": None,
"constraint_roll_in": None,
"apply_x_in": None,
"apply_y_in": None,
"apply_z_in": None},
}
class FoundationPoseROS2Node(Node):
def __init__(self, args):
super().__init__("foundation_pose_node")
# Declare ROS parameters
self.declare_parameter("mesh_file", args.mesh_file)
self.declare_parameter("target_object", args.target_object)
self.declare_parameter("est_refine_iter", args.est_refine_iter)
self.declare_parameter("track_refine_iter", args.track_refine_iter)
self.declare_parameter("debug", args.debug)
self.declare_parameter("debug_dir", args.debug_dir)
self.declare_parameter("depth_scale", args.depth_scale)
self.declare_parameter("color_topic", args.color_topic)
self.declare_parameter("depth_topic", args.depth_topic)
self.declare_parameter("camera_info_topic", args.camera_info_topic)
self.declare_parameter("pose_frame_id", args.pose_frame_id)
self.declare_parameter("slop", args.slop)
self.declare_parameter("marker_mesh_scale", 1.0)
self.declare_parameter("marker_mesh_use_embedded_materials", True)
self.declare_parameter("pose_filter_process_noise", 0.1)
self.declare_parameter("pose_filter_meas_noise", 0.05)
self.declare_parameter("pose_filter_slerp_factor", 0.15)
self.declare_parameter("pose_filter_reset_lost_frames", 15)
# Set current code directory
code_dir = os.path.dirname(os.path.realpath(__file__))
# Get parameters
self.mesh_file = self.get_parameter("mesh_file").value
assert(os.path.exists(self.mesh_file)), f"Mesh file {self.mesh_file} does not exist"
mesh_file_basename = os.path.basename(self.mesh_file)
mesh_file_rn = mesh_file_basename.split(".")[0]
self._marker_mesh_resource = f"file:///mesh_assets/{mesh_file_rn}/{mesh_file_basename}"
_abs_mesh = os.path.normpath(os.path.abspath(self.mesh_file))
self.apply_z_in: Optional[Sequence[float]] = None
self.apply_x_in: Optional[Sequence[float]] = None
self.apply_y_in: Optional[Sequence[float]] = None
self.constraint_yaw_in = None
self.constraint_pitch_in = None
self.constraint_roll_in = None
for params in OBJECT_KEYS_TO_PARAMETERS.values():
if mesh_file_basename in params["mesh_file"]:
self.apply_z_in = params.get("apply_z_in")
self.apply_x_in = params.get("apply_x_in")
self.apply_y_in = params.get("apply_y_in")
self.constraint_yaw_in = params.get("constraint_yaw_in")
self.constraint_pitch_in = params.get("constraint_pitch_in")
self.constraint_roll_in = params.get("constraint_roll_in")
print(f"Apply z in: {self.apply_z_in}")
break
# Get debug directory and create if it doesn't exist
self.debug_dir = self.get_parameter("debug_dir").value
if not self.debug_dir:
timestamp = time.strftime("%Y%m%d_%H%M%S")
self.debug_dir = f"{code_dir}/debug_node/{timestamp}_{args.target_object}"
if args.debug > 0:
os.makedirs(self.debug_dir, exist_ok=True)
# Set parameters
self.target_object = self.get_parameter("target_object").value
self.est_refine_iter = self.get_parameter("est_refine_iter").value
self.track_refine_iter = self.get_parameter("track_refine_iter").value
self.debug = self.get_parameter("debug").value
self.depth_scale = self.get_parameter("depth_scale").value
self.pose_frame_id = self.get_parameter("pose_frame_id").value
self.slop = self.get_parameter("slop").value
self.seg_model_name = args.seg_model_name
self.resize_factor = args.resize_factor
self.min_initial_detection_counter = args.min_initial_detection_counter
self.enable_pose_tracking = args.enable_pose_tracking
self.seg_model_type = args.seg_model_type
self.symmetry_x_angles = args.symmetry_x_angles
self.symmetry_y_angles = args.symmetry_y_angles
self.symmetry_z_angles = args.symmetry_z_angles
self.fp_verbosity = args.fp_verbosity
self.use_kalman_filter = args.use_kalman_filter
if self.fp_verbosity not in ["debug", "info", "warning", "error", "critical"]:
raise ValueError(f"Invalid verbosity: {self.fp_verbosity}. Valid: debug, info, warning, error, critical")
# Make some checks on the parameters
assert(self.seg_model_type in ["sam3", "yolo"]), f"Invalid segmentation model type: {self.seg_model_type}"
if self.seg_model_type == "sam3":
self.seg_model_name = "sam3.pt"
elif self.seg_model_type == "yolo":
assert("yolo" in self.seg_model_name), f"Invalid YOLO model name: {self.seg_model_name}"
coco_names = list(DET_NAMES.values())
if self.seg_model_type == "yolo":
assert(self.target_object in coco_names), f"Invalid target object: {self.target_object} (must be one of {coco_names})"
# Print parameters
self.get_logger().debug("==== PARAMETERS ====")
self.get_logger().debug(f"Mesh file: {self.mesh_file}")
self.get_logger().debug(f"Target object: {self.target_object}")
self.get_logger().debug(f"Est refine iter: {self.est_refine_iter}")
self.get_logger().debug(f"Track refine iter: {self.track_refine_iter}")
self.get_logger().debug(f"Debug: {self.debug}")
self.get_logger().debug(f"Debug dir: {self.debug_dir}")
self.get_logger().debug(f"Depth scale: {self.depth_scale}")
self.get_logger().debug(f"Pose frame id: {self.pose_frame_id}")
self.get_logger().debug(f"Slop: {self.slop}")
self.get_logger().debug(f"Resize factor: {self.resize_factor}")
self.get_logger().debug(f"Min initial detection counter: {self.min_initial_detection_counter}")
self.get_logger().debug(f"Enable pose tracking: {self.enable_pose_tracking}")
self.get_logger().debug(f"Symmetry x angles: {self.symmetry_x_angles}")
self.get_logger().debug(f"Symmetry y angles: {self.symmetry_y_angles}")
self.get_logger().debug(f"Symmetry z angles: {self.symmetry_z_angles}")
self.get_logger().debug(f"Apply z in: {self.apply_z_in}")
self.get_logger().debug(f"Apply x in: {self.apply_x_in}")
self.get_logger().debug(f"Apply y in: {self.apply_y_in}")
self.get_logger().debug(f"Constraint yaw in: {self.constraint_yaw_in}")
self.get_logger().debug(f"Constraint pitch in: {self.constraint_pitch_in}")
self.get_logger().debug(f"Constraint roll in: {self.constraint_roll_in}")
self.get_logger().debug(f"Use Kalman filter: {self.use_kalman_filter}")
self.K = None # to be set by camera info callback
self.est = None # to be set by estimator initialization
self.current_phase = "NotInitialized"
self.pose_last = None
self.to_origin = None
self.bbox = None
self.frame_count = 0
self._lock = threading.Lock()
self._processing = False
self.is_on = True #False
self._prev_is_on = self.is_on
self.initial_detection_counter = 0
self.rgbd_frames_counter_received = 0
self.rgbd_frames_counter_processed = 0
# Set logger and seed (for estimater)
verbosity = {"debug": logging.DEBUG, "info": logging.INFO, "warning": logging.WARNING, "error": logging.ERROR, "critical": logging.CRITICAL}
set_logging_format(level=verbosity[self.fp_verbosity])
set_seed(0)
# Load mesh and compute bounds
mesh = trimesh.load(self.mesh_file, force="mesh", skip_materials=True)
self.to_origin, extents = trimesh.bounds.oriented_bounds(mesh)
self.bbox = np.stack([-extents / 2, extents / 2], axis=0).reshape(2, 3)
self.get_logger().info(f"Mesh lodaed from {self.mesh_file} | Bounds: {self.bbox.flatten()}")
# Initialize segmentation / detection model
self.get_logger().info(f"Initializing segmentation model {self.seg_model_type} ({self.seg_model_name})...")
if self.seg_model_type == "sam3":
# Initialize predictor with configuration
overrides = dict(
conf=0.25,
task="segment",
mode="predict",
model=f"sam3/{self.seg_model_name}",
half=True, # Use FP16 for faster inference
save=False,
)
self.seg_model = SAM3SemanticPredictor(overrides=overrides)
# run a fake pass to warm up the model
self.seg_model.set_image(np.zeros((1080, 1920, 3), dtype=np.uint8))
self.seg_model(text=[self.target_object])
elif self.seg_model_type == "yolo":
self.seg_model = YOLO(self.seg_model_name)
else:
raise ValueError(f"Invalid segmentation model type: {self.seg_model_type}")
self.get_logger().info(f"Segmentation model {self.seg_model_type} ({self.seg_model_name}) initialized")
# Load symmetry transforms (combined over x, y, z axes)
symmetry_tfs = symmetry_tfs_from_angles(
self.symmetry_x_angles, self.symmetry_y_angles, self.symmetry_z_angles
)
if symmetry_tfs is not None:
self.get_logger().debug(f"Symmetry transforms: {symmetry_tfs.shape}")
else:
self.get_logger().debug(f"No symmetry transforms")
# Initialize estimator
self.get_logger().info("Initializing estimator...")
scorer = ScorePredictor()
refiner = PoseRefinePredictor()
glctx = dr.RasterizeCudaContext()
self.est = FoundationPose(
model_pts=mesh.vertices,
model_normals=mesh.vertex_normals,
mesh=mesh,
scorer=scorer,
refiner=refiner,
debug_dir=self.debug_dir,
debug=self.debug,
glctx=glctx,
symmetry_tfs=symmetry_tfs,
meshname=mesh_file_rn,
)
self.get_logger().info("FoundationPose estimator initialized")
# Update current phase
self.current_phase = "Initialized"
# Initialize ROS2 subscribers and publishers
qos_sensor = QoSProfile(
reliability=ReliabilityPolicy.BEST_EFFORT,
history=HistoryPolicy.KEEP_LAST,
depth=15, # allow for 15 frames to be buffered
)
qos_info = QoSProfile(
reliability=ReliabilityPolicy.RELIABLE,
history=HistoryPolicy.KEEP_LAST,
depth=1,
)
qos_marker = QoSProfile(
reliability=ReliabilityPolicy.RELIABLE,
durability=DurabilityPolicy.TRANSIENT_LOCAL,
history=HistoryPolicy.KEEP_LAST,
depth=1,
)
self._camera_info_sub = self.create_subscription(
CameraInfo,
self.get_parameter("camera_info_topic").value,
self._camera_info_cb,
qos_info,
)
self._pose_pub = self.create_publisher(
PoseStamped,
"object_pose",
1,
)
self._marker_pub = self.create_publisher(
Marker,
"object_marker",
qos_marker,
)
self._toggle_fp_sub = self.create_subscription(
Bool,
"/orchestrator/pose/toggle_fp",
self._toggle_fp_cb,
1,
)
self._target_object_sub = self.create_subscription(
String,
"/orchestrator/pose/target_object",
self._target_object_cb,
1,
)
sub_color = Subscriber(
self,
CompressedImage,
self.get_parameter("color_topic").value,
qos_profile=qos_sensor,
)
sub_depth = Subscriber(
self,
CompressedImage,
self.get_parameter("depth_topic").value,
qos_profile=qos_sensor,
)
self._sync = ApproximateTimeSynchronizer(
[sub_color, sub_depth],
queue_size=100,
slop=self.slop,
)
self._sync.registerCallback(self._rgbd_cb)
self.get_logger().info(
f"Subscribed to {self.get_parameter('color_topic').value} and {self.get_parameter('depth_topic').value}; waiting for camera_info and RGBD messages"
)
self.current_phase = "WaitingForCameraInfo"
self.get_logger().info("FoundationPose ROS2 node initialized")
# Optional pose filter (position KF + orientation SLERP smoothing)
self._pose_filter = PoseFilter()
self._pose_filter_last_stamp_s: Optional[float] = None
self._pose_filter_lost_frames = 0
def _reset_pose_filter(self, reason: str):
self._pose_filter.reset()
self._pose_filter_last_stamp_s = None
self._pose_filter_lost_frames = 0
self.get_logger().info(f"Pose filter reset: {reason}")
def _note_pose_lost_for_filter(self):
if not self.use_kalman_filter:
return
self._pose_filter_lost_frames += 1
lost_thr = int(self.get_parameter("pose_filter_reset_lost_frames").value)
if self._pose_filter_lost_frames > lost_thr:
self._reset_pose_filter(f"object lost for > {lost_thr} frames")
def _toggle_fp_cb(self, msg: Bool):
prev = self.is_on
self.is_on = msg.data
self._prev_is_on = prev
self.get_logger().info(f"FoundationPose toggled: is_on = {self.is_on}")
if (not prev) and self.is_on:
self._reset_pose_filter("node toggled off->on")
if msg.data == False:
if self.current_phase == "PoseTracking" or self.current_phase == "StartPoseTracking":
self.get_logger().info("Stopping pose tracking back to detecting for later")
self.current_phase = "DetectingAgain"
def _target_object_cb(self, msg: String):
new_target = msg.data.strip()
if not new_target:
self.get_logger().error("Received empty target object, ignoring")
return
if new_target.startswith("mesh_update_"):
# it will be a full update with new mesh
self.get_logger().info(f"Received mesh update request: {new_target}. Will restart the estimator with new mesh")
self._lock.acquire()
key_name = new_target.replace("mesh_update_", "") # name of target e.g.
if key_name not in OBJECT_KEYS_TO_PARAMETERS:
self.get_logger().error(f"Invalid key name: {key_name}. Valid: {list(OBJECT_KEYS_TO_PARAMETERS.keys())}")
self._lock.release()
return
self.mesh_file = OBJECT_KEYS_TO_PARAMETERS[key_name]["mesh_file"]
if not os.path.exists(self.mesh_file):
self.get_logger().error(f"Mesh file {self.mesh_file} does not exist")
self._lock.release()