blob: e712a392f0c612dab67b2e69b50e8615559746b2 [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 Schuh2a1abec2024-07-10 20:31:16 -0700577
Austin Schuh0f881092024-06-28 15:36:48 -0700578 result_py.emplace_back("# Returns the derivative of our state vector");
579 result_py.emplace_back("# [thetas0, thetad0, omegas0, omegad0,");
580 result_py.emplace_back("# thetas1, thetad1, omegas1, omegad1,");
581 result_py.emplace_back("# thetas2, thetad2, omegas2, omegad2,");
582 result_py.emplace_back("# thetas3, thetad3, omegas3, omegad3,");
583 result_py.emplace_back("# x, y, theta, vx, vy, omega,");
584 result_py.emplace_back("# Fx, Fy, Moment]");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700585 result_py.emplace_back("def swerve_full_dynamics(X, U):");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700586 WriteCasadiVariables(&result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700587
588 result_py.emplace_back("");
589 result_py.emplace_back(" result = casadi.SX.sym('result', 25, 1)");
590 result_py.emplace_back("");
591
592 // And then write out the derivative of each state.
593 for (size_t m = 0; m < kNumModules; ++m) {
594 result_py.emplace_back(
595 absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m));
596 result_py.emplace_back(
597 absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m));
598
Austin Schuh6ea789e2024-07-27 13:45:53 -0700599 result_py.emplace_back(
600 absl::Substitute(" result[$0, 0] = $1", m * 4 + 2,
601 ccode(*modules_[m].full.alphas_eqn)));
602 result_py.emplace_back(
603 absl::Substitute(" result[$0, 0] = $1", m * 4 + 3,
604 ccode(*modules_[m].full.alphad_eqn)));
Austin Schuh0f881092024-06-28 15:36:48 -0700605 }
606
607 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700608 absl::Substitute(" result[$0, 0] = vx", kNumModules * 4 + 0));
Austin Schuh0f881092024-06-28 15:36:48 -0700609 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700610 absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1));
Austin Schuh0f881092024-06-28 15:36:48 -0700611 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700612 absl::Substitute(" result[$0, 0] = omega", kNumModules * 4 + 2));
Austin Schuh0f881092024-06-28 15:36:48 -0700613
Austin Schuh0f881092024-06-28 15:36:48 -0700614 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700615 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700616 ccode(*full_accel_.get(0, 0))));
Austin Schuh0f881092024-06-28 15:36:48 -0700617 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700618 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700619 ccode(*full_accel_.get(1, 0))));
620 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
621 kNumModules * 4 + 5,
622 ccode(*full_angular_accel_)));
Austin Schuh0f881092024-06-28 15:36:48 -0700623
624 result_py.emplace_back(
625 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6));
626 result_py.emplace_back(
627 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7));
628 result_py.emplace_back(
629 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8));
630
631 result_py.emplace_back("");
632 result_py.emplace_back(
633 " return casadi.Function('xdot', [X, U], [result])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700634
Austin Schuh6ea789e2024-07-27 13:45:53 -0700635 result_py.emplace_back("");
636
637 result_py.emplace_back("# Returns the derivative of our state vector");
638 result_py.emplace_back("# [thetas0, omegas0,");
639 result_py.emplace_back("# thetas1, omegas1,");
640 result_py.emplace_back("# thetas2, omegas2,");
641 result_py.emplace_back("# thetas3, omegas3,");
642 result_py.emplace_back("# theta, vx, vy, omega]");
643 result_py.emplace_back("def velocity_swerve_physics(X, U):");
644 WriteCasadiVelocityVariables(&result_py);
645
646 result_py.emplace_back("");
647 result_py.emplace_back(
648 " result = casadi.SX.sym('result', NUM_VELOCITY_STATES, 1)");
649 result_py.emplace_back("");
650
651 // And then write out the derivative of each state.
652 for (size_t m = 0; m < kNumModules; ++m) {
653 result_py.emplace_back(
654 absl::Substitute(" result[$0, 0] = omegas$1", m * 2 + 0, m));
655 result_py.emplace_back(
656 absl::Substitute(" result[$0, 0] = $1", m * 2 + 1,
657 ccode(*modules_[m].direct.alphas_eqn)));
658 }
659 result_py.emplace_back(
660 absl::Substitute(" result[$0, 0] = omega", kNumModules * 2 + 0));
661
662 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
663 kNumModules * 2 + 1,
664 ccode(*direct_accel_.get(0, 0))));
665 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
666 kNumModules * 2 + 2,
667 ccode(*direct_accel_.get(1, 0))));
668 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
669 kNumModules * 2 + 3,
670 ccode(*direct_angular_accel_)));
671
672 // result_py.emplace_back(
673 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 4));
674 // result_py.emplace_back(
675 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 5));
676 // result_py.emplace_back(
677 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 6));
678
679 result_py.emplace_back("");
680 result_py.emplace_back(
681 " return casadi.Function('xdot', [X, U], [result])");
682
Austin Schuhb8b34be2024-07-14 16:06:19 -0700683 DefineVector2dFunction(
684 "contact_patch_velocity",
685 "# Returns the velocity of the wheel in global coordinates.",
686 [](const Module &m, int dimension) {
687 return ccode(*m.contact_patch_velocity.get(dimension, 0));
688 },
689 &result_py);
690 DefineVector2dFunction(
691 "wheel_ground_velocity",
692 "# Returns the velocity of the wheel in steer module coordinates.",
693 [](const Module &m, int dimension) {
694 return ccode(*m.wheel_ground_velocity.get(dimension, 0));
695 },
696 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700697
Austin Schuhb8b34be2024-07-14 16:06:19 -0700698 DefineVector2dFunction(
699 "wheel_slip_velocity",
700 "# Returns the difference in velocities of the wheel surface and the "
701 "ground.",
702 [](const Module &m, int dimension) {
703 return ccode(*m.wheel_slip_velocity.get(dimension, 0));
704 },
705 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700706
Austin Schuhb8b34be2024-07-14 16:06:19 -0700707 DefineScalarFunction(
708 "slip_angle", "Returns the slip angle of the ith wheel",
709 [](const Module &m) { return ccode(*m.slip_angle); }, &result_py);
710 DefineScalarFunction(
711 "slip_ratio", "Returns the slip ratio of the ith wheel",
712 [](const Module &m) { return ccode(*m.slip_ratio); }, &result_py);
713 DefineScalarFunction(
714 "module_angular_accel",
715 "Returns the angular acceleration of the robot due to the ith wheel",
Austin Schuh6ea789e2024-07-27 13:45:53 -0700716 [this](const Module &m) { return ccode(*div(m.full.torque, Js_)); },
717 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700718
Austin Schuhb8b34be2024-07-14 16:06:19 -0700719 DefineVector2dFunction(
720 "wheel_force",
721 "Returns the force on the wheel in steer module coordinates",
722 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700723 return ccode(
724 *std::vector<RCP<const Basic>>{m.full.Fwx, m.Fwy}[dimension]);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700725 },
726 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700727
Austin Schuhb8b34be2024-07-14 16:06:19 -0700728 DefineVector2dFunction(
729 "F", "Returns the force on the wheel in absolute coordinates",
730 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700731 return ccode(*m.full.F.get(dimension, 0));
Austin Schuhb8b34be2024-07-14 16:06:19 -0700732 },
733 &result_py);
734
735 DefineVector2dFunction(
736 "mounting_location",
737 "Returns the mounting location of wheel in robot coordinates",
738 [](const Module &m, int dimension) {
739 return ccode(*m.mounting_location.get(dimension, 0));
740 },
741 &result_py);
742
743 DefineScalarFunction(
744 "Ms", "Returns the self aligning moment of the ith wheel",
745 [this](const Module &m) {
746 return ccode(*(div(m.Ms, add(Jsm_, div(div(Js_, Gs_), Gs_)))));
747 },
748 &result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700749
750 aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n"));
751 }
752
Austin Schuhb8b34be2024-07-14 16:06:19 -0700753 void DefineScalarFunction(
754 std::string_view name, std::string_view documentation,
755 std::function<std::string(const Module &)> scalar_fn,
756 std::vector<std::string> *result_py) {
757 result_py->emplace_back("");
758 result_py->emplace_back(absl::Substitute("# $0.", documentation));
759 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
760 WriteCasadiVariables(result_py);
761 for (size_t m = 0; m < kNumModules; ++m) {
762 if (m == 0) {
763 result_py->emplace_back(" if i == 0:");
764 } else {
765 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
766 }
767 result_py->emplace_back(
768 absl::Substitute(" return casadi.Function('$0', [X, U], [$1])",
769 name, scalar_fn(modules_[m])));
770 }
771 result_py->emplace_back(" raise ValueError(\"Invalid module number\")");
772 }
773
774 void DefineVector2dFunction(
775 std::string_view name, std::string_view documentation,
776 std::function<std::string(const Module &, int)> scalar_fn,
777 std::vector<std::string> *result_py) {
778 result_py->emplace_back("");
779 result_py->emplace_back(absl::Substitute("# $0.", documentation));
780 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
781 WriteCasadiVariables(result_py);
782 result_py->emplace_back(
783 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
784 for (size_t m = 0; m < kNumModules; ++m) {
785 if (m == 0) {
786 result_py->emplace_back(" if i == 0:");
787 } else {
788 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
789 }
790 for (int j = 0; j < 2; ++j) {
791 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
792 j, scalar_fn(modules_[m], j)));
793 }
794 }
795 result_py->emplace_back(" else:");
796 result_py->emplace_back(
797 " raise ValueError(\"Invalid module number\")");
798 result_py->emplace_back(absl::Substitute(
799 " return casadi.Function('$0', [X, U], [result])", name));
800 }
801
justinT21446e4f62024-06-16 22:36:10 -0700802 private:
803 static constexpr uint8_t kNumModules = 4;
804
Austin Schuh6ea789e2024-07-27 13:45:53 -0700805 RCP<const Basic> SteerAccel(RCP<const Basic> Fwx, RCP<const Basic> Ms,
806 RCP<const Basic> Is) {
807 RCP<const Basic> lhms =
808 mul(add(neg(wb_), mul(add(rs_, rp_), sub(integer(1), div(rb1_, rp_)))),
809 mul(div(rw_, rb2_), neg(Fwx)));
810 RCP<const Basic> lhs = add(add(Ms, div(mul(Kts_, Is), Gs_)), lhms);
811 RCP<const Basic> rhs = add(Jsm_, div(div(Js_, Gs_), Gs_));
812 return simplify(div(lhs, rhs));
813 }
814
justinT21446e4f62024-06-16 22:36:10 -0700815 Module ModulePhysics(const int m, DenseMatrix mounting_location) {
816 VLOG(1) << "Solving module " << m;
817
818 Module result;
Austin Schuhb8b34be2024-07-14 16:06:19 -0700819 result.mounting_location = mounting_location;
justinT21446e4f62024-06-16 22:36:10 -0700820
821 result.Is = symbol(absl::StrFormat("Is%u", m));
822 result.Id = symbol(absl::StrFormat("Id%u", m));
823
824 RCP<const Symbol> thetamd = symbol(absl::StrFormat("theta_md%u", m));
825 RCP<const Symbol> omegamd = symbol(absl::StrFormat("omega_md%u", m));
826 RCP<const Symbol> alphamd = symbol(absl::StrFormat("alpha_md%u", m));
827
828 result.thetas = symbol(absl::StrFormat("thetas%u", m));
829 result.omegas = symbol(absl::StrFormat("omegas%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -0700830 RCP<const Symbol> alphas = symbol(absl::StrFormat("alphas%u", m));
justinT21446e4f62024-06-16 22:36:10 -0700831
justinT21446e4f62024-06-16 22:36:10 -0700832 result.omegad = symbol(absl::StrFormat("omegad%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -0700833 RCP<const Symbol> alphad = symbol(absl::StrFormat("alphad%u", m));
justinT21446e4f62024-06-16 22:36:10 -0700834
835 // Velocity of the module in field coordinates
Austin Schuh2a1abec2024-07-10 20:31:16 -0700836 DenseMatrix robot_velocity = DenseMatrix(2, 1, {vx_, vy_});
justinT21446e4f62024-06-16 22:36:10 -0700837 VLOG(1) << "robot velocity: " << robot_velocity.__str__();
838
839 // Velocity of the contact patch in field coordinates
840 DenseMatrix temp_matrix = DenseMatrix(2, 1);
841 DenseMatrix temp_matrix2 = DenseMatrix(2, 1);
Austin Schuhbd75c482024-08-18 00:03:51 -0700842 DenseMatrix temp_matrix3 = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700843 result.contact_patch_velocity = DenseMatrix(2, 1);
justinT21446e4f62024-06-16 22:36:10 -0700844
Austin Schuhb8b34be2024-07-14 16:06:19 -0700845 mul_dense_dense(R(theta_), result.mounting_location, temp_matrix);
justinT21446e4f62024-06-16 22:36:10 -0700846 add_dense_dense(angle_cross(temp_matrix, omega_), robot_velocity,
847 temp_matrix2);
848 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuhbd75c482024-08-18 00:03:51 -0700849 DenseMatrix(2, 1, {neg(caster_), integer(0)}),
850 temp_matrix3);
justinT21446e4f62024-06-16 22:36:10 -0700851 add_dense_dense(temp_matrix2,
Austin Schuhbd75c482024-08-18 00:03:51 -0700852 angle_cross(temp_matrix3, add(omega_, result.omegas)),
Austin Schuh2a1abec2024-07-10 20:31:16 -0700853 result.contact_patch_velocity);
justinT21446e4f62024-06-16 22:36:10 -0700854
855 VLOG(1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700856 VLOG(1) << "contact patch velocity: "
857 << result.contact_patch_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -0700858
859 // Relative velocity of the surface of the wheel to the ground.
Austin Schuhb67a38f2024-07-04 13:48:38 -0700860 result.wheel_ground_velocity = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700861 mul_dense_dense(R(neg(add(result.thetas, theta_))),
862 result.contact_patch_velocity,
Austin Schuhb67a38f2024-07-04 13:48:38 -0700863 result.wheel_ground_velocity);
justinT21446e4f62024-06-16 22:36:10 -0700864
Austin Schuhb8b34be2024-07-14 16:06:19 -0700865 // Compute the relative velocity between the wheel surface and the ground in
866 // the wheel coordinate system.
867 result.wheel_slip_velocity = DenseMatrix(2, 1);
868 DenseMatrix wheel_velocity =
869 DenseMatrix(2, 1, {mul(rw_, result.omegad), integer(0)});
870 DenseMatrix negative_wheel_ground_velocity =
871 DenseMatrix(2, 1,
872 {neg(result.wheel_ground_velocity.get(0, 0)),
873 neg(result.wheel_ground_velocity.get(1, 0))});
874 add_dense_dense(negative_wheel_ground_velocity, wheel_velocity,
875 result.wheel_slip_velocity);
876
justinT21446e4f62024-06-16 22:36:10 -0700877 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700878 VLOG(1) << "wheel ground velocity: "
879 << result.wheel_ground_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -0700880
Austin Schuh5ddcb472024-07-21 17:55:34 -0700881 result.slip_angle = sin(neg(atan2(result.wheel_ground_velocity.get(1, 0),
882 result.wheel_ground_velocity.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -0700883
884 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700885 VLOG(1) << "slip angle: " << result.slip_angle->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700886
Austin Schuh2a1abec2024-07-10 20:31:16 -0700887 // TODO(austin): Does this handle decel properly?
Austin Schuhb67a38f2024-07-04 13:48:38 -0700888 result.slip_ratio = div(
Austin Schuh2a1abec2024-07-10 20:31:16 -0700889 sub(mul(rw_, result.omegad), result.wheel_ground_velocity.get(0, 0)),
890 SymEngine::max(
891 {real_double(0.02), abs(result.wheel_ground_velocity.get(0, 0))}));
justinT21446e4f62024-06-16 22:36:10 -0700892 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700893 VLOG(1) << "Slip ratio " << result.slip_ratio->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700894
Austin Schuh6ea789e2024-07-27 13:45:53 -0700895 result.full.Fwx = simplify(mul(Cx_, result.slip_ratio));
Austin Schuhb67a38f2024-07-04 13:48:38 -0700896 result.Fwy = simplify(mul(Cy_, result.slip_angle));
justinT21446e4f62024-06-16 22:36:10 -0700897
Austin Schuh27694fa2024-07-20 16:29:49 -0700898 // The self-aligning moment needs to flip when the module flips direction.
Austin Schuh78a1b312024-08-18 17:21:34 -0700899 RCP<const Basic> softsign_velocity = add(
900 div(integer(-2),
901 add(integer(1), exp(mul(integer(100),
902 result.wheel_ground_velocity.get(0, 0))))),
903 integer(1));
904 result.Ms =
905 mul(neg(result.Fwy),
906 add(div(mul(softsign_velocity, contact_patch_length_), integer(3)),
907 caster_));
justinT21446e4f62024-06-16 22:36:10 -0700908 VLOG(1);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700909 VLOG(1) << "Ms " << result.Ms->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700910 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700911 VLOG(1) << "full.Fwx " << result.full.Fwx->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700912 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700913 VLOG(1) << "Fwy " << result.Fwy->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700914
Austin Schuh6ea789e2024-07-27 13:45:53 -0700915 // -K_td * Id / Gd + Fwx * rw = 0
916 // Fwx = K_td * Id / Gd / rw
917 result.direct.Fwx = mul(Ktd_, div(result.Id, mul(Gd_, rw_)));
918
919 result.direct.alphas_eqn =
920 SteerAccel(result.direct.Fwx, result.Ms, result.Is);
921
922 // d/dt omegas = ...
923 result.full.alphas_eqn = SteerAccel(result.full.Fwx, result.Ms, result.Is);
justinT21446e4f62024-06-16 22:36:10 -0700924
925 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700926 VLOG(1) << alphas->__str__() << " = " << result.full.alphas_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700927
Austin Schuh6ea789e2024-07-27 13:45:53 -0700928 RCP<const Basic> lhs =
929 sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), alphas),
930 mul(Gd1_, mul(Gd2_, alphamd)));
931 RCP<const Basic> ddplanitary_eqn = sub(mul(Gd3_, lhs), alphad);
justinT21446e4f62024-06-16 22:36:10 -0700932
Austin Schuh6ea789e2024-07-27 13:45:53 -0700933 RCP<const Basic> full_drive_eqn =
934 sub(add(mul(neg(Jdm_), div(alphamd, Gd_)),
935 mul(Ktd_, div(neg(result.Id), Gd_))),
936 mul(neg(result.full.Fwx), rw_));
justinT21446e4f62024-06-16 22:36:10 -0700937
Austin Schuh6ea789e2024-07-27 13:45:53 -0700938 VLOG(1) << "full_drive_eqn: " << full_drive_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700939
940 // Substitute in ddplanitary_eqn so we get rid of alphamd
941 map_basic_basic map;
942 RCP<const Set> reals = interval(NegInf, Inf, true, true);
943 RCP<const Set> solve_solution = solve(ddplanitary_eqn, alphamd, reals);
944 map[alphamd] = solve_solution->get_args()[1]->get_args()[0];
945 VLOG(1) << "temp: " << solve_solution->__str__();
Austin Schuh6ea789e2024-07-27 13:45:53 -0700946 RCP<const Basic> drive_eqn_subs = full_drive_eqn->subs(map);
justinT21446e4f62024-06-16 22:36:10 -0700947
948 map.clear();
Austin Schuh6ea789e2024-07-27 13:45:53 -0700949 map[alphas] = result.full.alphas_eqn;
justinT21446e4f62024-06-16 22:36:10 -0700950 RCP<const Basic> drive_eqn_subs2 = drive_eqn_subs->subs(map);
951 RCP<const Basic> drive_eqn_subs3 = simplify(drive_eqn_subs2);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700952 VLOG(1) << "full_drive_eqn simplified: " << drive_eqn_subs3->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700953
Austin Schuh6ea789e2024-07-27 13:45:53 -0700954 solve_solution = solve(drive_eqn_subs3, alphad, reals);
justinT21446e4f62024-06-16 22:36:10 -0700955
Austin Schuh6ea789e2024-07-27 13:45:53 -0700956 result.full.alphad_eqn =
justinT21446e4f62024-06-16 22:36:10 -0700957 simplify(solve_solution->get_args()[1]->get_args()[0]);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700958 VLOG(1) << "drive_accel: " << result.full.alphad_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700959
Austin Schuh2a1abec2024-07-10 20:31:16 -0700960 // Compute the resulting force from the module.
Austin Schuh6ea789e2024-07-27 13:45:53 -0700961 result.full.F = DenseMatrix(2, 1);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700962 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuh6ea789e2024-07-27 13:45:53 -0700963 DenseMatrix(2, 1, {result.full.Fwx, result.Fwy}),
964 result.full.F);
965 result.full.torque = force_cross(result.mounting_location, result.full.F);
justinT21446e4f62024-06-16 22:36:10 -0700966
Austin Schuh6ea789e2024-07-27 13:45:53 -0700967 result.direct.F = DenseMatrix(2, 1);
968 mul_dense_dense(R(add(theta_, result.thetas)),
969 DenseMatrix(2, 1, {result.direct.Fwx, result.Fwy}),
970 result.direct.F);
971 result.direct.torque =
972 force_cross(result.mounting_location, result.direct.F);
justinT21446e4f62024-06-16 22:36:10 -0700973
974 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700975 VLOG(1) << "full torque = " << result.full.torque->__str__();
976 VLOG(1) << "direct torque = " << result.full.torque->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700977
justinT21446e4f62024-06-16 22:36:10 -0700978 return result;
979 }
980
981 DenseMatrix R(const RCP<const Basic> theta) {
982 return DenseMatrix(2, 2,
983 {cos(theta), neg(sin(theta)), sin(theta), cos(theta)});
984 }
985
986 DenseMatrix angle_cross(DenseMatrix a, RCP<const Basic> b) {
Austin Schuh2a1abec2024-07-10 20:31:16 -0700987 return DenseMatrix(2, 1, {mul(neg(a.get(1, 0)), b), mul(a.get(0, 0), b)});
justinT21446e4f62024-06-16 22:36:10 -0700988 }
989
990 RCP<const Basic> force_cross(DenseMatrix r, DenseMatrix f) {
991 return sub(mul(r.get(0, 0), f.get(1, 0)), mul(r.get(1, 0), f.get(0, 0)));
992 }
993
994 // z represents the number of teeth per gear, theta is the angle between
995 // shafts(in degrees), D_02 is the pitch diameter of gear 2 and b_2 is the
996 // length of the tooth of gear 2
997 // returns std::pair(r_01, r_02)
998 std::pair<double, double> GetBevelPitchRadius(double z1, double z2,
999 double theta, double D_02,
1000 double b_2) {
1001 double gamma_1 = std::atan2(z1, z2);
1002 double gamma_2 = theta / 180.0 * std::numbers::pi - gamma_1;
1003 double R_m = D_02 / 2 / std::sin(gamma_2) - b_2 / 2;
1004 return std::pair(R_m * std::cos(gamma_2), R_m * std::sin(gamma_2));
1005 }
1006
1007 Motor drive_motor_;
1008 Motor steer_motor_;
1009
1010 RCP<const Basic> Cx_;
1011 RCP<const Basic> Cy_;
Austin Schuh2a1abec2024-07-10 20:31:16 -07001012 RCP<const Basic> rw_;
justinT21446e4f62024-06-16 22:36:10 -07001013 RCP<const Basic> m_;
1014 RCP<const Basic> J_;
1015 RCP<const Basic> Gd1_;
1016 RCP<const Basic> rs_;
1017 RCP<const Basic> rp_;
1018 RCP<const Basic> Gd2_;
1019 RCP<const Basic> rb1_;
1020 RCP<const Basic> rb2_;
1021 RCP<const Basic> Gd3_;
1022 RCP<const Basic> Gd_;
1023 RCP<const Basic> Js_;
1024 RCP<const Basic> Gs_;
1025 RCP<const Basic> wb_;
1026 RCP<const Basic> Jdm_;
1027 RCP<const Basic> Jsm_;
1028 RCP<const Basic> Kts_;
1029 RCP<const Basic> Ktd_;
1030 RCP<const Basic> robot_width_;
1031 RCP<const Basic> caster_;
1032 RCP<const Basic> contact_patch_length_;
1033 RCP<const Basic> x_;
1034 RCP<const Basic> y_;
1035 RCP<const Basic> theta_;
1036 RCP<const Basic> vx_;
1037 RCP<const Basic> vy_;
1038 RCP<const Basic> omega_;
1039 RCP<const Basic> ax_;
1040 RCP<const Basic> ay_;
1041 RCP<const Basic> atheta_;
1042
1043 std::array<Module, kNumModules> modules_;
1044
Austin Schuh6ea789e2024-07-27 13:45:53 -07001045 DenseMatrix full_accel_;
1046 RCP<const Basic> full_angular_accel_;
1047 DenseMatrix direct_accel_;
1048 RCP<const Basic> direct_angular_accel_;
justinT21446e4f62024-06-16 22:36:10 -07001049};
1050
1051} // namespace frc971::control_loops::swerve
1052
1053int main(int argc, char **argv) {
1054 aos::InitGoogle(&argc, &argv);
1055
1056 frc971::control_loops::swerve::SwerveSimulation sim;
1057
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001058 if (!absl::GetFlag(FLAGS_cc_output_path).empty() &&
1059 !absl::GetFlag(FLAGS_h_output_path).empty()) {
1060 sim.Write(absl::GetFlag(FLAGS_cc_output_path),
1061 absl::GetFlag(FLAGS_h_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001062 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001063 if (!absl::GetFlag(FLAGS_casadi_py_output_path).empty()) {
1064 sim.WriteCasadi(absl::GetFlag(FLAGS_casadi_py_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001065 }
justinT21446e4f62024-06-16 22:36:10 -07001066
1067 return 0;
1068}