blob: 2df9a337882dcdd5ff5c6b4e71f3844aa6f02042 [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)
17 if reverse:
18 cc_file.append(
19 " trajectories->emplace_back(Path::Reversed(%s()), 0.005);" %
20 (path_function_name(str(name))))
21 else:
22 cc_file.append(
23 " trajectories->emplace_back(%s(), 0.005);" %
24 (path_function_name(str(name))))
25
26 start_index = None
27 end_index = None
28 for point, name in graph_generate.points:
29 if (point == segment.start).all():
30 start_index = name
31 if (point == segment.end).all():
32 end_index = name
33
34 if reverse:
35 start_index, end_index = end_index, start_index
36
37 cc_file.append(" edges.push_back(SearchGraph::Edge{%s(), %s()," %
38 (index_function_name(start_index),
39 index_function_name(end_index)))
40 cc_file.append(
41 " trajectories->back().path().length()});"
42 )
43
44 # TODO(austin): Allow different vmaxes for different paths.
45 cc_file.append(
46 " trajectories->back().OptimizeTrajectory(alpha_unitizer, vmax);"
47 )
48 cc_file.append("")
49
50
51def main(argv):
52 cc_file = []
53 cc_file.append(
54 "#include \"y2018/control_loops/superstructure/arm/generated_graph.h\""
55 )
56 cc_file.append("")
57 cc_file.append("#include <memory>")
58 cc_file.append("")
59 cc_file.append(
60 "#include \"y2018/control_loops/superstructure/arm/trajectory.h\"")
61 cc_file.append(
62 "#include \"y2018/control_loops/superstructure/arm/graph.h\"")
63 cc_file.append("")
64 cc_file.append("namespace y2018 {")
65 cc_file.append("namespace control_loops {")
66 cc_file.append("namespace superstructure {")
67 cc_file.append("namespace arm {")
68
69 h_file = []
70 h_file.append(
71 "#ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
72 h_file.append(
73 "#define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
74 h_file.append("")
75 h_file.append("#include <memory>")
76 h_file.append("")
77 h_file.append(
78 "#include \"y2018/control_loops/superstructure/arm/trajectory.h\"")
79 h_file.append(
80 "#include \"y2018/control_loops/superstructure/arm/graph.h\"")
81 h_file.append("")
82 h_file.append("namespace y2018 {")
83 h_file.append("namespace control_loops {")
84 h_file.append("namespace superstructure {")
85 h_file.append("namespace arm {")
86
87 # Now dump out the vertices and associated constexpr vertex name functions.
88 for index, point in enumerate(graph_generate.points):
89 h_file.append("")
90 h_file.append("constexpr uint32_t %s() { return %d; }" %
91 (index_function_name(point[1]), index))
92 h_file.append(
93 "inline ::Eigen::Matrix<double, 2, 1> %sPoint() {" % point[1])
94 h_file.append(
95 " return (::Eigen::Matrix<double, 2, 1>() << %f, %f).finished();"
96 % (numpy.pi / 2.0 - point[0][0], numpy.pi / 2.0 - point[0][1]))
97 h_file.append("}")
98
99 # Add the Make*Path functions.
100 h_file.append("")
101 cc_file.append("")
102 for name, segment in list(enumerate(graph_generate.unnamed_segments)) + [
103 (x.name, x) for x in graph_generate.named_segments
104 ]:
105 h_file.append(
106 "::std::unique_ptr<Path> %s();" % path_function_name(name))
107 cc_file.append(
108 "::std::unique_ptr<Path> %s() {" % path_function_name(name))
109 cc_file.append(" return ::std::unique_ptr<Path>(new Path({")
110 for point in segment.ToThetaPoints():
111 cc_file.append(" {{%.12f, %.12f, %.12f," %
112 (numpy.pi / 2.0 - point[0],
113 numpy.pi / 2.0 - point[1], -point[2]))
114 cc_file.append(" %.12f, %.12f, %.12f}}," %
115 (-point[3], -point[4], -point[5]))
116 cc_file.append(" }));")
117 cc_file.append("}")
118
119 # Now create the MakeSearchGraph function.
120 h_file.append("")
121 h_file.append("// Builds a search graph.")
122 h_file.append("SearchGraph MakeSearchGraph("
123 "::std::vector<Trajectory> *trajectories,")
124 h_file.append(" "
125 "const ::Eigen::Matrix<double, 2, 2> &alpha_unitizer,")
126 h_file.append(" double vmax);")
127 cc_file.append("SearchGraph MakeSearchGraph("
128 "::std::vector<Trajectory> *trajectories,")
129 cc_file.append(" "
130 "const ::Eigen::Matrix<double, 2, 2> &alpha_unitizer,")
131 cc_file.append(" " "double vmax) {")
132 cc_file.append(" ::std::vector<SearchGraph::Edge> edges;")
133
134 index = 0
135 segments_and_names = list(enumerate(graph_generate.unnamed_segments)) + [
136 (x.name, x) for x in graph_generate.named_segments
137 ]
138
139 for name, segment in segments_and_names:
140 add_edge(cc_file, name, segment, index, False)
141 index += 1
142 add_edge(cc_file, name, segment, index, True)
143 index += 1
144
145 cc_file.append(" return SearchGraph(%d, ::std::move(edges));" % len(
146 graph_generate.points))
147 cc_file.append("}")
148
149 h_file.append("")
150 h_file.append("} // namespace arm")
151 h_file.append("} // namespace superstructure")
152 h_file.append("} // namespace control_loops")
153 h_file.append("} // namespace y2018")
154 h_file.append("")
155 h_file.append(
156 "#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
157
158 cc_file.append("} // namespace arm")
159 cc_file.append("} // namespace superstructure")
160 cc_file.append("} // namespace control_loops")
161 cc_file.append("} // namespace y2018")
162
163 if len(argv) == 3:
164 with open(argv[1], "w") as hfd:
165 hfd.write("\n".join(h_file))
166
167 with open(argv[2], "w") as ccfd:
168 ccfd.write("\n".join(cc_file))
169 else:
170 print("\n".join(h_file))
171 print("\n".join(cc_file))
172
173
174if __name__ == '__main__':
175 main(sys.argv)