File size: 12,161 Bytes
211ae03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import time
import threading
import cv2
import numpy as np
import dataclasses
import logging
import pathlib
import zmq
import tyro
from typing import Optional
from multiprocessing import Array, Event

from teleop.master_whole_body import RobotTaskmaster
from teleop.robot_control.compute_tau import GetTauer
from groot_policy.server_client import PolicyClient
logger = logging.getLogger(__name__)


@dataclasses.dataclass
class Args:
    """Command line arguments."""

    # Host and port to connect to the server.
    host: str = "127.0.0.1"
    # Port to connect to the server. If None, the server will use the default port.
    port: Optional[int] = 8014

    api_key: Optional[str] = None
    # Number of steps to run the policy for.
    num_steps: int = 20
    # Path to save the timings to a parquet file. (e.g., timing.parquet)
    timing_file: Optional[pathlib.Path] = None
    # Environment to run the policy in.
    # env: EnvMode = EnvMode.ALOHA_SIM

args = tyro.cli(Args)
policy = PolicyClient(host=args.host, port=args.port)
VIDEO_KEY = "rs_view"

TASK_INSTRUCTION = "Push cart grasp and place grapes on plate."

FREQ_VLA = 30     
FREQ_CTRL = 60  
MAX_STEPS = 500

ACTION_REPEAT = max(1, int(round(FREQ_CTRL / FREQ_VLA)))


class RSCamera:
    def __init__(self):
        self.context = zmq.Context()
        self.socket = self.context.socket(zmq.REQ)
        self.socket.connect("tcp://192.168.123.164:5556")

    def get_frame(self):
        self.socket.send(b"get_frame")

        rgb_bytes, _, _ = self.socket.recv_multipart()

        rgb_array = np.frombuffer(rgb_bytes, np.uint8)
        rgb_image = cv2.imdecode(rgb_array, cv2.IMREAD_COLOR)
        return rgb_image



def get_observation(camera, state):
    frame = camera.get_frame()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    height = float(state["height"][0]) if isinstance(state["height"], np.ndarray) else float(
        state["height"]
    )
    rpy = np.asarray(state["rpy"], dtype=np.float32)
    left_arm = np.asarray(state["left_arm"], dtype=np.float32)
    right_arm = np.asarray(state["right_arm"], dtype=np.float32)
    left_hand = np.asarray(state["left_hand"], dtype=np.float32)
    right_hand = np.asarray(state["right_hand"], dtype=np.float32)
    obs = {
        "video": {
            VIDEO_KEY: frame[None, None, :, :, :].astype(np.uint8),
        },
        "state": {
            "height": np.array([[[height]]], dtype=np.float32),
            "left_arm": left_arm[None, None, :],
            "right_arm": right_arm[None, None, :],
            "left_hand": left_hand[None, None, :],
            "right_hand": right_hand[None, None, :],
            "rpy": rpy[None, None, :],
        },
        "language": {
            "annotation.human.task_description": [[TASK_INSTRUCTION]],
        },
    }
    return obs


