milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 1 | from __future__ import print_function |
| 2 | import sys |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 3 | import numpy as np |
| 4 | import y2023.control_loops.python.graph_paths as graph_paths |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 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 | 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-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 23 | 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-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 34 | cc_file.append(" trajectories->emplace_back(%s," % (vmax)) |
| 35 | cc_file.append(" %s," % (alpha_unitizer)) |
| 36 | if reverse: |
| 37 | cc_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 38 | " Trajectory(dynamics, &hybrid_roll_joint_loop->plant(), Path::Reversed(%s()), 0.005));" |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 39 | % (path_function_name(str(name)))) |
| 40 | else: |
| 41 | cc_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 42 | " Trajectory(dynamics, &hybrid_roll_joint_loop->plant(), %s(), 0.005));" |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 43 | % (path_function_name(str(name)))) |
| 44 | |
| 45 | start_index = None |
| 46 | end_index = None |
| 47 | for point, name in graph_paths.points: |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 48 | if (point[:2] == segment.start |
| 49 | ).all() and point[2] == segment.alpha_rolls[0][1]: |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 50 | start_index = name |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 51 | if (point[:2] == segment.end |
| 52 | ).all() and point[2] == segment.alpha_rolls[-1][1]: |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 53 | 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 | |
| 72 | def main(argv): |
| 73 | cc_file = [] |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 74 | cc_file.append("#include <memory>") |
| 75 | cc_file.append("") |
| 76 | cc_file.append( |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 77 | "#include \"frc971/control_loops/double_jointed_arm/graph.h\"") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 78 | cc_file.append( |
| 79 | "#include \"y2023/control_loops/superstructure/arm/generated_graph.h\"" |
| 80 | ) |
| 81 | cc_file.append( |
| 82 | "#include \"y2023/control_loops/superstructure/arm/trajectory.h\"") |
| 83 | cc_file.append( |
| 84 | "#include \"y2023/control_loops/superstructure/roll/integral_hybrid_roll_plant.h\"" |
| 85 | ) |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 86 | cc_file.append("") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 87 | |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 88 | cc_file.append("using frc971::control_loops::arm::SearchGraph;") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 89 | 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-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 95 | |
| 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-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 111 | "#include \"frc971/control_loops/double_jointed_arm/graph.h\"") |
| 112 | h_file.append( |
| 113 | "#include \"y2023/control_loops/superstructure/arm/arm_constants.h\"") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 114 | h_file.append( |
| 115 | "#include \"y2023/control_loops/superstructure/arm/trajectory.h\"") |
| 116 | |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 117 | 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-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 123 | h_file.append("using frc971::control_loops::arm::SearchGraph;") |
| 124 | h_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 125 | "using y2023::control_loops::superstructure::arm::Trajectory;") |
| 126 | h_file.append("using y2023::control_loops::superstructure::arm::Path;") |
| 127 | h_file.append( |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 128 | "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-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 134 | " const ::Eigen::Matrix<double, 3, 3> &new_alpha_unitizer," |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 135 | ) |
| 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-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 141 | h_file.append(" ::Eigen::Matrix<double, 3, 3> alpha_unitizer;") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 142 | 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-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 151 | h_file.append("inline ::Eigen::Matrix<double, 3, 1> %sPoint() {" % |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 152 | point[1]) |
| 153 | h_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 154 | " return (::Eigen::Matrix<double, 3, 1>() << %f, %f, %f).finished();" |
milind-u | 060e4cf | 2023-02-22 00:08:52 -0800 | [diff] [blame] | 155 | % (point[0][0], point[0][1], point[0][2])) |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 156 | h_file.append("}") |
| 157 | |
| 158 | front_points = [ |
| 159 | index_function_name(point[1]) + "()" |
| 160 | for point in graph_paths.front_points |
| 161 | ] |
| 162 | h_file.append("") |
| 163 | h_file.append("constexpr ::std::array<uint32_t, %d> FrontPoints() {" % |
| 164 | len(front_points)) |
| 165 | h_file.append(" return ::std::array<uint32_t, %d>{{%s}};" % |
| 166 | (len(front_points), ", ".join(front_points))) |
| 167 | h_file.append("}") |
| 168 | |
| 169 | back_points = [ |
| 170 | index_function_name(point[1]) + "()" |
| 171 | for point in graph_paths.back_points |
| 172 | ] |
| 173 | h_file.append("") |
| 174 | h_file.append("constexpr ::std::array<uint32_t, %d> BackPoints() {" % |
| 175 | len(back_points)) |
| 176 | h_file.append(" return ::std::array<uint32_t, %d>{{%s}};" % |
| 177 | (len(back_points), ", ".join(back_points))) |
| 178 | h_file.append("}") |
| 179 | |
| 180 | # Add the Make*Path functions. |
| 181 | h_file.append("") |
| 182 | cc_file.append("") |
| 183 | for name, segment in list(enumerate(graph_paths.unnamed_segments)) + [ |
| 184 | (x.name, x) for x in graph_paths.named_segments |
| 185 | ]: |
| 186 | h_file.append("::std::unique_ptr<Path> %s();" % |
| 187 | path_function_name(name)) |
| 188 | cc_file.append("::std::unique_ptr<Path> %s() {" % |
| 189 | path_function_name(name)) |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 190 | cc_file.append(" return ::std::unique_ptr<Path>(new Path(CosSpline{") |
| 191 | cc_file.append(" NSpline<4, 2>((Eigen::Matrix<double, 2, 4>() <<") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 192 | points = [ |
| 193 | segment.start, segment.control1, segment.control2, segment.end |
| 194 | ] |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 195 | cc_file.append(" " + |
| 196 | " ".join(["%.12f," % (p[0]) for p in points])) |
| 197 | cc_file.append(" " + |
| 198 | ", ".join(["%.12f" % (p[1]) for p in points])) |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 199 | |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 200 | cc_file.append(" ).finished()), {") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 201 | for alpha, roll in segment.alpha_rolls: |
| 202 | cc_file.append( |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 203 | " CosSpline::AlphaTheta{.alpha = %.12f, .theta = %.12f}," |
| 204 | % (alpha, roll)) |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 205 | cc_file.append(" }}));") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 206 | cc_file.append("}") |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 207 | cc_file.append("") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 208 | |
| 209 | # Matrix of nodes |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 210 | h_file.append("::std::vector<::Eigen::Matrix<double, 3, 1>> PointList();") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 211 | |
| 212 | cc_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 213 | "::std::vector<::Eigen::Matrix<double, 3, 1>> PointList() {") |
| 214 | cc_file.append(" ::std::vector<::Eigen::Matrix<double, 3, 1>> points;") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 215 | for point in graph_paths.points: |
| 216 | cc_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 217 | " points.push_back((::Eigen::Matrix<double, 3, 1>() << %.12s, %.12s, %.12s).finished());" |
milind-u | 060e4cf | 2023-02-22 00:08:52 -0800 | [diff] [blame] | 218 | % (point[0][0], point[0][1], point[0][2])) |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 219 | cc_file.append(" return points;") |
| 220 | cc_file.append("}") |
| 221 | |
| 222 | # Now create the MakeSearchGraph function. |
| 223 | h_file.append("") |
| 224 | h_file.append("// Builds a search graph.") |
| 225 | h_file.append("SearchGraph MakeSearchGraph(" |
| 226 | "const frc971::control_loops::arm::Dynamics *dynamics, " |
| 227 | "::std::vector<TrajectoryAndParams> *trajectories,") |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 228 | h_file.append( |
| 229 | " const ::Eigen::Matrix<double, 3, 3> &alpha_unitizer," |
| 230 | ) |
| 231 | h_file.append(" double vmax,") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 232 | h_file.append( |
| 233 | "const StateFeedbackLoop<3, 1, 1, double, StateFeedbackHybridPlant<3, 1, 1>, HybridKalman<3, 1, 1>> *hybrid_roll_joint_loop);" |
| 234 | ) |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 235 | cc_file.append("") |
| 236 | cc_file.append("SearchGraph MakeSearchGraph(") |
| 237 | cc_file.append(" const frc971::control_loops::arm::Dynamics *dynamics,") |
| 238 | cc_file.append(" std::vector<TrajectoryAndParams> *trajectories,") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 239 | cc_file.append( |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 240 | " const ::Eigen::Matrix<double, 3, 3> &alpha_unitizer, double vmax," |
| 241 | ) |
| 242 | cc_file.append( |
| 243 | " const StateFeedbackLoop<3, 1, 1, double, StateFeedbackHybridPlant<3, 1, 1>," |
| 244 | ) |
| 245 | cc_file.append( |
| 246 | " HybridKalman<3, 1, 1>> *hybrid_roll_joint_loop) {" |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 247 | ) |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 248 | cc_file.append(" ::std::vector<SearchGraph::Edge> edges;") |
| 249 | |
| 250 | index = 0 |
| 251 | segments_and_names = list(enumerate(graph_paths.unnamed_segments)) + [ |
| 252 | (x.name, x) for x in graph_paths.named_segments |
| 253 | ] |
| 254 | |
| 255 | for name, segment in segments_and_names: |
| 256 | add_edge(cc_file, name, segment, index, False) |
| 257 | index += 1 |
| 258 | add_edge(cc_file, name, segment, index, True) |
| 259 | index += 1 |
| 260 | |
| 261 | cc_file.append(" return SearchGraph(%d, ::std::move(edges));" % |
| 262 | len(graph_paths.points)) |
| 263 | cc_file.append("}") |
| 264 | |
| 265 | h_file.append("") |
| 266 | h_file.append("} // namespace arm") |
| 267 | h_file.append("} // namespace superstructure") |
| 268 | h_file.append("} // namespace control_loops") |
| 269 | h_file.append("} // namespace y2023") |
| 270 | h_file.append("") |
| 271 | h_file.append( |
| 272 | "#endif // Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_") |
| 273 | |
| 274 | cc_file.append("} // namespace arm") |
| 275 | cc_file.append("} // namespace superstructure") |
| 276 | cc_file.append("} // namespace control_loops") |
| 277 | cc_file.append("} // namespace y2023") |
| 278 | |
| 279 | if len(argv) == 3: |
| 280 | with open(argv[1], "w") as hfd: |
| 281 | hfd.write("\n".join(h_file)) |
| 282 | |
| 283 | with open(argv[2], "w") as ccfd: |
| 284 | ccfd.write("\n".join(cc_file)) |
| 285 | else: |
| 286 | print("\n".join(h_file)) |
| 287 | print("\n".join(cc_file)) |
| 288 | |
| 289 | |
| 290 | if __name__ == '__main__': |
| 291 | main(sys.argv) |