feat: use redis to communicate movement
This commit is contained in:
26
worker.py
26
worker.py
@@ -1,9 +1,10 @@
|
||||
import sched
|
||||
from threading import Thread
|
||||
import json
|
||||
|
||||
from redis import Redis
|
||||
|
||||
# from control.walle import WallE
|
||||
from control.walle import WallE
|
||||
from control.camera import Camera
|
||||
|
||||
INTERVAL_TICK = 0.05 # 20/s
|
||||
@@ -21,16 +22,33 @@ class EventListener(Thread):
|
||||
for item in self.pubsub.listen():
|
||||
channel = item['channel'].decode()
|
||||
data = item['data']
|
||||
print(channel)
|
||||
print(data)
|
||||
if not isinstance(data, bytes):
|
||||
print(f"Channel {channel} got message that is not bytes {data}")
|
||||
continue
|
||||
try:
|
||||
self.handle_message(channel, data)
|
||||
except ValueError:
|
||||
pass # todo
|
||||
|
||||
def handle_message(self, channel: str, data: bytes):
|
||||
data_dict = json.loads(data.decode())
|
||||
if channel == 'move':
|
||||
walle.set_movement(data_dict['angle'], data_dict['force'])
|
||||
elif channel == 'look':
|
||||
walle.set_eye_velocity(data_dict['angle'], data_dict['force'])
|
||||
else:
|
||||
print(f'Unknown channel {channel}')
|
||||
|
||||
|
||||
scheduler = sched.scheduler()
|
||||
camera = Camera()
|
||||
walle = WallE()
|
||||
walle.setup()
|
||||
|
||||
|
||||
def walle_tick():
|
||||
pass
|
||||
scheduler.enter(INTERVAL_TICK, 1, camera_image)
|
||||
walle.tick()
|
||||
|
||||
|
||||
def camera_image():
|
||||
|
||||
Reference in New Issue
Block a user