@@ -23,28 +23,26 @@ class Robot:
23
23
def __init__ (self , histogram_grid , polar_histogram , init_location , target_location , init_speed ):
24
24
# CHANGED: we shouldn't need polar_histogram, only histogram_grid
25
25
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
28
26
self .target_location = target_location
29
27
self .location = init_location
30
28
self .speed = init_speed
31
29
self .update_angle ()
32
30
31
+
33
32
@classmethod
34
33
def from_map (cls , map_fname , init_location , target_location , init_speed , active_region_dimension , resolution , num_bins ):
35
34
histogram_grid = HistogramGrid .from_map (map_fname , active_region_dimension , resolution , init_location )
36
35
polar_histogram = PolarHistogram (num_bins )
37
36
return cls (histogram_grid , polar_histogram , init_location , target_location , init_speed )
38
37
39
38
40
-
41
-
42
39
def update_angle (self ):
43
40
continuous_displacement = (self .target_location [0 ] - self .location [0 ], self .target_location [1 ] - self .location [1 ])
44
41
continuous_robot_to_target_angle = math .atan2 (continuous_displacement [1 ], continuous_displacement [0 ])
45
42
self .angle = self .path_planner .get_best_angle (continuous_robot_to_target_angle )
46
43
self .continuous_robot_to_target_angle = continuous_robot_to_target_angle
47
44
45
+
48
46
def set_speed (self , speed ):
49
47
self .speed = speed
50
48
@@ -59,9 +57,6 @@ def update_location(self):
59
57
angle_radian = self .angle * math .pi / 180
60
58
velocity_x , velocity_y = self .velocity
61
59
62
- # delta_x = self.speed * math.cos(angle_radian)
63
- # delta_y = self.speed * math.sin(angle_radian)
64
-
65
60
old_x , old_y = self .location
66
61
self .location = (old_x + velocity_x , old_y + velocity_y )
67
62
@@ -71,42 +66,18 @@ def update_location(self):
71
66
self .path_planner .set_robot_location (discrete_location )
72
67
73
68
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.
100
74
def step (self , draw = True ):
101
75
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
105
78
print ("robot: step: best angle =" , self .angle )
106
79
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
110
81
111
82
def loop (self , num_steps , draw = True ):
112
83
plt .ion () # enable interactive plotting mode
@@ -139,39 +110,24 @@ def loop(self, num_steps, draw=True):
139
110
bin_certainties = [certainty for angle , certainty in polar_histogram_by_angle ]
140
111
colors = ['blue' if certainty < valley_threshold else 'red' for angle , certainty in polar_histogram_by_angle ]
141
112
labels = [angle for angle , certainty in polar_histogram_by_angle ]
142
- # index = 0
143
113
generator = enumerate (polar_histogram_by_angle )
144
114
def make_autopct (bin_percentages ):
145
115
def my_autopct (pct ):
146
- # index = 0
147
- # total = sum(values)
148
- # val = int(round(pct*total/100.0))
149
116
index , (angle , certainty ) = next (generator )
150
- # index += 1
151
117
return '{angle:.0f}: {certainty:.1f}' .format (angle = angle , certainty = certainty )
152
118
return my_autopct
153
119
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 ))
155
121
156
122
157
123
# 3. Plot the valley
158
124
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')
161
127
histogram_grid_plot .clear ()
162
128
histogram_grid_plot .matshow (histogram_grid_active_region , origin = "upper" )
163
129
164
130
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
-
175
131
for i in range (num_steps ):
176
132
177
133
self .step ()
@@ -186,7 +142,6 @@ def my_autopct(pct):
186
142
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 )
187
143
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 )
188
144
189
- # simulation_plot.invert_yaxis()
190
145
191
146
# 2. Replot the polar histogram
192
147
# sectors = self.path_planner.get_sectors() # NOTE: sectors are only valid
@@ -198,40 +153,29 @@ def my_autopct(pct):
198
153
bin_certainties = [certainty for angle , certainty in polar_histogram_by_angle ]
199
154
colors = ['blue' if certainty < valley_threshold else 'red' for angle , certainty in polar_histogram_by_angle ]
200
155
labels = [angle for angle , certainty in polar_histogram_by_angle ]
201
- # index = 0
202
156
generator = enumerate (polar_histogram_by_angle )
203
157
def make_autopct (bin_percentages ):
204
158
def my_autopct (pct ):
205
- # index = 0
206
- # total = sum(values)
207
- # val = int(round(pct*total/100.0))
208
159
index , (angle , certainty ) = next (generator )
209
- # index += 1
210
160
return '{certainty:.1f}' .format (certainty = certainty )
211
161
return my_autopct
212
162
213
163
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 ))
215
166
216
167
217
168
# 3. Replot the histogram_grid
218
169
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')
221
172
histogram_grid_plot .clear ()
222
173
histogram_grid_plot .matshow (histogram_grid_active_region , origin = "upper" )
223
174
224
175
225
176
# 4. Actually display the plots
226
177
# display.clear_output(wait=True) # NOTE: Uncomment this for animation. Comment this out if you want to see all steps.
227
178
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 ()
235
179
236
180
237
181
def print_histogram (self ):
0 commit comments