blob: 7ea3db5a62a4f76c98a4f58780c145e473925c9e [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;
73
justinT21446e4f62024-06-16 22:36:10 -070074 RCP<const Symbol> Is;
75
76 RCP<const Symbol> Id;
77
78 RCP<const Symbol> thetas;
79 RCP<const Symbol> omegas;
justinT21446e4f62024-06-16 22:36:10 -070080
justinT21446e4f62024-06-16 22:36:10 -070081 RCP<const Symbol> omegad;
justinT21446e4f62024-06-16 22:36:10 -070082
Austin Schuh2a1abec2024-07-10 20:31:16 -070083 DenseMatrix contact_patch_velocity;
Austin Schuhb67a38f2024-07-04 13:48:38 -070084 DenseMatrix wheel_ground_velocity;
Austin Schuhb8b34be2024-07-14 16:06:19 -070085 DenseMatrix wheel_slip_velocity;
Austin Schuhb67a38f2024-07-04 13:48:38 -070086 RCP<const Basic> slip_angle;
87 RCP<const Basic> slip_ratio;
88
Austin Schuhb8b34be2024-07-14 16:06:19 -070089 RCP<const Basic> Ms;
Austin Schuhb67a38f2024-07-04 13:48:38 -070090 RCP<const Basic> Fwy;
91
Austin Schuh6ea789e2024-07-27 13:45:53 -070092 struct Full {
93 RCP<const Basic> Fwx;
94 DenseMatrix F;
95
96 RCP<const Basic> torque;
97
98 RCP<const Basic> alphas_eqn;
99 RCP<const Basic> alphad_eqn;
100 } full;
101
102 struct Direct {
103 RCP<const Basic> Fwx;
104 DenseMatrix F;
105
106 RCP<const Basic> torque;
107
108 RCP<const Basic> alphas_eqn;
109 } direct;
justinT21446e4f62024-06-16 22:36:10 -0700110};
111
Austin Schuh6ea789e2024-07-27 13:45:53 -0700112DenseMatrix SumMatrices(DenseMatrix a) { return a; }
113
114template <typename... Args>
115DenseMatrix SumMatrices(DenseMatrix a, Args... args) {
116 DenseMatrix result = DenseMatrix(2, 1, {integer(0), integer(0)});
117
118 DenseMatrix b = SumMatrices(args...);
119 add_dense_dense(a, b, result);
120 return result;
121}
122
justinT21446e4f62024-06-16 22:36:10 -0700123class SwerveSimulation {
124 public:
125 SwerveSimulation() : drive_motor_(KrakenFOC()), steer_motor_(KrakenFOC()) {
126 auto fx = symbol("fx");
127 auto fy = symbol("fy");
128 auto moment = symbol("moment");
129
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700130 if (absl::GetFlag(FLAGS_symbolic)) {
justinT21446e4f62024-06-16 22:36:10 -0700131 Cx_ = symbol("Cx");
132 Cy_ = symbol("Cy");
133
Austin Schuh2a1abec2024-07-10 20:31:16 -0700134 rw_ = symbol("rw");
justinT21446e4f62024-06-16 22:36:10 -0700135
136 m_ = symbol("m");
137 J_ = symbol("J");
138
139 Gd1_ = symbol("Gd1");
140 rs_ = symbol("rs");
141 rp_ = symbol("rp");
142 Gd2_ = symbol("Gd2");
143
144 rb1_ = symbol("rb1");
145 rb2_ = symbol("rb2");
146
Austin Schuh76534f32024-09-02 13:52:45 -0700147 Gd3_ = symbol("Gd3");
justinT21446e4f62024-06-16 22:36:10 -0700148 Gd_ = symbol("Gd");
149
150 Js_ = symbol("Js");
151
152 Gs_ = symbol("Gs");
153 wb_ = symbol("wb");
154
155 Jdm_ = symbol("Jdm");
156 Jsm_ = symbol("Jsm");
157 Kts_ = symbol("Kts");
158 Ktd_ = symbol("Ktd");
159
160 robot_width_ = symbol("robot_width");
161
162 caster_ = symbol("caster");
163 contact_patch_length_ = symbol("Lcp");
164 } else {
Austin Schuhb8b34be2024-07-14 16:06:19 -0700165 Cx_ = real_double(25.0 * 9.8 / 4.0 / 0.05);
justinT21446e4f62024-06-16 22:36:10 -0700166 Cy_ = real_double(5 * 9.8 / 0.05 / 4.0);
167
Austin Schuh2a1abec2024-07-10 20:31:16 -0700168 rw_ = real_double(2 * 0.0254);
justinT21446e4f62024-06-16 22:36:10 -0700169
170 m_ = real_double(25.0); // base is 20 kg without battery
171 J_ = real_double(6.0);
172
173 Gd1_ = real_double(12.0 / 42.0);
174 rs_ = real_double(28.0 / 20.0 / 2.0);
175 rp_ = real_double(18.0 / 20.0 / 2.0);
176 Gd2_ = div(rs_, rp_);
177
178 // 15 / 45 bevel ratio, calculated using python script ported over to
179 // GetBevelPitchRadius(double
180 // TODO(Justin): Use the function instead of computed constantss
181 rb1_ = real_double(0.3805473);
182 rb2_ = real_double(1.14164);
183
184 Gd3_ = div(rb1_, rb2_);
185 Gd_ = mul(mul(Gd1_, Gd2_), Gd3_);
186
Austin Schuhb8b34be2024-07-14 16:06:19 -0700187 Js_ = real_double(0.001);
justinT21446e4f62024-06-16 22:36:10 -0700188
189 Gs_ = real_double(35.0 / 468.0);
190 wb_ = real_double(0.725);
191
192 Jdm_ = real_double(drive_motor_.motor_inertia);
193 Jsm_ = real_double(steer_motor_.motor_inertia);
194 Kts_ = real_double(steer_motor_.Kt);
195 Ktd_ = real_double(drive_motor_.Kt);
196
197 robot_width_ = real_double(24.75 * 0.0254);
198
Austin Schuh27694fa2024-07-20 16:29:49 -0700199 caster_ = real_double(absl::GetFlag(FLAGS_caster));
justinT21446e4f62024-06-16 22:36:10 -0700200 contact_patch_length_ = real_double(0.02);
201 }
202
203 x_ = symbol("x");
204 y_ = symbol("y");
205 theta_ = symbol("theta");
206
207 vx_ = symbol("vx");
208 vy_ = symbol("vy");
209 omega_ = symbol("omega");
210
211 ax_ = symbol("ax");
212 ay_ = symbol("ay");
213 atheta_ = symbol("atheta");
214
215 // Now, compute the accelerations due to the disturbance forces.
justinT21446e4f62024-06-16 22:36:10 -0700216 DenseMatrix external_accel = DenseMatrix(2, 1, {div(fx, m_), div(fy, m_)});
Austin Schuh6ea789e2024-07-27 13:45:53 -0700217 DenseMatrix external_force = DenseMatrix(2, 1, {fx, fy});
justinT21446e4f62024-06-16 22:36:10 -0700218
219 // And compute the physics contributions from each module.
220 modules_[0] = ModulePhysics(
221 0, DenseMatrix(
222 2, 1,
223 {div(robot_width_, integer(2)), div(robot_width_, integer(2))}));
224 modules_[1] =
225 ModulePhysics(1, DenseMatrix(2, 1,
226 {div(robot_width_, integer(-2)),
227 div(robot_width_, integer(2))}));
228 modules_[2] =
229 ModulePhysics(2, DenseMatrix(2, 1,
230 {div(robot_width_, integer(-2)),
231 div(robot_width_, integer(-2))}));
232 modules_[3] =
233 ModulePhysics(3, DenseMatrix(2, 1,
234 {div(robot_width_, integer(2)),
235 div(robot_width_, integer(-2))}));
236
237 // And convert them into the overall robot contribution.
Austin Schuh6ea789e2024-07-27 13:45:53 -0700238 DenseMatrix net_full_force =
239 SumMatrices(modules_[0].full.F, modules_[1].full.F, modules_[2].full.F,
240 modules_[3].full.F, external_force);
justinT21446e4f62024-06-16 22:36:10 -0700241
Austin Schuh6ea789e2024-07-27 13:45:53 -0700242 DenseMatrix net_direct_force =
243 SumMatrices(modules_[0].direct.F, modules_[1].direct.F,
244 modules_[2].direct.F, modules_[3].direct.F, external_force);
justinT21446e4f62024-06-16 22:36:10 -0700245
Austin Schuh6ea789e2024-07-27 13:45:53 -0700246 full_accel_ = DenseMatrix(2, 1);
247 mul_dense_scalar(net_full_force, pow(m_, minus_one), full_accel_);
justinT21446e4f62024-06-16 22:36:10 -0700248
Austin Schuh6ea789e2024-07-27 13:45:53 -0700249 full_angular_accel_ = div(
250 add(moment, add(add(modules_[0].full.torque, modules_[1].full.torque),
251 add(modules_[2].full.torque, modules_[3].full.torque))),
252 J_);
justinT21446e4f62024-06-16 22:36:10 -0700253
Austin Schuh6ea789e2024-07-27 13:45:53 -0700254 direct_accel_ = DenseMatrix(2, 1);
255 mul_dense_scalar(net_direct_force, pow(m_, minus_one), direct_accel_);
justinT21942892b2024-07-02 22:33:50 -0700256
Austin Schuh6ea789e2024-07-27 13:45:53 -0700257 direct_angular_accel_ =
258 div(add(moment,
259 add(add(modules_[0].direct.torque, modules_[1].direct.torque),
260 add(modules_[2].direct.torque, modules_[3].direct.torque))),
261 J_);
justinT21942892b2024-07-02 22:33:50 -0700262
Austin Schuh6ea789e2024-07-27 13:45:53 -0700263 VLOG(1) << "accel(0, 0) = " << ccode(*full_accel_.get(0, 0));
264 VLOG(1) << "accel(1, 0) = " << ccode(*full_accel_.get(1, 0));
265 VLOG(1) << "angular_accel = " << ccode(*full_angular_accel_);
justinT21942892b2024-07-02 22:33:50 -0700266 }
267
justinT21446e4f62024-06-16 22:36:10 -0700268 // Writes the physics out to the provided .cc and .h path.
269 void Write(std::string_view cc_path, std::string_view h_path) {
270 std::vector<std::string> result_cc;
271 std::vector<std::string> result_h;
272
Austin Schuh0f881092024-06-28 15:36:48 -0700273 std::string_view include_guard_stripped = h_path;
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700274 CHECK(absl::ConsumePrefix(&include_guard_stripped,
275 absl::GetFlag(FLAGS_output_base)));
justinT21446e4f62024-06-16 22:36:10 -0700276 std::string include_guard =
277 absl::StrReplaceAll(absl::AsciiStrToUpper(include_guard_stripped),
278 {{"/", "_"}, {".", "_"}});
279
280 // Write out the header.
281 result_h.emplace_back(absl::Substitute("#ifndef $0_", include_guard));
282 result_h.emplace_back(absl::Substitute("#define $0_", include_guard));
283 result_h.emplace_back("");
284 result_h.emplace_back("#include <Eigen/Dense>");
285 result_h.emplace_back("");
286 result_h.emplace_back("namespace frc971::control_loops::swerve {");
287 result_h.emplace_back("");
James Kuszmaulef14ab42024-09-14 15:05:24 -0700288 result_h.emplace_back("struct FullDynamicsStates {");
289 result_h.emplace_back("enum States {");
290 result_h.emplace_back(" kThetas0 = 0,");
291 result_h.emplace_back(" kThetad0 = 1,");
292 result_h.emplace_back(" kOmegas0 = 2,");
293 result_h.emplace_back(" kOmegad0 = 3,");
294 result_h.emplace_back(" kThetas1 = 4,");
295 result_h.emplace_back(" kThetad1 = 5,");
296 result_h.emplace_back(" kOmegas1 = 6,");
297 result_h.emplace_back(" kOmegad1 = 7,");
298 result_h.emplace_back(" kThetas2 = 8,");
299 result_h.emplace_back(" kThetad2 = 9,");
300 result_h.emplace_back(" kOmegas2 = 10,");
301 result_h.emplace_back(" kOmegad2 = 11,");
302 result_h.emplace_back(" kThetas3 = 12,");
303 result_h.emplace_back(" kThetad3 = 13,");
304 result_h.emplace_back(" kOmegas3 = 14,");
305 result_h.emplace_back(" kOmegad3 = 15,");
306 result_h.emplace_back(" kX = 16,");
307 result_h.emplace_back(" kY = 17,");
308 result_h.emplace_back(" kTheta = 18,");
309 result_h.emplace_back(" kVx = 19,");
310 result_h.emplace_back(" kVy = 20,");
311 result_h.emplace_back(" kOmega = 21,");
312 result_h.emplace_back(" kFx = 22,");
313 result_h.emplace_back(" kFy = 23,");
314 result_h.emplace_back(" kMoment = 24,");
315 result_h.emplace_back(" kNumStates");
316 result_h.emplace_back("};");
317 result_h.emplace_back("};");
318 result_h.emplace_back(
319 "inline constexpr size_t kNumFullDynamicsStates = "
320 "static_cast<size_t>(FullDynamicsStates::kNumStates);");
321 result_h.emplace_back("struct VelocityStates {");
322 result_h.emplace_back("enum States {");
323 result_h.emplace_back(" kThetas0 = 0,");
324 result_h.emplace_back(" kOmegas0 = 1,");
325 result_h.emplace_back(" kThetas1 = 2,");
326 result_h.emplace_back(" kOmegas1 = 3,");
327 result_h.emplace_back(" kThetas2 = 4,");
328 result_h.emplace_back(" kOmegas2 = 5,");
329 result_h.emplace_back(" kThetas3 = 6,");
330 result_h.emplace_back(" kOmegas3 = 7,");
331 result_h.emplace_back(" kTheta = 8,");
332 result_h.emplace_back(" kVx = 9,");
333 result_h.emplace_back(" kVy = 10,");
334 result_h.emplace_back(" kOmega = 11,");
335 result_h.emplace_back(" kNumStates");
336 result_h.emplace_back("};");
337 result_h.emplace_back("};");
338 result_h.emplace_back(
339 "inline constexpr size_t kNumVelocityStates = "
340 "static_cast<size_t>(VelocityStates::kNumStates);");
341 result_h.emplace_back("struct Inputs {");
342 result_h.emplace_back("enum States {");
343 result_h.emplace_back(" kIs0 = 0,");
344 result_h.emplace_back(" kId0 = 1,");
345 result_h.emplace_back(" kIs1 = 2,");
346 result_h.emplace_back(" kId1 = 3,");
347 result_h.emplace_back(" kIs2 = 4,");
348 result_h.emplace_back(" kId2 = 5,");
349 result_h.emplace_back(" kIs3 = 6,");
350 result_h.emplace_back(" kId3 = 7,");
351 result_h.emplace_back(" kNumInputs = 8");
352 result_h.emplace_back("};");
353 result_h.emplace_back("};");
354 result_h.emplace_back(
355 "inline constexpr size_t kNumInputs = "
356 "static_cast<size_t>(Inputs::kNumInputs);");
357 result_h.emplace_back("");
justinT21446e4f62024-06-16 22:36:10 -0700358 result_h.emplace_back("// Returns the derivative of our state vector");
justinT21446e4f62024-06-16 22:36:10 -0700359 result_h.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700360 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> SwervePhysics(");
justinT21446e4f62024-06-16 22:36:10 -0700361 result_h.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700362 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
363 "1>> X,");
364 result_h.emplace_back(
365 " Eigen::Ref<const Eigen::Matrix<double, kNumInputs, 1>> U);");
366 result_h.emplace_back("");
367 result_h.emplace_back(
368 "Eigen::Matrix<double, kNumVelocityStates, 1> ToVelocityState(");
369 result_h.emplace_back(
370 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
371 "1>> X);");
372 result_h.emplace_back("");
373 result_h.emplace_back(
374 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> FromVelocityState(");
375 result_h.emplace_back(
376 " Eigen::Ref<const Eigen::Matrix<double, kNumVelocityStates, 1>> "
377 "X);");
378 result_h.emplace_back("");
379 result_h.emplace_back(
380 "inline Eigen::Matrix<double, kNumVelocityStates, 1> VelocityPhysics(");
381 result_h.emplace_back(
382 " Eigen::Ref<const Eigen::Matrix<double, kNumVelocityStates, 1>> "
383 "X,");
384 result_h.emplace_back(
385 " Eigen::Ref<const Eigen::Matrix<double, kNumInputs, 1>> U) {");
386 result_h.emplace_back(
387 " return ToVelocityState(SwervePhysics(FromVelocityState(X), U));");
388 result_h.emplace_back("}");
justinT21446e4f62024-06-16 22:36:10 -0700389 result_h.emplace_back("");
390 result_h.emplace_back("} // namespace frc971::control_loops::swerve");
391 result_h.emplace_back("");
392 result_h.emplace_back(absl::Substitute("#endif // $0_", include_guard));
393
394 // Write out the .cc
395 result_cc.emplace_back(
396 absl::Substitute("#include \"$0\"", include_guard_stripped));
397 result_cc.emplace_back("");
398 result_cc.emplace_back("#include <cmath>");
399 result_cc.emplace_back("");
400 result_cc.emplace_back("namespace frc971::control_loops::swerve {");
401 result_cc.emplace_back("");
justinT21446e4f62024-06-16 22:36:10 -0700402 result_cc.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700403 "Eigen::Matrix<double, kNumVelocityStates, 1> ToVelocityState(");
justinT21446e4f62024-06-16 22:36:10 -0700404 result_cc.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700405 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
406 "1>> X) {");
407 result_cc.emplace_back(
408 " Eigen::Matrix<double, kNumVelocityStates, 1> velocity;");
409 const std::vector<std::string_view> velocity_states = {
410 "kThetas0", "kOmegas0", "kThetas1", "kOmegas1", "kThetas2", "kOmegas2",
411 "kThetas3", "kOmegas3", "kTheta", "kVx", "kVy", "kOmega"};
412 for (const std::string_view velocity_state : velocity_states) {
413 result_cc.emplace_back(absl::StrFormat(
414 " velocity(VelocityStates::%s) = X(FullDynamicsStates::%s);",
415 velocity_state, velocity_state));
416 }
417 result_cc.emplace_back(" return velocity;");
418 result_cc.emplace_back("}");
419 result_cc.emplace_back("");
420 result_cc.emplace_back(
421 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> FromVelocityState(");
422 result_cc.emplace_back(
423 " Eigen::Ref<const Eigen::Matrix<double, kNumVelocityStates, 1>> X) "
424 "{");
425 result_cc.emplace_back(
426 " Eigen::Matrix<double, kNumFullDynamicsStates, 1> full;");
427 result_cc.emplace_back(" full.setZero();");
428 for (const std::string_view velocity_state : velocity_states) {
429 result_cc.emplace_back(absl::StrFormat(
430 " full(FullDynamicsStates::%s) = X(VelocityStates::%s);",
431 velocity_state, velocity_state));
432 }
433 result_cc.emplace_back(" return full;");
434 result_cc.emplace_back("}");
435 result_cc.emplace_back("");
436 result_cc.emplace_back(
437 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> SwervePhysics(");
438 result_cc.emplace_back(
439 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
440 "1>> X,");
441 result_cc.emplace_back(
442 " Eigen::Ref<const Eigen::Matrix<double, kNumInputs, 1>> U) {");
443 result_cc.emplace_back(
444 " Eigen::Matrix<double, kNumFullDynamicsStates, 1> result;");
justinT21446e4f62024-06-16 22:36:10 -0700445
446 // Start by writing out variables matching each of the symbol names we use
447 // so we don't have to modify the computed equations too much.
448 for (size_t m = 0; m < kNumModules; ++m) {
449 result_cc.emplace_back(
450 absl::Substitute(" const double thetas$0 = X($1, 0);", m, m * 4));
451 result_cc.emplace_back(absl::Substitute(
452 " const double omegas$0 = X($1, 0);", m, m * 4 + 2));
453 result_cc.emplace_back(absl::Substitute(
454 " const double omegad$0 = X($1, 0);", m, m * 4 + 3));
455 }
456
457 result_cc.emplace_back(absl::Substitute(" const double theta = X($0, 0);",
458 kNumModules * 4 + 2));
459 result_cc.emplace_back(
460 absl::Substitute(" const double vx = X($0, 0);", kNumModules * 4 + 3));
461 result_cc.emplace_back(
462 absl::Substitute(" const double vy = X($0, 0);", kNumModules * 4 + 4));
463 result_cc.emplace_back(absl::Substitute(" const double omega = X($0, 0);",
464 kNumModules * 4 + 5));
465
466 result_cc.emplace_back(
467 absl::Substitute(" const double fx = X($0, 0);", kNumModules * 4 + 6));
468 result_cc.emplace_back(
469 absl::Substitute(" const double fy = X($0, 0);", kNumModules * 4 + 7));
470 result_cc.emplace_back(absl::Substitute(" const double moment = X($0, 0);",
471 kNumModules * 4 + 8));
472
473 // Now do the same for the inputs.
474 for (size_t m = 0; m < kNumModules; ++m) {
475 result_cc.emplace_back(
476 absl::Substitute(" const double Is$0 = U($1, 0);", m, m * 2));
477 result_cc.emplace_back(
478 absl::Substitute(" const double Id$0 = U($1, 0);", m, m * 2 + 1));
479 }
480
481 result_cc.emplace_back("");
482
483 // And then write out the derivative of each state.
484 for (size_t m = 0; m < kNumModules; ++m) {
485 result_cc.emplace_back(
486 absl::Substitute(" result($0, 0) = omegas$1;", m * 4, m));
487 result_cc.emplace_back(
488 absl::Substitute(" result($0, 0) = omegad$1;", m * 4 + 1, m));
489
Austin Schuh6ea789e2024-07-27 13:45:53 -0700490 result_cc.emplace_back(
491 absl::Substitute(" result($0, 0) = $1;", m * 4 + 2,
492 ccode(*modules_[m].full.alphas_eqn)));
493 result_cc.emplace_back(
494 absl::Substitute(" result($0, 0) = $1;", m * 4 + 3,
495 ccode(*modules_[m].full.alphad_eqn)));
justinT21446e4f62024-06-16 22:36:10 -0700496 }
497
498 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700499 absl::Substitute(" result($0, 0) = vx;", kNumModules * 4 + 0));
justinT21446e4f62024-06-16 22:36:10 -0700500 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700501 absl::Substitute(" result($0, 0) = vy;", kNumModules * 4 + 1));
justinT21446e4f62024-06-16 22:36:10 -0700502 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700503 absl::Substitute(" result($0, 0) = omega;", kNumModules * 4 + 2));
justinT21446e4f62024-06-16 22:36:10 -0700504
justinT21446e4f62024-06-16 22:36:10 -0700505 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700506 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700507 ccode(*full_accel_.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -0700508 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700509 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700510 ccode(*full_accel_.get(1, 0))));
511 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
512 kNumModules * 4 + 5,
513 ccode(*full_angular_accel_)));
justinT21446e4f62024-06-16 22:36:10 -0700514
515 result_cc.emplace_back(
516 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 6));
517 result_cc.emplace_back(
518 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 7));
519 result_cc.emplace_back(
520 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 8));
521
522 result_cc.emplace_back("");
523 result_cc.emplace_back(" return result;");
524 result_cc.emplace_back("}");
525 result_cc.emplace_back("");
526 result_cc.emplace_back("} // namespace frc971::control_loops::swerve");
527
528 aos::util::WriteStringToFileOrDie(cc_path, absl::StrJoin(result_cc, "\n"));
529 aos::util::WriteStringToFileOrDie(h_path, absl::StrJoin(result_h, "\n"));
530 }
531
Austin Schuhb67a38f2024-07-04 13:48:38 -0700532 void WriteCasadiVariables(std::vector<std::string> *result_py) {
533 result_py->emplace_back(" sin = casadi.sin");
534 result_py->emplace_back(" cos = casadi.cos");
Austin Schuh78a1b312024-08-18 17:21:34 -0700535 result_py->emplace_back(" exp = casadi.exp");
Austin Schuh5371c802024-09-02 13:18:48 -0700536 if (absl::GetFlag(FLAGS_function)) {
537 result_py->emplace_back(" atan2 = soft_atan2()");
538 } else {
539 result_py->emplace_back(" atan2 = soft_atan2");
540 }
Austin Schuh2a1abec2024-07-10 20:31:16 -0700541 result_py->emplace_back(" fmax = casadi.fmax");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700542 result_py->emplace_back(" fabs = casadi.fabs");
543
544 // Start by writing out variables matching each of the symbol names we use
545 // so we don't have to modify the computed equations too much.
546 for (size_t m = 0; m < kNumModules; ++m) {
547 result_py->emplace_back(
548 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 4));
549 result_py->emplace_back(
550 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 4 + 2));
551 result_py->emplace_back(
552 absl::Substitute(" omegad$0 = X[$1, 0]", m, m * 4 + 3));
553 }
554
555 result_py->emplace_back(
556 absl::Substitute(" theta = X[$0, 0]", kNumModules * 4 + 2));
557 result_py->emplace_back(
558 absl::Substitute(" vx = X[$0, 0]", kNumModules * 4 + 3));
559 result_py->emplace_back(
560 absl::Substitute(" vy = X[$0, 0]", kNumModules * 4 + 4));
561 result_py->emplace_back(
562 absl::Substitute(" omega = X[$0, 0]", kNumModules * 4 + 5));
563
564 result_py->emplace_back(
565 absl::Substitute(" fx = X[$0, 0]", kNumModules * 4 + 6));
566 result_py->emplace_back(
567 absl::Substitute(" fy = X[$0, 0]", kNumModules * 4 + 7));
568 result_py->emplace_back(
569 absl::Substitute(" moment = X[$0, 0]", kNumModules * 4 + 8));
570
571 // Now do the same for the inputs.
572 for (size_t m = 0; m < kNumModules; ++m) {
573 result_py->emplace_back(
574 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
575 result_py->emplace_back(
576 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
577 }
578 }
579
Austin Schuh6ea789e2024-07-27 13:45:53 -0700580 void WriteCasadiVelocityVariables(std::vector<std::string> *result_py) {
581 result_py->emplace_back(" sin = casadi.sin");
Austin Schuh78a1b312024-08-18 17:21:34 -0700582 result_py->emplace_back(" exp = casadi.exp");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700583 result_py->emplace_back(" cos = casadi.cos");
Austin Schuh5371c802024-09-02 13:18:48 -0700584 if (absl::GetFlag(FLAGS_function)) {
585 result_py->emplace_back(" atan2 = soft_atan2()");
586 } else {
587 result_py->emplace_back(" atan2 = soft_atan2");
588 }
Austin Schuh6ea789e2024-07-27 13:45:53 -0700589 result_py->emplace_back(" fmax = casadi.fmax");
590 result_py->emplace_back(" fabs = casadi.fabs");
591
592 // Start by writing out variables matching each of the symbol names we use
593 // so we don't have to modify the computed equations too much.
594 for (size_t m = 0; m < kNumModules; ++m) {
595 result_py->emplace_back(
596 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 2 + 0));
597 result_py->emplace_back(
598 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 2 + 1));
599 }
600
601 result_py->emplace_back(
602 absl::Substitute(" theta = X[$0, 0]", kNumModules * 2 + 0));
603 result_py->emplace_back(
604 absl::Substitute(" vx = X[$0, 0]", kNumModules * 2 + 1));
605 result_py->emplace_back(
606 absl::Substitute(" vy = X[$0, 0]", kNumModules * 2 + 2));
607 result_py->emplace_back(
608 absl::Substitute(" omega = X[$0, 0]", kNumModules * 2 + 3));
609
610 // result_py->emplace_back(
611 // absl::Substitute(" fx = X[$0, 0]", kNumModules * 3 + 4));
612 // result_py->emplace_back(
613 // absl::Substitute(" fy = X[$0, 0]", kNumModules * 3 + 5));
614 // result_py->emplace_back(
615 // absl::Substitute(" moment = X[$0, 0]", kNumModules * 3 + 6));
616 //
617 result_py->emplace_back(" fx = 0");
618 result_py->emplace_back(" fy = 0");
619 result_py->emplace_back(" moment = 0");
620
621 // Now do the same for the inputs.
622 for (size_t m = 0; m < kNumModules; ++m) {
623 result_py->emplace_back(
624 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
625 result_py->emplace_back(
626 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
627 }
628 }
629
Austin Schuha9550c02024-10-19 13:48:10 -0700630 void WriteConstantsFile(std::string_view path) {
631 std::vector<std::string> result_py;
632
633 // Write out the header.
634 result_py.emplace_back("#!/usr/bin/env python3");
635 result_py.emplace_back("");
636
637 WriteConstants(&result_py);
638
639 aos::util::WriteStringToFileOrDie(path, absl::StrJoin(result_py, "\n"));
640 }
641
642 void WriteConstants(std::vector<std::string> *result_py) {
643 result_py->emplace_back(absl::Substitute("WHEEL_RADIUS = $0", ccode(*rw_)));
644 result_py->emplace_back(
645 absl::Substitute("ROBOT_WIDTH = $0", ccode(*robot_width_)));
646 result_py->emplace_back(absl::Substitute("CASTER = $0", ccode(*caster_)));
647 result_py->emplace_back("STATE_THETAS0 = 0");
648 result_py->emplace_back("STATE_THETAD0 = 1");
649 result_py->emplace_back("STATE_OMEGAS0 = 2");
650 result_py->emplace_back("STATE_OMEGAD0 = 3");
651 result_py->emplace_back("STATE_THETAS1 = 4");
652 result_py->emplace_back("STATE_THETAD1 = 5");
653 result_py->emplace_back("STATE_OMEGAS1 = 6");
654 result_py->emplace_back("STATE_OMEGAD1 = 7");
655 result_py->emplace_back("STATE_THETAS2 = 8");
656 result_py->emplace_back("STATE_THETAD2 = 9");
657 result_py->emplace_back("STATE_OMEGAS2 = 10");
658 result_py->emplace_back("STATE_OMEGAD2 = 11");
659 result_py->emplace_back("STATE_THETAS3 = 12");
660 result_py->emplace_back("STATE_THETAD3 = 13");
661 result_py->emplace_back("STATE_OMEGAS3 = 14");
662 result_py->emplace_back("STATE_OMEGAD3 = 15");
663 result_py->emplace_back("STATE_X = 16");
664 result_py->emplace_back("STATE_Y = 17");
665 result_py->emplace_back("STATE_THETA = 18");
666 result_py->emplace_back("STATE_VX = 19");
667 result_py->emplace_back("STATE_VY = 20");
668 result_py->emplace_back("STATE_OMEGA = 21");
669 result_py->emplace_back("STATE_FX = 22");
670 result_py->emplace_back("STATE_FY = 23");
671 result_py->emplace_back("STATE_MOMENT = 24");
672 result_py->emplace_back("NUM_STATES = 25");
673 result_py->emplace_back("");
674 result_py->emplace_back("VELOCITY_STATE_THETAS0 = 0");
675 result_py->emplace_back("VELOCITY_STATE_OMEGAS0 = 1");
676 result_py->emplace_back("VELOCITY_STATE_THETAS1 = 2");
677 result_py->emplace_back("VELOCITY_STATE_OMEGAS1 = 3");
678 result_py->emplace_back("VELOCITY_STATE_THETAS2 = 4");
679 result_py->emplace_back("VELOCITY_STATE_OMEGAS2 = 5");
680 result_py->emplace_back("VELOCITY_STATE_THETAS3 = 6");
681 result_py->emplace_back("VELOCITY_STATE_OMEGAS3 = 7");
682 result_py->emplace_back("VELOCITY_STATE_THETA = 8");
683 result_py->emplace_back("VELOCITY_STATE_VX = 9");
684 result_py->emplace_back("VELOCITY_STATE_VY = 10");
685 result_py->emplace_back("VELOCITY_STATE_OMEGA = 11");
686 // result_py->emplace_back("VELOCITY_STATE_FX = 16");
687 // result_py->emplace_back("VELOCITY_STATE_FY = 17");
688 // result_py->emplace_back("VELOCITY_STATE_MOMENT = 18");
689 result_py->emplace_back("NUM_VELOCITY_STATES = 12");
690 result_py->emplace_back("");
691 result_py->emplace_back("");
692 result_py->emplace_back("# Is = STEER_CURRENT_COUPLING_FACTOR * Id");
693 result_py->emplace_back(absl::Substitute(
694 "STEER_CURRENT_COUPLING_FACTOR = $0",
695 ccode(*(neg(
696 mul(div(Gs_, Kts_),
697 mul(div(Ktd_, mul(Gd_, rw_)),
698 neg(mul(add(neg(wb_), mul(add(rs_, rp_),
699 sub(integer(1), div(rb1_, rp_)))),
700 div(rw_, rb2_))))))))));
701 result_py->emplace_back("");
702 }
703
Austin Schuh0f881092024-06-28 15:36:48 -0700704 // Writes the physics out to the provided .cc and .h path.
705 void WriteCasadi(std::string_view py_path) {
706 std::vector<std::string> result_py;
707
708 // Write out the header.
Austin Schuh5371c802024-09-02 13:18:48 -0700709 result_py.emplace_back("#!/usr/bin/env python3");
Austin Schuh0f881092024-06-28 15:36:48 -0700710 result_py.emplace_back("");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700711 result_py.emplace_back("import casadi, numpy");
Austin Schuh0f881092024-06-28 15:36:48 -0700712 result_py.emplace_back("");
Austin Schuha9550c02024-10-19 13:48:10 -0700713
714 WriteConstants(&result_py);
715
Austin Schuh6ea789e2024-07-27 13:45:53 -0700716 result_py.emplace_back("def to_velocity_state(X):");
717 result_py.emplace_back(" return numpy.array([");
718 result_py.emplace_back(" [X[STATE_THETAS0, 0]],");
719 result_py.emplace_back(" [X[STATE_OMEGAS0, 0]],");
720 result_py.emplace_back(" [X[STATE_THETAS1, 0]],");
721 result_py.emplace_back(" [X[STATE_OMEGAS1, 0]],");
722 result_py.emplace_back(" [X[STATE_THETAS2, 0]],");
723 result_py.emplace_back(" [X[STATE_OMEGAS2, 0]],");
724 result_py.emplace_back(" [X[STATE_THETAS3, 0]],");
725 result_py.emplace_back(" [X[STATE_OMEGAS3, 0]],");
726 result_py.emplace_back(" [X[STATE_THETA, 0]],");
727 result_py.emplace_back(" [X[STATE_VX, 0]],");
728 result_py.emplace_back(" [X[STATE_VY, 0]],");
729 result_py.emplace_back(" [X[STATE_OMEGA, 0]],");
730 // result_py.emplace_back(" [X[STATE_FX, 0]],");
731 // result_py.emplace_back(" [X[STATE_FY, 0]],");
732 // result_py.emplace_back(" [X[STATE_MOMENT, 0]],");
733 result_py.emplace_back(" ])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700734 result_py.emplace_back("");
Austin Schuhbd75c482024-08-18 00:03:51 -0700735 constexpr double kLogGain = 1.0 / 0.05;
Austin Schuh5371c802024-09-02 13:18:48 -0700736 constexpr double kAbsGain = 1.0 / 0.01;
737 if (absl::GetFlag(FLAGS_function)) {
738 result_py.emplace_back("def soft_atan2():");
739 result_py.emplace_back(" y = casadi.SX.sym('y')");
740 result_py.emplace_back(" x = casadi.SX.sym('x')");
741 result_py.emplace_back(
742 " return casadi.Function('soft_atan2', [y, x], [");
743 result_py.emplace_back(" casadi.arctan2(");
744 result_py.emplace_back(" y,");
745 result_py.emplace_back(" casadi.logsumexp(");
746 result_py.emplace_back(" casadi.SX(");
747 result_py.emplace_back(" numpy.array([");
748 result_py.emplace_back(" 1.0, x * (1.0 - 2.0 /");
749 result_py.emplace_back(
750 absl::Substitute(" (1 + "
751 "casadi.exp($1.0 * x))) * $0.0",
752 kLogGain, kAbsGain));
753 result_py.emplace_back(
754 absl::Substitute(" ]))) / $0.0)", kLogGain));
755 result_py.emplace_back(" ])");
756 } else {
757 result_py.emplace_back("def soft_atan2(y, x):");
758 result_py.emplace_back(" return casadi.arctan2(");
759 result_py.emplace_back(" y,");
760 result_py.emplace_back(" casadi.logsumexp(casadi.SX(numpy.array(");
761 result_py.emplace_back(
762 absl::Substitute(" [1.0, x * (1.0 - 2.0 / (1 + "
763 "casadi.exp($1.0 * x))) * $0.0]))) / $0.0)",
764 kLogGain, kAbsGain));
765 }
Austin Schuh2a1abec2024-07-10 20:31:16 -0700766
Austin Schuh0f881092024-06-28 15:36:48 -0700767 result_py.emplace_back("# Returns the derivative of our state vector");
768 result_py.emplace_back("# [thetas0, thetad0, omegas0, omegad0,");
769 result_py.emplace_back("# thetas1, thetad1, omegas1, omegad1,");
770 result_py.emplace_back("# thetas2, thetad2, omegas2, omegad2,");
771 result_py.emplace_back("# thetas3, thetad3, omegas3, omegad3,");
772 result_py.emplace_back("# x, y, theta, vx, vy, omega,");
773 result_py.emplace_back("# Fx, Fy, Moment]");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700774 result_py.emplace_back("def swerve_full_dynamics(X, U):");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700775 WriteCasadiVariables(&result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700776
777 result_py.emplace_back("");
778 result_py.emplace_back(" result = casadi.SX.sym('result', 25, 1)");
779 result_py.emplace_back("");
780
781 // And then write out the derivative of each state.
782 for (size_t m = 0; m < kNumModules; ++m) {
783 result_py.emplace_back(
784 absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m));
785 result_py.emplace_back(
786 absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m));
787
Austin Schuh6ea789e2024-07-27 13:45:53 -0700788 result_py.emplace_back(
789 absl::Substitute(" result[$0, 0] = $1", m * 4 + 2,
790 ccode(*modules_[m].full.alphas_eqn)));
791 result_py.emplace_back(
792 absl::Substitute(" result[$0, 0] = $1", m * 4 + 3,
793 ccode(*modules_[m].full.alphad_eqn)));
Austin Schuh0f881092024-06-28 15:36:48 -0700794 }
795
796 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700797 absl::Substitute(" result[$0, 0] = vx", kNumModules * 4 + 0));
Austin Schuh0f881092024-06-28 15:36:48 -0700798 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700799 absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1));
Austin Schuh0f881092024-06-28 15:36:48 -0700800 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700801 absl::Substitute(" result[$0, 0] = omega", kNumModules * 4 + 2));
Austin Schuh0f881092024-06-28 15:36:48 -0700802
Austin Schuh0f881092024-06-28 15:36:48 -0700803 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700804 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700805 ccode(*full_accel_.get(0, 0))));
Austin Schuh0f881092024-06-28 15:36:48 -0700806 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700807 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700808 ccode(*full_accel_.get(1, 0))));
809 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
810 kNumModules * 4 + 5,
811 ccode(*full_angular_accel_)));
Austin Schuh0f881092024-06-28 15:36:48 -0700812
813 result_py.emplace_back(
814 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6));
815 result_py.emplace_back(
816 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7));
817 result_py.emplace_back(
818 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8));
819
820 result_py.emplace_back("");
821 result_py.emplace_back(
822 " return casadi.Function('xdot', [X, U], [result])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700823
Austin Schuh6ea789e2024-07-27 13:45:53 -0700824 result_py.emplace_back("");
825
826 result_py.emplace_back("# Returns the derivative of our state vector");
827 result_py.emplace_back("# [thetas0, omegas0,");
828 result_py.emplace_back("# thetas1, omegas1,");
829 result_py.emplace_back("# thetas2, omegas2,");
830 result_py.emplace_back("# thetas3, omegas3,");
831 result_py.emplace_back("# theta, vx, vy, omega]");
832 result_py.emplace_back("def velocity_swerve_physics(X, U):");
833 WriteCasadiVelocityVariables(&result_py);
834
835 result_py.emplace_back("");
836 result_py.emplace_back(
837 " result = casadi.SX.sym('result', NUM_VELOCITY_STATES, 1)");
838 result_py.emplace_back("");
839
840 // And then write out the derivative of each state.
841 for (size_t m = 0; m < kNumModules; ++m) {
842 result_py.emplace_back(
843 absl::Substitute(" result[$0, 0] = omegas$1", m * 2 + 0, m));
844 result_py.emplace_back(
845 absl::Substitute(" result[$0, 0] = $1", m * 2 + 1,
846 ccode(*modules_[m].direct.alphas_eqn)));
847 }
848 result_py.emplace_back(
849 absl::Substitute(" result[$0, 0] = omega", kNumModules * 2 + 0));
850
851 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
852 kNumModules * 2 + 1,
853 ccode(*direct_accel_.get(0, 0))));
854 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
855 kNumModules * 2 + 2,
856 ccode(*direct_accel_.get(1, 0))));
857 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
858 kNumModules * 2 + 3,
859 ccode(*direct_angular_accel_)));
860
861 // result_py.emplace_back(
862 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 4));
863 // result_py.emplace_back(
864 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 5));
865 // result_py.emplace_back(
866 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 6));
867
868 result_py.emplace_back("");
869 result_py.emplace_back(
870 " return casadi.Function('xdot', [X, U], [result])");
871
Austin Schuhb8b34be2024-07-14 16:06:19 -0700872 DefineVector2dFunction(
873 "contact_patch_velocity",
874 "# Returns the velocity of the wheel in global coordinates.",
875 [](const Module &m, int dimension) {
876 return ccode(*m.contact_patch_velocity.get(dimension, 0));
877 },
878 &result_py);
879 DefineVector2dFunction(
880 "wheel_ground_velocity",
881 "# Returns the velocity of the wheel in steer module coordinates.",
882 [](const Module &m, int dimension) {
883 return ccode(*m.wheel_ground_velocity.get(dimension, 0));
884 },
885 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700886
Austin Schuhb8b34be2024-07-14 16:06:19 -0700887 DefineVector2dFunction(
888 "wheel_slip_velocity",
889 "# Returns the difference in velocities of the wheel surface and the "
890 "ground.",
891 [](const Module &m, int dimension) {
892 return ccode(*m.wheel_slip_velocity.get(dimension, 0));
893 },
894 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700895
Austin Schuhb8b34be2024-07-14 16:06:19 -0700896 DefineScalarFunction(
897 "slip_angle", "Returns the slip angle of the ith wheel",
898 [](const Module &m) { return ccode(*m.slip_angle); }, &result_py);
899 DefineScalarFunction(
900 "slip_ratio", "Returns the slip ratio of the ith wheel",
901 [](const Module &m) { return ccode(*m.slip_ratio); }, &result_py);
902 DefineScalarFunction(
903 "module_angular_accel",
904 "Returns the angular acceleration of the robot due to the ith wheel",
Austin Schuh6ea789e2024-07-27 13:45:53 -0700905 [this](const Module &m) { return ccode(*div(m.full.torque, Js_)); },
906 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700907
Austin Schuhb8b34be2024-07-14 16:06:19 -0700908 DefineVector2dFunction(
909 "wheel_force",
910 "Returns the force on the wheel in steer module coordinates",
911 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700912 return ccode(
913 *std::vector<RCP<const Basic>>{m.full.Fwx, m.Fwy}[dimension]);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700914 },
915 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700916
Austin Schuhb8b34be2024-07-14 16:06:19 -0700917 DefineVector2dFunction(
918 "F", "Returns the force on the wheel in absolute coordinates",
919 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700920 return ccode(*m.full.F.get(dimension, 0));
Austin Schuhb8b34be2024-07-14 16:06:19 -0700921 },
922 &result_py);
923
justinT21820767f2024-09-11 19:57:55 -0700924 DefineVector2dVelocityFunction(
925 "F_vel",
926 "Returns the force on the wheel in absolute coordinates based on the "
927 "velocity controller",
928 [](const Module &m, int dimension) {
929 return ccode(*m.direct.F.get(dimension, 0));
930 },
931 &result_py);
932
933 DefineVector2dVelocityFunction(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700934 "mounting_location",
935 "Returns the mounting location of wheel in robot coordinates",
936 [](const Module &m, int dimension) {
937 return ccode(*m.mounting_location.get(dimension, 0));
938 },
939 &result_py);
940
941 DefineScalarFunction(
942 "Ms", "Returns the self aligning moment of the ith wheel",
943 [this](const Module &m) {
944 return ccode(*(div(m.Ms, add(Jsm_, div(div(Js_, Gs_), Gs_)))));
945 },
946 &result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700947
948 aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n"));
949 }
950
Austin Schuhb8b34be2024-07-14 16:06:19 -0700951 void DefineScalarFunction(
952 std::string_view name, std::string_view documentation,
953 std::function<std::string(const Module &)> scalar_fn,
954 std::vector<std::string> *result_py) {
955 result_py->emplace_back("");
956 result_py->emplace_back(absl::Substitute("# $0.", documentation));
957 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
958 WriteCasadiVariables(result_py);
959 for (size_t m = 0; m < kNumModules; ++m) {
960 if (m == 0) {
961 result_py->emplace_back(" if i == 0:");
962 } else {
963 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
964 }
965 result_py->emplace_back(
966 absl::Substitute(" return casadi.Function('$0', [X, U], [$1])",
967 name, scalar_fn(modules_[m])));
968 }
969 result_py->emplace_back(" raise ValueError(\"Invalid module number\")");
970 }
971
972 void DefineVector2dFunction(
973 std::string_view name, std::string_view documentation,
974 std::function<std::string(const Module &, int)> scalar_fn,
975 std::vector<std::string> *result_py) {
976 result_py->emplace_back("");
977 result_py->emplace_back(absl::Substitute("# $0.", documentation));
978 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
979 WriteCasadiVariables(result_py);
980 result_py->emplace_back(
981 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
982 for (size_t m = 0; m < kNumModules; ++m) {
983 if (m == 0) {
984 result_py->emplace_back(" if i == 0:");
985 } else {
986 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
987 }
988 for (int j = 0; j < 2; ++j) {
989 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
990 j, scalar_fn(modules_[m], j)));
991 }
992 }
993 result_py->emplace_back(" else:");
994 result_py->emplace_back(
995 " raise ValueError(\"Invalid module number\")");
996 result_py->emplace_back(absl::Substitute(
997 " return casadi.Function('$0', [X, U], [result])", name));
998 }
999
justinT21820767f2024-09-11 19:57:55 -07001000 void DefineVector2dVelocityFunction(
1001 std::string_view name, std::string_view documentation,
1002 std::function<std::string(const Module &, int)> scalar_fn,
1003 std::vector<std::string> *result_py) {
1004 result_py->emplace_back("");
1005 result_py->emplace_back(absl::Substitute("# $0.", documentation));
1006 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
1007 WriteCasadiVelocityVariables(result_py);
1008 result_py->emplace_back(
1009 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
1010 for (size_t m = 0; m < kNumModules; ++m) {
1011 if (m == 0) {
1012 result_py->emplace_back(" if i == 0:");
1013 } else {
1014 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
1015 }
1016 for (int j = 0; j < 2; ++j) {
1017 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
1018 j, scalar_fn(modules_[m], j)));
1019 }
1020 }
1021 result_py->emplace_back(" else:");
1022 result_py->emplace_back(
1023 " raise ValueError(\"Invalid module number\")");
1024 result_py->emplace_back(absl::Substitute(
1025 " return casadi.Function('$0', [X, U], [result])", name));
1026 }
1027
justinT21446e4f62024-06-16 22:36:10 -07001028 private:
1029 static constexpr uint8_t kNumModules = 4;
1030
Austin Schuh6ea789e2024-07-27 13:45:53 -07001031 RCP<const Basic> SteerAccel(RCP<const Basic> Fwx, RCP<const Basic> Ms,
1032 RCP<const Basic> Is) {
1033 RCP<const Basic> lhms =
1034 mul(add(neg(wb_), mul(add(rs_, rp_), sub(integer(1), div(rb1_, rp_)))),
1035 mul(div(rw_, rb2_), neg(Fwx)));
1036 RCP<const Basic> lhs = add(add(Ms, div(mul(Kts_, Is), Gs_)), lhms);
1037 RCP<const Basic> rhs = add(Jsm_, div(div(Js_, Gs_), Gs_));
1038 return simplify(div(lhs, rhs));
1039 }
1040
justinT21446e4f62024-06-16 22:36:10 -07001041 Module ModulePhysics(const int m, DenseMatrix mounting_location) {
1042 VLOG(1) << "Solving module " << m;
1043
1044 Module result;
Austin Schuhb8b34be2024-07-14 16:06:19 -07001045 result.mounting_location = mounting_location;
justinT21446e4f62024-06-16 22:36:10 -07001046
1047 result.Is = symbol(absl::StrFormat("Is%u", m));
1048 result.Id = symbol(absl::StrFormat("Id%u", m));
1049
1050 RCP<const Symbol> thetamd = symbol(absl::StrFormat("theta_md%u", m));
1051 RCP<const Symbol> omegamd = symbol(absl::StrFormat("omega_md%u", m));
1052 RCP<const Symbol> alphamd = symbol(absl::StrFormat("alpha_md%u", m));
1053
1054 result.thetas = symbol(absl::StrFormat("thetas%u", m));
1055 result.omegas = symbol(absl::StrFormat("omegas%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -07001056 RCP<const Symbol> alphas = symbol(absl::StrFormat("alphas%u", m));
justinT21446e4f62024-06-16 22:36:10 -07001057
justinT21446e4f62024-06-16 22:36:10 -07001058 result.omegad = symbol(absl::StrFormat("omegad%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -07001059 RCP<const Symbol> alphad = symbol(absl::StrFormat("alphad%u", m));
justinT21446e4f62024-06-16 22:36:10 -07001060
1061 // Velocity of the module in field coordinates
Austin Schuh2a1abec2024-07-10 20:31:16 -07001062 DenseMatrix robot_velocity = DenseMatrix(2, 1, {vx_, vy_});
justinT21446e4f62024-06-16 22:36:10 -07001063 VLOG(1) << "robot velocity: " << robot_velocity.__str__();
1064
1065 // Velocity of the contact patch in field coordinates
1066 DenseMatrix temp_matrix = DenseMatrix(2, 1);
1067 DenseMatrix temp_matrix2 = DenseMatrix(2, 1);
Austin Schuhbd75c482024-08-18 00:03:51 -07001068 DenseMatrix temp_matrix3 = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -07001069 result.contact_patch_velocity = DenseMatrix(2, 1);
justinT21446e4f62024-06-16 22:36:10 -07001070
Austin Schuhb8b34be2024-07-14 16:06:19 -07001071 mul_dense_dense(R(theta_), result.mounting_location, temp_matrix);
justinT21446e4f62024-06-16 22:36:10 -07001072 add_dense_dense(angle_cross(temp_matrix, omega_), robot_velocity,
1073 temp_matrix2);
1074 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuhbd75c482024-08-18 00:03:51 -07001075 DenseMatrix(2, 1, {neg(caster_), integer(0)}),
1076 temp_matrix3);
justinT21446e4f62024-06-16 22:36:10 -07001077 add_dense_dense(temp_matrix2,
Austin Schuhbd75c482024-08-18 00:03:51 -07001078 angle_cross(temp_matrix3, add(omega_, result.omegas)),
Austin Schuh2a1abec2024-07-10 20:31:16 -07001079 result.contact_patch_velocity);
justinT21446e4f62024-06-16 22:36:10 -07001080
1081 VLOG(1);
Austin Schuh2a1abec2024-07-10 20:31:16 -07001082 VLOG(1) << "contact patch velocity: "
1083 << result.contact_patch_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -07001084
1085 // Relative velocity of the surface of the wheel to the ground.
Austin Schuhb67a38f2024-07-04 13:48:38 -07001086 result.wheel_ground_velocity = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -07001087 mul_dense_dense(R(neg(add(result.thetas, theta_))),
1088 result.contact_patch_velocity,
Austin Schuhb67a38f2024-07-04 13:48:38 -07001089 result.wheel_ground_velocity);
justinT21446e4f62024-06-16 22:36:10 -07001090
Austin Schuhb8b34be2024-07-14 16:06:19 -07001091 // Compute the relative velocity between the wheel surface and the ground in
1092 // the wheel coordinate system.
1093 result.wheel_slip_velocity = DenseMatrix(2, 1);
1094 DenseMatrix wheel_velocity =
1095 DenseMatrix(2, 1, {mul(rw_, result.omegad), integer(0)});
1096 DenseMatrix negative_wheel_ground_velocity =
1097 DenseMatrix(2, 1,
1098 {neg(result.wheel_ground_velocity.get(0, 0)),
1099 neg(result.wheel_ground_velocity.get(1, 0))});
1100 add_dense_dense(negative_wheel_ground_velocity, wheel_velocity,
1101 result.wheel_slip_velocity);
1102
justinT21446e4f62024-06-16 22:36:10 -07001103 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001104 VLOG(1) << "wheel ground velocity: "
1105 << result.wheel_ground_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -07001106
Austin Schuh5ddcb472024-07-21 17:55:34 -07001107 result.slip_angle = sin(neg(atan2(result.wheel_ground_velocity.get(1, 0),
1108 result.wheel_ground_velocity.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -07001109
1110 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001111 VLOG(1) << "slip angle: " << result.slip_angle->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001112
Austin Schuh2a1abec2024-07-10 20:31:16 -07001113 // TODO(austin): Does this handle decel properly?
Austin Schuhb67a38f2024-07-04 13:48:38 -07001114 result.slip_ratio = div(
Austin Schuh2a1abec2024-07-10 20:31:16 -07001115 sub(mul(rw_, result.omegad), result.wheel_ground_velocity.get(0, 0)),
1116 SymEngine::max(
1117 {real_double(0.02), abs(result.wheel_ground_velocity.get(0, 0))}));
justinT21446e4f62024-06-16 22:36:10 -07001118 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001119 VLOG(1) << "Slip ratio " << result.slip_ratio->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001120
Austin Schuh6ea789e2024-07-27 13:45:53 -07001121 result.full.Fwx = simplify(mul(Cx_, result.slip_ratio));
Austin Schuhb67a38f2024-07-04 13:48:38 -07001122 result.Fwy = simplify(mul(Cy_, result.slip_angle));
justinT21446e4f62024-06-16 22:36:10 -07001123
Austin Schuh27694fa2024-07-20 16:29:49 -07001124 // The self-aligning moment needs to flip when the module flips direction.
Austin Schuh78a1b312024-08-18 17:21:34 -07001125 RCP<const Basic> softsign_velocity = add(
1126 div(integer(-2),
1127 add(integer(1), exp(mul(integer(100),
1128 result.wheel_ground_velocity.get(0, 0))))),
1129 integer(1));
1130 result.Ms =
1131 mul(neg(result.Fwy),
1132 add(div(mul(softsign_velocity, contact_patch_length_), integer(3)),
1133 caster_));
justinT21446e4f62024-06-16 22:36:10 -07001134 VLOG(1);
Austin Schuhb8b34be2024-07-14 16:06:19 -07001135 VLOG(1) << "Ms " << result.Ms->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001136 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001137 VLOG(1) << "full.Fwx " << result.full.Fwx->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001138 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001139 VLOG(1) << "Fwy " << result.Fwy->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001140
Austin Schuh6ea789e2024-07-27 13:45:53 -07001141 // -K_td * Id / Gd + Fwx * rw = 0
1142 // Fwx = K_td * Id / Gd / rw
1143 result.direct.Fwx = mul(Ktd_, div(result.Id, mul(Gd_, rw_)));
1144
1145 result.direct.alphas_eqn =
1146 SteerAccel(result.direct.Fwx, result.Ms, result.Is);
1147
1148 // d/dt omegas = ...
1149 result.full.alphas_eqn = SteerAccel(result.full.Fwx, result.Ms, result.Is);
justinT21446e4f62024-06-16 22:36:10 -07001150
1151 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001152 VLOG(1) << alphas->__str__() << " = " << result.full.alphas_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001153
Austin Schuh6ea789e2024-07-27 13:45:53 -07001154 RCP<const Basic> lhs =
1155 sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), alphas),
1156 mul(Gd1_, mul(Gd2_, alphamd)));
1157 RCP<const Basic> ddplanitary_eqn = sub(mul(Gd3_, lhs), alphad);
justinT21446e4f62024-06-16 22:36:10 -07001158
Austin Schuh6ea789e2024-07-27 13:45:53 -07001159 RCP<const Basic> full_drive_eqn =
1160 sub(add(mul(neg(Jdm_), div(alphamd, Gd_)),
1161 mul(Ktd_, div(neg(result.Id), Gd_))),
1162 mul(neg(result.full.Fwx), rw_));
justinT21446e4f62024-06-16 22:36:10 -07001163
Austin Schuh6ea789e2024-07-27 13:45:53 -07001164 VLOG(1) << "full_drive_eqn: " << full_drive_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001165
1166 // Substitute in ddplanitary_eqn so we get rid of alphamd
1167 map_basic_basic map;
1168 RCP<const Set> reals = interval(NegInf, Inf, true, true);
1169 RCP<const Set> solve_solution = solve(ddplanitary_eqn, alphamd, reals);
1170 map[alphamd] = solve_solution->get_args()[1]->get_args()[0];
1171 VLOG(1) << "temp: " << solve_solution->__str__();
Austin Schuh6ea789e2024-07-27 13:45:53 -07001172 RCP<const Basic> drive_eqn_subs = full_drive_eqn->subs(map);
justinT21446e4f62024-06-16 22:36:10 -07001173
1174 map.clear();
Austin Schuh6ea789e2024-07-27 13:45:53 -07001175 map[alphas] = result.full.alphas_eqn;
justinT21446e4f62024-06-16 22:36:10 -07001176 RCP<const Basic> drive_eqn_subs2 = drive_eqn_subs->subs(map);
1177 RCP<const Basic> drive_eqn_subs3 = simplify(drive_eqn_subs2);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001178 VLOG(1) << "full_drive_eqn simplified: " << drive_eqn_subs3->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001179
Austin Schuh6ea789e2024-07-27 13:45:53 -07001180 solve_solution = solve(drive_eqn_subs3, alphad, reals);
justinT21446e4f62024-06-16 22:36:10 -07001181
Austin Schuh6ea789e2024-07-27 13:45:53 -07001182 result.full.alphad_eqn =
justinT21446e4f62024-06-16 22:36:10 -07001183 simplify(solve_solution->get_args()[1]->get_args()[0]);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001184 VLOG(1) << "drive_accel: " << result.full.alphad_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001185
Austin Schuh2a1abec2024-07-10 20:31:16 -07001186 // Compute the resulting force from the module.
Austin Schuh6ea789e2024-07-27 13:45:53 -07001187 result.full.F = DenseMatrix(2, 1);
Austin Schuhb8b34be2024-07-14 16:06:19 -07001188 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuh6ea789e2024-07-27 13:45:53 -07001189 DenseMatrix(2, 1, {result.full.Fwx, result.Fwy}),
1190 result.full.F);
justinT21d18f79f2024-09-22 19:43:05 -07001191
1192 DenseMatrix rotated_mounting_location = DenseMatrix(2, 1);
1193 mul_dense_dense(R(theta_), result.mounting_location,
1194 rotated_mounting_location);
1195 result.full.torque = force_cross(rotated_mounting_location, result.full.F);
justinT21446e4f62024-06-16 22:36:10 -07001196
Austin Schuh6ea789e2024-07-27 13:45:53 -07001197 result.direct.F = DenseMatrix(2, 1);
1198 mul_dense_dense(R(add(theta_, result.thetas)),
1199 DenseMatrix(2, 1, {result.direct.Fwx, result.Fwy}),
1200 result.direct.F);
1201 result.direct.torque =
justinT21d18f79f2024-09-22 19:43:05 -07001202 force_cross(rotated_mounting_location, result.direct.F);
justinT21446e4f62024-06-16 22:36:10 -07001203
1204 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001205 VLOG(1) << "full torque = " << result.full.torque->__str__();
1206 VLOG(1) << "direct torque = " << result.full.torque->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001207
justinT21446e4f62024-06-16 22:36:10 -07001208 return result;
1209 }
1210
1211 DenseMatrix R(const RCP<const Basic> theta) {
1212 return DenseMatrix(2, 2,
1213 {cos(theta), neg(sin(theta)), sin(theta), cos(theta)});
1214 }
1215
1216 DenseMatrix angle_cross(DenseMatrix a, RCP<const Basic> b) {
Austin Schuh2a1abec2024-07-10 20:31:16 -07001217 return DenseMatrix(2, 1, {mul(neg(a.get(1, 0)), b), mul(a.get(0, 0), b)});
justinT21446e4f62024-06-16 22:36:10 -07001218 }
1219
1220 RCP<const Basic> force_cross(DenseMatrix r, DenseMatrix f) {
1221 return sub(mul(r.get(0, 0), f.get(1, 0)), mul(r.get(1, 0), f.get(0, 0)));
1222 }
1223
1224 // z represents the number of teeth per gear, theta is the angle between
1225 // shafts(in degrees), D_02 is the pitch diameter of gear 2 and b_2 is the
1226 // length of the tooth of gear 2
1227 // returns std::pair(r_01, r_02)
1228 std::pair<double, double> GetBevelPitchRadius(double z1, double z2,
1229 double theta, double D_02,
1230 double b_2) {
1231 double gamma_1 = std::atan2(z1, z2);
1232 double gamma_2 = theta / 180.0 * std::numbers::pi - gamma_1;
1233 double R_m = D_02 / 2 / std::sin(gamma_2) - b_2 / 2;
1234 return std::pair(R_m * std::cos(gamma_2), R_m * std::sin(gamma_2));
1235 }
1236
1237 Motor drive_motor_;
1238 Motor steer_motor_;
1239
1240 RCP<const Basic> Cx_;
1241 RCP<const Basic> Cy_;
Austin Schuh2a1abec2024-07-10 20:31:16 -07001242 RCP<const Basic> rw_;
justinT21446e4f62024-06-16 22:36:10 -07001243 RCP<const Basic> m_;
1244 RCP<const Basic> J_;
1245 RCP<const Basic> Gd1_;
1246 RCP<const Basic> rs_;
1247 RCP<const Basic> rp_;
1248 RCP<const Basic> Gd2_;
1249 RCP<const Basic> rb1_;
1250 RCP<const Basic> rb2_;
1251 RCP<const Basic> Gd3_;
1252 RCP<const Basic> Gd_;
1253 RCP<const Basic> Js_;
1254 RCP<const Basic> Gs_;
1255 RCP<const Basic> wb_;
1256 RCP<const Basic> Jdm_;
1257 RCP<const Basic> Jsm_;
1258 RCP<const Basic> Kts_;
1259 RCP<const Basic> Ktd_;
1260 RCP<const Basic> robot_width_;
1261 RCP<const Basic> caster_;
1262 RCP<const Basic> contact_patch_length_;
1263 RCP<const Basic> x_;
1264 RCP<const Basic> y_;
1265 RCP<const Basic> theta_;
1266 RCP<const Basic> vx_;
1267 RCP<const Basic> vy_;
1268 RCP<const Basic> omega_;
1269 RCP<const Basic> ax_;
1270 RCP<const Basic> ay_;
1271 RCP<const Basic> atheta_;
1272
1273 std::array<Module, kNumModules> modules_;
1274
Austin Schuh6ea789e2024-07-27 13:45:53 -07001275 DenseMatrix full_accel_;
1276 RCP<const Basic> full_angular_accel_;
1277 DenseMatrix direct_accel_;
1278 RCP<const Basic> direct_angular_accel_;
justinT21446e4f62024-06-16 22:36:10 -07001279};
1280
1281} // namespace frc971::control_loops::swerve
1282
1283int main(int argc, char **argv) {
1284 aos::InitGoogle(&argc, &argv);
1285
1286 frc971::control_loops::swerve::SwerveSimulation sim;
1287
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001288 if (!absl::GetFlag(FLAGS_cc_output_path).empty() &&
1289 !absl::GetFlag(FLAGS_h_output_path).empty()) {
1290 sim.Write(absl::GetFlag(FLAGS_cc_output_path),
1291 absl::GetFlag(FLAGS_h_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001292 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001293 if (!absl::GetFlag(FLAGS_casadi_py_output_path).empty()) {
1294 sim.WriteCasadi(absl::GetFlag(FLAGS_casadi_py_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001295 }
justinT21446e4f62024-06-16 22:36:10 -07001296
Austin Schuha9550c02024-10-19 13:48:10 -07001297 if (!absl::GetFlag(FLAGS_constants_output_path).empty()) {
1298 sim.WriteConstantsFile(absl::GetFlag(FLAGS_constants_output_path));
1299 }
1300
justinT21446e4f62024-06-16 22:36:10 -07001301 return 0;
1302}