Skip to content
Snippets Groups Projects
Commit 72405a18 authored by gawen.ackerman's avatar gawen.ackerman :robot:
Browse files

ADD : Comments for UDPSender.py

parent 70a76f32
Branches
No related tags found
No related merge requests found
import socket
import tkinter as tk
# Size of the main window and canvas
WINDOW_SIZE = 400
# Center coordinate of the canvas
CENTER_CANVAS = WINDOW_SIZE // 2
# Minimum bound for scaled values
MIN_BOUND = 15
# Maximum bound for scaled values
MAX_BOUND = 65
# UDP socket configuration
UDP_IP = "127.0.0.1"
UDP_PORT = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Refresh rate in milliseconds for sending data
REFRESH_RATE = 100
# Mouse position
mouse_x = 0
mouse_y = 0
# Y-axis rotation (theta)
y_theta = 0
# Tracking state
is_tracking = False
def aggregate_values_to_string(x, y, z, x_theta, y_theta, z_theta):
"""Create a comma-separated string from position and rotation values.
Args:
x (float): X position.
y (float): Y position.
z (float): Z position.
x_theta (float): X-axis rotation.
y_theta (float): Y-axis rotation.
z_theta (float): Z-axis rotation.
Returns:
str: Formatted string of all values.
"""
return f"{x},{y},{z},{x_theta},{y_theta},{z_theta}"
def update_position():
"""Update position and rotation data and send it via UDP.
No parameters.
"""
global mouse_x, mouse_y, y_theta, is_tracking
# Compute relative positions and rotation
x = scale_value(mouse_x - CENTER_CANVAS)
y = scale_value(CENTER_CANVAS - mouse_y)
z = 0
x_theta = 0
z_theta = 0
# If tracking is off, reset position values
if is_tracking != True:
x = scale_value(0)
y = scale_value(0)
z = 0
# Format data and send via UDP
data = aggregate_values_to_string(x, y, z, x_theta, y_theta, z_theta)
sock.sendto(data.encode(), (UDP_IP, UDP_PORT))
print("Sent:", data)
# Schedule next update
root.after(REFRESH_RATE, update_position)
......@@ -51,18 +82,40 @@ def scale_value(
min_target=MIN_BOUND,
max_target=MAX_BOUND,
):
"""Scale a value from source range to target range.
Args:
value (float): The value to scale.
min_source (float, optional): Minimum of source range. Defaults to -CENTER_CANVAS.
max_source (float, optional): Maximum of source range. Defaults to CENTER_CANVAS.
min_target (float, optional): Minimum of target range. Defaults to MIN_BOUND.
max_target (float, optional): Maximum of target range. Defaults to MAX_BOUND.
Returns:
float: Scaled value.
"""
return min_target + (value - min_source) * (max_target - min_target) / (
max_source - min_source
)
def track_mouse(event):
"""Update mouse_x and mouse_y with current mouse position.
Args:
event (tk.Event): The motion event containing x and y coordinates.
"""
global mouse_x, mouse_y
mouse_x = event.x
mouse_y = event.y
def adjust_rotation(event):
"""Adjust the y-axis rotation based on scroll direction.
Args:
event (tk.Event): The mouse wheel event with delta value.
"""
global y_theta
if event.delta > 0:
y_theta += 5
......@@ -72,6 +125,11 @@ def adjust_rotation(event):
def draw_center_and_arrows(canvas):
"""Draw the center circle and directional arrows on the canvas.
Args:
canvas (tk.Canvas): The canvas where shapes are drawn.
"""
CIRCLE_RADIUS = 20
canvas.create_oval(
CENTER_CANVAS - CIRCLE_RADIUS,
......@@ -83,6 +141,8 @@ def draw_center_and_arrows(canvas):
ARROW_MARGIN = 30
ARROW_LENGTH = 150
# Up arrow
canvas.create_line(
CENTER_CANVAS,
CENTER_CANVAS - ARROW_MARGIN,
......@@ -91,6 +151,7 @@ def draw_center_and_arrows(canvas):
arrow=tk.LAST,
width=2,
)
# Right arrow
canvas.create_line(
CENTER_CANVAS + ARROW_MARGIN,
CENTER_CANVAS,
......@@ -99,6 +160,7 @@ def draw_center_and_arrows(canvas):
arrow=tk.LAST,
width=2,
)
# Down arrow
canvas.create_line(
CENTER_CANVAS,
CENTER_CANVAS + ARROW_MARGIN,
......@@ -107,6 +169,7 @@ def draw_center_and_arrows(canvas):
arrow=tk.LAST,
width=2,
)
# Left arrow
canvas.create_line(
CENTER_CANVAS - ARROW_MARGIN,
CENTER_CANVAS,
......@@ -118,6 +181,11 @@ def draw_center_and_arrows(canvas):
def add_scroll_info_label(canvas):
"""Add a label with scroll instructions and redirect events.
Args:
canvas (tk.Canvas): The canvas to attach the label and event redirection.
"""
info_text = "Scroll ↑ : rotate right\nScroll ↓ : rotate left"
label_info = tk.Label(
canvas, text=info_text, bg="white", fg="black", justify="right"
......@@ -125,22 +193,41 @@ def add_scroll_info_label(canvas):
label_info.place(relx=1.0, x=-10, y=10, anchor="ne")
def redirect_scroll(event):
"""Redirect scroll event to the canvas.
Args:
event (tk.Event): The scroll event.
"""
canvas.event_generate("<MouseWheel>", x=event.x, y=event.y, delta=event.delta)
def redirect_motion(event):
"""Redirect motion event to the canvas.
Args:
event (tk.Event): The motion event.
"""
canvas.event_generate("<Motion>", x=event.x, y=event.y)
label_info.bind("<MouseWheel>", redirect_scroll)
label_info.bind("<Motion>", redirect_motion)
def disable_tracking(event):
"""Disable tracking when mouse leaves the canvas.
Args:
event (tk.Event): The leave event.
"""
global is_tracking
is_tracking = False
def enable_tracking(event):
"""Enable tracking when mouse enters the canvas.
Args:
event (tk.Event): The enter event.
"""
global is_tracking
is_tracking = True
......@@ -152,19 +239,24 @@ def main():
root.geometry(f"{WINDOW_SIZE}x{WINDOW_SIZE}")
root.resizable(False, False)
# Create and pack the canvas
canvas = tk.Canvas(root, width=WINDOW_SIZE, height=WINDOW_SIZE, bg="white")
canvas.pack()
# Draw visuals and label
draw_center_and_arrows(canvas)
add_scroll_info_label(canvas)
# Bind interaction events
canvas.bind("<Motion>", track_mouse)
canvas.bind("<MouseWheel>", adjust_rotation)
canvas.bind("<Enter>", enable_tracking)
canvas.bind("<Leave>", disable_tracking)
# Start update loop
update_position()
# Start the Tkinter event loop
root.mainloop()
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment