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