blob: 8a7fe16af435e6e0a5fc8c82686e943c35d2f655 [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 Schuha9550c02024-10-19 13:48:10 -070035ABSL_FLAG(std::string, constants_output_path, "",
36 "Path to write constants python code to");
Austin Schuh27694fa2024-07-20 16:29:49 -070037ABSL_FLAG(double, caster, 0.01, "Caster in meters for the module.");
justinT21446e4f62024-06-16 22:36:10 -070038
Austin Schuh99f7c6a2024-06-25 22:07:44 -070039ABSL_FLAG(bool, symbolic, false, "If true, write everything out symbolically.");
Austin Schuh5371c802024-09-02 13:18:48 -070040ABSL_FLAG(bool, function, true, "If true, make soft_atan2 a function.");
justinT21446e4f62024-06-16 22:36:10 -070041
justinT21942892b2024-07-02 22:33:50 -070042using SymEngine::abs;
justinT21446e4f62024-06-16 22:36:10 -070043using SymEngine::add;
44using SymEngine::atan2;
45using SymEngine::Basic;
46using SymEngine::ccode;
47using SymEngine::cos;
48using SymEngine::DenseMatrix;
49using SymEngine::div;
Austin Schuh78a1b312024-08-18 17:21:34 -070050using SymEngine::exp;
justinT21446e4f62024-06-16 22:36:10 -070051using SymEngine::Inf;
52using SymEngine::integer;
53using SymEngine::map_basic_basic;
54using SymEngine::minus_one;
55using SymEngine::neg;
56using SymEngine::NegInf;
57using SymEngine::pow;
58using SymEngine::RCP;
59using SymEngine::real_double;
60using SymEngine::RealDouble;
61using SymEngine::Set;
62using SymEngine::simplify;
63using SymEngine::sin;
64using SymEngine::solve;
65using SymEngine::symbol;
66using SymEngine::Symbol;
67
68namespace frc971::control_loops::swerve {
69
70// State per module.
71struct Module {
Austin Schuh6ea789e2024-07-27 13:45:53 -070072 DenseMatrix mounting_location;
Austin Schuh5dac2292024-10-19 13:56:58 -070073 DenseMatrix rotated_mounting_location;
Austin Schuh6ea789e2024-07-27 13:45:53 -070074
justinT21446e4f62024-06-16 22:36:10 -070075 RCP<const Symbol> Is;
76
77 RCP<const Symbol> Id;
78
79 RCP<const Symbol> thetas;
80 RCP<const Symbol> omegas;
justinT21446e4f62024-06-16 22:36:10 -070081
justinT21446e4f62024-06-16 22:36:10 -070082 RCP<const Symbol> omegad;
justinT21446e4f62024-06-16 22:36:10 -070083
Austin Schuh2a1abec2024-07-10 20:31:16 -070084 DenseMatrix contact_patch_velocity;
Austin Schuhb67a38f2024-07-04 13:48:38 -070085 DenseMatrix wheel_ground_velocity;
Austin Schuhb8b34be2024-07-14 16:06:19 -070086 DenseMatrix wheel_slip_velocity;
Austin Schuhb67a38f2024-07-04 13:48:38 -070087 RCP<const Basic> slip_angle;
88 RCP<const Basic> slip_ratio;
89
Austin Schuhb8b34be2024-07-14 16:06:19 -070090 RCP<const Basic> Ms;
Austin Schuhb67a38f2024-07-04 13:48:38 -070091 RCP<const Basic> Fwy;
92
Austin Schuh6ea789e2024-07-27 13:45:53 -070093 struct Full {
94 RCP<const Basic> Fwx;
95 DenseMatrix F;
96
97 RCP<const Basic> torque;
98
99 RCP<const Basic> alphas_eqn;
100 RCP<const Basic> alphad_eqn;
101 } full;
102
103 struct Direct {
104 RCP<const Basic> Fwx;
105 DenseMatrix F;
106
107 RCP<const Basic> torque;
108
109 RCP<const Basic> alphas_eqn;
110 } direct;
justinT21446e4f62024-06-16 22:36:10 -0700111};
112
Austin Schuh6ea789e2024-07-27 13:45:53 -0700113DenseMatrix SumMatrices(DenseMatrix a) { return a; }
114
115template <typename... Args>
116DenseMatrix SumMatrices(DenseMatrix a, Args... args) {
117 DenseMatrix result = DenseMatrix(2, 1, {integer(0), integer(0)});
118
119 DenseMatrix b = SumMatrices(args...);
120 add_dense_dense(a, b, result);
121 return result;
122}
123
justinT21446e4f62024-06-16 22:36:10 -0700124class SwerveSimulation {
125 public:
126 SwerveSimulation() : drive_motor_(KrakenFOC()), steer_motor_(KrakenFOC()) {
127 auto fx = symbol("fx");
128 auto fy = symbol("fy");
129 auto moment = symbol("moment");
130
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700131 if (absl::GetFlag(FLAGS_symbolic)) {
justinT21446e4f62024-06-16 22:36:10 -0700132 Cx_ = symbol("Cx");
133 Cy_ = symbol("Cy");
134
Austin Schuh2a1abec2024-07-10 20:31:16 -0700135 rw_ = symbol("rw");
justinT21446e4f62024-06-16 22:36:10 -0700136
137 m_ = symbol("m");
138 J_ = symbol("J");
139
140 Gd1_ = symbol("Gd1");
141 rs_ = symbol("rs");
142 rp_ = symbol("rp");
143 Gd2_ = symbol("Gd2");
144
145 rb1_ = symbol("rb1");
146 rb2_ = symbol("rb2");
147
Austin Schuh76534f32024-09-02 13:52:45 -0700148 Gd3_ = symbol("Gd3");
justinT21446e4f62024-06-16 22:36:10 -0700149 Gd_ = symbol("Gd");
150
151 Js_ = symbol("Js");
152
153 Gs_ = symbol("Gs");
154 wb_ = symbol("wb");
155
156 Jdm_ = symbol("Jdm");
157 Jsm_ = symbol("Jsm");
158 Kts_ = symbol("Kts");
159 Ktd_ = symbol("Ktd");
160
161 robot_width_ = symbol("robot_width");
162
163 caster_ = symbol("caster");
164 contact_patch_length_ = symbol("Lcp");
165 } else {
Austin Schuhb8b34be2024-07-14 16:06:19 -0700166 Cx_ = real_double(25.0 * 9.8 / 4.0 / 0.05);
justinT21446e4f62024-06-16 22:36:10 -0700167 Cy_ = real_double(5 * 9.8 / 0.05 / 4.0);
168
Austin Schuh2a1abec2024-07-10 20:31:16 -0700169 rw_ = real_double(2 * 0.0254);
justinT21446e4f62024-06-16 22:36:10 -0700170
171 m_ = real_double(25.0); // base is 20 kg without battery
172 J_ = real_double(6.0);
173
174 Gd1_ = real_double(12.0 / 42.0);
175 rs_ = real_double(28.0 / 20.0 / 2.0);
176 rp_ = real_double(18.0 / 20.0 / 2.0);
177 Gd2_ = div(rs_, rp_);
178
179 // 15 / 45 bevel ratio, calculated using python script ported over to
180 // GetBevelPitchRadius(double
181 // TODO(Justin): Use the function instead of computed constantss
182 rb1_ = real_double(0.3805473);
183 rb2_ = real_double(1.14164);
184
185 Gd3_ = div(rb1_, rb2_);
186 Gd_ = mul(mul(Gd1_, Gd2_), Gd3_);
187
Austin Schuhb8b34be2024-07-14 16:06:19 -0700188 Js_ = real_double(0.001);
justinT21446e4f62024-06-16 22:36:10 -0700189
190 Gs_ = real_double(35.0 / 468.0);
191 wb_ = real_double(0.725);
192
193 Jdm_ = real_double(drive_motor_.motor_inertia);
194 Jsm_ = real_double(steer_motor_.motor_inertia);
195 Kts_ = real_double(steer_motor_.Kt);
196 Ktd_ = real_double(drive_motor_.Kt);
197
198 robot_width_ = real_double(24.75 * 0.0254);
199
Austin Schuh27694fa2024-07-20 16:29:49 -0700200 caster_ = real_double(absl::GetFlag(FLAGS_caster));
justinT21446e4f62024-06-16 22:36:10 -0700201 contact_patch_length_ = real_double(0.02);
202 }
203
204 x_ = symbol("x");
205 y_ = symbol("y");
206 theta_ = symbol("theta");
207
208 vx_ = symbol("vx");
209 vy_ = symbol("vy");
210 omega_ = symbol("omega");
211
212 ax_ = symbol("ax");
213 ay_ = symbol("ay");
214 atheta_ = symbol("atheta");
215
216 // Now, compute the accelerations due to the disturbance forces.
justinT21446e4f62024-06-16 22:36:10 -0700217 DenseMatrix external_accel = DenseMatrix(2, 1, {div(fx, m_), div(fy, m_)});
Austin Schuh6ea789e2024-07-27 13:45:53 -0700218 DenseMatrix external_force = DenseMatrix(2, 1, {fx, fy});
justinT21446e4f62024-06-16 22:36:10 -0700219
220 // And compute the physics contributions from each module.
221 modules_[0] = ModulePhysics(
222 0, DenseMatrix(
223 2, 1,
224 {div(robot_width_, integer(2)), div(robot_width_, integer(2))}));
225 modules_[1] =
226 ModulePhysics(1, DenseMatrix(2, 1,
227 {div(robot_width_, integer(-2)),
228 div(robot_width_, integer(2))}));
229 modules_[2] =
230 ModulePhysics(2, DenseMatrix(2, 1,
231 {div(robot_width_, integer(-2)),
232 div(robot_width_, integer(-2))}));
233 modules_[3] =
234 ModulePhysics(3, DenseMatrix(2, 1,
235 {div(robot_width_, integer(2)),
236 div(robot_width_, integer(-2))}));
237
238 // And convert them into the overall robot contribution.
Austin Schuh6ea789e2024-07-27 13:45:53 -0700239 DenseMatrix net_full_force =
240 SumMatrices(modules_[0].full.F, modules_[1].full.F, modules_[2].full.F,
241 modules_[3].full.F, external_force);
justinT21446e4f62024-06-16 22:36:10 -0700242
Austin Schuh6ea789e2024-07-27 13:45:53 -0700243 DenseMatrix net_direct_force =
244 SumMatrices(modules_[0].direct.F, modules_[1].direct.F,
245 modules_[2].direct.F, modules_[3].direct.F, external_force);
justinT21446e4f62024-06-16 22:36:10 -0700246
Austin Schuh6ea789e2024-07-27 13:45:53 -0700247 full_accel_ = DenseMatrix(2, 1);
248 mul_dense_scalar(net_full_force, pow(m_, minus_one), full_accel_);
justinT21446e4f62024-06-16 22:36:10 -0700249
Austin Schuh6ea789e2024-07-27 13:45:53 -0700250 full_angular_accel_ = div(
251 add(moment, add(add(modules_[0].full.torque, modules_[1].full.torque),
252 add(modules_[2].full.torque, modules_[3].full.torque))),
253 J_);
justinT21446e4f62024-06-16 22:36:10 -0700254
Austin Schuh6ea789e2024-07-27 13:45:53 -0700255 direct_accel_ = DenseMatrix(2, 1);
256 mul_dense_scalar(net_direct_force, pow(m_, minus_one), direct_accel_);
justinT21942892b2024-07-02 22:33:50 -0700257
Austin Schuh6ea789e2024-07-27 13:45:53 -0700258 direct_angular_accel_ =
259 div(add(moment,
260 add(add(modules_[0].direct.torque, modules_[1].direct.torque),
261 add(modules_[2].direct.torque, modules_[3].direct.torque))),
262 J_);
justinT21942892b2024-07-02 22:33:50 -0700263
Austin Schuh6ea789e2024-07-27 13:45:53 -0700264 VLOG(1) << "accel(0, 0) = " << ccode(*full_accel_.get(0, 0));
265 VLOG(1) << "accel(1, 0) = " << ccode(*full_accel_.get(1, 0));
266 VLOG(1) << "angular_accel = " << ccode(*full_angular_accel_);
justinT21942892b2024-07-02 22:33:50 -0700267 }
268
justinT21446e4f62024-06-16 22:36:10 -0700269 // Writes the physics out to the provided .cc and .h path.
270 void Write(std::string_view cc_path, std::string_view h_path) {
271 std::vector<std::string> result_cc;
272 std::vector<std::string> result_h;
273
Austin Schuh0f881092024-06-28 15:36:48 -0700274 std::string_view include_guard_stripped = h_path;
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700275 CHECK(absl::ConsumePrefix(&include_guard_stripped,
276 absl::GetFlag(FLAGS_output_base)));
justinT21446e4f62024-06-16 22:36:10 -0700277 std::string include_guard =
278 absl::StrReplaceAll(absl::AsciiStrToUpper(include_guard_stripped),
279 {{"/", "_"}, {".", "_"}});
280
281 // Write out the header.
282 result_h.emplace_back(absl::Substitute("#ifndef $0_", include_guard));
283 result_h.emplace_back(absl::Substitute("#define $0_", include_guard));
284 result_h.emplace_back("");
285 result_h.emplace_back("#include <Eigen/Dense>");
286 result_h.emplace_back("");
287 result_h.emplace_back("namespace frc971::control_loops::swerve {");
288 result_h.emplace_back("");
James Kuszmaulef14ab42024-09-14 15:05:24 -0700289 result_h.emplace_back("struct FullDynamicsStates {");
290 result_h.emplace_back("enum States {");
291 result_h.emplace_back(" kThetas0 = 0,");
292 result_h.emplace_back(" kThetad0 = 1,");
293 result_h.emplace_back(" kOmegas0 = 2,");
294 result_h.emplace_back(" kOmegad0 = 3,");
295 result_h.emplace_back(" kThetas1 = 4,");
296 result_h.emplace_back(" kThetad1 = 5,");
297 result_h.emplace_back(" kOmegas1 = 6,");
298 result_h.emplace_back(" kOmegad1 = 7,");
299 result_h.emplace_back(" kThetas2 = 8,");
300 result_h.emplace_back(" kThetad2 = 9,");
301 result_h.emplace_back(" kOmegas2 = 10,");
302 result_h.emplace_back(" kOmegad2 = 11,");
303 result_h.emplace_back(" kThetas3 = 12,");
304 result_h.emplace_back(" kThetad3 = 13,");
305 result_h.emplace_back(" kOmegas3 = 14,");
306 result_h.emplace_back(" kOmegad3 = 15,");
307 result_h.emplace_back(" kX = 16,");
308 result_h.emplace_back(" kY = 17,");
309 result_h.emplace_back(" kTheta = 18,");
310 result_h.emplace_back(" kVx = 19,");
311 result_h.emplace_back(" kVy = 20,");
312 result_h.emplace_back(" kOmega = 21,");
313 result_h.emplace_back(" kFx = 22,");
314 result_h.emplace_back(" kFy = 23,");
315 result_h.emplace_back(" kMoment = 24,");
316 result_h.emplace_back(" kNumStates");
317 result_h.emplace_back("};");
318 result_h.emplace_back("};");
319 result_h.emplace_back(
320 "inline constexpr size_t kNumFullDynamicsStates = "
321 "static_cast<size_t>(FullDynamicsStates::kNumStates);");
322 result_h.emplace_back("struct VelocityStates {");
323 result_h.emplace_back("enum States {");
324 result_h.emplace_back(" kThetas0 = 0,");
325 result_h.emplace_back(" kOmegas0 = 1,");
326 result_h.emplace_back(" kThetas1 = 2,");
327 result_h.emplace_back(" kOmegas1 = 3,");
328 result_h.emplace_back(" kThetas2 = 4,");
329 result_h.emplace_back(" kOmegas2 = 5,");
330 result_h.emplace_back(" kThetas3 = 6,");
331 result_h.emplace_back(" kOmegas3 = 7,");
332 result_h.emplace_back(" kTheta = 8,");
333 result_h.emplace_back(" kVx = 9,");
334 result_h.emplace_back(" kVy = 10,");
335 result_h.emplace_back(" kOmega = 11,");
336 result_h.emplace_back(" kNumStates");
337 result_h.emplace_back("};");
338 result_h.emplace_back("};");
339 result_h.emplace_back(
340 "inline constexpr size_t kNumVelocityStates = "
341 "static_cast<size_t>(VelocityStates::kNumStates);");
James Kuszmaulf65b1a52024-10-08 23:37:35 -0700342 result_h.emplace_back("struct InputStates {");
James Kuszmaulef14ab42024-09-14 15:05:24 -0700343 result_h.emplace_back("enum States {");
344 result_h.emplace_back(" kIs0 = 0,");
345 result_h.emplace_back(" kId0 = 1,");
346 result_h.emplace_back(" kIs1 = 2,");
347 result_h.emplace_back(" kId1 = 3,");
348 result_h.emplace_back(" kIs2 = 4,");
349 result_h.emplace_back(" kId2 = 5,");
350 result_h.emplace_back(" kIs3 = 6,");
351 result_h.emplace_back(" kId3 = 7,");
352 result_h.emplace_back(" kNumInputs = 8");
353 result_h.emplace_back("};");
354 result_h.emplace_back("};");
355 result_h.emplace_back(
356 "inline constexpr size_t kNumInputs = "
James Kuszmaulf65b1a52024-10-08 23:37:35 -0700357 "static_cast<size_t>(InputStates::kNumInputs);");
James Kuszmaulef14ab42024-09-14 15:05:24 -0700358 result_h.emplace_back("");
justinT21446e4f62024-06-16 22:36:10 -0700359 result_h.emplace_back("// Returns the derivative of our state vector");
justinT21446e4f62024-06-16 22:36:10 -0700360 result_h.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700361 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> SwervePhysics(");
justinT21446e4f62024-06-16 22:36:10 -0700362 result_h.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700363 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
364 "1>> X,");
365 result_h.emplace_back(
366 " Eigen::Ref<const Eigen::Matrix<double, kNumInputs, 1>> U);");
367 result_h.emplace_back("");
justinT21446e4f62024-06-16 22:36:10 -0700368 result_h.emplace_back("} // namespace frc971::control_loops::swerve");
369 result_h.emplace_back("");
370 result_h.emplace_back(absl::Substitute("#endif // $0_", include_guard));
371
372 // Write out the .cc
373 result_cc.emplace_back(
374 absl::Substitute("#include \"$0\"", include_guard_stripped));
375 result_cc.emplace_back("");
376 result_cc.emplace_back("#include <cmath>");
377 result_cc.emplace_back("");
378 result_cc.emplace_back("namespace frc971::control_loops::swerve {");
379 result_cc.emplace_back("");
justinT21446e4f62024-06-16 22:36:10 -0700380 result_cc.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700381 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> SwervePhysics(");
382 result_cc.emplace_back(
383 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
384 "1>> X,");
385 result_cc.emplace_back(
386 " Eigen::Ref<const Eigen::Matrix<double, kNumInputs, 1>> U) {");
387 result_cc.emplace_back(
388 " Eigen::Matrix<double, kNumFullDynamicsStates, 1> result;");
justinT21446e4f62024-06-16 22:36:10 -0700389
390 // Start by writing out variables matching each of the symbol names we use
391 // so we don't have to modify the computed equations too much.
392 for (size_t m = 0; m < kNumModules; ++m) {
393 result_cc.emplace_back(
394 absl::Substitute(" const double thetas$0 = X($1, 0);", m, m * 4));
395 result_cc.emplace_back(absl::Substitute(
396 " const double omegas$0 = X($1, 0);", m, m * 4 + 2));
397 result_cc.emplace_back(absl::Substitute(
398 " const double omegad$0 = X($1, 0);", m, m * 4 + 3));
399 }
400
401 result_cc.emplace_back(absl::Substitute(" const double theta = X($0, 0);",
402 kNumModules * 4 + 2));
403 result_cc.emplace_back(
404 absl::Substitute(" const double vx = X($0, 0);", kNumModules * 4 + 3));
405 result_cc.emplace_back(
406 absl::Substitute(" const double vy = X($0, 0);", kNumModules * 4 + 4));
407 result_cc.emplace_back(absl::Substitute(" const double omega = X($0, 0);",
408 kNumModules * 4 + 5));
409
410 result_cc.emplace_back(
411 absl::Substitute(" const double fx = X($0, 0);", kNumModules * 4 + 6));
412 result_cc.emplace_back(
413 absl::Substitute(" const double fy = X($0, 0);", kNumModules * 4 + 7));
414 result_cc.emplace_back(absl::Substitute(" const double moment = X($0, 0);",
415 kNumModules * 4 + 8));
416
417 // Now do the same for the inputs.
418 for (size_t m = 0; m < kNumModules; ++m) {
419 result_cc.emplace_back(
420 absl::Substitute(" const double Is$0 = U($1, 0);", m, m * 2));
421 result_cc.emplace_back(
422 absl::Substitute(" const double Id$0 = U($1, 0);", m, m * 2 + 1));
423 }
424
425 result_cc.emplace_back("");
426
427 // And then write out the derivative of each state.
428 for (size_t m = 0; m < kNumModules; ++m) {
429 result_cc.emplace_back(
430 absl::Substitute(" result($0, 0) = omegas$1;", m * 4, m));
431 result_cc.emplace_back(
432 absl::Substitute(" result($0, 0) = omegad$1;", m * 4 + 1, m));
433
Austin Schuh6ea789e2024-07-27 13:45:53 -0700434 result_cc.emplace_back(
435 absl::Substitute(" result($0, 0) = $1;", m * 4 + 2,
436 ccode(*modules_[m].full.alphas_eqn)));
437 result_cc.emplace_back(
438 absl::Substitute(" result($0, 0) = $1;", m * 4 + 3,
439 ccode(*modules_[m].full.alphad_eqn)));
justinT21446e4f62024-06-16 22:36:10 -0700440 }
441
442 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700443 absl::Substitute(" result($0, 0) = vx;", kNumModules * 4 + 0));
justinT21446e4f62024-06-16 22:36:10 -0700444 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700445 absl::Substitute(" result($0, 0) = vy;", kNumModules * 4 + 1));
justinT21446e4f62024-06-16 22:36:10 -0700446 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700447 absl::Substitute(" result($0, 0) = omega;", kNumModules * 4 + 2));
justinT21446e4f62024-06-16 22:36:10 -0700448
justinT21446e4f62024-06-16 22:36:10 -0700449 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700450 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700451 ccode(*full_accel_.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -0700452 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700453 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700454 ccode(*full_accel_.get(1, 0))));
455 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
456 kNumModules * 4 + 5,
457 ccode(*full_angular_accel_)));
justinT21446e4f62024-06-16 22:36:10 -0700458
459 result_cc.emplace_back(
460 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 6));
461 result_cc.emplace_back(
462 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 7));
463 result_cc.emplace_back(
464 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 8));
465
466 result_cc.emplace_back("");
467 result_cc.emplace_back(" return result;");
468 result_cc.emplace_back("}");
469 result_cc.emplace_back("");
470 result_cc.emplace_back("} // namespace frc971::control_loops::swerve");
471
472 aos::util::WriteStringToFileOrDie(cc_path, absl::StrJoin(result_cc, "\n"));
473 aos::util::WriteStringToFileOrDie(h_path, absl::StrJoin(result_h, "\n"));
474 }
475
Austin Schuhb67a38f2024-07-04 13:48:38 -0700476 void WriteCasadiVariables(std::vector<std::string> *result_py) {
477 result_py->emplace_back(" sin = casadi.sin");
478 result_py->emplace_back(" cos = casadi.cos");
Austin Schuh78a1b312024-08-18 17:21:34 -0700479 result_py->emplace_back(" exp = casadi.exp");
Austin Schuh5371c802024-09-02 13:18:48 -0700480 if (absl::GetFlag(FLAGS_function)) {
481 result_py->emplace_back(" atan2 = soft_atan2()");
482 } else {
483 result_py->emplace_back(" atan2 = soft_atan2");
484 }
Austin Schuh2a1abec2024-07-10 20:31:16 -0700485 result_py->emplace_back(" fmax = casadi.fmax");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700486 result_py->emplace_back(" fabs = casadi.fabs");
487
488 // Start by writing out variables matching each of the symbol names we use
489 // so we don't have to modify the computed equations too much.
490 for (size_t m = 0; m < kNumModules; ++m) {
491 result_py->emplace_back(
492 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 4));
493 result_py->emplace_back(
494 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 4 + 2));
495 result_py->emplace_back(
496 absl::Substitute(" omegad$0 = X[$1, 0]", m, m * 4 + 3));
497 }
498
499 result_py->emplace_back(
500 absl::Substitute(" theta = X[$0, 0]", kNumModules * 4 + 2));
501 result_py->emplace_back(
502 absl::Substitute(" vx = X[$0, 0]", kNumModules * 4 + 3));
503 result_py->emplace_back(
504 absl::Substitute(" vy = X[$0, 0]", kNumModules * 4 + 4));
505 result_py->emplace_back(
506 absl::Substitute(" omega = X[$0, 0]", kNumModules * 4 + 5));
507
508 result_py->emplace_back(
509 absl::Substitute(" fx = X[$0, 0]", kNumModules * 4 + 6));
510 result_py->emplace_back(
511 absl::Substitute(" fy = X[$0, 0]", kNumModules * 4 + 7));
512 result_py->emplace_back(
513 absl::Substitute(" moment = X[$0, 0]", kNumModules * 4 + 8));
514
515 // Now do the same for the inputs.
516 for (size_t m = 0; m < kNumModules; ++m) {
517 result_py->emplace_back(
518 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
519 result_py->emplace_back(
520 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
521 }
522 }
523
Austin Schuh6ea789e2024-07-27 13:45:53 -0700524 void WriteCasadiVelocityVariables(std::vector<std::string> *result_py) {
525 result_py->emplace_back(" sin = casadi.sin");
Austin Schuh78a1b312024-08-18 17:21:34 -0700526 result_py->emplace_back(" exp = casadi.exp");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700527 result_py->emplace_back(" cos = casadi.cos");
Austin Schuh5371c802024-09-02 13:18:48 -0700528 if (absl::GetFlag(FLAGS_function)) {
529 result_py->emplace_back(" atan2 = soft_atan2()");
530 } else {
531 result_py->emplace_back(" atan2 = soft_atan2");
532 }
Austin Schuh6ea789e2024-07-27 13:45:53 -0700533 result_py->emplace_back(" fmax = casadi.fmax");
534 result_py->emplace_back(" fabs = casadi.fabs");
535
536 // Start by writing out variables matching each of the symbol names we use
537 // so we don't have to modify the computed equations too much.
538 for (size_t m = 0; m < kNumModules; ++m) {
539 result_py->emplace_back(
540 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 2 + 0));
541 result_py->emplace_back(
542 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 2 + 1));
543 }
544
545 result_py->emplace_back(
546 absl::Substitute(" theta = X[$0, 0]", kNumModules * 2 + 0));
547 result_py->emplace_back(
548 absl::Substitute(" vx = X[$0, 0]", kNumModules * 2 + 1));
549 result_py->emplace_back(
550 absl::Substitute(" vy = X[$0, 0]", kNumModules * 2 + 2));
551 result_py->emplace_back(
552 absl::Substitute(" omega = X[$0, 0]", kNumModules * 2 + 3));
553
554 // result_py->emplace_back(
555 // absl::Substitute(" fx = X[$0, 0]", kNumModules * 3 + 4));
556 // result_py->emplace_back(
557 // absl::Substitute(" fy = X[$0, 0]", kNumModules * 3 + 5));
558 // result_py->emplace_back(
559 // absl::Substitute(" moment = X[$0, 0]", kNumModules * 3 + 6));
560 //
561 result_py->emplace_back(" fx = 0");
562 result_py->emplace_back(" fy = 0");
563 result_py->emplace_back(" moment = 0");
564
565 // Now do the same for the inputs.
566 for (size_t m = 0; m < kNumModules; ++m) {
567 result_py->emplace_back(
568 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
569 result_py->emplace_back(
570 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
571 }
572 }
573
Austin Schuha9550c02024-10-19 13:48:10 -0700574 void WriteConstantsFile(std::string_view path) {
575 std::vector<std::string> result_py;
576
577 // Write out the header.
578 result_py.emplace_back("#!/usr/bin/env python3");
579 result_py.emplace_back("");
580
581 WriteConstants(&result_py);
582
583 aos::util::WriteStringToFileOrDie(path, absl::StrJoin(result_py, "\n"));
584 }
585
586 void WriteConstants(std::vector<std::string> *result_py) {
587 result_py->emplace_back(absl::Substitute("WHEEL_RADIUS = $0", ccode(*rw_)));
588 result_py->emplace_back(
589 absl::Substitute("ROBOT_WIDTH = $0", ccode(*robot_width_)));
590 result_py->emplace_back(absl::Substitute("CASTER = $0", ccode(*caster_)));
591 result_py->emplace_back("STATE_THETAS0 = 0");
592 result_py->emplace_back("STATE_THETAD0 = 1");
593 result_py->emplace_back("STATE_OMEGAS0 = 2");
594 result_py->emplace_back("STATE_OMEGAD0 = 3");
595 result_py->emplace_back("STATE_THETAS1 = 4");
596 result_py->emplace_back("STATE_THETAD1 = 5");
597 result_py->emplace_back("STATE_OMEGAS1 = 6");
598 result_py->emplace_back("STATE_OMEGAD1 = 7");
599 result_py->emplace_back("STATE_THETAS2 = 8");
600 result_py->emplace_back("STATE_THETAD2 = 9");
601 result_py->emplace_back("STATE_OMEGAS2 = 10");
602 result_py->emplace_back("STATE_OMEGAD2 = 11");
603 result_py->emplace_back("STATE_THETAS3 = 12");
604 result_py->emplace_back("STATE_THETAD3 = 13");
605 result_py->emplace_back("STATE_OMEGAS3 = 14");
606 result_py->emplace_back("STATE_OMEGAD3 = 15");
607 result_py->emplace_back("STATE_X = 16");
608 result_py->emplace_back("STATE_Y = 17");
609 result_py->emplace_back("STATE_THETA = 18");
610 result_py->emplace_back("STATE_VX = 19");
611 result_py->emplace_back("STATE_VY = 20");
612 result_py->emplace_back("STATE_OMEGA = 21");
613 result_py->emplace_back("STATE_FX = 22");
614 result_py->emplace_back("STATE_FY = 23");
615 result_py->emplace_back("STATE_MOMENT = 24");
616 result_py->emplace_back("NUM_STATES = 25");
617 result_py->emplace_back("");
618 result_py->emplace_back("VELOCITY_STATE_THETAS0 = 0");
619 result_py->emplace_back("VELOCITY_STATE_OMEGAS0 = 1");
620 result_py->emplace_back("VELOCITY_STATE_THETAS1 = 2");
621 result_py->emplace_back("VELOCITY_STATE_OMEGAS1 = 3");
622 result_py->emplace_back("VELOCITY_STATE_THETAS2 = 4");
623 result_py->emplace_back("VELOCITY_STATE_OMEGAS2 = 5");
624 result_py->emplace_back("VELOCITY_STATE_THETAS3 = 6");
625 result_py->emplace_back("VELOCITY_STATE_OMEGAS3 = 7");
626 result_py->emplace_back("VELOCITY_STATE_THETA = 8");
627 result_py->emplace_back("VELOCITY_STATE_VX = 9");
628 result_py->emplace_back("VELOCITY_STATE_VY = 10");
629 result_py->emplace_back("VELOCITY_STATE_OMEGA = 11");
630 // result_py->emplace_back("VELOCITY_STATE_FX = 16");
631 // result_py->emplace_back("VELOCITY_STATE_FY = 17");
632 // result_py->emplace_back("VELOCITY_STATE_MOMENT = 18");
633 result_py->emplace_back("NUM_VELOCITY_STATES = 12");
634 result_py->emplace_back("");
635 result_py->emplace_back("");
636 result_py->emplace_back("# Is = STEER_CURRENT_COUPLING_FACTOR * Id");
637 result_py->emplace_back(absl::Substitute(
638 "STEER_CURRENT_COUPLING_FACTOR = $0",
639 ccode(*(neg(
640 mul(div(Gs_, Kts_),
641 mul(div(Ktd_, mul(Gd_, rw_)),
642 neg(mul(add(neg(wb_), mul(add(rs_, rp_),
643 sub(integer(1), div(rb1_, rp_)))),
644 div(rw_, rb2_))))))))));
645 result_py->emplace_back("");
646 }
647
Austin Schuh0f881092024-06-28 15:36:48 -0700648 // Writes the physics out to the provided .cc and .h path.
649 void WriteCasadi(std::string_view py_path) {
650 std::vector<std::string> result_py;
651
652 // Write out the header.
Austin Schuh5371c802024-09-02 13:18:48 -0700653 result_py.emplace_back("#!/usr/bin/env python3");
Austin Schuh0f881092024-06-28 15:36:48 -0700654 result_py.emplace_back("");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700655 result_py.emplace_back("import casadi, numpy");
Austin Schuh0f881092024-06-28 15:36:48 -0700656 result_py.emplace_back("");
Austin Schuha9550c02024-10-19 13:48:10 -0700657
658 WriteConstants(&result_py);
659
Austin Schuh6ea789e2024-07-27 13:45:53 -0700660 result_py.emplace_back("def to_velocity_state(X):");
661 result_py.emplace_back(" return numpy.array([");
662 result_py.emplace_back(" [X[STATE_THETAS0, 0]],");
663 result_py.emplace_back(" [X[STATE_OMEGAS0, 0]],");
664 result_py.emplace_back(" [X[STATE_THETAS1, 0]],");
665 result_py.emplace_back(" [X[STATE_OMEGAS1, 0]],");
666 result_py.emplace_back(" [X[STATE_THETAS2, 0]],");
667 result_py.emplace_back(" [X[STATE_OMEGAS2, 0]],");
668 result_py.emplace_back(" [X[STATE_THETAS3, 0]],");
669 result_py.emplace_back(" [X[STATE_OMEGAS3, 0]],");
670 result_py.emplace_back(" [X[STATE_THETA, 0]],");
671 result_py.emplace_back(" [X[STATE_VX, 0]],");
672 result_py.emplace_back(" [X[STATE_VY, 0]],");
673 result_py.emplace_back(" [X[STATE_OMEGA, 0]],");
674 // result_py.emplace_back(" [X[STATE_FX, 0]],");
675 // result_py.emplace_back(" [X[STATE_FY, 0]],");
676 // result_py.emplace_back(" [X[STATE_MOMENT, 0]],");
677 result_py.emplace_back(" ])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700678 result_py.emplace_back("");
Austin Schuhbd75c482024-08-18 00:03:51 -0700679 constexpr double kLogGain = 1.0 / 0.05;
Austin Schuh5371c802024-09-02 13:18:48 -0700680 constexpr double kAbsGain = 1.0 / 0.01;
681 if (absl::GetFlag(FLAGS_function)) {
682 result_py.emplace_back("def soft_atan2():");
683 result_py.emplace_back(" y = casadi.SX.sym('y')");
684 result_py.emplace_back(" x = casadi.SX.sym('x')");
685 result_py.emplace_back(
686 " return casadi.Function('soft_atan2', [y, x], [");
687 result_py.emplace_back(" casadi.arctan2(");
688 result_py.emplace_back(" y,");
689 result_py.emplace_back(" casadi.logsumexp(");
690 result_py.emplace_back(" casadi.SX(");
691 result_py.emplace_back(" numpy.array([");
692 result_py.emplace_back(" 1.0, x * (1.0 - 2.0 /");
693 result_py.emplace_back(
694 absl::Substitute(" (1 + "
695 "casadi.exp($1.0 * x))) * $0.0",
696 kLogGain, kAbsGain));
697 result_py.emplace_back(
698 absl::Substitute(" ]))) / $0.0)", kLogGain));
699 result_py.emplace_back(" ])");
700 } else {
701 result_py.emplace_back("def soft_atan2(y, x):");
702 result_py.emplace_back(" return casadi.arctan2(");
703 result_py.emplace_back(" y,");
704 result_py.emplace_back(" casadi.logsumexp(casadi.SX(numpy.array(");
705 result_py.emplace_back(
706 absl::Substitute(" [1.0, x * (1.0 - 2.0 / (1 + "
707 "casadi.exp($1.0 * x))) * $0.0]))) / $0.0)",
708 kLogGain, kAbsGain));
709 }
Austin Schuh2a1abec2024-07-10 20:31:16 -0700710
Austin Schuh0f881092024-06-28 15:36:48 -0700711 result_py.emplace_back("# Returns the derivative of our state vector");
712 result_py.emplace_back("# [thetas0, thetad0, omegas0, omegad0,");
713 result_py.emplace_back("# thetas1, thetad1, omegas1, omegad1,");
714 result_py.emplace_back("# thetas2, thetad2, omegas2, omegad2,");
715 result_py.emplace_back("# thetas3, thetad3, omegas3, omegad3,");
716 result_py.emplace_back("# x, y, theta, vx, vy, omega,");
717 result_py.emplace_back("# Fx, Fy, Moment]");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700718 result_py.emplace_back("def swerve_full_dynamics(X, U):");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700719 WriteCasadiVariables(&result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700720
721 result_py.emplace_back("");
722 result_py.emplace_back(" result = casadi.SX.sym('result', 25, 1)");
723 result_py.emplace_back("");
724
725 // And then write out the derivative of each state.
726 for (size_t m = 0; m < kNumModules; ++m) {
727 result_py.emplace_back(
728 absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m));
729 result_py.emplace_back(
730 absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m));
731
Austin Schuh6ea789e2024-07-27 13:45:53 -0700732 result_py.emplace_back(
733 absl::Substitute(" result[$0, 0] = $1", m * 4 + 2,
734 ccode(*modules_[m].full.alphas_eqn)));
735 result_py.emplace_back(
736 absl::Substitute(" result[$0, 0] = $1", m * 4 + 3,
737 ccode(*modules_[m].full.alphad_eqn)));
Austin Schuh0f881092024-06-28 15:36:48 -0700738 }
739
740 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700741 absl::Substitute(" result[$0, 0] = vx", kNumModules * 4 + 0));
Austin Schuh0f881092024-06-28 15:36:48 -0700742 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700743 absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1));
Austin Schuh0f881092024-06-28 15:36:48 -0700744 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700745 absl::Substitute(" result[$0, 0] = omega", kNumModules * 4 + 2));
Austin Schuh0f881092024-06-28 15:36:48 -0700746
Austin Schuh0f881092024-06-28 15:36:48 -0700747 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700748 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700749 ccode(*full_accel_.get(0, 0))));
Austin Schuh0f881092024-06-28 15:36:48 -0700750 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700751 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700752 ccode(*full_accel_.get(1, 0))));
753 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
754 kNumModules * 4 + 5,
755 ccode(*full_angular_accel_)));
Austin Schuh0f881092024-06-28 15:36:48 -0700756
757 result_py.emplace_back(
758 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6));
759 result_py.emplace_back(
760 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7));
761 result_py.emplace_back(
762 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8));
763
764 result_py.emplace_back("");
765 result_py.emplace_back(
766 " return casadi.Function('xdot', [X, U], [result])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700767
Austin Schuh6ea789e2024-07-27 13:45:53 -0700768 result_py.emplace_back("");
769
770 result_py.emplace_back("# Returns the derivative of our state vector");
771 result_py.emplace_back("# [thetas0, omegas0,");
772 result_py.emplace_back("# thetas1, omegas1,");
773 result_py.emplace_back("# thetas2, omegas2,");
774 result_py.emplace_back("# thetas3, omegas3,");
775 result_py.emplace_back("# theta, vx, vy, omega]");
776 result_py.emplace_back("def velocity_swerve_physics(X, U):");
777 WriteCasadiVelocityVariables(&result_py);
778
779 result_py.emplace_back("");
780 result_py.emplace_back(
781 " result = casadi.SX.sym('result', NUM_VELOCITY_STATES, 1)");
782 result_py.emplace_back("");
783
784 // And then write out the derivative of each state.
785 for (size_t m = 0; m < kNumModules; ++m) {
786 result_py.emplace_back(
787 absl::Substitute(" result[$0, 0] = omegas$1", m * 2 + 0, m));
788 result_py.emplace_back(
789 absl::Substitute(" result[$0, 0] = $1", m * 2 + 1,
790 ccode(*modules_[m].direct.alphas_eqn)));
791 }
792 result_py.emplace_back(
793 absl::Substitute(" result[$0, 0] = omega", kNumModules * 2 + 0));
794
795 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
796 kNumModules * 2 + 1,
797 ccode(*direct_accel_.get(0, 0))));
798 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
799 kNumModules * 2 + 2,
800 ccode(*direct_accel_.get(1, 0))));
801 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
802 kNumModules * 2 + 3,
803 ccode(*direct_angular_accel_)));
804
805 // result_py.emplace_back(
806 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 4));
807 // result_py.emplace_back(
808 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 5));
809 // result_py.emplace_back(
810 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 6));
811
812 result_py.emplace_back("");
813 result_py.emplace_back(
814 " return casadi.Function('xdot', [X, U], [result])");
815
Austin Schuhb8b34be2024-07-14 16:06:19 -0700816 DefineVector2dFunction(
817 "contact_patch_velocity",
818 "# Returns the velocity of the wheel in global coordinates.",
819 [](const Module &m, int dimension) {
820 return ccode(*m.contact_patch_velocity.get(dimension, 0));
821 },
822 &result_py);
823 DefineVector2dFunction(
824 "wheel_ground_velocity",
825 "# Returns the velocity of the wheel in steer module coordinates.",
826 [](const Module &m, int dimension) {
827 return ccode(*m.wheel_ground_velocity.get(dimension, 0));
828 },
829 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700830
Austin Schuhb8b34be2024-07-14 16:06:19 -0700831 DefineVector2dFunction(
832 "wheel_slip_velocity",
833 "# Returns the difference in velocities of the wheel surface and the "
834 "ground.",
835 [](const Module &m, int dimension) {
836 return ccode(*m.wheel_slip_velocity.get(dimension, 0));
837 },
838 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700839
Austin Schuhb8b34be2024-07-14 16:06:19 -0700840 DefineScalarFunction(
841 "slip_angle", "Returns the slip angle of the ith wheel",
842 [](const Module &m) { return ccode(*m.slip_angle); }, &result_py);
843 DefineScalarFunction(
844 "slip_ratio", "Returns the slip ratio of the ith wheel",
845 [](const Module &m) { return ccode(*m.slip_ratio); }, &result_py);
846 DefineScalarFunction(
847 "module_angular_accel",
848 "Returns the angular acceleration of the robot due to the ith wheel",
Austin Schuh6ea789e2024-07-27 13:45:53 -0700849 [this](const Module &m) { return ccode(*div(m.full.torque, Js_)); },
850 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700851
Austin Schuhb8b34be2024-07-14 16:06:19 -0700852 DefineVector2dFunction(
853 "wheel_force",
854 "Returns the force on the wheel in steer module coordinates",
855 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700856 return ccode(
857 *std::vector<RCP<const Basic>>{m.full.Fwx, m.Fwy}[dimension]);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700858 },
859 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700860
Austin Schuhb8b34be2024-07-14 16:06:19 -0700861 DefineVector2dFunction(
862 "F", "Returns the force on the wheel in absolute coordinates",
863 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700864 return ccode(*m.full.F.get(dimension, 0));
Austin Schuhb8b34be2024-07-14 16:06:19 -0700865 },
866 &result_py);
867
justinT21820767f2024-09-11 19:57:55 -0700868 DefineVector2dVelocityFunction(
869 "F_vel",
870 "Returns the force on the wheel in absolute coordinates based on the "
871 "velocity controller",
872 [](const Module &m, int dimension) {
873 return ccode(*m.direct.F.get(dimension, 0));
874 },
875 &result_py);
876
877 DefineVector2dVelocityFunction(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700878 "mounting_location",
879 "Returns the mounting location of wheel in robot coordinates",
880 [](const Module &m, int dimension) {
881 return ccode(*m.mounting_location.get(dimension, 0));
882 },
883 &result_py);
884
Austin Schuh5dac2292024-10-19 13:56:58 -0700885 DefineVector2dVelocityFunction(
886 "rotated_mounting_location",
887 "Returns the mounting location of wheel in field aligned coordinates",
888 [](const Module &m, int dimension) {
889 return ccode(*m.rotated_mounting_location.get(dimension, 0));
890 },
891 &result_py);
892
Austin Schuhb8b34be2024-07-14 16:06:19 -0700893 DefineScalarFunction(
894 "Ms", "Returns the self aligning moment of the ith wheel",
895 [this](const Module &m) {
896 return ccode(*(div(m.Ms, add(Jsm_, div(div(Js_, Gs_), Gs_)))));
897 },
898 &result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700899
900 aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n"));
901 }
902
Austin Schuhb8b34be2024-07-14 16:06:19 -0700903 void DefineScalarFunction(
904 std::string_view name, std::string_view documentation,
905 std::function<std::string(const Module &)> scalar_fn,
906 std::vector<std::string> *result_py) {
907 result_py->emplace_back("");
908 result_py->emplace_back(absl::Substitute("# $0.", documentation));
909 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
910 WriteCasadiVariables(result_py);
911 for (size_t m = 0; m < kNumModules; ++m) {
912 if (m == 0) {
913 result_py->emplace_back(" if i == 0:");
914 } else {
915 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
916 }
917 result_py->emplace_back(
918 absl::Substitute(" return casadi.Function('$0', [X, U], [$1])",
919 name, scalar_fn(modules_[m])));
920 }
921 result_py->emplace_back(" raise ValueError(\"Invalid module number\")");
922 }
923
924 void DefineVector2dFunction(
925 std::string_view name, std::string_view documentation,
926 std::function<std::string(const Module &, int)> scalar_fn,
927 std::vector<std::string> *result_py) {
928 result_py->emplace_back("");
929 result_py->emplace_back(absl::Substitute("# $0.", documentation));
930 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
931 WriteCasadiVariables(result_py);
932 result_py->emplace_back(
933 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
934 for (size_t m = 0; m < kNumModules; ++m) {
935 if (m == 0) {
936 result_py->emplace_back(" if i == 0:");
937 } else {
938 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
939 }
940 for (int j = 0; j < 2; ++j) {
941 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
942 j, scalar_fn(modules_[m], j)));
943 }
944 }
945 result_py->emplace_back(" else:");
946 result_py->emplace_back(
947 " raise ValueError(\"Invalid module number\")");
948 result_py->emplace_back(absl::Substitute(
949 " return casadi.Function('$0', [X, U], [result])", name));
950 }
951
justinT21820767f2024-09-11 19:57:55 -0700952 void DefineVector2dVelocityFunction(
953 std::string_view name, std::string_view documentation,
954 std::function<std::string(const Module &, int)> scalar_fn,
955 std::vector<std::string> *result_py) {
956 result_py->emplace_back("");
957 result_py->emplace_back(absl::Substitute("# $0.", documentation));
958 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
959 WriteCasadiVelocityVariables(result_py);
960 result_py->emplace_back(
961 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
962 for (size_t m = 0; m < kNumModules; ++m) {
963 if (m == 0) {
964 result_py->emplace_back(" if i == 0:");
965 } else {
966 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
967 }
968 for (int j = 0; j < 2; ++j) {
969 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
970 j, scalar_fn(modules_[m], j)));
971 }
972 }
973 result_py->emplace_back(" else:");
974 result_py->emplace_back(
975 " raise ValueError(\"Invalid module number\")");
976 result_py->emplace_back(absl::Substitute(
977 " return casadi.Function('$0', [X, U], [result])", name));
978 }
979
justinT21446e4f62024-06-16 22:36:10 -0700980 private:
981 static constexpr uint8_t kNumModules = 4;
982
Austin Schuh6ea789e2024-07-27 13:45:53 -0700983 RCP<const Basic> SteerAccel(RCP<const Basic> Fwx, RCP<const Basic> Ms,
984 RCP<const Basic> Is) {
985 RCP<const Basic> lhms =
986 mul(add(neg(wb_), mul(add(rs_, rp_), sub(integer(1), div(rb1_, rp_)))),
987 mul(div(rw_, rb2_), neg(Fwx)));
988 RCP<const Basic> lhs = add(add(Ms, div(mul(Kts_, Is), Gs_)), lhms);
989 RCP<const Basic> rhs = add(Jsm_, div(div(Js_, Gs_), Gs_));
990 return simplify(div(lhs, rhs));
991 }
992
justinT21446e4f62024-06-16 22:36:10 -0700993 Module ModulePhysics(const int m, DenseMatrix mounting_location) {
994 VLOG(1) << "Solving module " << m;
995
996 Module result;
Austin Schuhb8b34be2024-07-14 16:06:19 -0700997 result.mounting_location = mounting_location;
justinT21446e4f62024-06-16 22:36:10 -0700998
999 result.Is = symbol(absl::StrFormat("Is%u", m));
1000 result.Id = symbol(absl::StrFormat("Id%u", m));
1001
1002 RCP<const Symbol> thetamd = symbol(absl::StrFormat("theta_md%u", m));
1003 RCP<const Symbol> omegamd = symbol(absl::StrFormat("omega_md%u", m));
1004 RCP<const Symbol> alphamd = symbol(absl::StrFormat("alpha_md%u", m));
1005
1006 result.thetas = symbol(absl::StrFormat("thetas%u", m));
1007 result.omegas = symbol(absl::StrFormat("omegas%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -07001008 RCP<const Symbol> alphas = symbol(absl::StrFormat("alphas%u", m));
justinT21446e4f62024-06-16 22:36:10 -07001009
justinT21446e4f62024-06-16 22:36:10 -07001010 result.omegad = symbol(absl::StrFormat("omegad%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -07001011 RCP<const Symbol> alphad = symbol(absl::StrFormat("alphad%u", m));
justinT21446e4f62024-06-16 22:36:10 -07001012
1013 // Velocity of the module in field coordinates
Austin Schuh2a1abec2024-07-10 20:31:16 -07001014 DenseMatrix robot_velocity = DenseMatrix(2, 1, {vx_, vy_});
justinT21446e4f62024-06-16 22:36:10 -07001015 VLOG(1) << "robot velocity: " << robot_velocity.__str__();
1016
1017 // Velocity of the contact patch in field coordinates
1018 DenseMatrix temp_matrix = DenseMatrix(2, 1);
1019 DenseMatrix temp_matrix2 = DenseMatrix(2, 1);
Austin Schuhbd75c482024-08-18 00:03:51 -07001020 DenseMatrix temp_matrix3 = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -07001021 result.contact_patch_velocity = DenseMatrix(2, 1);
justinT21446e4f62024-06-16 22:36:10 -07001022
Austin Schuhb8b34be2024-07-14 16:06:19 -07001023 mul_dense_dense(R(theta_), result.mounting_location, temp_matrix);
justinT21446e4f62024-06-16 22:36:10 -07001024 add_dense_dense(angle_cross(temp_matrix, omega_), robot_velocity,
1025 temp_matrix2);
1026 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuhbd75c482024-08-18 00:03:51 -07001027 DenseMatrix(2, 1, {neg(caster_), integer(0)}),
1028 temp_matrix3);
justinT21446e4f62024-06-16 22:36:10 -07001029 add_dense_dense(temp_matrix2,
Austin Schuhbd75c482024-08-18 00:03:51 -07001030 angle_cross(temp_matrix3, add(omega_, result.omegas)),
Austin Schuh2a1abec2024-07-10 20:31:16 -07001031 result.contact_patch_velocity);
justinT21446e4f62024-06-16 22:36:10 -07001032
1033 VLOG(1);
Austin Schuh2a1abec2024-07-10 20:31:16 -07001034 VLOG(1) << "contact patch velocity: "
1035 << result.contact_patch_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -07001036
1037 // Relative velocity of the surface of the wheel to the ground.
Austin Schuhb67a38f2024-07-04 13:48:38 -07001038 result.wheel_ground_velocity = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -07001039 mul_dense_dense(R(neg(add(result.thetas, theta_))),
1040 result.contact_patch_velocity,
Austin Schuhb67a38f2024-07-04 13:48:38 -07001041 result.wheel_ground_velocity);
justinT21446e4f62024-06-16 22:36:10 -07001042
Austin Schuhb8b34be2024-07-14 16:06:19 -07001043 // Compute the relative velocity between the wheel surface and the ground in
1044 // the wheel coordinate system.
1045 result.wheel_slip_velocity = DenseMatrix(2, 1);
1046 DenseMatrix wheel_velocity =
1047 DenseMatrix(2, 1, {mul(rw_, result.omegad), integer(0)});
1048 DenseMatrix negative_wheel_ground_velocity =
1049 DenseMatrix(2, 1,
1050 {neg(result.wheel_ground_velocity.get(0, 0)),
1051 neg(result.wheel_ground_velocity.get(1, 0))});
1052 add_dense_dense(negative_wheel_ground_velocity, wheel_velocity,
1053 result.wheel_slip_velocity);
1054
justinT21446e4f62024-06-16 22:36:10 -07001055 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001056 VLOG(1) << "wheel ground velocity: "
1057 << result.wheel_ground_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -07001058
Austin Schuh5ddcb472024-07-21 17:55:34 -07001059 result.slip_angle = sin(neg(atan2(result.wheel_ground_velocity.get(1, 0),
1060 result.wheel_ground_velocity.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -07001061
1062 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001063 VLOG(1) << "slip angle: " << result.slip_angle->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001064
Austin Schuh2a1abec2024-07-10 20:31:16 -07001065 // TODO(austin): Does this handle decel properly?
Austin Schuhb67a38f2024-07-04 13:48:38 -07001066 result.slip_ratio = div(
Austin Schuh2a1abec2024-07-10 20:31:16 -07001067 sub(mul(rw_, result.omegad), result.wheel_ground_velocity.get(0, 0)),
1068 SymEngine::max(
1069 {real_double(0.02), abs(result.wheel_ground_velocity.get(0, 0))}));
justinT21446e4f62024-06-16 22:36:10 -07001070 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001071 VLOG(1) << "Slip ratio " << result.slip_ratio->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001072
Austin Schuh6ea789e2024-07-27 13:45:53 -07001073 result.full.Fwx = simplify(mul(Cx_, result.slip_ratio));
Austin Schuhb67a38f2024-07-04 13:48:38 -07001074 result.Fwy = simplify(mul(Cy_, result.slip_angle));
justinT21446e4f62024-06-16 22:36:10 -07001075
Austin Schuh27694fa2024-07-20 16:29:49 -07001076 // The self-aligning moment needs to flip when the module flips direction.
Austin Schuh78a1b312024-08-18 17:21:34 -07001077 RCP<const Basic> softsign_velocity = add(
1078 div(integer(-2),
1079 add(integer(1), exp(mul(integer(100),
1080 result.wheel_ground_velocity.get(0, 0))))),
1081 integer(1));
1082 result.Ms =
1083 mul(neg(result.Fwy),
1084 add(div(mul(softsign_velocity, contact_patch_length_), integer(3)),
1085 caster_));
justinT21446e4f62024-06-16 22:36:10 -07001086 VLOG(1);
Austin Schuhb8b34be2024-07-14 16:06:19 -07001087 VLOG(1) << "Ms " << result.Ms->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001088 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001089 VLOG(1) << "full.Fwx " << result.full.Fwx->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001090 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001091 VLOG(1) << "Fwy " << result.Fwy->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001092
Austin Schuh6ea789e2024-07-27 13:45:53 -07001093 // -K_td * Id / Gd + Fwx * rw = 0
1094 // Fwx = K_td * Id / Gd / rw
1095 result.direct.Fwx = mul(Ktd_, div(result.Id, mul(Gd_, rw_)));
1096
1097 result.direct.alphas_eqn =
1098 SteerAccel(result.direct.Fwx, result.Ms, result.Is);
1099
1100 // d/dt omegas = ...
1101 result.full.alphas_eqn = SteerAccel(result.full.Fwx, result.Ms, result.Is);
justinT21446e4f62024-06-16 22:36:10 -07001102
1103 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001104 VLOG(1) << alphas->__str__() << " = " << result.full.alphas_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001105
Austin Schuh6ea789e2024-07-27 13:45:53 -07001106 RCP<const Basic> lhs =
1107 sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), alphas),
1108 mul(Gd1_, mul(Gd2_, alphamd)));
1109 RCP<const Basic> ddplanitary_eqn = sub(mul(Gd3_, lhs), alphad);
justinT21446e4f62024-06-16 22:36:10 -07001110
Austin Schuh6ea789e2024-07-27 13:45:53 -07001111 RCP<const Basic> full_drive_eqn =
1112 sub(add(mul(neg(Jdm_), div(alphamd, Gd_)),
1113 mul(Ktd_, div(neg(result.Id), Gd_))),
1114 mul(neg(result.full.Fwx), rw_));
justinT21446e4f62024-06-16 22:36:10 -07001115
Austin Schuh6ea789e2024-07-27 13:45:53 -07001116 VLOG(1) << "full_drive_eqn: " << full_drive_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001117
1118 // Substitute in ddplanitary_eqn so we get rid of alphamd
1119 map_basic_basic map;
1120 RCP<const Set> reals = interval(NegInf, Inf, true, true);
1121 RCP<const Set> solve_solution = solve(ddplanitary_eqn, alphamd, reals);
1122 map[alphamd] = solve_solution->get_args()[1]->get_args()[0];
1123 VLOG(1) << "temp: " << solve_solution->__str__();
Austin Schuh6ea789e2024-07-27 13:45:53 -07001124 RCP<const Basic> drive_eqn_subs = full_drive_eqn->subs(map);
justinT21446e4f62024-06-16 22:36:10 -07001125
1126 map.clear();
Austin Schuh6ea789e2024-07-27 13:45:53 -07001127 map[alphas] = result.full.alphas_eqn;
justinT21446e4f62024-06-16 22:36:10 -07001128 RCP<const Basic> drive_eqn_subs2 = drive_eqn_subs->subs(map);
1129 RCP<const Basic> drive_eqn_subs3 = simplify(drive_eqn_subs2);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001130 VLOG(1) << "full_drive_eqn simplified: " << drive_eqn_subs3->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001131
Austin Schuh6ea789e2024-07-27 13:45:53 -07001132 solve_solution = solve(drive_eqn_subs3, alphad, reals);
justinT21446e4f62024-06-16 22:36:10 -07001133
Austin Schuh6ea789e2024-07-27 13:45:53 -07001134 result.full.alphad_eqn =
justinT21446e4f62024-06-16 22:36:10 -07001135 simplify(solve_solution->get_args()[1]->get_args()[0]);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001136 VLOG(1) << "drive_accel: " << result.full.alphad_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001137
Austin Schuh2a1abec2024-07-10 20:31:16 -07001138 // Compute the resulting force from the module.
Austin Schuh6ea789e2024-07-27 13:45:53 -07001139 result.full.F = DenseMatrix(2, 1);
Austin Schuhb8b34be2024-07-14 16:06:19 -07001140 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuh6ea789e2024-07-27 13:45:53 -07001141 DenseMatrix(2, 1, {result.full.Fwx, result.Fwy}),
1142 result.full.F);
justinT21d18f79f2024-09-22 19:43:05 -07001143
Austin Schuh5dac2292024-10-19 13:56:58 -07001144 result.rotated_mounting_location = DenseMatrix(2, 1);
justinT21d18f79f2024-09-22 19:43:05 -07001145 mul_dense_dense(R(theta_), result.mounting_location,
Austin Schuh5dac2292024-10-19 13:56:58 -07001146 result.rotated_mounting_location);
1147 result.full.torque =
1148 force_cross(result.rotated_mounting_location, result.full.F);
justinT21446e4f62024-06-16 22:36:10 -07001149
Austin Schuh6ea789e2024-07-27 13:45:53 -07001150 result.direct.F = DenseMatrix(2, 1);
1151 mul_dense_dense(R(add(theta_, result.thetas)),
1152 DenseMatrix(2, 1, {result.direct.Fwx, result.Fwy}),
1153 result.direct.F);
1154 result.direct.torque =
Austin Schuh5dac2292024-10-19 13:56:58 -07001155 force_cross(result.rotated_mounting_location, result.direct.F);
justinT21446e4f62024-06-16 22:36:10 -07001156
1157 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001158 VLOG(1) << "full torque = " << result.full.torque->__str__();
1159 VLOG(1) << "direct torque = " << result.full.torque->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001160
justinT21446e4f62024-06-16 22:36:10 -07001161 return result;
1162 }
1163
1164 DenseMatrix R(const RCP<const Basic> theta) {
1165 return DenseMatrix(2, 2,
1166 {cos(theta), neg(sin(theta)), sin(theta), cos(theta)});
1167 }
1168
1169 DenseMatrix angle_cross(DenseMatrix a, RCP<const Basic> b) {
Austin Schuh2a1abec2024-07-10 20:31:16 -07001170 return DenseMatrix(2, 1, {mul(neg(a.get(1, 0)), b), mul(a.get(0, 0), b)});
justinT21446e4f62024-06-16 22:36:10 -07001171 }
1172
1173 RCP<const Basic> force_cross(DenseMatrix r, DenseMatrix f) {
1174 return sub(mul(r.get(0, 0), f.get(1, 0)), mul(r.get(1, 0), f.get(0, 0)));
1175 }
1176
1177 // z represents the number of teeth per gear, theta is the angle between
1178 // shafts(in degrees), D_02 is the pitch diameter of gear 2 and b_2 is the
1179 // length of the tooth of gear 2
1180 // returns std::pair(r_01, r_02)
1181 std::pair<double, double> GetBevelPitchRadius(double z1, double z2,
1182 double theta, double D_02,
1183 double b_2) {
1184 double gamma_1 = std::atan2(z1, z2);
1185 double gamma_2 = theta / 180.0 * std::numbers::pi - gamma_1;
1186 double R_m = D_02 / 2 / std::sin(gamma_2) - b_2 / 2;
1187 return std::pair(R_m * std::cos(gamma_2), R_m * std::sin(gamma_2));
1188 }
1189
1190 Motor drive_motor_;
1191 Motor steer_motor_;
1192
1193 RCP<const Basic> Cx_;
1194 RCP<const Basic> Cy_;
Austin Schuh2a1abec2024-07-10 20:31:16 -07001195 RCP<const Basic> rw_;
justinT21446e4f62024-06-16 22:36:10 -07001196 RCP<const Basic> m_;
1197 RCP<const Basic> J_;
1198 RCP<const Basic> Gd1_;
1199 RCP<const Basic> rs_;
1200 RCP<const Basic> rp_;
1201 RCP<const Basic> Gd2_;
1202 RCP<const Basic> rb1_;
1203 RCP<const Basic> rb2_;
1204 RCP<const Basic> Gd3_;
1205 RCP<const Basic> Gd_;
1206 RCP<const Basic> Js_;
1207 RCP<const Basic> Gs_;
1208 RCP<const Basic> wb_;
1209 RCP<const Basic> Jdm_;
1210 RCP<const Basic> Jsm_;
1211 RCP<const Basic> Kts_;
1212 RCP<const Basic> Ktd_;
1213 RCP<const Basic> robot_width_;
1214 RCP<const Basic> caster_;
1215 RCP<const Basic> contact_patch_length_;
1216 RCP<const Basic> x_;
1217 RCP<const Basic> y_;
1218 RCP<const Basic> theta_;
1219 RCP<const Basic> vx_;
1220 RCP<const Basic> vy_;
1221 RCP<const Basic> omega_;
1222 RCP<const Basic> ax_;
1223 RCP<const Basic> ay_;
1224 RCP<const Basic> atheta_;
1225
1226 std::array<Module, kNumModules> modules_;
1227
Austin Schuh6ea789e2024-07-27 13:45:53 -07001228 DenseMatrix full_accel_;
1229 RCP<const Basic> full_angular_accel_;
1230 DenseMatrix direct_accel_;
1231 RCP<const Basic> direct_angular_accel_;
justinT21446e4f62024-06-16 22:36:10 -07001232};
1233
1234} // namespace frc971::control_loops::swerve
1235
1236int main(int argc, char **argv) {
1237 aos::InitGoogle(&argc, &argv);
1238
1239 frc971::control_loops::swerve::SwerveSimulation sim;
1240
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001241 if (!absl::GetFlag(FLAGS_cc_output_path).empty() &&
1242 !absl::GetFlag(FLAGS_h_output_path).empty()) {
1243 sim.Write(absl::GetFlag(FLAGS_cc_output_path),
1244 absl::GetFlag(FLAGS_h_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001245 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001246 if (!absl::GetFlag(FLAGS_casadi_py_output_path).empty()) {
1247 sim.WriteCasadi(absl::GetFlag(FLAGS_casadi_py_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001248 }
justinT21446e4f62024-06-16 22:36:10 -07001249
Austin Schuha9550c02024-10-19 13:48:10 -07001250 if (!absl::GetFlag(FLAGS_constants_output_path).empty()) {
1251 sim.WriteConstantsFile(absl::GetFlag(FLAGS_constants_output_path));
1252 }
1253
justinT21446e4f62024-06-16 22:36:10 -07001254 return 0;
1255}