Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 1 | from __future__ import print_function |
| 2 | import sys |
| 3 | import numpy |
| 4 | import graph_generate |
| 5 | |
| 6 | |
| 7 | def index_function_name(name): |
| 8 | return "%sIndex" % name |
| 9 | |
| 10 | |
| 11 | def path_function_name(name): |
| 12 | return "Make%sPath" % name |
| 13 | |
| 14 | |
| 15 | def add_edge(cc_file, name, segment, index, reverse): |
| 16 | cc_file.append(" // Adding edge %d" % index) |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 17 | vmax = "vmax" |
| 18 | if segment.vmax: |
| 19 | vmax = "::std::min(vmax, %f)" % segment.vmax |
Austin Schuh | cf96d32 | 2018-04-07 15:52:31 -0700 | [diff] [blame] | 20 | |
| 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 Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 26 | cc_file.append(" trajectories->emplace_back(%s," % (vmax)) |
| 27 | cc_file.append(" %s," % (alpha_unitizer)) |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 28 | if reverse: |
| 29 | cc_file.append( |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame^] | 30 | " Trajectory(dynamics, Path::Reversed(%s()), 0.005));" |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 31 | % (path_function_name(str(name)))) |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 32 | else: |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 33 | cc_file.append( |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame^] | 34 | " Trajectory(dynamics, %s(), 0.005));" |
| 35 | % (path_function_name(str(name)))) |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 36 | |
| 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 Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 48 | cc_file.append( |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 49 | " 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 Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 54 | |
| 55 | # TODO(austin): Allow different vmaxes for different paths. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 56 | cc_file.append(" trajectories->back().trajectory.OptimizeTrajectory(") |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 57 | cc_file.append(" trajectories->back().alpha_unitizer,") |
| 58 | cc_file.append(" trajectories->back().vmax);") |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 59 | cc_file.append("") |
| 60 | |
| 61 | |
| 62 | def 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 Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame^] | 71 | "#include \"frc971/control_loops/double_jointed_arm/trajectory.h\"") |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 72 | cc_file.append( |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame^] | 73 | "#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 Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 79 | 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 Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame^] | 94 | "#include \"frc971/control_loops/double_jointed_arm/trajectory.h\"") |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 95 | h_file.append( |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame^] | 96 | "#include \"frc971/control_loops/double_jointed_arm/graph.h\"") |
| 97 | h_file.append( |
| 98 | "#include \"frc971/control_loops/double_jointed_arm/dynamics.h\"") |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 99 | 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 Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame^] | 105 | 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 Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 109 | 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 Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 125 | # 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 Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 130 | h_file.append("inline ::Eigen::Matrix<double, 2, 1> %sPoint() {" % |
| 131 | point[1]) |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 132 | 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 Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 137 | front_points = [ |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 138 | index_function_name(point[1]) + "()" |
| 139 | for point in graph_generate.front_points |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 140 | ] |
| 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 Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 149 | index_function_name(point[1]) + "()" |
| 150 | for point in graph_generate.back_points |
Austin Schuh | 60cdb3e | 2018-04-06 21:52:32 -0700 | [diff] [blame] | 151 | ] |
| 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 Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 159 | # 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 Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 165 | 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 Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 169 | 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 Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 178 | |
Andrew Runke | f9f5745 | 2018-03-04 18:19:36 -0800 | [diff] [blame] | 179 | # Matrix of nodes |
| 180 | h_file.append("::std::vector<::Eigen::Matrix<double, 2, 1>> PointList();") |
| 181 | |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 182 | cc_file.append( |
| 183 | "::std::vector<::Eigen::Matrix<double, 2, 1>> PointList() {") |
Andrew Runke | f9f5745 | 2018-03-04 18:19:36 -0800 | [diff] [blame] | 184 | cc_file.append(" ::std::vector<::Eigen::Matrix<double, 2, 1>> points;") |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 185 | for point in graph_generate.points: |
Andrew Runke | f9f5745 | 2018-03-04 18:19:36 -0800 | [diff] [blame] | 186 | cc_file.append( |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 187 | " 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 Runke | f9f5745 | 2018-03-04 18:19:36 -0800 | [diff] [blame] | 189 | cc_file.append(" return points;") |
| 190 | cc_file.append("}") |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 191 | |
| 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 Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame^] | 196 | "const frc971::control_loops::arm::Dynamics *dynamics, " |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 197 | "::std::vector<TrajectoryAndParams> *trajectories,") |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 198 | 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 Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame^] | 202 | "const frc971::control_loops::arm::Dynamics *dynamics, " |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 203 | "::std::vector<TrajectoryAndParams> *trajectories,") |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 204 | cc_file.append(" " |
| 205 | "const ::Eigen::Matrix<double, 2, 2> &alpha_unitizer,") |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 206 | cc_file.append(" " |
| 207 | "double vmax) {") |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 208 | 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 Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 221 | cc_file.append(" return SearchGraph(%d, ::std::move(edges));" % |
| 222 | len(graph_generate.points)) |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 223 | 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 | |
| 250 | if __name__ == '__main__': |
| 251 | main(sys.argv) |