blob: abe5ed3b9de00c884a7e55f516e00b788d8e5bc3 [file] [log] [blame]
Austin Schuh7dfccf62018-03-03 21:28:14 -08001from __future__ import print_function
2import sys
3import numpy
4import graph_generate
5
6
7def index_function_name(name):
8 return "%sIndex" % name
9
10
11def path_function_name(name):
12 return "Make%sPath" % name
13
14
15def add_edge(cc_file, name, segment, index, reverse):
16 cc_file.append(" // Adding edge %d" % index)
Austin Schuh41c71e42018-04-04 20:11:20 -070017 vmax = "vmax"
18 if segment.vmax:
19 vmax = "::std::min(vmax, %f)" % segment.vmax
20 cc_file.append( " trajectories->emplace_back(%s," % (vmax))
21 cc_file.append( " alpha_unitizer,")
Austin Schuh7dfccf62018-03-03 21:28:14 -080022 if reverse:
23 cc_file.append(
Austin Schuh41c71e42018-04-04 20:11:20 -070024 " Trajectory(Path::Reversed(%s()), 0.005));"
25 % (path_function_name(str(name))))
Austin Schuh7dfccf62018-03-03 21:28:14 -080026 else:
Austin Schuh41c71e42018-04-04 20:11:20 -070027 cc_file.append(
28 " Trajectory(%s(), 0.005));"
29 % (path_function_name(str(name))))
Austin Schuh7dfccf62018-03-03 21:28:14 -080030
31 start_index = None
32 end_index = None
33 for point, name in graph_generate.points:
34 if (point == segment.start).all():
35 start_index = name
36 if (point == segment.end).all():
37 end_index = name
38
39 if reverse:
40 start_index, end_index = end_index, start_index
41
42 cc_file.append(" edges.push_back(SearchGraph::Edge{%s(), %s()," %
43 (index_function_name(start_index),
44 index_function_name(end_index)))
45 cc_file.append(
Austin Schuh41c71e42018-04-04 20:11:20 -070046 " (trajectories->back().trajectory.path().length() + 1.0)});")
Austin Schuh7dfccf62018-03-03 21:28:14 -080047
48 # TODO(austin): Allow different vmaxes for different paths.
49 cc_file.append(
Austin Schuh41c71e42018-04-04 20:11:20 -070050 " trajectories->back().trajectory.OptimizeTrajectory(")
51 cc_file.append(" trajectories->back().alpha_unitizer,")
52 cc_file.append(" trajectories->back().vmax);")
Austin Schuh7dfccf62018-03-03 21:28:14 -080053 cc_file.append("")
54
55
56def main(argv):
57 cc_file = []
58 cc_file.append(
59 "#include \"y2018/control_loops/superstructure/arm/generated_graph.h\""
60 )
61 cc_file.append("")
62 cc_file.append("#include <memory>")
63 cc_file.append("")
64 cc_file.append(
65 "#include \"y2018/control_loops/superstructure/arm/trajectory.h\"")
66 cc_file.append(
67 "#include \"y2018/control_loops/superstructure/arm/graph.h\"")
68 cc_file.append("")
69 cc_file.append("namespace y2018 {")
70 cc_file.append("namespace control_loops {")
71 cc_file.append("namespace superstructure {")
72 cc_file.append("namespace arm {")
73
74 h_file = []
75 h_file.append(
76 "#ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
77 h_file.append(
78 "#define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
79 h_file.append("")
80 h_file.append("#include <memory>")
81 h_file.append("")
82 h_file.append(
83 "#include \"y2018/control_loops/superstructure/arm/trajectory.h\"")
84 h_file.append(
85 "#include \"y2018/control_loops/superstructure/arm/graph.h\"")
86 h_file.append("")
87 h_file.append("namespace y2018 {")
88 h_file.append("namespace control_loops {")
89 h_file.append("namespace superstructure {")
90 h_file.append("namespace arm {")
91
Austin Schuh41c71e42018-04-04 20:11:20 -070092 h_file.append("")
93 h_file.append("struct TrajectoryAndParams {")
94 h_file.append(" TrajectoryAndParams(double new_vmax,")
95 h_file.append(
96 " const ::Eigen::Matrix<double, 2, 2> &new_alpha_unitizer,"
97 )
98 h_file.append(" Trajectory &&new_trajectory)")
99 h_file.append(" : vmax(new_vmax),")
100 h_file.append(" alpha_unitizer(new_alpha_unitizer),")
101 h_file.append(" trajectory(::std::move(new_trajectory)) {}")
102 h_file.append(" double vmax;")
103 h_file.append(" ::Eigen::Matrix<double, 2, 2> alpha_unitizer;")
104 h_file.append(" Trajectory trajectory;")
105 h_file.append("};")
106 h_file.append("")
107
Austin Schuh7dfccf62018-03-03 21:28:14 -0800108 # Now dump out the vertices and associated constexpr vertex name functions.
109 for index, point in enumerate(graph_generate.points):
110 h_file.append("")
111 h_file.append("constexpr uint32_t %s() { return %d; }" %
112 (index_function_name(point[1]), index))
113 h_file.append(
114 "inline ::Eigen::Matrix<double, 2, 1> %sPoint() {" % point[1])
115 h_file.append(
116 " return (::Eigen::Matrix<double, 2, 1>() << %f, %f).finished();"
117 % (numpy.pi / 2.0 - point[0][0], numpy.pi / 2.0 - point[0][1]))
118 h_file.append("}")
119
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700120 front_points = [
121 index_function_name(point[1]) + "()" for point in graph_generate.front_points
122 ]
123 h_file.append("")
124 h_file.append("constexpr ::std::array<uint32_t, %d> FrontPoints() {" %
125 len(front_points))
126 h_file.append(" return ::std::array<uint32_t, %d>{{%s}};" %
127 (len(front_points), ", ".join(front_points)))
128 h_file.append("}")
129
130 back_points = [
131 index_function_name(point[1]) + "()" for point in graph_generate.back_points
132 ]
133 h_file.append("")
134 h_file.append("constexpr ::std::array<uint32_t, %d> BackPoints() {" %
135 len(back_points))
136 h_file.append(" return ::std::array<uint32_t, %d>{{%s}};" %
137 (len(back_points), ", ".join(back_points)))
138 h_file.append("}")
139
Austin Schuh7dfccf62018-03-03 21:28:14 -0800140 # Add the Make*Path functions.
141 h_file.append("")
142 cc_file.append("")
143 for name, segment in list(enumerate(graph_generate.unnamed_segments)) + [
144 (x.name, x) for x in graph_generate.named_segments
145 ]:
146 h_file.append(
147 "::std::unique_ptr<Path> %s();" % path_function_name(name))
148 cc_file.append(
149 "::std::unique_ptr<Path> %s() {" % path_function_name(name))
150 cc_file.append(" return ::std::unique_ptr<Path>(new Path({")
151 for point in segment.ToThetaPoints():
152 cc_file.append(" {{%.12f, %.12f, %.12f," %
153 (numpy.pi / 2.0 - point[0],
154 numpy.pi / 2.0 - point[1], -point[2]))
155 cc_file.append(" %.12f, %.12f, %.12f}}," %
156 (-point[3], -point[4], -point[5]))
157 cc_file.append(" }));")
158 cc_file.append("}")
Austin Schuhb874fd32018-03-05 00:27:10 -0800159
Andrew Runkef9f57452018-03-04 18:19:36 -0800160 # Matrix of nodes
161 h_file.append("::std::vector<::Eigen::Matrix<double, 2, 1>> PointList();")
162
Austin Schuhb874fd32018-03-05 00:27:10 -0800163 cc_file.append(
164 "::std::vector<::Eigen::Matrix<double, 2, 1>> PointList() {")
Andrew Runkef9f57452018-03-04 18:19:36 -0800165 cc_file.append(" ::std::vector<::Eigen::Matrix<double, 2, 1>> points;")
Austin Schuhb874fd32018-03-05 00:27:10 -0800166 for point in graph_generate.points:
Andrew Runkef9f57452018-03-04 18:19:36 -0800167 cc_file.append(
Austin Schuhb874fd32018-03-05 00:27:10 -0800168 " points.push_back((::Eigen::Matrix<double, 2, 1>() << %.12s, %.12s).finished());"
169 % (numpy.pi / 2.0 - point[0][0], numpy.pi / 2.0 - point[0][1]))
Andrew Runkef9f57452018-03-04 18:19:36 -0800170 cc_file.append(" return points;")
171 cc_file.append("}")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800172
173 # Now create the MakeSearchGraph function.
174 h_file.append("")
175 h_file.append("// Builds a search graph.")
176 h_file.append("SearchGraph MakeSearchGraph("
Austin Schuh41c71e42018-04-04 20:11:20 -0700177 "::std::vector<TrajectoryAndParams> *trajectories,")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800178 h_file.append(" "
179 "const ::Eigen::Matrix<double, 2, 2> &alpha_unitizer,")
180 h_file.append(" double vmax);")
181 cc_file.append("SearchGraph MakeSearchGraph("
Austin Schuh41c71e42018-04-04 20:11:20 -0700182 "::std::vector<TrajectoryAndParams> *trajectories,")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800183 cc_file.append(" "
184 "const ::Eigen::Matrix<double, 2, 2> &alpha_unitizer,")
185 cc_file.append(" " "double vmax) {")
186 cc_file.append(" ::std::vector<SearchGraph::Edge> edges;")
187
188 index = 0
189 segments_and_names = list(enumerate(graph_generate.unnamed_segments)) + [
190 (x.name, x) for x in graph_generate.named_segments
191 ]
192
193 for name, segment in segments_and_names:
194 add_edge(cc_file, name, segment, index, False)
195 index += 1
196 add_edge(cc_file, name, segment, index, True)
197 index += 1
198
199 cc_file.append(" return SearchGraph(%d, ::std::move(edges));" % len(
200 graph_generate.points))
201 cc_file.append("}")
202
203 h_file.append("")
204 h_file.append("} // namespace arm")
205 h_file.append("} // namespace superstructure")
206 h_file.append("} // namespace control_loops")
207 h_file.append("} // namespace y2018")
208 h_file.append("")
209 h_file.append(
210 "#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
211
212 cc_file.append("} // namespace arm")
213 cc_file.append("} // namespace superstructure")
214 cc_file.append("} // namespace control_loops")
215 cc_file.append("} // namespace y2018")
216
217 if len(argv) == 3:
218 with open(argv[1], "w") as hfd:
219 hfd.write("\n".join(h_file))
220
221 with open(argv[2], "w") as ccfd:
222 ccfd.write("\n".join(cc_file))
223 else:
224 print("\n".join(h_file))
225 print("\n".join(cc_file))
226
227
228if __name__ == '__main__':
229 main(sys.argv)