blob: e7f8ce1922620840b30b1764a867e6ace29709f4 [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
Austin Schuhcf96d322018-04-07 15:52:31 -070020
21 alpha_unitizer = "alpha_unitizer"
22 if segment.alpha_unitizer is not None:
23 alpha_unitizer = "(::Eigen::Matrix<double, 2, 2>() << %f, %f, %f, %f).finished()" % (
24 segment.alpha_unitizer[0, 0], segment.alpha_unitizer[0, 1],
25 segment.alpha_unitizer[1, 0], segment.alpha_unitizer[1, 1])
Ravago Jones5127ccc2022-07-31 16:32:45 -070026 cc_file.append(" trajectories->emplace_back(%s," % (vmax))
27 cc_file.append(" %s," % (alpha_unitizer))
Austin Schuh7dfccf62018-03-03 21:28:14 -080028 if reverse:
29 cc_file.append(
Austin Schuh41c71e42018-04-04 20:11:20 -070030 " Trajectory(Path::Reversed(%s()), 0.005));"
31 % (path_function_name(str(name))))
Austin Schuh7dfccf62018-03-03 21:28:14 -080032 else:
Austin Schuh41c71e42018-04-04 20:11:20 -070033 cc_file.append(
Ravago Jones5127ccc2022-07-31 16:32:45 -070034 " Trajectory(%s(), 0.005));" %
35 (path_function_name(str(name))))
Austin Schuh7dfccf62018-03-03 21:28:14 -080036
37 start_index = None
38 end_index = None
39 for point, name in graph_generate.points:
40 if (point == segment.start).all():
41 start_index = name
42 if (point == segment.end).all():
43 end_index = name
44
45 if reverse:
46 start_index, end_index = end_index, start_index
47
Austin Schuh7dfccf62018-03-03 21:28:14 -080048 cc_file.append(
Ravago Jones5127ccc2022-07-31 16:32:45 -070049 " edges.push_back(SearchGraph::Edge{%s(), %s()," %
50 (index_function_name(start_index), index_function_name(end_index)))
51 cc_file.append(
52 " (trajectories->back().trajectory.path().length() + 0.2)});"
53 )
Austin Schuh7dfccf62018-03-03 21:28:14 -080054
55 # TODO(austin): Allow different vmaxes for different paths.
Ravago Jones5127ccc2022-07-31 16:32:45 -070056 cc_file.append(" trajectories->back().trajectory.OptimizeTrajectory(")
Austin Schuh41c71e42018-04-04 20:11:20 -070057 cc_file.append(" trajectories->back().alpha_unitizer,")
58 cc_file.append(" trajectories->back().vmax);")
Austin Schuh7dfccf62018-03-03 21:28:14 -080059 cc_file.append("")
60
61
62def main(argv):
63 cc_file = []
64 cc_file.append(
65 "#include \"y2018/control_loops/superstructure/arm/generated_graph.h\""
66 )
67 cc_file.append("")
68 cc_file.append("#include <memory>")
69 cc_file.append("")
70 cc_file.append(
71 "#include \"y2018/control_loops/superstructure/arm/trajectory.h\"")
72 cc_file.append(
73 "#include \"y2018/control_loops/superstructure/arm/graph.h\"")
74 cc_file.append("")
75 cc_file.append("namespace y2018 {")
76 cc_file.append("namespace control_loops {")
77 cc_file.append("namespace superstructure {")
78 cc_file.append("namespace arm {")
79
80 h_file = []
81 h_file.append(
82 "#ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
83 h_file.append(
84 "#define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
85 h_file.append("")
86 h_file.append("#include <memory>")
87 h_file.append("")
88 h_file.append(
89 "#include \"y2018/control_loops/superstructure/arm/trajectory.h\"")
90 h_file.append(
91 "#include \"y2018/control_loops/superstructure/arm/graph.h\"")
92 h_file.append("")
93 h_file.append("namespace y2018 {")
94 h_file.append("namespace control_loops {")
95 h_file.append("namespace superstructure {")
96 h_file.append("namespace arm {")
97
Austin Schuh41c71e42018-04-04 20:11:20 -070098 h_file.append("")
99 h_file.append("struct TrajectoryAndParams {")
100 h_file.append(" TrajectoryAndParams(double new_vmax,")
101 h_file.append(
102 " const ::Eigen::Matrix<double, 2, 2> &new_alpha_unitizer,"
103 )
104 h_file.append(" Trajectory &&new_trajectory)")
105 h_file.append(" : vmax(new_vmax),")
106 h_file.append(" alpha_unitizer(new_alpha_unitizer),")
107 h_file.append(" trajectory(::std::move(new_trajectory)) {}")
108 h_file.append(" double vmax;")
109 h_file.append(" ::Eigen::Matrix<double, 2, 2> alpha_unitizer;")
110 h_file.append(" Trajectory trajectory;")
111 h_file.append("};")
112 h_file.append("")
113
Austin Schuh7dfccf62018-03-03 21:28:14 -0800114 # Now dump out the vertices and associated constexpr vertex name functions.
115 for index, point in enumerate(graph_generate.points):
116 h_file.append("")
117 h_file.append("constexpr uint32_t %s() { return %d; }" %
118 (index_function_name(point[1]), index))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700119 h_file.append("inline ::Eigen::Matrix<double, 2, 1> %sPoint() {" %
120 point[1])
Austin Schuh7dfccf62018-03-03 21:28:14 -0800121 h_file.append(
122 " return (::Eigen::Matrix<double, 2, 1>() << %f, %f).finished();"
123 % (numpy.pi / 2.0 - point[0][0], numpy.pi / 2.0 - point[0][1]))
124 h_file.append("}")
125
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700126 front_points = [
Ravago Jones5127ccc2022-07-31 16:32:45 -0700127 index_function_name(point[1]) + "()"
128 for point in graph_generate.front_points
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700129 ]
130 h_file.append("")
131 h_file.append("constexpr ::std::array<uint32_t, %d> FrontPoints() {" %
132 len(front_points))
133 h_file.append(" return ::std::array<uint32_t, %d>{{%s}};" %
134 (len(front_points), ", ".join(front_points)))
135 h_file.append("}")
136
137 back_points = [
Ravago Jones5127ccc2022-07-31 16:32:45 -0700138 index_function_name(point[1]) + "()"
139 for point in graph_generate.back_points
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700140 ]
141 h_file.append("")
142 h_file.append("constexpr ::std::array<uint32_t, %d> BackPoints() {" %
143 len(back_points))
144 h_file.append(" return ::std::array<uint32_t, %d>{{%s}};" %
145 (len(back_points), ", ".join(back_points)))
146 h_file.append("}")
147
Austin Schuh7dfccf62018-03-03 21:28:14 -0800148 # Add the Make*Path functions.
149 h_file.append("")
150 cc_file.append("")
151 for name, segment in list(enumerate(graph_generate.unnamed_segments)) + [
152 (x.name, x) for x in graph_generate.named_segments
153 ]:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700154 h_file.append("::std::unique_ptr<Path> %s();" %
155 path_function_name(name))
156 cc_file.append("::std::unique_ptr<Path> %s() {" %
157 path_function_name(name))
Austin Schuh7dfccf62018-03-03 21:28:14 -0800158 cc_file.append(" return ::std::unique_ptr<Path>(new Path({")
159 for point in segment.ToThetaPoints():
160 cc_file.append(" {{%.12f, %.12f, %.12f," %
161 (numpy.pi / 2.0 - point[0],
162 numpy.pi / 2.0 - point[1], -point[2]))
163 cc_file.append(" %.12f, %.12f, %.12f}}," %
164 (-point[3], -point[4], -point[5]))
165 cc_file.append(" }));")
166 cc_file.append("}")
Austin Schuhb874fd32018-03-05 00:27:10 -0800167
Andrew Runkef9f57452018-03-04 18:19:36 -0800168 # Matrix of nodes
169 h_file.append("::std::vector<::Eigen::Matrix<double, 2, 1>> PointList();")
170
Austin Schuhb874fd32018-03-05 00:27:10 -0800171 cc_file.append(
172 "::std::vector<::Eigen::Matrix<double, 2, 1>> PointList() {")
Andrew Runkef9f57452018-03-04 18:19:36 -0800173 cc_file.append(" ::std::vector<::Eigen::Matrix<double, 2, 1>> points;")
Austin Schuhb874fd32018-03-05 00:27:10 -0800174 for point in graph_generate.points:
Andrew Runkef9f57452018-03-04 18:19:36 -0800175 cc_file.append(
Austin Schuhb874fd32018-03-05 00:27:10 -0800176 " points.push_back((::Eigen::Matrix<double, 2, 1>() << %.12s, %.12s).finished());"
177 % (numpy.pi / 2.0 - point[0][0], numpy.pi / 2.0 - point[0][1]))
Andrew Runkef9f57452018-03-04 18:19:36 -0800178 cc_file.append(" return points;")
179 cc_file.append("}")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800180
181 # Now create the MakeSearchGraph function.
182 h_file.append("")
183 h_file.append("// Builds a search graph.")
184 h_file.append("SearchGraph MakeSearchGraph("
Austin Schuh41c71e42018-04-04 20:11:20 -0700185 "::std::vector<TrajectoryAndParams> *trajectories,")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800186 h_file.append(" "
187 "const ::Eigen::Matrix<double, 2, 2> &alpha_unitizer,")
188 h_file.append(" double vmax);")
189 cc_file.append("SearchGraph MakeSearchGraph("
Austin Schuh41c71e42018-04-04 20:11:20 -0700190 "::std::vector<TrajectoryAndParams> *trajectories,")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800191 cc_file.append(" "
192 "const ::Eigen::Matrix<double, 2, 2> &alpha_unitizer,")
Ravago Jones5127ccc2022-07-31 16:32:45 -0700193 cc_file.append(" "
194 "double vmax) {")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800195 cc_file.append(" ::std::vector<SearchGraph::Edge> edges;")
196
197 index = 0
198 segments_and_names = list(enumerate(graph_generate.unnamed_segments)) + [
199 (x.name, x) for x in graph_generate.named_segments
200 ]
201
202 for name, segment in segments_and_names:
203 add_edge(cc_file, name, segment, index, False)
204 index += 1
205 add_edge(cc_file, name, segment, index, True)
206 index += 1
207
Ravago Jones5127ccc2022-07-31 16:32:45 -0700208 cc_file.append(" return SearchGraph(%d, ::std::move(edges));" %
209 len(graph_generate.points))
Austin Schuh7dfccf62018-03-03 21:28:14 -0800210 cc_file.append("}")
211
212 h_file.append("")
213 h_file.append("} // namespace arm")
214 h_file.append("} // namespace superstructure")
215 h_file.append("} // namespace control_loops")
216 h_file.append("} // namespace y2018")
217 h_file.append("")
218 h_file.append(
219 "#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
220
221 cc_file.append("} // namespace arm")
222 cc_file.append("} // namespace superstructure")
223 cc_file.append("} // namespace control_loops")
224 cc_file.append("} // namespace y2018")
225
226 if len(argv) == 3:
227 with open(argv[1], "w") as hfd:
228 hfd.write("\n".join(h_file))
229
230 with open(argv[2], "w") as ccfd:
231 ccfd.write("\n".join(cc_file))
232 else:
233 print("\n".join(h_file))
234 print("\n".join(cc_file))
235
236
237if __name__ == '__main__':
238 main(sys.argv)