diff --git a/Client/4.py b/Client/4.py new file mode 100644 index 0000000..6faa614 --- /dev/null +++ b/Client/4.py @@ -0,0 +1,15 @@ +import asyncio +import websockets + +socket_connections = {} + + +async def main(): + async with websockets.connect("ws://10.0.0.29:8765") as websocket: + while 1: + await websocket.send("Hellow world") + print("sent") + + await asyncio.sleep(0.5) + +asyncio.run(main()) \ No newline at end of file diff --git a/Client/joystick_control sockets copy.py b/Client/joystick_control sockets copy.py new file mode 100644 index 0000000..1b12c12 --- /dev/null +++ b/Client/joystick_control sockets copy.py @@ -0,0 +1,30 @@ +import asyncio +import websockets + +socket_connections = {} + +async def handle_client(websocket, path): + global socket_connections + + print("packet connected") + socket_connections[websocket] = "" + + try: + while True: + print("waiting for data") + data = await websocket.recv() + # await update_from_packet(data) + except websockets.exceptions.ConnectionClosed: + print("Connection closed with: ", websocket.remote_address) + finally: + print("closing websocket") + del socket_connections[websocket] + +async def main(): + server = await websockets.serve(handle_client, "localhost", 8765) + + print("server started") + + await server.wait_closed() + +asyncio.run(main()) \ No newline at end of file diff --git a/Client/joystick_control sockets.py b/Client/joystick_control sockets.py new file mode 100644 index 0000000..e568530 --- /dev/null +++ b/Client/joystick_control sockets.py @@ -0,0 +1,180 @@ +import pygame +import sys +import time +import asyncio +import websockets + +# Initialize Pygame +pygame.init() + +# Set up the screen +screen_width = 600 +screen_height = 400 +screen = pygame.display.set_mode((screen_width, screen_height)) +pygame.display.set_caption("Joystick Control") + +# Set up colors +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) +RED = (255, 0, 0) +GREEN = (0, 255, 0) +GRAY = (150, 150, 150) + +async def websocket_thread(websocket, axis_x, axis_y): + waiting = await websocket.ping() + latency = await waiting # This extra await gives async the time to send the websocket stuff I think + # It's a bit hacky, but it works + # print(f"latency: {latency}") + if axis_y >= 0: + await websocket.send(f"U{axis_y}") + print("packet?") + elif axis_y < 0: + await websocket.send(f"D{-axis_y}") + print("packet?") + if axis_x >= 0: + await websocket.send(f"L{axis_x}") + print("packet?") + elif axis_x < 0: + await websocket.send(f"R{-axis_x}") + print("packet?") + + +# Main loop +async def main(): + running = True + # Get joystick + joystick_count = pygame.joystick.get_count() + if joystick_count == 0: + print("No joystick detected") + pygame.quit() + sys.exit() + joystick = pygame.joystick.Joystick(0) + joystick.init() + + # Set up font + font = pygame.font.SysFont(None, 36) + + # Set up clock + clock = pygame.time.Clock() + + # Initial request status and toggle status + request_status = False + request_text = "Not Enabled" + toggle_status = False # False for disabled, True for enabled + + # IP and port for the request + ip_address = "10.0.0.29" + port = "5000" + + # Flag to track focus on IP and Port entry + ip_focused = False + port_focused = False + async with websockets.connect("ws://10.0.0.29:8765") as websocket: + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + elif event.type == pygame.MOUSEBUTTONDOWN: + # Check if mouse click is inside IP entry box + if ip_rect.collidepoint(event.pos): + ip_focused = True + port_focused = False + # Check if mouse click is inside Port entry box + elif port_rect.collidepoint(event.pos): + port_focused = True + ip_focused = False + # Check if mouse click is inside toggle button + elif toggle_rect.collidepoint(event.pos): + toggle_status = not toggle_status + request_status = False # Reset request status when toggling + else: + ip_focused = False + port_focused = False + elif event.type == pygame.KEYDOWN: + # Capture user input for IP and Port + if ip_focused: + if event.key == pygame.K_BACKSPACE: + ip_address = ip_address[:-1] + if event.key == pygame.K_TAB: + port_focused = True + ip_focused = False + elif event.unicode.isdigit() or event.unicode == '.': + ip_address += event.unicode + elif port_focused: + if event.key == pygame.K_BACKSPACE: + port = port[:-1] + if event.key == pygame.K_TAB: + port_focused = False + ip_focused = True + elif event.unicode.isdigit(): + port += event.unicode + + # Clear the screen + screen.fill(BLACK) + + axis_x = int(joystick.get_axis(0) * 100) + axis_y = int(joystick.get_axis(1) * 100) + + if axis_x > -10 and axis_x < 10: + axis_x = 0 + if axis_y > -10 and axis_y < 10: + axis_y = 0 + + # Draw IP entry box + ip_text = font.render("IP:", True, WHITE) + screen.blit(ip_text, (10, 25)) + ip_rect = pygame.Rect(70, 20, 150, 36) + pygame.draw.rect(screen, GRAY if ip_focused else WHITE, ip_rect, 2) + ip_text_surface = font.render(ip_address, True, WHITE) + screen.blit(ip_text_surface, (ip_rect.x + 5, ip_rect.y + 5)) + + # Draw Port entry box + port_text = font.render("Port:", True, WHITE) + screen.blit(port_text, (10, 75)) + port_rect = pygame.Rect(70, 70, 150, 36) + pygame.draw.rect(screen, GRAY if port_focused else WHITE, port_rect, 2) + port_text_surface = font.render(port, True, WHITE) + screen.blit(port_text_surface, (port_rect.x + 5, port_rect.y + 5)) + + # Draw toggle button + toggle_text = font.render("Enabled" if toggle_status else "Disabled", True, WHITE) + toggle_rect = pygame.Rect(10, 130, 150, 36) + pygame.draw.rect(screen, GREEN if toggle_status else RED, toggle_rect) + pygame.draw.rect(screen, WHITE, toggle_rect, 2) + screen.blit(toggle_text, (toggle_rect.x + 10, toggle_rect.y + 5)) + + # Draw request status + status_text = font.render("Status:", True, WHITE) + screen.blit(status_text, (320, 10)) + if request_status: + pygame.draw.circle(screen, GREEN, (455, 23), 12) + else: + pygame.draw.circle(screen, RED, (455, 23), 12) + + if request_text != "": + request_error_text = font.render(request_text, True, WHITE) + screen.blit(request_error_text, (350, 55)) + + # Draw the ball speed + speed_text = font.render(f"X Speed: {axis_x}%, Y Speed: {axis_y}%", True, WHITE) + screen.blit(speed_text, (10, 300)) + + + # Update the display + pygame.display.flip() + + # Send data request every 200 ms if toggle is enabled + if toggle_status and time.time() % 0.01 < 0.01: + if ip_address and port: + await websocket_thread(websocket, axis_x, axis_y) + else: + request_text = "IP and Port empty" + asyncio.wait(0.01) + + # Cap the frame rate + clock.tick(60) + + # Quit Pygame + pygame.quit() + +asyncio.run(main()) diff --git a/Client/joystick_control.py b/Client/joystick_control.py new file mode 100644 index 0000000..263f570 --- /dev/null +++ b/Client/joystick_control.py @@ -0,0 +1,189 @@ +import pygame +import sys +import requests +import time + +# Initialize Pygame +pygame.init() + +# Set up the screen +screen_width = 600 +screen_height = 400 +screen = pygame.display.set_mode((screen_width, screen_height)) +pygame.display.set_caption("Joystick Control") + +# Set up colors +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) +RED = (255, 0, 0) +GREEN = (0, 255, 0) +GRAY = (150, 150, 150) + +# Get joystick +joystick_count = pygame.joystick.get_count() +if joystick_count == 0: + print("No joystick detected") + pygame.quit() + sys.exit() +joystick = pygame.joystick.Joystick(0) +joystick.init() + +# Set up font +font = pygame.font.SysFont(None, 36) + +# Set up clock +clock = pygame.time.Clock() + +# Initial request status and toggle status +request_status = False +request_text = "Not Enabled" +toggle_status = False # False for disabled, True for enabled + +# IP and port for the request +ip_address = "10.0.0.29" +port = "5000" + +# Flag to track focus on IP and Port entry +ip_focused = False +port_focused = False + +# Main loop +running = True +while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + elif event.type == pygame.MOUSEBUTTONDOWN: + # Check if mouse click is inside IP entry box + if ip_rect.collidepoint(event.pos): + ip_focused = True + port_focused = False + # Check if mouse click is inside Port entry box + elif port_rect.collidepoint(event.pos): + port_focused = True + ip_focused = False + # Check if mouse click is inside toggle button + elif toggle_rect.collidepoint(event.pos): + toggle_status = not toggle_status + request_status = False # Reset request status when toggling + else: + ip_focused = False + port_focused = False + elif event.type == pygame.KEYDOWN: + # Capture user input for IP and Port + if ip_focused: + if event.key == pygame.K_BACKSPACE: + ip_address = ip_address[:-1] + if event.key == pygame.K_TAB: + port_focused = True + ip_focused = False + elif event.unicode.isdigit() or event.unicode == '.': + ip_address += event.unicode + elif port_focused: + if event.key == pygame.K_BACKSPACE: + port = port[:-1] + if event.key == pygame.K_TAB: + port_focused = False + ip_focused = True + elif event.unicode.isdigit(): + port += event.unicode + + # Clear the screen + screen.fill(BLACK) + + axis_x = int(joystick.get_axis(0) * 100) + axis_y = int(joystick.get_axis(1) * 100) + + if axis_x > -10 and axis_x < 10: + axis_x = 0 + if axis_y > -10 and axis_y < 10: + axis_y = 0 + + # Draw IP entry box + ip_text = font.render("IP:", True, WHITE) + screen.blit(ip_text, (10, 25)) + ip_rect = pygame.Rect(70, 20, 150, 36) + pygame.draw.rect(screen, GRAY if ip_focused else WHITE, ip_rect, 2) + ip_text_surface = font.render(ip_address, True, WHITE) + screen.blit(ip_text_surface, (ip_rect.x + 5, ip_rect.y + 5)) + + # Draw Port entry box + port_text = font.render("Port:", True, WHITE) + screen.blit(port_text, (10, 75)) + port_rect = pygame.Rect(70, 70, 150, 36) + pygame.draw.rect(screen, GRAY if port_focused else WHITE, port_rect, 2) + port_text_surface = font.render(port, True, WHITE) + screen.blit(port_text_surface, (port_rect.x + 5, port_rect.y + 5)) + + # Draw toggle button + toggle_text = font.render("Enabled" if toggle_status else "Disabled", True, WHITE) + toggle_rect = pygame.Rect(10, 130, 150, 36) + pygame.draw.rect(screen, GREEN if toggle_status else RED, toggle_rect) + pygame.draw.rect(screen, WHITE, toggle_rect, 2) + screen.blit(toggle_text, (toggle_rect.x + 10, toggle_rect.y + 5)) + + # Draw request status + status_text = font.render("Status:", True, WHITE) + screen.blit(status_text, (320, 10)) + if request_status: + pygame.draw.circle(screen, GREEN, (455, 23), 12) + else: + pygame.draw.circle(screen, RED, (455, 23), 12) + + if request_text != "": + request_error_text = font.render(request_text, True, WHITE) + screen.blit(request_error_text, (350, 55)) + + # Draw the ball speed + speed_text = font.render(f"X Speed: {axis_x}%, Y Speed: {axis_y}%", True, WHITE) + screen.blit(speed_text, (10, 300)) + + + # Update the display + pygame.display.flip() + + # Send data request every 200 ms if toggle is enabled + if toggle_status and time.time() % 0.15 < 0.01: # approximate 150 ms interval + if ip_address and port: + data_packet = { } + if axis_y > 0: + data_packet["up"] = axis_y + data_packet["down"] = 0 + elif axis_y < 0: + data_packet["down"] = -axis_y + data_packet["up"] = 0 + if axis_x > 0: + data_packet["right"] = axis_x + data_packet["left"] = 0 + elif axis_x < 0: + data_packet["left"] = -axis_x + data_packet["right"] = 0 + if axis_x == 0 and axis_y == 0: + data_packet["right"] = 0 + data_packet["left"] = 0 + data_packet["up"] = 0 + data_packet["down"] = 0 + + url = f"http://{ip_address}:{port}/move_camera" + try: + response = requests.post(url, json=data_packet, timeout=0.2) + print(f"sent: {data_packet}") + if response.status_code == 200: + request_status = True + else: + print(response.data) + request_status = False + except requests.Timeout: + request_status = False + request_text = "IP Not Found" + except requests.ConnectionError: + request_status = False + request_text = "" + else: + request_text = "IP and Port empty" + + # Cap the frame rate + clock.tick(60) + +# Quit Pygame +pygame.quit() diff --git a/Client/local_socket.py b/Client/local_socket.py new file mode 100644 index 0000000..3dda14d --- /dev/null +++ b/Client/local_socket.py @@ -0,0 +1,35 @@ +import time +import sys +import websockets +import asyncio + +socket_connections = {} + +async def handle_client(websocket, path): + global socket_connections + + print("packet connected") + socket_connections[websocket] = "" + + try: + while True: + data = await websocket.recv() + for command in data.split(":"): + print(f"data is: {command}") + except websockets.exceptions.ConnectionClosed: + print("Connection closed with: ", websocket.remote_address) + finally: + print("closing websocket") + del socket_connections[websocket] + +axis_x = 0 +axis_y = 0 + +async def main(): + server = await websockets.serve(handle_client, "0.0.0.0", 8765) + + print("server started") + + await server.wait_closed() + +asyncio.run(main()) \ No newline at end of file