File size: 34,440 Bytes
f96995c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Callable, List
from omegaconf import DictConfig, OmegaConf
from enum import Enum
import torch
import numpy as np
import multiprocess as mp
import time
import threading
import cv2
import pygame
import os
import pickle
import subprocess
from pathlib import Path
from copy import deepcopy
import open3d as o3d

from pgnd.utils import get_root, mkdir
root: Path = get_root(__file__)

from camera.multi_realsense import MultiRealsense
from camera.single_realsense import SingleRealsense

from modules_planning.segment_perception import SegmentPerception
from modules_planning.planning_module import PlanningModule
from modules_planning.xarm_controller import XarmController
from modules_planning.udp_util import udpReceiver, udpSender
from modules_planning.common.communication import XARM_STATE_PORT, XARM_CONTROL_PORT, XARM_CONTROL_PORT_L, XARM_CONTROL_PORT_R
from modules_planning.common.xarm import GRIPPER_OPEN_MIN, GRIPPER_OPEN_MAX, POSITION_UPDATE_INTERVAL, COMMAND_CHECK_INTERVAL


class EnvEnum(Enum):
    NONE = 0
    INFO = 1
    DEBUG = 2
    VERBOSE = 3


class RobotPlanningEnv(mp.Process):

    def __init__(
        self, 
        cfg: DictConfig,
        exp_root: str,
        ckpt_path: Path | str,

        realsense: MultiRealsense | SingleRealsense | None = None,
        shm_manager: mp.managers.SharedMemoryManager | None = None,
        serial_numbers: list[str] | None= None,
        resolution: tuple[int, int] = (1280, 720),
        capture_fps: int = 30,
        enable_depth: bool = True,
        enable_color: bool = True,

        record_fps: int | None = 0,
        record_time: float | None = 60 * 10,  # 10 minutes
        text_prompts: str = "white cotton rope.",
        show_annotation: bool = True,

        use_robot: bool = False,
        robot_ip: str = '192.168.1.196',
        gripper_enable: bool = False,
        calibrate_result_dir: Path = root / "log" / "latest_calibration",
        debug: bool | int | None = False,
        
        bimanual: bool = False,
        bimanual_robot_ip: List[str] = ['192.168.1.196', '192.168.1.224'],

        construct_target: bool = False,

    ) -> None:

        # Debug level
        self.debug = 0 if debug is None else (2 if debug is True else debug)

        self.cfg = cfg
        self.exp_root = Path(exp_root)

        if os.path.exists(self.exp_root.parent / "target" / "target.pcd"):
            mkdir(self.exp_root / "target", overwrite=False, resume=True)
            os.system(f"cp {self.exp_root.parent / 'target' / 'target.pcd'} {self.exp_root / 'target'}")
        else:
            assert construct_target, "target.pcd not found"
        self.construct_target = construct_target
        if construct_target:
            mkdir(self.exp_root.parent / "target", overwrite=False, resume=False)

        self.torch_device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

        self.bimanual = bimanual
        if self.bimanual:
            self.bbox = np.array([[0.0, 0.7], [-0.35, 0.45 + 0.75], [-0.8, 0.0]])
        else:
            self.bbox = np.array([[0.0, 0.7], [-0.35, 0.45], [-0.8, 0.0]])
        
        self.text_prompts = text_prompts
        self.show_annotation = show_annotation

        self.capture_fps = capture_fps
        self.record_fps = record_fps

        self.state = mp.Manager().dict() # should be main explict exposed variable to the child class / process

        # Realsense camera setup
        # camera is always required for real env
        if realsense is not None:
            assert isinstance(realsense, MultiRealsense) or isinstance(realsense, SingleRealsense)
            self.realsense = realsense
            self.serial_numbers = list(self.realsense.cameras.keys())
        else:
            self.realsense = MultiRealsense(
                shm_manager=shm_manager,
                serial_numbers=serial_numbers,
                resolution=resolution,
                capture_fps=capture_fps,
                enable_depth=enable_depth,
                enable_color=enable_color,
                verbose=self.debug > EnvEnum.VERBOSE.value
            )
            self.serial_numbers = list(self.realsense.cameras.keys())
    
        # manual
        self.realsense.set_exposure(exposure=60, gain=60)  # 100: bright, 60: dark
        self.realsense.set_white_balance(3800)

        # base calibration
        self.calibrate_result_dir = calibrate_result_dir
        with open(f'{self.calibrate_result_dir}/base.pkl', 'rb') as f:
            base = pickle.load(f)
        if self.bimanual:
            R_leftbase2board = base['R_leftbase2world']
            t_leftbase2board = base['t_leftbase2world']
            R_rightbase2board = base['R_rightbase2world']
            t_rightbase2board = base['t_rightbase2world']
            leftbase2world_mat = np.eye(4)
            leftbase2world_mat[:3, :3] = R_leftbase2board
            leftbase2world_mat[:3, 3] = t_leftbase2board
            self.state["b2w_l"] = leftbase2world_mat
            rightbase2world_mat = np.eye(4)
            rightbase2world_mat[:3, :3] = R_rightbase2board
            rightbase2world_mat[:3, 3] = t_rightbase2board
            self.state["b2w_r"] = rightbase2world_mat
        else:
            R_base2board = base['R_base2world']
            t_base2board = base['t_base2world']
            base2world_mat = np.eye(4)
            base2world_mat[:3, :3] = R_base2board
            base2world_mat[:3, 3] = t_base2board
            self.state["b2w"] = base2world_mat

        # camera calibration
        extr_list = []
        with open(f'{self.calibrate_result_dir}/rvecs.pkl', 'rb') as f:
            rvecs = pickle.load(f)
        with open(f'{self.calibrate_result_dir}/tvecs.pkl', 'rb') as f:
            tvecs = pickle.load(f)
        for i in range(len(self.serial_numbers)):
            device = self.serial_numbers[i]
            R_world2cam = cv2.Rodrigues(rvecs[device])[0]
            t_world2cam = tvecs[device][:, 0]
            extr_mat = np.eye(4)
            extr_mat[:3, :3] = R_world2cam
            extr_mat[:3, 3] = t_world2cam
            extr_list.append(extr_mat)
        self.state["extr"] = np.stack(extr_list)

        # save calibration
        mkdir(self.exp_root / "calibration", overwrite=False, resume=False)
        subprocess.run(f'cp -r {self.calibrate_result_dir}/* {str(self.exp_root)}/calibration', shell=True)

        # Perception setup
        self.perception = SegmentPerception(
            realsense=self.realsense,
            capture_fps=self.realsense.capture_fps,  # mush be the same as realsense capture fps 
            record_fps=record_fps,
            record_time=record_time,
            text_prompts=self.text_prompts,
            show_annotation=self.show_annotation,
            bbox=self.bbox,
            device=self.torch_device,
            verbose=self.debug > EnvEnum.VERBOSE.value)

        # Robot setup
        self.use_robot = use_robot
        if use_robot:
            if bimanual:
                self.left_xarm_controller = XarmController(
                    start_time=time.time(),
                    ip=bimanual_robot_ip[0],
                    gripper_enable=gripper_enable,
                    mode='3D',
                    command_mode='cartesian',
                    robot_id=0,
                    verbose=True,
                )
                self.right_xarm_controller = XarmController(
                    start_time=time.time(),
                    ip=bimanual_robot_ip[1],
                    gripper_enable=gripper_enable,
                    mode='3D',
                    command_mode='cartesian',
                    robot_id=1,
                    verbose=True,
                )
                self.xarm_controller = None
            else:
                self.xarm_controller = XarmController(
                    start_time=time.time(),
                    ip=robot_ip,
                    gripper_enable=gripper_enable,
                    mode='3D',
                    command_mode='cartesian',
                    verbose=True,
                )
                self.left_xarm_controller = None
                self.right_xarm_controller = None
        else:
            self.left_xarm_controller = None
            self.right_xarm_controller = None
            self.xarm_controller = None

        # subprocess can only start a process object created by current process
        self._real_alive = mp.Value('b', False)

        self.start_time = 0
        mp.Process.__init__(self)
        self._alive = mp.Value('b', False)

        # pygame
        # Initialize a separate Pygame window for image display
        img_w, img_h = 848, 480
        col_num = 2
        self.screen_width, self.screen_height = img_w * col_num, img_h * len(self.realsense.serial_numbers)
        self.image_window = None

        # Shared memory for image data
        self.image_data = mp.Array('B', np.zeros((self.screen_height, self.screen_width, 3), dtype=np.uint8).flatten())

        # robot eef
        self.eef_point = np.array([[0.0, 0.0, 0.175]])  # the eef point in the gripper frame

        # planning_module setup
        self.planning_module = PlanningModule(
            cfg=cfg,
            exp_root=self.exp_root,
            ckpt_path=ckpt_path,
            device=self.torch_device,
            bimanual=bimanual,
            bbox=self.bbox,
            eef_point=self.eef_point,
            debug=self.debug
        )
        self.planning_module.set_target(str(self.exp_root / "target" / "target.pcd"))
        self.command = []
        self.command_sender = None
        self.command_sender_left = None
        self.command_sender_right = None

    def real_start(self, start_time) -> None:
        self._real_alive.value = True
        print("starting real env")
        
        # Realsense camera setup
        self.realsense.start()
        self.realsense.restart_put(start_time + 1)
        time.sleep(2)

        # Perception setup
        if self.perception is not None:
            self.perception.start()
        self.perception.update_extrinsics(self.state["extr"])
    
        # Robot setup
        if self.use_robot:
            if self.bimanual:
                assert self.left_xarm_controller is not None
                assert self.right_xarm_controller is not None
                self.left_xarm_controller.start()
                self.right_xarm_controller.start()
            else:
                assert self.xarm_controller is not None
                self.xarm_controller.start()
        
        while not self.real_alive:
            self._real_alive.value = True
            print(".", end="")
            time.sleep(0.5)
        
        # get intrinsics
        intrs = self.get_intrinsics()
        intrs = np.array(intrs)
        np.save(self.exp_root / "calibration" / "intrinsics.npy", intrs)

        print("real env started")

        self.update_real_state_t = threading.Thread(name="update_real_state", target=self.update_real_state)
        self.update_real_state_t.start()

    def real_stop(self, wait=False) -> None:
        self._real_alive.value = False
        if self.use_robot:
            if self.bimanual and self.left_xarm_controller and self.left_xarm_controller.is_controller_alive:
                self.left_xarm_controller.stop()
            if self.bimanual and self.right_xarm_controller and self.right_xarm_controller.is_controller_alive:
                self.right_xarm_controller.stop()
            if not self.bimanual and self.xarm_controller and self.xarm_controller.is_controller_alive:
                self.xarm_controller.stop()
        if self.perception is not None and self.perception.alive.value:
            self.perception.stop()
        self.realsense.stop(wait=False)

        self.image_display_thread.join()
        self.update_real_state_t.join()
        print("real env stopped")

    @property
    def real_alive(self) -> bool:
        alive = self._real_alive.value
        if self.perception is not None:
            alive = alive and self.perception.alive.value
        if self.use_robot:
            controller_alive = \
                (self.bimanual and self.left_xarm_controller and self.left_xarm_controller.is_controller_alive \
                    and self.right_xarm_controller and self.right_xarm_controller.is_controller_alive) \
                or (not self.bimanual and self.xarm_controller and self.xarm_controller.is_controller_alive)
            alive = alive and controller_alive
        self._real_alive.value = alive
        return self._real_alive.value

    def _update_perception(self) -> None:
        if self.perception.alive.value and self.perception.do_process.value:
            if not self.perception.perception_q.empty():
                self.state["perception_out"] = {
                    "time": time.time(),
                    "value": self.perception.perception_q.get()
                }
        return

    def _update_robot(self) -> None:
        if self.bimanual:
            assert self.left_xarm_controller is not None
            assert self.right_xarm_controller is not None
            if self.left_xarm_controller.is_controller_alive and self.right_xarm_controller.is_controller_alive:
                if not self.left_xarm_controller.cur_trans_q.empty() and not self.right_xarm_controller.cur_trans_q.empty():
                    self.state["robot_out"] = {
                        "time": time.time(),
                        "left_value": self.left_xarm_controller.cur_trans_q.get(),
                        "right_value": self.right_xarm_controller.cur_trans_q.get()
                    }
                if not self.left_xarm_controller.cur_gripper_q.empty() and not self.right_xarm_controller.cur_trans_q.empty():
                    self.state["gripper_out"] = {
                        "time": time.time(),
                        "left_value": self.left_xarm_controller.cur_gripper_q.get(),
                        "right_value": self.right_xarm_controller.cur_gripper_q.get()
                    }
        else:
            assert self.xarm_controller is not None
            if self.xarm_controller.is_controller_alive:
                if not self.xarm_controller.cur_trans_q.empty():
                    self.state["robot_out"] = {
                        "time": time.time(),
                        "value": self.xarm_controller.cur_trans_q.get()
                    }
                if not self.xarm_controller.cur_gripper_q.empty():
                    self.state["gripper_out"] = {
                        "time": time.time(),
                        "value": self.xarm_controller.cur_gripper_q.get()
                    }
        return

    def update_real_state(self) -> None:
        while self.real_alive:
            try:
                if self.use_robot:
                    self._update_robot()
                if self.perception is not None:
                    self._update_perception()
            except BaseException as e:
                print(f"Error in update_real_state: {e.with_traceback()}")
                break
        print("update_real_state stopped")

    def display_image(self):
        pygame.init()
        self.image_window = pygame.display.set_mode((self.screen_width, self.screen_height))
        pygame.display.set_caption('Image Display Window')
        while self._alive.value:
            # Extract image data from the shared array
            image = np.frombuffer(self.image_data.get_obj(), dtype=np.uint8).reshape((self.screen_height, self.screen_width, 3))
            pygame_image = pygame.surfarray.make_surface(image.swapaxes(0, 1))

            # Blit the image to the window
            self.image_window.blit(pygame_image, (0, 0))
            pygame.display.update()

            # Handle events (e.g., close window)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.stop()
                    pygame.quit()
                    return

            time.sleep(1 / self.realsense.capture_fps)  # 30 FPS
        print("Image display stopped")

    def start_image_display(self):
        # Start a thread for the image display loop
        self.image_display_thread = threading.Thread(name="display_image", target=self.display_image)
        self.image_display_thread.start()

    def run(self) -> None:

        robot_record_dir = None
        if self.bimanual:
            self.command_sender_left = udpSender(port=XARM_CONTROL_PORT_L)
            self.command_sender_right = udpSender(port=XARM_CONTROL_PORT_R)
        else:
            self.command_sender = udpSender(port=XARM_CONTROL_PORT)

        # initialize images
        rgbs = []  # bgr
        depths = []
        resolution = self.realsense.resolution
        for i in range(len(self.realsense.serial_numbers)):
            rgbs.append(np.zeros((resolution[1], resolution[0], 3), np.uint8))
            depths.append(np.zeros((resolution[1], resolution[0]), np.uint16))

        fps = self.record_fps if self.record_fps and self.record_fps > 0 else self.realsense.capture_fps  # visualization fps
        idx = 0

        while self.alive:
            try:
                tic = time.time()
                state = deepcopy(self.state)
                if self.bimanual:
                    b2w_l = state["b2w_l"]
                    b2w_r = state["b2w_r"]
                    b2w = None
                else:
                    b2w = state["b2w"]
                    b2w_l = None
                    b2w_r = None

                # update images from realsense to shared memory
                perception_out = state.get("perception_out", None)
                robot_out = state.get("robot_out", None)
                gripper_out = state.get("gripper_out", None)

                # clear previous images
                if perception_out is not None:
                    self.state["perception_out"] = None

                intrinsics = self.get_intrinsics()
                state["intr"] = intrinsics
                self.state["intr"] = intrinsics
                self.perception.update_intrinsics(intrinsics)

                update_perception = True
                if update_perception and perception_out is not None:
                    print("update image display")
                    for k in range(len(perception_out['value']['color'])):
                        rgbs[k] = perception_out['value']['color'][k].copy()
                        depths[k] = perception_out['value']['depth'][k].copy()
                        intr = intrinsics[k]

                        l = 0.1
                        origin = state["extr"][k] @ np.array([0, 0, 0, 1])
                        x_axis = state["extr"][k] @ np.array([l, 0, 0, 1])
                        y_axis = state["extr"][k] @ np.array([0, l, 0, 1])
                        z_axis = state["extr"][k] @ np.array([0, 0, l, 1])
                        origin = origin[:3] / origin[2]
                        x_axis = x_axis[:3] / x_axis[2]
                        y_axis = y_axis[:3] / y_axis[2]
                        z_axis = z_axis[:3] / z_axis[2]
                        origin = intr @ origin
                        x_axis = intr @ x_axis
                        y_axis = intr @ y_axis
                        z_axis = intr @ z_axis
                        cv2.line(rgbs[k], (int(origin[0]), int(origin[1])), (int(x_axis[0]), int(x_axis[1])), (255, 0, 0), 2)
                        cv2.line(rgbs[k], (int(origin[0]), int(origin[1])), (int(y_axis[0]), int(y_axis[1])), (0, 255, 0), 2)
                        cv2.line(rgbs[k], (int(origin[0]), int(origin[1])), (int(z_axis[0]), int(z_axis[1])), (0, 0, 255), 2)
                        if self.use_robot:
                            eef_points = np.concatenate([self.eef_point, np.ones((self.eef_point.shape[0], 1))], axis=1)  # (n, 4)
                            eef_colors = [(0, 255, 255)]

                            eef_axis = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])  # (3, 4)
                            eef_axis_colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0)]

                            if robot_out is not None:
                                assert gripper_out is not None
                                eef_points_world_vis = []
                                eef_points_vis = []
                                if self.bimanual:
                                    left_eef_world_list = []
                                    right_eef_world_list = []
                                    assert b2w_l is not None
                                    assert b2w_r is not None
                                    for val, b2w, eef_world_list in zip(["left_value", "right_value"], [b2w_l, b2w_r], [left_eef_world_list, right_eef_world_list]):
                                        e2b = robot_out[val]  # (4, 4)
                                        eef_points_world = (b2w @ e2b @ eef_points.T).T[:, :3]  # (n, 3)
                                        eef_points_vis.append(eef_points)
                                        eef_points_world_vis.append(eef_points_world)
                                        eef_orientation_world = (b2w[:3, :3] @ e2b[:3, :3] @ eef_axis[:, :3].T).T  # (3, 3)
                                        eef_world = np.concatenate([eef_points_world, eef_orientation_world], axis=0)  # (n+3, 3)
                                        eef_world_list.append(eef_world)
                                    left_eef_world = np.concatenate(left_eef_world_list, axis=0)  # (n+3, 3)
                                    right_eef_world = np.concatenate(right_eef_world_list, axis=0)  # (n+3, 3)
                                    eef_world = np.concatenate([left_eef_world, right_eef_world], axis=0)  # (2n+6, 3)
                                else:
                                    assert b2w is not None
                                    e2b = robot_out["value"]  # (4, 4)
                                    eef_points_world = (b2w @ e2b @ eef_points.T).T[:, :3]  # (n, 3)
                                    eef_points_vis.append(eef_points)
                                    eef_points_world_vis.append(eef_points_world)
                                    eef_orientation_world = (b2w[:3, :3] @ e2b[:3, :3] @ eef_axis[:, :3].T).T  # (3, 3)
                                    eef_world = np.concatenate([eef_points_world, eef_orientation_world], axis=0)  # (n+3, 3)
                                
                                # add gripper
                                if self.bimanual:
                                    left_gripper = gripper_out["left_value"]
                                    right_gripper = gripper_out["right_value"]
                                    gripper_world = np.array([left_gripper, right_gripper, 0.0])[None, :]  # (1, 3)
                                else:
                                    gripper = gripper_out["value"]
                                    gripper_world = np.array([gripper, 0.0, 0.0])[None, :]  # (1, 3)

                                eef_world = np.concatenate([eef_world, gripper_world], axis=0)  # (n+4, 3) or (2n+7, 3)
                                if robot_record_dir is not None:
                                    np.savetxt(robot_record_dir / f"{robot_out['time']:.3f}.txt", eef_world, fmt="%.6f")
                                
                                eef_points_vis = np.concatenate(eef_points_vis, axis=0)
                                eef_points_world_vis = np.concatenate(eef_points_world_vis, axis=0)
                                eef_points_world_vis = np.concatenate([eef_points_world_vis, np.ones((eef_points_world_vis.shape[0], 1))], axis=1)  # (n, 4)
                                eef_colors = eef_colors * eef_points_world_vis.shape[0]
                                
                                if self.bimanual:
                                    for point_orig, point, color, val, b2w in zip(eef_points_vis, eef_points_world_vis, eef_colors, ["left_value", "right_value"], [b2w_l, b2w_r]):
                                        e2b = robot_out[val]  # (4, 4)
                                        point = state["extr"][k] @ point
                                        point = point[:3] / point[2]
                                        point = intr @ point
                                        cv2.circle(rgbs[k], (int(point[0]), int(point[1])), 2, color, -1)
                                    
                                        # draw eef axis
                                        for axis, color in zip(eef_axis, eef_axis_colors):
                                            eef_point_axis = point_orig + 0.1 * axis
                                            eef_point_axis_world = (b2w @ e2b @ eef_point_axis).T
                                            eef_point_axis_world = state["extr"][k] @ eef_point_axis_world
                                            eef_point_axis_world = eef_point_axis_world[:3] / eef_point_axis_world[2]
                                            eef_point_axis_world = intr @ eef_point_axis_world
                                            cv2.line(rgbs[k], 
                                                (int(point[0]), int(point[1])), 
                                                (int(eef_point_axis_world[0]), int(eef_point_axis_world[1])), 
                                                color, 2)
                                else:
                                    point_orig, point, color, val, b2w = eef_points_vis[0], eef_points_world_vis[0], eef_colors[0], "value", b2w
                                    e2b = robot_out[val]  # (4, 4)
                                    point = state["extr"][k] @ point
                                    point = point[:3] / point[2]
                                    point = intr @ point
                                    cv2.circle(rgbs[k], (int(point[0]), int(point[1])), 2, color, -1)
                                
                                    # draw eef axis
                                    for axis, color in zip(eef_axis, eef_axis_colors):
                                        eef_point_axis = point_orig + 0.1 * axis
                                        eef_point_axis_world = (b2w @ e2b @ eef_point_axis).T
                                        eef_point_axis_world = state["extr"][k] @ eef_point_axis_world
                                        eef_point_axis_world = eef_point_axis_world[:3] / eef_point_axis_world[2]
                                        eef_point_axis_world = intr @ eef_point_axis_world
                                        cv2.line(rgbs[k], 
                                            (int(point[0]), int(point[1])), 
                                            (int(eef_point_axis_world[0]), int(eef_point_axis_world[1])), 
                                            color, 2)

                    row_imgs = []
                    for row in range(len(self.realsense.serial_numbers)):
                        row_imgs.append(
                            np.hstack(
                                (cv2.cvtColor(rgbs[row], cv2.COLOR_BGR2RGB), 
                                cv2.applyColorMap(cv2.convertScaleAbs(depths[row], alpha=0.03), cv2.COLORMAP_JET))
                            )
                        )
                    combined_img = np.vstack(row_imgs)
                    combined_img = cv2.resize(combined_img, (self.screen_width,self.screen_height))
                    np.copyto(
                        np.frombuffer(self.image_data.get_obj(), dtype=np.uint8).reshape((self.screen_height, self.screen_width, 3)), 
                        combined_img
                    )
                
                # save target
                if self.construct_target and perception_out is not None and robot_out is not None:
                    assert os.path.exists(self.exp_root.parent / "target")
                    save_dir = str(self.exp_root.parent / "target" / "target.pcd")
                    pts = perception_out['value']['pts']
                    print(f"target pts: {pts}")
                    pts = np.concatenate(pts, axis=0)
                    pcd = o3d.geometry.PointCloud()
                    pcd.points = o3d.utility.Vector3dVector(pts)
                    o3d.io.write_point_cloud(save_dir, pcd)
                    
                    mkdir(self.exp_root.parent / "target" / "vis", overwrite=False, resume=True)
                    eef_points_world_vis = []
                    eef_points = np.concatenate([self.eef_point, np.ones((self.eef_point.shape[0], 1))], axis=1)  # (n, 4)
                    eef_axis = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])  # (3, 4)
                    if self.bimanual:
                        left_eef_world_list = []
                        right_eef_world_list = []
                        assert b2w_l is not None
                        assert b2w_r is not None
                        for val, b2w, eef_world_list in zip(["left_value", "right_value"], [b2w_l, b2w_r], [left_eef_world_list, right_eef_world_list]):
                            e2b = robot_out[val]  # (4, 4)
                            eef_points_world = (b2w @ e2b @ eef_points.T).T[:, :3]  # (n, 3)
                            eef_points_world_vis.append(eef_points_world)
                            eef_orientation_world = (b2w[:3, :3] @ e2b[:3, :3] @ eef_axis[:, :3].T).T  # (3, 3)
                            eef_world = np.concatenate([eef_points_world, eef_orientation_world], axis=0)  # (n+3, 3)
                            eef_world_list.append(eef_world)
                        left_eef_world = np.concatenate(left_eef_world_list, axis=0)  # (n+3, 3)
                        right_eef_world = np.concatenate(right_eef_world_list, axis=0)  # (n+3, 3)
                        eef_world = np.concatenate([left_eef_world, right_eef_world], axis=0)  # (2n+6, 3)
                    else:
                        assert b2w is not None
                        e2b = robot_out["value"]  # (4, 4)
                        eef_points_world = (b2w @ e2b @ eef_points.T).T[:, :3]  # (n, 3)
                        eef_points_world_vis.append(eef_points_world)
                        eef_orientation_world = (b2w[:3, :3] @ e2b[:3, :3] @ eef_axis[:, :3].T).T  # (3, 3)
                        eef_world = np.concatenate([eef_points_world, eef_orientation_world], axis=0)  # (n+3, 3)
                    np.savetxt(str(self.exp_root.parent / "target" / "vis" / f"robot.txt"), eef_world, fmt="%.6f")

                    for k in range(len(perception_out['value']['color'])):
                        rgbs[k] = perception_out['value']['color'][k]
                        depths[k] = perception_out['value']['depth'][k]
                        rgb_save_dir = str(self.exp_root.parent / "target" / "vis" / f"rgb_{k}.png")
                        depth_save_dir = str(self.exp_root.parent / "target" / "vis" / f"depth_{k}.png")
                        cv2.imwrite(rgb_save_dir, rgbs[k])
                        cv2.imwrite(depth_save_dir, depths[k])
                    
                    intr_list = []
                    extr_list = []
                    for k in range(len(perception_out['value']['color'])):
                        intr = intrinsics[k]
                        extr = state["extr"][k]
                        intr_list.append(intr)
                        extr_list.append(extr)
                    np.save(str(self.exp_root.parent / "target" / "vis" / "intrinsics.npy"), np.stack(intr_list))
                    np.save(str(self.exp_root.parent / "target" / "vis" / "extrinsics.npy"), np.stack(extr_list))

                    print(f"target saved to {save_dir}")
                    time.sleep(5)
                    continue
                
                # do planning
                if perception_out is not None and robot_out is not None and gripper_out is not None:
                    if len(perception_out['value']['pts']) > 0:
                        self.perception.do_process.value = False  # pause perception
                        command = self.planning_module.get_command(state, save_dir=f'{self.exp_root}/interaction_{idx:02d}', is_first_iter=(idx == 0))
                        if self.bimanual:
                            self.command_sender_left.send(command[0])
                            self.command_sender_right.send(command[1])
                        else:
                            self.command_sender.send(command[0])
                        idx += 1
                        time.sleep(10)  # TODO execution time
                        self.perception.do_process.value = True  # resume perception
                    else:
                        print(f'no points detected in perception_out: {perception_out["value"]["pts"]}')
                else:
                    print(f'perception_out is None: {perception_out is None}', end=' ')
                    print(f'robot_out is None: {robot_out is None}', end=' ')
                    print(f'gripper_out is None: {gripper_out is None}')

                time.sleep(max(0, 1 / fps - (time.time() - tic)))

            except BaseException as e:
                print(f"Error in robot planning env: {e.with_traceback()}")
                break

        if self.bimanual:
            assert self.command_sender_left is not None
            assert self.command_sender_right is not None
            self.command_sender_left.close()
            self.command_sender_right.close()
        else:
            assert self.command_sender is not None
            self.command_sender.close()
        self.stop()
        print("RealEnv process stopped")

    def get_intrinsics(self):
        return self.realsense.get_intrinsics()

    def get_extrinsics(self):
        return self.state["extr"]

    @property
    def alive(self) -> bool:
        alive = self._alive.value and self.real_alive
        self._alive.value = alive
        return alive

    def start(self) -> None:
        self.start_time = time.time()
        self._alive.value = True
        self.real_start(time.time())
        self.start_image_display()
        super().start()

    def stop(self) -> None:
        self._alive.value = False
        self.real_stop()