-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_teleop.py
More file actions
220 lines (186 loc) · 8.19 KB
/
Copy pathdebug_teleop.py
File metadata and controls
220 lines (186 loc) · 8.19 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
#!/usr/bin/env python3
"""
Debug Teleop - Shows exactly what's happening in the teleop loop
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import time
import pickle
from frankateach.utils import notify_component_start
from frankateach.network import (
ZMQKeypointSubscriber,
create_request_socket,
ZMQKeypointPublisher,
)
from frankateach.constants import (
COMMANDED_STATE_PORT,
CONTROL_PORT,
HOST,
STATE_PORT,
VR_CONTROLLER_STATE_PORT,
GRIPPER_OPEN,
GRIPPER_CLOSE,
)
from frankateach.messages import FrankaAction, FrankaState
class DebugFrankaOperator:
def __init__(self, teleop_mode="human"):
print("🔧 Initializing DebugFrankaOperator...")
# Subscribe controller state
print("🔧 Creating controller state subscriber...")
self._controller_state_subscriber = ZMQKeypointSubscriber(
host=HOST, port=VR_CONTROLLER_STATE_PORT, topic="controller_state"
)
print("✅ Controller state subscriber created")
print("🔧 Creating action socket...")
self.action_socket = create_request_socket(HOST, CONTROL_PORT)
print("✅ Action socket created")
print("🔧 Creating state publishers...")
self.state_socket = ZMQKeypointPublisher(HOST, STATE_PORT)
self.commanded_state_socket = ZMQKeypointPublisher(HOST, COMMANDED_STATE_PORT)
print("✅ State publishers created")
# Class variables
self.is_first_frame = True
self.gripper_state = GRIPPER_OPEN
self.start_teleop = False
self.init_affine = None
self.teleop_mode = teleop_mode
self.home_offset = [-0.22, 0.0, 0.1] if teleop_mode == "human" else [0, 0, 0]
print(f"✅ DebugFrankaOperator initialized for {teleop_mode} mode")
def debug_apply_retargeted_angles(self):
print(f"\n🔄 [Frame] Starting _apply_retargeted_angles (first_frame: {self.is_first_frame})")
# Try to receive controller state
print("📡 Receiving controller state...")
try:
self.controller_state = self._controller_state_subscriber.recv_keypoints()
print(f"✅ Received controller state:")
print(f" Right A: {self.controller_state.right_a}")
print(f" Right B: {self.controller_state.right_b}")
print(f" Right position: {self.controller_state.right_local_position}")
except Exception as e:
print(f"❌ Error receiving controller state: {e}")
return
if self.is_first_frame:
print("🏠 First frame - performing reset sequence...")
# Reset robot
print("🔄 Sending reset action...")
action = FrankaAction(
pos=np.zeros(3),
quat=np.zeros(4),
gripper=self.gripper_state,
reset=True,
timestamp=time.time(),
)
self.action_socket.send(bytes(pickle.dumps(action, protocol=-1)))
robot_state = pickle.loads(self.action_socket.recv())
print(f"✅ Reset complete: {robot_state}")
# Move to offset position
print("🎯 Moving to offset position...")
import numpy as np
target_pos = robot_state.pos + np.array(self.home_offset)
target_quat = robot_state.quat
action = FrankaAction(
pos=target_pos.flatten().astype(np.float32),
quat=target_quat.flatten().astype(np.float32),
gripper=self.gripper_state,
reset=False,
timestamp=time.time(),
)
self.action_socket.send(bytes(pickle.dumps(action, protocol=-1)))
robot_state = pickle.loads(self.action_socket.recv())
print(f"✅ Moved to home position: {robot_state}")
# Store home position
from deoxys.utils import transform_utils
self.home_rot, self.home_pos = (
transform_utils.quat2mat(robot_state.quat),
robot_state.pos,
)
print(f"🏠 Home position stored: {self.home_pos}")
self.is_first_frame = False
print("✅ First frame complete, entering main loop...")
# Check button states
print(f"🎮 Button states - A: {self.controller_state.right_a}, B: {self.controller_state.right_b}")
if self.controller_state.right_a:
print("🟢 A button pressed - Starting teleop!")
self.start_teleop = True
self.init_affine = self.controller_state.right_affine
if self.controller_state.right_b:
print("🔴 B button pressed - Stopping teleop!")
self.start_teleop = False
self.init_affine = None
# Get current robot state
self.action_socket.send(b"get_state")
robot_state = pickle.loads(self.action_socket.recv())
if robot_state != b"state_error":
from deoxys.utils import transform_utils
self.home_rot, self.home_pos = (
transform_utils.quat2mat(robot_state.quat),
robot_state.pos,
)
print(f"📊 Teleop status: {self.start_teleop}")
# In human mode, always send get_state
if self.teleop_mode == "human":
print("👤 Human mode - sending get_state request...")
self.action_socket.send(b"get_state")
else:
print("🤖 Robot mode - would send movement commands...")
# Would send actual movement commands in robot mode
# Receive robot state
print("📥 Receiving robot state...")
robot_state = self.action_socket.recv()
robot_state = pickle.loads(robot_state)
robot_state.start_teleop = self.start_teleop
print(f"✅ Robot state received: start_teleop={robot_state.start_teleop}")
# Publish states
print("📤 Publishing states...")
self.state_socket.pub_keypoints(robot_state, "robot_state")
# Create dummy action for commanded state
import numpy as np
from deoxys.utils import transform_utils
dummy_action = FrankaAction(
pos=self.home_pos.flatten().astype(np.float32),
quat=transform_utils.mat2quat(self.home_rot).flatten().astype(np.float32),
gripper=self.gripper_state,
reset=False,
timestamp=time.time(),
)
self.commanded_state_socket.pub_keypoints(dummy_action, "commanded_robot_state")
print("✅ States published")
def debug_stream(self):
notify_component_start("Debug Franka teleoperator control")
print("🚀 Starting debug teleop stream...")
print("🎮 Use mouse VR server to control:")
print(" - Right click: A button (start/stop recording)")
print(" - Left click + move: Hand tracking")
print(" - Press Ctrl+C to stop")
print()
loop_count = 0
try:
while True:
loop_count += 1
print(f"\n{'='*60}")
print(f"🔄 LOOP {loop_count}")
print(f"{'='*60}")
# Call the debug version
self.debug_apply_retargeted_angles()
print(f"✅ Loop {loop_count} complete")
time.sleep(0.1) # Small delay to make output readable
except KeyboardInterrupt:
print("\n🛑 Debug teleop stopped by user")
except Exception as e:
print(f"\n❌ Error in debug teleop: {e}")
import traceback
traceback.print_exc()
finally:
print("🧹 Cleaning up...")
self._controller_state_subscriber.stop()
self.action_socket.close()
print("✅ Cleanup complete")
def main():
import numpy as np # Import here to avoid issues
print("🐛 Starting Debug Teleop for Human Mode")
print("=" * 50)
operator = DebugFrankaOperator(teleop_mode="human")
operator.debug_stream()
if __name__ == "__main__":
main()