blob: d647d4bac96d99ed79beca33b2ae5d1da830e732 [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;
justinT21cc6eed02024-08-26 00:15:57 -0700570 constexpr double kAbsGain = 1.0 / 0.05;
Austin Schuhbd75c482024-08-18 00:03:51 -0700571 result_py.emplace_back("def soft_atan2(y, x):");
572 result_py.emplace_back(" return casadi.arctan2(");
573 result_py.emplace_back(" y,");
574 result_py.emplace_back(" casadi.logsumexp(casadi.SX(numpy.array(");
justinT21cc6eed02024-08-26 00:15:57 -0700575 result_py.emplace_back(
576 absl::Substitute(" [1.0, x * (1.0 - 2.0 / (1 + "
577 "casadi.exp($1.0 * x))) * $0.0]))) / $0.0)",
578 kLogGain, kAbsGain));
Austin Schuhbd75c482024-08-18 00:03:51 -0700579 result_py.emplace_back("");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700580
Austin Schuh0f881092024-06-28 15:36:48 -0700581 result_py.emplace_back("# Returns the derivative of our state vector");
582 result_py.emplace_back("# [thetas0, thetad0, omegas0, omegad0,");
583 result_py.emplace_back("# thetas1, thetad1, omegas1, omegad1,");
584 result_py.emplace_back("# thetas2, thetad2, omegas2, omegad2,");
585 result_py.emplace_back("# thetas3, thetad3, omegas3, omegad3,");
586 result_py.emplace_back("# x, y, theta, vx, vy, omega,");
587 result_py.emplace_back("# Fx, Fy, Moment]");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700588 result_py.emplace_back("def swerve_full_dynamics(X, U):");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700589 WriteCasadiVariables(&result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700590
591 result_py.emplace_back("");
592 result_py.emplace_back(" result = casadi.SX.sym('result', 25, 1)");
593 result_py.emplace_back("");
594
595 // And then write out the derivative of each state.
596 for (size_t m = 0; m < kNumModules; ++m) {
597 result_py.emplace_back(
598 absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m));
599 result_py.emplace_back(
600 absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m));
601
Austin Schuh6ea789e2024-07-27 13:45:53 -0700602 result_py.emplace_back(
603 absl::Substitute(" result[$0, 0] = $1", m * 4 + 2,
604 ccode(*modules_[m].full.alphas_eqn)));
605 result_py.emplace_back(
606 absl::Substitute(" result[$0, 0] = $1", m * 4 + 3,
607 ccode(*modules_[m].full.alphad_eqn)));
Austin Schuh0f881092024-06-28 15:36:48 -0700608 }
609
610 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700611 absl::Substitute(" result[$0, 0] = vx", kNumModules * 4 + 0));
Austin Schuh0f881092024-06-28 15:36:48 -0700612 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700613 absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1));
Austin Schuh0f881092024-06-28 15:36:48 -0700614 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700615 absl::Substitute(" result[$0, 0] = omega", kNumModules * 4 + 2));
Austin Schuh0f881092024-06-28 15:36:48 -0700616
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 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700619 ccode(*full_accel_.get(0, 0))));
Austin Schuh0f881092024-06-28 15:36:48 -0700620 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700621 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700622 ccode(*full_accel_.get(1, 0))));
623 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
624 kNumModules * 4 + 5,
625 ccode(*full_angular_accel_)));
Austin Schuh0f881092024-06-28 15:36:48 -0700626
627 result_py.emplace_back(
628 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6));
629 result_py.emplace_back(
630 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7));
631 result_py.emplace_back(
632 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8));
633
634 result_py.emplace_back("");
635 result_py.emplace_back(
636 " return casadi.Function('xdot', [X, U], [result])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700637
Austin Schuh6ea789e2024-07-27 13:45:53 -0700638 result_py.emplace_back("");
639
640 result_py.emplace_back("# Returns the derivative of our state vector");
641 result_py.emplace_back("# [thetas0, omegas0,");
642 result_py.emplace_back("# thetas1, omegas1,");
643 result_py.emplace_back("# thetas2, omegas2,");
644 result_py.emplace_back("# thetas3, omegas3,");
645 result_py.emplace_back("# theta, vx, vy, omega]");
646 result_py.emplace_back("def velocity_swerve_physics(X, U):");
647 WriteCasadiVelocityVariables(&result_py);
648
649 result_py.emplace_back("");
650 result_py.emplace_back(
651 " result = casadi.SX.sym('result', NUM_VELOCITY_STATES, 1)");
652 result_py.emplace_back("");
653
654 // And then write out the derivative of each state.
655 for (size_t m = 0; m < kNumModules; ++m) {
656 result_py.emplace_back(
657 absl::Substitute(" result[$0, 0] = omegas$1", m * 2 + 0, m));
658 result_py.emplace_back(
659 absl::Substitute(" result[$0, 0] = $1", m * 2 + 1,
660 ccode(*modules_[m].direct.alphas_eqn)));
661 }
662 result_py.emplace_back(
663 absl::Substitute(" result[$0, 0] = omega", kNumModules * 2 + 0));
664
665 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
666 kNumModules * 2 + 1,
667 ccode(*direct_accel_.get(0, 0))));
668 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
669 kNumModules * 2 + 2,
670 ccode(*direct_accel_.get(1, 0))));
671 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
672 kNumModules * 2 + 3,
673 ccode(*direct_angular_accel_)));
674
675 // result_py.emplace_back(
676 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 4));
677 // result_py.emplace_back(
678 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 5));
679 // result_py.emplace_back(
680 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 6));
681
682 result_py.emplace_back("");
683 result_py.emplace_back(
684 " return casadi.Function('xdot', [X, U], [result])");
685
Austin Schuhb8b34be2024-07-14 16:06:19 -0700686 DefineVector2dFunction(
687 "contact_patch_velocity",
688 "# Returns the velocity of the wheel in global coordinates.",
689 [](const Module &m, int dimension) {
690 return ccode(*m.contact_patch_velocity.get(dimension, 0));
691 },
692 &result_py);
693 DefineVector2dFunction(
694 "wheel_ground_velocity",
695 "# Returns the velocity of the wheel in steer module coordinates.",
696 [](const Module &m, int dimension) {
697 return ccode(*m.wheel_ground_velocity.get(dimension, 0));
698 },
699 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700700
Austin Schuhb8b34be2024-07-14 16:06:19 -0700701 DefineVector2dFunction(
702 "wheel_slip_velocity",
703 "# Returns the difference in velocities of the wheel surface and the "
704 "ground.",
705 [](const Module &m, int dimension) {
706 return ccode(*m.wheel_slip_velocity.get(dimension, 0));
707 },
708 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700709
Austin Schuhb8b34be2024-07-14 16:06:19 -0700710 DefineScalarFunction(
711 "slip_angle", "Returns the slip angle of the ith wheel",
712 [](const Module &m) { return ccode(*m.slip_angle); }, &result_py);
713 DefineScalarFunction(
714 "slip_ratio", "Returns the slip ratio of the ith wheel",
715 [](const Module &m) { return ccode(*m.slip_ratio); }, &result_py);
716 DefineScalarFunction(
717 "module_angular_accel",
718 "Returns the angular acceleration of the robot due to the ith wheel",
Austin Schuh6ea789e2024-07-27 13:45:53 -0700719 [this](const Module &m) { return ccode(*div(m.full.torque, Js_)); },
720 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700721
Austin Schuhb8b34be2024-07-14 16:06:19 -0700722 DefineVector2dFunction(
723 "wheel_force",
724 "Returns the force on the wheel in steer module coordinates",
725 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700726 return ccode(
727 *std::vector<RCP<const Basic>>{m.full.Fwx, m.Fwy}[dimension]);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700728 },
729 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700730
Austin Schuhb8b34be2024-07-14 16:06:19 -0700731 DefineVector2dFunction(
732 "F", "Returns the force on the wheel in absolute coordinates",
733 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700734 return ccode(*m.full.F.get(dimension, 0));
Austin Schuhb8b34be2024-07-14 16:06:19 -0700735 },
736 &result_py);
737
738 DefineVector2dFunction(
739 "mounting_location",
740 "Returns the mounting location of wheel in robot coordinates",
741 [](const Module &m, int dimension) {
742 return ccode(*m.mounting_location.get(dimension, 0));
743 },
744 &result_py);
745
746 DefineScalarFunction(
747 "Ms", "Returns the self aligning moment of the ith wheel",
748 [this](const Module &m) {
749 return ccode(*(div(m.Ms, add(Jsm_, div(div(Js_, Gs_), Gs_)))));
750 },
751 &result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700752
753 aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n"));
754 }
755
Austin Schuhb8b34be2024-07-14 16:06:19 -0700756 void DefineScalarFunction(
757 std::string_view name, std::string_view documentation,
758 std::function<std::string(const Module &)> scalar_fn,
759 std::vector<std::string> *result_py) {
760 result_py->emplace_back("");
761 result_py->emplace_back(absl::Substitute("# $0.", documentation));
762 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
763 WriteCasadiVariables(result_py);
764 for (size_t m = 0; m < kNumModules; ++m) {
765 if (m == 0) {
766 result_py->emplace_back(" if i == 0:");
767 } else {
768 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
769 }
770 result_py->emplace_back(
771 absl::Substitute(" return casadi.Function('$0', [X, U], [$1])",
772 name, scalar_fn(modules_[m])));
773 }
774 result_py->emplace_back(" raise ValueError(\"Invalid module number\")");
775 }
776
777 void DefineVector2dFunction(
778 std::string_view name, std::string_view documentation,
779 std::function<std::string(const Module &, int)> scalar_fn,
780 std::vector<std::string> *result_py) {
781 result_py->emplace_back("");
782 result_py->emplace_back(absl::Substitute("# $0.", documentation));
783 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
784 WriteCasadiVariables(result_py);
785 result_py->emplace_back(
786 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
787 for (size_t m = 0; m < kNumModules; ++m) {
788 if (m == 0) {
789 result_py->emplace_back(" if i == 0:");
790 } else {
791 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
792 }
793 for (int j = 0; j < 2; ++j) {
794 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
795 j, scalar_fn(modules_[m], j)));
796 }
797 }
798 result_py->emplace_back(" else:");
799 result_py->emplace_back(
800 " raise ValueError(\"Invalid module number\")");
801 result_py->emplace_back(absl::Substitute(
802 " return casadi.Function('$0', [X, U], [result])", name));
803 }
804
justinT21446e4f62024-06-16 22:36:10 -0700805 private:
806 static constexpr uint8_t kNumModules = 4;
807
Austin Schuh6ea789e2024-07-27 13:45:53 -0700808 RCP<const Basic> SteerAccel(RCP<const Basic> Fwx, RCP<const Basic> Ms,
809 RCP<const Basic> Is) {
810 RCP<const Basic> lhms =
811 mul(add(neg(wb_), mul(add(rs_, rp_), sub(integer(1), div(rb1_, rp_)))),
812 mul(div(rw_, rb2_), neg(Fwx)));
813 RCP<const Basic> lhs = add(add(Ms, div(mul(Kts_, Is), Gs_)), lhms);
814 RCP<const Basic> rhs = add(Jsm_, div(div(Js_, Gs_), Gs_));
815 return simplify(div(lhs, rhs));
816 }
817
justinT21446e4f62024-06-16 22:36:10 -0700818 Module ModulePhysics(const int m, DenseMatrix mounting_location) {
819 VLOG(1) << "Solving module " << m;
820
821 Module result;
Austin Schuhb8b34be2024-07-14 16:06:19 -0700822 result.mounting_location = mounting_location;
justinT21446e4f62024-06-16 22:36:10 -0700823
824 result.Is = symbol(absl::StrFormat("Is%u", m));
825 result.Id = symbol(absl::StrFormat("Id%u", m));
826
827 RCP<const Symbol> thetamd = symbol(absl::StrFormat("theta_md%u", m));
828 RCP<const Symbol> omegamd = symbol(absl::StrFormat("omega_md%u", m));
829 RCP<const Symbol> alphamd = symbol(absl::StrFormat("alpha_md%u", m));
830
831 result.thetas = symbol(absl::StrFormat("thetas%u", m));
832 result.omegas = symbol(absl::StrFormat("omegas%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -0700833 RCP<const Symbol> alphas = symbol(absl::StrFormat("alphas%u", m));
justinT21446e4f62024-06-16 22:36:10 -0700834
justinT21446e4f62024-06-16 22:36:10 -0700835 result.omegad = symbol(absl::StrFormat("omegad%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -0700836 RCP<const Symbol> alphad = symbol(absl::StrFormat("alphad%u", m));
justinT21446e4f62024-06-16 22:36:10 -0700837
838 // Velocity of the module in field coordinates
Austin Schuh2a1abec2024-07-10 20:31:16 -0700839 DenseMatrix robot_velocity = DenseMatrix(2, 1, {vx_, vy_});
justinT21446e4f62024-06-16 22:36:10 -0700840 VLOG(1) << "robot velocity: " << robot_velocity.__str__();
841
842 // Velocity of the contact patch in field coordinates
843 DenseMatrix temp_matrix = DenseMatrix(2, 1);
844 DenseMatrix temp_matrix2 = DenseMatrix(2, 1);
Austin Schuhbd75c482024-08-18 00:03:51 -0700845 DenseMatrix temp_matrix3 = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700846 result.contact_patch_velocity = DenseMatrix(2, 1);
justinT21446e4f62024-06-16 22:36:10 -0700847
Austin Schuhb8b34be2024-07-14 16:06:19 -0700848 mul_dense_dense(R(theta_), result.mounting_location, temp_matrix);
justinT21446e4f62024-06-16 22:36:10 -0700849 add_dense_dense(angle_cross(temp_matrix, omega_), robot_velocity,
850 temp_matrix2);
851 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuhbd75c482024-08-18 00:03:51 -0700852 DenseMatrix(2, 1, {neg(caster_), integer(0)}),
853 temp_matrix3);
justinT21446e4f62024-06-16 22:36:10 -0700854 add_dense_dense(temp_matrix2,
Austin Schuhbd75c482024-08-18 00:03:51 -0700855 angle_cross(temp_matrix3, add(omega_, result.omegas)),
Austin Schuh2a1abec2024-07-10 20:31:16 -0700856 result.contact_patch_velocity);
justinT21446e4f62024-06-16 22:36:10 -0700857
858 VLOG(1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700859 VLOG(1) << "contact patch velocity: "
860 << result.contact_patch_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -0700861
862 // Relative velocity of the surface of the wheel to the ground.
Austin Schuhb67a38f2024-07-04 13:48:38 -0700863 result.wheel_ground_velocity = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -0700864 mul_dense_dense(R(neg(add(result.thetas, theta_))),
865 result.contact_patch_velocity,
Austin Schuhb67a38f2024-07-04 13:48:38 -0700866 result.wheel_ground_velocity);
justinT21446e4f62024-06-16 22:36:10 -0700867
Austin Schuhb8b34be2024-07-14 16:06:19 -0700868 // Compute the relative velocity between the wheel surface and the ground in
869 // the wheel coordinate system.
870 result.wheel_slip_velocity = DenseMatrix(2, 1);
871 DenseMatrix wheel_velocity =
872 DenseMatrix(2, 1, {mul(rw_, result.omegad), integer(0)});
873 DenseMatrix negative_wheel_ground_velocity =
874 DenseMatrix(2, 1,
875 {neg(result.wheel_ground_velocity.get(0, 0)),
876 neg(result.wheel_ground_velocity.get(1, 0))});
877 add_dense_dense(negative_wheel_ground_velocity, wheel_velocity,
878 result.wheel_slip_velocity);
879
justinT21446e4f62024-06-16 22:36:10 -0700880 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700881 VLOG(1) << "wheel ground velocity: "
882 << result.wheel_ground_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -0700883
Austin Schuh5ddcb472024-07-21 17:55:34 -0700884 result.slip_angle = sin(neg(atan2(result.wheel_ground_velocity.get(1, 0),
885 result.wheel_ground_velocity.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -0700886
887 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700888 VLOG(1) << "slip angle: " << result.slip_angle->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700889
Austin Schuh2a1abec2024-07-10 20:31:16 -0700890 // TODO(austin): Does this handle decel properly?
Austin Schuhb67a38f2024-07-04 13:48:38 -0700891 result.slip_ratio = div(
Austin Schuh2a1abec2024-07-10 20:31:16 -0700892 sub(mul(rw_, result.omegad), result.wheel_ground_velocity.get(0, 0)),
893 SymEngine::max(
894 {real_double(0.02), abs(result.wheel_ground_velocity.get(0, 0))}));
justinT21446e4f62024-06-16 22:36:10 -0700895 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700896 VLOG(1) << "Slip ratio " << result.slip_ratio->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700897
Austin Schuh6ea789e2024-07-27 13:45:53 -0700898 result.full.Fwx = simplify(mul(Cx_, result.slip_ratio));
Austin Schuhb67a38f2024-07-04 13:48:38 -0700899 result.Fwy = simplify(mul(Cy_, result.slip_angle));
justinT21446e4f62024-06-16 22:36:10 -0700900
Austin Schuh27694fa2024-07-20 16:29:49 -0700901 // The self-aligning moment needs to flip when the module flips direction.
Austin Schuh78a1b312024-08-18 17:21:34 -0700902 RCP<const Basic> softsign_velocity = add(
903 div(integer(-2),
904 add(integer(1), exp(mul(integer(100),
905 result.wheel_ground_velocity.get(0, 0))))),
906 integer(1));
907 result.Ms =
908 mul(neg(result.Fwy),
909 add(div(mul(softsign_velocity, contact_patch_length_), integer(3)),
910 caster_));
justinT21446e4f62024-06-16 22:36:10 -0700911 VLOG(1);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700912 VLOG(1) << "Ms " << result.Ms->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700913 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700914 VLOG(1) << "full.Fwx " << result.full.Fwx->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700915 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700916 VLOG(1) << "Fwy " << result.Fwy->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700917
Austin Schuh6ea789e2024-07-27 13:45:53 -0700918 // -K_td * Id / Gd + Fwx * rw = 0
919 // Fwx = K_td * Id / Gd / rw
920 result.direct.Fwx = mul(Ktd_, div(result.Id, mul(Gd_, rw_)));
921
922 result.direct.alphas_eqn =
923 SteerAccel(result.direct.Fwx, result.Ms, result.Is);
924
925 // d/dt omegas = ...
926 result.full.alphas_eqn = SteerAccel(result.full.Fwx, result.Ms, result.Is);
justinT21446e4f62024-06-16 22:36:10 -0700927
928 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700929 VLOG(1) << alphas->__str__() << " = " << result.full.alphas_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700930
Austin Schuh6ea789e2024-07-27 13:45:53 -0700931 RCP<const Basic> lhs =
932 sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), alphas),
933 mul(Gd1_, mul(Gd2_, alphamd)));
934 RCP<const Basic> ddplanitary_eqn = sub(mul(Gd3_, lhs), alphad);
justinT21446e4f62024-06-16 22:36:10 -0700935
Austin Schuh6ea789e2024-07-27 13:45:53 -0700936 RCP<const Basic> full_drive_eqn =
937 sub(add(mul(neg(Jdm_), div(alphamd, Gd_)),
938 mul(Ktd_, div(neg(result.Id), Gd_))),
939 mul(neg(result.full.Fwx), rw_));
justinT21446e4f62024-06-16 22:36:10 -0700940
Austin Schuh6ea789e2024-07-27 13:45:53 -0700941 VLOG(1) << "full_drive_eqn: " << full_drive_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700942
943 // Substitute in ddplanitary_eqn so we get rid of alphamd
944 map_basic_basic map;
945 RCP<const Set> reals = interval(NegInf, Inf, true, true);
946 RCP<const Set> solve_solution = solve(ddplanitary_eqn, alphamd, reals);
947 map[alphamd] = solve_solution->get_args()[1]->get_args()[0];
948 VLOG(1) << "temp: " << solve_solution->__str__();
Austin Schuh6ea789e2024-07-27 13:45:53 -0700949 RCP<const Basic> drive_eqn_subs = full_drive_eqn->subs(map);
justinT21446e4f62024-06-16 22:36:10 -0700950
951 map.clear();
Austin Schuh6ea789e2024-07-27 13:45:53 -0700952 map[alphas] = result.full.alphas_eqn;
justinT21446e4f62024-06-16 22:36:10 -0700953 RCP<const Basic> drive_eqn_subs2 = drive_eqn_subs->subs(map);
954 RCP<const Basic> drive_eqn_subs3 = simplify(drive_eqn_subs2);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700955 VLOG(1) << "full_drive_eqn simplified: " << drive_eqn_subs3->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700956
Austin Schuh6ea789e2024-07-27 13:45:53 -0700957 solve_solution = solve(drive_eqn_subs3, alphad, reals);
justinT21446e4f62024-06-16 22:36:10 -0700958
Austin Schuh6ea789e2024-07-27 13:45:53 -0700959 result.full.alphad_eqn =
justinT21446e4f62024-06-16 22:36:10 -0700960 simplify(solve_solution->get_args()[1]->get_args()[0]);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700961 VLOG(1) << "drive_accel: " << result.full.alphad_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700962
Austin Schuh2a1abec2024-07-10 20:31:16 -0700963 // Compute the resulting force from the module.
Austin Schuh6ea789e2024-07-27 13:45:53 -0700964 result.full.F = DenseMatrix(2, 1);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700965 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuh6ea789e2024-07-27 13:45:53 -0700966 DenseMatrix(2, 1, {result.full.Fwx, result.Fwy}),
967 result.full.F);
968 result.full.torque = force_cross(result.mounting_location, result.full.F);
justinT21446e4f62024-06-16 22:36:10 -0700969
Austin Schuh6ea789e2024-07-27 13:45:53 -0700970 result.direct.F = DenseMatrix(2, 1);
971 mul_dense_dense(R(add(theta_, result.thetas)),
972 DenseMatrix(2, 1, {result.direct.Fwx, result.Fwy}),
973 result.direct.F);
974 result.direct.torque =
975 force_cross(result.mounting_location, result.direct.F);
justinT21446e4f62024-06-16 22:36:10 -0700976
977 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -0700978 VLOG(1) << "full torque = " << result.full.torque->__str__();
979 VLOG(1) << "direct torque = " << result.full.torque->__str__();
justinT21446e4f62024-06-16 22:36:10 -0700980
justinT21446e4f62024-06-16 22:36:10 -0700981 return result;
982 }
983
984 DenseMatrix R(const RCP<const Basic> theta) {
985 return DenseMatrix(2, 2,
986 {cos(theta), neg(sin(theta)), sin(theta), cos(theta)});
987 }
988
989 DenseMatrix angle_cross(DenseMatrix a, RCP<const Basic> b) {
Austin Schuh2a1abec2024-07-10 20:31:16 -0700990 return DenseMatrix(2, 1, {mul(neg(a.get(1, 0)), b), mul(a.get(0, 0), b)});
justinT21446e4f62024-06-16 22:36:10 -0700991 }
992
993 RCP<const Basic> force_cross(DenseMatrix r, DenseMatrix f) {
994 return sub(mul(r.get(0, 0), f.get(1, 0)), mul(r.get(1, 0), f.get(0, 0)));
995 }
996
997 // z represents the number of teeth per gear, theta is the angle between
998 // shafts(in degrees), D_02 is the pitch diameter of gear 2 and b_2 is the
999 // length of the tooth of gear 2
1000 // returns std::pair(r_01, r_02)
1001 std::pair<double, double> GetBevelPitchRadius(double z1, double z2,
1002 double theta, double D_02,
1003 double b_2) {
1004 double gamma_1 = std::atan2(z1, z2);
1005 double gamma_2 = theta / 180.0 * std::numbers::pi - gamma_1;
1006 double R_m = D_02 / 2 / std::sin(gamma_2) - b_2 / 2;
1007 return std::pair(R_m * std::cos(gamma_2), R_m * std::sin(gamma_2));
1008 }
1009
1010 Motor drive_motor_;
1011 Motor steer_motor_;
1012
1013 RCP<const Basic> Cx_;
1014 RCP<const Basic> Cy_;
Austin Schuh2a1abec2024-07-10 20:31:16 -07001015 RCP<const Basic> rw_;
justinT21446e4f62024-06-16 22:36:10 -07001016 RCP<const Basic> m_;
1017 RCP<const Basic> J_;
1018 RCP<const Basic> Gd1_;
1019 RCP<const Basic> rs_;
1020 RCP<const Basic> rp_;
1021 RCP<const Basic> Gd2_;
1022 RCP<const Basic> rb1_;
1023 RCP<const Basic> rb2_;
1024 RCP<const Basic> Gd3_;
1025 RCP<const Basic> Gd_;
1026 RCP<const Basic> Js_;
1027 RCP<const Basic> Gs_;
1028 RCP<const Basic> wb_;
1029 RCP<const Basic> Jdm_;
1030 RCP<const Basic> Jsm_;
1031 RCP<const Basic> Kts_;
1032 RCP<const Basic> Ktd_;
1033 RCP<const Basic> robot_width_;
1034 RCP<const Basic> caster_;
1035 RCP<const Basic> contact_patch_length_;
1036 RCP<const Basic> x_;
1037 RCP<const Basic> y_;
1038 RCP<const Basic> theta_;
1039 RCP<const Basic> vx_;
1040 RCP<const Basic> vy_;
1041 RCP<const Basic> omega_;
1042 RCP<const Basic> ax_;
1043 RCP<const Basic> ay_;
1044 RCP<const Basic> atheta_;
1045
1046 std::array<Module, kNumModules> modules_;
1047
Austin Schuh6ea789e2024-07-27 13:45:53 -07001048 DenseMatrix full_accel_;
1049 RCP<const Basic> full_angular_accel_;
1050 DenseMatrix direct_accel_;
1051 RCP<const Basic> direct_angular_accel_;
justinT21446e4f62024-06-16 22:36:10 -07001052};
1053
1054} // namespace frc971::control_loops::swerve
1055
1056int main(int argc, char **argv) {
1057 aos::InitGoogle(&argc, &argv);
1058
1059 frc971::control_loops::swerve::SwerveSimulation sim;
1060
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001061 if (!absl::GetFlag(FLAGS_cc_output_path).empty() &&
1062 !absl::GetFlag(FLAGS_h_output_path).empty()) {
1063 sim.Write(absl::GetFlag(FLAGS_cc_output_path),
1064 absl::GetFlag(FLAGS_h_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001065 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001066 if (!absl::GetFlag(FLAGS_casadi_py_output_path).empty()) {
1067 sim.WriteCasadi(absl::GetFlag(FLAGS_casadi_py_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001068 }
justinT21446e4f62024-06-16 22:36:10 -07001069
1070 return 0;
1071}