Skip to content

Commit

Permalink
feat - Desactivate API for the moment
Browse files Browse the repository at this point in the history
Add camera

WIP - The API remove the change mode due to asyncio
We need to use a thread
  • Loading branch information
EtienneSchmitz committed Sep 4, 2023
1 parent 222059d commit 004429d
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 18 deletions.
5 changes: 3 additions & 2 deletions rpi/manager/thymio/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@
class ThymioManager(BaseManager):
current_mode = 0
last_mode_change_time = 0
mode_change_delay = 0.5
mode_change_delay = 0.2

def __init__(self):
self.controller = ThymioController()
self.logger = logging.getLogger(__name__)

self.tasks = [
# API(self.controller),
ObjectCollector(self.controller),
FollowLine(self.controller),
API(self.controller)
]

self.num_modes = len(self.tasks)

def change_mode(self):
print("test")
current_time = time.time()

if current_time - self.last_mode_change_time < self.mode_change_delay:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def run(self):
self.loop = asyncio.get_event_loop()
self.loop.run_until_complete(server)
self.loop.create_task(self.send_to_all_clients())
self.loop.run_forever()

def close(self):
self.loop.close()
66 changes: 62 additions & 4 deletions rpi/tasks/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,64 @@

from controller.base import BaseController
from tasks.base import Task
from websocket.input_output import InputOuputServer
from sockets.input_output import InputOuputServer

import asyncio
import websockets

from PIL import Image
from io import BytesIO
from vision.camera import Camera

class CameraServer:
def __init__(self):
self.connected = set()
self.logger = logging.getLogger(__name__)
self.camera = Camera()

async def send_to_all_clients(self):
while True:
await asyncio.sleep(1/20.0)

if len(self.connected) == 0:
continue

img = self.camera.grab_frame_loop()
if img is None:
continue

with BytesIO() as bytes:
pil_img = Image.fromarray(img)
pil_img.save(bytes, 'jpeg')
message = bytes.getvalue()

for websocket in list(self.connected):
try:
await websocket.send(message)
except websockets.ConnectionClosed:
self.logger.warning("Client disconnected. Removing from list.")
self.connected.remove(websocket)

async def handler(self, websocket, path):
self.logger.debug("New connection added.")
self.connected.add(websocket)

try:
async for message in websocket:
self.logger.debug(f"Receive commands :", message)
except (websockets.ConnectionClosed, Exception) as e:
self.logger.warning(f"Connection closed due to error: {e}")
finally:
self.connected.remove(websocket)

def run(self):
server_coro = websockets.serve(self.handler, '0.0.0.0', 5678)

self.loop = asyncio.get_event_loop()

self.loop.run_until_complete(server_coro)
self.loop.create_task(self.send_to_all_clients())


class API(Task):
def __init__(self, controller: BaseController):
Expand All @@ -12,9 +69,10 @@ def __init__(self, controller: BaseController):
self.input_output = InputOuputServer(self.controller)
self.input_output.run()


def initialize(self):
pass
self.camera_server = CameraServer()
self.camera_server.run()

asyncio.get_event_loop().run_forever()

def run(self):
self.logger.info("API")
Expand Down
4 changes: 0 additions & 4 deletions rpi/tasks/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from abc import ABC, abstractmethod

class Task(ABC):
@abstractmethod
def initialize(self):
pass

@abstractmethod
def run(self):
pass
Expand Down
4 changes: 0 additions & 4 deletions rpi/tasks/thymio/follow_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ def __init__(self, controller: ThymioController):
self.controller = controller
self.logger = logging.getLogger(__name__)


def initialize(self):
pass

def run(self):
self.logger.info("FOLLOW LINE")

Expand Down
3 changes: 0 additions & 3 deletions rpi/tasks/thymio/object_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ def __init__(self, controller: ThymioController):
self.logger = logging.getLogger(__name__)
self.cam = Camera()

def initialize(self):
pass

def run(self):
self.logger.info("OBJECT COLLECTOR")
img = self.cam.grab_frame_loop()
Expand Down

0 comments on commit 004429d

Please sign in to comment.