blob: 19b4f127b59139cd4f20f5ab48806c015e3a3d7e [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");
Austin Schuh99f7c6a2024-06-25 22:07:44 -070033ABSL_FLAG(std::string, casadi_py_output_path, "",
34 "Path to write casadi generated py code to");
Austin Schuh27694fa2024-07-20 16:29:49 -070035ABSL_FLAG(double, caster, 0.01, "Caster in meters for the module.");
justinT21446e4f62024-06-16 22:36:10 -070036
Austin Schuh99f7c6a2024-06-25 22:07:44 -070037ABSL_FLAG(bool, symbolic, false, "If true, write everything out symbolically.");
justinT21446e4f62024-06-16 22:36:10 -070038
justinT21942892b2024-07-02 22:33:50 -070039using SymEngine::abs;
justinT21446e4f62024-06-16 22:36:10 -070040using SymEngine::add;
41using SymEngine::atan2;
42using SymEngine::Basic;
43using SymEngine::ccode;
44using SymEngine::cos;
45using SymEngine::DenseMatrix;
46using SymEngine::div;
Austin Schuh78a1b312024-08-18 17:21:34 -070047using SymEngine::exp;
justinT21446e4f62024-06-16 22:36:10 -070048using SymEngine::Inf;
49using SymEngine::integer;
50using SymEngine::map_basic_basic;
51using SymEngine::minus_one;
52using SymEngine::neg;
53using SymEngine::NegInf;
54using SymEngine::pow;
55using SymEngine::RCP;
56using SymEngine::real_double;
57using SymEngine::RealDouble;
58using SymEngine::Set;
59using SymEngine::simplify;
60using SymEngine::sin;
61using SymEngine::solve;
62using SymEngine::symbol;
63using SymEngine::Symbol;
64
65namespace frc971::control_loops::swerve {
66
67// State per module.
68struct Module {
Austin Schuh6ea789e2024-07-27 13:45:53 -070069 DenseMatrix mounting_location;
70
justinT21446e4f62024-06-16 22:36:10 -070071 RCP<const Symbol> Is;
72
73 RCP<const Symbol> Id;
74
75 RCP<const Symbol> thetas;
76 RCP<const Symbol> omegas;
justinT21446e4f62024-06-16 22:36:10 -070077
justinT21446e4f62024-06-16 22:36:10 -070078 RCP<const Symbol> omegad;
justinT21446e4f62024-06-16 22:36:10 -070079
Austin Schuh2a1abec2024-07-10 20:31:16 -070080 DenseMatrix contact_patch_velocity;
Austin Schuhb67a38f2024-07-04 13:48:38 -070081 DenseMatrix wheel_ground_velocity;
Austin Schuhb8b34be2024-07-14 16:06:19 -070082 DenseMatrix wheel_slip_velocity;
Austin Schuhb67a38f2024-07-04 13:48:38 -070083 RCP<const Basic> slip_angle;
84 RCP<const Basic> slip_ratio;
85
Austin Schuhb8b34be2024-07-14 16:06:19 -070086 RCP<const Basic> Ms;
Austin Schuhb67a38f2024-07-04 13:48:38 -070087 RCP<const Basic> Fwy;
88
Austin Schuh6ea789e2024-07-27 13:45:53 -070089 struct Full {
90 RCP<const Basic> Fwx;
91 DenseMatrix F;
92
93 RCP<const Basic> torque;
94
95 RCP<const Basic> alphas_eqn;
96 RCP<const Basic> alphad_eqn;
97 } full;
98
99 struct Direct {
100 RCP<const Basic> Fwx;
101 DenseMatrix F;
102
103 RCP<const Basic> torque;
104
105 RCP<const Basic> alphas_eqn;
106 } direct;
justinT21446e4f62024-06-16 22:36:10 -0700107};
108
Austin Schuh6ea789e2024-07-27 13:45:53 -0700109DenseMatrix SumMatrices(DenseMatrix a) { return a; }
110
111template <typename... Args>
112DenseMatrix SumMatrices(DenseMatrix a, Args... args) {
113 DenseMatrix result = DenseMatrix(2, 1, {integer(0), integer(0)});
114
115 DenseMatrix b = SumMatrices(args...);
116 add_dense_dense(a, b, result);
117 return result;
118}
119
justinT21446e4f62024-06-16 22:36:10 -0700120class SwerveSimulation {
121 public:
122 SwerveSimulation() : drive_motor_(KrakenFOC()), steer_motor_(KrakenFOC()) {
123 auto fx = symbol("fx");
124 auto fy = symbol("fy");
125 auto moment = symbol("moment");
126
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700127 if (absl::GetFlag(FLAGS_symbolic)) {
justinT21446e4f62024-06-16 22:36:10 -0700128 Cx_ = symbol("Cx");
129 Cy_ = symbol("Cy");
130
Austin Schuh2a1abec2024-07-10 20:31:16 -0700131 rw_ = symbol("rw");
justinT21446e4f62024-06-16 22:36:10 -0700132
133 m_ = symbol("m");
134 J_ = symbol("J");
135
136 Gd1_ = symbol("Gd1");
137 rs_ = symbol("rs");
138 rp_ = symbol("rp");
139 Gd2_ = symbol("Gd2");
140
141 rb1_ = symbol("rb1");
142 rb2_ = symbol("rb2");
143
144 Gd2_ = symbol("Gd3");
145 Gd_ = symbol("Gd");
146
147 Js_ = symbol("Js");
148
149 Gs_ = symbol("Gs");
150 wb_ = symbol("wb");
151
152 Jdm_ = symbol("Jdm");
153 Jsm_ = symbol("Jsm");
154 Kts_ = symbol("Kts");
155 Ktd_ = symbol("Ktd");
156
157 robot_width_ = symbol("robot_width");
158
159 caster_ = symbol("caster");
160 contact_patch_length_ = symbol("Lcp");
161 } else {
Austin Schuhb8b34be2024-07-14 16:06:19 -0700162 Cx_ = real_double(25.0 * 9.8 / 4.0 / 0.05);
justinT21446e4f62024-06-16 22:36:10 -0700163 Cy_ = real_double(5 * 9.8 / 0.05 / 4.0);
164
Austin Schuh2a1abec2024-07-10 20:31:16 -0700165 rw_ = real_double(2 * 0.0254);
justinT21446e4f62024-06-16 22:36:10 -0700166
167 m_ = real_double(25.0); // base is 20 kg without battery
168 J_ = real_double(6.0);
169
170 Gd1_ = real_double(12.0 / 42.0);
171 rs_ = real_double(28.0 / 20.0 / 2.0);
172 rp_ = real_double(18.0 / 20.0 / 2.0);
173 Gd2_ = div(rs_, rp_);
174
175 // 15 / 45 bevel ratio, calculated using python script ported over to
176 // GetBevelPitchRadius(double
177 // TODO(Justin): Use the function instead of computed constantss
178 rb1_ = real_double(0.3805473);
179 rb2_ = real_double(1.14164);
180
181 Gd3_ = div(rb1_, rb2_);
182 Gd_ = mul(mul(Gd1_, Gd2_), Gd3_);
183
Austin Schuhb8b34be2024-07-14 16:06:19 -0700184 Js_ = real_double(0.001);
justinT21446e4f62024-06-16 22:36:10 -0700185
186 Gs_ = real_double(35.0 / 468.0);
187 wb_ = real_double(0.725);
188
189 Jdm_ = real_double(drive_motor_.motor_inertia);
190 Jsm_ = real_double(steer_motor_.motor_inertia);
191 Kts_ = real_double(steer_motor_.Kt);
192 Ktd_ = real_double(drive_motor_.Kt);
193
194 robot_width_ = real_double(24.75 * 0.0254);
195
Austin Schuh27694fa2024-07-20 16:29:49 -0700196 caster_ = real_double(absl::GetFlag(FLAGS_caster));
justinT21446e4f62024-06-16 22:36:10 -0700197 contact_patch_length_ = real_double(0.02);
198 }
199
200 x_ = symbol("x");
201 y_ = symbol("y");
202 theta_ = symbol("theta");
203
204 vx_ = symbol("vx");
205 vy_ = symbol("vy");
206 omega_ = symbol("omega");
207
208 ax_ = symbol("ax");
209 ay_ = symbol("ay");
210 atheta_ = symbol("atheta");
211
212 // Now, compute the accelerations due to the disturbance forces.
justinT21446e4f62024-06-16 22:36:10 -0700213 DenseMatrix external_accel = DenseMatrix(2, 1, {div(fx, m_), div(fy, m_)});
Austin Schuh6ea789e2024-07-27 13:45:53 -0700214 DenseMatrix external_force = DenseMatrix(2, 1, {fx, fy});
justinT21446e4f62024-06-16 22:36:10 -0700215
216 // And compute the physics contributions from each module.
217 modules_[0] = ModulePhysics(
218 0, DenseMatrix(
219 2, 1,
220 {div(robot_width_, integer(2)), div(robot_width_, integer(2))}));
221 modules_[1] =
222 ModulePhysics(1, DenseMatrix(2, 1,
223 {div(robot_width_, integer(-2)),
224 div(robot_width_, integer(2))}));
225 modules_[2] =
226 ModulePhysics(2, DenseMatrix(2, 1,
227 {div(robot_width_, integer(-2)),
228 div(robot_width_, integer(-2))}));
229 modules_[3] =
230 ModulePhysics(3, DenseMatrix(2, 1,
231 {div(robot_width_, integer(2)),
232 div(robot_width_, integer(-2))}));
233
234 // And convert them into the overall robot contribution.
Austin Schuh6ea789e2024-07-27 13:45:53 -0700235 DenseMatrix net_full_force =
236 SumMatrices(modules_[0].full.F, modules_[1].full.F, modules_[2].full.F,
237 modules_[3].full.F, external_force);
justinT21446e4f62024-06-16 22:36:10 -0700238
Austin Schuh6ea789e2024-07-27 13:45:53 -0700239 DenseMatrix net_direct_force =
240 SumMatrices(modules_[0].direct.F, modules_[1].direct.F,
241 modules_[2].direct.F, modules_[3].direct.F, external_force);
justinT21446e4f62024-06-16 22:36:10 -0700242
Austin Schuh6ea789e2024-07-27 13:45:53 -0700243 full_accel_ = DenseMatrix(2, 1);
244 mul_dense_scalar(net_full_force, pow(m_, minus_one), full_accel_);
justinT21446e4f62024-06-16 22:36:10 -0700245
Austin Schuh6ea789e2024-07-27 13:45:53 -0700246 full_angular_accel_ = div(
247 add(moment, add(add(modules_[0].full.torque, modules_[1].full.torque),
248 add(modules_[2].full.torque, modules_[3].full.torque))),
249 J_);
justinT21446e4f62024-06-16 22:36:10 -0700250
Austin Schuh6ea789e2024-07-27 13:45:53 -0700251 direct_accel_ = DenseMatrix(2, 1);
252 mul_dense_scalar(net_direct_force, pow(m_, minus_one), direct_accel_);
justinT21942892b2024-07-02 22:33:50 -0700253
Austin Schuh6ea789e2024-07-27 13:45:53 -0700254 direct_angular_accel_ =
255 div(add(moment,
256 add(add(modules_[0].direct.torque, modules_[1].direct.torque),
257 add(modules_[2].direct.torque, modules_[3].direct.torque))),
258 J_);
justinT21942892b2024-07-02 22:33:50 -0700259
Austin Schuh6ea789e2024-07-27 13:45:53 -0700260 VLOG(1) << "accel(0, 0) = " << ccode(*full_accel_.get(0, 0));
261 VLOG(1) << "accel(1, 0) = " << ccode(*full_accel_.get(1, 0));
262 VLOG(1) << "angular_accel = " << ccode(*full_angular_accel_);
justinT21942892b2024-07-02 22:33:50 -0700263 }
264
justinT21446e4f62024-06-16 22:36:10 -0700265 // Writes the physics out to the provided .cc and .h path.
266 void Write(std::string_view cc_path, std::string_view h_path) {
267 std::vector<std::string> result_cc;
268 std::vector<std::string> result_h;
269
Austin Schuh0f881092024-06-28 15:36:48 -0700270 std::string_view include_guard_stripped = h_path;
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700271 CHECK(absl::ConsumePrefix(&include_guard_stripped,
272 absl::GetFlag(FLAGS_output_base)));
justinT21446e4f62024-06-16 22:36:10 -0700273 std::string include_guard =
274 absl::StrReplaceAll(absl::AsciiStrToUpper(include_guard_stripped),
275 {{"/", "_"}, {".", "_"}});
276
277 // Write out the header.
278 result_h.emplace_back(absl::Substitute("#ifndef $0_", include_guard));
279 result_h.emplace_back(absl::Substitute("#define $0_", include_guard));
280 result_h.emplace_back("");
281 result_h.emplace_back("#include <Eigen/Dense>");
282 result_h.emplace_back("");
283 result_h.emplace_back("namespace frc971::control_loops::swerve {");
284 result_h.emplace_back("");
285 result_h.emplace_back("// Returns the derivative of our state vector");
286 result_h.emplace_back("// [thetas0, thetad0, omegas0, omegad0,");
287 result_h.emplace_back("// thetas1, thetad1, omegas1, omegad1,");
288 result_h.emplace_back("// thetas2, thetad2, omegas2, omegad2,");
289 result_h.emplace_back("// thetas3, thetad3, omegas3, omegad3,");
290 result_h.emplace_back("// x, y, theta, vx, vy, omega,");
291 result_h.emplace_back("// Fx, Fy, Moment]");
292 result_h.emplace_back("Eigen::Matrix<double, 25, 1> SwervePhysics(");
293 result_h.emplace_back(
294 " Eigen::Map<const Eigen::Matrix<double, 25, 1>> X,");
295 result_h.emplace_back(
296 " Eigen::Map<const Eigen::Matrix<double, 8, 1>> U);");
297 result_h.emplace_back("");
298 result_h.emplace_back("} // namespace frc971::control_loops::swerve");
299 result_h.emplace_back("");
300 result_h.emplace_back(absl::Substitute("#endif // $0_", include_guard));
301
302 // Write out the .cc
303 result_cc.emplace_back(
304 absl::Substitute("#include \"$0\"", include_guard_stripped));
305 result_cc.emplace_back("");
306 result_cc.emplace_back("#include <cmath>");
307 result_cc.emplace_back("");
308 result_cc.emplace_back("namespace frc971::control_loops::swerve {");
309 result_cc.emplace_back("");
310 result_cc.emplace_back("Eigen::Matrix<double, 25, 1> SwervePhysics(");
311 result_cc.emplace_back(
312 " Eigen::Map<const Eigen::Matrix<double, 25, 1>> X,");
313 result_cc.emplace_back(
314 " Eigen::Map<const Eigen::Matrix<double, 8, 1>> U) {");
315 result_cc.emplace_back(" Eigen::Matrix<double, 25, 1> result;");
316
317 // Start by writing out variables matching each of the symbol names we use
318 // so we don't have to modify the computed equations too much.
319 for (size_t m = 0; m < kNumModules; ++m) {
320 result_cc.emplace_back(
321 absl::Substitute(" const double thetas$0 = X($1, 0);", m, m * 4));
322 result_cc.emplace_back(absl::Substitute(
323 " const double omegas$0 = X($1, 0);", m, m * 4 + 2));
324 result_cc.emplace_back(absl::Substitute(
325 " const double omegad$0 = X($1, 0);", m, m * 4 + 3));
326 }
327
328 result_cc.emplace_back(absl::Substitute(" const double theta = X($0, 0);",
329 kNumModules * 4 + 2));
330 result_cc.emplace_back(
331 absl::Substitute(" const double vx = X($0, 0);", kNumModules * 4 + 3));
332 result_cc.emplace_back(
333 absl::Substitute(" const double vy = X($0, 0);", kNumModules * 4 + 4));
334 result_cc.emplace_back(absl::Substitute(" const double omega = X($0, 0);",
335 kNumModules * 4 + 5));
336
337 result_cc.emplace_back(
338 absl::Substitute(" const double fx = X($0, 0);", kNumModules * 4 + 6));
339 result_cc.emplace_back(
340 absl::Substitute(" const double fy = X($0, 0);", kNumModules * 4 + 7));
341 result_cc.emplace_back(absl::Substitute(" const double moment = X($0, 0);",
342 kNumModules * 4 + 8));
343
344 // Now do the same for the inputs.
345 for (size_t m = 0; m < kNumModules; ++m) {
346 result_cc.emplace_back(
347 absl::Substitute(" const double Is$0 = U($1, 0);", m, m * 2));
348 result_cc.emplace_back(
349 absl::Substitute(" const double Id$0 = U($1, 0);", m, m * 2 + 1));
350 }
351
352 result_cc.emplace_back("");
353
354 // And then write out the derivative of each state.
355 for (size_t m = 0; m < kNumModules; ++m) {
356 result_cc.emplace_back(
357 absl::Substitute(" result($0, 0) = omegas$1;", m * 4, m));
358 result_cc.emplace_back(
359 absl::Substitute(" result($0, 0) = omegad$1;", m * 4 + 1, m));
360
Austin Schuh6ea789e2024-07-27 13:45:53 -0700361 result_cc.emplace_back(
362 absl::Substitute(" result($0, 0) = $1;", m * 4 + 2,
363 ccode(*modules_[m].full.alphas_eqn)));
364 result_cc.emplace_back(
365 absl::Substitute(" result($0, 0) = $1;", m * 4 + 3,
366 ccode(*modules_[m].full.alphad_eqn)));
justinT21446e4f62024-06-16 22:36:10 -0700367 }
368
369 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700370 absl::Substitute(" result($0, 0) = vx;", kNumModules * 4 + 0));
justinT21446e4f62024-06-16 22:36:10 -0700371 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700372 absl::Substitute(" result($0, 0) = vy;", kNumModules * 4 + 1));
justinT21446e4f62024-06-16 22:36:10 -0700373 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700374 absl::Substitute(" result($0, 0) = omega;", kNumModules * 4 + 2));
justinT21446e4f62024-06-16 22:36:10 -0700375
justinT21446e4f62024-06-16 22:36:10 -0700376 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700377 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700378 ccode(*full_accel_.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -0700379 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700380 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700381 ccode(*full_accel_.get(1, 0))));
382 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
383 kNumModules * 4 + 5,
384 ccode(*full_angular_accel_)));
justinT21446e4f62024-06-16 22:36:10 -0700385
386 result_cc.emplace_back(
387 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 6));
388 result_cc.emplace_back(
389 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 7));
390 result_cc.emplace_back(
391 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 8));
392
393 result_cc.emplace_back("");
394 result_cc.emplace_back(" return result;");
395 result_cc.emplace_back("}");
396 result_cc.emplace_back("");
397 result_cc.emplace_back("} // namespace frc971::control_loops::swerve");
398
399 aos::util::WriteStringToFileOrDie(cc_path, absl::StrJoin(result_cc, "\n"));
400 aos::util::WriteStringToFileOrDie(h_path, absl::StrJoin(result_h, "\n"));
401 }
402
Austin Schuhb67a38f2024-07-04 13:48:38 -0700403 void WriteCasadiVariables(std::vector<std::string> *result_py) {
404 result_py->emplace_back(" sin = casadi.sin");
405 result_py->emplace_back(" cos = casadi.cos");
Austin Schuh78a1b312024-08-18 17:21:34 -0700406 result_py->emplace_back(" exp = casadi.exp");
Austin Schuhbd75c482024-08-18 00:03:51 -0700407 result_py->emplace_back(" atan2 = soft_atan2");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700408 result_py->emplace_back(" fmax = casadi.fmax");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700409 result_py->emplace_back(" fabs = casadi.fabs");
410
411 // Start by writing out variables matching each of the symbol names we use
412 // so we don't have to modify the computed equations too much.
413 for (size_t m = 0; m < kNumModules; ++m) {
414 result_py->emplace_back(
415 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 4));
416 result_py->emplace_back(
417 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 4 + 2));
418 result_py->emplace_back(
419 absl::Substitute(" omegad$0 = X[$1, 0]", m, m * 4 + 3));
420 }
421
422 result_py->emplace_back(
423 absl::Substitute(" theta = X[$0, 0]", kNumModules * 4 + 2));
424 result_py->emplace_back(
425 absl::Substitute(" vx = X[$0, 0]", kNumModules * 4 + 3));
426 result_py->emplace_back(
427 absl::Substitute(" vy = X[$0, 0]", kNumModules * 4 + 4));
428 result_py->emplace_back(
429 absl::Substitute(" omega = X[$0, 0]", kNumModules * 4 + 5));
430
431 result_py->emplace_back(
432 absl::Substitute(" fx = X[$0, 0]", kNumModules * 4 + 6));
433 result_py->emplace_back(
434 absl::Substitute(" fy = X[$0, 0]", kNumModules * 4 + 7));
435 result_py->emplace_back(
436 absl::Substitute(" moment = X[$0, 0]", kNumModules * 4 + 8));
437
438 // Now do the same for the inputs.
439 for (size_t m = 0; m < kNumModules; ++m) {
440 result_py->emplace_back(
441 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
442 result_py->emplace_back(
443 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
444 }
445 }
446
Austin Schuh6ea789e2024-07-27 13:45:53 -0700447 void WriteCasadiVelocityVariables(std::vector<std::string> *result_py) {
448 result_py->emplace_back(" sin = casadi.sin");
Austin Schuh78a1b312024-08-18 17:21:34 -0700449 result_py->emplace_back(" exp = casadi.exp");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700450 result_py->emplace_back(" cos = casadi.cos");
Austin Schuhbd75c482024-08-18 00:03:51 -0700451 result_py->emplace_back(" atan2 = soft_atan2");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700452 result_py->emplace_back(" fmax = casadi.fmax");
453 result_py->emplace_back(" fabs = casadi.fabs");
454
455 // Start by writing out variables matching each of the symbol names we use
456 // so we don't have to modify the computed equations too much.
457 for (size_t m = 0; m < kNumModules; ++m) {
458 result_py->emplace_back(
459 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 2 + 0));
460 result_py->emplace_back(
461 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 2 + 1));
462 }
463
464 result_py->emplace_back(
465 absl::Substitute(" theta = X[$0, 0]", kNumModules * 2 + 0));
466 result_py->emplace_back(
467 absl::Substitute(" vx = X[$0, 0]", kNumModules * 2 + 1));
468 result_py->emplace_back(
469 absl::Substitute(" vy = X[$0, 0]", kNumModules * 2 + 2));
470 result_py->emplace_back(
471 absl::Substitute(" omega = X[$0, 0]", kNumModules * 2 + 3));
472
473 // result_py->emplace_back(
474 // absl::Substitute(" fx = X[$0, 0]", kNumModules * 3 + 4));
475 // result_py->emplace_back(
476 // absl::Substitute(" fy = X[$0, 0]", kNumModules * 3 + 5));
477 // result_py->emplace_back(
478 // absl::Substitute(" moment = X[$0, 0]", kNumModules * 3 + 6));
479 //
480 result_py->emplace_back(" fx = 0");
481 result_py->emplace_back(" fy = 0");
482 result_py->emplace_back(" moment = 0");
483
484 // Now do the same for the inputs.
485 for (size_t m = 0; m < kNumModules; ++m) {
486 result_py->emplace_back(
487 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
488 result_py->emplace_back(
489 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
490 }
491 }
492
Austin Schuh0f881092024-06-28 15:36:48 -0700493 // Writes the physics out to the provided .cc and .h path.
494 void WriteCasadi(std::string_view py_path) {
495 std::vector<std::string> result_py;
496
497 // Write out the header.
498 result_py.emplace_back("#!/usr/bin/python3");
499 result_py.emplace_back("");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700500 result_py.emplace_back("import casadi, numpy");
Austin Schuh0f881092024-06-28 15:36:48 -0700501 result_py.emplace_back("");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700502 result_py.emplace_back(absl::Substitute("WHEEL_RADIUS = $0", ccode(*rw_)));
503 result_py.emplace_back(
504 absl::Substitute("ROBOT_WIDTH = $0", ccode(*robot_width_)));
505 result_py.emplace_back(absl::Substitute("CASTER = $0", ccode(*caster_)));
Austin Schuh6ea789e2024-07-27 13:45:53 -0700506 result_py.emplace_back("STATE_THETAS0 = 0");
507 result_py.emplace_back("STATE_THETAD0 = 1");
508 result_py.emplace_back("STATE_OMEGAS0 = 2");
509 result_py.emplace_back("STATE_OMEGAD0 = 3");
510 result_py.emplace_back("STATE_THETAS1 = 4");
511 result_py.emplace_back("STATE_THETAD1 = 5");
512 result_py.emplace_back("STATE_OMEGAS1 = 6");
513 result_py.emplace_back("STATE_OMEGAD1 = 7");
514 result_py.emplace_back("STATE_THETAS2 = 8");
515 result_py.emplace_back("STATE_THETAD2 = 9");
516 result_py.emplace_back("STATE_OMEGAS2 = 10");
517 result_py.emplace_back("STATE_OMEGAD2 = 11");
518 result_py.emplace_back("STATE_THETAS3 = 12");
519 result_py.emplace_back("STATE_THETAD3 = 13");
520 result_py.emplace_back("STATE_OMEGAS3 = 14");
521 result_py.emplace_back("STATE_OMEGAD3 = 15");
522 result_py.emplace_back("STATE_X = 16");
523 result_py.emplace_back("STATE_Y = 17");
524 result_py.emplace_back("STATE_THETA = 18");
525 result_py.emplace_back("STATE_VX = 19");
526 result_py.emplace_back("STATE_VY = 20");
527 result_py.emplace_back("STATE_OMEGA = 21");
528 result_py.emplace_back("STATE_FX = 22");
529 result_py.emplace_back("STATE_FY = 23");
530 result_py.emplace_back("STATE_MOMENT = 24");
531 result_py.emplace_back("NUM_STATES = 25");
532 result_py.emplace_back("");
533 result_py.emplace_back("VELOCITY_STATE_THETAS0 = 0");
534 result_py.emplace_back("VELOCITY_STATE_OMEGAS0 = 1");
535 result_py.emplace_back("VELOCITY_STATE_THETAS1 = 2");
536 result_py.emplace_back("VELOCITY_STATE_OMEGAS1 = 3");
537 result_py.emplace_back("VELOCITY_STATE_THETAS2 = 4");
538 result_py.emplace_back("VELOCITY_STATE_OMEGAS2 = 5");
539 result_py.emplace_back("VELOCITY_STATE_THETAS3 = 6");
540 result_py.emplace_back("VELOCITY_STATE_OMEGAS3 = 7");
541 result_py.emplace_back("VELOCITY_STATE_THETA = 8");
542 result_py.emplace_back("VELOCITY_STATE_VX = 9");
543 result_py.emplace_back("VELOCITY_STATE_VY = 10");
544 result_py.emplace_back("VELOCITY_STATE_OMEGA = 11");
545 // result_py.emplace_back("VELOCITY_STATE_FX = 16");
546 // result_py.emplace_back("VELOCITY_STATE_FY = 17");
547 // result_py.emplace_back("VELOCITY_STATE_MOMENT = 18");
548 result_py.emplace_back("NUM_VELOCITY_STATES = 12");
549 result_py.emplace_back("");
550 result_py.emplace_back("def to_velocity_state(X):");
551 result_py.emplace_back(" return numpy.array([");
552 result_py.emplace_back(" [X[STATE_THETAS0, 0]],");
553 result_py.emplace_back(" [X[STATE_OMEGAS0, 0]],");
554 result_py.emplace_back(" [X[STATE_THETAS1, 0]],");
555 result_py.emplace_back(" [X[STATE_OMEGAS1, 0]],");
556 result_py.emplace_back(" [X[STATE_THETAS2, 0]],");
557 result_py.emplace_back(" [X[STATE_OMEGAS2, 0]],");
558 result_py.emplace_back(" [X[STATE_THETAS3, 0]],");
559 result_py.emplace_back(" [X[STATE_OMEGAS3, 0]],");
560 result_py.emplace_back(" [X[STATE_THETA, 0]],");
561 result_py.emplace_back(" [X[STATE_VX, 0]],");
562 result_py.emplace_back(" [X[STATE_VY, 0]],");
563 result_py.emplace_back(" [X[STATE_OMEGA, 0]],");
564 // result_py.emplace_back(" [X[STATE_FX, 0]],");
565 // result_py.emplace_back(" [X[STATE_FY, 0]],");
566 // result_py.emplace_back(" [X[STATE_MOMENT, 0]],");
567 result_py.emplace_back(" ])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700568 result_py.emplace_back("");
Austin Schuhbd75c482024-08-18 00:03:51 -0700569 constexpr double kLogGain = 1.0 / 0.05;
570 result_py.emplace_back("def soft_atan2(y, x):");
571 result_py.emplace_back(" return casadi.arctan2(");
572 result_py.emplace_back(" y,");
573 result_py.emplace_back(" casadi.logsumexp(casadi.SX(numpy.array(");
574 result_py.emplace_back(absl::Substitute(
575 " [1.0, casadi.fabs(x) * $0.0]))) / $0.0)", kLogGain));
576 result_py.emplace_back("");
Austin Schuh98fbbe82024-08-18 23:07:26 -0700577 result_py.emplace_back("# Is = STEER_CURRENT_COUPLING_FACTOR * Id");
578 result_py.emplace_back(absl::Substitute(
579 "STEER_CURRENT_COUPLING_FACTOR = $0",
580 ccode(*(neg(
581 mul(div(Gs_, Kts_),
582 mul(div(Ktd_, mul(Gd_, rw_)),
583 neg(mul(add(neg(wb_), mul(add(rs_, rp_),
584 sub(integer(1), div(rb1_, rp_)))),
585 div(rw_, rb2_))))))))));
586 result_py.emplace_back("");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700587
Austin Schuh0f881092024-06-28 15:36:48 -0700588 result_py.emplace_back("# Returns the derivative of our state vector");
589 result_py.emplace_back("# [thetas0, thetad0, omegas0, omegad0,");
590 result_py.emplace_back("# thetas1, thetad1, omegas1, omegad1,");
591 result_py.emplace_back("# thetas2, thetad2, omegas2, omegad2,");
592 result_py.emplace_back("# thetas3, thetad3, omegas3, omegad3,");
593 result_py.emplace_back("# x, y, theta, vx, vy, omega,");
594 result_py.emplace_back("# Fx, Fy, Moment]");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700595 result_py.emplace_back("def swerve_full_dynamics(X, U):");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700596 WriteCasadiVariables(&result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700597
598 result_py.emplace_back("");
599 result_py.emplace_back(" result = casadi.SX.sym('result', 25, 1)");
600 result_py.emplace_back("");
601
602 // And then write out the derivative of each state.
603 for (size_t m = 0; m < kNumModules; ++m) {
604 result_py.emplace_back(
605 absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m));
606 result_py.emplace_back(
607 absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m));
608
Austin Schuh6ea789e2024-07-27 13:45:53 -0700609 result_py.emplace_back(
610 absl::Substitute(" result[$0, 0] = $1", m * 4 + 2,
611 ccode(*modules_[m].full.alphas_eqn)));
612 result_py.emplace_back(
613 absl::Substitute(" result[$0, 0] = $1", m * 4 + 3,
614 ccode(*modules_[m].full.alphad_eqn)));
Austin Schuh0f881092024-06-28 15:36:48 -0700615 }
616
617 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700618 absl::Substitute(" result[$0, 0] = vx", kNumModules * 4 + 0));
Austin Schuh0f881092024-06-28 15:36:48 -0700619 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700620 absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1));
Austin Schuh0f881092024-06-28 15:36:48 -0700621 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700622 absl::Substitute(" result[$0, 0] = omega", kNumModules * 4 + 2));
Austin Schuh0f881092024-06-28 15:36:48 -0700623
Austin Schuh0f881092024-06-28 15:36:48 -0700624 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700625 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700626 ccode(*full_accel_.get(0, 0))));
Austin Schuh0f881092024-06-28 15:36:48 -0700627 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700628 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700629 ccode(*full_accel_.get(1, 0))));
630 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
631 kNumModules * 4 + 5,
632 ccode(*full_angular_accel_)));
Austin Schuh0f881092024-06-28 15:36:48 -0700633
634 result_py.emplace_back(
635 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6));
636 result_py.emplace_back(
637 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7));
638 result_py.emplace_back(
639 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8));
640
641 result_py.emplace_back("");
642 result_py.emplace_back(
643 " return casadi.Function('xdot', [X, U], [result])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700644
Austin Schuh6ea789e2024-07-27 13:45:53 -0700645 result_py.emplace_back("");
646
647 result_py.emplace_back("# Returns the derivative of our state vector");
648 result_py.emplace_back("# [thetas0, omegas0,");
649 result_py.emplace_back("# thetas1, omegas1,");
650 result_py.emplace_back("# thetas2, omegas2,");
651 result_py.emplace_back("# thetas3, omegas3,");
652 result_py.emplace_back("# theta, vx, vy, omega]");
653 result_py.emplace_back("def velocity_swerve_physics(X, U):");
654 WriteCasadiVelocityVariables(&result_py);
655
656 result_py.emplace_back("");
657 result_py.emplace_back(
658 " result = casadi.SX.sym('result', NUM_VELOCITY_STATES, 1)");
659 result_py.emplace_back("");
660
661 // And then write out the derivative of each state.
662 for (size_t m = 0; m < kNumModules; ++m) {
663 result_py.emplace_back(
664 absl::Substitute(" result[$0, 0] = omegas$1", m * 2 + 0, m));
665 result_py.emplace_back(
666 absl::Substitute(" result[$0, 0] = $1", m * 2 + 1,
667 ccode(*modules_[m].direct.alphas_eqn)));
668 }
669 result_py.emplace_back(
670 absl::Substitute(" result[$0, 0] = omega", kNumModules * 2 + 0));
671
672 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
673 kNumModules * 2 + 1,
674 ccode(*direct_accel_.get(0, 0))));
675 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
676 kNumModules * 2 + 2,
677 ccode(*direct_accel_.get(1, 0))));
678 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
679 kNumModules * 2 + 3,
680 ccode(*direct_angular_accel_)));
681
682 // result_py.emplace_back(
683 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 4));
684 // result_py.emplace_back(
685 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 5));
686 // result_py.emplace_back(
687 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 6));
688
689 result_py.emplace_back("");
690 result_py.emplace_back(
691 " return casadi.Function('xdot', [X, U], [result])");
692
Austin Schuhb8b34be2024-07-14 16:06:19 -0700693 DefineVector2dFunction(
694 "contact_patch_velocity",
695 "# Returns the velocity of the wheel in global coordinates.",
696 [](const Module &m, int dimension) {
697 return ccode(*m.contact_patch_velocity.get(dimension, 0));
698 },
699 &result_py);
700 DefineVector2dFunction(
701 "wheel_ground_velocity",
702 "# Returns the velocity of the wheel in steer module coordinates.",
703 [](const Module &m, int dimension) {
704 return ccode(*m.wheel_ground_velocity.get(dimension, 0));
705 },
706 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700707
Austin Schuhb8b34be2024-07-14 16:06:19 -0700708 DefineVector2dFunction(
709 "wheel_slip_velocity",
710 "# Returns the difference in velocities of the wheel surface and the "
711 "ground.",
712 [](const Module &m, int dimension) {
713 return ccode(*m.wheel_slip_velocity.get(dimension, 0));
714 },
715 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700716
Austin Schuhb8b34be2024-07-14 16:06:19 -0700717 DefineScalarFunction(
718 "slip_angle", "Returns the slip angle of the ith wheel",
719 [](const Module &m) { return ccode(*m.slip_angle); }, &result_py);
720 DefineScalarFunction(
721 "slip_ratio", "Returns the slip ratio of the ith wheel",
722 [](const Module &m) { return ccode(*m.slip_ratio); }, &result_py);
723 DefineScalarFunction(
724 "module_angular_accel",
725 "Returns the angular acceleration of the robot due to the ith wheel",
Austin Schuh6ea789e2024-07-27 13:45:53 -0700726 [this](const Module &m) { return ccode(*div(m.full.torque, Js_)); },
727 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700728
Austin Schuhb8b34be2024-07-14 16:06:19 -0700729 DefineVector2dFunction(
730 "wheel_force",
731 "Returns the force on the wheel in steer module coordinates",
732 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700733 return ccode(
734 *std::vector<RCP<const Basic>>{m.full.Fwx, m.Fwy}[dimension]);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700735 },
736 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700737
Austin Schuhb8b34be2024-07-14 16:06:19 -0700738 DefineVector2dFunction(
739 "F", "Returns the force on the wheel in absolute coordinates",
740 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700741 return ccode(*m.full.F.get(dimension, 0));
Austin Schuhb8b34be2024-07-14 16:06:19 -0700742 },
743 &result_py);
744
745 DefineVector2dFunction(
746 "mounting_location",
747 "Returns the mounting location of wheel in robot coordinates",
748 [](const Module &m, int dimension) {
749 return ccode(*m.mounting_location.get(dimension, 0));
750 },
751 &result_py);
752
753 DefineScalarFunction(
754 "Ms", "Returns the self aligning moment of the ith wheel",
755 [this](const Module &m) {
756 return ccode(*(div(m.Ms, add(Jsm_, div(div(Js_, Gs_), Gs_)))));
757 },
758 &result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700759
760 aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n"));
761 }
762
Austin Schuhb8b34be2024-07-14 16:06:19 -0700763 void DefineScalarFunction(
764 std::string_view name, std::string_view documentation,
765 std::function<std::string(const Module &)> scalar_fn,
766 std::vector<std::string> *result_py) {
767 result_py->emplace_back("");
768 result_py->emplace_back(absl::Substitute("# $0.", documentation));
769 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
770 WriteCasadiVariables(result_py);
771 for (size_t m = 0; m < kNumModules; ++m) {
772 if (m == 0) {
773 result_py->emplace_back(" if i == 0:");
774 } else {
775 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
776 }
777 result_py->emplace_back(
778 absl::Substitute(" return casadi.Function('$0', [X, U], [$1])",
779 name, scalar_fn(modules_[m])));
780 }
781 result_py->emplace_back(" raise ValueError(\"Invalid module number\")");
782 }
783
784 void DefineVector2dFunction(
785 std::string_view name, std::string_view documentation,
786 std::function<std::string(const Module &, int)> scalar_fn,
787 std::vector<std::string> *result_py) {
788 result_py->emplace_back("");
789 result_py->emplace_back(absl::Substitute("# $0.", documentation));
790 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
791 WriteCasadiVariables(result_py);
792 result_py->emplace_back(
793 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
794 for (size_t m = 0; m < kNumModules; ++m) {
795 if (m == 0) {
796 result_py->emplace_back(" if i == 0:");
797 } else {
798 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
799 }
800 for (int j = 0; j < 2; ++j) {
801 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
802 j, scalar_fn(modules_[m], j)));
803 }
804 }
805 result_py->emplace_back(" else:");
806 result_py->emplace_back(
807 " raise ValueError(\"Invalid module number\")");
808 result_py->emplace_back(absl::Substitute(
809 " return casadi.Function('$0', [X, U], [result])", name));
810 }
811
justinT21446e4f62024-06-16 22:36:10 -0700812 private:
813 static constexpr uint8_t kNumModules = 4;
814
Austin Schuh6ea789e2024-07-27 13:45:53 -0700815 RCP<const Basic> SteerAccel(RCP<const Basic> Fwx, RCP<const Basic> Ms,
816 RCP<const Basic> Is) {
817 RCP<const Basic> lhms =
818 mul(add(neg(wb_), mul(add(rs_, rp_), sub(integer(1), div(rb1_, rp_)))),
819 mul(div(rw_, rb2_), neg(Fwx)));
820 RCP<const Basic> lhs = add(add(Ms, div(mul(Kts_, Is), Gs_)), lhms);
821 RCP<const Basic> rhs = add(Jsm_, div(div(Js_, Gs_), Gs_));
822 return simplify(div(lhs, rhs));
823 }
824
justinT21446e4f62024-06-16 22:36:10 -0700825 Module ModulePhysics(const int m, DenseMatrix mounting_location) {
826 VLOG(1) << "Solving module " << m;
827
828 Module result;
Austin Schuhb8b34be2024-07-14 16:06:19 -0700829 result.mounting_location = mounting_location;
justinT21446e4f62024-06-16 22:36:10 -0700830
831 result.Is = symbol(absl::StrFormat("Is%u", m));
832 result.Id = symbol(absl::StrFormat("Id%u", m));
833
834 RCP<const Symbol> thetamd = symbol(absl::StrFormat("theta_md%u", m));
835 RCP<const Symbol> omegamd = symbol(absl::StrFormat("omega_md%u", m));
836 RCP<const Symbol> alphamd = symbol(absl::StrFormat("alpha_md%u", m));
837
838 result.thetas = symbol(absl::StrFormat("thetas%u", m));
839 result.omegas = symbol(absl::StrFormat("omegas%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -0700840 RCP<const Symbol> alphas = symbol(absl::StrFormat("alphas%u", m));
justinT21446e4f62024-06-16 22:36:10 -0700841
justinT21446e4f62024-06-16 22:36:10 -0700842 result.omegad = symbol(absl::StrFormat("omegad%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -0700843 RCP<const Symbol> alphad = symbol(absl::StrFormat("alphad%u", m));
justinT21446e4f62024-06-16 22:36:10 -0700844
845 // Velocity of the module in field coordinates
Austin Schuh2a1abec2024-07-10 20:31:16 -0700846 DenseMatrix robot_velocity = DenseMatrix(2, 1, {vx_, vy_});
justinT21446e4f62024-06-16 22:36:10 -0700847 VLOG(1) << "robot velocity: " << robot_velocity.__str__();
848
849 // Velocity of the contact patch in field coordinates
850 DenseMatrix temp_matrix = DenseMatrix(2, 1);
851 DenseMatrix temp_matrix2 = DenseMatrix(2, 1);
Austin Schuhbd75c482024-08-18 00:03:51 -0700852 DenseMatrix temp_matrix3 = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700853 result.contact_patch_velocity = DenseMatrix(2, 1);
justinT21446e4f62024-06-16 22:36:10 -0700854
Austin Schuhb8b34be2024-07-14 16:06:19 -0700855 mul_dense_dense(R(theta_), result.mounting_location, temp_matrix);
justinT21446e4f62024-06-16 22:36:10 -0700856 add_dense_dense(angle_cross(temp_matrix, omega_), robot_velocity,
857 temp_matrix2);
858 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuhbd75c482024-08-18 00:03:51 -0700859 DenseMatrix(2, 1, {neg(caster_), integer(0)}),
860 temp_matrix3);
justinT21446e4f62024-06-16 22:36:10 -0700861 add_dense_dense(temp_matrix2,
Austin Schuhbd75c482024-08-18 00:03:51 -0700862 angle_cross(temp_matrix3, add(omega_, result.omegas)),
Austin Schuh2a1abec2024-07-10 20:31:16 -0700863 result.contact_patch_velocity);
justinT21446e4f62024-06-16 22:36:10 -0700864
865 VLOG(1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700866 VLOG(1) << "contact patch velocity: "
867 << result.contact_patch_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -0700868
869 // Relative velocity of the surface of the wheel to the ground.
Austin Schuhb67a38f2024-07-04 13:48:38 -0700870 result.wheel_ground_velocity = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700871 mul_dense_dense(R(neg(add(result.thetas, theta_))),
872 result.contact_patch_velocity,
Austin Schuhb67a38f2024-07-04 13:48:38 -0700873 result.wheel_ground_velocity);
justinT21446e4f62024-06-16 22:36:10 -0700874
Austin Schuhb8b34be2024-07-14 16:06:19 -0700875 // Compute the relative velocity between the wheel surface and the ground in
876 // the wheel coordinate system.
877 result.wheel_slip_velocity = DenseMatrix(2, 1);
878 DenseMatrix wheel_velocity =
879 DenseMatrix(2, 1, {mul(rw_, result.omegad), integer(0)});
880 DenseMatrix negative_wheel_ground_velocity =
881 DenseMatrix(2, 1,
882 {neg(result.wheel_ground_velocity.get(0, 0)),
883 neg(result.wheel_ground_velocity.get(1, 0))});
884 add_dense_dense(negative_wheel_ground_velocity, wheel_velocity,
885 result.wheel_slip_velocity);
886
justinT21446e4f62024-06-16 22:36:10 -0700887 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700888 VLOG(1) << "wheel ground velocity: "
889 << result.wheel_ground_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -0700890
Austin Schuh5ddcb472024-07-21 17:55:34 -0700891 result.slip_angle = sin(neg(atan2(result.wheel_ground_velocity.get(1, 0),
892 result.wheel_ground_velocity.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -0700893
894 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700895 VLOG(1) << "slip angle: " << result.slip_angle->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700896
Austin Schuh2a1abec2024-07-10 20:31:16 -0700897 // TODO(austin): Does this handle decel properly?
Austin Schuhb67a38f2024-07-04 13:48:38 -0700898 result.slip_ratio = div(
Austin Schuh2a1abec2024-07-10 20:31:16 -0700899 sub(mul(rw_, result.omegad), result.wheel_ground_velocity.get(0, 0)),
900 SymEngine::max(
901 {real_double(0.02), abs(result.wheel_ground_velocity.get(0, 0))}));
justinT21446e4f62024-06-16 22:36:10 -0700902 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700903 VLOG(1) << "Slip ratio " << result.slip_ratio->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700904
Austin Schuh6ea789e2024-07-27 13:45:53 -0700905 result.full.Fwx = simplify(mul(Cx_, result.slip_ratio));
Austin Schuhb67a38f2024-07-04 13:48:38 -0700906 result.Fwy = simplify(mul(Cy_, result.slip_angle));
justinT21446e4f62024-06-16 22:36:10 -0700907
Austin Schuh27694fa2024-07-20 16:29:49 -0700908 // The self-aligning moment needs to flip when the module flips direction.
Austin Schuh78a1b312024-08-18 17:21:34 -0700909 RCP<const Basic> softsign_velocity = add(
910 div(integer(-2),
911 add(integer(1), exp(mul(integer(100),
912 result.wheel_ground_velocity.get(0, 0))))),
913 integer(1));
914 result.Ms =
915 mul(neg(result.Fwy),
916 add(div(mul(softsign_velocity, contact_patch_length_), integer(3)),
917 caster_));
justinT21446e4f62024-06-16 22:36:10 -0700918 VLOG(1);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700919 VLOG(1) << "Ms " << result.Ms->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700920 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700921 VLOG(1) << "full.Fwx " << result.full.Fwx->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700922 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700923 VLOG(1) << "Fwy " << result.Fwy->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700924
Austin Schuh6ea789e2024-07-27 13:45:53 -0700925 // -K_td * Id / Gd + Fwx * rw = 0
926 // Fwx = K_td * Id / Gd / rw
927 result.direct.Fwx = mul(Ktd_, div(result.Id, mul(Gd_, rw_)));
928
929 result.direct.alphas_eqn =
930 SteerAccel(result.direct.Fwx, result.Ms, result.Is);
931
932 // d/dt omegas = ...
933 result.full.alphas_eqn = SteerAccel(result.full.Fwx, result.Ms, result.Is);
justinT21446e4f62024-06-16 22:36:10 -0700934
935 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700936 VLOG(1) << alphas->__str__() << " = " << result.full.alphas_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700937
Austin Schuh6ea789e2024-07-27 13:45:53 -0700938 RCP<const Basic> lhs =
939 sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), alphas),
940 mul(Gd1_, mul(Gd2_, alphamd)));
941 RCP<const Basic> ddplanitary_eqn = sub(mul(Gd3_, lhs), alphad);
justinT21446e4f62024-06-16 22:36:10 -0700942
Austin Schuh6ea789e2024-07-27 13:45:53 -0700943 RCP<const Basic> full_drive_eqn =
944 sub(add(mul(neg(Jdm_), div(alphamd, Gd_)),
945 mul(Ktd_, div(neg(result.Id), Gd_))),
946 mul(neg(result.full.Fwx), rw_));
justinT21446e4f62024-06-16 22:36:10 -0700947
Austin Schuh6ea789e2024-07-27 13:45:53 -0700948 VLOG(1) << "full_drive_eqn: " << full_drive_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700949
950 // Substitute in ddplanitary_eqn so we get rid of alphamd
951 map_basic_basic map;
952 RCP<const Set> reals = interval(NegInf, Inf, true, true);
953 RCP<const Set> solve_solution = solve(ddplanitary_eqn, alphamd, reals);
954 map[alphamd] = solve_solution->get_args()[1]->get_args()[0];
955 VLOG(1) << "temp: " << solve_solution->__str__();
Austin Schuh6ea789e2024-07-27 13:45:53 -0700956 RCP<const Basic> drive_eqn_subs = full_drive_eqn->subs(map);
justinT21446e4f62024-06-16 22:36:10 -0700957
958 map.clear();
Austin Schuh6ea789e2024-07-27 13:45:53 -0700959 map[alphas] = result.full.alphas_eqn;
justinT21446e4f62024-06-16 22:36:10 -0700960 RCP<const Basic> drive_eqn_subs2 = drive_eqn_subs->subs(map);
961 RCP<const Basic> drive_eqn_subs3 = simplify(drive_eqn_subs2);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700962 VLOG(1) << "full_drive_eqn simplified: " << drive_eqn_subs3->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700963
Austin Schuh6ea789e2024-07-27 13:45:53 -0700964 solve_solution = solve(drive_eqn_subs3, alphad, reals);
justinT21446e4f62024-06-16 22:36:10 -0700965
Austin Schuh6ea789e2024-07-27 13:45:53 -0700966 result.full.alphad_eqn =
justinT21446e4f62024-06-16 22:36:10 -0700967 simplify(solve_solution->get_args()[1]->get_args()[0]);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700968 VLOG(1) << "drive_accel: " << result.full.alphad_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700969
Austin Schuh2a1abec2024-07-10 20:31:16 -0700970 // Compute the resulting force from the module.
Austin Schuh6ea789e2024-07-27 13:45:53 -0700971 result.full.F = DenseMatrix(2, 1);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700972 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuh6ea789e2024-07-27 13:45:53 -0700973 DenseMatrix(2, 1, {result.full.Fwx, result.Fwy}),
974 result.full.F);
975 result.full.torque = force_cross(result.mounting_location, result.full.F);
justinT21446e4f62024-06-16 22:36:10 -0700976
Austin Schuh6ea789e2024-07-27 13:45:53 -0700977 result.direct.F = DenseMatrix(2, 1);
978 mul_dense_dense(R(add(theta_, result.thetas)),
979 DenseMatrix(2, 1, {result.direct.Fwx, result.Fwy}),
980 result.direct.F);
981 result.direct.torque =
982 force_cross(result.mounting_location, result.direct.F);
justinT21446e4f62024-06-16 22:36:10 -0700983
984 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700985 VLOG(1) << "full torque = " << result.full.torque->__str__();
986 VLOG(1) << "direct torque = " << result.full.torque->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700987
justinT21446e4f62024-06-16 22:36:10 -0700988 return result;
989 }
990
991 DenseMatrix R(const RCP<const Basic> theta) {
992 return DenseMatrix(2, 2,
993 {cos(theta), neg(sin(theta)), sin(theta), cos(theta)});
994 }
995
996 DenseMatrix angle_cross(DenseMatrix a, RCP<const Basic> b) {
Austin Schuh2a1abec2024-07-10 20:31:16 -0700997 return DenseMatrix(2, 1, {mul(neg(a.get(1, 0)), b), mul(a.get(0, 0), b)});
justinT21446e4f62024-06-16 22:36:10 -0700998 }
999
1000 RCP<const Basic> force_cross(DenseMatrix r, DenseMatrix f) {
1001 return sub(mul(r.get(0, 0), f.get(1, 0)), mul(r.get(1, 0), f.get(0, 0)));
1002 }
1003
1004 // z represents the number of teeth per gear, theta is the angle between
1005 // shafts(in degrees), D_02 is the pitch diameter of gear 2 and b_2 is the
1006 // length of the tooth of gear 2
1007 // returns std::pair(r_01, r_02)
1008 std::pair<double, double> GetBevelPitchRadius(double z1, double z2,
1009 double theta, double D_02,
1010 double b_2) {
1011 double gamma_1 = std::atan2(z1, z2);
1012 double gamma_2 = theta / 180.0 * std::numbers::pi - gamma_1;
1013 double R_m = D_02 / 2 / std::sin(gamma_2) - b_2 / 2;
1014 return std::pair(R_m * std::cos(gamma_2), R_m * std::sin(gamma_2));
1015 }
1016
1017 Motor drive_motor_;
1018 Motor steer_motor_;
1019
1020 RCP<const Basic> Cx_;
1021 RCP<const Basic> Cy_;
Austin Schuh2a1abec2024-07-10 20:31:16 -07001022 RCP<const Basic> rw_;
justinT21446e4f62024-06-16 22:36:10 -07001023 RCP<const Basic> m_;
1024 RCP<const Basic> J_;
1025 RCP<const Basic> Gd1_;
1026 RCP<const Basic> rs_;
1027 RCP<const Basic> rp_;
1028 RCP<const Basic> Gd2_;
1029 RCP<const Basic> rb1_;
1030 RCP<const Basic> rb2_;
1031 RCP<const Basic> Gd3_;
1032 RCP<const Basic> Gd_;
1033 RCP<const Basic> Js_;
1034 RCP<const Basic> Gs_;
1035 RCP<const Basic> wb_;
1036 RCP<const Basic> Jdm_;
1037 RCP<const Basic> Jsm_;
1038 RCP<const Basic> Kts_;
1039 RCP<const Basic> Ktd_;
1040 RCP<const Basic> robot_width_;
1041 RCP<const Basic> caster_;
1042 RCP<const Basic> contact_patch_length_;
1043 RCP<const Basic> x_;
1044 RCP<const Basic> y_;
1045 RCP<const Basic> theta_;
1046 RCP<const Basic> vx_;
1047 RCP<const Basic> vy_;
1048 RCP<const Basic> omega_;
1049 RCP<const Basic> ax_;
1050 RCP<const Basic> ay_;
1051 RCP<const Basic> atheta_;
1052
1053 std::array<Module, kNumModules> modules_;
1054
Austin Schuh6ea789e2024-07-27 13:45:53 -07001055 DenseMatrix full_accel_;
1056 RCP<const Basic> full_angular_accel_;
1057 DenseMatrix direct_accel_;
1058 RCP<const Basic> direct_angular_accel_;
justinT21446e4f62024-06-16 22:36:10 -07001059};
1060
1061} // namespace frc971::control_loops::swerve
1062
1063int main(int argc, char **argv) {
1064 aos::InitGoogle(&argc, &argv);
1065
1066 frc971::control_loops::swerve::SwerveSimulation sim;
1067
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001068 if (!absl::GetFlag(FLAGS_cc_output_path).empty() &&
1069 !absl::GetFlag(FLAGS_h_output_path).empty()) {
1070 sim.Write(absl::GetFlag(FLAGS_cc_output_path),
1071 absl::GetFlag(FLAGS_h_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001072 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001073 if (!absl::GetFlag(FLAGS_casadi_py_output_path).empty()) {
1074 sim.WriteCasadi(absl::GetFlag(FLAGS_casadi_py_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001075 }
justinT21446e4f62024-06-16 22:36:10 -07001076
1077 return 0;
1078}