-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_grid2.py
More file actions
144 lines (110 loc) · 4.89 KB
/
simple_grid2.py
File metadata and controls
144 lines (110 loc) · 4.89 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Arrow
# Parameters
grid_size = 200
plot_points = np.zeros((grid_size, grid_size))
# Setting initial points near the center (considering 0,0 as the center)
center = grid_size // 2
plot_points[center - 35, center - 10:center + 10] = 1
plot_points[center - 25, center - 10:center + 10] = 1
plot_points[center - 34:center - 25, center - 10] = 1
plot_points[center - 34:center - 25, center + 10] = 1
# Triangle
plot_points[center + 20, center + 10:center + 15] = 1
plot_points[center + 21, center + 11:center + 14] = 1
plot_points[center + 22, center + 12:center + 13] = 1
arrow_angle = 90 # Initial arrow angle
# Function definitions
def draw_grid(points, arrow_angle):
"""Draw the grid with the points and the arrow."""
# Set the figure size
#plt.figure(figsize=(8, 8))
# Display the grid
plt.imshow(points, cmap='Reds', extent=(-grid_size / 2, grid_size / 2, -grid_size / 2, grid_size / 2))
plt.grid(which='both')
plt.xticks(range(-grid_size // 2, grid_size // 2 + 1, 5))
plt.yticks(range(-grid_size // 2, grid_size // 2 + 1, 5))
# Find the points to plot
y_coords, x_coords = np.where(points == 1)
# Scatter plot the points in red and with a larger size
plt.scatter(x_coords - center, center - y_coords, color='red', s=1)
# Draw the arrow
#dx = 5 * np.sin(np.radians(arrow_angle))
#dy = 5 * np.cos(np.radians(arrow_angle))
dy = 5
dx = 0
plt.gca().add_patch(Arrow(0, 0, dx, dy, width=3, color='blue'))
# Refresh the plot
plt.draw()
import numpy as np
def rotate_points(angle, sensor_offset_front=5, wheelbase=7.9):
"""Rotate points around the center of rotation considering the sensor's front offset."""
global plot_points, center
# The center of rotation is halfway between the front and rear axles
center_of_rotation_y = center - (wheelbase / 2.0) + sensor_offset_front
angle_rad = np.radians(angle) # Convert angle to radians
cos_angle = np.cos(angle_rad)
sin_angle = np.sin(angle_rad)
new_points = np.zeros_like(plot_points)
for y in range(grid_size):
for x in range(grid_size):
if plot_points[y, x] == 1:
# Translate point to the rotation axis
trans_x = x - center
trans_y = y - center_of_rotation_y
# Rotate the point
rotated_x = trans_x * cos_angle - trans_y * sin_angle
rotated_y = trans_x * sin_angle + trans_y * cos_angle
# Translate the point back
new_x = int(rotated_x + center)
new_y = int(rotated_y + center_of_rotation_y)
if 0 <= new_x < grid_size and 0 <= new_y < grid_size:
new_points[new_y, new_x] = 1
plot_points = new_points
def shift_points(direction):
global plot_points, arrow_angle
"""Shift points based on the arrow key pressed and arrow direction."""
global plot_points, arrow_angle
# Set travel distance based on direction
#rotation = 12.85 # LEFT for 106ms ~=12.85 degrees
rotation = 90 # LEFT for 106ms ~=12.85 degrees
travel_distance = 23 if direction == 'up' else -23
# Calculate shift direction based on arrow orientation
dy = np.round(travel_distance * np.sin(np.radians(arrow_angle)))
dx = np.round(travel_distance * np.cos(np.radians(arrow_angle)))
if direction in ['up', 'down']:
# Shift points based on calculated dx and dy
new_points = np.zeros((grid_size, grid_size))
for y in range(grid_size):
for x in range(grid_size):
new_x = x + int(-dx)
new_y = y + int(dy)
if 0 <= new_x < grid_size and 0 <= new_y < grid_size:
new_points[new_y, new_x] = plot_points[y, x]
plot_points = new_points
elif direction == 'left':
rotate_points(rotation)
arrow_angle = (arrow_angle + rotation) % 360 # Rotate arrow by +12.85 degrees
elif direction == 'right':
rotate_points(-rotation)
arrow_angle = (arrow_angle - rotation) % 360 # Rotate arrow by -12.85 degrees
#plt.clf()
#draw_grid(points, arrow_angle)
# update_plot()
print(f'arrow_angle = {arrow_angle}')
print(f'dx = {dx}')
print(f'dy = {dy}')
print()
def on_key(event):
if event.key in ['up', 'down', 'left', 'right']:
shift_points(event.key)
plt.clf() # Clear the current figure
draw_grid(plot_points, arrow_angle) # Redraw the grid with the updated points and arrow angle
plt.pause(0.01) # Pause to update the figure
# Set up plot
plt.figure(figsize=(8, 8))
draw_grid(plot_points, arrow_angle)
plt.gcf().canvas.mpl_connect('key_press_event', on_key)
# Display the plot
plt.show()