blob: 54b77a9f9c5bd9a72f7a7fceafc1bcbaf8ca5133 [file] [log] [blame]
milind-ua7af5162023-02-20 15:13:48 -08001from __future__ import print_function
2import sys
milind-u18a901d2023-02-17 21:51:55 -08003import numpy as np
4import y2023.control_loops.python.graph_paths as graph_paths
milind-ua7af5162023-02-20 15:13:48 -08005
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 vmax = "vmax"
18 if segment.vmax:
19 vmax = "::std::min(vmax, %f)" % segment.vmax
20
21 alpha_unitizer = "alpha_unitizer"
22 if segment.alpha_unitizer is not None:
milind-u18a901d2023-02-17 21:51:55 -080023 alpha_unitizer = "(::Eigen::Matrix<double, 3, 3>() << %f, %f, %f, %f, %f, %f, %f, %f, %f).finished()" % (
24 segment.alpha_unitizer[0, 0],
25 segment.alpha_unitizer[0, 1],
26 segment.alpha_unitizer[0, 2],
27 segment.alpha_unitizer[1, 0],
28 segment.alpha_unitizer[1, 1],
29 segment.alpha_unitizer[1, 2],
30 segment.alpha_unitizer[2, 0],
31 segment.alpha_unitizer[2, 1],
32 segment.alpha_unitizer[2, 2],
33 )
milind-ua7af5162023-02-20 15:13:48 -080034 cc_file.append(" trajectories->emplace_back(%s," % (vmax))
35 cc_file.append(" %s," % (alpha_unitizer))
36 if reverse:
37 cc_file.append(
milind-u18a901d2023-02-17 21:51:55 -080038 " Trajectory(dynamics, &hybrid_roll_joint_loop->plant(), Path::Reversed(%s()), 0.005));"
milind-ua7af5162023-02-20 15:13:48 -080039 % (path_function_name(str(name))))
40 else:
41 cc_file.append(
milind-u18a901d2023-02-17 21:51:55 -080042 " Trajectory(dynamics, &hybrid_roll_joint_loop->plant(), %s(), 0.005));"
milind-ua7af5162023-02-20 15:13:48 -080043 % (path_function_name(str(name))))
44
45 start_index = None
46 end_index = None
47 for point, name in graph_paths.points:
milind-u18a901d2023-02-17 21:51:55 -080048 if (point[:2] == segment.start
49 ).all() and point[2] == segment.alpha_rolls[0][1]:
milind-ua7af5162023-02-20 15:13:48 -080050 start_index = name
milind-u18a901d2023-02-17 21:51:55 -080051 if (point[:2] == segment.end
52 ).all() and point[2] == segment.alpha_rolls[-1][1]:
milind-ua7af5162023-02-20 15:13:48 -080053 end_index = name
54
55 if reverse:
56 start_index, end_index = end_index, start_index
57
58 cc_file.append(
59 " edges.push_back(SearchGraph::Edge{%s(), %s()," %
60 (index_function_name(start_index), index_function_name(end_index)))
61 cc_file.append(
62 " (trajectories->back().trajectory.path().length() + 0.2)});"
63 )
64
65 # TODO(austin): Allow different vmaxes for different paths.
66 cc_file.append(" trajectories->back().trajectory.OptimizeTrajectory(")
67 cc_file.append(" trajectories->back().alpha_unitizer,")
68 cc_file.append(" trajectories->back().vmax);")
69 cc_file.append("")
70
71
72def main(argv):
73 cc_file = []
milind-ua7af5162023-02-20 15:13:48 -080074 cc_file.append("")
75 cc_file.append("#include <memory>")
76 cc_file.append("")
77 cc_file.append(
milind-ua7af5162023-02-20 15:13:48 -080078 "#include \"frc971/control_loops/double_jointed_arm/graph.h\"")
milind-u18a901d2023-02-17 21:51:55 -080079 cc_file.append(
80 "#include \"y2023/control_loops/superstructure/arm/generated_graph.h\""
81 )
82 cc_file.append(
83 "#include \"y2023/control_loops/superstructure/arm/trajectory.h\"")
84 cc_file.append(
85 "#include \"y2023/control_loops/superstructure/roll/integral_hybrid_roll_plant.h\""
86 )
milind-ua7af5162023-02-20 15:13:48 -080087
milind-ua7af5162023-02-20 15:13:48 -080088 cc_file.append("using frc971::control_loops::arm::SearchGraph;")
milind-u18a901d2023-02-17 21:51:55 -080089 cc_file.append(
90 "using y2023::control_loops::superstructure::arm::Trajectory;")
91 cc_file.append("using y2023::control_loops::superstructure::arm::Path;")
92 cc_file.append("using y2023::control_loops::superstructure::arm::NSpline;")
93 cc_file.append(
94 "using y2023::control_loops::superstructure::arm::CosSpline;")
milind-ua7af5162023-02-20 15:13:48 -080095
96 cc_file.append("")
97 cc_file.append("namespace y2023 {")
98 cc_file.append("namespace control_loops {")
99 cc_file.append("namespace superstructure {")
100 cc_file.append("namespace arm {")
101
102 h_file = []
103 h_file.append(
104 "#ifndef Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
105 h_file.append(
106 "#define Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
107 h_file.append("")
108 h_file.append("#include <memory>")
109 h_file.append("")
110 h_file.append(
milind-ua7af5162023-02-20 15:13:48 -0800111 "#include \"frc971/control_loops/double_jointed_arm/graph.h\"")
112 h_file.append(
113 "#include \"y2023/control_loops/superstructure/arm/arm_constants.h\"")
milind-u18a901d2023-02-17 21:51:55 -0800114 h_file.append(
115 "#include \"y2023/control_loops/superstructure/arm/trajectory.h\"")
116
milind-ua7af5162023-02-20 15:13:48 -0800117 h_file.append("")
118 h_file.append("namespace y2023 {")
119 h_file.append("namespace control_loops {")
120 h_file.append("namespace superstructure {")
121 h_file.append("namespace arm {")
122
milind-ua7af5162023-02-20 15:13:48 -0800123 h_file.append("using frc971::control_loops::arm::SearchGraph;")
124 h_file.append(
milind-u18a901d2023-02-17 21:51:55 -0800125 "using y2023::control_loops::superstructure::arm::Trajectory;")
126 h_file.append("using y2023::control_loops::superstructure::arm::Path;")
127 h_file.append(
milind-ua7af5162023-02-20 15:13:48 -0800128 "using y2023::control_loops::superstructure::arm::kArmConstants;")
129
130 h_file.append("")
131 h_file.append("struct TrajectoryAndParams {")
132 h_file.append(" TrajectoryAndParams(double new_vmax,")
133 h_file.append(
milind-u18a901d2023-02-17 21:51:55 -0800134 " const ::Eigen::Matrix<double, 3, 3> &new_alpha_unitizer,"
milind-ua7af5162023-02-20 15:13:48 -0800135 )
136 h_file.append(" Trajectory &&new_trajectory)")
137 h_file.append(" : vmax(new_vmax),")
138 h_file.append(" alpha_unitizer(new_alpha_unitizer),")
139 h_file.append(" trajectory(::std::move(new_trajectory)) {}")
140 h_file.append(" double vmax;")
milind-u18a901d2023-02-17 21:51:55 -0800141 h_file.append(" ::Eigen::Matrix<double, 3, 3> alpha_unitizer;")
milind-ua7af5162023-02-20 15:13:48 -0800142 h_file.append(" Trajectory trajectory;")
143 h_file.append("};")
144 h_file.append("")
145
146 # Now dump out the vertices and associated constexpr vertex name functions.
147 for index, point in enumerate(graph_paths.points):
148 h_file.append("")
149 h_file.append("constexpr uint32_t %s() { return %d; }" %
150 (index_function_name(point[1]), index))
milind-u18a901d2023-02-17 21:51:55 -0800151 h_file.append("inline ::Eigen::Matrix<double, 3, 1> %sPoint() {" %
milind-ua7af5162023-02-20 15:13:48 -0800152 point[1])
153 h_file.append(
milind-u18a901d2023-02-17 21:51:55 -0800154 " return (::Eigen::Matrix<double, 3, 1>() << %f, %f, %f).finished();"
155 % (np.pi / 2.0 - point[0][0], np.pi / 2.0 - point[0][1],
156 np.pi / 2.0 - point[0][2]))
milind-ua7af5162023-02-20 15:13:48 -0800157 h_file.append("}")
158
159 front_points = [
160 index_function_name(point[1]) + "()"
161 for point in graph_paths.front_points
162 ]
163 h_file.append("")
164 h_file.append("constexpr ::std::array<uint32_t, %d> FrontPoints() {" %
165 len(front_points))
166 h_file.append(" return ::std::array<uint32_t, %d>{{%s}};" %
167 (len(front_points), ", ".join(front_points)))
168 h_file.append("}")
169
170 back_points = [
171 index_function_name(point[1]) + "()"
172 for point in graph_paths.back_points
173 ]
174 h_file.append("")
175 h_file.append("constexpr ::std::array<uint32_t, %d> BackPoints() {" %
176 len(back_points))
177 h_file.append(" return ::std::array<uint32_t, %d>{{%s}};" %
178 (len(back_points), ", ".join(back_points)))
179 h_file.append("}")
180
181 # Add the Make*Path functions.
182 h_file.append("")
183 cc_file.append("")
184 for name, segment in list(enumerate(graph_paths.unnamed_segments)) + [
185 (x.name, x) for x in graph_paths.named_segments
186 ]:
187 h_file.append("::std::unique_ptr<Path> %s();" %
188 path_function_name(name))
189 cc_file.append("::std::unique_ptr<Path> %s() {" %
190 path_function_name(name))
milind-u18a901d2023-02-17 21:51:55 -0800191 cc_file.append(
192 " return ::std::unique_ptr<Path>(new Path(CosSpline{NSpline<4, 2>((Eigen::Matrix<double, 2, 4>() << "
193 )
194 points = [
195 segment.start, segment.control1, segment.control2, segment.end
196 ]
197 for i in range(len(points)):
198 cc_file.append("%.12f," % (np.pi / 2.0 - points[i][0]))
199 for i in range(len(points)):
200 cc_file.append("%.12f%s" % (np.pi / 2.0 - points[i][1],
201 ", " if i != len(points) - 1 else ""))
202
203 cc_file.append(").finished()), {")
204 for alpha, roll in segment.alpha_rolls:
205 cc_file.append(
206 "CosSpline::AlphaTheta{.alpha = %.12f, .theta = %.12f}" %
207 (alpha, np.pi / 2.0 - roll))
208 if alpha != segment.alpha_rolls[-1][0]:
209 cc_file.append(", ")
210 cc_file.append(" }}));")
milind-ua7af5162023-02-20 15:13:48 -0800211 cc_file.append("}")
212
213 # Matrix of nodes
milind-u18a901d2023-02-17 21:51:55 -0800214 h_file.append("::std::vector<::Eigen::Matrix<double, 3, 1>> PointList();")
milind-ua7af5162023-02-20 15:13:48 -0800215
216 cc_file.append(
milind-u18a901d2023-02-17 21:51:55 -0800217 "::std::vector<::Eigen::Matrix<double, 3, 1>> PointList() {")
218 cc_file.append(" ::std::vector<::Eigen::Matrix<double, 3, 1>> points;")
milind-ua7af5162023-02-20 15:13:48 -0800219 for point in graph_paths.points:
220 cc_file.append(
milind-u18a901d2023-02-17 21:51:55 -0800221 " points.push_back((::Eigen::Matrix<double, 3, 1>() << %.12s, %.12s, %.12s).finished());"
222 % (np.pi / 2.0 - point[0][0], np.pi / 2.0 - point[0][1],
223 np.pi / 2.0 - point[0][2]))
milind-ua7af5162023-02-20 15:13:48 -0800224 cc_file.append(" return points;")
225 cc_file.append("}")
226
227 # Now create the MakeSearchGraph function.
228 h_file.append("")
229 h_file.append("// Builds a search graph.")
230 h_file.append("SearchGraph MakeSearchGraph("
231 "const frc971::control_loops::arm::Dynamics *dynamics, "
232 "::std::vector<TrajectoryAndParams> *trajectories,")
233 h_file.append(" "
milind-u18a901d2023-02-17 21:51:55 -0800234 "const ::Eigen::Matrix<double, 3, 3> &alpha_unitizer,")
235 h_file.append(" "
236 "double vmax,")
237 h_file.append(
238 "const StateFeedbackLoop<3, 1, 1, double, StateFeedbackHybridPlant<3, 1, 1>, HybridKalman<3, 1, 1>> *hybrid_roll_joint_loop);"
239 )
milind-ua7af5162023-02-20 15:13:48 -0800240 cc_file.append("SearchGraph MakeSearchGraph("
241 "const frc971::control_loops::arm::Dynamics *dynamics, "
milind-u37385182023-02-20 15:07:28 -0800242 "::std::vector<TrajectoryAndParams> *trajectories,")
milind-ua7af5162023-02-20 15:13:48 -0800243 cc_file.append(" "
milind-u18a901d2023-02-17 21:51:55 -0800244 "const ::Eigen::Matrix<double, 3, 3> &alpha_unitizer,")
milind-ua7af5162023-02-20 15:13:48 -0800245 cc_file.append(" "
milind-u18a901d2023-02-17 21:51:55 -0800246 "double vmax,")
247 cc_file.append(
248 "const StateFeedbackLoop<3, 1, 1, double, StateFeedbackHybridPlant<3, 1, 1>, HybridKalman<3, 1, 1>> *hybrid_roll_joint_loop) {"
249 )
milind-ua7af5162023-02-20 15:13:48 -0800250 cc_file.append(" ::std::vector<SearchGraph::Edge> edges;")
251
252 index = 0
253 segments_and_names = list(enumerate(graph_paths.unnamed_segments)) + [
254 (x.name, x) for x in graph_paths.named_segments
255 ]
256
257 for name, segment in segments_and_names:
258 add_edge(cc_file, name, segment, index, False)
259 index += 1
260 add_edge(cc_file, name, segment, index, True)
261 index += 1
262
263 cc_file.append(" return SearchGraph(%d, ::std::move(edges));" %
264 len(graph_paths.points))
265 cc_file.append("}")
266
267 h_file.append("")
268 h_file.append("} // namespace arm")
269 h_file.append("} // namespace superstructure")
270 h_file.append("} // namespace control_loops")
271 h_file.append("} // namespace y2023")
272 h_file.append("")
273 h_file.append(
274 "#endif // Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_")
275
276 cc_file.append("} // namespace arm")
277 cc_file.append("} // namespace superstructure")
278 cc_file.append("} // namespace control_loops")
279 cc_file.append("} // namespace y2023")
280
281 if len(argv) == 3:
282 with open(argv[1], "w") as hfd:
283 hfd.write("\n".join(h_file))
284
285 with open(argv[2], "w") as ccfd:
286 ccfd.write("\n".join(cc_file))
287 else:
288 print("\n".join(h_file))
289 print("\n".join(cc_file))
290
291
292if __name__ == '__main__':
293 main(sys.argv)