blob: 5559f7ca621569ec14c134db607194e021512a52 [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
120 # Add the Make*Path functions.
121 h_file.append("")
122 cc_file.append("")
123 for name, segment in list(enumerate(graph_generate.unnamed_segments)) + [
124 (x.name, x) for x in graph_generate.named_segments
125 ]:
126 h_file.append(
127 "::std::unique_ptr<Path> %s();" % path_function_name(name))
128 cc_file.append(
129 "::std::unique_ptr<Path> %s() {" % path_function_name(name))
130 cc_file.append(" return ::std::unique_ptr<Path>(new Path({")
131 for point in segment.ToThetaPoints():
132 cc_file.append(" {{%.12f, %.12f, %.12f," %
133 (numpy.pi / 2.0 - point[0],
134 numpy.pi / 2.0 - point[1], -point[2]))
135 cc_file.append(" %.12f, %.12f, %.12f}}," %
136 (-point[3], -point[4], -point[5]))
137 cc_file.append(" }));")
138 cc_file.append("}")
Austin Schuhb874fd32018-03-05 00:27:10 -0800139
Andrew Runkef9f57452018-03-04 18:19:36 -0800140 # Matrix of nodes
141 h_file.append("::std::vector<::Eigen::Matrix<double, 2, 1>> PointList();")
142
Austin Schuhb874fd32018-03-05 00:27:10 -0800143 cc_file.append(
144 "::std::vector<::Eigen::Matrix<double, 2, 1>> PointList() {")
Andrew Runkef9f57452018-03-04 18:19:36 -0800145 cc_file.append(" ::std::vector<::Eigen::Matrix<double, 2, 1>> points;")
Austin Schuhb874fd32018-03-05 00:27:10 -0800146 for point in graph_generate.points:
Andrew Runkef9f57452018-03-04 18:19:36 -0800147 cc_file.append(
Austin Schuhb874fd32018-03-05 00:27:10 -0800148 " points.push_back((::Eigen::Matrix<double, 2, 1>() << %.12s, %.12s).finished());"
149 % (numpy.pi / 2.0 - point[0][0], numpy.pi / 2.0 - point[0][1]))
Andrew Runkef9f57452018-03-04 18:19:36 -0800150 cc_file.append(" return points;")
151 cc_file.append("}")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800152
153 # Now create the MakeSearchGraph function.
154 h_file.append("")
155 h_file.append("// Builds a search graph.")
156 h_file.append("SearchGraph MakeSearchGraph("
Austin Schuh41c71e42018-04-04 20:11:20 -0700157 "::std::vector<TrajectoryAndParams> *trajectories,")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800158 h_file.append(" "
159 "const ::Eigen::Matrix<double, 2, 2> &alpha_unitizer,")
160 h_file.append(" double vmax);")
161 cc_file.append("SearchGraph MakeSearchGraph("
Austin Schuh41c71e42018-04-04 20:11:20 -0700162 "::std::vector<TrajectoryAndParams> *trajectories,")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800163 cc_file.append(" "
164 "const ::Eigen::Matrix<double, 2, 2> &alpha_unitizer,")
165 cc_file.append(" " "double vmax) {")
166 cc_file.append(" ::std::vector<SearchGraph::Edge> edges;")
167
168 index = 0
169 segments_and_names = list(enumerate(graph_generate.unnamed_segments)) + [
170 (x.name, x) for x in graph_generate.named_segments
171 ]
172
173 for name, segment in segments_and_names:
174 add_edge(cc_file, name, segment, index, False)
175 index += 1
176 add_edge(cc_file, name, segment, index, True)
177 index += 1
178
179 cc_file.append(" return SearchGraph(%d, ::std::move(edges));" % len(
180 graph_generate.points))
181 cc_file.append("}")
182
183 h_file.append("")
184 h_file.append("} // namespace arm")
185 h_file.append("} // namespace superstructure")
186 h_file.append("} // namespace control_loops")
187 h_file.append("} // namespace y2018")
188 h_file.append("")
189 h_file.append(
190 "#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
191
192 cc_file.append("} // namespace arm")
193 cc_file.append("} // namespace superstructure")
194 cc_file.append("} // namespace control_loops")
195 cc_file.append("} // namespace y2018")
196
197 if len(argv) == 3:
198 with open(argv[1], "w") as hfd:
199 hfd.write("\n".join(h_file))
200
201 with open(argv[2], "w") as ccfd:
202 ccfd.write("\n".join(cc_file))
203 else:
204 print("\n".join(h_file))
205 print("\n".join(cc_file))
206
207
208if __name__ == '__main__':
209 main(sys.argv)