def main():
    shared_data = {
        "kill_event": Event(),
        "session_start_event": Event(),
        "failure_event": Event(),
        "end_event": Event(),
        "dirname": None,
    }
    kill_event = shared_data["kill_event"]

    robot_shm_array = Array("d", 512, lock=False)
    teleop_shm_array = Array("d", 64, lock=False)

    

    master = RobotTaskmaster(
        task_name="inference",
        shared_data=shared_data,
        robot_shm_array=robot_shm_array,
        teleop_shm_array=teleop_shm_array,
        robot="g1",
    )

    master.reset_yaw_offset = True

    get_tauer = GetTauer()
    camera = RSCamera()

    pred_action_buffer = {"actions": None, "idx": 0}
    pred_action_lock = threading.Lock()
    state_lock = threading.Lock()
    shared_robot_state = {
        "motor": None,
        "hand": None,
    }


    running = Event()
    running.set()

    sequence_done_event = Event()
    sequence_done_event.set() 

    def action_request_thread():
        for step in range(MAX_STEPS):
            if not running.is_set():
                break

            sequence_done_event.wait()

            time.sleep(1/FREQ_VLA)

            try:
                with state_lock:
                    motor = shared_robot_state["motor"].copy() if shared_robot_state["motor"] is not None else None
                    hand = shared_robot_state["hand"].copy() if shared_robot_state["hand"] is not None else None

                if motor is None or hand is None:
                    print("[VLA] Waiting for robot state...")
                    time.sleep(0.01)
                    continue

                arm_joints = motor[15:29]
                hand_joints = hand
                leg_joints = motor[:15]

                # HTTP obs payload
                state = {
                    "rpy": np.array([
                        master.torso_roll,
                        master.torso_pitch,
                        master.torso_yaw,
                    ], dtype=np.float32),
                    "height": np.array([master.torso_height], dtype=np.float32),
                    "left_arm": arm_joints[0:7],
                    "right_arm": arm_joints[7:14],
                    "left_hand": hand_joints[0:7],
                    "right_hand": hand_joints[7:14],
                }
                obs = get_observation(camera, state)
                action_dict, _ = policy.get_action(obs)

                keys = [
                    "left_hand",
                    "right_hand",
                    "left_arm",
                    "right_arm",
                    "rpy",
                    "height",
                    "torso_vx",
                    "torso_vy",
                    "torso_vyaw",
                    "target_yaw",
                ]
                actions = np.concatenate([action_dict[k][0] for k in keys], axis=1)
                assert actions.shape == (16, 36), (
                    f"expecting actions.shape = (16, 36), found {actions.shape}"
                )

                
                with pred_action_lock:
                    pred_action_buffer["actions"] = actions
                    pred_action_buffer["idx"] = 0

                print(f"[VLA] Got action sequence: {len(actions)} actions")

                sequence_done_event.clear()

            except Exception as e:
                print(f"[VLA] Inference error: {e}")
                time.sleep(0.05)


    def apply_action_from_buffer():
        current_lr_arm_q, current_lr_arm_dq = master.get_robot_data()
        with state_lock:
            shared_robot_state["motor"] = master.motorstate.copy()
            shared_robot_state["hand"] = master.handstate.copy()

        with pred_action_lock:
            actions = pred_action_buffer["actions"]
            idx = pred_action_buffer["idx"]

            action = None
            not_between_rollouts = False

            if actions is not None:
                real_idx = idx // ACTION_REPEAT
                if real_idx < len(actions):
                    action = actions[real_idx]
                    not_between_rollouts = True

                    pred_action_buffer["idx"] += 1

                    next_real_idx = pred_action_buffer["idx"] // ACTION_REPEAT
                    if next_real_idx >= len(actions):
                        pred_action_buffer["actions"] = None
                        pred_action_buffer["idx"] = 0
                        sequence_done_event.set()
                else:
                    pred_action_buffer["actions"] = None
                    pred_action_buffer["idx"] = 0
                    sequence_done_event.set()

        arm_cmd = None
        hand_cmd = None
        if not_between_rollouts:
            if action.shape[0] < 36:
                print("[CTRL] Invalid action shape:", action.shape)
            else:
                vx = action[32]
                vy = action[33]
                vyaw = action[34]
                target_yaw = action[35]

                vx = 0.35 if vx > 0.25 else 0
                vy = 0 if abs(vy) < 0.3 else 0.5 * (1 if vy > 0 else -1)

                rpyh   = action[28:32]
                arm_cmd = action[14:28]
                hand_cmd = action[0:14]

                master.torso_roll   = rpyh[0]
                master.torso_pitch  = rpyh[1]
                master.torso_yaw    = rpyh[2]
                master.torso_height = rpyh[3]

                master.vx = vx
                master.vy = vy
                master.vyaw = vyaw
                master.target_yaw = target_yaw


                master.prev_torso_roll   = master.torso_roll
                master.prev_torso_pitch  = master.torso_pitch
                master.prev_torso_yaw    = master.torso_yaw
                master.prev_torso_height = master.torso_height

                master.prev_vx   = master.vx
                master.prev_vy  = master.vy
                master.prev_vyaw    = master.vyaw
                master.prev_target_yaw = master.target_yaw

                master.prev_arm = arm_cmd
                master.prev_hand = hand_cmd

                print("VLA output vx, vy, vyaw, target_yaw, rpyh:", vx, vy, vyaw, target_yaw, rpyh)
        
        if not not_between_rollouts:
            master.torso_roll   = master.prev_torso_roll
            master.torso_pitch  = master.prev_torso_pitch
            master.torso_yaw    = master.prev_torso_yaw
            master.torso_height = master.prev_torso_height

            arm_cmd = master.prev_arm
            hand_cmd = master.prev_hand

            master.vx = master.prev_vx
            master.vy = 0
            master.vyaw = master.prev_vyaw
            master.target_yaw = master.prev_target_yaw
        

        master.get_ik_observation(record=False)


        pd_target, pd_tauff, raw_action = master.body_ik.solve_whole_body_ik(
            left_wrist=None,
            right_wrist=None,
            current_lr_arm_q=current_lr_arm_q,
            current_lr_arm_dq=current_lr_arm_dq,
            observation=master.observation,
            extra_hist=master.extra_hist,
            is_teleop=False,
        )

        master.last_action = np.concatenate([
            raw_action.copy(),
            (master.motorstate - master.default_dof_pos)[15:] / master.action_scale,
        ])


        if arm_cmd is not None:
            pd_target[15:] = arm_cmd
            tau_arm = np.asarray(get_tauer(arm_cmd), dtype=np.float64).reshape(-1)
            pd_tauff[15:] = tau_arm

        if hand_cmd is not None:
            with master.dual_hand_data_lock:
                master.hand_shm_array[:] = hand_cmd

        master.body_ctrl.ctrl_whole_body(
            pd_target[15:], pd_tauff[15:], pd_target[:15], pd_tauff[:15]
        )

        return pd_target
    


    def control_loop_thread():
        dt = 1.0 / FREQ_CTRL
        while running.is_set() and not kill_event.is_set():
            try:
                apply_action_from_buffer()
            except Exception as e:
                print("[CTRL] loop error:", e)
            time.sleep(dt) 
        print("[CTRL] Control loop stopped.")

    try:
        stabilize_thread = threading.Thread(target=master.maintain_standing, daemon=True)
        stabilize_thread.start()
        master.episode_kill_event.set()
        print("[MAIN] Initialize with standing pose...")
        time.sleep(30)
        master.episode_kill_event.clear()  

        master.reset_yaw_offset = True

        t_req = threading.Thread(target=action_request_thread, daemon=True)
        t_ctrl = threading.Thread(target=control_loop_thread, daemon=True)
        t_req.start()
        t_ctrl.start()

        print("[MAIN] Running. Ctrl+C to stop.")
        while not kill_event.is_set():
            time.sleep(0.5)

        print("[MAIN] kill_event set, preparing to stop...")
        running.clear()
        time.sleep(0.5)

        master.episode_kill_event.set()
        print("[MAIN] Returning to standing pose for 5s...")
        time.sleep(5)
        master.episode_kill_event.clear()

    except KeyboardInterrupt:
        print("[MAIN] Caught Ctrl+C, exiting...")
        running.clear()
        kill_event.set()
    finally:
        shared_data["end_event"].set()
        master.stop()
        print("[MAIN] Shutdown complete.")

if __name__ == "__main__":
    main()