Skip to content

Commit 2f51017

Browse files
committed
working?
1 parent 7577494 commit 2f51017

File tree

6 files changed

+11293
-3523
lines changed

6 files changed

+11293
-3523
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ consideration is that Phil sucks at C++ but no one sucks at Python.
1616

1717
# TODOs
1818

19-
- [] BUG: Histogram grid active region is upside down. Potential cause: have been using \[x\]\[y\] but should have been \[y\]\[x\]
19+
- [x] (Fixed) BUG: Histogram grid active region is upside down. Potential cause: have been using \[x\]\[y\] but should have been \[y\]\[x\] (it was the plotting x.append(y))
20+
- [] FIXME: Polar Histogram pie chart switch angle and maybe counterclockwise.

lib/histogram_grid.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import numpy as np
12
import csv
23
from operator import sub # for get_distance_between_discrete_points
34
import math
@@ -29,8 +30,8 @@ def from_map(cls, map_fname, active_region_dimension, resolution, robot_location
2930

3031
# Convert string "1"s and "0"s to integer 1s and 0s
3132
lines = list(map(lambda l: list(map(int, l)), lines))
32-
print("histogram_grid: histogram =")
33-
print(*lines, sep="\n")
33+
# print("histogram_grid: histogram =")
34+
# print(*lines, sep="\n")
3435
dimension = (len(lines[0]), len(lines))
3536
hg = cls(dimension, resolution, robot_location, active_region_dimension)
3637
hg.histogram_grid = lines
@@ -91,14 +92,17 @@ def get_angle_between_discrete_points(cls, discrete_start, discrete_end):
9192
"""
9293
Returns the angle between the line between pos2 and posRef and the horizontal along positive i direction.
9394
"""
94-
discrete_displacement = get_discrete_displacement(discrete_start, discrete_end)
95-
96-
delta_x, delta_y = discrete_displacement
97-
# print("histogram_grid: (delta_x, delta_y) =", discrete_displacement)
98-
99-
angle_radian = math.atan2(delta_y, delta_x)
100-
angle_degrees = math.degrees(angle_radian)
101-
return angle_degrees
95+
# discrete_displacement = get_discrete_displacement(discrete_start, discrete_end)
96+
97+
ang1 = np.arctan2(*discrete_start[::-1])
98+
ang2 = np.arctan2(*discrete_end[::-1])
99+
return np.rad2deg((ang1 - ang2) % (2 * np.pi))
100+
# delta_x, delta_y = discrete_displacement
101+
# # print("histogram_grid: (delta_x, delta_y) =", discrete_displacement)
102+
#
103+
# angle_radian = math.atan2(delta_y, delta_x)
104+
# angle_degrees = math.degrees(angle_radian)
105+
# return angle_degrees
102106

103107
def get_active_region(self, robot_location):
104108
# REVIEW: the four coordinates are discrete?

lib/path_planner.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ def generate_histogram(self, robot_location):
6767
distance = histogram_grid.get_continuous_distance_between_discrete_points(node_considered, robot_location)
6868
delta_certainty = (certainty ** 2) * (self.a - self.b * distance)
6969
robot_to_node_angle = histogram_grid.get_angle_between_discrete_points(robot_location, node_considered)
70-
polar_histogram.add_certainty_to_bin_at_angle(robot_to_node_angle, delta_certainty)
71-
histogram_grid.get_certainty_at_discrete_point(node_considered)
70+
print("path_planner: robot_to_node_angle between robot_location", robot_location, "and node_considered", node_considered, "is", robot_to_node_angle)
71+
if delta_certainty != 0:
72+
print("path_planner: adding certainty %.1f to angle %s or node" % (delta_certainty, robot_to_node_angle), node_considered)
73+
polar_histogram.add_certainty_to_bin_at_angle(robot_to_node_angle, delta_certainty)
7274

7375

7476
polar_histogram.smooth_histogram(self.l)

lib/polar_histogram.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ def get_certainty_from_angle(self, angle):
7878

7979
def add_certainty_to_bin_at_angle(self, angle, delta_certainty):
8080
"""Adds the passed value to the current value of the histogr1am grid."""
81-
self._polar_histogram[self.get_bin_index_from_angle(angle)] += delta_certainty
81+
bin_index = self.get_bin_index_from_angle(angle)
82+
# print("polar_histogram: adding certainty %s to bin #%s = %s" % (delta_certainty, bin_index, angle))
83+
self._polar_histogram[bin_index] += delta_certainty
8284

8385

8486
def smooth_histogram(self, l):

lib/robot.py

+17-73
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,26 @@ class Robot:
2323
def __init__(self, histogram_grid, polar_histogram, init_location, target_location, init_speed):
2424
# CHANGED: we shouldn't need polar_histogram, only histogram_grid
2525
self.path_planner = PathPlanner(histogram_grid, polar_histogram, init_location, target_location)
26-
# // discretePoint location; //Stores the location of the robot
27-
# contPoint location; //Stores the location of the robot
2826
self.target_location = target_location
2927
self.location = init_location
3028
self.speed = init_speed
3129
self.update_angle()
3230

31+
3332
@classmethod
3433
def from_map(cls, map_fname, init_location, target_location, init_speed, active_region_dimension, resolution, num_bins):
3534
histogram_grid = HistogramGrid.from_map(map_fname, active_region_dimension, resolution, init_location)
3635
polar_histogram = PolarHistogram(num_bins)
3736
return cls(histogram_grid, polar_histogram, init_location, target_location, init_speed)
3837

3938

40-
41-
4239
def update_angle(self):
4340
continuous_displacement = (self.target_location[0] - self.location[0], self.target_location[1] - self.location[1])
4441
continuous_robot_to_target_angle = math.atan2(continuous_displacement[1], continuous_displacement[0])
4542
self.angle = self.path_planner.get_best_angle(continuous_robot_to_target_angle)
4643
self.continuous_robot_to_target_angle = continuous_robot_to_target_angle
4744

45+
4846
def set_speed(self, speed):
4947
self.speed = speed
5048

@@ -59,9 +57,6 @@ def update_location(self):
5957
angle_radian = self.angle * math.pi/180
6058
velocity_x, velocity_y = self.velocity
6159

62-
# delta_x = self.speed * math.cos(angle_radian)
63-
# delta_y = self.speed * math.sin(angle_radian)
64-
6560
old_x, old_y = self.location
6661
self.location = (old_x + velocity_x, old_y + velocity_y)
6762

@@ -71,42 +66,18 @@ def update_location(self):
7166
self.path_planner.set_robot_location(discrete_location)
7267

7368

74-
# def talk(self):
75-
# discrete_target_location = self.histogram_grid.get_discrete_target_location();
76-
# std::cout << "location is (" << location.x << ", "
77-
# << location.y << "); Desired Position is ("
78-
# << targetLoc.x << ", " << targetLoc.y << ")\n";
79-
# //Constructor for the RobotTest class
80-
# //CHANGED: no need for initAngle or initSpeed: robot should figure out.
81-
# // RobotTest(discretePoint initPos, double initAngle, double initSpeed):
82-
# RobotTest(discretePoint initPos):
83-
# grid("../map.txt", initPos, ACTIVE_REGION_SIZE_I, ACTIVE_REGION_SIZE_J, HIST_WIDTH, HIST_LENGTH, NODE_SIZE_IN),
84-
# hist(NUM_BINS),
85-
# pather(hist, &grid, A_IN, B_IN, L_IN, MAX_NUM_NODES_FOR_VALLEY_IN, VALLEY_THRESHOLD_IN)
86-
# {
87-
# location.x = initPos.x;
88-
# location.y = initPos.y;
89-
90-
# pather.updateRobotPosition(discreteLocation);
91-
# grid.setTargetLoc({TARGET_X, TARGET_Y});
92-
# }
93-
94-
95-
#//main function per timestep
96-
#// 1. Get angle from nothing at t=0, then
97-
#// 2. get speed from nothing at t=0.
98-
#// 3. Given position at 0, draw simulation at t=0,
99-
#// 4. Now move from t=0 to t=1 by only updating the robot's position.
69+
# Main function per timestep
70+
# 1. Get angle from nothing at t=0, then
71+
# 2. get speed from nothing at t=0.
72+
# 3. Given position at 0, draw simulation at t=0,
73+
# 4. Now move from t=0 to t=1 by only updating the robot's position.
10074
def step(self, draw=True):
10175
self.print_histogram()
102-
# print("robot: angle =", self.angle)
103-
self.update_angle() # // angle: Null (or optionally, t-1) => t
104-
# self.set_speed() # // speed: Null (or optionally, t-1) => t
76+
self.update_angle() # angle: Null (or optionally, t-1) => t
77+
# self.set_speed() # speed: Null (or optionally, t-1) => t
10578
print("robot: step: best angle =", self.angle )
10679
self.update_velocity()
107-
# // at t=0, angle: t=0, speed, position: t
108-
109-
self.update_location() #// position: t => t+1
80+
self.update_location() # position: t => t+1
11081

11182
def loop(self, num_steps, draw=True):
11283
plt.ion() # enable interactive plotting mode
@@ -139,39 +110,24 @@ def loop(self, num_steps, draw=True):
139110
bin_certainties = [certainty for angle, certainty in polar_histogram_by_angle]
140111
colors = ['blue' if certainty < valley_threshold else 'red' for angle, certainty in polar_histogram_by_angle]
141112
labels = [angle for angle, certainty in polar_histogram_by_angle]
142-
# index = 0
143113
generator = enumerate(polar_histogram_by_angle)
144114
def make_autopct(bin_percentages):
145115
def my_autopct(pct):
146-
# index = 0
147-
# total = sum(values)
148-
# val = int(round(pct*total/100.0))
149116
index, (angle, certainty) = next(generator)
150-
# index += 1
151117
return '{angle:.0f}: {certainty:.1f}'.format(angle=angle, certainty=certainty)
152118
return my_autopct
153119

154-
(pie_patches, pie_texts, pie_autotexts) = polar_plot.pie(bin_percentages, colors=colors, labels=labels, startangle=0, counterclock=True, autopct=make_autopct(bin_percentages))
120+
pie_patches, pie_texts, pie_autotexts = polar_plot.pie(bin_percentages, colors=colors, labels=labels, startangle=0, counterclock=True, autopct=make_autopct(bin_percentages))
155121

156122

157123
# 3. Plot the valley
158124
histogram_grid_active_region = self.path_planner.histogram_grid.get_histogram_grid_active_region(active_region_min_x, active_region_min_y, active_region_max_x, active_region_max_y)
159-
print('active region histogram =')
160-
print(*histogram_grid_active_region, sep='\n')
125+
# print('active region histogram =')
126+
# print(*histogram_grid_active_region, sep='\n')
161127
histogram_grid_plot.clear()
162128
histogram_grid_plot.matshow(histogram_grid_active_region, origin="upper")
163129

164130

165-
# self.draw(ax)
166-
# display.clear_output(wait=True)
167-
# display.display(plt.gcf())
168-
# plt.draw()
169-
# bnext = Button(ax, 'Next')
170-
# bnext.on_clicked(self.step_callback)
171-
# bprev = Button(ax, 'Previous')
172-
# bprev.on_clicked(callback.prev)
173-
174-
175131
for i in range(num_steps):
176132

177133
self.step()
@@ -186,7 +142,6 @@ def my_autopct(pct):
186142
active_region_min_x, active_region_min_y, active_region_max_x, active_region_max_y = self.path_planner.histogram_grid.get_active_region(self.location)
187143
rectangle.set_bounds(active_region_min_x, active_region_min_y, active_region_max_x - active_region_min_x, active_region_max_y - active_region_min_y)
188144

189-
# simulation_plot.invert_yaxis()
190145

191146
# 2. Replot the polar histogram
192147
# sectors = self.path_planner.get_sectors() # NOTE: sectors are only valid
@@ -198,40 +153,29 @@ def my_autopct(pct):
198153
bin_certainties = [certainty for angle, certainty in polar_histogram_by_angle]
199154
colors = ['blue' if certainty < valley_threshold else 'red' for angle, certainty in polar_histogram_by_angle]
200155
labels = [angle for angle, certainty in polar_histogram_by_angle]
201-
# index = 0
202156
generator = enumerate(polar_histogram_by_angle)
203157
def make_autopct(bin_percentages):
204158
def my_autopct(pct):
205-
# index = 0
206-
# total = sum(values)
207-
# val = int(round(pct*total/100.0))
208159
index, (angle, certainty) = next(generator)
209-
# index += 1
210160
return '{certainty:.1f}'.format(certainty=certainty)
211161
return my_autopct
212162

213163
polar_plot.clear()
214-
polar_plot.pie(bin_percentages, colors=colors, labels=labels, startangle=0, counterclock=True, autopct=make_autopct(bin_percentages))
164+
# polar_plot.pie(bin_percentages, colors=colors, labels=labels, startangle=0, counterclock=True, autopct=make_autopct(bin_percentages))
165+
polar_plot.pie(bin_percentages, colors=colors, labels=labels, startangle=0, autopct=make_autopct(bin_percentages))
215166

216167

217168
# 3. Replot the histogram_grid
218169
histogram_grid_active_region = self.path_planner.histogram_grid.get_histogram_grid_active_region(active_region_min_x, active_region_min_y, active_region_max_x, active_region_max_y)
219-
print('active region histogram =')
220-
print(*histogram_grid_active_region, sep='\n')
170+
# print('active region histogram =')
171+
# print(*histogram_grid_active_region, sep='\n')
221172
histogram_grid_plot.clear()
222173
histogram_grid_plot.matshow(histogram_grid_active_region, origin="upper")
223174

224175

225176
# 4. Actually display the plots
226177
# display.clear_output(wait=True) # NOTE: Uncomment this for animation. Comment this out if you want to see all steps.
227178
display.display(plt.gcf())
228-
# self.print_histogram()
229-
# figure.canvas.draw_idle()
230-
# plt.pause(0.1)
231-
232-
def step_callback(self, event):
233-
self.step()
234-
self.print_histogram()
235179

236180

237181
def print_histogram(self):

0 commit comments

Comments
 (0)