blob: 1326fd72b593c56e9db8521603ee9fccdfc549be [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(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700449 absl::Substitute(" result($0, 0) = vx;", kNumModules * 4 + 0));
justinT21446e4f62024-06-16 22:36:10 -0700450 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700451 absl::Substitute(" result($0, 0) = vy;", kNumModules * 4 + 1));
justinT21446e4f62024-06-16 22:36:10 -0700452 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700453 absl::Substitute(" result($0, 0) = omega;", kNumModules * 4 + 2));
justinT21446e4f62024-06-16 22:36:10 -0700454
justinT21446e4f62024-06-16 22:36:10 -0700455 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700456 kNumModules * 4 + 3,
justinT21446e4f62024-06-16 22:36:10 -0700457 ccode(*accel_.get(0, 0))));
458 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700459 kNumModules * 4 + 4,
justinT21446e4f62024-06-16 22:36:10 -0700460 ccode(*accel_.get(1, 0))));
Austin Schuh5ddcb472024-07-21 17:55:34 -0700461 result_cc.emplace_back(absl::Substitute(
462 " result($0, 0) = $1;", kNumModules * 4 + 5, ccode(*angular_accel_)));
justinT21446e4f62024-06-16 22:36:10 -0700463
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 Schuh5ddcb472024-07-21 17:55:34 -0700485 result_py->emplace_back(" atan2 = casadi.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 Schuh2a1abec2024-07-10 20:31:16 -0700539
Austin Schuh0f881092024-06-28 15:36:48 -0700540 result_py.emplace_back("# Returns the derivative of our state vector");
541 result_py.emplace_back("# [thetas0, thetad0, omegas0, omegad0,");
542 result_py.emplace_back("# thetas1, thetad1, omegas1, omegad1,");
543 result_py.emplace_back("# thetas2, thetad2, omegas2, omegad2,");
544 result_py.emplace_back("# thetas3, thetad3, omegas3, omegad3,");
545 result_py.emplace_back("# x, y, theta, vx, vy, omega,");
546 result_py.emplace_back("# Fx, Fy, Moment]");
547 result_py.emplace_back("def swerve_physics(X, U):");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700548 WriteCasadiVariables(&result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700549
550 result_py.emplace_back("");
551 result_py.emplace_back(" result = casadi.SX.sym('result', 25, 1)");
552 result_py.emplace_back("");
553
554 // And then write out the derivative of each state.
555 for (size_t m = 0; m < kNumModules; ++m) {
556 result_py.emplace_back(
557 absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m));
558 result_py.emplace_back(
559 absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m));
560
561 result_py.emplace_back(absl::Substitute(
562 " result[$0, 0] = $1", m * 4 + 2, ccode(*modules_[m].alphas_eqn)));
563 result_py.emplace_back(absl::Substitute(
564 " result[$0, 0] = $1", m * 4 + 3, ccode(*modules_[m].alphad_eqn)));
565 }
566
567 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700568 absl::Substitute(" result[$0, 0] = vx", kNumModules * 4 + 0));
Austin Schuh0f881092024-06-28 15:36:48 -0700569 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700570 absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1));
Austin Schuh0f881092024-06-28 15:36:48 -0700571 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700572 absl::Substitute(" result[$0, 0] = omega", kNumModules * 4 + 2));
Austin Schuh0f881092024-06-28 15:36:48 -0700573
Austin Schuh0f881092024-06-28 15:36:48 -0700574 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700575 kNumModules * 4 + 3,
Austin Schuh0f881092024-06-28 15:36:48 -0700576 ccode(*accel_.get(0, 0))));
577 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700578 kNumModules * 4 + 4,
Austin Schuh0f881092024-06-28 15:36:48 -0700579 ccode(*accel_.get(1, 0))));
Austin Schuhb8b34be2024-07-14 16:06:19 -0700580 result_py.emplace_back(absl::Substitute(
581 " result[$0, 0] = $1", kNumModules * 4 + 5, ccode(*angular_accel_)));
Austin Schuh0f881092024-06-28 15:36:48 -0700582
583 result_py.emplace_back(
584 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6));
585 result_py.emplace_back(
586 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7));
587 result_py.emplace_back(
588 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8));
589
590 result_py.emplace_back("");
591 result_py.emplace_back(
592 " return casadi.Function('xdot', [X, U], [result])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700593
Austin Schuhb8b34be2024-07-14 16:06:19 -0700594 DefineVector2dFunction(
595 "contact_patch_velocity",
596 "# Returns the velocity of the wheel in global coordinates.",
597 [](const Module &m, int dimension) {
598 return ccode(*m.contact_patch_velocity.get(dimension, 0));
599 },
600 &result_py);
601 DefineVector2dFunction(
602 "wheel_ground_velocity",
603 "# Returns the velocity of the wheel in steer module coordinates.",
604 [](const Module &m, int dimension) {
605 return ccode(*m.wheel_ground_velocity.get(dimension, 0));
606 },
607 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700608
Austin Schuhb8b34be2024-07-14 16:06:19 -0700609 DefineVector2dFunction(
610 "wheel_slip_velocity",
611 "# Returns the difference in velocities of the wheel surface and the "
612 "ground.",
613 [](const Module &m, int dimension) {
614 return ccode(*m.wheel_slip_velocity.get(dimension, 0));
615 },
616 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700617
Austin Schuhb8b34be2024-07-14 16:06:19 -0700618 DefineScalarFunction(
619 "slip_angle", "Returns the slip angle of the ith wheel",
620 [](const Module &m) { return ccode(*m.slip_angle); }, &result_py);
621 DefineScalarFunction(
622 "slip_ratio", "Returns the slip ratio of the ith wheel",
623 [](const Module &m) { return ccode(*m.slip_ratio); }, &result_py);
624 DefineScalarFunction(
625 "module_angular_accel",
626 "Returns the angular acceleration of the robot due to the ith wheel",
627 [](const Module &m) { return ccode(*m.angular_accel); }, &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700628
Austin Schuhb8b34be2024-07-14 16:06:19 -0700629 DefineVector2dFunction(
630 "wheel_force",
631 "Returns the force on the wheel in steer module coordinates",
632 [](const Module &m, int dimension) {
633 return ccode(*std::vector<RCP<const Basic>>{m.Fwx, m.Fwy}[dimension]);
634 },
635 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700636
Austin Schuhb8b34be2024-07-14 16:06:19 -0700637 DefineVector2dFunction(
638 "F", "Returns the force on the wheel in absolute coordinates",
639 [](const Module &m, int dimension) {
640 return ccode(*m.F.get(dimension, 0));
641 },
642 &result_py);
643
644 DefineVector2dFunction(
645 "mounting_location",
646 "Returns the mounting location of wheel in robot coordinates",
647 [](const Module &m, int dimension) {
648 return ccode(*m.mounting_location.get(dimension, 0));
649 },
650 &result_py);
651
652 DefineScalarFunction(
653 "Ms", "Returns the self aligning moment of the ith wheel",
654 [this](const Module &m) {
655 return ccode(*(div(m.Ms, add(Jsm_, div(div(Js_, Gs_), Gs_)))));
656 },
657 &result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700658
659 aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n"));
660 }
661
Austin Schuhb8b34be2024-07-14 16:06:19 -0700662 void DefineScalarFunction(
663 std::string_view name, std::string_view documentation,
664 std::function<std::string(const Module &)> scalar_fn,
665 std::vector<std::string> *result_py) {
666 result_py->emplace_back("");
667 result_py->emplace_back(absl::Substitute("# $0.", documentation));
668 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
669 WriteCasadiVariables(result_py);
670 for (size_t m = 0; m < kNumModules; ++m) {
671 if (m == 0) {
672 result_py->emplace_back(" if i == 0:");
673 } else {
674 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
675 }
676 result_py->emplace_back(
677 absl::Substitute(" return casadi.Function('$0', [X, U], [$1])",
678 name, scalar_fn(modules_[m])));
679 }
680 result_py->emplace_back(" raise ValueError(\"Invalid module number\")");
681 }
682
683 void DefineVector2dFunction(
684 std::string_view name, std::string_view documentation,
685 std::function<std::string(const Module &, int)> scalar_fn,
686 std::vector<std::string> *result_py) {
687 result_py->emplace_back("");
688 result_py->emplace_back(absl::Substitute("# $0.", documentation));
689 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
690 WriteCasadiVariables(result_py);
691 result_py->emplace_back(
692 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
693 for (size_t m = 0; m < kNumModules; ++m) {
694 if (m == 0) {
695 result_py->emplace_back(" if i == 0:");
696 } else {
697 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
698 }
699 for (int j = 0; j < 2; ++j) {
700 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
701 j, scalar_fn(modules_[m], j)));
702 }
703 }
704 result_py->emplace_back(" else:");
705 result_py->emplace_back(
706 " raise ValueError(\"Invalid module number\")");
707 result_py->emplace_back(absl::Substitute(
708 " return casadi.Function('$0', [X, U], [result])", name));
709 }
710
justinT21446e4f62024-06-16 22:36:10 -0700711 private:
712 static constexpr uint8_t kNumModules = 4;
713
714 Module ModulePhysics(const int m, DenseMatrix mounting_location) {
715 VLOG(1) << "Solving module " << m;
716
717 Module result;
Austin Schuhb8b34be2024-07-14 16:06:19 -0700718 result.mounting_location = mounting_location;
justinT21446e4f62024-06-16 22:36:10 -0700719
720 result.Is = symbol(absl::StrFormat("Is%u", m));
721 result.Id = symbol(absl::StrFormat("Id%u", m));
722
723 RCP<const Symbol> thetamd = symbol(absl::StrFormat("theta_md%u", m));
724 RCP<const Symbol> omegamd = symbol(absl::StrFormat("omega_md%u", m));
725 RCP<const Symbol> alphamd = symbol(absl::StrFormat("alpha_md%u", m));
726
727 result.thetas = symbol(absl::StrFormat("thetas%u", m));
728 result.omegas = symbol(absl::StrFormat("omegas%u", m));
729 result.alphas = symbol(absl::StrFormat("alphas%u", m));
730
731 result.thetad = symbol(absl::StrFormat("thetad%u", m));
732 result.omegad = symbol(absl::StrFormat("omegad%u", m));
733 result.alphad = symbol(absl::StrFormat("alphad%u", m));
734
735 // Velocity of the module in field coordinates
Austin Schuh2a1abec2024-07-10 20:31:16 -0700736 DenseMatrix robot_velocity = DenseMatrix(2, 1, {vx_, vy_});
justinT21446e4f62024-06-16 22:36:10 -0700737 VLOG(1) << "robot velocity: " << robot_velocity.__str__();
738
739 // Velocity of the contact patch in field coordinates
740 DenseMatrix temp_matrix = DenseMatrix(2, 1);
741 DenseMatrix temp_matrix2 = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700742 result.contact_patch_velocity = DenseMatrix(2, 1);
justinT21446e4f62024-06-16 22:36:10 -0700743
Austin Schuhb8b34be2024-07-14 16:06:19 -0700744 mul_dense_dense(R(theta_), result.mounting_location, temp_matrix);
justinT21446e4f62024-06-16 22:36:10 -0700745 add_dense_dense(angle_cross(temp_matrix, omega_), robot_velocity,
746 temp_matrix2);
747 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuh6927bc32024-07-14 17:24:56 -0700748 DenseMatrix(2, 1, {neg(caster_), integer(0)}), temp_matrix);
justinT21446e4f62024-06-16 22:36:10 -0700749 add_dense_dense(temp_matrix2,
750 angle_cross(temp_matrix, add(omega_, result.omegas)),
Austin Schuh2a1abec2024-07-10 20:31:16 -0700751 result.contact_patch_velocity);
justinT21446e4f62024-06-16 22:36:10 -0700752
753 VLOG(1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700754 VLOG(1) << "contact patch velocity: "
755 << result.contact_patch_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -0700756
757 // Relative velocity of the surface of the wheel to the ground.
Austin Schuhb67a38f2024-07-04 13:48:38 -0700758 result.wheel_ground_velocity = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700759 mul_dense_dense(R(neg(add(result.thetas, theta_))),
760 result.contact_patch_velocity,
Austin Schuhb67a38f2024-07-04 13:48:38 -0700761 result.wheel_ground_velocity);
justinT21446e4f62024-06-16 22:36:10 -0700762
Austin Schuhb8b34be2024-07-14 16:06:19 -0700763 // Compute the relative velocity between the wheel surface and the ground in
764 // the wheel coordinate system.
765 result.wheel_slip_velocity = DenseMatrix(2, 1);
766 DenseMatrix wheel_velocity =
767 DenseMatrix(2, 1, {mul(rw_, result.omegad), integer(0)});
768 DenseMatrix negative_wheel_ground_velocity =
769 DenseMatrix(2, 1,
770 {neg(result.wheel_ground_velocity.get(0, 0)),
771 neg(result.wheel_ground_velocity.get(1, 0))});
772 add_dense_dense(negative_wheel_ground_velocity, wheel_velocity,
773 result.wheel_slip_velocity);
774
justinT21446e4f62024-06-16 22:36:10 -0700775 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700776 VLOG(1) << "wheel ground velocity: "
777 << result.wheel_ground_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -0700778
Austin Schuh5ddcb472024-07-21 17:55:34 -0700779 result.slip_angle = sin(neg(atan2(result.wheel_ground_velocity.get(1, 0),
780 result.wheel_ground_velocity.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -0700781
782 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700783 VLOG(1) << "slip angle: " << result.slip_angle->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700784
Austin Schuh2a1abec2024-07-10 20:31:16 -0700785 // TODO(austin): Does this handle decel properly?
Austin Schuhb67a38f2024-07-04 13:48:38 -0700786 result.slip_ratio = div(
Austin Schuh2a1abec2024-07-10 20:31:16 -0700787 sub(mul(rw_, result.omegad), result.wheel_ground_velocity.get(0, 0)),
788 SymEngine::max(
789 {real_double(0.02), abs(result.wheel_ground_velocity.get(0, 0))}));
justinT21446e4f62024-06-16 22:36:10 -0700790 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700791 VLOG(1) << "Slip ratio " << result.slip_ratio->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700792
Austin Schuhb67a38f2024-07-04 13:48:38 -0700793 result.Fwx = simplify(mul(Cx_, result.slip_ratio));
794 result.Fwy = simplify(mul(Cy_, result.slip_angle));
justinT21446e4f62024-06-16 22:36:10 -0700795
Austin Schuh27694fa2024-07-20 16:29:49 -0700796 // The self-aligning moment needs to flip when the module flips direction.
Austin Schuhb8b34be2024-07-14 16:06:19 -0700797 result.Ms = mul(neg(result.Fwy),
Austin Schuh27694fa2024-07-20 16:29:49 -0700798 add(div(mul(sign(result.wheel_ground_velocity.get(0, 0)),
799 contact_patch_length_),
800 integer(3)),
801 caster_));
justinT21446e4f62024-06-16 22:36:10 -0700802 VLOG(1);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700803 VLOG(1) << "Ms " << result.Ms->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700804 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700805 VLOG(1) << "Fwx " << result.Fwx->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700806 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700807 VLOG(1) << "Fwy " << result.Fwy->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700808
809 // alphas = ...
810 RCP<const Basic> lhms =
811 mul(add(neg(wb_), mul(add(rs_, rp_), sub(integer(1), div(rb1_, rp_)))),
Austin Schuh2a1abec2024-07-10 20:31:16 -0700812 mul(div(rw_, rb2_), neg(result.Fwx)));
Austin Schuhb8b34be2024-07-14 16:06:19 -0700813 RCP<const Basic> lhs =
justinT21b65288d2024-07-27 17:15:54 -0700814 add(add(result.Ms, div(mul(Kts_, result.Is), Gs_)), lhms);
justinT21446e4f62024-06-16 22:36:10 -0700815 RCP<const Basic> rhs = add(Jsm_, div(div(Js_, Gs_), Gs_));
816 RCP<const Basic> accel_steer_eqn = simplify(div(lhs, rhs));
817
818 VLOG(1);
819 VLOG(1) << result.alphas->__str__() << " = " << accel_steer_eqn->__str__();
820
821 lhs = sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), result.omegas),
822 mul(Gd1_, mul(Gd2_, omegamd)));
823 RCP<const Basic> dplanitary_eqn = sub(mul(Gd3_, lhs), result.omegad);
824
825 lhs = sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), result.alphas),
826 mul(Gd1_, mul(Gd2_, alphamd)));
827 RCP<const Basic> ddplanitary_eqn = sub(mul(Gd3_, lhs), result.alphad);
828
justinT21b65288d2024-07-27 17:15:54 -0700829 RCP<const Basic> drive_eqn = sub(add(mul(neg(Jdm_), div(alphamd, Gd_)),
830 mul(Ktd_, div(neg(result.Id), Gd_))),
831 mul(neg(result.Fwx), rw_));
justinT21446e4f62024-06-16 22:36:10 -0700832
833 VLOG(1) << "drive_eqn: " << drive_eqn->__str__();
834
835 // Substitute in ddplanitary_eqn so we get rid of alphamd
836 map_basic_basic map;
837 RCP<const Set> reals = interval(NegInf, Inf, true, true);
838 RCP<const Set> solve_solution = solve(ddplanitary_eqn, alphamd, reals);
839 map[alphamd] = solve_solution->get_args()[1]->get_args()[0];
840 VLOG(1) << "temp: " << solve_solution->__str__();
841 RCP<const Basic> drive_eqn_subs = drive_eqn->subs(map);
842
843 map.clear();
844 map[result.alphas] = accel_steer_eqn;
845 RCP<const Basic> drive_eqn_subs2 = drive_eqn_subs->subs(map);
846 RCP<const Basic> drive_eqn_subs3 = simplify(drive_eqn_subs2);
847 VLOG(1) << "drive_eqn simplified: " << drive_eqn_subs3->__str__();
848
849 solve_solution = solve(drive_eqn_subs3, result.alphad, reals);
850
851 RCP<const Basic> drive_accel =
852 simplify(solve_solution->get_args()[1]->get_args()[0]);
853 VLOG(1) << "drive_accel: " << drive_accel->__str__();
854
Austin Schuh2a1abec2024-07-10 20:31:16 -0700855 // Compute the resulting force from the module.
Austin Schuhb8b34be2024-07-14 16:06:19 -0700856 result.F = DenseMatrix(2, 1);
857 mul_dense_dense(R(add(theta_, result.thetas)),
858 DenseMatrix(2, 1, {result.Fwx, result.Fwy}), result.F);
justinT21446e4f62024-06-16 22:36:10 -0700859
Austin Schuhb8b34be2024-07-14 16:06:19 -0700860 RCP<const Basic> torque = force_cross(result.mounting_location, result.F);
justinT21446e4f62024-06-16 22:36:10 -0700861 result.accel = DenseMatrix(2, 1);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700862 mul_dense_scalar(result.F, pow(m_, minus_one), result.accel);
justinT21446e4f62024-06-16 22:36:10 -0700863 result.angular_accel = div(torque, J_);
864 VLOG(1);
865 VLOG(1) << "angular_accel = " << result.angular_accel->__str__();
866
867 VLOG(1);
868 VLOG(1) << "accel(0, 0) = " << result.accel.get(0, 0)->__str__();
869 VLOG(1);
870 VLOG(1) << "accel(1, 0) = " << result.accel.get(1, 0)->__str__();
871
872 result.alphad_eqn = drive_accel;
873 result.alphas_eqn = accel_steer_eqn;
874 return result;
875 }
876
877 DenseMatrix R(const RCP<const Basic> theta) {
878 return DenseMatrix(2, 2,
879 {cos(theta), neg(sin(theta)), sin(theta), cos(theta)});
880 }
881
882 DenseMatrix angle_cross(DenseMatrix a, RCP<const Basic> b) {
Austin Schuh2a1abec2024-07-10 20:31:16 -0700883 return DenseMatrix(2, 1, {mul(neg(a.get(1, 0)), b), mul(a.get(0, 0), b)});
justinT21446e4f62024-06-16 22:36:10 -0700884 }
885
886 RCP<const Basic> force_cross(DenseMatrix r, DenseMatrix f) {
887 return sub(mul(r.get(0, 0), f.get(1, 0)), mul(r.get(1, 0), f.get(0, 0)));
888 }
889
890 // z represents the number of teeth per gear, theta is the angle between
891 // shafts(in degrees), D_02 is the pitch diameter of gear 2 and b_2 is the
892 // length of the tooth of gear 2
893 // returns std::pair(r_01, r_02)
894 std::pair<double, double> GetBevelPitchRadius(double z1, double z2,
895 double theta, double D_02,
896 double b_2) {
897 double gamma_1 = std::atan2(z1, z2);
898 double gamma_2 = theta / 180.0 * std::numbers::pi - gamma_1;
899 double R_m = D_02 / 2 / std::sin(gamma_2) - b_2 / 2;
900 return std::pair(R_m * std::cos(gamma_2), R_m * std::sin(gamma_2));
901 }
902
903 Motor drive_motor_;
904 Motor steer_motor_;
905
906 RCP<const Basic> Cx_;
907 RCP<const Basic> Cy_;
Austin Schuh2a1abec2024-07-10 20:31:16 -0700908 RCP<const Basic> rw_;
justinT21446e4f62024-06-16 22:36:10 -0700909 RCP<const Basic> m_;
910 RCP<const Basic> J_;
911 RCP<const Basic> Gd1_;
912 RCP<const Basic> rs_;
913 RCP<const Basic> rp_;
914 RCP<const Basic> Gd2_;
915 RCP<const Basic> rb1_;
916 RCP<const Basic> rb2_;
917 RCP<const Basic> Gd3_;
918 RCP<const Basic> Gd_;
919 RCP<const Basic> Js_;
920 RCP<const Basic> Gs_;
921 RCP<const Basic> wb_;
922 RCP<const Basic> Jdm_;
923 RCP<const Basic> Jsm_;
924 RCP<const Basic> Kts_;
925 RCP<const Basic> Ktd_;
926 RCP<const Basic> robot_width_;
927 RCP<const Basic> caster_;
928 RCP<const Basic> contact_patch_length_;
929 RCP<const Basic> x_;
930 RCP<const Basic> y_;
931 RCP<const Basic> theta_;
932 RCP<const Basic> vx_;
933 RCP<const Basic> vy_;
934 RCP<const Basic> omega_;
935 RCP<const Basic> ax_;
936 RCP<const Basic> ay_;
937 RCP<const Basic> atheta_;
938
939 std::array<Module, kNumModules> modules_;
940
941 DenseMatrix accel_;
942 RCP<const Basic> angular_accel_;
943};
944
945} // namespace frc971::control_loops::swerve
946
947int main(int argc, char **argv) {
948 aos::InitGoogle(&argc, &argv);
949
950 frc971::control_loops::swerve::SwerveSimulation sim;
951
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700952 if (!absl::GetFlag(FLAGS_cc_output_path).empty() &&
953 !absl::GetFlag(FLAGS_h_output_path).empty()) {
954 sim.Write(absl::GetFlag(FLAGS_cc_output_path),
955 absl::GetFlag(FLAGS_h_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -0700956 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700957 if (!absl::GetFlag(FLAGS_py_output_path).empty()) {
958 sim.WritePy(absl::GetFlag(FLAGS_py_output_path));
justinT21446e4f62024-06-16 22:36:10 -0700959 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700960 if (!absl::GetFlag(FLAGS_casadi_py_output_path).empty()) {
961 sim.WriteCasadi(absl::GetFlag(FLAGS_casadi_py_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -0700962 }
justinT21446e4f62024-06-16 22:36:10 -0700963
964 return 0;
965}