-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduinosilly.py
81 lines (63 loc) · 2.46 KB
/
arduinosilly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import pygame
import math
import serial
import time
# Set up serial communication (adjust COM port as needed)
ser = serial.Serial('COM7', 9600)
time.sleep(2)
# Initialize pygame
pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Radar Visualization")
# Colors
BLACK = (0, 0, 0)
GREEN = (98, 245, 31)
RED = (255, 10, 10)
# Radar center and radius
center_x, center_y = width // 2, height - 50
radius = 250
def draw_radar():
screen.fill(BLACK)
pygame.draw.circle(screen, GREEN, (center_x, center_y), radius, 1)
pygame.draw.circle(screen, GREEN, (center_x, center_y), radius // 2, 1)
pygame.draw.line(screen, GREEN, (center_x, center_y), (center_x - radius, center_y), 1)
pygame.draw.line(screen, GREEN, (center_x, center_y), (center_x + radius, center_y), 1)
pygame.draw.line(screen, GREEN, (center_x, center_y), (center_x, center_y - radius), 1)
def draw_object(angle, distance):
"""Draws the detected object based on angle and distance."""
angle_rad = math.radians(angle)
dist_scaled = distance * radius / 40 # Scale distance to radar range
# Calculate object's position
obj_x = center_x + dist_scaled * math.cos(angle_rad)
obj_y = center_y - dist_scaled * math.sin(angle_rad)
# Draw line and circle for detected object
pygame.draw.line(screen, RED, (center_x, center_y), (obj_x, obj_y), 5)
pygame.draw.circle(screen, RED, (int(obj_x), int(obj_y)), 3)
def main():
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
draw_radar()
if ser.in_waiting > 0:
try:
# Read and decode serial data
data = ser.readline().decode().strip()
# Split data by commas and parse
angle_str, distance_str = data.split(",")
angle = int(angle_str)
distance = int(distance_str.split(".")[0]) # Remove trailing period if any
# Debug output for parsed values
print(f"Angle: {angle}, Distance: {distance}")
# Draw object based on angle and distance
draw_object(angle, distance)
except ValueError as e:
print(f"Error parsing data: {e}")
pygame.display.flip()
pygame.time.delay(30)
pygame.quit()
ser.close()
if __name__ == "__main__":
main()