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