-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo2_web_interface_enhanced.py
More file actions
549 lines (458 loc) · 19.3 KB
/
go2_web_interface_enhanced.py
File metadata and controls
549 lines (458 loc) · 19.3 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
#!/usr/bin/env python3
"""
Unitree Go2 Web Interface - Enhanced with Joystick Control and Movement Sequences
"""
from flask import Flask, render_template, Response, jsonify, request
from flask_cors import CORS
import cv2
import asyncio
import threading
import time
from queue import Queue
import logging
import json
import os
# Suppress OpenCV warnings
os.environ['OPENCV_LOG_LEVEL'] = 'ERROR'
cv2.setLogLevel(0)
from go2_webrtc_driver.webrtc_driver import Go2WebRTCConnection, WebRTCConnectionMethod
from go2_webrtc_driver.constants import RTC_TOPIC, SPORT_CMD
from aiortc import MediaStreamTrack
app = Flask(__name__)
CORS(app)
logging.basicConfig(level=logging.WARNING)
# Global variables
robot_connection = None
frame_queue = Queue(maxsize=10)
is_connected = False
channels_ready = False
asyncio_loop = None
asyncio_thread = None
movement_active = False
current_velocity = {'x': 0.0, 'y': 0.0, 'z': 0.0}
sequence_running = False
sequence_abort = False
ROBOT_IP = "192.168.12.1"
# Movement limits
MAX_LINEAR_SPEED = 1.0 # m/s
MAX_ANGULAR_SPEED = 1.5 # rad/s
@app.route('/')
def index():
return render_template('index_enhanced.html')
@app.route('/connect', methods=['POST'])
def connect():
global robot_connection, is_connected, channels_ready, asyncio_loop, asyncio_thread
data = request.json
ip = data.get('ip', ROBOT_IP)
if is_connected:
return jsonify({'status': 'info', 'message': 'Already connected'})
try:
print(f"Connecting to robot at {ip}...")
robot_connection = Go2WebRTCConnection(
WebRTCConnectionMethod.LocalSTA,
ip=ip
)
# Async callback for video
async def recv_camera_stream(track: MediaStreamTrack):
while True:
try:
frame = await track.recv()
img = frame.to_ndarray(format="bgr24")
if not frame_queue.full():
frame_queue.put(img)
except Exception as e:
print(f"Video stream error: {e}")
break
# Enhanced setup with channel waiting
async def setup():
global channels_ready
try:
# Connect to robot
print("Establishing WebRTC connection...")
await robot_connection.connect()
print("✓ WebRTC connection established")
# Wait for data channel to be ready
print("Waiting for data channel...")
max_wait = 10 # seconds
wait_time = 0
while wait_time < max_wait:
if hasattr(robot_connection, 'datachannel') and robot_connection.datachannel:
if hasattr(robot_connection.datachannel, 'pub_sub'):
print("✓ Data channel ready!")
break
await asyncio.sleep(0.5)
wait_time += 0.5
if wait_time >= max_wait:
raise Exception("Data channel did not initialize in time")
# IMPORTANT: Check and set motion mode to "normal"
print("Checking motion mode...")
try:
response = await robot_connection.datachannel.pub_sub.publish_request_new(
RTC_TOPIC["MOTION_SWITCHER"],
{"api_id": 1001}
)
if response['data']['header']['status']['code'] == 0:
data = json.loads(response['data']['data'])
current_mode = data['name']
print(f"Current motion mode: {current_mode}")
if current_mode != "normal":
print(f"Switching to normal mode...")
await robot_connection.datachannel.pub_sub.publish_request_new(
RTC_TOPIC["MOTION_SWITCHER"],
{
"api_id": 1002,
"parameter": {"name": "normal"}
}
)
await asyncio.sleep(2) # Wait for mode switch
print("✓ Switched to normal mode")
except Exception as e:
print(f"⚠️ Could not set motion mode: {e}")
print("Continuing anyway...")
# Start video
print("Starting video stream...")
robot_connection.video.switchVideoChannel(True)
robot_connection.video.add_track_callback(recv_camera_stream)
print("✓ Video stream started")
channels_ready = True
print("✓ All channels ready!")
except Exception as e:
print(f"Setup error: {e}")
channels_ready = False
raise
# Run async in thread
def run_asyncio_loop(loop):
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(setup())
loop.run_forever()
except Exception as e:
print(f"Asyncio loop error: {e}")
asyncio_loop = asyncio.new_event_loop()
asyncio_thread = threading.Thread(target=run_asyncio_loop, args=(asyncio_loop,))
asyncio_thread.daemon = True
asyncio_thread.start()
# Wait for channels to be ready
print("Waiting for initialization...")
for i in range(15): # Wait up to 15 seconds
if channels_ready:
break
time.sleep(1)
print(f"Waiting... {i+1}s")
if not channels_ready:
raise Exception("Failed to initialize channels")
is_connected = True
print("✓ Connection complete and ready!")
return jsonify({
'status': 'connected',
'message': 'Successfully connected to robot and channels ready'
})
except Exception as e:
is_connected = False
channels_ready = False
print(f"Connection failed: {e}")
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/disconnect', methods=['POST'])
def disconnect():
global robot_connection, is_connected, channels_ready, asyncio_loop, asyncio_thread, movement_active
try:
is_connected = False
channels_ready = False
movement_active = False
if asyncio_loop:
asyncio_loop.call_soon_threadsafe(asyncio_loop.stop)
if asyncio_thread:
asyncio_thread.join(timeout=2)
robot_connection = None
asyncio_loop = None
asyncio_thread = None
while not frame_queue.empty():
frame_queue.get()
print("Disconnected from robot")
return jsonify({'status': 'disconnected', 'message': 'Disconnected from robot'})
except Exception as e:
print(f"Disconnect error: {e}")
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/status')
def status():
return jsonify({
'connected': is_connected,
'channels_ready': channels_ready,
'ip': ROBOT_IP,
'movement_active': movement_active,
'velocity': current_velocity
})
@app.route('/command', methods=['POST'])
def execute_command():
global robot_connection, asyncio_loop
if not is_connected or not channels_ready:
return jsonify({
'status': 'error',
'message': 'Not connected or channels not ready'
}), 400
if not robot_connection or not asyncio_loop:
return jsonify({'status': 'error', 'message': 'Robot connection not available'}), 400
data = request.json
command = data.get('command')
try:
print(f"Executing command: {command}")
# Map commands
command_mapping = {
'stand': 'StandUp',
'sit': 'Sit',
'lie': 'Damp',
'damp': 'Damp',
'stop': 'StopMove',
'hello': 'Hello',
'dance': 'Dance1'
}
sport_cmd = command_mapping.get(command)
if not sport_cmd or sport_cmd not in SPORT_CMD:
return jsonify({
'status': 'error',
'message': f"Unknown command: {command}"
}), 400
# Check datachannel is available
if not hasattr(robot_connection, 'datachannel') or not robot_connection.datachannel:
return jsonify({
'status': 'error',
'message': 'Data channel not available'
}), 500
# Send command
async def send_command():
try:
await robot_connection.datachannel.pub_sub.publish_request_new(
RTC_TOPIC["SPORT_MOD"],
{"api_id": SPORT_CMD[sport_cmd]}
)
print(f"✓ Command {sport_cmd} sent successfully")
except Exception as e:
print(f"Command send error: {e}")
raise
future = asyncio.run_coroutine_threadsafe(send_command(), asyncio_loop)
future.result(timeout=5)
return jsonify({'status': 'success', 'command': command})
except Exception as e:
print(f"Command error: {e}")
import traceback
traceback.print_exc()
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/move', methods=['POST'])
def move():
"""Control robot movement with velocity commands"""
global robot_connection, asyncio_loop, current_velocity, movement_active
if not is_connected or not channels_ready:
return jsonify({
'status': 'error',
'message': 'Not connected or channels not ready'
}), 400
data = request.json
vx = float(data.get('vx', 0.0)) # Forward/backward (m/s)
vy = float(data.get('vy', 0.0)) # Left/right strafe (m/s)
vz = float(data.get('vz', 0.0)) # Rotation (rad/s)
# Apply limits
vx = max(-MAX_LINEAR_SPEED, min(MAX_LINEAR_SPEED, vx))
vy = max(-MAX_LINEAR_SPEED, min(MAX_LINEAR_SPEED, vy))
vz = max(-MAX_ANGULAR_SPEED, min(MAX_ANGULAR_SPEED, vz))
current_velocity = {'x': vx, 'y': vy, 'z': vz}
movement_active = (abs(vx) > 0.01 or abs(vy) > 0.01 or abs(vz) > 0.01)
try:
async def send_movement():
try:
# Movement command structure for go2_webrtc_driver
# FIXED: Pass parameter as dict directly, NOT with json.dumps()!
await robot_connection.datachannel.pub_sub.publish_request_new(
RTC_TOPIC["SPORT_MOD"],
{
"api_id": 1008, # Move command API ID
"parameter": {
"x": vx,
"y": vy,
"z": vz
}
}
)
except Exception as e:
print(f"Movement send error: {e}")
raise
future = asyncio.run_coroutine_threadsafe(send_movement(), asyncio_loop)
future.result(timeout=1)
return jsonify({
'status': 'success',
'velocity': current_velocity
})
except Exception as e:
print(f"Movement error: {e}")
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/sequence/execute', methods=['POST'])
def execute_sequence():
"""Execute a sequence of movements"""
global robot_connection, asyncio_loop, sequence_running, sequence_abort
if not is_connected or not channels_ready:
return jsonify({
'status': 'error',
'message': 'Not connected or channels not ready'
}), 400
if sequence_running:
return jsonify({
'status': 'error',
'message': 'Sequence already running. Use stop endpoint first.'
}), 400
data = request.json
sequence = data.get('sequence', [])
if not sequence:
return jsonify({'status': 'error', 'message': 'Empty sequence'}), 400
try:
sequence_running = True
sequence_abort = False
async def run_sequence():
global sequence_running, sequence_abort
try:
for i, step in enumerate(sequence):
# Check abort flag
if sequence_abort:
print("⛔ Sequence aborted by user")
# Send stop command
await robot_connection.datachannel.pub_sub.publish_request_new(
RTC_TOPIC["SPORT_MOD"],
{
"api_id": 1008,
"parameter": {"x": 0.0, "y": 0.0, "z": 0.0}
}
)
break
action = step.get('action')
duration = step.get('duration', 1.0)
print(f"Executing step {i+1}/{len(sequence)}: {action}")
if action == 'move':
# Movement step
vx = float(step.get('vx', 0.0))
vy = float(step.get('vy', 0.0))
vz = float(step.get('vz', 0.0))
# Apply limits
vx = max(-MAX_LINEAR_SPEED, min(MAX_LINEAR_SPEED, vx))
vy = max(-MAX_LINEAR_SPEED, min(MAX_LINEAR_SPEED, vy))
vz = max(-MAX_ANGULAR_SPEED, min(MAX_ANGULAR_SPEED, vz))
# Send movement command repeatedly during duration
end_time = time.time() + duration
while time.time() < end_time:
if sequence_abort:
break
await robot_connection.datachannel.pub_sub.publish_request_new(
RTC_TOPIC["SPORT_MOD"],
{
"api_id": 1008,
"parameter": {
"x": vx,
"y": vy,
"z": vz
}
}
)
await asyncio.sleep(0.1)
# Stop movement
await robot_connection.datachannel.pub_sub.publish_request_new(
RTC_TOPIC["SPORT_MOD"],
{
"api_id": 1008,
"parameter": {
"x": 0.0,
"y": 0.0,
"z": 0.0
}
}
)
elif action == 'command':
# Regular command step
command = step.get('command')
command_mapping = {
'stand': 'StandUp',
'sit': 'Sit',
'damp': 'Damp',
'stop': 'StopMove',
'hello': 'Hello',
'dance': 'Dance1'
}
sport_cmd = command_mapping.get(command)
if sport_cmd and sport_cmd in SPORT_CMD:
await robot_connection.datachannel.pub_sub.publish_request_new(
RTC_TOPIC["SPORT_MOD"],
{"api_id": SPORT_CMD[sport_cmd]}
)
await asyncio.sleep(duration)
elif action == 'wait':
# Wait step with abort checking
end_time = time.time() + duration
while time.time() < end_time:
if sequence_abort:
break
await asyncio.sleep(0.1)
if not sequence_abort:
print("✓ Sequence completed successfully")
except Exception as e:
print(f"Sequence execution error: {e}")
raise
finally:
sequence_running = False
sequence_abort = False
# Run in asyncio loop
asyncio.run_coroutine_threadsafe(run_sequence(), asyncio_loop)
return jsonify({
'status': 'success',
'message': 'Sequence started',
'steps': len(sequence)
})
except Exception as e:
sequence_running = False
sequence_abort = False
print(f"Sequence error: {e}")
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/sequence/stop', methods=['POST'])
def stop_sequence():
"""Stop currently running sequence"""
global sequence_abort, sequence_running
if not sequence_running:
return jsonify({
'status': 'info',
'message': 'No sequence is currently running'
})
sequence_abort = True
print("⛔ Sequence stop requested")
return jsonify({
'status': 'success',
'message': 'Sequence stop signal sent'
})
def generate_video():
"""Generator for video streaming"""
while True:
if is_connected and not frame_queue.empty():
try:
frame = frame_queue.get(timeout=1)
ret, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 80])
if ret:
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + buffer.tobytes() + b'\r\n')
except Exception as e:
pass
else:
time.sleep(0.033)
@app.route('/video_feed')
def video_feed():
return Response(generate_video(),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
print("=" * 60)
print("Unitree Go2 Web Interface - Enhanced with Joystick Control")
print("=" * 60)
print(f"Default Robot IP: {ROBOT_IP}")
print("\n⚠️ IMPORTANT: Close the Unitree Go app before connecting!")
print("\nFeatures:")
print(" • Virtual joystick controls for movement")
print(" • Movement sequence programming")
print(" • Real-time video streaming")
print(" • Basic command buttons")
print("\nStarting web server...")
print("Open your browser and go to: http://localhost:5000")
print("\nPress Ctrl+C to stop the server")
print("=" * 60)
app.run(host='0.0.0.0', port=5000, debug=False, threaded=True)