/
plan.py
253 lines (160 loc) · 6.14 KB
/
plan.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import sys
def prepare_parser():
import argparse
parser = \
argparse.ArgumentParser(
description = \
"Планирование пути наземного беспилотного транспортного\
средства (роботизированной платформы)"
)
required_arguments = parser.add_argument_group("required arguments")
parser.add_argument(
"-m", "--map",
type = argparse.FileType("r"),
default = sys.stdin,
metavar = ("path"),
help = \
"Имя файла, содержащего карту местности, используемую для\
совершения планирования пути. В случае отсутствия опции, чтение\
карты производится из стандартного потока ввода"
)
required_arguments.add_argument(
"-s", "--start",
nargs = 2,
type = float,
required = True,
metavar = ("x", "y"),
help = "Координаты <x, y> исходного положения платформы"
)
required_arguments.add_argument(
"-g", "--goal",
nargs = 2,
type = float,
required = True,
metavar = ("x", "y"),
help = "Координаты <x, y> целевого положения платформы"
)
parser.add_argument(
"-o", "--output",
type = argparse.FileType("w"),
default = sys.stdout,
metavar = ("path"),
help = \
"Имя файла, в который производится запись результатов планирования\
пути. В случае отсутствия опции, запись результатов\
производится в стандартный потока вывода"
)
parser.add_argument(
"--visualization",
type = argparse.FileType("w"),
metavar = ("path"),
help = \
"Имя файла, в который производится запись изображения,\
визуализирующего спланированный путь"
)
return parser
def build_surface(arguments):
from planning.surface.surface_building import build_surface
surface = \
build_surface(
arguments.map,
0.0001
)
return surface
def plan_path(surface, arguments):
from planning.planning.global_planning.planning \
import PlanningParameters, \
plan
from planning.surface.polygon import Polygon
#
initial_point_x, initial_point_y = tuple(arguments.start)
initial_polygon = \
Polygon(
[
(10 * (initial_point_x // 10), 10 * (initial_point_y // 10), 0),
(10 * (initial_point_x // 10) + 10, 10 * (initial_point_y // 10), 0),
(10 * (initial_point_x // 10) + 10, 10 * (initial_point_y // 10) + 10, 0),
(10 * (initial_point_x // 10), 10 * (initial_point_y // 10) + 10, 0),
],
0.0
)
final_point_x, final_point_y = tuple(arguments.goal)
final_polygon = \
Polygon(
[
(10 * (final_point_x // 10), 10 * (final_point_y // 10), 0),
(10 * (final_point_x // 10) + 10, 10 * (final_point_y // 10), 0),
(10 * (final_point_x // 10) + 10, 10 * (final_point_y // 10) + 10, 0),
(10 * (final_point_x // 10), 10 * (final_point_y // 10) + 10, 0),
],
0.0
)
# Установка параметров планирования пути
planning_parameters = PlanningParameters()
planning_parameters.surface = surface
planning_parameters.planner = "a-star-planner"
# planning_parameters.planner = "progressive-a-star-planner"
# planning_parameters.planner = "ant-colony-planner"
planning_parameters.initial_polygon = initial_polygon #list(surface.polygons)[0]
planning_parameters.final_polygon = final_polygon
planning_parameters.smoothing_depth = 1
# Планирование пути
planning_result = plan(planning_parameters)
return planning_result
def visualize_path(planning_result, surface, arguments):
from planning.visualization.visualization import create_svg
controls_sequence, cost = planning_result
create_svg(
arguments.visualization,
surface,
controls_sequence
)
if __name__ == "__main__":
import importlib
def check_module_presence(module_name):
is_module_present = True
try:
if importlib.find_loader(module_name) is None:
is_module_present = False
except ValueError:
is_module_present = False
return is_module_present
# Проверка наличия требуемых модулей
required_modules_names = ["numpy"]
are_required_modules_present = True
for required_module_name in required_modules_names:
if not check_module_presence(required_module_name):
sys.stderr.write(
"Ошибка: для работы программы требуется %s\n"
% required_module_name
)
are_required_modules_present = False
if not are_required_modules_present:
exit()
# Чтение параметров командной строки
parser = prepare_parser()
arguments = parser.parse_args()
# Чтение карты местности
surface = build_surface(arguments)
# Планирование глобального пути
planning_result = plan_path(surface, arguments)
states_sequence, _ = planning_result
for state in states_sequence:
polygon = state.polygons_sequence[-1]
map_polygon_index = surface.get_map_polygon_index(polygon)
arguments.output.write("%s\n" % map_polygon_index)
#!!!!!<Временно>
from planning.planning.local_planning.trajectory_planning import plan
polygons_sequence = \
[state.polygons_sequence[-1] for state in states_sequence]
plan(surface, states_sequence, tuple(arguments.start), tuple(arguments.goal))
#!!!!!</Временно>
# Визуализация
if arguments.visualization is not None:
if not check_module_presence("svgwrite"):
sys.stderr.write(
"Ошибка: для визуализации спланированного пути требуется" \
+ " svgwrite\n"
)
else:
visualize_path(planning_result, surface, arguments)