blob: be267deb9c24bfc1055060dd3e37f011f0dfcefe [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(
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -080030 " Trajectory(dynamics, Path::Reversed(%s()), 0.005));"
Austin Schuh41c71e42018-04-04 20:11:20 -070031 % (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(
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -080034 " Trajectory(dynamics, %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(
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -080071 "#include \"frc971/control_loops/double_jointed_arm/trajectory.h\"")
Austin Schuh7dfccf62018-03-03 21:28:14 -080072 cc_file.append(
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -080073 "#include \"frc971/control_loops/double_jointed_arm/graph.h\"")
74
75 cc_file.append("using frc971::control_loops::arm::Trajectory;")
76 cc_file.append("using frc971::control_loops::arm::Path;")
77 cc_file.append("using frc971::control_loops::arm::SearchGraph;")
78
Austin Schuh7dfccf62018-03-03 21:28:14 -080079 cc_file.append("")
80 cc_file.append("namespace y2018 {")
81 cc_file.append("namespace control_loops {")
82 cc_file.append("namespace superstructure {")
83 cc_file.append("namespace arm {")
84
85 h_file = []
86 h_file.append(
87 "#ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
88 h_file.append(
89 "#define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
90 h_file.append("")
91 h_file.append("#include <memory>")
92 h_file.append("")
93 h_file.append(
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -080094 "#include \"frc971/control_loops/double_jointed_arm/trajectory.h\"")
Austin Schuh7dfccf62018-03-03 21:28:14 -080095 h_file.append(
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -080096 "#include \"frc971/control_loops/double_jointed_arm/graph.h\"")
97 h_file.append(
98 "#include \"frc971/control_loops/double_jointed_arm/dynamics.h\"")
Austin Schuh7dfccf62018-03-03 21:28:14 -080099 h_file.append("")
100 h_file.append("namespace y2018 {")
101 h_file.append("namespace control_loops {")
102 h_file.append("namespace superstructure {")
103 h_file.append("namespace arm {")
104
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -0800105 h_file.append("using frc971::control_loops::arm::Trajectory;")
106 h_file.append("using frc971::control_loops::arm::Path;")
107 h_file.append("using frc971::control_loops::arm::SearchGraph;")
108
Austin Schuh41c71e42018-04-04 20:11:20 -0700109 h_file.append("")
110 h_file.append("struct TrajectoryAndParams {")
111 h_file.append(" TrajectoryAndParams(double new_vmax,")
112 h_file.append(
113 " const ::Eigen::Matrix<double, 2, 2> &new_alpha_unitizer,"
114 )
115 h_file.append(" Trajectory &&new_trajectory)")
116 h_file.append(" : vmax(new_vmax),")
117 h_file.append(" alpha_unitizer(new_alpha_unitizer),")
118 h_file.append(" trajectory(::std::move(new_trajectory)) {}")
119 h_file.append(" double vmax;")
120 h_file.append(" ::Eigen::Matrix<double, 2, 2> alpha_unitizer;")
121 h_file.append(" Trajectory trajectory;")
122 h_file.append("};")
123 h_file.append("")
124
Austin Schuh7dfccf62018-03-03 21:28:14 -0800125 # Now dump out the vertices and associated constexpr vertex name functions.
126 for index, point in enumerate(graph_generate.points):
127 h_file.append("")
128 h_file.append("constexpr uint32_t %s() { return %d; }" %
129 (index_function_name(point[1]), index))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700130 h_file.append("inline ::Eigen::Matrix<double, 2, 1> %sPoint() {" %
131 point[1])
Austin Schuh7dfccf62018-03-03 21:28:14 -0800132 h_file.append(
133 " return (::Eigen::Matrix<double, 2, 1>() << %f, %f).finished();"
134 % (numpy.pi / 2.0 - point[0][0], numpy.pi / 2.0 - point[0][1]))
135 h_file.append("}")
136
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700137 front_points = [
Ravago Jones5127ccc2022-07-31 16:32:45 -0700138 index_function_name(point[1]) + "()"
139 for point in graph_generate.front_points
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700140 ]
141 h_file.append("")
142 h_file.append("constexpr ::std::array<uint32_t, %d> FrontPoints() {" %
143 len(front_points))
144 h_file.append(" return ::std::array<uint32_t, %d>{{%s}};" %
145 (len(front_points), ", ".join(front_points)))
146 h_file.append("}")
147
148 back_points = [
Ravago Jones5127ccc2022-07-31 16:32:45 -0700149 index_function_name(point[1]) + "()"
150 for point in graph_generate.back_points
Austin Schuh60cdb3e2018-04-06 21:52:32 -0700151 ]
152 h_file.append("")
153 h_file.append("constexpr ::std::array<uint32_t, %d> BackPoints() {" %
154 len(back_points))
155 h_file.append(" return ::std::array<uint32_t, %d>{{%s}};" %
156 (len(back_points), ", ".join(back_points)))
157 h_file.append("}")
158
Austin Schuh7dfccf62018-03-03 21:28:14 -0800159 # Add the Make*Path functions.
160 h_file.append("")
161 cc_file.append("")
162 for name, segment in list(enumerate(graph_generate.unnamed_segments)) + [
163 (x.name, x) for x in graph_generate.named_segments
164 ]:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700165 h_file.append("::std::unique_ptr<Path> %s();" %
166 path_function_name(name))
167 cc_file.append("::std::unique_ptr<Path> %s() {" %
168 path_function_name(name))
Austin Schuh7dfccf62018-03-03 21:28:14 -0800169 cc_file.append(" return ::std::unique_ptr<Path>(new Path({")
170 for point in segment.ToThetaPoints():
171 cc_file.append(" {{%.12f, %.12f, %.12f," %
172 (numpy.pi / 2.0 - point[0],
173 numpy.pi / 2.0 - point[1], -point[2]))
174 cc_file.append(" %.12f, %.12f, %.12f}}," %
175 (-point[3], -point[4], -point[5]))
176 cc_file.append(" }));")
177 cc_file.append("}")
Austin Schuhb874fd32018-03-05 00:27:10 -0800178
Andrew Runkef9f57452018-03-04 18:19:36 -0800179 # Matrix of nodes
180 h_file.append("::std::vector<::Eigen::Matrix<double, 2, 1>> PointList();")
181
Austin Schuhb874fd32018-03-05 00:27:10 -0800182 cc_file.append(
183 "::std::vector<::Eigen::Matrix<double, 2, 1>> PointList() {")
Andrew Runkef9f57452018-03-04 18:19:36 -0800184 cc_file.append(" ::std::vector<::Eigen::Matrix<double, 2, 1>> points;")
Austin Schuhb874fd32018-03-05 00:27:10 -0800185 for point in graph_generate.points:
Andrew Runkef9f57452018-03-04 18:19:36 -0800186 cc_file.append(
Austin Schuhb874fd32018-03-05 00:27:10 -0800187 " points.push_back((::Eigen::Matrix<double, 2, 1>() << %.12s, %.12s).finished());"
188 % (numpy.pi / 2.0 - point[0][0], numpy.pi / 2.0 - point[0][1]))
Andrew Runkef9f57452018-03-04 18:19:36 -0800189 cc_file.append(" return points;")
190 cc_file.append("}")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800191
192 # Now create the MakeSearchGraph function.
193 h_file.append("")
194 h_file.append("// Builds a search graph.")
195 h_file.append("SearchGraph MakeSearchGraph("
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -0800196 "const frc971::control_loops::arm::Dynamics *dynamics, "
Austin Schuh41c71e42018-04-04 20:11:20 -0700197 "::std::vector<TrajectoryAndParams> *trajectories,")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800198 h_file.append(" "
199 "const ::Eigen::Matrix<double, 2, 2> &alpha_unitizer,")
200 h_file.append(" double vmax);")
201 cc_file.append("SearchGraph MakeSearchGraph("
Maxwell Henderson8f0e07f2023-02-08 21:10:58 -0800202 "const frc971::control_loops::arm::Dynamics *dynamics, "
Austin Schuh41c71e42018-04-04 20:11:20 -0700203 "::std::vector<TrajectoryAndParams> *trajectories,")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800204 cc_file.append(" "
205 "const ::Eigen::Matrix<double, 2, 2> &alpha_unitizer,")
Ravago Jones5127ccc2022-07-31 16:32:45 -0700206 cc_file.append(" "
207 "double vmax) {")
Austin Schuh7dfccf62018-03-03 21:28:14 -0800208 cc_file.append(" ::std::vector<SearchGraph::Edge> edges;")
209
210 index = 0
211 segments_and_names = list(enumerate(graph_generate.unnamed_segments)) + [
212 (x.name, x) for x in graph_generate.named_segments
213 ]
214
215 for name, segment in segments_and_names:
216 add_edge(cc_file, name, segment, index, False)
217 index += 1
218 add_edge(cc_file, name, segment, index, True)
219 index += 1
220
Ravago Jones5127ccc2022-07-31 16:32:45 -0700221 cc_file.append(" return SearchGraph(%d, ::std::move(edges));" %
222 len(graph_generate.points))
Austin Schuh7dfccf62018-03-03 21:28:14 -0800223 cc_file.append("}")
224
225 h_file.append("")
226 h_file.append("} // namespace arm")
227 h_file.append("} // namespace superstructure")
228 h_file.append("} // namespace control_loops")
229 h_file.append("} // namespace y2018")
230 h_file.append("")
231 h_file.append(
232 "#endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
233
234 cc_file.append("} // namespace arm")
235 cc_file.append("} // namespace superstructure")
236 cc_file.append("} // namespace control_loops")
237 cc_file.append("} // namespace y2018")
238
239 if len(argv) == 3:
240 with open(argv[1], "w") as hfd:
241 hfd.write("\n".join(h_file))
242
243 with open(argv[2], "w") as ccfd:
244 ccfd.write("\n".join(cc_file))
245 else:
246 print("\n".join(h_file))
247 print("\n".join(cc_file))
248
249
250if __name__ == '__main__':
251 main(sys.argv)