blob: 0e8b0135519453404d6a9f734aebe03dbeb39f6c [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;
47using SymEngine::Inf;
48using SymEngine::integer;
49using SymEngine::map_basic_basic;
50using SymEngine::minus_one;
51using SymEngine::neg;
52using SymEngine::NegInf;
53using SymEngine::pow;
54using SymEngine::RCP;
55using SymEngine::real_double;
56using SymEngine::RealDouble;
57using SymEngine::Set;
Austin Schuh27694fa2024-07-20 16:29:49 -070058using SymEngine::sign;
justinT21446e4f62024-06-16 22:36:10 -070059using 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 {");
Austin Schuh27694fa2024-07-20 16:29:49 -0700309 result_cc.emplace_back("namespace {");
310 result_cc.emplace_back("");
311 result_cc.emplace_back("double sign(double x) {");
312 result_cc.emplace_back(" return (x > 0) - (x < 0);");
313 result_cc.emplace_back("}");
314
315 result_cc.emplace_back("");
316 result_cc.emplace_back("} // namespace");
justinT21446e4f62024-06-16 22:36:10 -0700317 result_cc.emplace_back("");
318 result_cc.emplace_back("Eigen::Matrix<double, 25, 1> SwervePhysics(");
319 result_cc.emplace_back(
320 " Eigen::Map<const Eigen::Matrix<double, 25, 1>> X,");
321 result_cc.emplace_back(
322 " Eigen::Map<const Eigen::Matrix<double, 8, 1>> U) {");
323 result_cc.emplace_back(" Eigen::Matrix<double, 25, 1> result;");
324
325 // Start by writing out variables matching each of the symbol names we use
326 // so we don't have to modify the computed equations too much.
327 for (size_t m = 0; m < kNumModules; ++m) {
328 result_cc.emplace_back(
329 absl::Substitute(" const double thetas$0 = X($1, 0);", m, m * 4));
330 result_cc.emplace_back(absl::Substitute(
331 " const double omegas$0 = X($1, 0);", m, m * 4 + 2));
332 result_cc.emplace_back(absl::Substitute(
333 " const double omegad$0 = X($1, 0);", m, m * 4 + 3));
334 }
335
336 result_cc.emplace_back(absl::Substitute(" const double theta = X($0, 0);",
337 kNumModules * 4 + 2));
338 result_cc.emplace_back(
339 absl::Substitute(" const double vx = X($0, 0);", kNumModules * 4 + 3));
340 result_cc.emplace_back(
341 absl::Substitute(" const double vy = X($0, 0);", kNumModules * 4 + 4));
342 result_cc.emplace_back(absl::Substitute(" const double omega = X($0, 0);",
343 kNumModules * 4 + 5));
344
345 result_cc.emplace_back(
346 absl::Substitute(" const double fx = X($0, 0);", kNumModules * 4 + 6));
347 result_cc.emplace_back(
348 absl::Substitute(" const double fy = X($0, 0);", kNumModules * 4 + 7));
349 result_cc.emplace_back(absl::Substitute(" const double moment = X($0, 0);",
350 kNumModules * 4 + 8));
351
352 // Now do the same for the inputs.
353 for (size_t m = 0; m < kNumModules; ++m) {
354 result_cc.emplace_back(
355 absl::Substitute(" const double Is$0 = U($1, 0);", m, m * 2));
356 result_cc.emplace_back(
357 absl::Substitute(" const double Id$0 = U($1, 0);", m, m * 2 + 1));
358 }
359
360 result_cc.emplace_back("");
361
362 // And then write out the derivative of each state.
363 for (size_t m = 0; m < kNumModules; ++m) {
364 result_cc.emplace_back(
365 absl::Substitute(" result($0, 0) = omegas$1;", m * 4, m));
366 result_cc.emplace_back(
367 absl::Substitute(" result($0, 0) = omegad$1;", m * 4 + 1, m));
368
Austin Schuh6ea789e2024-07-27 13:45:53 -0700369 result_cc.emplace_back(
370 absl::Substitute(" result($0, 0) = $1;", m * 4 + 2,
371 ccode(*modules_[m].full.alphas_eqn)));
372 result_cc.emplace_back(
373 absl::Substitute(" result($0, 0) = $1;", m * 4 + 3,
374 ccode(*modules_[m].full.alphad_eqn)));
justinT21446e4f62024-06-16 22:36:10 -0700375 }
376
377 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700378 absl::Substitute(" result($0, 0) = vx;", kNumModules * 4 + 0));
justinT21446e4f62024-06-16 22:36:10 -0700379 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700380 absl::Substitute(" result($0, 0) = vy;", kNumModules * 4 + 1));
justinT21446e4f62024-06-16 22:36:10 -0700381 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700382 absl::Substitute(" result($0, 0) = omega;", kNumModules * 4 + 2));
justinT21446e4f62024-06-16 22:36:10 -0700383
justinT21446e4f62024-06-16 22:36:10 -0700384 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700385 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700386 ccode(*full_accel_.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -0700387 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700388 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700389 ccode(*full_accel_.get(1, 0))));
390 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
391 kNumModules * 4 + 5,
392 ccode(*full_angular_accel_)));
justinT21446e4f62024-06-16 22:36:10 -0700393
394 result_cc.emplace_back(
395 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 6));
396 result_cc.emplace_back(
397 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 7));
398 result_cc.emplace_back(
399 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 8));
400
401 result_cc.emplace_back("");
402 result_cc.emplace_back(" return result;");
403 result_cc.emplace_back("}");
404 result_cc.emplace_back("");
405 result_cc.emplace_back("} // namespace frc971::control_loops::swerve");
406
407 aos::util::WriteStringToFileOrDie(cc_path, absl::StrJoin(result_cc, "\n"));
408 aos::util::WriteStringToFileOrDie(h_path, absl::StrJoin(result_h, "\n"));
409 }
410
Austin Schuhb67a38f2024-07-04 13:48:38 -0700411 void WriteCasadiVariables(std::vector<std::string> *result_py) {
412 result_py->emplace_back(" sin = casadi.sin");
Austin Schuh27694fa2024-07-20 16:29:49 -0700413 result_py->emplace_back(" sign = casadi.sign");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700414 result_py->emplace_back(" cos = casadi.cos");
Austin Schuhbd75c482024-08-18 00:03:51 -0700415 result_py->emplace_back(" atan2 = soft_atan2");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700416 result_py->emplace_back(" fmax = casadi.fmax");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700417 result_py->emplace_back(" fabs = casadi.fabs");
418
419 // Start by writing out variables matching each of the symbol names we use
420 // so we don't have to modify the computed equations too much.
421 for (size_t m = 0; m < kNumModules; ++m) {
422 result_py->emplace_back(
423 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 4));
424 result_py->emplace_back(
425 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 4 + 2));
426 result_py->emplace_back(
427 absl::Substitute(" omegad$0 = X[$1, 0]", m, m * 4 + 3));
428 }
429
430 result_py->emplace_back(
431 absl::Substitute(" theta = X[$0, 0]", kNumModules * 4 + 2));
432 result_py->emplace_back(
433 absl::Substitute(" vx = X[$0, 0]", kNumModules * 4 + 3));
434 result_py->emplace_back(
435 absl::Substitute(" vy = X[$0, 0]", kNumModules * 4 + 4));
436 result_py->emplace_back(
437 absl::Substitute(" omega = X[$0, 0]", kNumModules * 4 + 5));
438
439 result_py->emplace_back(
440 absl::Substitute(" fx = X[$0, 0]", kNumModules * 4 + 6));
441 result_py->emplace_back(
442 absl::Substitute(" fy = X[$0, 0]", kNumModules * 4 + 7));
443 result_py->emplace_back(
444 absl::Substitute(" moment = X[$0, 0]", kNumModules * 4 + 8));
445
446 // Now do the same for the inputs.
447 for (size_t m = 0; m < kNumModules; ++m) {
448 result_py->emplace_back(
449 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
450 result_py->emplace_back(
451 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
452 }
453 }
454
Austin Schuh6ea789e2024-07-27 13:45:53 -0700455 void WriteCasadiVelocityVariables(std::vector<std::string> *result_py) {
456 result_py->emplace_back(" sin = casadi.sin");
457 result_py->emplace_back(" sign = casadi.sign");
458 result_py->emplace_back(" cos = casadi.cos");
Austin Schuhbd75c482024-08-18 00:03:51 -0700459 result_py->emplace_back(" atan2 = soft_atan2");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700460 result_py->emplace_back(" fmax = casadi.fmax");
461 result_py->emplace_back(" fabs = casadi.fabs");
462
463 // Start by writing out variables matching each of the symbol names we use
464 // so we don't have to modify the computed equations too much.
465 for (size_t m = 0; m < kNumModules; ++m) {
466 result_py->emplace_back(
467 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 2 + 0));
468 result_py->emplace_back(
469 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 2 + 1));
470 }
471
472 result_py->emplace_back(
473 absl::Substitute(" theta = X[$0, 0]", kNumModules * 2 + 0));
474 result_py->emplace_back(
475 absl::Substitute(" vx = X[$0, 0]", kNumModules * 2 + 1));
476 result_py->emplace_back(
477 absl::Substitute(" vy = X[$0, 0]", kNumModules * 2 + 2));
478 result_py->emplace_back(
479 absl::Substitute(" omega = X[$0, 0]", kNumModules * 2 + 3));
480
481 // result_py->emplace_back(
482 // absl::Substitute(" fx = X[$0, 0]", kNumModules * 3 + 4));
483 // result_py->emplace_back(
484 // absl::Substitute(" fy = X[$0, 0]", kNumModules * 3 + 5));
485 // result_py->emplace_back(
486 // absl::Substitute(" moment = X[$0, 0]", kNumModules * 3 + 6));
487 //
488 result_py->emplace_back(" fx = 0");
489 result_py->emplace_back(" fy = 0");
490 result_py->emplace_back(" moment = 0");
491
492 // Now do the same for the inputs.
493 for (size_t m = 0; m < kNumModules; ++m) {
494 result_py->emplace_back(
495 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
496 result_py->emplace_back(
497 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
498 }
499 }
500
Austin Schuh0f881092024-06-28 15:36:48 -0700501 // Writes the physics out to the provided .cc and .h path.
502 void WriteCasadi(std::string_view py_path) {
503 std::vector<std::string> result_py;
504
505 // Write out the header.
506 result_py.emplace_back("#!/usr/bin/python3");
507 result_py.emplace_back("");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700508 result_py.emplace_back("import casadi, numpy");
Austin Schuh0f881092024-06-28 15:36:48 -0700509 result_py.emplace_back("");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700510 result_py.emplace_back(absl::Substitute("WHEEL_RADIUS = $0", ccode(*rw_)));
511 result_py.emplace_back(
512 absl::Substitute("ROBOT_WIDTH = $0", ccode(*robot_width_)));
513 result_py.emplace_back(absl::Substitute("CASTER = $0", ccode(*caster_)));
Austin Schuh6ea789e2024-07-27 13:45:53 -0700514 result_py.emplace_back("STATE_THETAS0 = 0");
515 result_py.emplace_back("STATE_THETAD0 = 1");
516 result_py.emplace_back("STATE_OMEGAS0 = 2");
517 result_py.emplace_back("STATE_OMEGAD0 = 3");
518 result_py.emplace_back("STATE_THETAS1 = 4");
519 result_py.emplace_back("STATE_THETAD1 = 5");
520 result_py.emplace_back("STATE_OMEGAS1 = 6");
521 result_py.emplace_back("STATE_OMEGAD1 = 7");
522 result_py.emplace_back("STATE_THETAS2 = 8");
523 result_py.emplace_back("STATE_THETAD2 = 9");
524 result_py.emplace_back("STATE_OMEGAS2 = 10");
525 result_py.emplace_back("STATE_OMEGAD2 = 11");
526 result_py.emplace_back("STATE_THETAS3 = 12");
527 result_py.emplace_back("STATE_THETAD3 = 13");
528 result_py.emplace_back("STATE_OMEGAS3 = 14");
529 result_py.emplace_back("STATE_OMEGAD3 = 15");
530 result_py.emplace_back("STATE_X = 16");
531 result_py.emplace_back("STATE_Y = 17");
532 result_py.emplace_back("STATE_THETA = 18");
533 result_py.emplace_back("STATE_VX = 19");
534 result_py.emplace_back("STATE_VY = 20");
535 result_py.emplace_back("STATE_OMEGA = 21");
536 result_py.emplace_back("STATE_FX = 22");
537 result_py.emplace_back("STATE_FY = 23");
538 result_py.emplace_back("STATE_MOMENT = 24");
539 result_py.emplace_back("NUM_STATES = 25");
540 result_py.emplace_back("");
541 result_py.emplace_back("VELOCITY_STATE_THETAS0 = 0");
542 result_py.emplace_back("VELOCITY_STATE_OMEGAS0 = 1");
543 result_py.emplace_back("VELOCITY_STATE_THETAS1 = 2");
544 result_py.emplace_back("VELOCITY_STATE_OMEGAS1 = 3");
545 result_py.emplace_back("VELOCITY_STATE_THETAS2 = 4");
546 result_py.emplace_back("VELOCITY_STATE_OMEGAS2 = 5");
547 result_py.emplace_back("VELOCITY_STATE_THETAS3 = 6");
548 result_py.emplace_back("VELOCITY_STATE_OMEGAS3 = 7");
549 result_py.emplace_back("VELOCITY_STATE_THETA = 8");
550 result_py.emplace_back("VELOCITY_STATE_VX = 9");
551 result_py.emplace_back("VELOCITY_STATE_VY = 10");
552 result_py.emplace_back("VELOCITY_STATE_OMEGA = 11");
553 // result_py.emplace_back("VELOCITY_STATE_FX = 16");
554 // result_py.emplace_back("VELOCITY_STATE_FY = 17");
555 // result_py.emplace_back("VELOCITY_STATE_MOMENT = 18");
556 result_py.emplace_back("NUM_VELOCITY_STATES = 12");
557 result_py.emplace_back("");
558 result_py.emplace_back("def to_velocity_state(X):");
559 result_py.emplace_back(" return numpy.array([");
560 result_py.emplace_back(" [X[STATE_THETAS0, 0]],");
561 result_py.emplace_back(" [X[STATE_OMEGAS0, 0]],");
562 result_py.emplace_back(" [X[STATE_THETAS1, 0]],");
563 result_py.emplace_back(" [X[STATE_OMEGAS1, 0]],");
564 result_py.emplace_back(" [X[STATE_THETAS2, 0]],");
565 result_py.emplace_back(" [X[STATE_OMEGAS2, 0]],");
566 result_py.emplace_back(" [X[STATE_THETAS3, 0]],");
567 result_py.emplace_back(" [X[STATE_OMEGAS3, 0]],");
568 result_py.emplace_back(" [X[STATE_THETA, 0]],");
569 result_py.emplace_back(" [X[STATE_VX, 0]],");
570 result_py.emplace_back(" [X[STATE_VY, 0]],");
571 result_py.emplace_back(" [X[STATE_OMEGA, 0]],");
572 // result_py.emplace_back(" [X[STATE_FX, 0]],");
573 // result_py.emplace_back(" [X[STATE_FY, 0]],");
574 // result_py.emplace_back(" [X[STATE_MOMENT, 0]],");
575 result_py.emplace_back(" ])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700576 result_py.emplace_back("");
Austin Schuhbd75c482024-08-18 00:03:51 -0700577 constexpr double kLogGain = 1.0 / 0.05;
578 result_py.emplace_back("def soft_atan2(y, x):");
579 result_py.emplace_back(" return casadi.arctan2(");
580 result_py.emplace_back(" y,");
581 result_py.emplace_back(" casadi.logsumexp(casadi.SX(numpy.array(");
582 result_py.emplace_back(absl::Substitute(
583 " [1.0, casadi.fabs(x) * $0.0]))) / $0.0)", kLogGain));
584 result_py.emplace_back("");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700585
Austin Schuh0f881092024-06-28 15:36:48 -0700586 result_py.emplace_back("# Returns the derivative of our state vector");
587 result_py.emplace_back("# [thetas0, thetad0, omegas0, omegad0,");
588 result_py.emplace_back("# thetas1, thetad1, omegas1, omegad1,");
589 result_py.emplace_back("# thetas2, thetad2, omegas2, omegad2,");
590 result_py.emplace_back("# thetas3, thetad3, omegas3, omegad3,");
591 result_py.emplace_back("# x, y, theta, vx, vy, omega,");
592 result_py.emplace_back("# Fx, Fy, Moment]");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700593 result_py.emplace_back("def swerve_full_dynamics(X, U):");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700594 WriteCasadiVariables(&result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700595
596 result_py.emplace_back("");
597 result_py.emplace_back(" result = casadi.SX.sym('result', 25, 1)");
598 result_py.emplace_back("");
599
600 // And then write out the derivative of each state.
601 for (size_t m = 0; m < kNumModules; ++m) {
602 result_py.emplace_back(
603 absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m));
604 result_py.emplace_back(
605 absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m));
606
Austin Schuh6ea789e2024-07-27 13:45:53 -0700607 result_py.emplace_back(
608 absl::Substitute(" result[$0, 0] = $1", m * 4 + 2,
609 ccode(*modules_[m].full.alphas_eqn)));
610 result_py.emplace_back(
611 absl::Substitute(" result[$0, 0] = $1", m * 4 + 3,
612 ccode(*modules_[m].full.alphad_eqn)));
Austin Schuh0f881092024-06-28 15:36:48 -0700613 }
614
615 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700616 absl::Substitute(" result[$0, 0] = vx", kNumModules * 4 + 0));
Austin Schuh0f881092024-06-28 15:36:48 -0700617 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700618 absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1));
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] = omega", kNumModules * 4 + 2));
Austin Schuh0f881092024-06-28 15:36:48 -0700621
Austin Schuh0f881092024-06-28 15:36:48 -0700622 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700623 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700624 ccode(*full_accel_.get(0, 0))));
Austin Schuh0f881092024-06-28 15:36:48 -0700625 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700626 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700627 ccode(*full_accel_.get(1, 0))));
628 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
629 kNumModules * 4 + 5,
630 ccode(*full_angular_accel_)));
Austin Schuh0f881092024-06-28 15:36:48 -0700631
632 result_py.emplace_back(
633 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6));
634 result_py.emplace_back(
635 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7));
636 result_py.emplace_back(
637 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8));
638
639 result_py.emplace_back("");
640 result_py.emplace_back(
641 " return casadi.Function('xdot', [X, U], [result])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700642
Austin Schuh6ea789e2024-07-27 13:45:53 -0700643 result_py.emplace_back("");
644
645 result_py.emplace_back("# Returns the derivative of our state vector");
646 result_py.emplace_back("# [thetas0, omegas0,");
647 result_py.emplace_back("# thetas1, omegas1,");
648 result_py.emplace_back("# thetas2, omegas2,");
649 result_py.emplace_back("# thetas3, omegas3,");
650 result_py.emplace_back("# theta, vx, vy, omega]");
651 result_py.emplace_back("def velocity_swerve_physics(X, U):");
652 WriteCasadiVelocityVariables(&result_py);
653
654 result_py.emplace_back("");
655 result_py.emplace_back(
656 " result = casadi.SX.sym('result', NUM_VELOCITY_STATES, 1)");
657 result_py.emplace_back("");
658
659 // And then write out the derivative of each state.
660 for (size_t m = 0; m < kNumModules; ++m) {
661 result_py.emplace_back(
662 absl::Substitute(" result[$0, 0] = omegas$1", m * 2 + 0, m));
663 result_py.emplace_back(
664 absl::Substitute(" result[$0, 0] = $1", m * 2 + 1,
665 ccode(*modules_[m].direct.alphas_eqn)));
666 }
667 result_py.emplace_back(
668 absl::Substitute(" result[$0, 0] = omega", kNumModules * 2 + 0));
669
670 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
671 kNumModules * 2 + 1,
672 ccode(*direct_accel_.get(0, 0))));
673 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
674 kNumModules * 2 + 2,
675 ccode(*direct_accel_.get(1, 0))));
676 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
677 kNumModules * 2 + 3,
678 ccode(*direct_angular_accel_)));
679
680 // result_py.emplace_back(
681 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 4));
682 // result_py.emplace_back(
683 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 5));
684 // result_py.emplace_back(
685 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 6));
686
687 result_py.emplace_back("");
688 result_py.emplace_back(
689 " return casadi.Function('xdot', [X, U], [result])");
690
Austin Schuhb8b34be2024-07-14 16:06:19 -0700691 DefineVector2dFunction(
692 "contact_patch_velocity",
693 "# Returns the velocity of the wheel in global coordinates.",
694 [](const Module &m, int dimension) {
695 return ccode(*m.contact_patch_velocity.get(dimension, 0));
696 },
697 &result_py);
698 DefineVector2dFunction(
699 "wheel_ground_velocity",
700 "# Returns the velocity of the wheel in steer module coordinates.",
701 [](const Module &m, int dimension) {
702 return ccode(*m.wheel_ground_velocity.get(dimension, 0));
703 },
704 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700705
Austin Schuhb8b34be2024-07-14 16:06:19 -0700706 DefineVector2dFunction(
707 "wheel_slip_velocity",
708 "# Returns the difference in velocities of the wheel surface and the "
709 "ground.",
710 [](const Module &m, int dimension) {
711 return ccode(*m.wheel_slip_velocity.get(dimension, 0));
712 },
713 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700714
Austin Schuhb8b34be2024-07-14 16:06:19 -0700715 DefineScalarFunction(
716 "slip_angle", "Returns the slip angle of the ith wheel",
717 [](const Module &m) { return ccode(*m.slip_angle); }, &result_py);
718 DefineScalarFunction(
719 "slip_ratio", "Returns the slip ratio of the ith wheel",
720 [](const Module &m) { return ccode(*m.slip_ratio); }, &result_py);
721 DefineScalarFunction(
722 "module_angular_accel",
723 "Returns the angular acceleration of the robot due to the ith wheel",
Austin Schuh6ea789e2024-07-27 13:45:53 -0700724 [this](const Module &m) { return ccode(*div(m.full.torque, Js_)); },
725 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700726
Austin Schuhb8b34be2024-07-14 16:06:19 -0700727 DefineVector2dFunction(
728 "wheel_force",
729 "Returns the force on the wheel in steer module coordinates",
730 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700731 return ccode(
732 *std::vector<RCP<const Basic>>{m.full.Fwx, m.Fwy}[dimension]);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700733 },
734 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700735
Austin Schuhb8b34be2024-07-14 16:06:19 -0700736 DefineVector2dFunction(
737 "F", "Returns the force on the wheel in absolute coordinates",
738 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700739 return ccode(*m.full.F.get(dimension, 0));
Austin Schuhb8b34be2024-07-14 16:06:19 -0700740 },
741 &result_py);
742
743 DefineVector2dFunction(
744 "mounting_location",
745 "Returns the mounting location of wheel in robot coordinates",
746 [](const Module &m, int dimension) {
747 return ccode(*m.mounting_location.get(dimension, 0));
748 },
749 &result_py);
750
751 DefineScalarFunction(
752 "Ms", "Returns the self aligning moment of the ith wheel",
753 [this](const Module &m) {
754 return ccode(*(div(m.Ms, add(Jsm_, div(div(Js_, Gs_), Gs_)))));
755 },
756 &result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700757
758 aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n"));
759 }
760
Austin Schuhb8b34be2024-07-14 16:06:19 -0700761 void DefineScalarFunction(
762 std::string_view name, std::string_view documentation,
763 std::function<std::string(const Module &)> scalar_fn,
764 std::vector<std::string> *result_py) {
765 result_py->emplace_back("");
766 result_py->emplace_back(absl::Substitute("# $0.", documentation));
767 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
768 WriteCasadiVariables(result_py);
769 for (size_t m = 0; m < kNumModules; ++m) {
770 if (m == 0) {
771 result_py->emplace_back(" if i == 0:");
772 } else {
773 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
774 }
775 result_py->emplace_back(
776 absl::Substitute(" return casadi.Function('$0', [X, U], [$1])",
777 name, scalar_fn(modules_[m])));
778 }
779 result_py->emplace_back(" raise ValueError(\"Invalid module number\")");
780 }
781
782 void DefineVector2dFunction(
783 std::string_view name, std::string_view documentation,
784 std::function<std::string(const Module &, int)> scalar_fn,
785 std::vector<std::string> *result_py) {
786 result_py->emplace_back("");
787 result_py->emplace_back(absl::Substitute("# $0.", documentation));
788 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
789 WriteCasadiVariables(result_py);
790 result_py->emplace_back(
791 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
792 for (size_t m = 0; m < kNumModules; ++m) {
793 if (m == 0) {
794 result_py->emplace_back(" if i == 0:");
795 } else {
796 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
797 }
798 for (int j = 0; j < 2; ++j) {
799 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
800 j, scalar_fn(modules_[m], j)));
801 }
802 }
803 result_py->emplace_back(" else:");
804 result_py->emplace_back(
805 " raise ValueError(\"Invalid module number\")");
806 result_py->emplace_back(absl::Substitute(
807 " return casadi.Function('$0', [X, U], [result])", name));
808 }
809
justinT21446e4f62024-06-16 22:36:10 -0700810 private:
811 static constexpr uint8_t kNumModules = 4;
812
Austin Schuh6ea789e2024-07-27 13:45:53 -0700813 RCP<const Basic> SteerAccel(RCP<const Basic> Fwx, RCP<const Basic> Ms,
814 RCP<const Basic> Is) {
815 RCP<const Basic> lhms =
816 mul(add(neg(wb_), mul(add(rs_, rp_), sub(integer(1), div(rb1_, rp_)))),
817 mul(div(rw_, rb2_), neg(Fwx)));
818 RCP<const Basic> lhs = add(add(Ms, div(mul(Kts_, Is), Gs_)), lhms);
819 RCP<const Basic> rhs = add(Jsm_, div(div(Js_, Gs_), Gs_));
820 return simplify(div(lhs, rhs));
821 }
822
justinT21446e4f62024-06-16 22:36:10 -0700823 Module ModulePhysics(const int m, DenseMatrix mounting_location) {
824 VLOG(1) << "Solving module " << m;
825
826 Module result;
Austin Schuhb8b34be2024-07-14 16:06:19 -0700827 result.mounting_location = mounting_location;
justinT21446e4f62024-06-16 22:36:10 -0700828
829 result.Is = symbol(absl::StrFormat("Is%u", m));
830 result.Id = symbol(absl::StrFormat("Id%u", m));
831
832 RCP<const Symbol> thetamd = symbol(absl::StrFormat("theta_md%u", m));
833 RCP<const Symbol> omegamd = symbol(absl::StrFormat("omega_md%u", m));
834 RCP<const Symbol> alphamd = symbol(absl::StrFormat("alpha_md%u", m));
835
836 result.thetas = symbol(absl::StrFormat("thetas%u", m));
837 result.omegas = symbol(absl::StrFormat("omegas%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -0700838 RCP<const Symbol> alphas = symbol(absl::StrFormat("alphas%u", m));
justinT21446e4f62024-06-16 22:36:10 -0700839
justinT21446e4f62024-06-16 22:36:10 -0700840 result.omegad = symbol(absl::StrFormat("omegad%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -0700841 RCP<const Symbol> alphad = symbol(absl::StrFormat("alphad%u", m));
justinT21446e4f62024-06-16 22:36:10 -0700842
843 // Velocity of the module in field coordinates
Austin Schuh2a1abec2024-07-10 20:31:16 -0700844 DenseMatrix robot_velocity = DenseMatrix(2, 1, {vx_, vy_});
justinT21446e4f62024-06-16 22:36:10 -0700845 VLOG(1) << "robot velocity: " << robot_velocity.__str__();
846
847 // Velocity of the contact patch in field coordinates
848 DenseMatrix temp_matrix = DenseMatrix(2, 1);
849 DenseMatrix temp_matrix2 = DenseMatrix(2, 1);
Austin Schuhbd75c482024-08-18 00:03:51 -0700850 DenseMatrix temp_matrix3 = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700851 result.contact_patch_velocity = DenseMatrix(2, 1);
justinT21446e4f62024-06-16 22:36:10 -0700852
Austin Schuhb8b34be2024-07-14 16:06:19 -0700853 mul_dense_dense(R(theta_), result.mounting_location, temp_matrix);
justinT21446e4f62024-06-16 22:36:10 -0700854 add_dense_dense(angle_cross(temp_matrix, omega_), robot_velocity,
855 temp_matrix2);
856 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuhbd75c482024-08-18 00:03:51 -0700857 DenseMatrix(2, 1, {neg(caster_), integer(0)}),
858 temp_matrix3);
justinT21446e4f62024-06-16 22:36:10 -0700859 add_dense_dense(temp_matrix2,
Austin Schuhbd75c482024-08-18 00:03:51 -0700860 angle_cross(temp_matrix3, add(omega_, result.omegas)),
Austin Schuh2a1abec2024-07-10 20:31:16 -0700861 result.contact_patch_velocity);
justinT21446e4f62024-06-16 22:36:10 -0700862
863 VLOG(1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700864 VLOG(1) << "contact patch velocity: "
865 << result.contact_patch_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -0700866
867 // Relative velocity of the surface of the wheel to the ground.
Austin Schuhb67a38f2024-07-04 13:48:38 -0700868 result.wheel_ground_velocity = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700869 mul_dense_dense(R(neg(add(result.thetas, theta_))),
870 result.contact_patch_velocity,
Austin Schuhb67a38f2024-07-04 13:48:38 -0700871 result.wheel_ground_velocity);
justinT21446e4f62024-06-16 22:36:10 -0700872
Austin Schuhb8b34be2024-07-14 16:06:19 -0700873 // Compute the relative velocity between the wheel surface and the ground in
874 // the wheel coordinate system.
875 result.wheel_slip_velocity = DenseMatrix(2, 1);
876 DenseMatrix wheel_velocity =
877 DenseMatrix(2, 1, {mul(rw_, result.omegad), integer(0)});
878 DenseMatrix negative_wheel_ground_velocity =
879 DenseMatrix(2, 1,
880 {neg(result.wheel_ground_velocity.get(0, 0)),
881 neg(result.wheel_ground_velocity.get(1, 0))});
882 add_dense_dense(negative_wheel_ground_velocity, wheel_velocity,
883 result.wheel_slip_velocity);
884
justinT21446e4f62024-06-16 22:36:10 -0700885 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700886 VLOG(1) << "wheel ground velocity: "
887 << result.wheel_ground_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -0700888
Austin Schuh5ddcb472024-07-21 17:55:34 -0700889 result.slip_angle = sin(neg(atan2(result.wheel_ground_velocity.get(1, 0),
890 result.wheel_ground_velocity.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -0700891
892 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700893 VLOG(1) << "slip angle: " << result.slip_angle->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700894
Austin Schuh2a1abec2024-07-10 20:31:16 -0700895 // TODO(austin): Does this handle decel properly?
Austin Schuhb67a38f2024-07-04 13:48:38 -0700896 result.slip_ratio = div(
Austin Schuh2a1abec2024-07-10 20:31:16 -0700897 sub(mul(rw_, result.omegad), result.wheel_ground_velocity.get(0, 0)),
898 SymEngine::max(
899 {real_double(0.02), abs(result.wheel_ground_velocity.get(0, 0))}));
justinT21446e4f62024-06-16 22:36:10 -0700900 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700901 VLOG(1) << "Slip ratio " << result.slip_ratio->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700902
Austin Schuh6ea789e2024-07-27 13:45:53 -0700903 result.full.Fwx = simplify(mul(Cx_, result.slip_ratio));
Austin Schuhb67a38f2024-07-04 13:48:38 -0700904 result.Fwy = simplify(mul(Cy_, result.slip_angle));
justinT21446e4f62024-06-16 22:36:10 -0700905
Austin Schuh27694fa2024-07-20 16:29:49 -0700906 // The self-aligning moment needs to flip when the module flips direction.
Austin Schuhb8b34be2024-07-14 16:06:19 -0700907 result.Ms = mul(neg(result.Fwy),
Austin Schuh27694fa2024-07-20 16:29:49 -0700908 add(div(mul(sign(result.wheel_ground_velocity.get(0, 0)),
909 contact_patch_length_),
910 integer(3)),
911 caster_));
justinT21446e4f62024-06-16 22:36:10 -0700912 VLOG(1);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700913 VLOG(1) << "Ms " << result.Ms->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700914 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700915 VLOG(1) << "full.Fwx " << result.full.Fwx->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700916 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700917 VLOG(1) << "Fwy " << result.Fwy->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700918
Austin Schuh6ea789e2024-07-27 13:45:53 -0700919 // -K_td * Id / Gd + Fwx * rw = 0
920 // Fwx = K_td * Id / Gd / rw
921 result.direct.Fwx = mul(Ktd_, div(result.Id, mul(Gd_, rw_)));
922
923 result.direct.alphas_eqn =
924 SteerAccel(result.direct.Fwx, result.Ms, result.Is);
925
926 // d/dt omegas = ...
927 result.full.alphas_eqn = SteerAccel(result.full.Fwx, result.Ms, result.Is);
justinT21446e4f62024-06-16 22:36:10 -0700928
929 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700930 VLOG(1) << alphas->__str__() << " = " << result.full.alphas_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700931
Austin Schuh6ea789e2024-07-27 13:45:53 -0700932 RCP<const Basic> lhs =
933 sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), alphas),
934 mul(Gd1_, mul(Gd2_, alphamd)));
935 RCP<const Basic> ddplanitary_eqn = sub(mul(Gd3_, lhs), alphad);
justinT21446e4f62024-06-16 22:36:10 -0700936
Austin Schuh6ea789e2024-07-27 13:45:53 -0700937 RCP<const Basic> full_drive_eqn =
938 sub(add(mul(neg(Jdm_), div(alphamd, Gd_)),
939 mul(Ktd_, div(neg(result.Id), Gd_))),
940 mul(neg(result.full.Fwx), rw_));
justinT21446e4f62024-06-16 22:36:10 -0700941
Austin Schuh6ea789e2024-07-27 13:45:53 -0700942 VLOG(1) << "full_drive_eqn: " << full_drive_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700943
944 // Substitute in ddplanitary_eqn so we get rid of alphamd
945 map_basic_basic map;
946 RCP<const Set> reals = interval(NegInf, Inf, true, true);
947 RCP<const Set> solve_solution = solve(ddplanitary_eqn, alphamd, reals);
948 map[alphamd] = solve_solution->get_args()[1]->get_args()[0];
949 VLOG(1) << "temp: " << solve_solution->__str__();
Austin Schuh6ea789e2024-07-27 13:45:53 -0700950 RCP<const Basic> drive_eqn_subs = full_drive_eqn->subs(map);
justinT21446e4f62024-06-16 22:36:10 -0700951
952 map.clear();
Austin Schuh6ea789e2024-07-27 13:45:53 -0700953 map[alphas] = result.full.alphas_eqn;
justinT21446e4f62024-06-16 22:36:10 -0700954 RCP<const Basic> drive_eqn_subs2 = drive_eqn_subs->subs(map);
955 RCP<const Basic> drive_eqn_subs3 = simplify(drive_eqn_subs2);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700956 VLOG(1) << "full_drive_eqn simplified: " << drive_eqn_subs3->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700957
Austin Schuh6ea789e2024-07-27 13:45:53 -0700958 solve_solution = solve(drive_eqn_subs3, alphad, reals);
justinT21446e4f62024-06-16 22:36:10 -0700959
Austin Schuh6ea789e2024-07-27 13:45:53 -0700960 result.full.alphad_eqn =
justinT21446e4f62024-06-16 22:36:10 -0700961 simplify(solve_solution->get_args()[1]->get_args()[0]);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700962 VLOG(1) << "drive_accel: " << result.full.alphad_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700963
Austin Schuh2a1abec2024-07-10 20:31:16 -0700964 // Compute the resulting force from the module.
Austin Schuh6ea789e2024-07-27 13:45:53 -0700965 result.full.F = DenseMatrix(2, 1);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700966 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuh6ea789e2024-07-27 13:45:53 -0700967 DenseMatrix(2, 1, {result.full.Fwx, result.Fwy}),
968 result.full.F);
969 result.full.torque = force_cross(result.mounting_location, result.full.F);
justinT21446e4f62024-06-16 22:36:10 -0700970
Austin Schuh6ea789e2024-07-27 13:45:53 -0700971 result.direct.F = DenseMatrix(2, 1);
972 mul_dense_dense(R(add(theta_, result.thetas)),
973 DenseMatrix(2, 1, {result.direct.Fwx, result.Fwy}),
974 result.direct.F);
975 result.direct.torque =
976 force_cross(result.mounting_location, result.direct.F);
justinT21446e4f62024-06-16 22:36:10 -0700977
978 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700979 VLOG(1) << "full torque = " << result.full.torque->__str__();
980 VLOG(1) << "direct torque = " << result.full.torque->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700981
justinT21446e4f62024-06-16 22:36:10 -0700982 return result;
983 }
984
985 DenseMatrix R(const RCP<const Basic> theta) {
986 return DenseMatrix(2, 2,
987 {cos(theta), neg(sin(theta)), sin(theta), cos(theta)});
988 }
989
990 DenseMatrix angle_cross(DenseMatrix a, RCP<const Basic> b) {
Austin Schuh2a1abec2024-07-10 20:31:16 -0700991 return DenseMatrix(2, 1, {mul(neg(a.get(1, 0)), b), mul(a.get(0, 0), b)});
justinT21446e4f62024-06-16 22:36:10 -0700992 }
993
994 RCP<const Basic> force_cross(DenseMatrix r, DenseMatrix f) {
995 return sub(mul(r.get(0, 0), f.get(1, 0)), mul(r.get(1, 0), f.get(0, 0)));
996 }
997
998 // z represents the number of teeth per gear, theta is the angle between
999 // shafts(in degrees), D_02 is the pitch diameter of gear 2 and b_2 is the
1000 // length of the tooth of gear 2
1001 // returns std::pair(r_01, r_02)
1002 std::pair<double, double> GetBevelPitchRadius(double z1, double z2,
1003 double theta, double D_02,
1004 double b_2) {
1005 double gamma_1 = std::atan2(z1, z2);
1006 double gamma_2 = theta / 180.0 * std::numbers::pi - gamma_1;
1007 double R_m = D_02 / 2 / std::sin(gamma_2) - b_2 / 2;
1008 return std::pair(R_m * std::cos(gamma_2), R_m * std::sin(gamma_2));
1009 }
1010
1011 Motor drive_motor_;
1012 Motor steer_motor_;
1013
1014 RCP<const Basic> Cx_;
1015 RCP<const Basic> Cy_;
Austin Schuh2a1abec2024-07-10 20:31:16 -07001016 RCP<const Basic> rw_;
justinT21446e4f62024-06-16 22:36:10 -07001017 RCP<const Basic> m_;
1018 RCP<const Basic> J_;
1019 RCP<const Basic> Gd1_;
1020 RCP<const Basic> rs_;
1021 RCP<const Basic> rp_;
1022 RCP<const Basic> Gd2_;
1023 RCP<const Basic> rb1_;
1024 RCP<const Basic> rb2_;
1025 RCP<const Basic> Gd3_;
1026 RCP<const Basic> Gd_;
1027 RCP<const Basic> Js_;
1028 RCP<const Basic> Gs_;
1029 RCP<const Basic> wb_;
1030 RCP<const Basic> Jdm_;
1031 RCP<const Basic> Jsm_;
1032 RCP<const Basic> Kts_;
1033 RCP<const Basic> Ktd_;
1034 RCP<const Basic> robot_width_;
1035 RCP<const Basic> caster_;
1036 RCP<const Basic> contact_patch_length_;
1037 RCP<const Basic> x_;
1038 RCP<const Basic> y_;
1039 RCP<const Basic> theta_;
1040 RCP<const Basic> vx_;
1041 RCP<const Basic> vy_;
1042 RCP<const Basic> omega_;
1043 RCP<const Basic> ax_;
1044 RCP<const Basic> ay_;
1045 RCP<const Basic> atheta_;
1046
1047 std::array<Module, kNumModules> modules_;
1048
Austin Schuh6ea789e2024-07-27 13:45:53 -07001049 DenseMatrix full_accel_;
1050 RCP<const Basic> full_angular_accel_;
1051 DenseMatrix direct_accel_;
1052 RCP<const Basic> direct_angular_accel_;
justinT21446e4f62024-06-16 22:36:10 -07001053};
1054
1055} // namespace frc971::control_loops::swerve
1056
1057int main(int argc, char **argv) {
1058 aos::InitGoogle(&argc, &argv);
1059
1060 frc971::control_loops::swerve::SwerveSimulation sim;
1061
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001062 if (!absl::GetFlag(FLAGS_cc_output_path).empty() &&
1063 !absl::GetFlag(FLAGS_h_output_path).empty()) {
1064 sim.Write(absl::GetFlag(FLAGS_cc_output_path),
1065 absl::GetFlag(FLAGS_h_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001066 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001067 if (!absl::GetFlag(FLAGS_casadi_py_output_path).empty()) {
1068 sim.WriteCasadi(absl::GetFlag(FLAGS_casadi_py_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001069 }
justinT21446e4f62024-06-16 22:36:10 -07001070
1071 return 0;
1072}