blob: bb5a51a39ab28d2c43a395664c73a49022b1dbf1 [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
Austin Schuh99f7c6a2024-06-25 22:07:44 -070015#include "absl/flags/flag.h"
16#include "absl/log/check.h"
17#include "absl/log/log.h"
justinT21446e4f62024-06-16 22:36:10 -070018#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"
justinT21446e4f62024-06-16 22:36:10 -070022
23#include "aos/init.h"
24#include "aos/util/file.h"
25#include "frc971/control_loops/swerve/motors.h"
26
Austin Schuh99f7c6a2024-06-25 22:07:44 -070027ABSL_FLAG(std::string, output_base, "",
28 "Path to strip off the front of the output paths.");
29ABSL_FLAG(std::string, cc_output_path, "",
30 "Path to write generated cc code to");
31ABSL_FLAG(std::string, h_output_path, "",
32 "Path to write generated header code to");
33ABSL_FLAG(std::string, py_output_path, "",
34 "Path to write generated py code to");
35ABSL_FLAG(std::string, casadi_py_output_path, "",
36 "Path to write casadi generated py code to");
Austin Schuh27694fa2024-07-20 16:29:49 -070037ABSL_FLAG(double, caster, 0.01, "Caster in meters for the module.");
justinT21446e4f62024-06-16 22:36:10 -070038
Austin Schuh99f7c6a2024-06-25 22:07:44 -070039ABSL_FLAG(bool, symbolic, false, "If true, write everything out symbolically.");
justinT21446e4f62024-06-16 22:36:10 -070040
justinT21942892b2024-07-02 22:33:50 -070041using SymEngine::abs;
justinT21446e4f62024-06-16 22:36:10 -070042using SymEngine::add;
43using SymEngine::atan2;
44using SymEngine::Basic;
45using SymEngine::ccode;
46using SymEngine::cos;
47using SymEngine::DenseMatrix;
48using SymEngine::div;
49using SymEngine::Inf;
50using SymEngine::integer;
51using SymEngine::map_basic_basic;
52using SymEngine::minus_one;
53using SymEngine::neg;
54using SymEngine::NegInf;
55using SymEngine::pow;
56using SymEngine::RCP;
57using SymEngine::real_double;
58using SymEngine::RealDouble;
59using SymEngine::Set;
Austin Schuh27694fa2024-07-20 16:29:49 -070060using SymEngine::sign;
justinT21446e4f62024-06-16 22:36:10 -070061using SymEngine::simplify;
62using SymEngine::sin;
63using SymEngine::solve;
64using SymEngine::symbol;
65using SymEngine::Symbol;
66
67namespace frc971::control_loops::swerve {
68
69// State per module.
70struct Module {
71 RCP<const Symbol> Is;
72
73 RCP<const Symbol> Id;
74
75 RCP<const Symbol> thetas;
76 RCP<const Symbol> omegas;
77 RCP<const Symbol> alphas;
78 RCP<const Basic> alphas_eqn;
79
80 RCP<const Symbol> thetad;
81 RCP<const Symbol> omegad;
82 RCP<const Symbol> alphad;
83 RCP<const Basic> alphad_eqn;
84
Austin Schuh2a1abec2024-07-10 20:31:16 -070085 DenseMatrix contact_patch_velocity;
Austin Schuhb67a38f2024-07-04 13:48:38 -070086 DenseMatrix wheel_ground_velocity;
Austin Schuhb8b34be2024-07-14 16:06:19 -070087 DenseMatrix wheel_slip_velocity;
Austin Schuhb67a38f2024-07-04 13:48:38 -070088 RCP<const Basic> slip_angle;
89 RCP<const Basic> slip_ratio;
90
Austin Schuhb8b34be2024-07-14 16:06:19 -070091 RCP<const Basic> Ms;
Austin Schuhb67a38f2024-07-04 13:48:38 -070092 RCP<const Basic> Fwx;
93 RCP<const Basic> Fwy;
Austin Schuhb8b34be2024-07-14 16:06:19 -070094 DenseMatrix F;
95 DenseMatrix mounting_location;
Austin Schuhb67a38f2024-07-04 13:48:38 -070096
justinT21446e4f62024-06-16 22:36:10 -070097 // Acceleration contribution from this module.
98 DenseMatrix accel;
99 RCP<const Basic> angular_accel;
100};
101
102class SwerveSimulation {
103 public:
104 SwerveSimulation() : drive_motor_(KrakenFOC()), steer_motor_(KrakenFOC()) {
105 auto fx = symbol("fx");
106 auto fy = symbol("fy");
107 auto moment = symbol("moment");
108
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700109 if (absl::GetFlag(FLAGS_symbolic)) {
justinT21446e4f62024-06-16 22:36:10 -0700110 Cx_ = symbol("Cx");
111 Cy_ = symbol("Cy");
112
Austin Schuh2a1abec2024-07-10 20:31:16 -0700113 rw_ = symbol("rw");
justinT21446e4f62024-06-16 22:36:10 -0700114
115 m_ = symbol("m");
116 J_ = symbol("J");
117
118 Gd1_ = symbol("Gd1");
119 rs_ = symbol("rs");
120 rp_ = symbol("rp");
121 Gd2_ = symbol("Gd2");
122
123 rb1_ = symbol("rb1");
124 rb2_ = symbol("rb2");
125
126 Gd2_ = symbol("Gd3");
127 Gd_ = symbol("Gd");
128
129 Js_ = symbol("Js");
130
131 Gs_ = symbol("Gs");
132 wb_ = symbol("wb");
133
134 Jdm_ = symbol("Jdm");
135 Jsm_ = symbol("Jsm");
136 Kts_ = symbol("Kts");
137 Ktd_ = symbol("Ktd");
138
139 robot_width_ = symbol("robot_width");
140
141 caster_ = symbol("caster");
142 contact_patch_length_ = symbol("Lcp");
143 } else {
Austin Schuhb8b34be2024-07-14 16:06:19 -0700144 Cx_ = real_double(25.0 * 9.8 / 4.0 / 0.05);
justinT21446e4f62024-06-16 22:36:10 -0700145 Cy_ = real_double(5 * 9.8 / 0.05 / 4.0);
146
Austin Schuh2a1abec2024-07-10 20:31:16 -0700147 rw_ = real_double(2 * 0.0254);
justinT21446e4f62024-06-16 22:36:10 -0700148
149 m_ = real_double(25.0); // base is 20 kg without battery
150 J_ = real_double(6.0);
151
152 Gd1_ = real_double(12.0 / 42.0);
153 rs_ = real_double(28.0 / 20.0 / 2.0);
154 rp_ = real_double(18.0 / 20.0 / 2.0);
155 Gd2_ = div(rs_, rp_);
156
157 // 15 / 45 bevel ratio, calculated using python script ported over to
158 // GetBevelPitchRadius(double
159 // TODO(Justin): Use the function instead of computed constantss
160 rb1_ = real_double(0.3805473);
161 rb2_ = real_double(1.14164);
162
163 Gd3_ = div(rb1_, rb2_);
164 Gd_ = mul(mul(Gd1_, Gd2_), Gd3_);
165
Austin Schuhb8b34be2024-07-14 16:06:19 -0700166 Js_ = real_double(0.001);
justinT21446e4f62024-06-16 22:36:10 -0700167
168 Gs_ = real_double(35.0 / 468.0);
169 wb_ = real_double(0.725);
170
171 Jdm_ = real_double(drive_motor_.motor_inertia);
172 Jsm_ = real_double(steer_motor_.motor_inertia);
173 Kts_ = real_double(steer_motor_.Kt);
174 Ktd_ = real_double(drive_motor_.Kt);
175
176 robot_width_ = real_double(24.75 * 0.0254);
177
Austin Schuh27694fa2024-07-20 16:29:49 -0700178 caster_ = real_double(absl::GetFlag(FLAGS_caster));
justinT21446e4f62024-06-16 22:36:10 -0700179 contact_patch_length_ = real_double(0.02);
180 }
181
182 x_ = symbol("x");
183 y_ = symbol("y");
184 theta_ = symbol("theta");
185
186 vx_ = symbol("vx");
187 vy_ = symbol("vy");
188 omega_ = symbol("omega");
189
190 ax_ = symbol("ax");
191 ay_ = symbol("ay");
192 atheta_ = symbol("atheta");
193
194 // Now, compute the accelerations due to the disturbance forces.
justinT21446e4f62024-06-16 22:36:10 -0700195 DenseMatrix external_accel = DenseMatrix(2, 1, {div(fx, m_), div(fy, m_)});
196
197 // And compute the physics contributions from each module.
198 modules_[0] = ModulePhysics(
199 0, DenseMatrix(
200 2, 1,
201 {div(robot_width_, integer(2)), div(robot_width_, integer(2))}));
202 modules_[1] =
203 ModulePhysics(1, DenseMatrix(2, 1,
204 {div(robot_width_, integer(-2)),
205 div(robot_width_, integer(2))}));
206 modules_[2] =
207 ModulePhysics(2, DenseMatrix(2, 1,
208 {div(robot_width_, integer(-2)),
209 div(robot_width_, integer(-2))}));
210 modules_[3] =
211 ModulePhysics(3, DenseMatrix(2, 1,
212 {div(robot_width_, integer(2)),
213 div(robot_width_, integer(-2))}));
214
215 // And convert them into the overall robot contribution.
216 DenseMatrix temp0 = DenseMatrix(2, 1);
217 DenseMatrix temp1 = DenseMatrix(2, 1);
218 DenseMatrix temp2 = DenseMatrix(2, 1);
219 accel_ = DenseMatrix(2, 1);
220
221 add_dense_dense(modules_[0].accel, external_accel, temp0);
222 add_dense_dense(temp0, modules_[1].accel, temp1);
223 add_dense_dense(temp1, modules_[2].accel, temp2);
224 add_dense_dense(temp2, modules_[3].accel, accel_);
225
Austin Schuhb8b34be2024-07-14 16:06:19 -0700226 angular_accel_ =
227 add(div(moment, J_),
228 add(add(modules_[0].angular_accel, modules_[1].angular_accel),
229 add(modules_[2].angular_accel, modules_[3].angular_accel)));
justinT21446e4f62024-06-16 22:36:10 -0700230
231 VLOG(1) << "accel(0, 0) = " << ccode(*accel_.get(0, 0));
232 VLOG(1) << "accel(1, 0) = " << ccode(*accel_.get(1, 0));
233 VLOG(1) << "angular_accel = " << ccode(*angular_accel_);
234 }
235
justinT21942892b2024-07-02 22:33:50 -0700236 // Writes the physics out to the provided .py path.
237 void WritePy(std::string_view py_path) {
238 std::vector<std::string> result_py;
239
240 result_py.emplace_back("#!/usr/bin/python3");
241 result_py.emplace_back("");
242 result_py.emplace_back("import numpy");
justinT21942892b2024-07-02 22:33:50 -0700243 result_py.emplace_back("");
244
justinT21942892b2024-07-02 22:33:50 -0700245 result_py.emplace_back("def swerve_physics(t, X, U_func):");
Austin Schuh0f881092024-06-28 15:36:48 -0700246 result_py.emplace_back(" def atan2(y, x):");
247 result_py.emplace_back(" if x < 0:");
248 result_py.emplace_back(" return -numpy.atan2(y, x)");
249 result_py.emplace_back(" else:");
250 result_py.emplace_back(" return numpy.atan2(y, x)");
251 result_py.emplace_back(" sin = numpy.sin");
252 result_py.emplace_back(" cos = numpy.cos");
253 result_py.emplace_back(" fabs = numpy.fabs");
254
justinT21942892b2024-07-02 22:33:50 -0700255 result_py.emplace_back(" result = numpy.empty([25, 1])");
256 result_py.emplace_back(" X = X.reshape(25, 1)");
257 result_py.emplace_back(" U = U_func(X)");
258 result_py.emplace_back("");
259
260 // Start by writing out variables matching each of the symbol names we use
261 // so we don't have to modify the computed equations too much.
262 for (size_t m = 0; m < kNumModules; ++m) {
263 result_py.emplace_back(
264 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 4));
265 result_py.emplace_back(
266 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 4 + 2));
267 result_py.emplace_back(
268 absl::Substitute(" omegad$0 = X[$1, 0]", m, m * 4 + 3));
269 }
270
271 result_py.emplace_back(
272 absl::Substitute(" theta = X[$0, 0]", kNumModules * 4 + 2));
273 result_py.emplace_back(
274 absl::Substitute(" vx = X[$0, 0]", kNumModules * 4 + 3));
275 result_py.emplace_back(
276 absl::Substitute(" vy = X[$0, 0]", kNumModules * 4 + 4));
277 result_py.emplace_back(
278 absl::Substitute(" omega = X[$0, 0]", kNumModules * 4 + 5));
279
280 result_py.emplace_back(
281 absl::Substitute(" fx = X[$0, 0]", kNumModules * 4 + 6));
282 result_py.emplace_back(
283 absl::Substitute(" fy = X[$0, 0]", kNumModules * 4 + 7));
284 result_py.emplace_back(
285 absl::Substitute(" moment = X[$0, 0]", kNumModules * 4 + 8));
286
287 // Now do the same for the inputs.
288 for (size_t m = 0; m < kNumModules; ++m) {
289 result_py.emplace_back(absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
290 result_py.emplace_back(
291 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
292 }
293
294 result_py.emplace_back("");
295
296 // And then write out the derivative of each state.
297 for (size_t m = 0; m < kNumModules; ++m) {
298 result_py.emplace_back(
299 absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m));
300 result_py.emplace_back(
301 absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m));
302
303 result_py.emplace_back(absl::Substitute(
304 " result[$0, 0] = $1", m * 4 + 2, ccode(*modules_[m].alphas_eqn)));
305 result_py.emplace_back(absl::Substitute(
306 " result[$0, 0] = $1", m * 4 + 3, ccode(*modules_[m].alphad_eqn)));
307 }
308
309 result_py.emplace_back(
310 absl::Substitute(" result[$0, 0] = vx", kNumModules * 4));
311 result_py.emplace_back(
312 absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1));
313 result_py.emplace_back(
314 absl::Substitute(" result[$0, 0] = omega", kNumModules * 4 + 2));
315
316 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
317 kNumModules * 4 + 3,
318 ccode(*accel_.get(0, 0))));
319 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
320 kNumModules * 4 + 4,
321 ccode(*accel_.get(1, 0))));
322 result_py.emplace_back(absl::Substitute(
323 " result[$0, 0] = $1", kNumModules * 4 + 5, ccode(*angular_accel_)));
324
325 result_py.emplace_back(
326 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6));
327 result_py.emplace_back(
328 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7));
329 result_py.emplace_back(
330 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8));
331
332 result_py.emplace_back("");
333 result_py.emplace_back(" return result.reshape(25,)\n");
334
335 aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n"));
336 }
337
justinT21446e4f62024-06-16 22:36:10 -0700338 // Writes the physics out to the provided .cc and .h path.
339 void Write(std::string_view cc_path, std::string_view h_path) {
340 std::vector<std::string> result_cc;
341 std::vector<std::string> result_h;
342
Austin Schuh0f881092024-06-28 15:36:48 -0700343 std::string_view include_guard_stripped = h_path;
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700344 CHECK(absl::ConsumePrefix(&include_guard_stripped,
345 absl::GetFlag(FLAGS_output_base)));
justinT21446e4f62024-06-16 22:36:10 -0700346 std::string include_guard =
347 absl::StrReplaceAll(absl::AsciiStrToUpper(include_guard_stripped),
348 {{"/", "_"}, {".", "_"}});
349
350 // Write out the header.
351 result_h.emplace_back(absl::Substitute("#ifndef $0_", include_guard));
352 result_h.emplace_back(absl::Substitute("#define $0_", include_guard));
353 result_h.emplace_back("");
354 result_h.emplace_back("#include <Eigen/Dense>");
355 result_h.emplace_back("");
356 result_h.emplace_back("namespace frc971::control_loops::swerve {");
357 result_h.emplace_back("");
358 result_h.emplace_back("// Returns the derivative of our state vector");
359 result_h.emplace_back("// [thetas0, thetad0, omegas0, omegad0,");
360 result_h.emplace_back("// thetas1, thetad1, omegas1, omegad1,");
361 result_h.emplace_back("// thetas2, thetad2, omegas2, omegad2,");
362 result_h.emplace_back("// thetas3, thetad3, omegas3, omegad3,");
363 result_h.emplace_back("// x, y, theta, vx, vy, omega,");
364 result_h.emplace_back("// Fx, Fy, Moment]");
365 result_h.emplace_back("Eigen::Matrix<double, 25, 1> SwervePhysics(");
366 result_h.emplace_back(
367 " Eigen::Map<const Eigen::Matrix<double, 25, 1>> X,");
368 result_h.emplace_back(
369 " Eigen::Map<const Eigen::Matrix<double, 8, 1>> U);");
370 result_h.emplace_back("");
371 result_h.emplace_back("} // namespace frc971::control_loops::swerve");
372 result_h.emplace_back("");
373 result_h.emplace_back(absl::Substitute("#endif // $0_", include_guard));
374
375 // Write out the .cc
376 result_cc.emplace_back(
377 absl::Substitute("#include \"$0\"", include_guard_stripped));
378 result_cc.emplace_back("");
379 result_cc.emplace_back("#include <cmath>");
380 result_cc.emplace_back("");
381 result_cc.emplace_back("namespace frc971::control_loops::swerve {");
Austin Schuh27694fa2024-07-20 16:29:49 -0700382 result_cc.emplace_back("namespace {");
383 result_cc.emplace_back("");
384 result_cc.emplace_back("double sign(double x) {");
385 result_cc.emplace_back(" return (x > 0) - (x < 0);");
386 result_cc.emplace_back("}");
387
388 result_cc.emplace_back("");
389 result_cc.emplace_back("} // namespace");
justinT21446e4f62024-06-16 22:36:10 -0700390 result_cc.emplace_back("");
391 result_cc.emplace_back("Eigen::Matrix<double, 25, 1> SwervePhysics(");
392 result_cc.emplace_back(
393 " Eigen::Map<const Eigen::Matrix<double, 25, 1>> X,");
394 result_cc.emplace_back(
395 " Eigen::Map<const Eigen::Matrix<double, 8, 1>> U) {");
396 result_cc.emplace_back(" Eigen::Matrix<double, 25, 1> result;");
397
398 // Start by writing out variables matching each of the symbol names we use
399 // so we don't have to modify the computed equations too much.
400 for (size_t m = 0; m < kNumModules; ++m) {
401 result_cc.emplace_back(
402 absl::Substitute(" const double thetas$0 = X($1, 0);", m, m * 4));
403 result_cc.emplace_back(absl::Substitute(
404 " const double omegas$0 = X($1, 0);", m, m * 4 + 2));
405 result_cc.emplace_back(absl::Substitute(
406 " const double omegad$0 = X($1, 0);", m, m * 4 + 3));
407 }
408
409 result_cc.emplace_back(absl::Substitute(" const double theta = X($0, 0);",
410 kNumModules * 4 + 2));
411 result_cc.emplace_back(
412 absl::Substitute(" const double vx = X($0, 0);", kNumModules * 4 + 3));
413 result_cc.emplace_back(
414 absl::Substitute(" const double vy = X($0, 0);", kNumModules * 4 + 4));
415 result_cc.emplace_back(absl::Substitute(" const double omega = X($0, 0);",
416 kNumModules * 4 + 5));
417
418 result_cc.emplace_back(
419 absl::Substitute(" const double fx = X($0, 0);", kNumModules * 4 + 6));
420 result_cc.emplace_back(
421 absl::Substitute(" const double fy = X($0, 0);", kNumModules * 4 + 7));
422 result_cc.emplace_back(absl::Substitute(" const double moment = X($0, 0);",
423 kNumModules * 4 + 8));
424
425 // Now do the same for the inputs.
426 for (size_t m = 0; m < kNumModules; ++m) {
427 result_cc.emplace_back(
428 absl::Substitute(" const double Is$0 = U($1, 0);", m, m * 2));
429 result_cc.emplace_back(
430 absl::Substitute(" const double Id$0 = U($1, 0);", m, m * 2 + 1));
431 }
432
433 result_cc.emplace_back("");
434
435 // And then write out the derivative of each state.
436 for (size_t m = 0; m < kNumModules; ++m) {
437 result_cc.emplace_back(
438 absl::Substitute(" result($0, 0) = omegas$1;", m * 4, m));
439 result_cc.emplace_back(
440 absl::Substitute(" result($0, 0) = omegad$1;", m * 4 + 1, m));
441
442 result_cc.emplace_back(absl::Substitute(
443 " result($0, 0) = $1;", m * 4 + 2, ccode(*modules_[m].alphas_eqn)));
444 result_cc.emplace_back(absl::Substitute(
445 " result($0, 0) = $1;", m * 4 + 3, ccode(*modules_[m].alphad_eqn)));
446 }
447
448 result_cc.emplace_back(
449 absl::Substitute(" result($0, 0) = omega;", kNumModules * 4));
450 result_cc.emplace_back(
451 absl::Substitute(" result($0, 0) = vx;", kNumModules * 4 + 1));
452 result_cc.emplace_back(
453 absl::Substitute(" result($0, 0) = vy;", kNumModules * 4 + 2));
454
455 result_cc.emplace_back(absl::Substitute(
456 " result($0, 0) = $1;", kNumModules * 4 + 3, ccode(*angular_accel_)));
457 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
458 kNumModules * 4 + 4,
459 ccode(*accel_.get(0, 0))));
460 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
461 kNumModules * 4 + 5,
462 ccode(*accel_.get(1, 0))));
463
464 result_cc.emplace_back(
465 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 6));
466 result_cc.emplace_back(
467 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 7));
468 result_cc.emplace_back(
469 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 8));
470
471 result_cc.emplace_back("");
472 result_cc.emplace_back(" return result;");
473 result_cc.emplace_back("}");
474 result_cc.emplace_back("");
475 result_cc.emplace_back("} // namespace frc971::control_loops::swerve");
476
477 aos::util::WriteStringToFileOrDie(cc_path, absl::StrJoin(result_cc, "\n"));
478 aos::util::WriteStringToFileOrDie(h_path, absl::StrJoin(result_h, "\n"));
479 }
480
Austin Schuhb67a38f2024-07-04 13:48:38 -0700481 void WriteCasadiVariables(std::vector<std::string> *result_py) {
482 result_py->emplace_back(" sin = casadi.sin");
Austin Schuh27694fa2024-07-20 16:29:49 -0700483 result_py->emplace_back(" sign = casadi.sign");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700484 result_py->emplace_back(" cos = casadi.cos");
Austin Schuh27694fa2024-07-20 16:29:49 -0700485 result_py->emplace_back(" atan2 = sin_atan2");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700486 result_py->emplace_back(" fmax = casadi.fmax");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700487 result_py->emplace_back(" fabs = casadi.fabs");
488
489 // Start by writing out variables matching each of the symbol names we use
490 // so we don't have to modify the computed equations too much.
491 for (size_t m = 0; m < kNumModules; ++m) {
492 result_py->emplace_back(
493 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 4));
494 result_py->emplace_back(
495 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 4 + 2));
496 result_py->emplace_back(
497 absl::Substitute(" omegad$0 = X[$1, 0]", m, m * 4 + 3));
498 }
499
500 result_py->emplace_back(
501 absl::Substitute(" theta = X[$0, 0]", kNumModules * 4 + 2));
502 result_py->emplace_back(
503 absl::Substitute(" vx = X[$0, 0]", kNumModules * 4 + 3));
504 result_py->emplace_back(
505 absl::Substitute(" vy = X[$0, 0]", kNumModules * 4 + 4));
506 result_py->emplace_back(
507 absl::Substitute(" omega = X[$0, 0]", kNumModules * 4 + 5));
508
509 result_py->emplace_back(
510 absl::Substitute(" fx = X[$0, 0]", kNumModules * 4 + 6));
511 result_py->emplace_back(
512 absl::Substitute(" fy = X[$0, 0]", kNumModules * 4 + 7));
513 result_py->emplace_back(
514 absl::Substitute(" moment = X[$0, 0]", kNumModules * 4 + 8));
515
516 // Now do the same for the inputs.
517 for (size_t m = 0; m < kNumModules; ++m) {
518 result_py->emplace_back(
519 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
520 result_py->emplace_back(
521 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
522 }
523 }
524
Austin Schuh0f881092024-06-28 15:36:48 -0700525 // Writes the physics out to the provided .cc and .h path.
526 void WriteCasadi(std::string_view py_path) {
527 std::vector<std::string> result_py;
528
529 // Write out the header.
530 result_py.emplace_back("#!/usr/bin/python3");
531 result_py.emplace_back("");
532 result_py.emplace_back("import casadi");
533 result_py.emplace_back("");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700534 result_py.emplace_back(absl::Substitute("WHEEL_RADIUS = $0", ccode(*rw_)));
535 result_py.emplace_back(
536 absl::Substitute("ROBOT_WIDTH = $0", ccode(*robot_width_)));
537 result_py.emplace_back(absl::Substitute("CASTER = $0", ccode(*caster_)));
538 result_py.emplace_back("");
Austin Schuh27694fa2024-07-20 16:29:49 -0700539 result_py.emplace_back("def sin_atan2(y, x):");
540 result_py.emplace_back(" return casadi.sin(casadi.atan2(y, x))");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700541 result_py.emplace_back("");
542
Austin Schuh0f881092024-06-28 15:36:48 -0700543 result_py.emplace_back("# Returns the derivative of our state vector");
544 result_py.emplace_back("# [thetas0, thetad0, omegas0, omegad0,");
545 result_py.emplace_back("# thetas1, thetad1, omegas1, omegad1,");
546 result_py.emplace_back("# thetas2, thetad2, omegas2, omegad2,");
547 result_py.emplace_back("# thetas3, thetad3, omegas3, omegad3,");
548 result_py.emplace_back("# x, y, theta, vx, vy, omega,");
549 result_py.emplace_back("# Fx, Fy, Moment]");
550 result_py.emplace_back("def swerve_physics(X, U):");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700551 WriteCasadiVariables(&result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700552
553 result_py.emplace_back("");
554 result_py.emplace_back(" result = casadi.SX.sym('result', 25, 1)");
555 result_py.emplace_back("");
556
557 // And then write out the derivative of each state.
558 for (size_t m = 0; m < kNumModules; ++m) {
559 result_py.emplace_back(
560 absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m));
561 result_py.emplace_back(
562 absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m));
563
564 result_py.emplace_back(absl::Substitute(
565 " result[$0, 0] = $1", m * 4 + 2, ccode(*modules_[m].alphas_eqn)));
566 result_py.emplace_back(absl::Substitute(
567 " result[$0, 0] = $1", m * 4 + 3, ccode(*modules_[m].alphad_eqn)));
568 }
569
570 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700571 absl::Substitute(" result[$0, 0] = vx", kNumModules * 4 + 0));
Austin Schuh0f881092024-06-28 15:36:48 -0700572 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700573 absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1));
Austin Schuh0f881092024-06-28 15:36:48 -0700574 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700575 absl::Substitute(" result[$0, 0] = omega", kNumModules * 4 + 2));
Austin Schuh0f881092024-06-28 15:36:48 -0700576
Austin Schuh0f881092024-06-28 15:36:48 -0700577 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700578 kNumModules * 4 + 3,
Austin Schuh0f881092024-06-28 15:36:48 -0700579 ccode(*accel_.get(0, 0))));
580 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700581 kNumModules * 4 + 4,
Austin Schuh0f881092024-06-28 15:36:48 -0700582 ccode(*accel_.get(1, 0))));
Austin Schuhb8b34be2024-07-14 16:06:19 -0700583 result_py.emplace_back(absl::Substitute(
584 " result[$0, 0] = $1", kNumModules * 4 + 5, ccode(*angular_accel_)));
Austin Schuh0f881092024-06-28 15:36:48 -0700585
586 result_py.emplace_back(
587 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6));
588 result_py.emplace_back(
589 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7));
590 result_py.emplace_back(
591 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8));
592
593 result_py.emplace_back("");
594 result_py.emplace_back(
595 " return casadi.Function('xdot', [X, U], [result])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700596
Austin Schuhb8b34be2024-07-14 16:06:19 -0700597 DefineVector2dFunction(
598 "contact_patch_velocity",
599 "# Returns the velocity of the wheel in global coordinates.",
600 [](const Module &m, int dimension) {
601 return ccode(*m.contact_patch_velocity.get(dimension, 0));
602 },
603 &result_py);
604 DefineVector2dFunction(
605 "wheel_ground_velocity",
606 "# Returns the velocity of the wheel in steer module coordinates.",
607 [](const Module &m, int dimension) {
608 return ccode(*m.wheel_ground_velocity.get(dimension, 0));
609 },
610 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700611
Austin Schuhb8b34be2024-07-14 16:06:19 -0700612 DefineVector2dFunction(
613 "wheel_slip_velocity",
614 "# Returns the difference in velocities of the wheel surface and the "
615 "ground.",
616 [](const Module &m, int dimension) {
617 return ccode(*m.wheel_slip_velocity.get(dimension, 0));
618 },
619 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700620
Austin Schuhb8b34be2024-07-14 16:06:19 -0700621 DefineScalarFunction(
622 "slip_angle", "Returns the slip angle of the ith wheel",
623 [](const Module &m) { return ccode(*m.slip_angle); }, &result_py);
624 DefineScalarFunction(
625 "slip_ratio", "Returns the slip ratio of the ith wheel",
626 [](const Module &m) { return ccode(*m.slip_ratio); }, &result_py);
627 DefineScalarFunction(
628 "module_angular_accel",
629 "Returns the angular acceleration of the robot due to the ith wheel",
630 [](const Module &m) { return ccode(*m.angular_accel); }, &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700631
Austin Schuhb8b34be2024-07-14 16:06:19 -0700632 DefineVector2dFunction(
633 "wheel_force",
634 "Returns the force on the wheel in steer module coordinates",
635 [](const Module &m, int dimension) {
636 return ccode(*std::vector<RCP<const Basic>>{m.Fwx, m.Fwy}[dimension]);
637 },
638 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700639
Austin Schuhb8b34be2024-07-14 16:06:19 -0700640 DefineVector2dFunction(
641 "F", "Returns the force on the wheel in absolute coordinates",
642 [](const Module &m, int dimension) {
643 return ccode(*m.F.get(dimension, 0));
644 },
645 &result_py);
646
647 DefineVector2dFunction(
648 "mounting_location",
649 "Returns the mounting location of wheel in robot coordinates",
650 [](const Module &m, int dimension) {
651 return ccode(*m.mounting_location.get(dimension, 0));
652 },
653 &result_py);
654
655 DefineScalarFunction(
656 "Ms", "Returns the self aligning moment of the ith wheel",
657 [this](const Module &m) {
658 return ccode(*(div(m.Ms, add(Jsm_, div(div(Js_, Gs_), Gs_)))));
659 },
660 &result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700661
662 aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n"));
663 }
664
Austin Schuhb8b34be2024-07-14 16:06:19 -0700665 void DefineScalarFunction(
666 std::string_view name, std::string_view documentation,
667 std::function<std::string(const Module &)> scalar_fn,
668 std::vector<std::string> *result_py) {
669 result_py->emplace_back("");
670 result_py->emplace_back(absl::Substitute("# $0.", documentation));
671 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
672 WriteCasadiVariables(result_py);
673 for (size_t m = 0; m < kNumModules; ++m) {
674 if (m == 0) {
675 result_py->emplace_back(" if i == 0:");
676 } else {
677 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
678 }
679 result_py->emplace_back(
680 absl::Substitute(" return casadi.Function('$0', [X, U], [$1])",
681 name, scalar_fn(modules_[m])));
682 }
683 result_py->emplace_back(" raise ValueError(\"Invalid module number\")");
684 }
685
686 void DefineVector2dFunction(
687 std::string_view name, std::string_view documentation,
688 std::function<std::string(const Module &, int)> scalar_fn,
689 std::vector<std::string> *result_py) {
690 result_py->emplace_back("");
691 result_py->emplace_back(absl::Substitute("# $0.", documentation));
692 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
693 WriteCasadiVariables(result_py);
694 result_py->emplace_back(
695 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
696 for (size_t m = 0; m < kNumModules; ++m) {
697 if (m == 0) {
698 result_py->emplace_back(" if i == 0:");
699 } else {
700 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
701 }
702 for (int j = 0; j < 2; ++j) {
703 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
704 j, scalar_fn(modules_[m], j)));
705 }
706 }
707 result_py->emplace_back(" else:");
708 result_py->emplace_back(
709 " raise ValueError(\"Invalid module number\")");
710 result_py->emplace_back(absl::Substitute(
711 " return casadi.Function('$0', [X, U], [result])", name));
712 }
713
justinT21446e4f62024-06-16 22:36:10 -0700714 private:
715 static constexpr uint8_t kNumModules = 4;
716
717 Module ModulePhysics(const int m, DenseMatrix mounting_location) {
718 VLOG(1) << "Solving module " << m;
719
720 Module result;
Austin Schuhb8b34be2024-07-14 16:06:19 -0700721 result.mounting_location = mounting_location;
justinT21446e4f62024-06-16 22:36:10 -0700722
723 result.Is = symbol(absl::StrFormat("Is%u", m));
724 result.Id = symbol(absl::StrFormat("Id%u", m));
725
726 RCP<const Symbol> thetamd = symbol(absl::StrFormat("theta_md%u", m));
727 RCP<const Symbol> omegamd = symbol(absl::StrFormat("omega_md%u", m));
728 RCP<const Symbol> alphamd = symbol(absl::StrFormat("alpha_md%u", m));
729
730 result.thetas = symbol(absl::StrFormat("thetas%u", m));
731 result.omegas = symbol(absl::StrFormat("omegas%u", m));
732 result.alphas = symbol(absl::StrFormat("alphas%u", m));
733
734 result.thetad = symbol(absl::StrFormat("thetad%u", m));
735 result.omegad = symbol(absl::StrFormat("omegad%u", m));
736 result.alphad = symbol(absl::StrFormat("alphad%u", m));
737
738 // Velocity of the module in field coordinates
Austin Schuh2a1abec2024-07-10 20:31:16 -0700739 DenseMatrix robot_velocity = DenseMatrix(2, 1, {vx_, vy_});
justinT21446e4f62024-06-16 22:36:10 -0700740 VLOG(1) << "robot velocity: " << robot_velocity.__str__();
741
742 // Velocity of the contact patch in field coordinates
743 DenseMatrix temp_matrix = DenseMatrix(2, 1);
744 DenseMatrix temp_matrix2 = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700745 result.contact_patch_velocity = DenseMatrix(2, 1);
justinT21446e4f62024-06-16 22:36:10 -0700746
Austin Schuhb8b34be2024-07-14 16:06:19 -0700747 mul_dense_dense(R(theta_), result.mounting_location, temp_matrix);
justinT21446e4f62024-06-16 22:36:10 -0700748 add_dense_dense(angle_cross(temp_matrix, omega_), robot_velocity,
749 temp_matrix2);
750 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuh6927bc32024-07-14 17:24:56 -0700751 DenseMatrix(2, 1, {neg(caster_), integer(0)}), temp_matrix);
justinT21446e4f62024-06-16 22:36:10 -0700752 add_dense_dense(temp_matrix2,
753 angle_cross(temp_matrix, add(omega_, result.omegas)),
Austin Schuh2a1abec2024-07-10 20:31:16 -0700754 result.contact_patch_velocity);
justinT21446e4f62024-06-16 22:36:10 -0700755
756 VLOG(1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700757 VLOG(1) << "contact patch velocity: "
758 << result.contact_patch_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -0700759
760 // Relative velocity of the surface of the wheel to the ground.
Austin Schuhb67a38f2024-07-04 13:48:38 -0700761 result.wheel_ground_velocity = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700762 mul_dense_dense(R(neg(add(result.thetas, theta_))),
763 result.contact_patch_velocity,
Austin Schuhb67a38f2024-07-04 13:48:38 -0700764 result.wheel_ground_velocity);
justinT21446e4f62024-06-16 22:36:10 -0700765
Austin Schuhb8b34be2024-07-14 16:06:19 -0700766 // Compute the relative velocity between the wheel surface and the ground in
767 // the wheel coordinate system.
768 result.wheel_slip_velocity = DenseMatrix(2, 1);
769 DenseMatrix wheel_velocity =
770 DenseMatrix(2, 1, {mul(rw_, result.omegad), integer(0)});
771 DenseMatrix negative_wheel_ground_velocity =
772 DenseMatrix(2, 1,
773 {neg(result.wheel_ground_velocity.get(0, 0)),
774 neg(result.wheel_ground_velocity.get(1, 0))});
775 add_dense_dense(negative_wheel_ground_velocity, wheel_velocity,
776 result.wheel_slip_velocity);
777
justinT21446e4f62024-06-16 22:36:10 -0700778 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700779 VLOG(1) << "wheel ground velocity: "
780 << result.wheel_ground_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -0700781
Austin Schuhb67a38f2024-07-04 13:48:38 -0700782 result.slip_angle = neg(atan2(result.wheel_ground_velocity.get(1, 0),
783 result.wheel_ground_velocity.get(0, 0)));
justinT21446e4f62024-06-16 22:36:10 -0700784
785 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700786 VLOG(1) << "slip angle: " << result.slip_angle->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700787
Austin Schuh2a1abec2024-07-10 20:31:16 -0700788 // TODO(austin): Does this handle decel properly?
Austin Schuhb67a38f2024-07-04 13:48:38 -0700789 result.slip_ratio = div(
Austin Schuh2a1abec2024-07-10 20:31:16 -0700790 sub(mul(rw_, result.omegad), result.wheel_ground_velocity.get(0, 0)),
791 SymEngine::max(
792 {real_double(0.02), abs(result.wheel_ground_velocity.get(0, 0))}));
justinT21446e4f62024-06-16 22:36:10 -0700793 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700794 VLOG(1) << "Slip ratio " << result.slip_ratio->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700795
Austin Schuhb67a38f2024-07-04 13:48:38 -0700796 result.Fwx = simplify(mul(Cx_, result.slip_ratio));
797 result.Fwy = simplify(mul(Cy_, result.slip_angle));
justinT21446e4f62024-06-16 22:36:10 -0700798
Austin Schuh27694fa2024-07-20 16:29:49 -0700799 // The self-aligning moment needs to flip when the module flips direction.
Austin Schuhb8b34be2024-07-14 16:06:19 -0700800 result.Ms = mul(neg(result.Fwy),
Austin Schuh27694fa2024-07-20 16:29:49 -0700801 add(div(mul(sign(result.wheel_ground_velocity.get(0, 0)),
802 contact_patch_length_),
803 integer(3)),
804 caster_));
justinT21446e4f62024-06-16 22:36:10 -0700805 VLOG(1);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700806 VLOG(1) << "Ms " << result.Ms->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700807 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700808 VLOG(1) << "Fwx " << result.Fwx->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700809 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700810 VLOG(1) << "Fwy " << result.Fwy->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700811
812 // alphas = ...
813 RCP<const Basic> lhms =
814 mul(add(neg(wb_), mul(add(rs_, rp_), sub(integer(1), div(rb1_, rp_)))),
Austin Schuh2a1abec2024-07-10 20:31:16 -0700815 mul(div(rw_, rb2_), neg(result.Fwx)));
Austin Schuhb8b34be2024-07-14 16:06:19 -0700816 RCP<const Basic> lhs =
817 add(add(result.Ms, div(mul(Jsm_, result.Is), Gs_)), lhms);
justinT21446e4f62024-06-16 22:36:10 -0700818 RCP<const Basic> rhs = add(Jsm_, div(div(Js_, Gs_), Gs_));
819 RCP<const Basic> accel_steer_eqn = simplify(div(lhs, rhs));
820
821 VLOG(1);
822 VLOG(1) << result.alphas->__str__() << " = " << accel_steer_eqn->__str__();
823
824 lhs = sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), result.omegas),
825 mul(Gd1_, mul(Gd2_, omegamd)));
826 RCP<const Basic> dplanitary_eqn = sub(mul(Gd3_, lhs), result.omegad);
827
828 lhs = sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), result.alphas),
829 mul(Gd1_, mul(Gd2_, alphamd)));
830 RCP<const Basic> ddplanitary_eqn = sub(mul(Gd3_, lhs), result.alphad);
831
832 RCP<const Basic> drive_eqn = sub(
833 add(mul(neg(Jdm_), div(alphamd, Gd_)), mul(Ktd_, div(result.Id, Gd_))),
Austin Schuh2a1abec2024-07-10 20:31:16 -0700834 mul(neg(result.Fwx), rw_));
justinT21446e4f62024-06-16 22:36:10 -0700835
836 VLOG(1) << "drive_eqn: " << drive_eqn->__str__();
837
838 // Substitute in ddplanitary_eqn so we get rid of alphamd
839 map_basic_basic map;
840 RCP<const Set> reals = interval(NegInf, Inf, true, true);
841 RCP<const Set> solve_solution = solve(ddplanitary_eqn, alphamd, reals);
842 map[alphamd] = solve_solution->get_args()[1]->get_args()[0];
843 VLOG(1) << "temp: " << solve_solution->__str__();
844 RCP<const Basic> drive_eqn_subs = drive_eqn->subs(map);
845
846 map.clear();
847 map[result.alphas] = accel_steer_eqn;
848 RCP<const Basic> drive_eqn_subs2 = drive_eqn_subs->subs(map);
849 RCP<const Basic> drive_eqn_subs3 = simplify(drive_eqn_subs2);
850 VLOG(1) << "drive_eqn simplified: " << drive_eqn_subs3->__str__();
851
852 solve_solution = solve(drive_eqn_subs3, result.alphad, reals);
853
854 RCP<const Basic> drive_accel =
855 simplify(solve_solution->get_args()[1]->get_args()[0]);
856 VLOG(1) << "drive_accel: " << drive_accel->__str__();
857
Austin Schuh2a1abec2024-07-10 20:31:16 -0700858 // Compute the resulting force from the module.
Austin Schuhb8b34be2024-07-14 16:06:19 -0700859 result.F = DenseMatrix(2, 1);
860 mul_dense_dense(R(add(theta_, result.thetas)),
861 DenseMatrix(2, 1, {result.Fwx, result.Fwy}), result.F);
justinT21446e4f62024-06-16 22:36:10 -0700862
Austin Schuhb8b34be2024-07-14 16:06:19 -0700863 RCP<const Basic> torque = force_cross(result.mounting_location, result.F);
justinT21446e4f62024-06-16 22:36:10 -0700864 result.accel = DenseMatrix(2, 1);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700865 mul_dense_scalar(result.F, pow(m_, minus_one), result.accel);
justinT21446e4f62024-06-16 22:36:10 -0700866 result.angular_accel = div(torque, J_);
867 VLOG(1);
868 VLOG(1) << "angular_accel = " << result.angular_accel->__str__();
869
870 VLOG(1);
871 VLOG(1) << "accel(0, 0) = " << result.accel.get(0, 0)->__str__();
872 VLOG(1);
873 VLOG(1) << "accel(1, 0) = " << result.accel.get(1, 0)->__str__();
874
875 result.alphad_eqn = drive_accel;
876 result.alphas_eqn = accel_steer_eqn;
877 return result;
878 }
879
880 DenseMatrix R(const RCP<const Basic> theta) {
881 return DenseMatrix(2, 2,
882 {cos(theta), neg(sin(theta)), sin(theta), cos(theta)});
883 }
884
885 DenseMatrix angle_cross(DenseMatrix a, RCP<const Basic> b) {
Austin Schuh2a1abec2024-07-10 20:31:16 -0700886 return DenseMatrix(2, 1, {mul(neg(a.get(1, 0)), b), mul(a.get(0, 0), b)});
justinT21446e4f62024-06-16 22:36:10 -0700887 }
888
889 RCP<const Basic> force_cross(DenseMatrix r, DenseMatrix f) {
890 return sub(mul(r.get(0, 0), f.get(1, 0)), mul(r.get(1, 0), f.get(0, 0)));
891 }
892
893 // z represents the number of teeth per gear, theta is the angle between
894 // shafts(in degrees), D_02 is the pitch diameter of gear 2 and b_2 is the
895 // length of the tooth of gear 2
896 // returns std::pair(r_01, r_02)
897 std::pair<double, double> GetBevelPitchRadius(double z1, double z2,
898 double theta, double D_02,
899 double b_2) {
900 double gamma_1 = std::atan2(z1, z2);
901 double gamma_2 = theta / 180.0 * std::numbers::pi - gamma_1;
902 double R_m = D_02 / 2 / std::sin(gamma_2) - b_2 / 2;
903 return std::pair(R_m * std::cos(gamma_2), R_m * std::sin(gamma_2));
904 }
905
906 Motor drive_motor_;
907 Motor steer_motor_;
908
909 RCP<const Basic> Cx_;
910 RCP<const Basic> Cy_;
Austin Schuh2a1abec2024-07-10 20:31:16 -0700911 RCP<const Basic> rw_;
justinT21446e4f62024-06-16 22:36:10 -0700912 RCP<const Basic> m_;
913 RCP<const Basic> J_;
914 RCP<const Basic> Gd1_;
915 RCP<const Basic> rs_;
916 RCP<const Basic> rp_;
917 RCP<const Basic> Gd2_;
918 RCP<const Basic> rb1_;
919 RCP<const Basic> rb2_;
920 RCP<const Basic> Gd3_;
921 RCP<const Basic> Gd_;
922 RCP<const Basic> Js_;
923 RCP<const Basic> Gs_;
924 RCP<const Basic> wb_;
925 RCP<const Basic> Jdm_;
926 RCP<const Basic> Jsm_;
927 RCP<const Basic> Kts_;
928 RCP<const Basic> Ktd_;
929 RCP<const Basic> robot_width_;
930 RCP<const Basic> caster_;
931 RCP<const Basic> contact_patch_length_;
932 RCP<const Basic> x_;
933 RCP<const Basic> y_;
934 RCP<const Basic> theta_;
935 RCP<const Basic> vx_;
936 RCP<const Basic> vy_;
937 RCP<const Basic> omega_;
938 RCP<const Basic> ax_;
939 RCP<const Basic> ay_;
940 RCP<const Basic> atheta_;
941
942 std::array<Module, kNumModules> modules_;
943
944 DenseMatrix accel_;
945 RCP<const Basic> angular_accel_;
946};
947
948} // namespace frc971::control_loops::swerve
949
950int main(int argc, char **argv) {
951 aos::InitGoogle(&argc, &argv);
952
953 frc971::control_loops::swerve::SwerveSimulation sim;
954
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700955 if (!absl::GetFlag(FLAGS_cc_output_path).empty() &&
956 !absl::GetFlag(FLAGS_h_output_path).empty()) {
957 sim.Write(absl::GetFlag(FLAGS_cc_output_path),
958 absl::GetFlag(FLAGS_h_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -0700959 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700960 if (!absl::GetFlag(FLAGS_py_output_path).empty()) {
961 sim.WritePy(absl::GetFlag(FLAGS_py_output_path));
justinT21446e4f62024-06-16 22:36:10 -0700962 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700963 if (!absl::GetFlag(FLAGS_casadi_py_output_path).empty()) {
964 sim.WriteCasadi(absl::GetFlag(FLAGS_casadi_py_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -0700965 }
justinT21446e4f62024-06-16 22:36:10 -0700966
967 return 0;
968}