-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrobot.py
More file actions
193 lines (148 loc) · 5.82 KB
/
robot.py
File metadata and controls
193 lines (148 loc) · 5.82 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
import asyncio
import json
import websockets
import sys
import zmq
import cv2
from PIL import Image
import numpy as np
from av import VideoFrame
import pyautogui as pyg
from aiortc import *
robot_connection = None
control_data_channel = None
websocket = None
control_signals = {"w": "w",
"a": "a",
"s": "s",
"d": "d",
"ArrowUp": "up",
"ArrowLeft": "left",
"ArrowDown": "down",
"ArrowRight": "right"}
# pyg.PAUSE = 0
# button_pos = pyg.locateCenterOnScreen("back_button.jpg", confidence=0.9)
# pyg.moveTo(button_pos[0], button_pos[1])
# pyg.click()
async def connect_to_signalling_server(uri, login_message):
global websocket
websocket = await websockets.connect(uri)
print("Connected to server")
await websocket.send(json.dumps(login_message))
async def recv_message_handler():
global websocket
await asyncio.sleep(2)
async for message in websocket:
data = json.loads(message)
if data["type"] == "login":
await login_handler()
elif data["type"] == "offer":
await offer_handler(data["offer"], data["name"])
elif data["type"] == "leave":
await leave_handler(data["name"])
else:
print("Unknown message received from signalling server")
async def login_handler():
global robot_connection
global control_data_channel
global control_signals
config = RTCConfiguration([\
RTCIceServer("turn:18.142.123.26:3478", username="RaghavB", credential="RMTurnServer"),\
RTCIceServer("stun:stun.1.google.com:19302")])
robot_connection = RTCPeerConnection(configuration=config)
@robot_connection.on("datachannel")
def on_datachannel(channel):
@channel.on("message")
def on_message(message):
controls = json.loads(message)
print(controls)
# for key in control_signals:
# if key in controls:
# pyg.keyDown(control_signals[key])
# else:
# pyg.keyUp(control_signals[key])
# if " " in controls:
# pyg.mouseDown()
# else:
# pyg.mouseUp()
control_data_channel = robot_connection.createDataChannel("control_data_channel")
robot_connection.addTrack(S1AppTrack())
print("RTCPeerConnection object is created")
async def offer_handler(offer, name):
global websocket
global robot_connection
await robot_connection.setRemoteDescription(RTCSessionDescription(offer, "offer"))
answer = await robot_connection.createAnswer()
await robot_connection.setLocalDescription(answer)
message = json.dumps({"answer": \
{"sdp": robot_connection.localDescription.sdp, \
"type": robot_connection.localDescription.type}, \
"name": name,
"type": robot_connection.localDescription.type})
await websocket.send(message)
print("Answer sent to " + name)
async def leave_handler(name):
global robot_connection
global control_data_channel
global control_signals
# Reset keypresses
for key in control_signals:
pyg.keyUp(control_signals[key])
pyg.keyUp(" ")
print("Closing peer connection to " + str(name))
# Close peer connection
control_data_channel.close()
await robot_connection.close()
await login_handler()
class S1AppTrack(VideoStreamTrack):
def __init__(self):
super().__init__()
self.sub_context = zmq.Context()
self.sub = self.sub_context.socket(zmq.SUB)
self.sub.setsockopt(zmq.CONFLATE, 1)
self.sub.setsockopt_string(zmq.SUBSCRIBE, "")
self.is_init = True
self.cam = cv2.VideoCapture(0)
async def recv(self):
if self.is_init:
self.sub.connect("tcp://127.0.0.1:12345")
self.is_init = False
pts, time_base = await self.next_timestamp()
# raw_bytes = self.sub.recv()
# byte_arr = np.frombuffer(raw_bytes, dtype=np.uint8)
# cv_frame = np.reshape(byte_arr, (720, 1280, 3))
# cv_frame = np.zeros((720, 1280, 3), np.uint8)
x, cv_frame = self.cam.read()
# Draw crosshair
center_point = (int(cv_frame.shape[1]/2), int(cv_frame.shape[0]/2))
cv2.circle(cv_frame, center_point, 3, (255,255,255), thickness=-1)
cv2.line(cv_frame, (center_point[0],center_point[1]-20), (center_point[0],center_point[1]-40),
color=(255,255,255), thickness=2) # Up
cv2.line(cv_frame, (center_point[0],center_point[1]+20), (center_point[0],center_point[1]+40),
color=(255,255,255), thickness=2) # Down
cv2.line(cv_frame, (center_point[0]-20,center_point[1]), (center_point[0]-40,center_point[1]),
color=(255,255,255), thickness=2) # Left
cv2.line(cv_frame, (center_point[0]+20,center_point[1]), (center_point[0]+40,center_point[1]),
color=(255,255,255), thickness=2) # Right
cv_frame = cv2.resize(cv_frame, (int(cv_frame.shape[1] / 2), int(cv_frame.shape[0] / 2)))
frame = VideoFrame.from_ndarray(cv_frame, format="rgb24")
frame.pts = pts
frame.time_base = time_base
return frame
async def main():
signalling_server_uri = "ws://18.142.123.26:49621"
if len(sys.argv) == 3:
signalling_server_uri = "ws://localhost:49621"
robot_id = sys.argv[1]
await asyncio.gather(
connect_to_signalling_server(signalling_server_uri,
{"type": "robot-login",
"name": robot_id,
"joinedGame": "battle"}),
recv_message_handler())
if __name__ == "__main__":
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
except KeyboardInterrupt:
print("Exiting")