justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 1 | #include <symengine/add.h> |
| 2 | #include <symengine/matrix.h> |
| 3 | #include <symengine/number.h> |
| 4 | #include <symengine/printers.h> |
| 5 | #include <symengine/real_double.h> |
| 6 | #include <symengine/simplify.h> |
| 7 | #include <symengine/solve.h> |
| 8 | #include <symengine/symbol.h> |
| 9 | |
| 10 | #include <array> |
| 11 | #include <cmath> |
| 12 | #include <numbers> |
| 13 | #include <utility> |
| 14 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 15 | #include "absl/flags/flag.h" |
| 16 | #include "absl/log/check.h" |
| 17 | #include "absl/log/log.h" |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 18 | #include "absl/strings/str_format.h" |
| 19 | #include "absl/strings/str_join.h" |
| 20 | #include "absl/strings/str_replace.h" |
| 21 | #include "absl/strings/substitute.h" |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 22 | |
| 23 | #include "aos/init.h" |
| 24 | #include "aos/util/file.h" |
| 25 | #include "frc971/control_loops/swerve/motors.h" |
| 26 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 27 | ABSL_FLAG(std::string, output_base, "", |
| 28 | "Path to strip off the front of the output paths."); |
| 29 | ABSL_FLAG(std::string, cc_output_path, "", |
| 30 | "Path to write generated cc code to"); |
| 31 | ABSL_FLAG(std::string, h_output_path, "", |
| 32 | "Path to write generated header code to"); |
| 33 | ABSL_FLAG(std::string, py_output_path, "", |
| 34 | "Path to write generated py code to"); |
| 35 | ABSL_FLAG(std::string, casadi_py_output_path, "", |
| 36 | "Path to write casadi generated py code to"); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 37 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 38 | ABSL_FLAG(bool, symbolic, false, "If true, write everything out symbolically."); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 39 | |
justinT21 | 942892b | 2024-07-02 22:33:50 -0700 | [diff] [blame] | 40 | using SymEngine::abs; |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 41 | using SymEngine::add; |
| 42 | using SymEngine::atan2; |
| 43 | using SymEngine::Basic; |
| 44 | using SymEngine::ccode; |
| 45 | using SymEngine::cos; |
| 46 | using SymEngine::DenseMatrix; |
| 47 | using SymEngine::div; |
| 48 | using SymEngine::Inf; |
| 49 | using SymEngine::integer; |
| 50 | using SymEngine::map_basic_basic; |
| 51 | using SymEngine::minus_one; |
| 52 | using SymEngine::neg; |
| 53 | using SymEngine::NegInf; |
| 54 | using SymEngine::pow; |
| 55 | using SymEngine::RCP; |
| 56 | using SymEngine::real_double; |
| 57 | using SymEngine::RealDouble; |
| 58 | using SymEngine::Set; |
| 59 | using SymEngine::simplify; |
| 60 | using SymEngine::sin; |
| 61 | using SymEngine::solve; |
| 62 | using SymEngine::symbol; |
| 63 | using SymEngine::Symbol; |
| 64 | |
| 65 | namespace frc971::control_loops::swerve { |
| 66 | |
| 67 | // State per module. |
| 68 | struct Module { |
| 69 | RCP<const Symbol> Is; |
| 70 | |
| 71 | RCP<const Symbol> Id; |
| 72 | |
| 73 | RCP<const Symbol> thetas; |
| 74 | RCP<const Symbol> omegas; |
| 75 | RCP<const Symbol> alphas; |
| 76 | RCP<const Basic> alphas_eqn; |
| 77 | |
| 78 | RCP<const Symbol> thetad; |
| 79 | RCP<const Symbol> omegad; |
| 80 | RCP<const Symbol> alphad; |
| 81 | RCP<const Basic> alphad_eqn; |
| 82 | |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 83 | DenseMatrix contact_patch_velocity; |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 84 | DenseMatrix wheel_ground_velocity; |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 85 | DenseMatrix wheel_slip_velocity; |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 86 | RCP<const Basic> slip_angle; |
| 87 | RCP<const Basic> slip_ratio; |
| 88 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 89 | RCP<const Basic> Ms; |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 90 | RCP<const Basic> Fwx; |
| 91 | RCP<const Basic> Fwy; |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 92 | DenseMatrix F; |
| 93 | DenseMatrix mounting_location; |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 94 | |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 95 | // Acceleration contribution from this module. |
| 96 | DenseMatrix accel; |
| 97 | RCP<const Basic> angular_accel; |
| 98 | }; |
| 99 | |
| 100 | class SwerveSimulation { |
| 101 | public: |
| 102 | SwerveSimulation() : drive_motor_(KrakenFOC()), steer_motor_(KrakenFOC()) { |
| 103 | auto fx = symbol("fx"); |
| 104 | auto fy = symbol("fy"); |
| 105 | auto moment = symbol("moment"); |
| 106 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 107 | if (absl::GetFlag(FLAGS_symbolic)) { |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 108 | Cx_ = symbol("Cx"); |
| 109 | Cy_ = symbol("Cy"); |
| 110 | |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 111 | rw_ = symbol("rw"); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 112 | |
| 113 | m_ = symbol("m"); |
| 114 | J_ = symbol("J"); |
| 115 | |
| 116 | Gd1_ = symbol("Gd1"); |
| 117 | rs_ = symbol("rs"); |
| 118 | rp_ = symbol("rp"); |
| 119 | Gd2_ = symbol("Gd2"); |
| 120 | |
| 121 | rb1_ = symbol("rb1"); |
| 122 | rb2_ = symbol("rb2"); |
| 123 | |
| 124 | Gd2_ = symbol("Gd3"); |
| 125 | Gd_ = symbol("Gd"); |
| 126 | |
| 127 | Js_ = symbol("Js"); |
| 128 | |
| 129 | Gs_ = symbol("Gs"); |
| 130 | wb_ = symbol("wb"); |
| 131 | |
| 132 | Jdm_ = symbol("Jdm"); |
| 133 | Jsm_ = symbol("Jsm"); |
| 134 | Kts_ = symbol("Kts"); |
| 135 | Ktd_ = symbol("Ktd"); |
| 136 | |
| 137 | robot_width_ = symbol("robot_width"); |
| 138 | |
| 139 | caster_ = symbol("caster"); |
| 140 | contact_patch_length_ = symbol("Lcp"); |
| 141 | } else { |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 142 | Cx_ = real_double(25.0 * 9.8 / 4.0 / 0.05); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 143 | Cy_ = real_double(5 * 9.8 / 0.05 / 4.0); |
| 144 | |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 145 | rw_ = real_double(2 * 0.0254); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 146 | |
| 147 | m_ = real_double(25.0); // base is 20 kg without battery |
| 148 | J_ = real_double(6.0); |
| 149 | |
| 150 | Gd1_ = real_double(12.0 / 42.0); |
| 151 | rs_ = real_double(28.0 / 20.0 / 2.0); |
| 152 | rp_ = real_double(18.0 / 20.0 / 2.0); |
| 153 | Gd2_ = div(rs_, rp_); |
| 154 | |
| 155 | // 15 / 45 bevel ratio, calculated using python script ported over to |
| 156 | // GetBevelPitchRadius(double |
| 157 | // TODO(Justin): Use the function instead of computed constantss |
| 158 | rb1_ = real_double(0.3805473); |
| 159 | rb2_ = real_double(1.14164); |
| 160 | |
| 161 | Gd3_ = div(rb1_, rb2_); |
| 162 | Gd_ = mul(mul(Gd1_, Gd2_), Gd3_); |
| 163 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 164 | Js_ = real_double(0.001); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 165 | |
| 166 | Gs_ = real_double(35.0 / 468.0); |
| 167 | wb_ = real_double(0.725); |
| 168 | |
| 169 | Jdm_ = real_double(drive_motor_.motor_inertia); |
| 170 | Jsm_ = real_double(steer_motor_.motor_inertia); |
| 171 | Kts_ = real_double(steer_motor_.Kt); |
| 172 | Ktd_ = real_double(drive_motor_.Kt); |
| 173 | |
| 174 | robot_width_ = real_double(24.75 * 0.0254); |
| 175 | |
| 176 | caster_ = real_double(0.01); |
| 177 | contact_patch_length_ = real_double(0.02); |
| 178 | } |
| 179 | |
| 180 | x_ = symbol("x"); |
| 181 | y_ = symbol("y"); |
| 182 | theta_ = symbol("theta"); |
| 183 | |
| 184 | vx_ = symbol("vx"); |
| 185 | vy_ = symbol("vy"); |
| 186 | omega_ = symbol("omega"); |
| 187 | |
| 188 | ax_ = symbol("ax"); |
| 189 | ay_ = symbol("ay"); |
| 190 | atheta_ = symbol("atheta"); |
| 191 | |
| 192 | // Now, compute the accelerations due to the disturbance forces. |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 193 | DenseMatrix external_accel = DenseMatrix(2, 1, {div(fx, m_), div(fy, m_)}); |
| 194 | |
| 195 | // And compute the physics contributions from each module. |
| 196 | modules_[0] = ModulePhysics( |
| 197 | 0, DenseMatrix( |
| 198 | 2, 1, |
| 199 | {div(robot_width_, integer(2)), div(robot_width_, integer(2))})); |
| 200 | modules_[1] = |
| 201 | ModulePhysics(1, DenseMatrix(2, 1, |
| 202 | {div(robot_width_, integer(-2)), |
| 203 | div(robot_width_, integer(2))})); |
| 204 | modules_[2] = |
| 205 | ModulePhysics(2, DenseMatrix(2, 1, |
| 206 | {div(robot_width_, integer(-2)), |
| 207 | div(robot_width_, integer(-2))})); |
| 208 | modules_[3] = |
| 209 | ModulePhysics(3, DenseMatrix(2, 1, |
| 210 | {div(robot_width_, integer(2)), |
| 211 | div(robot_width_, integer(-2))})); |
| 212 | |
| 213 | // And convert them into the overall robot contribution. |
| 214 | DenseMatrix temp0 = DenseMatrix(2, 1); |
| 215 | DenseMatrix temp1 = DenseMatrix(2, 1); |
| 216 | DenseMatrix temp2 = DenseMatrix(2, 1); |
| 217 | accel_ = DenseMatrix(2, 1); |
| 218 | |
| 219 | add_dense_dense(modules_[0].accel, external_accel, temp0); |
| 220 | add_dense_dense(temp0, modules_[1].accel, temp1); |
| 221 | add_dense_dense(temp1, modules_[2].accel, temp2); |
| 222 | add_dense_dense(temp2, modules_[3].accel, accel_); |
| 223 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 224 | angular_accel_ = |
| 225 | add(div(moment, J_), |
| 226 | add(add(modules_[0].angular_accel, modules_[1].angular_accel), |
| 227 | add(modules_[2].angular_accel, modules_[3].angular_accel))); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 228 | |
| 229 | VLOG(1) << "accel(0, 0) = " << ccode(*accel_.get(0, 0)); |
| 230 | VLOG(1) << "accel(1, 0) = " << ccode(*accel_.get(1, 0)); |
| 231 | VLOG(1) << "angular_accel = " << ccode(*angular_accel_); |
| 232 | } |
| 233 | |
justinT21 | 942892b | 2024-07-02 22:33:50 -0700 | [diff] [blame] | 234 | // Writes the physics out to the provided .py path. |
| 235 | void WritePy(std::string_view py_path) { |
| 236 | std::vector<std::string> result_py; |
| 237 | |
| 238 | result_py.emplace_back("#!/usr/bin/python3"); |
| 239 | result_py.emplace_back(""); |
| 240 | result_py.emplace_back("import numpy"); |
justinT21 | 942892b | 2024-07-02 22:33:50 -0700 | [diff] [blame] | 241 | result_py.emplace_back(""); |
| 242 | |
justinT21 | 942892b | 2024-07-02 22:33:50 -0700 | [diff] [blame] | 243 | result_py.emplace_back("def swerve_physics(t, X, U_func):"); |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 244 | result_py.emplace_back(" def atan2(y, x):"); |
| 245 | result_py.emplace_back(" if x < 0:"); |
| 246 | result_py.emplace_back(" return -numpy.atan2(y, x)"); |
| 247 | result_py.emplace_back(" else:"); |
| 248 | result_py.emplace_back(" return numpy.atan2(y, x)"); |
| 249 | result_py.emplace_back(" sin = numpy.sin"); |
| 250 | result_py.emplace_back(" cos = numpy.cos"); |
| 251 | result_py.emplace_back(" fabs = numpy.fabs"); |
| 252 | |
justinT21 | 942892b | 2024-07-02 22:33:50 -0700 | [diff] [blame] | 253 | result_py.emplace_back(" result = numpy.empty([25, 1])"); |
| 254 | result_py.emplace_back(" X = X.reshape(25, 1)"); |
| 255 | result_py.emplace_back(" U = U_func(X)"); |
| 256 | result_py.emplace_back(""); |
| 257 | |
| 258 | // Start by writing out variables matching each of the symbol names we use |
| 259 | // so we don't have to modify the computed equations too much. |
| 260 | for (size_t m = 0; m < kNumModules; ++m) { |
| 261 | result_py.emplace_back( |
| 262 | absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 4)); |
| 263 | result_py.emplace_back( |
| 264 | absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 4 + 2)); |
| 265 | result_py.emplace_back( |
| 266 | absl::Substitute(" omegad$0 = X[$1, 0]", m, m * 4 + 3)); |
| 267 | } |
| 268 | |
| 269 | result_py.emplace_back( |
| 270 | absl::Substitute(" theta = X[$0, 0]", kNumModules * 4 + 2)); |
| 271 | result_py.emplace_back( |
| 272 | absl::Substitute(" vx = X[$0, 0]", kNumModules * 4 + 3)); |
| 273 | result_py.emplace_back( |
| 274 | absl::Substitute(" vy = X[$0, 0]", kNumModules * 4 + 4)); |
| 275 | result_py.emplace_back( |
| 276 | absl::Substitute(" omega = X[$0, 0]", kNumModules * 4 + 5)); |
| 277 | |
| 278 | result_py.emplace_back( |
| 279 | absl::Substitute(" fx = X[$0, 0]", kNumModules * 4 + 6)); |
| 280 | result_py.emplace_back( |
| 281 | absl::Substitute(" fy = X[$0, 0]", kNumModules * 4 + 7)); |
| 282 | result_py.emplace_back( |
| 283 | absl::Substitute(" moment = X[$0, 0]", kNumModules * 4 + 8)); |
| 284 | |
| 285 | // Now do the same for the inputs. |
| 286 | for (size_t m = 0; m < kNumModules; ++m) { |
| 287 | result_py.emplace_back(absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2)); |
| 288 | result_py.emplace_back( |
| 289 | absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1)); |
| 290 | } |
| 291 | |
| 292 | result_py.emplace_back(""); |
| 293 | |
| 294 | // And then write out the derivative of each state. |
| 295 | for (size_t m = 0; m < kNumModules; ++m) { |
| 296 | result_py.emplace_back( |
| 297 | absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m)); |
| 298 | result_py.emplace_back( |
| 299 | absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m)); |
| 300 | |
| 301 | result_py.emplace_back(absl::Substitute( |
| 302 | " result[$0, 0] = $1", m * 4 + 2, ccode(*modules_[m].alphas_eqn))); |
| 303 | result_py.emplace_back(absl::Substitute( |
| 304 | " result[$0, 0] = $1", m * 4 + 3, ccode(*modules_[m].alphad_eqn))); |
| 305 | } |
| 306 | |
| 307 | result_py.emplace_back( |
| 308 | absl::Substitute(" result[$0, 0] = vx", kNumModules * 4)); |
| 309 | result_py.emplace_back( |
| 310 | absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1)); |
| 311 | result_py.emplace_back( |
| 312 | absl::Substitute(" result[$0, 0] = omega", kNumModules * 4 + 2)); |
| 313 | |
| 314 | result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1", |
| 315 | kNumModules * 4 + 3, |
| 316 | ccode(*accel_.get(0, 0)))); |
| 317 | result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1", |
| 318 | kNumModules * 4 + 4, |
| 319 | ccode(*accel_.get(1, 0)))); |
| 320 | result_py.emplace_back(absl::Substitute( |
| 321 | " result[$0, 0] = $1", kNumModules * 4 + 5, ccode(*angular_accel_))); |
| 322 | |
| 323 | result_py.emplace_back( |
| 324 | absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6)); |
| 325 | result_py.emplace_back( |
| 326 | absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7)); |
| 327 | result_py.emplace_back( |
| 328 | absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8)); |
| 329 | |
| 330 | result_py.emplace_back(""); |
| 331 | result_py.emplace_back(" return result.reshape(25,)\n"); |
| 332 | |
| 333 | aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n")); |
| 334 | } |
| 335 | |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 336 | // Writes the physics out to the provided .cc and .h path. |
| 337 | void Write(std::string_view cc_path, std::string_view h_path) { |
| 338 | std::vector<std::string> result_cc; |
| 339 | std::vector<std::string> result_h; |
| 340 | |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 341 | std::string_view include_guard_stripped = h_path; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 342 | CHECK(absl::ConsumePrefix(&include_guard_stripped, |
| 343 | absl::GetFlag(FLAGS_output_base))); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 344 | std::string include_guard = |
| 345 | absl::StrReplaceAll(absl::AsciiStrToUpper(include_guard_stripped), |
| 346 | {{"/", "_"}, {".", "_"}}); |
| 347 | |
| 348 | // Write out the header. |
| 349 | result_h.emplace_back(absl::Substitute("#ifndef $0_", include_guard)); |
| 350 | result_h.emplace_back(absl::Substitute("#define $0_", include_guard)); |
| 351 | result_h.emplace_back(""); |
| 352 | result_h.emplace_back("#include <Eigen/Dense>"); |
| 353 | result_h.emplace_back(""); |
| 354 | result_h.emplace_back("namespace frc971::control_loops::swerve {"); |
| 355 | result_h.emplace_back(""); |
| 356 | result_h.emplace_back("// Returns the derivative of our state vector"); |
| 357 | result_h.emplace_back("// [thetas0, thetad0, omegas0, omegad0,"); |
| 358 | result_h.emplace_back("// thetas1, thetad1, omegas1, omegad1,"); |
| 359 | result_h.emplace_back("// thetas2, thetad2, omegas2, omegad2,"); |
| 360 | result_h.emplace_back("// thetas3, thetad3, omegas3, omegad3,"); |
| 361 | result_h.emplace_back("// x, y, theta, vx, vy, omega,"); |
| 362 | result_h.emplace_back("// Fx, Fy, Moment]"); |
| 363 | result_h.emplace_back("Eigen::Matrix<double, 25, 1> SwervePhysics("); |
| 364 | result_h.emplace_back( |
| 365 | " Eigen::Map<const Eigen::Matrix<double, 25, 1>> X,"); |
| 366 | result_h.emplace_back( |
| 367 | " Eigen::Map<const Eigen::Matrix<double, 8, 1>> U);"); |
| 368 | result_h.emplace_back(""); |
| 369 | result_h.emplace_back("} // namespace frc971::control_loops::swerve"); |
| 370 | result_h.emplace_back(""); |
| 371 | result_h.emplace_back(absl::Substitute("#endif // $0_", include_guard)); |
| 372 | |
| 373 | // Write out the .cc |
| 374 | result_cc.emplace_back( |
| 375 | absl::Substitute("#include \"$0\"", include_guard_stripped)); |
| 376 | result_cc.emplace_back(""); |
| 377 | result_cc.emplace_back("#include <cmath>"); |
| 378 | result_cc.emplace_back(""); |
| 379 | result_cc.emplace_back("namespace frc971::control_loops::swerve {"); |
| 380 | result_cc.emplace_back(""); |
| 381 | result_cc.emplace_back("Eigen::Matrix<double, 25, 1> SwervePhysics("); |
| 382 | result_cc.emplace_back( |
| 383 | " Eigen::Map<const Eigen::Matrix<double, 25, 1>> X,"); |
| 384 | result_cc.emplace_back( |
| 385 | " Eigen::Map<const Eigen::Matrix<double, 8, 1>> U) {"); |
| 386 | result_cc.emplace_back(" Eigen::Matrix<double, 25, 1> result;"); |
| 387 | |
| 388 | // Start by writing out variables matching each of the symbol names we use |
| 389 | // so we don't have to modify the computed equations too much. |
| 390 | for (size_t m = 0; m < kNumModules; ++m) { |
| 391 | result_cc.emplace_back( |
| 392 | absl::Substitute(" const double thetas$0 = X($1, 0);", m, m * 4)); |
| 393 | result_cc.emplace_back(absl::Substitute( |
| 394 | " const double omegas$0 = X($1, 0);", m, m * 4 + 2)); |
| 395 | result_cc.emplace_back(absl::Substitute( |
| 396 | " const double omegad$0 = X($1, 0);", m, m * 4 + 3)); |
| 397 | } |
| 398 | |
| 399 | result_cc.emplace_back(absl::Substitute(" const double theta = X($0, 0);", |
| 400 | kNumModules * 4 + 2)); |
| 401 | result_cc.emplace_back( |
| 402 | absl::Substitute(" const double vx = X($0, 0);", kNumModules * 4 + 3)); |
| 403 | result_cc.emplace_back( |
| 404 | absl::Substitute(" const double vy = X($0, 0);", kNumModules * 4 + 4)); |
| 405 | result_cc.emplace_back(absl::Substitute(" const double omega = X($0, 0);", |
| 406 | kNumModules * 4 + 5)); |
| 407 | |
| 408 | result_cc.emplace_back( |
| 409 | absl::Substitute(" const double fx = X($0, 0);", kNumModules * 4 + 6)); |
| 410 | result_cc.emplace_back( |
| 411 | absl::Substitute(" const double fy = X($0, 0);", kNumModules * 4 + 7)); |
| 412 | result_cc.emplace_back(absl::Substitute(" const double moment = X($0, 0);", |
| 413 | kNumModules * 4 + 8)); |
| 414 | |
| 415 | // Now do the same for the inputs. |
| 416 | for (size_t m = 0; m < kNumModules; ++m) { |
| 417 | result_cc.emplace_back( |
| 418 | absl::Substitute(" const double Is$0 = U($1, 0);", m, m * 2)); |
| 419 | result_cc.emplace_back( |
| 420 | absl::Substitute(" const double Id$0 = U($1, 0);", m, m * 2 + 1)); |
| 421 | } |
| 422 | |
| 423 | result_cc.emplace_back(""); |
| 424 | |
| 425 | // And then write out the derivative of each state. |
| 426 | for (size_t m = 0; m < kNumModules; ++m) { |
| 427 | result_cc.emplace_back( |
| 428 | absl::Substitute(" result($0, 0) = omegas$1;", m * 4, m)); |
| 429 | result_cc.emplace_back( |
| 430 | absl::Substitute(" result($0, 0) = omegad$1;", m * 4 + 1, m)); |
| 431 | |
| 432 | result_cc.emplace_back(absl::Substitute( |
| 433 | " result($0, 0) = $1;", m * 4 + 2, ccode(*modules_[m].alphas_eqn))); |
| 434 | result_cc.emplace_back(absl::Substitute( |
| 435 | " result($0, 0) = $1;", m * 4 + 3, ccode(*modules_[m].alphad_eqn))); |
| 436 | } |
| 437 | |
| 438 | result_cc.emplace_back( |
| 439 | absl::Substitute(" result($0, 0) = omega;", kNumModules * 4)); |
| 440 | result_cc.emplace_back( |
| 441 | absl::Substitute(" result($0, 0) = vx;", kNumModules * 4 + 1)); |
| 442 | result_cc.emplace_back( |
| 443 | absl::Substitute(" result($0, 0) = vy;", kNumModules * 4 + 2)); |
| 444 | |
| 445 | result_cc.emplace_back(absl::Substitute( |
| 446 | " result($0, 0) = $1;", kNumModules * 4 + 3, ccode(*angular_accel_))); |
| 447 | result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;", |
| 448 | kNumModules * 4 + 4, |
| 449 | ccode(*accel_.get(0, 0)))); |
| 450 | result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;", |
| 451 | kNumModules * 4 + 5, |
| 452 | ccode(*accel_.get(1, 0)))); |
| 453 | |
| 454 | result_cc.emplace_back( |
| 455 | absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 6)); |
| 456 | result_cc.emplace_back( |
| 457 | absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 7)); |
| 458 | result_cc.emplace_back( |
| 459 | absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 8)); |
| 460 | |
| 461 | result_cc.emplace_back(""); |
| 462 | result_cc.emplace_back(" return result;"); |
| 463 | result_cc.emplace_back("}"); |
| 464 | result_cc.emplace_back(""); |
| 465 | result_cc.emplace_back("} // namespace frc971::control_loops::swerve"); |
| 466 | |
| 467 | aos::util::WriteStringToFileOrDie(cc_path, absl::StrJoin(result_cc, "\n")); |
| 468 | aos::util::WriteStringToFileOrDie(h_path, absl::StrJoin(result_h, "\n")); |
| 469 | } |
| 470 | |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 471 | void WriteCasadiVariables(std::vector<std::string> *result_py) { |
| 472 | result_py->emplace_back(" sin = casadi.sin"); |
| 473 | result_py->emplace_back(" cos = casadi.cos"); |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 474 | result_py->emplace_back(" atan2 = half_atan2"); |
| 475 | result_py->emplace_back(" fmax = casadi.fmax"); |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 476 | result_py->emplace_back(" fabs = casadi.fabs"); |
| 477 | |
| 478 | // Start by writing out variables matching each of the symbol names we use |
| 479 | // so we don't have to modify the computed equations too much. |
| 480 | for (size_t m = 0; m < kNumModules; ++m) { |
| 481 | result_py->emplace_back( |
| 482 | absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 4)); |
| 483 | result_py->emplace_back( |
| 484 | absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 4 + 2)); |
| 485 | result_py->emplace_back( |
| 486 | absl::Substitute(" omegad$0 = X[$1, 0]", m, m * 4 + 3)); |
| 487 | } |
| 488 | |
| 489 | result_py->emplace_back( |
| 490 | absl::Substitute(" theta = X[$0, 0]", kNumModules * 4 + 2)); |
| 491 | result_py->emplace_back( |
| 492 | absl::Substitute(" vx = X[$0, 0]", kNumModules * 4 + 3)); |
| 493 | result_py->emplace_back( |
| 494 | absl::Substitute(" vy = X[$0, 0]", kNumModules * 4 + 4)); |
| 495 | result_py->emplace_back( |
| 496 | absl::Substitute(" omega = X[$0, 0]", kNumModules * 4 + 5)); |
| 497 | |
| 498 | result_py->emplace_back( |
| 499 | absl::Substitute(" fx = X[$0, 0]", kNumModules * 4 + 6)); |
| 500 | result_py->emplace_back( |
| 501 | absl::Substitute(" fy = X[$0, 0]", kNumModules * 4 + 7)); |
| 502 | result_py->emplace_back( |
| 503 | absl::Substitute(" moment = X[$0, 0]", kNumModules * 4 + 8)); |
| 504 | |
| 505 | // Now do the same for the inputs. |
| 506 | for (size_t m = 0; m < kNumModules; ++m) { |
| 507 | result_py->emplace_back( |
| 508 | absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2)); |
| 509 | result_py->emplace_back( |
| 510 | absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1)); |
| 511 | } |
| 512 | } |
| 513 | |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 514 | // Writes the physics out to the provided .cc and .h path. |
| 515 | void WriteCasadi(std::string_view py_path) { |
| 516 | std::vector<std::string> result_py; |
| 517 | |
| 518 | // Write out the header. |
| 519 | result_py.emplace_back("#!/usr/bin/python3"); |
| 520 | result_py.emplace_back(""); |
| 521 | result_py.emplace_back("import casadi"); |
| 522 | result_py.emplace_back(""); |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 523 | result_py.emplace_back(absl::Substitute("WHEEL_RADIUS = $0", ccode(*rw_))); |
| 524 | result_py.emplace_back( |
| 525 | absl::Substitute("ROBOT_WIDTH = $0", ccode(*robot_width_))); |
| 526 | result_py.emplace_back(absl::Substitute("CASTER = $0", ccode(*caster_))); |
| 527 | result_py.emplace_back(""); |
| 528 | result_py.emplace_back("def half_atan2(y, x):"); |
| 529 | result_py.emplace_back( |
| 530 | " return casadi.fmod(casadi.atan2(y, x) + casadi.pi * 3.0 / 2.0, " |
| 531 | "casadi.pi) - casadi.pi / 2.0"); |
| 532 | result_py.emplace_back(""); |
| 533 | |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 534 | result_py.emplace_back("# Returns the derivative of our state vector"); |
| 535 | result_py.emplace_back("# [thetas0, thetad0, omegas0, omegad0,"); |
| 536 | result_py.emplace_back("# thetas1, thetad1, omegas1, omegad1,"); |
| 537 | result_py.emplace_back("# thetas2, thetad2, omegas2, omegad2,"); |
| 538 | result_py.emplace_back("# thetas3, thetad3, omegas3, omegad3,"); |
| 539 | result_py.emplace_back("# x, y, theta, vx, vy, omega,"); |
| 540 | result_py.emplace_back("# Fx, Fy, Moment]"); |
| 541 | result_py.emplace_back("def swerve_physics(X, U):"); |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 542 | WriteCasadiVariables(&result_py); |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 543 | |
| 544 | result_py.emplace_back(""); |
| 545 | result_py.emplace_back(" result = casadi.SX.sym('result', 25, 1)"); |
| 546 | result_py.emplace_back(""); |
| 547 | |
| 548 | // And then write out the derivative of each state. |
| 549 | for (size_t m = 0; m < kNumModules; ++m) { |
| 550 | result_py.emplace_back( |
| 551 | absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m)); |
| 552 | result_py.emplace_back( |
| 553 | absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m)); |
| 554 | |
| 555 | result_py.emplace_back(absl::Substitute( |
| 556 | " result[$0, 0] = $1", m * 4 + 2, ccode(*modules_[m].alphas_eqn))); |
| 557 | result_py.emplace_back(absl::Substitute( |
| 558 | " result[$0, 0] = $1", m * 4 + 3, ccode(*modules_[m].alphad_eqn))); |
| 559 | } |
| 560 | |
| 561 | result_py.emplace_back( |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 562 | absl::Substitute(" result[$0, 0] = vx", kNumModules * 4 + 0)); |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 563 | result_py.emplace_back( |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 564 | absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1)); |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 565 | result_py.emplace_back( |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 566 | absl::Substitute(" result[$0, 0] = omega", kNumModules * 4 + 2)); |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 567 | |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 568 | result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1", |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 569 | kNumModules * 4 + 3, |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 570 | ccode(*accel_.get(0, 0)))); |
| 571 | result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1", |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 572 | kNumModules * 4 + 4, |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 573 | ccode(*accel_.get(1, 0)))); |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 574 | result_py.emplace_back(absl::Substitute( |
| 575 | " result[$0, 0] = $1", kNumModules * 4 + 5, ccode(*angular_accel_))); |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 576 | |
| 577 | result_py.emplace_back( |
| 578 | absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6)); |
| 579 | result_py.emplace_back( |
| 580 | absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7)); |
| 581 | result_py.emplace_back( |
| 582 | absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8)); |
| 583 | |
| 584 | result_py.emplace_back(""); |
| 585 | result_py.emplace_back( |
| 586 | " return casadi.Function('xdot', [X, U], [result])"); |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 587 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 588 | DefineVector2dFunction( |
| 589 | "contact_patch_velocity", |
| 590 | "# Returns the velocity of the wheel in global coordinates.", |
| 591 | [](const Module &m, int dimension) { |
| 592 | return ccode(*m.contact_patch_velocity.get(dimension, 0)); |
| 593 | }, |
| 594 | &result_py); |
| 595 | DefineVector2dFunction( |
| 596 | "wheel_ground_velocity", |
| 597 | "# Returns the velocity of the wheel in steer module coordinates.", |
| 598 | [](const Module &m, int dimension) { |
| 599 | return ccode(*m.wheel_ground_velocity.get(dimension, 0)); |
| 600 | }, |
| 601 | &result_py); |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 602 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 603 | DefineVector2dFunction( |
| 604 | "wheel_slip_velocity", |
| 605 | "# Returns the difference in velocities of the wheel surface and the " |
| 606 | "ground.", |
| 607 | [](const Module &m, int dimension) { |
| 608 | return ccode(*m.wheel_slip_velocity.get(dimension, 0)); |
| 609 | }, |
| 610 | &result_py); |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 611 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 612 | DefineScalarFunction( |
| 613 | "slip_angle", "Returns the slip angle of the ith wheel", |
| 614 | [](const Module &m) { return ccode(*m.slip_angle); }, &result_py); |
| 615 | DefineScalarFunction( |
| 616 | "slip_ratio", "Returns the slip ratio of the ith wheel", |
| 617 | [](const Module &m) { return ccode(*m.slip_ratio); }, &result_py); |
| 618 | DefineScalarFunction( |
| 619 | "module_angular_accel", |
| 620 | "Returns the angular acceleration of the robot due to the ith wheel", |
| 621 | [](const Module &m) { return ccode(*m.angular_accel); }, &result_py); |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 622 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 623 | DefineVector2dFunction( |
| 624 | "wheel_force", |
| 625 | "Returns the force on the wheel in steer module coordinates", |
| 626 | [](const Module &m, int dimension) { |
| 627 | return ccode(*std::vector<RCP<const Basic>>{m.Fwx, m.Fwy}[dimension]); |
| 628 | }, |
| 629 | &result_py); |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 630 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 631 | DefineVector2dFunction( |
| 632 | "F", "Returns the force on the wheel in absolute coordinates", |
| 633 | [](const Module &m, int dimension) { |
| 634 | return ccode(*m.F.get(dimension, 0)); |
| 635 | }, |
| 636 | &result_py); |
| 637 | |
| 638 | DefineVector2dFunction( |
| 639 | "mounting_location", |
| 640 | "Returns the mounting location of wheel in robot coordinates", |
| 641 | [](const Module &m, int dimension) { |
| 642 | return ccode(*m.mounting_location.get(dimension, 0)); |
| 643 | }, |
| 644 | &result_py); |
| 645 | |
| 646 | DefineScalarFunction( |
| 647 | "Ms", "Returns the self aligning moment of the ith wheel", |
| 648 | [this](const Module &m) { |
| 649 | return ccode(*(div(m.Ms, add(Jsm_, div(div(Js_, Gs_), Gs_))))); |
| 650 | }, |
| 651 | &result_py); |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 652 | |
| 653 | aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n")); |
| 654 | } |
| 655 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 656 | void DefineScalarFunction( |
| 657 | std::string_view name, std::string_view documentation, |
| 658 | std::function<std::string(const Module &)> scalar_fn, |
| 659 | std::vector<std::string> *result_py) { |
| 660 | result_py->emplace_back(""); |
| 661 | result_py->emplace_back(absl::Substitute("# $0.", documentation)); |
| 662 | result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name)); |
| 663 | WriteCasadiVariables(result_py); |
| 664 | for (size_t m = 0; m < kNumModules; ++m) { |
| 665 | if (m == 0) { |
| 666 | result_py->emplace_back(" if i == 0:"); |
| 667 | } else { |
| 668 | result_py->emplace_back(absl::Substitute(" elif i == $0:", m)); |
| 669 | } |
| 670 | result_py->emplace_back( |
| 671 | absl::Substitute(" return casadi.Function('$0', [X, U], [$1])", |
| 672 | name, scalar_fn(modules_[m]))); |
| 673 | } |
| 674 | result_py->emplace_back(" raise ValueError(\"Invalid module number\")"); |
| 675 | } |
| 676 | |
| 677 | void DefineVector2dFunction( |
| 678 | std::string_view name, std::string_view documentation, |
| 679 | std::function<std::string(const Module &, int)> scalar_fn, |
| 680 | std::vector<std::string> *result_py) { |
| 681 | result_py->emplace_back(""); |
| 682 | result_py->emplace_back(absl::Substitute("# $0.", documentation)); |
| 683 | result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name)); |
| 684 | WriteCasadiVariables(result_py); |
| 685 | result_py->emplace_back( |
| 686 | absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name)); |
| 687 | for (size_t m = 0; m < kNumModules; ++m) { |
| 688 | if (m == 0) { |
| 689 | result_py->emplace_back(" if i == 0:"); |
| 690 | } else { |
| 691 | result_py->emplace_back(absl::Substitute(" elif i == $0:", m)); |
| 692 | } |
| 693 | for (int j = 0; j < 2; ++j) { |
| 694 | result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1", |
| 695 | j, scalar_fn(modules_[m], j))); |
| 696 | } |
| 697 | } |
| 698 | result_py->emplace_back(" else:"); |
| 699 | result_py->emplace_back( |
| 700 | " raise ValueError(\"Invalid module number\")"); |
| 701 | result_py->emplace_back(absl::Substitute( |
| 702 | " return casadi.Function('$0', [X, U], [result])", name)); |
| 703 | } |
| 704 | |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 705 | private: |
| 706 | static constexpr uint8_t kNumModules = 4; |
| 707 | |
| 708 | Module ModulePhysics(const int m, DenseMatrix mounting_location) { |
| 709 | VLOG(1) << "Solving module " << m; |
| 710 | |
| 711 | Module result; |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 712 | result.mounting_location = mounting_location; |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 713 | |
| 714 | result.Is = symbol(absl::StrFormat("Is%u", m)); |
| 715 | result.Id = symbol(absl::StrFormat("Id%u", m)); |
| 716 | |
| 717 | RCP<const Symbol> thetamd = symbol(absl::StrFormat("theta_md%u", m)); |
| 718 | RCP<const Symbol> omegamd = symbol(absl::StrFormat("omega_md%u", m)); |
| 719 | RCP<const Symbol> alphamd = symbol(absl::StrFormat("alpha_md%u", m)); |
| 720 | |
| 721 | result.thetas = symbol(absl::StrFormat("thetas%u", m)); |
| 722 | result.omegas = symbol(absl::StrFormat("omegas%u", m)); |
| 723 | result.alphas = symbol(absl::StrFormat("alphas%u", m)); |
| 724 | |
| 725 | result.thetad = symbol(absl::StrFormat("thetad%u", m)); |
| 726 | result.omegad = symbol(absl::StrFormat("omegad%u", m)); |
| 727 | result.alphad = symbol(absl::StrFormat("alphad%u", m)); |
| 728 | |
| 729 | // Velocity of the module in field coordinates |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 730 | DenseMatrix robot_velocity = DenseMatrix(2, 1, {vx_, vy_}); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 731 | VLOG(1) << "robot velocity: " << robot_velocity.__str__(); |
| 732 | |
| 733 | // Velocity of the contact patch in field coordinates |
| 734 | DenseMatrix temp_matrix = DenseMatrix(2, 1); |
| 735 | DenseMatrix temp_matrix2 = DenseMatrix(2, 1); |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 736 | result.contact_patch_velocity = DenseMatrix(2, 1); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 737 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 738 | mul_dense_dense(R(theta_), result.mounting_location, temp_matrix); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 739 | add_dense_dense(angle_cross(temp_matrix, omega_), robot_velocity, |
| 740 | temp_matrix2); |
| 741 | mul_dense_dense(R(add(theta_, result.thetas)), |
Austin Schuh | 6927bc3 | 2024-07-14 17:24:56 -0700 | [diff] [blame] | 742 | DenseMatrix(2, 1, {neg(caster_), integer(0)}), temp_matrix); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 743 | add_dense_dense(temp_matrix2, |
| 744 | angle_cross(temp_matrix, add(omega_, result.omegas)), |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 745 | result.contact_patch_velocity); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 746 | |
| 747 | VLOG(1); |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 748 | VLOG(1) << "contact patch velocity: " |
| 749 | << result.contact_patch_velocity.__str__(); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 750 | |
| 751 | // Relative velocity of the surface of the wheel to the ground. |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 752 | result.wheel_ground_velocity = DenseMatrix(2, 1); |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 753 | mul_dense_dense(R(neg(add(result.thetas, theta_))), |
| 754 | result.contact_patch_velocity, |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 755 | result.wheel_ground_velocity); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 756 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 757 | // Compute the relative velocity between the wheel surface and the ground in |
| 758 | // the wheel coordinate system. |
| 759 | result.wheel_slip_velocity = DenseMatrix(2, 1); |
| 760 | DenseMatrix wheel_velocity = |
| 761 | DenseMatrix(2, 1, {mul(rw_, result.omegad), integer(0)}); |
| 762 | DenseMatrix negative_wheel_ground_velocity = |
| 763 | DenseMatrix(2, 1, |
| 764 | {neg(result.wheel_ground_velocity.get(0, 0)), |
| 765 | neg(result.wheel_ground_velocity.get(1, 0))}); |
| 766 | add_dense_dense(negative_wheel_ground_velocity, wheel_velocity, |
| 767 | result.wheel_slip_velocity); |
| 768 | |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 769 | VLOG(1); |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 770 | VLOG(1) << "wheel ground velocity: " |
| 771 | << result.wheel_ground_velocity.__str__(); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 772 | |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 773 | result.slip_angle = neg(atan2(result.wheel_ground_velocity.get(1, 0), |
| 774 | result.wheel_ground_velocity.get(0, 0))); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 775 | |
| 776 | VLOG(1); |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 777 | VLOG(1) << "slip angle: " << result.slip_angle->__str__(); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 778 | |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 779 | // TODO(austin): Does this handle decel properly? |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 780 | result.slip_ratio = div( |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 781 | sub(mul(rw_, result.omegad), result.wheel_ground_velocity.get(0, 0)), |
| 782 | SymEngine::max( |
| 783 | {real_double(0.02), abs(result.wheel_ground_velocity.get(0, 0))})); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 784 | VLOG(1); |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 785 | VLOG(1) << "Slip ratio " << result.slip_ratio->__str__(); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 786 | |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 787 | result.Fwx = simplify(mul(Cx_, result.slip_ratio)); |
| 788 | result.Fwy = simplify(mul(Cy_, result.slip_angle)); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 789 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 790 | result.Ms = mul(neg(result.Fwy), |
| 791 | add(div(contact_patch_length_, integer(3)), caster_)); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 792 | VLOG(1); |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 793 | VLOG(1) << "Ms " << result.Ms->__str__(); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 794 | VLOG(1); |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 795 | VLOG(1) << "Fwx " << result.Fwx->__str__(); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 796 | VLOG(1); |
Austin Schuh | b67a38f | 2024-07-04 13:48:38 -0700 | [diff] [blame] | 797 | VLOG(1) << "Fwy " << result.Fwy->__str__(); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 798 | |
| 799 | // alphas = ... |
| 800 | RCP<const Basic> lhms = |
| 801 | mul(add(neg(wb_), mul(add(rs_, rp_), sub(integer(1), div(rb1_, rp_)))), |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 802 | mul(div(rw_, rb2_), neg(result.Fwx))); |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 803 | RCP<const Basic> lhs = |
| 804 | add(add(result.Ms, div(mul(Jsm_, result.Is), Gs_)), lhms); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 805 | RCP<const Basic> rhs = add(Jsm_, div(div(Js_, Gs_), Gs_)); |
| 806 | RCP<const Basic> accel_steer_eqn = simplify(div(lhs, rhs)); |
| 807 | |
| 808 | VLOG(1); |
| 809 | VLOG(1) << result.alphas->__str__() << " = " << accel_steer_eqn->__str__(); |
| 810 | |
| 811 | lhs = sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), result.omegas), |
| 812 | mul(Gd1_, mul(Gd2_, omegamd))); |
| 813 | RCP<const Basic> dplanitary_eqn = sub(mul(Gd3_, lhs), result.omegad); |
| 814 | |
| 815 | lhs = sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), result.alphas), |
| 816 | mul(Gd1_, mul(Gd2_, alphamd))); |
| 817 | RCP<const Basic> ddplanitary_eqn = sub(mul(Gd3_, lhs), result.alphad); |
| 818 | |
| 819 | RCP<const Basic> drive_eqn = sub( |
| 820 | add(mul(neg(Jdm_), div(alphamd, Gd_)), mul(Ktd_, div(result.Id, Gd_))), |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 821 | mul(neg(result.Fwx), rw_)); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 822 | |
| 823 | VLOG(1) << "drive_eqn: " << drive_eqn->__str__(); |
| 824 | |
| 825 | // Substitute in ddplanitary_eqn so we get rid of alphamd |
| 826 | map_basic_basic map; |
| 827 | RCP<const Set> reals = interval(NegInf, Inf, true, true); |
| 828 | RCP<const Set> solve_solution = solve(ddplanitary_eqn, alphamd, reals); |
| 829 | map[alphamd] = solve_solution->get_args()[1]->get_args()[0]; |
| 830 | VLOG(1) << "temp: " << solve_solution->__str__(); |
| 831 | RCP<const Basic> drive_eqn_subs = drive_eqn->subs(map); |
| 832 | |
| 833 | map.clear(); |
| 834 | map[result.alphas] = accel_steer_eqn; |
| 835 | RCP<const Basic> drive_eqn_subs2 = drive_eqn_subs->subs(map); |
| 836 | RCP<const Basic> drive_eqn_subs3 = simplify(drive_eqn_subs2); |
| 837 | VLOG(1) << "drive_eqn simplified: " << drive_eqn_subs3->__str__(); |
| 838 | |
| 839 | solve_solution = solve(drive_eqn_subs3, result.alphad, reals); |
| 840 | |
| 841 | RCP<const Basic> drive_accel = |
| 842 | simplify(solve_solution->get_args()[1]->get_args()[0]); |
| 843 | VLOG(1) << "drive_accel: " << drive_accel->__str__(); |
| 844 | |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 845 | // Compute the resulting force from the module. |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 846 | result.F = DenseMatrix(2, 1); |
| 847 | mul_dense_dense(R(add(theta_, result.thetas)), |
| 848 | DenseMatrix(2, 1, {result.Fwx, result.Fwy}), result.F); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 849 | |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 850 | RCP<const Basic> torque = force_cross(result.mounting_location, result.F); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 851 | result.accel = DenseMatrix(2, 1); |
Austin Schuh | b8b34be | 2024-07-14 16:06:19 -0700 | [diff] [blame] | 852 | mul_dense_scalar(result.F, pow(m_, minus_one), result.accel); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 853 | result.angular_accel = div(torque, J_); |
| 854 | VLOG(1); |
| 855 | VLOG(1) << "angular_accel = " << result.angular_accel->__str__(); |
| 856 | |
| 857 | VLOG(1); |
| 858 | VLOG(1) << "accel(0, 0) = " << result.accel.get(0, 0)->__str__(); |
| 859 | VLOG(1); |
| 860 | VLOG(1) << "accel(1, 0) = " << result.accel.get(1, 0)->__str__(); |
| 861 | |
| 862 | result.alphad_eqn = drive_accel; |
| 863 | result.alphas_eqn = accel_steer_eqn; |
| 864 | return result; |
| 865 | } |
| 866 | |
| 867 | DenseMatrix R(const RCP<const Basic> theta) { |
| 868 | return DenseMatrix(2, 2, |
| 869 | {cos(theta), neg(sin(theta)), sin(theta), cos(theta)}); |
| 870 | } |
| 871 | |
| 872 | DenseMatrix angle_cross(DenseMatrix a, RCP<const Basic> b) { |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 873 | return DenseMatrix(2, 1, {mul(neg(a.get(1, 0)), b), mul(a.get(0, 0), b)}); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | RCP<const Basic> force_cross(DenseMatrix r, DenseMatrix f) { |
| 877 | return sub(mul(r.get(0, 0), f.get(1, 0)), mul(r.get(1, 0), f.get(0, 0))); |
| 878 | } |
| 879 | |
| 880 | // z represents the number of teeth per gear, theta is the angle between |
| 881 | // shafts(in degrees), D_02 is the pitch diameter of gear 2 and b_2 is the |
| 882 | // length of the tooth of gear 2 |
| 883 | // returns std::pair(r_01, r_02) |
| 884 | std::pair<double, double> GetBevelPitchRadius(double z1, double z2, |
| 885 | double theta, double D_02, |
| 886 | double b_2) { |
| 887 | double gamma_1 = std::atan2(z1, z2); |
| 888 | double gamma_2 = theta / 180.0 * std::numbers::pi - gamma_1; |
| 889 | double R_m = D_02 / 2 / std::sin(gamma_2) - b_2 / 2; |
| 890 | return std::pair(R_m * std::cos(gamma_2), R_m * std::sin(gamma_2)); |
| 891 | } |
| 892 | |
| 893 | Motor drive_motor_; |
| 894 | Motor steer_motor_; |
| 895 | |
| 896 | RCP<const Basic> Cx_; |
| 897 | RCP<const Basic> Cy_; |
Austin Schuh | 2a1abec | 2024-07-10 20:31:16 -0700 | [diff] [blame] | 898 | RCP<const Basic> rw_; |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 899 | RCP<const Basic> m_; |
| 900 | RCP<const Basic> J_; |
| 901 | RCP<const Basic> Gd1_; |
| 902 | RCP<const Basic> rs_; |
| 903 | RCP<const Basic> rp_; |
| 904 | RCP<const Basic> Gd2_; |
| 905 | RCP<const Basic> rb1_; |
| 906 | RCP<const Basic> rb2_; |
| 907 | RCP<const Basic> Gd3_; |
| 908 | RCP<const Basic> Gd_; |
| 909 | RCP<const Basic> Js_; |
| 910 | RCP<const Basic> Gs_; |
| 911 | RCP<const Basic> wb_; |
| 912 | RCP<const Basic> Jdm_; |
| 913 | RCP<const Basic> Jsm_; |
| 914 | RCP<const Basic> Kts_; |
| 915 | RCP<const Basic> Ktd_; |
| 916 | RCP<const Basic> robot_width_; |
| 917 | RCP<const Basic> caster_; |
| 918 | RCP<const Basic> contact_patch_length_; |
| 919 | RCP<const Basic> x_; |
| 920 | RCP<const Basic> y_; |
| 921 | RCP<const Basic> theta_; |
| 922 | RCP<const Basic> vx_; |
| 923 | RCP<const Basic> vy_; |
| 924 | RCP<const Basic> omega_; |
| 925 | RCP<const Basic> ax_; |
| 926 | RCP<const Basic> ay_; |
| 927 | RCP<const Basic> atheta_; |
| 928 | |
| 929 | std::array<Module, kNumModules> modules_; |
| 930 | |
| 931 | DenseMatrix accel_; |
| 932 | RCP<const Basic> angular_accel_; |
| 933 | }; |
| 934 | |
| 935 | } // namespace frc971::control_loops::swerve |
| 936 | |
| 937 | int main(int argc, char **argv) { |
| 938 | aos::InitGoogle(&argc, &argv); |
| 939 | |
| 940 | frc971::control_loops::swerve::SwerveSimulation sim; |
| 941 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 942 | if (!absl::GetFlag(FLAGS_cc_output_path).empty() && |
| 943 | !absl::GetFlag(FLAGS_h_output_path).empty()) { |
| 944 | sim.Write(absl::GetFlag(FLAGS_cc_output_path), |
| 945 | absl::GetFlag(FLAGS_h_output_path)); |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 946 | } |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 947 | if (!absl::GetFlag(FLAGS_py_output_path).empty()) { |
| 948 | sim.WritePy(absl::GetFlag(FLAGS_py_output_path)); |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 949 | } |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 950 | if (!absl::GetFlag(FLAGS_casadi_py_output_path).empty()) { |
| 951 | sim.WriteCasadi(absl::GetFlag(FLAGS_casadi_py_output_path)); |
Austin Schuh | 0f88109 | 2024-06-28 15:36:48 -0700 | [diff] [blame] | 952 | } |
justinT21 | 446e4f6 | 2024-06-16 22:36:10 -0700 | [diff] [blame] | 953 | |
| 954 | return 0; |
| 955 | } |