Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
IGesture
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
gawen.ackerman
IGesture
Commits
72405a18
Commit
72405a18
authored
1 month ago
by
gawen.ackerman
Browse files
Options
Downloads
Patches
Plain Diff
ADD : Comments for UDPSender.py
parent
70a76f32
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
UDP_App/UDPsender.py
+93
-1
93 additions, 1 deletion
UDP_App/UDPsender.py
impulse/Assets/Models/Ocean Plane.asset
+3
-3
3 additions, 3 deletions
impulse/Assets/Models/Ocean Plane.asset
with
96 additions
and
4 deletions
UDP_App/UDPsender.py
+
93
−
1
View file @
72405a18
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
\n
Scroll ↓ : 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.
Click to expand it.
impulse/Assets/Models/Ocean Plane.asset
+
3
−
3
View file @
72405a18
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment