-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsClient.py
More file actions
104 lines (90 loc) · 2.82 KB
/
wsClient.py
File metadata and controls
104 lines (90 loc) · 2.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
from websockets.sync.client import connect
import json
from time import sleep
import signal
import sys
import websockets
import asyncio
def main():
try:
#run()
asyncio.run(runAsServer())
except ConnectionRefusedError as e:
sleep(1)
except Exception as e:
print(e)
sleep(1)
def run():
with connect("ws://localhost:1302") as websocket:
signal.signal(signal.SIGINT, lambda sig, frame: signal_handler(sig, frame, websocket))
#publisher(websocket)
#subscriber(websocket)
publisherAngPos(websocket)
async def runAsServer():
async with websockets.serve(subscriberAsync, "", 1302, ping_interval=None):
await asyncio.Future() # run forever
def publisher(websocket):
sleepTime = 0.05
sleepTime = 1
while True:
for i in range(-90, 91, 1):
sendPitch(websocket, i/10)
sleep(sleepTime)
for i in range(90, -91, -1):
sendPitch(websocket, i/10)
sleep(sleepTime)
def sendPitch(websocket, pitch):
msg = {}
msg["pitch"] = pitch
msg["roll"] = 20
message = json.dumps(msg)
websocket.send(message)
#message = websocket.recv()
def publisherAngPos(websocket):
sleepTime = 1
count = 0
step = 10
while True:
roll = (count*step - 120) % 360
if roll > 180:
roll -= 360
pitch = (count*step) % 360
if pitch > 180:
pitch -= 360
yaw = (count*step + 120) % 360
if yaw > 180:
yaw -= 360
sendAngData(websocket, roll, pitch, yaw, count)
sendAngData(websocket, yaw, roll, pitch, count, command="angve")
websocket.send(f"{count}, armpo, {count%2}")
websocket.send(f"{count}, batvo, {count%10}")
websocket.send(f"{count}, elcur, {count%10}")
websocket.send(f"{count}, magst, {count%2}")
count += 1
sleep(sleepTime)
def sendAngData(websocket, roll, pitch, yaw, msgNum, command="angpo"):
msg = f"{msgNum}, {command}, {roll}, {pitch}, {yaw}"
websocket.send(msg)
def subscriber(websocket):
while True:
message = websocket.recv()
print(message)
async def subscriberAsync(websocket : websockets.WebSocketServerProtocol, path):
asyncio.create_task(publisherAsync(websocket, path))
while True:
#message = await websocket.recv()
#print(message)
await asyncio.sleep(1)
async def publisherAsync(websocket : websockets.WebSocketServerProtocol, path):
print("Enter messages: ")
while True:
msg = input()
await websocket.send(msg)
def signal_handler(sig, frame, websocket):
print('Stopping...')
#websocket.close()
exit()
if __name__ == "__main__":
print("Starting...")
while True:
main()