blob: af058f67e2175fafe07ec266f8ae2cb89a9127be [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.");
Austin Schuh5371c802024-09-02 13:18:48 -070038ABSL_FLAG(bool, function, true, "If true, make soft_atan2 a function.");
justinT21446e4f62024-06-16 22:36:10 -070039
justinT21942892b2024-07-02 22:33:50 -070040using SymEngine::abs;
justinT21446e4f62024-06-16 22:36:10 -070041using SymEngine::add;
42using SymEngine::atan2;
43using SymEngine::Basic;
44using SymEngine::ccode;
45using SymEngine::cos;
46using SymEngine::DenseMatrix;
47using SymEngine::div;
Austin Schuh78a1b312024-08-18 17:21:34 -070048using SymEngine::exp;
justinT21446e4f62024-06-16 22:36:10 -070049using SymEngine::Inf;
50using SymEngine::integer;
51using SymEngine::map_basic_basic;
52using SymEngine::minus_one;
53using SymEngine::neg;
54using SymEngine::NegInf;
55using SymEngine::pow;
56using SymEngine::RCP;
57using SymEngine::real_double;
58using SymEngine::RealDouble;
59using SymEngine::Set;
60using SymEngine::simplify;
61using SymEngine::sin;
62using SymEngine::solve;
63using SymEngine::symbol;
64using SymEngine::Symbol;
65
66namespace frc971::control_loops::swerve {
67
68// State per module.
69struct Module {
Austin Schuh6ea789e2024-07-27 13:45:53 -070070 DenseMatrix mounting_location;
71
justinT21446e4f62024-06-16 22:36:10 -070072 RCP<const Symbol> Is;
73
74 RCP<const Symbol> Id;
75
76 RCP<const Symbol> thetas;
77 RCP<const Symbol> omegas;
justinT21446e4f62024-06-16 22:36:10 -070078
justinT21446e4f62024-06-16 22:36:10 -070079 RCP<const Symbol> omegad;
justinT21446e4f62024-06-16 22:36:10 -070080
Austin Schuh2a1abec2024-07-10 20:31:16 -070081 DenseMatrix contact_patch_velocity;
Austin Schuhb67a38f2024-07-04 13:48:38 -070082 DenseMatrix wheel_ground_velocity;
Austin Schuhb8b34be2024-07-14 16:06:19 -070083 DenseMatrix wheel_slip_velocity;
Austin Schuhb67a38f2024-07-04 13:48:38 -070084 RCP<const Basic> slip_angle;
85 RCP<const Basic> slip_ratio;
86
Austin Schuhb8b34be2024-07-14 16:06:19 -070087 RCP<const Basic> Ms;
Austin Schuhb67a38f2024-07-04 13:48:38 -070088 RCP<const Basic> Fwy;
89
Austin Schuh6ea789e2024-07-27 13:45:53 -070090 struct Full {
91 RCP<const Basic> Fwx;
92 DenseMatrix F;
93
94 RCP<const Basic> torque;
95
96 RCP<const Basic> alphas_eqn;
97 RCP<const Basic> alphad_eqn;
98 } full;
99
100 struct Direct {
101 RCP<const Basic> Fwx;
102 DenseMatrix F;
103
104 RCP<const Basic> torque;
105
106 RCP<const Basic> alphas_eqn;
107 } direct;
justinT21446e4f62024-06-16 22:36:10 -0700108};
109
Austin Schuh6ea789e2024-07-27 13:45:53 -0700110DenseMatrix SumMatrices(DenseMatrix a) { return a; }
111
112template <typename... Args>
113DenseMatrix SumMatrices(DenseMatrix a, Args... args) {
114 DenseMatrix result = DenseMatrix(2, 1, {integer(0), integer(0)});
115
116 DenseMatrix b = SumMatrices(args...);
117 add_dense_dense(a, b, result);
118 return result;
119}
120
justinT21446e4f62024-06-16 22:36:10 -0700121class SwerveSimulation {
122 public:
123 SwerveSimulation() : drive_motor_(KrakenFOC()), steer_motor_(KrakenFOC()) {
124 auto fx = symbol("fx");
125 auto fy = symbol("fy");
126 auto moment = symbol("moment");
127
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700128 if (absl::GetFlag(FLAGS_symbolic)) {
justinT21446e4f62024-06-16 22:36:10 -0700129 Cx_ = symbol("Cx");
130 Cy_ = symbol("Cy");
131
Austin Schuh2a1abec2024-07-10 20:31:16 -0700132 rw_ = symbol("rw");
justinT21446e4f62024-06-16 22:36:10 -0700133
134 m_ = symbol("m");
135 J_ = symbol("J");
136
137 Gd1_ = symbol("Gd1");
138 rs_ = symbol("rs");
139 rp_ = symbol("rp");
140 Gd2_ = symbol("Gd2");
141
142 rb1_ = symbol("rb1");
143 rb2_ = symbol("rb2");
144
Austin Schuh76534f32024-09-02 13:52:45 -0700145 Gd3_ = symbol("Gd3");
justinT21446e4f62024-06-16 22:36:10 -0700146 Gd_ = symbol("Gd");
147
148 Js_ = symbol("Js");
149
150 Gs_ = symbol("Gs");
151 wb_ = symbol("wb");
152
153 Jdm_ = symbol("Jdm");
154 Jsm_ = symbol("Jsm");
155 Kts_ = symbol("Kts");
156 Ktd_ = symbol("Ktd");
157
158 robot_width_ = symbol("robot_width");
159
160 caster_ = symbol("caster");
161 contact_patch_length_ = symbol("Lcp");
162 } else {
Austin Schuhb8b34be2024-07-14 16:06:19 -0700163 Cx_ = real_double(25.0 * 9.8 / 4.0 / 0.05);
justinT21446e4f62024-06-16 22:36:10 -0700164 Cy_ = real_double(5 * 9.8 / 0.05 / 4.0);
165
Austin Schuh2a1abec2024-07-10 20:31:16 -0700166 rw_ = real_double(2 * 0.0254);
justinT21446e4f62024-06-16 22:36:10 -0700167
168 m_ = real_double(25.0); // base is 20 kg without battery
169 J_ = real_double(6.0);
170
171 Gd1_ = real_double(12.0 / 42.0);
172 rs_ = real_double(28.0 / 20.0 / 2.0);
173 rp_ = real_double(18.0 / 20.0 / 2.0);
174 Gd2_ = div(rs_, rp_);
175
176 // 15 / 45 bevel ratio, calculated using python script ported over to
177 // GetBevelPitchRadius(double
178 // TODO(Justin): Use the function instead of computed constantss
179 rb1_ = real_double(0.3805473);
180 rb2_ = real_double(1.14164);
181
182 Gd3_ = div(rb1_, rb2_);
183 Gd_ = mul(mul(Gd1_, Gd2_), Gd3_);
184
Austin Schuhb8b34be2024-07-14 16:06:19 -0700185 Js_ = real_double(0.001);
justinT21446e4f62024-06-16 22:36:10 -0700186
187 Gs_ = real_double(35.0 / 468.0);
188 wb_ = real_double(0.725);
189
190 Jdm_ = real_double(drive_motor_.motor_inertia);
191 Jsm_ = real_double(steer_motor_.motor_inertia);
192 Kts_ = real_double(steer_motor_.Kt);
193 Ktd_ = real_double(drive_motor_.Kt);
194
195 robot_width_ = real_double(24.75 * 0.0254);
196
Austin Schuh27694fa2024-07-20 16:29:49 -0700197 caster_ = real_double(absl::GetFlag(FLAGS_caster));
justinT21446e4f62024-06-16 22:36:10 -0700198 contact_patch_length_ = real_double(0.02);
199 }
200
201 x_ = symbol("x");
202 y_ = symbol("y");
203 theta_ = symbol("theta");
204
205 vx_ = symbol("vx");
206 vy_ = symbol("vy");
207 omega_ = symbol("omega");
208
209 ax_ = symbol("ax");
210 ay_ = symbol("ay");
211 atheta_ = symbol("atheta");
212
213 // Now, compute the accelerations due to the disturbance forces.
justinT21446e4f62024-06-16 22:36:10 -0700214 DenseMatrix external_accel = DenseMatrix(2, 1, {div(fx, m_), div(fy, m_)});
Austin Schuh6ea789e2024-07-27 13:45:53 -0700215 DenseMatrix external_force = DenseMatrix(2, 1, {fx, fy});
justinT21446e4f62024-06-16 22:36:10 -0700216
217 // And compute the physics contributions from each module.
218 modules_[0] = ModulePhysics(
219 0, DenseMatrix(
220 2, 1,
221 {div(robot_width_, integer(2)), div(robot_width_, integer(2))}));
222 modules_[1] =
223 ModulePhysics(1, DenseMatrix(2, 1,
224 {div(robot_width_, integer(-2)),
225 div(robot_width_, integer(2))}));
226 modules_[2] =
227 ModulePhysics(2, DenseMatrix(2, 1,
228 {div(robot_width_, integer(-2)),
229 div(robot_width_, integer(-2))}));
230 modules_[3] =
231 ModulePhysics(3, DenseMatrix(2, 1,
232 {div(robot_width_, integer(2)),
233 div(robot_width_, integer(-2))}));
234
235 // And convert them into the overall robot contribution.
Austin Schuh6ea789e2024-07-27 13:45:53 -0700236 DenseMatrix net_full_force =
237 SumMatrices(modules_[0].full.F, modules_[1].full.F, modules_[2].full.F,
238 modules_[3].full.F, external_force);
justinT21446e4f62024-06-16 22:36:10 -0700239
Austin Schuh6ea789e2024-07-27 13:45:53 -0700240 DenseMatrix net_direct_force =
241 SumMatrices(modules_[0].direct.F, modules_[1].direct.F,
242 modules_[2].direct.F, modules_[3].direct.F, external_force);
justinT21446e4f62024-06-16 22:36:10 -0700243
Austin Schuh6ea789e2024-07-27 13:45:53 -0700244 full_accel_ = DenseMatrix(2, 1);
245 mul_dense_scalar(net_full_force, pow(m_, minus_one), full_accel_);
justinT21446e4f62024-06-16 22:36:10 -0700246
Austin Schuh6ea789e2024-07-27 13:45:53 -0700247 full_angular_accel_ = div(
248 add(moment, add(add(modules_[0].full.torque, modules_[1].full.torque),
249 add(modules_[2].full.torque, modules_[3].full.torque))),
250 J_);
justinT21446e4f62024-06-16 22:36:10 -0700251
Austin Schuh6ea789e2024-07-27 13:45:53 -0700252 direct_accel_ = DenseMatrix(2, 1);
253 mul_dense_scalar(net_direct_force, pow(m_, minus_one), direct_accel_);
justinT21942892b2024-07-02 22:33:50 -0700254
Austin Schuh6ea789e2024-07-27 13:45:53 -0700255 direct_angular_accel_ =
256 div(add(moment,
257 add(add(modules_[0].direct.torque, modules_[1].direct.torque),
258 add(modules_[2].direct.torque, modules_[3].direct.torque))),
259 J_);
justinT21942892b2024-07-02 22:33:50 -0700260
Austin Schuh6ea789e2024-07-27 13:45:53 -0700261 VLOG(1) << "accel(0, 0) = " << ccode(*full_accel_.get(0, 0));
262 VLOG(1) << "accel(1, 0) = " << ccode(*full_accel_.get(1, 0));
263 VLOG(1) << "angular_accel = " << ccode(*full_angular_accel_);
justinT21942892b2024-07-02 22:33:50 -0700264 }
265
justinT21446e4f62024-06-16 22:36:10 -0700266 // Writes the physics out to the provided .cc and .h path.
267 void Write(std::string_view cc_path, std::string_view h_path) {
268 std::vector<std::string> result_cc;
269 std::vector<std::string> result_h;
270
Austin Schuh0f881092024-06-28 15:36:48 -0700271 std::string_view include_guard_stripped = h_path;
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700272 CHECK(absl::ConsumePrefix(&include_guard_stripped,
273 absl::GetFlag(FLAGS_output_base)));
justinT21446e4f62024-06-16 22:36:10 -0700274 std::string include_guard =
275 absl::StrReplaceAll(absl::AsciiStrToUpper(include_guard_stripped),
276 {{"/", "_"}, {".", "_"}});
277
278 // Write out the header.
279 result_h.emplace_back(absl::Substitute("#ifndef $0_", include_guard));
280 result_h.emplace_back(absl::Substitute("#define $0_", include_guard));
281 result_h.emplace_back("");
282 result_h.emplace_back("#include <Eigen/Dense>");
283 result_h.emplace_back("");
284 result_h.emplace_back("namespace frc971::control_loops::swerve {");
285 result_h.emplace_back("");
James Kuszmaulef14ab42024-09-14 15:05:24 -0700286 result_h.emplace_back("struct FullDynamicsStates {");
287 result_h.emplace_back("enum States {");
288 result_h.emplace_back(" kThetas0 = 0,");
289 result_h.emplace_back(" kThetad0 = 1,");
290 result_h.emplace_back(" kOmegas0 = 2,");
291 result_h.emplace_back(" kOmegad0 = 3,");
292 result_h.emplace_back(" kThetas1 = 4,");
293 result_h.emplace_back(" kThetad1 = 5,");
294 result_h.emplace_back(" kOmegas1 = 6,");
295 result_h.emplace_back(" kOmegad1 = 7,");
296 result_h.emplace_back(" kThetas2 = 8,");
297 result_h.emplace_back(" kThetad2 = 9,");
298 result_h.emplace_back(" kOmegas2 = 10,");
299 result_h.emplace_back(" kOmegad2 = 11,");
300 result_h.emplace_back(" kThetas3 = 12,");
301 result_h.emplace_back(" kThetad3 = 13,");
302 result_h.emplace_back(" kOmegas3 = 14,");
303 result_h.emplace_back(" kOmegad3 = 15,");
304 result_h.emplace_back(" kX = 16,");
305 result_h.emplace_back(" kY = 17,");
306 result_h.emplace_back(" kTheta = 18,");
307 result_h.emplace_back(" kVx = 19,");
308 result_h.emplace_back(" kVy = 20,");
309 result_h.emplace_back(" kOmega = 21,");
310 result_h.emplace_back(" kFx = 22,");
311 result_h.emplace_back(" kFy = 23,");
312 result_h.emplace_back(" kMoment = 24,");
313 result_h.emplace_back(" kNumStates");
314 result_h.emplace_back("};");
315 result_h.emplace_back("};");
316 result_h.emplace_back(
317 "inline constexpr size_t kNumFullDynamicsStates = "
318 "static_cast<size_t>(FullDynamicsStates::kNumStates);");
319 result_h.emplace_back("struct VelocityStates {");
320 result_h.emplace_back("enum States {");
321 result_h.emplace_back(" kThetas0 = 0,");
322 result_h.emplace_back(" kOmegas0 = 1,");
323 result_h.emplace_back(" kThetas1 = 2,");
324 result_h.emplace_back(" kOmegas1 = 3,");
325 result_h.emplace_back(" kThetas2 = 4,");
326 result_h.emplace_back(" kOmegas2 = 5,");
327 result_h.emplace_back(" kThetas3 = 6,");
328 result_h.emplace_back(" kOmegas3 = 7,");
329 result_h.emplace_back(" kTheta = 8,");
330 result_h.emplace_back(" kVx = 9,");
331 result_h.emplace_back(" kVy = 10,");
332 result_h.emplace_back(" kOmega = 11,");
333 result_h.emplace_back(" kNumStates");
334 result_h.emplace_back("};");
335 result_h.emplace_back("};");
336 result_h.emplace_back(
337 "inline constexpr size_t kNumVelocityStates = "
338 "static_cast<size_t>(VelocityStates::kNumStates);");
339 result_h.emplace_back("struct Inputs {");
340 result_h.emplace_back("enum States {");
341 result_h.emplace_back(" kIs0 = 0,");
342 result_h.emplace_back(" kId0 = 1,");
343 result_h.emplace_back(" kIs1 = 2,");
344 result_h.emplace_back(" kId1 = 3,");
345 result_h.emplace_back(" kIs2 = 4,");
346 result_h.emplace_back(" kId2 = 5,");
347 result_h.emplace_back(" kIs3 = 6,");
348 result_h.emplace_back(" kId3 = 7,");
349 result_h.emplace_back(" kNumInputs = 8");
350 result_h.emplace_back("};");
351 result_h.emplace_back("};");
352 result_h.emplace_back(
353 "inline constexpr size_t kNumInputs = "
354 "static_cast<size_t>(Inputs::kNumInputs);");
355 result_h.emplace_back("");
justinT21446e4f62024-06-16 22:36:10 -0700356 result_h.emplace_back("// Returns the derivative of our state vector");
justinT21446e4f62024-06-16 22:36:10 -0700357 result_h.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700358 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> SwervePhysics(");
justinT21446e4f62024-06-16 22:36:10 -0700359 result_h.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700360 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
361 "1>> X,");
362 result_h.emplace_back(
363 " Eigen::Ref<const Eigen::Matrix<double, kNumInputs, 1>> U);");
364 result_h.emplace_back("");
365 result_h.emplace_back(
366 "Eigen::Matrix<double, kNumVelocityStates, 1> ToVelocityState(");
367 result_h.emplace_back(
368 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
369 "1>> X);");
370 result_h.emplace_back("");
371 result_h.emplace_back(
372 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> FromVelocityState(");
373 result_h.emplace_back(
374 " Eigen::Ref<const Eigen::Matrix<double, kNumVelocityStates, 1>> "
375 "X);");
376 result_h.emplace_back("");
377 result_h.emplace_back(
378 "inline Eigen::Matrix<double, kNumVelocityStates, 1> VelocityPhysics(");
379 result_h.emplace_back(
380 " Eigen::Ref<const Eigen::Matrix<double, kNumVelocityStates, 1>> "
381 "X,");
382 result_h.emplace_back(
383 " Eigen::Ref<const Eigen::Matrix<double, kNumInputs, 1>> U) {");
384 result_h.emplace_back(
385 " return ToVelocityState(SwervePhysics(FromVelocityState(X), U));");
386 result_h.emplace_back("}");
justinT21446e4f62024-06-16 22:36:10 -0700387 result_h.emplace_back("");
388 result_h.emplace_back("} // namespace frc971::control_loops::swerve");
389 result_h.emplace_back("");
390 result_h.emplace_back(absl::Substitute("#endif // $0_", include_guard));
391
392 // Write out the .cc
393 result_cc.emplace_back(
394 absl::Substitute("#include \"$0\"", include_guard_stripped));
395 result_cc.emplace_back("");
396 result_cc.emplace_back("#include <cmath>");
397 result_cc.emplace_back("");
398 result_cc.emplace_back("namespace frc971::control_loops::swerve {");
399 result_cc.emplace_back("");
justinT21446e4f62024-06-16 22:36:10 -0700400 result_cc.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700401 "Eigen::Matrix<double, kNumVelocityStates, 1> ToVelocityState(");
justinT21446e4f62024-06-16 22:36:10 -0700402 result_cc.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700403 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
404 "1>> X) {");
405 result_cc.emplace_back(
406 " Eigen::Matrix<double, kNumVelocityStates, 1> velocity;");
407 const std::vector<std::string_view> velocity_states = {
408 "kThetas0", "kOmegas0", "kThetas1", "kOmegas1", "kThetas2", "kOmegas2",
409 "kThetas3", "kOmegas3", "kTheta", "kVx", "kVy", "kOmega"};
410 for (const std::string_view velocity_state : velocity_states) {
411 result_cc.emplace_back(absl::StrFormat(
412 " velocity(VelocityStates::%s) = X(FullDynamicsStates::%s);",
413 velocity_state, velocity_state));
414 }
415 result_cc.emplace_back(" return velocity;");
416 result_cc.emplace_back("}");
417 result_cc.emplace_back("");
418 result_cc.emplace_back(
419 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> FromVelocityState(");
420 result_cc.emplace_back(
421 " Eigen::Ref<const Eigen::Matrix<double, kNumVelocityStates, 1>> X) "
422 "{");
423 result_cc.emplace_back(
424 " Eigen::Matrix<double, kNumFullDynamicsStates, 1> full;");
425 result_cc.emplace_back(" full.setZero();");
426 for (const std::string_view velocity_state : velocity_states) {
427 result_cc.emplace_back(absl::StrFormat(
428 " full(FullDynamicsStates::%s) = X(VelocityStates::%s);",
429 velocity_state, velocity_state));
430 }
431 result_cc.emplace_back(" return full;");
432 result_cc.emplace_back("}");
433 result_cc.emplace_back("");
434 result_cc.emplace_back(
435 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> SwervePhysics(");
436 result_cc.emplace_back(
437 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
438 "1>> X,");
439 result_cc.emplace_back(
440 " Eigen::Ref<const Eigen::Matrix<double, kNumInputs, 1>> U) {");
441 result_cc.emplace_back(
442 " Eigen::Matrix<double, kNumFullDynamicsStates, 1> result;");
justinT21446e4f62024-06-16 22:36:10 -0700443
444 // Start by writing out variables matching each of the symbol names we use
445 // so we don't have to modify the computed equations too much.
446 for (size_t m = 0; m < kNumModules; ++m) {
447 result_cc.emplace_back(
448 absl::Substitute(" const double thetas$0 = X($1, 0);", m, m * 4));
449 result_cc.emplace_back(absl::Substitute(
450 " const double omegas$0 = X($1, 0);", m, m * 4 + 2));
451 result_cc.emplace_back(absl::Substitute(
452 " const double omegad$0 = X($1, 0);", m, m * 4 + 3));
453 }
454
455 result_cc.emplace_back(absl::Substitute(" const double theta = X($0, 0);",
456 kNumModules * 4 + 2));
457 result_cc.emplace_back(
458 absl::Substitute(" const double vx = X($0, 0);", kNumModules * 4 + 3));
459 result_cc.emplace_back(
460 absl::Substitute(" const double vy = X($0, 0);", kNumModules * 4 + 4));
461 result_cc.emplace_back(absl::Substitute(" const double omega = X($0, 0);",
462 kNumModules * 4 + 5));
463
464 result_cc.emplace_back(
465 absl::Substitute(" const double fx = X($0, 0);", kNumModules * 4 + 6));
466 result_cc.emplace_back(
467 absl::Substitute(" const double fy = X($0, 0);", kNumModules * 4 + 7));
468 result_cc.emplace_back(absl::Substitute(" const double moment = X($0, 0);",
469 kNumModules * 4 + 8));
470
471 // Now do the same for the inputs.
472 for (size_t m = 0; m < kNumModules; ++m) {
473 result_cc.emplace_back(
474 absl::Substitute(" const double Is$0 = U($1, 0);", m, m * 2));
475 result_cc.emplace_back(
476 absl::Substitute(" const double Id$0 = U($1, 0);", m, m * 2 + 1));
477 }
478
479 result_cc.emplace_back("");
480
481 // And then write out the derivative of each state.
482 for (size_t m = 0; m < kNumModules; ++m) {
483 result_cc.emplace_back(
484 absl::Substitute(" result($0, 0) = omegas$1;", m * 4, m));
485 result_cc.emplace_back(
486 absl::Substitute(" result($0, 0) = omegad$1;", m * 4 + 1, m));
487
Austin Schuh6ea789e2024-07-27 13:45:53 -0700488 result_cc.emplace_back(
489 absl::Substitute(" result($0, 0) = $1;", m * 4 + 2,
490 ccode(*modules_[m].full.alphas_eqn)));
491 result_cc.emplace_back(
492 absl::Substitute(" result($0, 0) = $1;", m * 4 + 3,
493 ccode(*modules_[m].full.alphad_eqn)));
justinT21446e4f62024-06-16 22:36:10 -0700494 }
495
496 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700497 absl::Substitute(" result($0, 0) = vx;", kNumModules * 4 + 0));
justinT21446e4f62024-06-16 22:36:10 -0700498 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700499 absl::Substitute(" result($0, 0) = vy;", kNumModules * 4 + 1));
justinT21446e4f62024-06-16 22:36:10 -0700500 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700501 absl::Substitute(" result($0, 0) = omega;", kNumModules * 4 + 2));
justinT21446e4f62024-06-16 22:36:10 -0700502
justinT21446e4f62024-06-16 22:36:10 -0700503 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700504 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700505 ccode(*full_accel_.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -0700506 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700507 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700508 ccode(*full_accel_.get(1, 0))));
509 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
510 kNumModules * 4 + 5,
511 ccode(*full_angular_accel_)));
justinT21446e4f62024-06-16 22:36:10 -0700512
513 result_cc.emplace_back(
514 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 6));
515 result_cc.emplace_back(
516 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 7));
517 result_cc.emplace_back(
518 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 8));
519
520 result_cc.emplace_back("");
521 result_cc.emplace_back(" return result;");
522 result_cc.emplace_back("}");
523 result_cc.emplace_back("");
524 result_cc.emplace_back("} // namespace frc971::control_loops::swerve");
525
526 aos::util::WriteStringToFileOrDie(cc_path, absl::StrJoin(result_cc, "\n"));
527 aos::util::WriteStringToFileOrDie(h_path, absl::StrJoin(result_h, "\n"));
528 }
529
Austin Schuhb67a38f2024-07-04 13:48:38 -0700530 void WriteCasadiVariables(std::vector<std::string> *result_py) {
531 result_py->emplace_back(" sin = casadi.sin");
532 result_py->emplace_back(" cos = casadi.cos");
Austin Schuh78a1b312024-08-18 17:21:34 -0700533 result_py->emplace_back(" exp = casadi.exp");
Austin Schuh5371c802024-09-02 13:18:48 -0700534 if (absl::GetFlag(FLAGS_function)) {
535 result_py->emplace_back(" atan2 = soft_atan2()");
536 } else {
537 result_py->emplace_back(" atan2 = soft_atan2");
538 }
Austin Schuh2a1abec2024-07-10 20:31:16 -0700539 result_py->emplace_back(" fmax = casadi.fmax");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700540 result_py->emplace_back(" fabs = casadi.fabs");
541
542 // Start by writing out variables matching each of the symbol names we use
543 // so we don't have to modify the computed equations too much.
544 for (size_t m = 0; m < kNumModules; ++m) {
545 result_py->emplace_back(
546 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 4));
547 result_py->emplace_back(
548 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 4 + 2));
549 result_py->emplace_back(
550 absl::Substitute(" omegad$0 = X[$1, 0]", m, m * 4 + 3));
551 }
552
553 result_py->emplace_back(
554 absl::Substitute(" theta = X[$0, 0]", kNumModules * 4 + 2));
555 result_py->emplace_back(
556 absl::Substitute(" vx = X[$0, 0]", kNumModules * 4 + 3));
557 result_py->emplace_back(
558 absl::Substitute(" vy = X[$0, 0]", kNumModules * 4 + 4));
559 result_py->emplace_back(
560 absl::Substitute(" omega = X[$0, 0]", kNumModules * 4 + 5));
561
562 result_py->emplace_back(
563 absl::Substitute(" fx = X[$0, 0]", kNumModules * 4 + 6));
564 result_py->emplace_back(
565 absl::Substitute(" fy = X[$0, 0]", kNumModules * 4 + 7));
566 result_py->emplace_back(
567 absl::Substitute(" moment = X[$0, 0]", kNumModules * 4 + 8));
568
569 // Now do the same for the inputs.
570 for (size_t m = 0; m < kNumModules; ++m) {
571 result_py->emplace_back(
572 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
573 result_py->emplace_back(
574 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
575 }
576 }
577
Austin Schuh6ea789e2024-07-27 13:45:53 -0700578 void WriteCasadiVelocityVariables(std::vector<std::string> *result_py) {
579 result_py->emplace_back(" sin = casadi.sin");
Austin Schuh78a1b312024-08-18 17:21:34 -0700580 result_py->emplace_back(" exp = casadi.exp");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700581 result_py->emplace_back(" cos = casadi.cos");
Austin Schuh5371c802024-09-02 13:18:48 -0700582 if (absl::GetFlag(FLAGS_function)) {
583 result_py->emplace_back(" atan2 = soft_atan2()");
584 } else {
585 result_py->emplace_back(" atan2 = soft_atan2");
586 }
Austin Schuh6ea789e2024-07-27 13:45:53 -0700587 result_py->emplace_back(" fmax = casadi.fmax");
588 result_py->emplace_back(" fabs = casadi.fabs");
589
590 // Start by writing out variables matching each of the symbol names we use
591 // so we don't have to modify the computed equations too much.
592 for (size_t m = 0; m < kNumModules; ++m) {
593 result_py->emplace_back(
594 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 2 + 0));
595 result_py->emplace_back(
596 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 2 + 1));
597 }
598
599 result_py->emplace_back(
600 absl::Substitute(" theta = X[$0, 0]", kNumModules * 2 + 0));
601 result_py->emplace_back(
602 absl::Substitute(" vx = X[$0, 0]", kNumModules * 2 + 1));
603 result_py->emplace_back(
604 absl::Substitute(" vy = X[$0, 0]", kNumModules * 2 + 2));
605 result_py->emplace_back(
606 absl::Substitute(" omega = X[$0, 0]", kNumModules * 2 + 3));
607
608 // result_py->emplace_back(
609 // absl::Substitute(" fx = X[$0, 0]", kNumModules * 3 + 4));
610 // result_py->emplace_back(
611 // absl::Substitute(" fy = X[$0, 0]", kNumModules * 3 + 5));
612 // result_py->emplace_back(
613 // absl::Substitute(" moment = X[$0, 0]", kNumModules * 3 + 6));
614 //
615 result_py->emplace_back(" fx = 0");
616 result_py->emplace_back(" fy = 0");
617 result_py->emplace_back(" moment = 0");
618
619 // Now do the same for the inputs.
620 for (size_t m = 0; m < kNumModules; ++m) {
621 result_py->emplace_back(
622 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
623 result_py->emplace_back(
624 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
625 }
626 }
627
Austin Schuh0f881092024-06-28 15:36:48 -0700628 // Writes the physics out to the provided .cc and .h path.
629 void WriteCasadi(std::string_view py_path) {
630 std::vector<std::string> result_py;
631
632 // Write out the header.
Austin Schuh5371c802024-09-02 13:18:48 -0700633 result_py.emplace_back("#!/usr/bin/env python3");
Austin Schuh0f881092024-06-28 15:36:48 -0700634 result_py.emplace_back("");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700635 result_py.emplace_back("import casadi, numpy");
Austin Schuh0f881092024-06-28 15:36:48 -0700636 result_py.emplace_back("");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700637 result_py.emplace_back(absl::Substitute("WHEEL_RADIUS = $0", ccode(*rw_)));
638 result_py.emplace_back(
639 absl::Substitute("ROBOT_WIDTH = $0", ccode(*robot_width_)));
640 result_py.emplace_back(absl::Substitute("CASTER = $0", ccode(*caster_)));
Austin Schuh6ea789e2024-07-27 13:45:53 -0700641 result_py.emplace_back("STATE_THETAS0 = 0");
642 result_py.emplace_back("STATE_THETAD0 = 1");
643 result_py.emplace_back("STATE_OMEGAS0 = 2");
644 result_py.emplace_back("STATE_OMEGAD0 = 3");
645 result_py.emplace_back("STATE_THETAS1 = 4");
646 result_py.emplace_back("STATE_THETAD1 = 5");
647 result_py.emplace_back("STATE_OMEGAS1 = 6");
648 result_py.emplace_back("STATE_OMEGAD1 = 7");
649 result_py.emplace_back("STATE_THETAS2 = 8");
650 result_py.emplace_back("STATE_THETAD2 = 9");
651 result_py.emplace_back("STATE_OMEGAS2 = 10");
652 result_py.emplace_back("STATE_OMEGAD2 = 11");
653 result_py.emplace_back("STATE_THETAS3 = 12");
654 result_py.emplace_back("STATE_THETAD3 = 13");
655 result_py.emplace_back("STATE_OMEGAS3 = 14");
656 result_py.emplace_back("STATE_OMEGAD3 = 15");
657 result_py.emplace_back("STATE_X = 16");
658 result_py.emplace_back("STATE_Y = 17");
659 result_py.emplace_back("STATE_THETA = 18");
660 result_py.emplace_back("STATE_VX = 19");
661 result_py.emplace_back("STATE_VY = 20");
662 result_py.emplace_back("STATE_OMEGA = 21");
663 result_py.emplace_back("STATE_FX = 22");
664 result_py.emplace_back("STATE_FY = 23");
665 result_py.emplace_back("STATE_MOMENT = 24");
666 result_py.emplace_back("NUM_STATES = 25");
667 result_py.emplace_back("");
668 result_py.emplace_back("VELOCITY_STATE_THETAS0 = 0");
669 result_py.emplace_back("VELOCITY_STATE_OMEGAS0 = 1");
670 result_py.emplace_back("VELOCITY_STATE_THETAS1 = 2");
671 result_py.emplace_back("VELOCITY_STATE_OMEGAS1 = 3");
672 result_py.emplace_back("VELOCITY_STATE_THETAS2 = 4");
673 result_py.emplace_back("VELOCITY_STATE_OMEGAS2 = 5");
674 result_py.emplace_back("VELOCITY_STATE_THETAS3 = 6");
675 result_py.emplace_back("VELOCITY_STATE_OMEGAS3 = 7");
676 result_py.emplace_back("VELOCITY_STATE_THETA = 8");
677 result_py.emplace_back("VELOCITY_STATE_VX = 9");
678 result_py.emplace_back("VELOCITY_STATE_VY = 10");
679 result_py.emplace_back("VELOCITY_STATE_OMEGA = 11");
680 // result_py.emplace_back("VELOCITY_STATE_FX = 16");
681 // result_py.emplace_back("VELOCITY_STATE_FY = 17");
682 // result_py.emplace_back("VELOCITY_STATE_MOMENT = 18");
683 result_py.emplace_back("NUM_VELOCITY_STATES = 12");
684 result_py.emplace_back("");
685 result_py.emplace_back("def to_velocity_state(X):");
686 result_py.emplace_back(" return numpy.array([");
687 result_py.emplace_back(" [X[STATE_THETAS0, 0]],");
688 result_py.emplace_back(" [X[STATE_OMEGAS0, 0]],");
689 result_py.emplace_back(" [X[STATE_THETAS1, 0]],");
690 result_py.emplace_back(" [X[STATE_OMEGAS1, 0]],");
691 result_py.emplace_back(" [X[STATE_THETAS2, 0]],");
692 result_py.emplace_back(" [X[STATE_OMEGAS2, 0]],");
693 result_py.emplace_back(" [X[STATE_THETAS3, 0]],");
694 result_py.emplace_back(" [X[STATE_OMEGAS3, 0]],");
695 result_py.emplace_back(" [X[STATE_THETA, 0]],");
696 result_py.emplace_back(" [X[STATE_VX, 0]],");
697 result_py.emplace_back(" [X[STATE_VY, 0]],");
698 result_py.emplace_back(" [X[STATE_OMEGA, 0]],");
699 // result_py.emplace_back(" [X[STATE_FX, 0]],");
700 // result_py.emplace_back(" [X[STATE_FY, 0]],");
701 // result_py.emplace_back(" [X[STATE_MOMENT, 0]],");
702 result_py.emplace_back(" ])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700703 result_py.emplace_back("");
Austin Schuhbd75c482024-08-18 00:03:51 -0700704 constexpr double kLogGain = 1.0 / 0.05;
Austin Schuh5371c802024-09-02 13:18:48 -0700705 constexpr double kAbsGain = 1.0 / 0.01;
706 if (absl::GetFlag(FLAGS_function)) {
707 result_py.emplace_back("def soft_atan2():");
708 result_py.emplace_back(" y = casadi.SX.sym('y')");
709 result_py.emplace_back(" x = casadi.SX.sym('x')");
710 result_py.emplace_back(
711 " return casadi.Function('soft_atan2', [y, x], [");
712 result_py.emplace_back(" casadi.arctan2(");
713 result_py.emplace_back(" y,");
714 result_py.emplace_back(" casadi.logsumexp(");
715 result_py.emplace_back(" casadi.SX(");
716 result_py.emplace_back(" numpy.array([");
717 result_py.emplace_back(" 1.0, x * (1.0 - 2.0 /");
718 result_py.emplace_back(
719 absl::Substitute(" (1 + "
720 "casadi.exp($1.0 * x))) * $0.0",
721 kLogGain, kAbsGain));
722 result_py.emplace_back(
723 absl::Substitute(" ]))) / $0.0)", kLogGain));
724 result_py.emplace_back(" ])");
725 } else {
726 result_py.emplace_back("def soft_atan2(y, x):");
727 result_py.emplace_back(" return casadi.arctan2(");
728 result_py.emplace_back(" y,");
729 result_py.emplace_back(" casadi.logsumexp(casadi.SX(numpy.array(");
730 result_py.emplace_back(
731 absl::Substitute(" [1.0, x * (1.0 - 2.0 / (1 + "
732 "casadi.exp($1.0 * x))) * $0.0]))) / $0.0)",
733 kLogGain, kAbsGain));
734 }
735 result_py.emplace_back("");
736 result_py.emplace_back("# Is = STEER_CURRENT_COUPLING_FACTOR * Id");
737 result_py.emplace_back(absl::Substitute(
738 "STEER_CURRENT_COUPLING_FACTOR = $0",
739 ccode(*(neg(
740 mul(div(Gs_, Kts_),
741 mul(div(Ktd_, mul(Gd_, rw_)),
742 neg(mul(add(neg(wb_), mul(add(rs_, rp_),
743 sub(integer(1), div(rb1_, rp_)))),
744 div(rw_, rb2_))))))))));
Austin Schuhbd75c482024-08-18 00:03:51 -0700745 result_py.emplace_back("");
Austin Schuh98fbbe82024-08-18 23:07:26 -0700746 result_py.emplace_back("# Is = STEER_CURRENT_COUPLING_FACTOR * Id");
747 result_py.emplace_back(absl::Substitute(
748 "STEER_CURRENT_COUPLING_FACTOR = $0",
749 ccode(*(neg(
750 mul(div(Gs_, Kts_),
751 mul(div(Ktd_, mul(Gd_, rw_)),
752 neg(mul(add(neg(wb_), mul(add(rs_, rp_),
753 sub(integer(1), div(rb1_, rp_)))),
754 div(rw_, rb2_))))))))));
755 result_py.emplace_back("");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700756
Austin Schuh0f881092024-06-28 15:36:48 -0700757 result_py.emplace_back("# Returns the derivative of our state vector");
758 result_py.emplace_back("# [thetas0, thetad0, omegas0, omegad0,");
759 result_py.emplace_back("# thetas1, thetad1, omegas1, omegad1,");
760 result_py.emplace_back("# thetas2, thetad2, omegas2, omegad2,");
761 result_py.emplace_back("# thetas3, thetad3, omegas3, omegad3,");
762 result_py.emplace_back("# x, y, theta, vx, vy, omega,");
763 result_py.emplace_back("# Fx, Fy, Moment]");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700764 result_py.emplace_back("def swerve_full_dynamics(X, U):");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700765 WriteCasadiVariables(&result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700766
767 result_py.emplace_back("");
768 result_py.emplace_back(" result = casadi.SX.sym('result', 25, 1)");
769 result_py.emplace_back("");
770
771 // And then write out the derivative of each state.
772 for (size_t m = 0; m < kNumModules; ++m) {
773 result_py.emplace_back(
774 absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m));
775 result_py.emplace_back(
776 absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m));
777
Austin Schuh6ea789e2024-07-27 13:45:53 -0700778 result_py.emplace_back(
779 absl::Substitute(" result[$0, 0] = $1", m * 4 + 2,
780 ccode(*modules_[m].full.alphas_eqn)));
781 result_py.emplace_back(
782 absl::Substitute(" result[$0, 0] = $1", m * 4 + 3,
783 ccode(*modules_[m].full.alphad_eqn)));
Austin Schuh0f881092024-06-28 15:36:48 -0700784 }
785
786 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700787 absl::Substitute(" result[$0, 0] = vx", kNumModules * 4 + 0));
Austin Schuh0f881092024-06-28 15:36:48 -0700788 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700789 absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1));
Austin Schuh0f881092024-06-28 15:36:48 -0700790 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700791 absl::Substitute(" result[$0, 0] = omega", kNumModules * 4 + 2));
Austin Schuh0f881092024-06-28 15:36:48 -0700792
Austin Schuh0f881092024-06-28 15:36:48 -0700793 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700794 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700795 ccode(*full_accel_.get(0, 0))));
Austin Schuh0f881092024-06-28 15:36:48 -0700796 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700797 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700798 ccode(*full_accel_.get(1, 0))));
799 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
800 kNumModules * 4 + 5,
801 ccode(*full_angular_accel_)));
Austin Schuh0f881092024-06-28 15:36:48 -0700802
803 result_py.emplace_back(
804 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6));
805 result_py.emplace_back(
806 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7));
807 result_py.emplace_back(
808 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8));
809
810 result_py.emplace_back("");
811 result_py.emplace_back(
812 " return casadi.Function('xdot', [X, U], [result])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700813
Austin Schuh6ea789e2024-07-27 13:45:53 -0700814 result_py.emplace_back("");
815
816 result_py.emplace_back("# Returns the derivative of our state vector");
817 result_py.emplace_back("# [thetas0, omegas0,");
818 result_py.emplace_back("# thetas1, omegas1,");
819 result_py.emplace_back("# thetas2, omegas2,");
820 result_py.emplace_back("# thetas3, omegas3,");
821 result_py.emplace_back("# theta, vx, vy, omega]");
822 result_py.emplace_back("def velocity_swerve_physics(X, U):");
823 WriteCasadiVelocityVariables(&result_py);
824
825 result_py.emplace_back("");
826 result_py.emplace_back(
827 " result = casadi.SX.sym('result', NUM_VELOCITY_STATES, 1)");
828 result_py.emplace_back("");
829
830 // And then write out the derivative of each state.
831 for (size_t m = 0; m < kNumModules; ++m) {
832 result_py.emplace_back(
833 absl::Substitute(" result[$0, 0] = omegas$1", m * 2 + 0, m));
834 result_py.emplace_back(
835 absl::Substitute(" result[$0, 0] = $1", m * 2 + 1,
836 ccode(*modules_[m].direct.alphas_eqn)));
837 }
838 result_py.emplace_back(
839 absl::Substitute(" result[$0, 0] = omega", kNumModules * 2 + 0));
840
841 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
842 kNumModules * 2 + 1,
843 ccode(*direct_accel_.get(0, 0))));
844 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
845 kNumModules * 2 + 2,
846 ccode(*direct_accel_.get(1, 0))));
847 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
848 kNumModules * 2 + 3,
849 ccode(*direct_angular_accel_)));
850
851 // result_py.emplace_back(
852 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 4));
853 // result_py.emplace_back(
854 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 5));
855 // result_py.emplace_back(
856 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 6));
857
858 result_py.emplace_back("");
859 result_py.emplace_back(
860 " return casadi.Function('xdot', [X, U], [result])");
861
Austin Schuhb8b34be2024-07-14 16:06:19 -0700862 DefineVector2dFunction(
863 "contact_patch_velocity",
864 "# Returns the velocity of the wheel in global coordinates.",
865 [](const Module &m, int dimension) {
866 return ccode(*m.contact_patch_velocity.get(dimension, 0));
867 },
868 &result_py);
869 DefineVector2dFunction(
870 "wheel_ground_velocity",
871 "# Returns the velocity of the wheel in steer module coordinates.",
872 [](const Module &m, int dimension) {
873 return ccode(*m.wheel_ground_velocity.get(dimension, 0));
874 },
875 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700876
Austin Schuhb8b34be2024-07-14 16:06:19 -0700877 DefineVector2dFunction(
878 "wheel_slip_velocity",
879 "# Returns the difference in velocities of the wheel surface and the "
880 "ground.",
881 [](const Module &m, int dimension) {
882 return ccode(*m.wheel_slip_velocity.get(dimension, 0));
883 },
884 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700885
Austin Schuhb8b34be2024-07-14 16:06:19 -0700886 DefineScalarFunction(
887 "slip_angle", "Returns the slip angle of the ith wheel",
888 [](const Module &m) { return ccode(*m.slip_angle); }, &result_py);
889 DefineScalarFunction(
890 "slip_ratio", "Returns the slip ratio of the ith wheel",
891 [](const Module &m) { return ccode(*m.slip_ratio); }, &result_py);
892 DefineScalarFunction(
893 "module_angular_accel",
894 "Returns the angular acceleration of the robot due to the ith wheel",
Austin Schuh6ea789e2024-07-27 13:45:53 -0700895 [this](const Module &m) { return ccode(*div(m.full.torque, Js_)); },
896 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700897
Austin Schuhb8b34be2024-07-14 16:06:19 -0700898 DefineVector2dFunction(
899 "wheel_force",
900 "Returns the force on the wheel in steer module coordinates",
901 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700902 return ccode(
903 *std::vector<RCP<const Basic>>{m.full.Fwx, m.Fwy}[dimension]);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700904 },
905 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700906
Austin Schuhb8b34be2024-07-14 16:06:19 -0700907 DefineVector2dFunction(
908 "F", "Returns the force on the wheel in absolute coordinates",
909 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700910 return ccode(*m.full.F.get(dimension, 0));
Austin Schuhb8b34be2024-07-14 16:06:19 -0700911 },
912 &result_py);
913
justinT21820767f2024-09-11 19:57:55 -0700914 DefineVector2dVelocityFunction(
915 "F_vel",
916 "Returns the force on the wheel in absolute coordinates based on the "
917 "velocity controller",
918 [](const Module &m, int dimension) {
919 return ccode(*m.direct.F.get(dimension, 0));
920 },
921 &result_py);
922
923 DefineVector2dVelocityFunction(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700924 "mounting_location",
925 "Returns the mounting location of wheel in robot coordinates",
926 [](const Module &m, int dimension) {
927 return ccode(*m.mounting_location.get(dimension, 0));
928 },
929 &result_py);
930
931 DefineScalarFunction(
932 "Ms", "Returns the self aligning moment of the ith wheel",
933 [this](const Module &m) {
934 return ccode(*(div(m.Ms, add(Jsm_, div(div(Js_, Gs_), Gs_)))));
935 },
936 &result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700937
938 aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n"));
939 }
940
Austin Schuhb8b34be2024-07-14 16:06:19 -0700941 void DefineScalarFunction(
942 std::string_view name, std::string_view documentation,
943 std::function<std::string(const Module &)> scalar_fn,
944 std::vector<std::string> *result_py) {
945 result_py->emplace_back("");
946 result_py->emplace_back(absl::Substitute("# $0.", documentation));
947 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
948 WriteCasadiVariables(result_py);
949 for (size_t m = 0; m < kNumModules; ++m) {
950 if (m == 0) {
951 result_py->emplace_back(" if i == 0:");
952 } else {
953 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
954 }
955 result_py->emplace_back(
956 absl::Substitute(" return casadi.Function('$0', [X, U], [$1])",
957 name, scalar_fn(modules_[m])));
958 }
959 result_py->emplace_back(" raise ValueError(\"Invalid module number\")");
960 }
961
962 void DefineVector2dFunction(
963 std::string_view name, std::string_view documentation,
964 std::function<std::string(const Module &, int)> scalar_fn,
965 std::vector<std::string> *result_py) {
966 result_py->emplace_back("");
967 result_py->emplace_back(absl::Substitute("# $0.", documentation));
968 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
969 WriteCasadiVariables(result_py);
970 result_py->emplace_back(
971 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
972 for (size_t m = 0; m < kNumModules; ++m) {
973 if (m == 0) {
974 result_py->emplace_back(" if i == 0:");
975 } else {
976 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
977 }
978 for (int j = 0; j < 2; ++j) {
979 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
980 j, scalar_fn(modules_[m], j)));
981 }
982 }
983 result_py->emplace_back(" else:");
984 result_py->emplace_back(
985 " raise ValueError(\"Invalid module number\")");
986 result_py->emplace_back(absl::Substitute(
987 " return casadi.Function('$0', [X, U], [result])", name));
988 }
989
justinT21820767f2024-09-11 19:57:55 -0700990 void DefineVector2dVelocityFunction(
991 std::string_view name, std::string_view documentation,
992 std::function<std::string(const Module &, int)> scalar_fn,
993 std::vector<std::string> *result_py) {
994 result_py->emplace_back("");
995 result_py->emplace_back(absl::Substitute("# $0.", documentation));
996 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
997 WriteCasadiVelocityVariables(result_py);
998 result_py->emplace_back(
999 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
1000 for (size_t m = 0; m < kNumModules; ++m) {
1001 if (m == 0) {
1002 result_py->emplace_back(" if i == 0:");
1003 } else {
1004 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
1005 }
1006 for (int j = 0; j < 2; ++j) {
1007 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
1008 j, scalar_fn(modules_[m], j)));
1009 }
1010 }
1011 result_py->emplace_back(" else:");
1012 result_py->emplace_back(
1013 " raise ValueError(\"Invalid module number\")");
1014 result_py->emplace_back(absl::Substitute(
1015 " return casadi.Function('$0', [X, U], [result])", name));
1016 }
1017
justinT21446e4f62024-06-16 22:36:10 -07001018 private:
1019 static constexpr uint8_t kNumModules = 4;
1020
Austin Schuh6ea789e2024-07-27 13:45:53 -07001021 RCP<const Basic> SteerAccel(RCP<const Basic> Fwx, RCP<const Basic> Ms,
1022 RCP<const Basic> Is) {
1023 RCP<const Basic> lhms =
1024 mul(add(neg(wb_), mul(add(rs_, rp_), sub(integer(1), div(rb1_, rp_)))),
1025 mul(div(rw_, rb2_), neg(Fwx)));
1026 RCP<const Basic> lhs = add(add(Ms, div(mul(Kts_, Is), Gs_)), lhms);
1027 RCP<const Basic> rhs = add(Jsm_, div(div(Js_, Gs_), Gs_));
1028 return simplify(div(lhs, rhs));
1029 }
1030
justinT21446e4f62024-06-16 22:36:10 -07001031 Module ModulePhysics(const int m, DenseMatrix mounting_location) {
1032 VLOG(1) << "Solving module " << m;
1033
1034 Module result;
Austin Schuhb8b34be2024-07-14 16:06:19 -07001035 result.mounting_location = mounting_location;
justinT21446e4f62024-06-16 22:36:10 -07001036
1037 result.Is = symbol(absl::StrFormat("Is%u", m));
1038 result.Id = symbol(absl::StrFormat("Id%u", m));
1039
1040 RCP<const Symbol> thetamd = symbol(absl::StrFormat("theta_md%u", m));
1041 RCP<const Symbol> omegamd = symbol(absl::StrFormat("omega_md%u", m));
1042 RCP<const Symbol> alphamd = symbol(absl::StrFormat("alpha_md%u", m));
1043
1044 result.thetas = symbol(absl::StrFormat("thetas%u", m));
1045 result.omegas = symbol(absl::StrFormat("omegas%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -07001046 RCP<const Symbol> alphas = symbol(absl::StrFormat("alphas%u", m));
justinT21446e4f62024-06-16 22:36:10 -07001047
justinT21446e4f62024-06-16 22:36:10 -07001048 result.omegad = symbol(absl::StrFormat("omegad%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -07001049 RCP<const Symbol> alphad = symbol(absl::StrFormat("alphad%u", m));
justinT21446e4f62024-06-16 22:36:10 -07001050
1051 // Velocity of the module in field coordinates
Austin Schuh2a1abec2024-07-10 20:31:16 -07001052 DenseMatrix robot_velocity = DenseMatrix(2, 1, {vx_, vy_});
justinT21446e4f62024-06-16 22:36:10 -07001053 VLOG(1) << "robot velocity: " << robot_velocity.__str__();
1054
1055 // Velocity of the contact patch in field coordinates
1056 DenseMatrix temp_matrix = DenseMatrix(2, 1);
1057 DenseMatrix temp_matrix2 = DenseMatrix(2, 1);
Austin Schuhbd75c482024-08-18 00:03:51 -07001058 DenseMatrix temp_matrix3 = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -07001059 result.contact_patch_velocity = DenseMatrix(2, 1);
justinT21446e4f62024-06-16 22:36:10 -07001060
Austin Schuhb8b34be2024-07-14 16:06:19 -07001061 mul_dense_dense(R(theta_), result.mounting_location, temp_matrix);
justinT21446e4f62024-06-16 22:36:10 -07001062 add_dense_dense(angle_cross(temp_matrix, omega_), robot_velocity,
1063 temp_matrix2);
1064 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuhbd75c482024-08-18 00:03:51 -07001065 DenseMatrix(2, 1, {neg(caster_), integer(0)}),
1066 temp_matrix3);
justinT21446e4f62024-06-16 22:36:10 -07001067 add_dense_dense(temp_matrix2,
Austin Schuhbd75c482024-08-18 00:03:51 -07001068 angle_cross(temp_matrix3, add(omega_, result.omegas)),
Austin Schuh2a1abec2024-07-10 20:31:16 -07001069 result.contact_patch_velocity);
justinT21446e4f62024-06-16 22:36:10 -07001070
1071 VLOG(1);
Austin Schuh2a1abec2024-07-10 20:31:16 -07001072 VLOG(1) << "contact patch velocity: "
1073 << result.contact_patch_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -07001074
1075 // Relative velocity of the surface of the wheel to the ground.
Austin Schuhb67a38f2024-07-04 13:48:38 -07001076 result.wheel_ground_velocity = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -07001077 mul_dense_dense(R(neg(add(result.thetas, theta_))),
1078 result.contact_patch_velocity,
Austin Schuhb67a38f2024-07-04 13:48:38 -07001079 result.wheel_ground_velocity);
justinT21446e4f62024-06-16 22:36:10 -07001080
Austin Schuhb8b34be2024-07-14 16:06:19 -07001081 // Compute the relative velocity between the wheel surface and the ground in
1082 // the wheel coordinate system.
1083 result.wheel_slip_velocity = DenseMatrix(2, 1);
1084 DenseMatrix wheel_velocity =
1085 DenseMatrix(2, 1, {mul(rw_, result.omegad), integer(0)});
1086 DenseMatrix negative_wheel_ground_velocity =
1087 DenseMatrix(2, 1,
1088 {neg(result.wheel_ground_velocity.get(0, 0)),
1089 neg(result.wheel_ground_velocity.get(1, 0))});
1090 add_dense_dense(negative_wheel_ground_velocity, wheel_velocity,
1091 result.wheel_slip_velocity);
1092
justinT21446e4f62024-06-16 22:36:10 -07001093 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001094 VLOG(1) << "wheel ground velocity: "
1095 << result.wheel_ground_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -07001096
Austin Schuh5ddcb472024-07-21 17:55:34 -07001097 result.slip_angle = sin(neg(atan2(result.wheel_ground_velocity.get(1, 0),
1098 result.wheel_ground_velocity.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -07001099
1100 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001101 VLOG(1) << "slip angle: " << result.slip_angle->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001102
Austin Schuh2a1abec2024-07-10 20:31:16 -07001103 // TODO(austin): Does this handle decel properly?
Austin Schuhb67a38f2024-07-04 13:48:38 -07001104 result.slip_ratio = div(
Austin Schuh2a1abec2024-07-10 20:31:16 -07001105 sub(mul(rw_, result.omegad), result.wheel_ground_velocity.get(0, 0)),
1106 SymEngine::max(
1107 {real_double(0.02), abs(result.wheel_ground_velocity.get(0, 0))}));
justinT21446e4f62024-06-16 22:36:10 -07001108 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001109 VLOG(1) << "Slip ratio " << result.slip_ratio->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001110
Austin Schuh6ea789e2024-07-27 13:45:53 -07001111 result.full.Fwx = simplify(mul(Cx_, result.slip_ratio));
Austin Schuhb67a38f2024-07-04 13:48:38 -07001112 result.Fwy = simplify(mul(Cy_, result.slip_angle));
justinT21446e4f62024-06-16 22:36:10 -07001113
Austin Schuh27694fa2024-07-20 16:29:49 -07001114 // The self-aligning moment needs to flip when the module flips direction.
Austin Schuh78a1b312024-08-18 17:21:34 -07001115 RCP<const Basic> softsign_velocity = add(
1116 div(integer(-2),
1117 add(integer(1), exp(mul(integer(100),
1118 result.wheel_ground_velocity.get(0, 0))))),
1119 integer(1));
1120 result.Ms =
1121 mul(neg(result.Fwy),
1122 add(div(mul(softsign_velocity, contact_patch_length_), integer(3)),
1123 caster_));
justinT21446e4f62024-06-16 22:36:10 -07001124 VLOG(1);
Austin Schuhb8b34be2024-07-14 16:06:19 -07001125 VLOG(1) << "Ms " << result.Ms->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001126 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001127 VLOG(1) << "full.Fwx " << result.full.Fwx->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001128 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001129 VLOG(1) << "Fwy " << result.Fwy->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001130
Austin Schuh6ea789e2024-07-27 13:45:53 -07001131 // -K_td * Id / Gd + Fwx * rw = 0
1132 // Fwx = K_td * Id / Gd / rw
1133 result.direct.Fwx = mul(Ktd_, div(result.Id, mul(Gd_, rw_)));
1134
1135 result.direct.alphas_eqn =
1136 SteerAccel(result.direct.Fwx, result.Ms, result.Is);
1137
1138 // d/dt omegas = ...
1139 result.full.alphas_eqn = SteerAccel(result.full.Fwx, result.Ms, result.Is);
justinT21446e4f62024-06-16 22:36:10 -07001140
1141 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001142 VLOG(1) << alphas->__str__() << " = " << result.full.alphas_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001143
Austin Schuh6ea789e2024-07-27 13:45:53 -07001144 RCP<const Basic> lhs =
1145 sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), alphas),
1146 mul(Gd1_, mul(Gd2_, alphamd)));
1147 RCP<const Basic> ddplanitary_eqn = sub(mul(Gd3_, lhs), alphad);
justinT21446e4f62024-06-16 22:36:10 -07001148
Austin Schuh6ea789e2024-07-27 13:45:53 -07001149 RCP<const Basic> full_drive_eqn =
1150 sub(add(mul(neg(Jdm_), div(alphamd, Gd_)),
1151 mul(Ktd_, div(neg(result.Id), Gd_))),
1152 mul(neg(result.full.Fwx), rw_));
justinT21446e4f62024-06-16 22:36:10 -07001153
Austin Schuh6ea789e2024-07-27 13:45:53 -07001154 VLOG(1) << "full_drive_eqn: " << full_drive_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001155
1156 // Substitute in ddplanitary_eqn so we get rid of alphamd
1157 map_basic_basic map;
1158 RCP<const Set> reals = interval(NegInf, Inf, true, true);
1159 RCP<const Set> solve_solution = solve(ddplanitary_eqn, alphamd, reals);
1160 map[alphamd] = solve_solution->get_args()[1]->get_args()[0];
1161 VLOG(1) << "temp: " << solve_solution->__str__();
Austin Schuh6ea789e2024-07-27 13:45:53 -07001162 RCP<const Basic> drive_eqn_subs = full_drive_eqn->subs(map);
justinT21446e4f62024-06-16 22:36:10 -07001163
1164 map.clear();
Austin Schuh6ea789e2024-07-27 13:45:53 -07001165 map[alphas] = result.full.alphas_eqn;
justinT21446e4f62024-06-16 22:36:10 -07001166 RCP<const Basic> drive_eqn_subs2 = drive_eqn_subs->subs(map);
1167 RCP<const Basic> drive_eqn_subs3 = simplify(drive_eqn_subs2);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001168 VLOG(1) << "full_drive_eqn simplified: " << drive_eqn_subs3->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001169
Austin Schuh6ea789e2024-07-27 13:45:53 -07001170 solve_solution = solve(drive_eqn_subs3, alphad, reals);
justinT21446e4f62024-06-16 22:36:10 -07001171
Austin Schuh6ea789e2024-07-27 13:45:53 -07001172 result.full.alphad_eqn =
justinT21446e4f62024-06-16 22:36:10 -07001173 simplify(solve_solution->get_args()[1]->get_args()[0]);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001174 VLOG(1) << "drive_accel: " << result.full.alphad_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001175
Austin Schuh2a1abec2024-07-10 20:31:16 -07001176 // Compute the resulting force from the module.
Austin Schuh6ea789e2024-07-27 13:45:53 -07001177 result.full.F = DenseMatrix(2, 1);
Austin Schuhb8b34be2024-07-14 16:06:19 -07001178 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuh6ea789e2024-07-27 13:45:53 -07001179 DenseMatrix(2, 1, {result.full.Fwx, result.Fwy}),
1180 result.full.F);
justinT21d18f79f2024-09-22 19:43:05 -07001181
1182 DenseMatrix rotated_mounting_location = DenseMatrix(2, 1);
1183 mul_dense_dense(R(theta_), result.mounting_location,
1184 rotated_mounting_location);
1185 result.full.torque = force_cross(rotated_mounting_location, result.full.F);
justinT21446e4f62024-06-16 22:36:10 -07001186
Austin Schuh6ea789e2024-07-27 13:45:53 -07001187 result.direct.F = DenseMatrix(2, 1);
1188 mul_dense_dense(R(add(theta_, result.thetas)),
1189 DenseMatrix(2, 1, {result.direct.Fwx, result.Fwy}),
1190 result.direct.F);
1191 result.direct.torque =
justinT21d18f79f2024-09-22 19:43:05 -07001192 force_cross(rotated_mounting_location, result.direct.F);
justinT21446e4f62024-06-16 22:36:10 -07001193
1194 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001195 VLOG(1) << "full torque = " << result.full.torque->__str__();
1196 VLOG(1) << "direct torque = " << result.full.torque->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001197
justinT21446e4f62024-06-16 22:36:10 -07001198 return result;
1199 }
1200
1201 DenseMatrix R(const RCP<const Basic> theta) {
1202 return DenseMatrix(2, 2,
1203 {cos(theta), neg(sin(theta)), sin(theta), cos(theta)});
1204 }
1205
1206 DenseMatrix angle_cross(DenseMatrix a, RCP<const Basic> b) {
Austin Schuh2a1abec2024-07-10 20:31:16 -07001207 return DenseMatrix(2, 1, {mul(neg(a.get(1, 0)), b), mul(a.get(0, 0), b)});
justinT21446e4f62024-06-16 22:36:10 -07001208 }
1209
1210 RCP<const Basic> force_cross(DenseMatrix r, DenseMatrix f) {
1211 return sub(mul(r.get(0, 0), f.get(1, 0)), mul(r.get(1, 0), f.get(0, 0)));
1212 }
1213
1214 // z represents the number of teeth per gear, theta is the angle between
1215 // shafts(in degrees), D_02 is the pitch diameter of gear 2 and b_2 is the
1216 // length of the tooth of gear 2
1217 // returns std::pair(r_01, r_02)
1218 std::pair<double, double> GetBevelPitchRadius(double z1, double z2,
1219 double theta, double D_02,
1220 double b_2) {
1221 double gamma_1 = std::atan2(z1, z2);
1222 double gamma_2 = theta / 180.0 * std::numbers::pi - gamma_1;
1223 double R_m = D_02 / 2 / std::sin(gamma_2) - b_2 / 2;
1224 return std::pair(R_m * std::cos(gamma_2), R_m * std::sin(gamma_2));
1225 }
1226
1227 Motor drive_motor_;
1228 Motor steer_motor_;
1229
1230 RCP<const Basic> Cx_;
1231 RCP<const Basic> Cy_;
Austin Schuh2a1abec2024-07-10 20:31:16 -07001232 RCP<const Basic> rw_;
justinT21446e4f62024-06-16 22:36:10 -07001233 RCP<const Basic> m_;
1234 RCP<const Basic> J_;
1235 RCP<const Basic> Gd1_;
1236 RCP<const Basic> rs_;
1237 RCP<const Basic> rp_;
1238 RCP<const Basic> Gd2_;
1239 RCP<const Basic> rb1_;
1240 RCP<const Basic> rb2_;
1241 RCP<const Basic> Gd3_;
1242 RCP<const Basic> Gd_;
1243 RCP<const Basic> Js_;
1244 RCP<const Basic> Gs_;
1245 RCP<const Basic> wb_;
1246 RCP<const Basic> Jdm_;
1247 RCP<const Basic> Jsm_;
1248 RCP<const Basic> Kts_;
1249 RCP<const Basic> Ktd_;
1250 RCP<const Basic> robot_width_;
1251 RCP<const Basic> caster_;
1252 RCP<const Basic> contact_patch_length_;
1253 RCP<const Basic> x_;
1254 RCP<const Basic> y_;
1255 RCP<const Basic> theta_;
1256 RCP<const Basic> vx_;
1257 RCP<const Basic> vy_;
1258 RCP<const Basic> omega_;
1259 RCP<const Basic> ax_;
1260 RCP<const Basic> ay_;
1261 RCP<const Basic> atheta_;
1262
1263 std::array<Module, kNumModules> modules_;
1264
Austin Schuh6ea789e2024-07-27 13:45:53 -07001265 DenseMatrix full_accel_;
1266 RCP<const Basic> full_angular_accel_;
1267 DenseMatrix direct_accel_;
1268 RCP<const Basic> direct_angular_accel_;
justinT21446e4f62024-06-16 22:36:10 -07001269};
1270
1271} // namespace frc971::control_loops::swerve
1272
1273int main(int argc, char **argv) {
1274 aos::InitGoogle(&argc, &argv);
1275
1276 frc971::control_loops::swerve::SwerveSimulation sim;
1277
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001278 if (!absl::GetFlag(FLAGS_cc_output_path).empty() &&
1279 !absl::GetFlag(FLAGS_h_output_path).empty()) {
1280 sim.Write(absl::GetFlag(FLAGS_cc_output_path),
1281 absl::GetFlag(FLAGS_h_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001282 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001283 if (!absl::GetFlag(FLAGS_casadi_py_output_path).empty()) {
1284 sim.WriteCasadi(absl::GetFlag(FLAGS_casadi_py_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001285 }
justinT21446e4f62024-06-16 22:36:10 -07001286
1287 return 0;
1288}