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): |
milind-u | 4037bc7 | 2023-02-22 21:39:40 -0800 | [diff] [blame] | 16 | segment.VerifyPoints() |
| 17 | |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 18 | cc_file.append(" // Adding edge %d" % index) |
| 19 | vmax = "vmax" |
| 20 | if segment.vmax: |
| 21 | vmax = "::std::min(vmax, %f)" % segment.vmax |
| 22 | |
| 23 | alpha_unitizer = "alpha_unitizer" |
| 24 | if segment.alpha_unitizer is not None: |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 25 | alpha_unitizer = "(::Eigen::Matrix<double, 3, 3>() << %f, %f, %f, %f, %f, %f, %f, %f, %f).finished()" % ( |
| 26 | segment.alpha_unitizer[0, 0], |
| 27 | segment.alpha_unitizer[0, 1], |
| 28 | segment.alpha_unitizer[0, 2], |
| 29 | segment.alpha_unitizer[1, 0], |
| 30 | segment.alpha_unitizer[1, 1], |
| 31 | segment.alpha_unitizer[1, 2], |
| 32 | segment.alpha_unitizer[2, 0], |
| 33 | segment.alpha_unitizer[2, 1], |
| 34 | segment.alpha_unitizer[2, 2], |
| 35 | ) |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 36 | cc_file.append(" trajectories->emplace_back(%s," % (vmax)) |
| 37 | cc_file.append(" %s," % (alpha_unitizer)) |
| 38 | if reverse: |
| 39 | cc_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 40 | " Trajectory(dynamics, &hybrid_roll_joint_loop->plant(), Path::Reversed(%s()), 0.005));" |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 41 | % (path_function_name(str(name)))) |
| 42 | else: |
| 43 | cc_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 44 | " Trajectory(dynamics, &hybrid_roll_joint_loop->plant(), %s(), 0.005));" |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 45 | % (path_function_name(str(name)))) |
| 46 | |
| 47 | start_index = None |
| 48 | end_index = None |
Austin Schuh | 9a11ebd | 2023-02-26 14:16:31 -0800 | [diff] [blame] | 49 | for key in sorted(graph_paths.points.keys()): |
| 50 | point = graph_paths.points[key] |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 51 | if (point[:2] == segment.start |
| 52 | ).all() and point[2] == segment.alpha_rolls[0][1]: |
Austin Schuh | 9a11ebd | 2023-02-26 14:16:31 -0800 | [diff] [blame] | 53 | start_index = key |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 54 | if (point[:2] == segment.end |
| 55 | ).all() and point[2] == segment.alpha_rolls[-1][1]: |
Austin Schuh | 9a11ebd | 2023-02-26 14:16:31 -0800 | [diff] [blame] | 56 | end_index = key |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 57 | |
| 58 | if reverse: |
| 59 | start_index, end_index = end_index, start_index |
| 60 | |
| 61 | cc_file.append( |
| 62 | " edges.push_back(SearchGraph::Edge{%s(), %s()," % |
| 63 | (index_function_name(start_index), index_function_name(end_index))) |
| 64 | cc_file.append( |
| 65 | " (trajectories->back().trajectory.path().length() + 0.2)});" |
| 66 | ) |
| 67 | |
| 68 | # TODO(austin): Allow different vmaxes for different paths. |
| 69 | cc_file.append(" trajectories->back().trajectory.OptimizeTrajectory(") |
| 70 | cc_file.append(" trajectories->back().alpha_unitizer,") |
| 71 | cc_file.append(" trajectories->back().vmax);") |
| 72 | cc_file.append("") |
| 73 | |
| 74 | |
| 75 | def main(argv): |
| 76 | cc_file = [] |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 77 | cc_file.append("#include <memory>") |
| 78 | cc_file.append("") |
| 79 | cc_file.append( |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 80 | "#include \"frc971/control_loops/double_jointed_arm/graph.h\"") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 81 | cc_file.append( |
| 82 | "#include \"y2023/control_loops/superstructure/arm/generated_graph.h\"" |
| 83 | ) |
| 84 | cc_file.append( |
| 85 | "#include \"y2023/control_loops/superstructure/arm/trajectory.h\"") |
| 86 | cc_file.append( |
| 87 | "#include \"y2023/control_loops/superstructure/roll/integral_hybrid_roll_plant.h\"" |
| 88 | ) |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 89 | cc_file.append("") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 90 | |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 91 | cc_file.append("using frc971::control_loops::arm::SearchGraph;") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 92 | cc_file.append( |
| 93 | "using y2023::control_loops::superstructure::arm::Trajectory;") |
| 94 | cc_file.append("using y2023::control_loops::superstructure::arm::Path;") |
| 95 | cc_file.append("using y2023::control_loops::superstructure::arm::NSpline;") |
| 96 | cc_file.append( |
| 97 | "using y2023::control_loops::superstructure::arm::CosSpline;") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 98 | |
| 99 | cc_file.append("") |
| 100 | cc_file.append("namespace y2023 {") |
| 101 | cc_file.append("namespace control_loops {") |
| 102 | cc_file.append("namespace superstructure {") |
| 103 | cc_file.append("namespace arm {") |
| 104 | |
| 105 | h_file = [] |
| 106 | h_file.append( |
| 107 | "#ifndef Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_") |
| 108 | h_file.append( |
| 109 | "#define Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_") |
| 110 | h_file.append("") |
| 111 | h_file.append("#include <memory>") |
| 112 | h_file.append("") |
| 113 | h_file.append( |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 114 | "#include \"frc971/control_loops/double_jointed_arm/graph.h\"") |
| 115 | h_file.append( |
| 116 | "#include \"y2023/control_loops/superstructure/arm/arm_constants.h\"") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 117 | h_file.append( |
| 118 | "#include \"y2023/control_loops/superstructure/arm/trajectory.h\"") |
| 119 | |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 120 | h_file.append("") |
| 121 | h_file.append("namespace y2023 {") |
| 122 | h_file.append("namespace control_loops {") |
| 123 | h_file.append("namespace superstructure {") |
| 124 | h_file.append("namespace arm {") |
| 125 | |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 126 | h_file.append("using frc971::control_loops::arm::SearchGraph;") |
| 127 | h_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 128 | "using y2023::control_loops::superstructure::arm::Trajectory;") |
| 129 | h_file.append("using y2023::control_loops::superstructure::arm::Path;") |
| 130 | h_file.append( |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 131 | "using y2023::control_loops::superstructure::arm::kArmConstants;") |
| 132 | |
| 133 | h_file.append("") |
| 134 | h_file.append("struct TrajectoryAndParams {") |
| 135 | h_file.append(" TrajectoryAndParams(double new_vmax,") |
| 136 | h_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 137 | " const ::Eigen::Matrix<double, 3, 3> &new_alpha_unitizer," |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 138 | ) |
| 139 | h_file.append(" Trajectory &&new_trajectory)") |
| 140 | h_file.append(" : vmax(new_vmax),") |
| 141 | h_file.append(" alpha_unitizer(new_alpha_unitizer),") |
| 142 | h_file.append(" trajectory(::std::move(new_trajectory)) {}") |
| 143 | h_file.append(" double vmax;") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 144 | h_file.append(" ::Eigen::Matrix<double, 3, 3> alpha_unitizer;") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 145 | h_file.append(" Trajectory trajectory;") |
| 146 | h_file.append("};") |
| 147 | h_file.append("") |
| 148 | |
| 149 | # Now dump out the vertices and associated constexpr vertex name functions. |
Austin Schuh | 9a11ebd | 2023-02-26 14:16:31 -0800 | [diff] [blame] | 150 | for index, key in enumerate(sorted(graph_paths.points.keys())): |
| 151 | point = graph_paths.points[key] |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 152 | h_file.append("") |
| 153 | h_file.append("constexpr uint32_t %s() { return %d; }" % |
Austin Schuh | 9a11ebd | 2023-02-26 14:16:31 -0800 | [diff] [blame] | 154 | (index_function_name(key), index)) |
Austin Schuh | a138192 | 2023-02-26 22:27:05 -0800 | [diff] [blame] | 155 | h_file.append("::Eigen::Matrix<double, 3, 1> %sPoint();" % key) |
| 156 | cc_file.append("::Eigen::Matrix<double, 3, 1> %sPoint() {" % key) |
| 157 | cc_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 158 | " return (::Eigen::Matrix<double, 3, 1>() << %f, %f, %f).finished();" |
Austin Schuh | 9a11ebd | 2023-02-26 14:16:31 -0800 | [diff] [blame] | 159 | % (point[0], point[1], point[2])) |
Austin Schuh | a138192 | 2023-02-26 22:27:05 -0800 | [diff] [blame] | 160 | cc_file.append("}") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 161 | |
| 162 | front_points = [ |
| 163 | index_function_name(point[1]) + "()" |
| 164 | for point in graph_paths.front_points |
| 165 | ] |
| 166 | h_file.append("") |
| 167 | h_file.append("constexpr ::std::array<uint32_t, %d> FrontPoints() {" % |
| 168 | len(front_points)) |
| 169 | h_file.append(" return ::std::array<uint32_t, %d>{{%s}};" % |
| 170 | (len(front_points), ", ".join(front_points))) |
| 171 | h_file.append("}") |
| 172 | |
| 173 | back_points = [ |
| 174 | index_function_name(point[1]) + "()" |
| 175 | for point in graph_paths.back_points |
| 176 | ] |
| 177 | h_file.append("") |
| 178 | h_file.append("constexpr ::std::array<uint32_t, %d> BackPoints() {" % |
| 179 | len(back_points)) |
| 180 | h_file.append(" return ::std::array<uint32_t, %d>{{%s}};" % |
| 181 | (len(back_points), ", ".join(back_points))) |
| 182 | h_file.append("}") |
| 183 | |
| 184 | # Add the Make*Path functions. |
| 185 | h_file.append("") |
| 186 | cc_file.append("") |
| 187 | for name, segment in list(enumerate(graph_paths.unnamed_segments)) + [ |
| 188 | (x.name, x) for x in graph_paths.named_segments |
| 189 | ]: |
| 190 | h_file.append("::std::unique_ptr<Path> %s();" % |
| 191 | path_function_name(name)) |
| 192 | cc_file.append("::std::unique_ptr<Path> %s() {" % |
| 193 | path_function_name(name)) |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 194 | cc_file.append(" return ::std::unique_ptr<Path>(new Path(CosSpline{") |
| 195 | cc_file.append(" NSpline<4, 2>((Eigen::Matrix<double, 2, 4>() <<") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 196 | points = [ |
| 197 | segment.start, segment.control1, segment.control2, segment.end |
| 198 | ] |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 199 | cc_file.append(" " + |
| 200 | " ".join(["%.12f," % (p[0]) for p in points])) |
| 201 | cc_file.append(" " + |
| 202 | ", ".join(["%.12f" % (p[1]) for p in points])) |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 203 | |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 204 | cc_file.append(" ).finished()), {") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 205 | for alpha, roll in segment.alpha_rolls: |
| 206 | cc_file.append( |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 207 | " CosSpline::AlphaTheta{.alpha = %.12f, .theta = %.12f}," |
| 208 | % (alpha, roll)) |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 209 | cc_file.append(" }}));") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 210 | cc_file.append("}") |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 211 | cc_file.append("") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 212 | |
| 213 | # Matrix of nodes |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 214 | h_file.append("::std::vector<::Eigen::Matrix<double, 3, 1>> PointList();") |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 215 | |
| 216 | cc_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 217 | "::std::vector<::Eigen::Matrix<double, 3, 1>> PointList() {") |
| 218 | cc_file.append(" ::std::vector<::Eigen::Matrix<double, 3, 1>> points;") |
Austin Schuh | 9a11ebd | 2023-02-26 14:16:31 -0800 | [diff] [blame] | 219 | for key in sorted(graph_paths.points.keys()): |
| 220 | point = graph_paths.points[key] |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 221 | cc_file.append( |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 222 | " points.push_back((::Eigen::Matrix<double, 3, 1>() << %.12s, %.12s, %.12s).finished());" |
Austin Schuh | 9a11ebd | 2023-02-26 14:16:31 -0800 | [diff] [blame] | 223 | % (point[0], point[1], point[2])) |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 224 | 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,") |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 233 | h_file.append( |
| 234 | " const ::Eigen::Matrix<double, 3, 3> &alpha_unitizer," |
| 235 | ) |
| 236 | h_file.append(" double vmax,") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 237 | h_file.append( |
| 238 | "const StateFeedbackLoop<3, 1, 1, double, StateFeedbackHybridPlant<3, 1, 1>, HybridKalman<3, 1, 1>> *hybrid_roll_joint_loop);" |
| 239 | ) |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 240 | cc_file.append("") |
| 241 | cc_file.append("SearchGraph MakeSearchGraph(") |
| 242 | cc_file.append(" const frc971::control_loops::arm::Dynamics *dynamics,") |
| 243 | cc_file.append(" std::vector<TrajectoryAndParams> *trajectories,") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 244 | cc_file.append( |
Austin Schuh | 7b8adf9 | 2023-02-22 21:17:31 -0800 | [diff] [blame] | 245 | " const ::Eigen::Matrix<double, 3, 3> &alpha_unitizer, double vmax," |
| 246 | ) |
| 247 | cc_file.append( |
| 248 | " const StateFeedbackLoop<3, 1, 1, double, StateFeedbackHybridPlant<3, 1, 1>," |
| 249 | ) |
| 250 | cc_file.append( |
| 251 | " HybridKalman<3, 1, 1>> *hybrid_roll_joint_loop) {" |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 252 | ) |
milind-u | a7af516 | 2023-02-20 15:13:48 -0800 | [diff] [blame] | 253 | cc_file.append(" ::std::vector<SearchGraph::Edge> edges;") |
| 254 | |
| 255 | index = 0 |
| 256 | segments_and_names = list(enumerate(graph_paths.unnamed_segments)) + [ |
| 257 | (x.name, x) for x in graph_paths.named_segments |
| 258 | ] |
| 259 | |
| 260 | for name, segment in segments_and_names: |
| 261 | add_edge(cc_file, name, segment, index, False) |
| 262 | index += 1 |
| 263 | add_edge(cc_file, name, segment, index, True) |
| 264 | index += 1 |
| 265 | |
| 266 | cc_file.append(" return SearchGraph(%d, ::std::move(edges));" % |
| 267 | len(graph_paths.points)) |
| 268 | cc_file.append("}") |
| 269 | |
| 270 | h_file.append("") |
| 271 | h_file.append("} // namespace arm") |
| 272 | h_file.append("} // namespace superstructure") |
| 273 | h_file.append("} // namespace control_loops") |
| 274 | h_file.append("} // namespace y2023") |
| 275 | h_file.append("") |
| 276 | h_file.append( |
| 277 | "#endif // Y2023_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_GENERATED_GRAPH_H_") |
| 278 | |
| 279 | cc_file.append("} // namespace arm") |
| 280 | cc_file.append("} // namespace superstructure") |
| 281 | cc_file.append("} // namespace control_loops") |
| 282 | cc_file.append("} // namespace y2023") |
| 283 | |
| 284 | if len(argv) == 3: |
| 285 | with open(argv[1], "w") as hfd: |
| 286 | hfd.write("\n".join(h_file)) |
| 287 | |
| 288 | with open(argv[2], "w") as ccfd: |
| 289 | ccfd.write("\n".join(cc_file)) |
| 290 | else: |
| 291 | print("\n".join(h_file)) |
| 292 | print("\n".join(cc_file)) |
| 293 | |
| 294 | |
| 295 | if __name__ == '__main__': |
| 296 | main(sys.argv) |