blob: de52d79f94f4269e0359cb2c186021110dea795b [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);");
342 result_h.emplace_back("struct Inputs {");
343 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 = "
357 "static_cast<size_t>(Inputs::kNumInputs);");
358 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("");
368 result_h.emplace_back(
369 "Eigen::Matrix<double, kNumVelocityStates, 1> ToVelocityState(");
370 result_h.emplace_back(
371 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
372 "1>> X);");
373 result_h.emplace_back("");
374 result_h.emplace_back(
375 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> FromVelocityState(");
376 result_h.emplace_back(
377 " Eigen::Ref<const Eigen::Matrix<double, kNumVelocityStates, 1>> "
378 "X);");
379 result_h.emplace_back("");
380 result_h.emplace_back(
381 "inline Eigen::Matrix<double, kNumVelocityStates, 1> VelocityPhysics(");
382 result_h.emplace_back(
383 " Eigen::Ref<const Eigen::Matrix<double, kNumVelocityStates, 1>> "
384 "X,");
385 result_h.emplace_back(
386 " Eigen::Ref<const Eigen::Matrix<double, kNumInputs, 1>> U) {");
387 result_h.emplace_back(
388 " return ToVelocityState(SwervePhysics(FromVelocityState(X), U));");
389 result_h.emplace_back("}");
justinT21446e4f62024-06-16 22:36:10 -0700390 result_h.emplace_back("");
391 result_h.emplace_back("} // namespace frc971::control_loops::swerve");
392 result_h.emplace_back("");
393 result_h.emplace_back(absl::Substitute("#endif // $0_", include_guard));
394
395 // Write out the .cc
396 result_cc.emplace_back(
397 absl::Substitute("#include \"$0\"", include_guard_stripped));
398 result_cc.emplace_back("");
399 result_cc.emplace_back("#include <cmath>");
400 result_cc.emplace_back("");
401 result_cc.emplace_back("namespace frc971::control_loops::swerve {");
402 result_cc.emplace_back("");
justinT21446e4f62024-06-16 22:36:10 -0700403 result_cc.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700404 "Eigen::Matrix<double, kNumVelocityStates, 1> ToVelocityState(");
justinT21446e4f62024-06-16 22:36:10 -0700405 result_cc.emplace_back(
James Kuszmaulef14ab42024-09-14 15:05:24 -0700406 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
407 "1>> X) {");
408 result_cc.emplace_back(
409 " Eigen::Matrix<double, kNumVelocityStates, 1> velocity;");
410 const std::vector<std::string_view> velocity_states = {
411 "kThetas0", "kOmegas0", "kThetas1", "kOmegas1", "kThetas2", "kOmegas2",
412 "kThetas3", "kOmegas3", "kTheta", "kVx", "kVy", "kOmega"};
413 for (const std::string_view velocity_state : velocity_states) {
414 result_cc.emplace_back(absl::StrFormat(
415 " velocity(VelocityStates::%s) = X(FullDynamicsStates::%s);",
416 velocity_state, velocity_state));
417 }
418 result_cc.emplace_back(" return velocity;");
419 result_cc.emplace_back("}");
420 result_cc.emplace_back("");
421 result_cc.emplace_back(
422 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> FromVelocityState(");
423 result_cc.emplace_back(
424 " Eigen::Ref<const Eigen::Matrix<double, kNumVelocityStates, 1>> X) "
425 "{");
426 result_cc.emplace_back(
427 " Eigen::Matrix<double, kNumFullDynamicsStates, 1> full;");
428 result_cc.emplace_back(" full.setZero();");
429 for (const std::string_view velocity_state : velocity_states) {
430 result_cc.emplace_back(absl::StrFormat(
431 " full(FullDynamicsStates::%s) = X(VelocityStates::%s);",
432 velocity_state, velocity_state));
433 }
434 result_cc.emplace_back(" return full;");
435 result_cc.emplace_back("}");
436 result_cc.emplace_back("");
437 result_cc.emplace_back(
438 "Eigen::Matrix<double, kNumFullDynamicsStates, 1> SwervePhysics(");
439 result_cc.emplace_back(
440 " Eigen::Ref<const Eigen::Matrix<double, kNumFullDynamicsStates, "
441 "1>> X,");
442 result_cc.emplace_back(
443 " Eigen::Ref<const Eigen::Matrix<double, kNumInputs, 1>> U) {");
444 result_cc.emplace_back(
445 " Eigen::Matrix<double, kNumFullDynamicsStates, 1> result;");
justinT21446e4f62024-06-16 22:36:10 -0700446
447 // Start by writing out variables matching each of the symbol names we use
448 // so we don't have to modify the computed equations too much.
449 for (size_t m = 0; m < kNumModules; ++m) {
450 result_cc.emplace_back(
451 absl::Substitute(" const double thetas$0 = X($1, 0);", m, m * 4));
452 result_cc.emplace_back(absl::Substitute(
453 " const double omegas$0 = X($1, 0);", m, m * 4 + 2));
454 result_cc.emplace_back(absl::Substitute(
455 " const double omegad$0 = X($1, 0);", m, m * 4 + 3));
456 }
457
458 result_cc.emplace_back(absl::Substitute(" const double theta = X($0, 0);",
459 kNumModules * 4 + 2));
460 result_cc.emplace_back(
461 absl::Substitute(" const double vx = X($0, 0);", kNumModules * 4 + 3));
462 result_cc.emplace_back(
463 absl::Substitute(" const double vy = X($0, 0);", kNumModules * 4 + 4));
464 result_cc.emplace_back(absl::Substitute(" const double omega = X($0, 0);",
465 kNumModules * 4 + 5));
466
467 result_cc.emplace_back(
468 absl::Substitute(" const double fx = X($0, 0);", kNumModules * 4 + 6));
469 result_cc.emplace_back(
470 absl::Substitute(" const double fy = X($0, 0);", kNumModules * 4 + 7));
471 result_cc.emplace_back(absl::Substitute(" const double moment = X($0, 0);",
472 kNumModules * 4 + 8));
473
474 // Now do the same for the inputs.
475 for (size_t m = 0; m < kNumModules; ++m) {
476 result_cc.emplace_back(
477 absl::Substitute(" const double Is$0 = U($1, 0);", m, m * 2));
478 result_cc.emplace_back(
479 absl::Substitute(" const double Id$0 = U($1, 0);", m, m * 2 + 1));
480 }
481
482 result_cc.emplace_back("");
483
484 // And then write out the derivative of each state.
485 for (size_t m = 0; m < kNumModules; ++m) {
486 result_cc.emplace_back(
487 absl::Substitute(" result($0, 0) = omegas$1;", m * 4, m));
488 result_cc.emplace_back(
489 absl::Substitute(" result($0, 0) = omegad$1;", m * 4 + 1, m));
490
Austin Schuh6ea789e2024-07-27 13:45:53 -0700491 result_cc.emplace_back(
492 absl::Substitute(" result($0, 0) = $1;", m * 4 + 2,
493 ccode(*modules_[m].full.alphas_eqn)));
494 result_cc.emplace_back(
495 absl::Substitute(" result($0, 0) = $1;", m * 4 + 3,
496 ccode(*modules_[m].full.alphad_eqn)));
justinT21446e4f62024-06-16 22:36:10 -0700497 }
498
499 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700500 absl::Substitute(" result($0, 0) = vx;", kNumModules * 4 + 0));
justinT21446e4f62024-06-16 22:36:10 -0700501 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700502 absl::Substitute(" result($0, 0) = vy;", kNumModules * 4 + 1));
justinT21446e4f62024-06-16 22:36:10 -0700503 result_cc.emplace_back(
Austin Schuh5ddcb472024-07-21 17:55:34 -0700504 absl::Substitute(" result($0, 0) = omega;", kNumModules * 4 + 2));
justinT21446e4f62024-06-16 22:36:10 -0700505
justinT21446e4f62024-06-16 22:36:10 -0700506 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700507 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700508 ccode(*full_accel_.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -0700509 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
Austin Schuh5ddcb472024-07-21 17:55:34 -0700510 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700511 ccode(*full_accel_.get(1, 0))));
512 result_cc.emplace_back(absl::Substitute(" result($0, 0) = $1;",
513 kNumModules * 4 + 5,
514 ccode(*full_angular_accel_)));
justinT21446e4f62024-06-16 22:36:10 -0700515
516 result_cc.emplace_back(
517 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 6));
518 result_cc.emplace_back(
519 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 7));
520 result_cc.emplace_back(
521 absl::Substitute(" result($0, 0) = 0.0;", kNumModules * 4 + 8));
522
523 result_cc.emplace_back("");
524 result_cc.emplace_back(" return result;");
525 result_cc.emplace_back("}");
526 result_cc.emplace_back("");
527 result_cc.emplace_back("} // namespace frc971::control_loops::swerve");
528
529 aos::util::WriteStringToFileOrDie(cc_path, absl::StrJoin(result_cc, "\n"));
530 aos::util::WriteStringToFileOrDie(h_path, absl::StrJoin(result_h, "\n"));
531 }
532
Austin Schuhb67a38f2024-07-04 13:48:38 -0700533 void WriteCasadiVariables(std::vector<std::string> *result_py) {
534 result_py->emplace_back(" sin = casadi.sin");
535 result_py->emplace_back(" cos = casadi.cos");
Austin Schuh78a1b312024-08-18 17:21:34 -0700536 result_py->emplace_back(" exp = casadi.exp");
Austin Schuh5371c802024-09-02 13:18:48 -0700537 if (absl::GetFlag(FLAGS_function)) {
538 result_py->emplace_back(" atan2 = soft_atan2()");
539 } else {
540 result_py->emplace_back(" atan2 = soft_atan2");
541 }
Austin Schuh2a1abec2024-07-10 20:31:16 -0700542 result_py->emplace_back(" fmax = casadi.fmax");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700543 result_py->emplace_back(" fabs = casadi.fabs");
544
545 // Start by writing out variables matching each of the symbol names we use
546 // so we don't have to modify the computed equations too much.
547 for (size_t m = 0; m < kNumModules; ++m) {
548 result_py->emplace_back(
549 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 4));
550 result_py->emplace_back(
551 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 4 + 2));
552 result_py->emplace_back(
553 absl::Substitute(" omegad$0 = X[$1, 0]", m, m * 4 + 3));
554 }
555
556 result_py->emplace_back(
557 absl::Substitute(" theta = X[$0, 0]", kNumModules * 4 + 2));
558 result_py->emplace_back(
559 absl::Substitute(" vx = X[$0, 0]", kNumModules * 4 + 3));
560 result_py->emplace_back(
561 absl::Substitute(" vy = X[$0, 0]", kNumModules * 4 + 4));
562 result_py->emplace_back(
563 absl::Substitute(" omega = X[$0, 0]", kNumModules * 4 + 5));
564
565 result_py->emplace_back(
566 absl::Substitute(" fx = X[$0, 0]", kNumModules * 4 + 6));
567 result_py->emplace_back(
568 absl::Substitute(" fy = X[$0, 0]", kNumModules * 4 + 7));
569 result_py->emplace_back(
570 absl::Substitute(" moment = X[$0, 0]", kNumModules * 4 + 8));
571
572 // Now do the same for the inputs.
573 for (size_t m = 0; m < kNumModules; ++m) {
574 result_py->emplace_back(
575 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
576 result_py->emplace_back(
577 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
578 }
579 }
580
Austin Schuh6ea789e2024-07-27 13:45:53 -0700581 void WriteCasadiVelocityVariables(std::vector<std::string> *result_py) {
582 result_py->emplace_back(" sin = casadi.sin");
Austin Schuh78a1b312024-08-18 17:21:34 -0700583 result_py->emplace_back(" exp = casadi.exp");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700584 result_py->emplace_back(" cos = casadi.cos");
Austin Schuh5371c802024-09-02 13:18:48 -0700585 if (absl::GetFlag(FLAGS_function)) {
586 result_py->emplace_back(" atan2 = soft_atan2()");
587 } else {
588 result_py->emplace_back(" atan2 = soft_atan2");
589 }
Austin Schuh6ea789e2024-07-27 13:45:53 -0700590 result_py->emplace_back(" fmax = casadi.fmax");
591 result_py->emplace_back(" fabs = casadi.fabs");
592
593 // Start by writing out variables matching each of the symbol names we use
594 // so we don't have to modify the computed equations too much.
595 for (size_t m = 0; m < kNumModules; ++m) {
596 result_py->emplace_back(
597 absl::Substitute(" thetas$0 = X[$1, 0]", m, m * 2 + 0));
598 result_py->emplace_back(
599 absl::Substitute(" omegas$0 = X[$1, 0]", m, m * 2 + 1));
600 }
601
602 result_py->emplace_back(
603 absl::Substitute(" theta = X[$0, 0]", kNumModules * 2 + 0));
604 result_py->emplace_back(
605 absl::Substitute(" vx = X[$0, 0]", kNumModules * 2 + 1));
606 result_py->emplace_back(
607 absl::Substitute(" vy = X[$0, 0]", kNumModules * 2 + 2));
608 result_py->emplace_back(
609 absl::Substitute(" omega = X[$0, 0]", kNumModules * 2 + 3));
610
611 // result_py->emplace_back(
612 // absl::Substitute(" fx = X[$0, 0]", kNumModules * 3 + 4));
613 // result_py->emplace_back(
614 // absl::Substitute(" fy = X[$0, 0]", kNumModules * 3 + 5));
615 // result_py->emplace_back(
616 // absl::Substitute(" moment = X[$0, 0]", kNumModules * 3 + 6));
617 //
618 result_py->emplace_back(" fx = 0");
619 result_py->emplace_back(" fy = 0");
620 result_py->emplace_back(" moment = 0");
621
622 // Now do the same for the inputs.
623 for (size_t m = 0; m < kNumModules; ++m) {
624 result_py->emplace_back(
625 absl::Substitute(" Is$0 = U[$1, 0]", m, m * 2));
626 result_py->emplace_back(
627 absl::Substitute(" Id$0 = U[$1, 0]", m, m * 2 + 1));
628 }
629 }
630
Austin Schuha9550c02024-10-19 13:48:10 -0700631 void WriteConstantsFile(std::string_view path) {
632 std::vector<std::string> result_py;
633
634 // Write out the header.
635 result_py.emplace_back("#!/usr/bin/env python3");
636 result_py.emplace_back("");
637
638 WriteConstants(&result_py);
639
640 aos::util::WriteStringToFileOrDie(path, absl::StrJoin(result_py, "\n"));
641 }
642
643 void WriteConstants(std::vector<std::string> *result_py) {
644 result_py->emplace_back(absl::Substitute("WHEEL_RADIUS = $0", ccode(*rw_)));
645 result_py->emplace_back(
646 absl::Substitute("ROBOT_WIDTH = $0", ccode(*robot_width_)));
647 result_py->emplace_back(absl::Substitute("CASTER = $0", ccode(*caster_)));
648 result_py->emplace_back("STATE_THETAS0 = 0");
649 result_py->emplace_back("STATE_THETAD0 = 1");
650 result_py->emplace_back("STATE_OMEGAS0 = 2");
651 result_py->emplace_back("STATE_OMEGAD0 = 3");
652 result_py->emplace_back("STATE_THETAS1 = 4");
653 result_py->emplace_back("STATE_THETAD1 = 5");
654 result_py->emplace_back("STATE_OMEGAS1 = 6");
655 result_py->emplace_back("STATE_OMEGAD1 = 7");
656 result_py->emplace_back("STATE_THETAS2 = 8");
657 result_py->emplace_back("STATE_THETAD2 = 9");
658 result_py->emplace_back("STATE_OMEGAS2 = 10");
659 result_py->emplace_back("STATE_OMEGAD2 = 11");
660 result_py->emplace_back("STATE_THETAS3 = 12");
661 result_py->emplace_back("STATE_THETAD3 = 13");
662 result_py->emplace_back("STATE_OMEGAS3 = 14");
663 result_py->emplace_back("STATE_OMEGAD3 = 15");
664 result_py->emplace_back("STATE_X = 16");
665 result_py->emplace_back("STATE_Y = 17");
666 result_py->emplace_back("STATE_THETA = 18");
667 result_py->emplace_back("STATE_VX = 19");
668 result_py->emplace_back("STATE_VY = 20");
669 result_py->emplace_back("STATE_OMEGA = 21");
670 result_py->emplace_back("STATE_FX = 22");
671 result_py->emplace_back("STATE_FY = 23");
672 result_py->emplace_back("STATE_MOMENT = 24");
673 result_py->emplace_back("NUM_STATES = 25");
674 result_py->emplace_back("");
675 result_py->emplace_back("VELOCITY_STATE_THETAS0 = 0");
676 result_py->emplace_back("VELOCITY_STATE_OMEGAS0 = 1");
677 result_py->emplace_back("VELOCITY_STATE_THETAS1 = 2");
678 result_py->emplace_back("VELOCITY_STATE_OMEGAS1 = 3");
679 result_py->emplace_back("VELOCITY_STATE_THETAS2 = 4");
680 result_py->emplace_back("VELOCITY_STATE_OMEGAS2 = 5");
681 result_py->emplace_back("VELOCITY_STATE_THETAS3 = 6");
682 result_py->emplace_back("VELOCITY_STATE_OMEGAS3 = 7");
683 result_py->emplace_back("VELOCITY_STATE_THETA = 8");
684 result_py->emplace_back("VELOCITY_STATE_VX = 9");
685 result_py->emplace_back("VELOCITY_STATE_VY = 10");
686 result_py->emplace_back("VELOCITY_STATE_OMEGA = 11");
687 // result_py->emplace_back("VELOCITY_STATE_FX = 16");
688 // result_py->emplace_back("VELOCITY_STATE_FY = 17");
689 // result_py->emplace_back("VELOCITY_STATE_MOMENT = 18");
690 result_py->emplace_back("NUM_VELOCITY_STATES = 12");
691 result_py->emplace_back("");
692 result_py->emplace_back("");
693 result_py->emplace_back("# Is = STEER_CURRENT_COUPLING_FACTOR * Id");
694 result_py->emplace_back(absl::Substitute(
695 "STEER_CURRENT_COUPLING_FACTOR = $0",
696 ccode(*(neg(
697 mul(div(Gs_, Kts_),
698 mul(div(Ktd_, mul(Gd_, rw_)),
699 neg(mul(add(neg(wb_), mul(add(rs_, rp_),
700 sub(integer(1), div(rb1_, rp_)))),
701 div(rw_, rb2_))))))))));
702 result_py->emplace_back("");
703 }
704
Austin Schuh0f881092024-06-28 15:36:48 -0700705 // Writes the physics out to the provided .cc and .h path.
706 void WriteCasadi(std::string_view py_path) {
707 std::vector<std::string> result_py;
708
709 // Write out the header.
Austin Schuh5371c802024-09-02 13:18:48 -0700710 result_py.emplace_back("#!/usr/bin/env python3");
Austin Schuh0f881092024-06-28 15:36:48 -0700711 result_py.emplace_back("");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700712 result_py.emplace_back("import casadi, numpy");
Austin Schuh0f881092024-06-28 15:36:48 -0700713 result_py.emplace_back("");
Austin Schuha9550c02024-10-19 13:48:10 -0700714
715 WriteConstants(&result_py);
716
Austin Schuh6ea789e2024-07-27 13:45:53 -0700717 result_py.emplace_back("def to_velocity_state(X):");
718 result_py.emplace_back(" return numpy.array([");
719 result_py.emplace_back(" [X[STATE_THETAS0, 0]],");
720 result_py.emplace_back(" [X[STATE_OMEGAS0, 0]],");
721 result_py.emplace_back(" [X[STATE_THETAS1, 0]],");
722 result_py.emplace_back(" [X[STATE_OMEGAS1, 0]],");
723 result_py.emplace_back(" [X[STATE_THETAS2, 0]],");
724 result_py.emplace_back(" [X[STATE_OMEGAS2, 0]],");
725 result_py.emplace_back(" [X[STATE_THETAS3, 0]],");
726 result_py.emplace_back(" [X[STATE_OMEGAS3, 0]],");
727 result_py.emplace_back(" [X[STATE_THETA, 0]],");
728 result_py.emplace_back(" [X[STATE_VX, 0]],");
729 result_py.emplace_back(" [X[STATE_VY, 0]],");
730 result_py.emplace_back(" [X[STATE_OMEGA, 0]],");
731 // result_py.emplace_back(" [X[STATE_FX, 0]],");
732 // result_py.emplace_back(" [X[STATE_FY, 0]],");
733 // result_py.emplace_back(" [X[STATE_MOMENT, 0]],");
734 result_py.emplace_back(" ])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700735 result_py.emplace_back("");
Austin Schuhbd75c482024-08-18 00:03:51 -0700736 constexpr double kLogGain = 1.0 / 0.05;
Austin Schuh5371c802024-09-02 13:18:48 -0700737 constexpr double kAbsGain = 1.0 / 0.01;
738 if (absl::GetFlag(FLAGS_function)) {
739 result_py.emplace_back("def soft_atan2():");
740 result_py.emplace_back(" y = casadi.SX.sym('y')");
741 result_py.emplace_back(" x = casadi.SX.sym('x')");
742 result_py.emplace_back(
743 " return casadi.Function('soft_atan2', [y, x], [");
744 result_py.emplace_back(" casadi.arctan2(");
745 result_py.emplace_back(" y,");
746 result_py.emplace_back(" casadi.logsumexp(");
747 result_py.emplace_back(" casadi.SX(");
748 result_py.emplace_back(" numpy.array([");
749 result_py.emplace_back(" 1.0, x * (1.0 - 2.0 /");
750 result_py.emplace_back(
751 absl::Substitute(" (1 + "
752 "casadi.exp($1.0 * x))) * $0.0",
753 kLogGain, kAbsGain));
754 result_py.emplace_back(
755 absl::Substitute(" ]))) / $0.0)", kLogGain));
756 result_py.emplace_back(" ])");
757 } else {
758 result_py.emplace_back("def soft_atan2(y, x):");
759 result_py.emplace_back(" return casadi.arctan2(");
760 result_py.emplace_back(" y,");
761 result_py.emplace_back(" casadi.logsumexp(casadi.SX(numpy.array(");
762 result_py.emplace_back(
763 absl::Substitute(" [1.0, x * (1.0 - 2.0 / (1 + "
764 "casadi.exp($1.0 * x))) * $0.0]))) / $0.0)",
765 kLogGain, kAbsGain));
766 }
Austin Schuh2a1abec2024-07-10 20:31:16 -0700767
Austin Schuh0f881092024-06-28 15:36:48 -0700768 result_py.emplace_back("# Returns the derivative of our state vector");
769 result_py.emplace_back("# [thetas0, thetad0, omegas0, omegad0,");
770 result_py.emplace_back("# thetas1, thetad1, omegas1, omegad1,");
771 result_py.emplace_back("# thetas2, thetad2, omegas2, omegad2,");
772 result_py.emplace_back("# thetas3, thetad3, omegas3, omegad3,");
773 result_py.emplace_back("# x, y, theta, vx, vy, omega,");
774 result_py.emplace_back("# Fx, Fy, Moment]");
Austin Schuh6ea789e2024-07-27 13:45:53 -0700775 result_py.emplace_back("def swerve_full_dynamics(X, U):");
Austin Schuhb67a38f2024-07-04 13:48:38 -0700776 WriteCasadiVariables(&result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700777
778 result_py.emplace_back("");
779 result_py.emplace_back(" result = casadi.SX.sym('result', 25, 1)");
780 result_py.emplace_back("");
781
782 // And then write out the derivative of each state.
783 for (size_t m = 0; m < kNumModules; ++m) {
784 result_py.emplace_back(
785 absl::Substitute(" result[$0, 0] = omegas$1", m * 4, m));
786 result_py.emplace_back(
787 absl::Substitute(" result[$0, 0] = omegad$1", m * 4 + 1, m));
788
Austin Schuh6ea789e2024-07-27 13:45:53 -0700789 result_py.emplace_back(
790 absl::Substitute(" result[$0, 0] = $1", m * 4 + 2,
791 ccode(*modules_[m].full.alphas_eqn)));
792 result_py.emplace_back(
793 absl::Substitute(" result[$0, 0] = $1", m * 4 + 3,
794 ccode(*modules_[m].full.alphad_eqn)));
Austin Schuh0f881092024-06-28 15:36:48 -0700795 }
796
797 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700798 absl::Substitute(" result[$0, 0] = vx", kNumModules * 4 + 0));
Austin Schuh0f881092024-06-28 15:36:48 -0700799 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700800 absl::Substitute(" result[$0, 0] = vy", kNumModules * 4 + 1));
Austin Schuh0f881092024-06-28 15:36:48 -0700801 result_py.emplace_back(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700802 absl::Substitute(" result[$0, 0] = omega", kNumModules * 4 + 2));
Austin Schuh0f881092024-06-28 15:36:48 -0700803
Austin Schuh0f881092024-06-28 15:36:48 -0700804 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700805 kNumModules * 4 + 3,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700806 ccode(*full_accel_.get(0, 0))));
Austin Schuh0f881092024-06-28 15:36:48 -0700807 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
Austin Schuhb8b34be2024-07-14 16:06:19 -0700808 kNumModules * 4 + 4,
Austin Schuh6ea789e2024-07-27 13:45:53 -0700809 ccode(*full_accel_.get(1, 0))));
810 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
811 kNumModules * 4 + 5,
812 ccode(*full_angular_accel_)));
Austin Schuh0f881092024-06-28 15:36:48 -0700813
814 result_py.emplace_back(
815 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 6));
816 result_py.emplace_back(
817 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 7));
818 result_py.emplace_back(
819 absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 4 + 8));
820
821 result_py.emplace_back("");
822 result_py.emplace_back(
823 " return casadi.Function('xdot', [X, U], [result])");
Austin Schuh2a1abec2024-07-10 20:31:16 -0700824
Austin Schuh6ea789e2024-07-27 13:45:53 -0700825 result_py.emplace_back("");
826
827 result_py.emplace_back("# Returns the derivative of our state vector");
828 result_py.emplace_back("# [thetas0, omegas0,");
829 result_py.emplace_back("# thetas1, omegas1,");
830 result_py.emplace_back("# thetas2, omegas2,");
831 result_py.emplace_back("# thetas3, omegas3,");
832 result_py.emplace_back("# theta, vx, vy, omega]");
833 result_py.emplace_back("def velocity_swerve_physics(X, U):");
834 WriteCasadiVelocityVariables(&result_py);
835
836 result_py.emplace_back("");
837 result_py.emplace_back(
838 " result = casadi.SX.sym('result', NUM_VELOCITY_STATES, 1)");
839 result_py.emplace_back("");
840
841 // And then write out the derivative of each state.
842 for (size_t m = 0; m < kNumModules; ++m) {
843 result_py.emplace_back(
844 absl::Substitute(" result[$0, 0] = omegas$1", m * 2 + 0, m));
845 result_py.emplace_back(
846 absl::Substitute(" result[$0, 0] = $1", m * 2 + 1,
847 ccode(*modules_[m].direct.alphas_eqn)));
848 }
849 result_py.emplace_back(
850 absl::Substitute(" result[$0, 0] = omega", kNumModules * 2 + 0));
851
852 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
853 kNumModules * 2 + 1,
854 ccode(*direct_accel_.get(0, 0))));
855 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
856 kNumModules * 2 + 2,
857 ccode(*direct_accel_.get(1, 0))));
858 result_py.emplace_back(absl::Substitute(" result[$0, 0] = $1",
859 kNumModules * 2 + 3,
860 ccode(*direct_angular_accel_)));
861
862 // result_py.emplace_back(
863 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 4));
864 // result_py.emplace_back(
865 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 5));
866 // result_py.emplace_back(
867 // absl::Substitute(" result[$0, 0] = 0.0", kNumModules * 3 + 6));
868
869 result_py.emplace_back("");
870 result_py.emplace_back(
871 " return casadi.Function('xdot', [X, U], [result])");
872
Austin Schuhb8b34be2024-07-14 16:06:19 -0700873 DefineVector2dFunction(
874 "contact_patch_velocity",
875 "# Returns the velocity of the wheel in global coordinates.",
876 [](const Module &m, int dimension) {
877 return ccode(*m.contact_patch_velocity.get(dimension, 0));
878 },
879 &result_py);
880 DefineVector2dFunction(
881 "wheel_ground_velocity",
882 "# Returns the velocity of the wheel in steer module coordinates.",
883 [](const Module &m, int dimension) {
884 return ccode(*m.wheel_ground_velocity.get(dimension, 0));
885 },
886 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700887
Austin Schuhb8b34be2024-07-14 16:06:19 -0700888 DefineVector2dFunction(
889 "wheel_slip_velocity",
890 "# Returns the difference in velocities of the wheel surface and the "
891 "ground.",
892 [](const Module &m, int dimension) {
893 return ccode(*m.wheel_slip_velocity.get(dimension, 0));
894 },
895 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700896
Austin Schuhb8b34be2024-07-14 16:06:19 -0700897 DefineScalarFunction(
898 "slip_angle", "Returns the slip angle of the ith wheel",
899 [](const Module &m) { return ccode(*m.slip_angle); }, &result_py);
900 DefineScalarFunction(
901 "slip_ratio", "Returns the slip ratio of the ith wheel",
902 [](const Module &m) { return ccode(*m.slip_ratio); }, &result_py);
903 DefineScalarFunction(
904 "module_angular_accel",
905 "Returns the angular acceleration of the robot due to the ith wheel",
Austin Schuh6ea789e2024-07-27 13:45:53 -0700906 [this](const Module &m) { return ccode(*div(m.full.torque, Js_)); },
907 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700908
Austin Schuhb8b34be2024-07-14 16:06:19 -0700909 DefineVector2dFunction(
910 "wheel_force",
911 "Returns the force on the wheel in steer module coordinates",
912 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700913 return ccode(
914 *std::vector<RCP<const Basic>>{m.full.Fwx, m.Fwy}[dimension]);
Austin Schuhb8b34be2024-07-14 16:06:19 -0700915 },
916 &result_py);
Austin Schuhb67a38f2024-07-04 13:48:38 -0700917
Austin Schuhb8b34be2024-07-14 16:06:19 -0700918 DefineVector2dFunction(
919 "F", "Returns the force on the wheel in absolute coordinates",
920 [](const Module &m, int dimension) {
Austin Schuh6ea789e2024-07-27 13:45:53 -0700921 return ccode(*m.full.F.get(dimension, 0));
Austin Schuhb8b34be2024-07-14 16:06:19 -0700922 },
923 &result_py);
924
justinT21820767f2024-09-11 19:57:55 -0700925 DefineVector2dVelocityFunction(
926 "F_vel",
927 "Returns the force on the wheel in absolute coordinates based on the "
928 "velocity controller",
929 [](const Module &m, int dimension) {
930 return ccode(*m.direct.F.get(dimension, 0));
931 },
932 &result_py);
933
934 DefineVector2dVelocityFunction(
Austin Schuhb8b34be2024-07-14 16:06:19 -0700935 "mounting_location",
936 "Returns the mounting location of wheel in robot coordinates",
937 [](const Module &m, int dimension) {
938 return ccode(*m.mounting_location.get(dimension, 0));
939 },
940 &result_py);
941
Austin Schuh5dac2292024-10-19 13:56:58 -0700942 DefineVector2dVelocityFunction(
943 "rotated_mounting_location",
944 "Returns the mounting location of wheel in field aligned coordinates",
945 [](const Module &m, int dimension) {
946 return ccode(*m.rotated_mounting_location.get(dimension, 0));
947 },
948 &result_py);
949
Austin Schuhb8b34be2024-07-14 16:06:19 -0700950 DefineScalarFunction(
951 "Ms", "Returns the self aligning moment of the ith wheel",
952 [this](const Module &m) {
953 return ccode(*(div(m.Ms, add(Jsm_, div(div(Js_, Gs_), Gs_)))));
954 },
955 &result_py);
Austin Schuh0f881092024-06-28 15:36:48 -0700956
957 aos::util::WriteStringToFileOrDie(py_path, absl::StrJoin(result_py, "\n"));
958 }
959
Austin Schuhb8b34be2024-07-14 16:06:19 -0700960 void DefineScalarFunction(
961 std::string_view name, std::string_view documentation,
962 std::function<std::string(const Module &)> scalar_fn,
963 std::vector<std::string> *result_py) {
964 result_py->emplace_back("");
965 result_py->emplace_back(absl::Substitute("# $0.", documentation));
966 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
967 WriteCasadiVariables(result_py);
968 for (size_t m = 0; m < kNumModules; ++m) {
969 if (m == 0) {
970 result_py->emplace_back(" if i == 0:");
971 } else {
972 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
973 }
974 result_py->emplace_back(
975 absl::Substitute(" return casadi.Function('$0', [X, U], [$1])",
976 name, scalar_fn(modules_[m])));
977 }
978 result_py->emplace_back(" raise ValueError(\"Invalid module number\")");
979 }
980
981 void DefineVector2dFunction(
982 std::string_view name, std::string_view documentation,
983 std::function<std::string(const Module &, int)> scalar_fn,
984 std::vector<std::string> *result_py) {
985 result_py->emplace_back("");
986 result_py->emplace_back(absl::Substitute("# $0.", documentation));
987 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
988 WriteCasadiVariables(result_py);
989 result_py->emplace_back(
990 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
991 for (size_t m = 0; m < kNumModules; ++m) {
992 if (m == 0) {
993 result_py->emplace_back(" if i == 0:");
994 } else {
995 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
996 }
997 for (int j = 0; j < 2; ++j) {
998 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
999 j, scalar_fn(modules_[m], j)));
1000 }
1001 }
1002 result_py->emplace_back(" else:");
1003 result_py->emplace_back(
1004 " raise ValueError(\"Invalid module number\")");
1005 result_py->emplace_back(absl::Substitute(
1006 " return casadi.Function('$0', [X, U], [result])", name));
1007 }
1008
justinT21820767f2024-09-11 19:57:55 -07001009 void DefineVector2dVelocityFunction(
1010 std::string_view name, std::string_view documentation,
1011 std::function<std::string(const Module &, int)> scalar_fn,
1012 std::vector<std::string> *result_py) {
1013 result_py->emplace_back("");
1014 result_py->emplace_back(absl::Substitute("# $0.", documentation));
1015 result_py->emplace_back(absl::Substitute("def $0(i, X, U):", name));
1016 WriteCasadiVelocityVariables(result_py);
1017 result_py->emplace_back(
1018 absl::Substitute(" result = casadi.SX.sym('$0', 2, 1)", name));
1019 for (size_t m = 0; m < kNumModules; ++m) {
1020 if (m == 0) {
1021 result_py->emplace_back(" if i == 0:");
1022 } else {
1023 result_py->emplace_back(absl::Substitute(" elif i == $0:", m));
1024 }
1025 for (int j = 0; j < 2; ++j) {
1026 result_py->emplace_back(absl::Substitute(" result[$0, 0] = $1",
1027 j, scalar_fn(modules_[m], j)));
1028 }
1029 }
1030 result_py->emplace_back(" else:");
1031 result_py->emplace_back(
1032 " raise ValueError(\"Invalid module number\")");
1033 result_py->emplace_back(absl::Substitute(
1034 " return casadi.Function('$0', [X, U], [result])", name));
1035 }
1036
justinT21446e4f62024-06-16 22:36:10 -07001037 private:
1038 static constexpr uint8_t kNumModules = 4;
1039
Austin Schuh6ea789e2024-07-27 13:45:53 -07001040 RCP<const Basic> SteerAccel(RCP<const Basic> Fwx, RCP<const Basic> Ms,
1041 RCP<const Basic> Is) {
1042 RCP<const Basic> lhms =
1043 mul(add(neg(wb_), mul(add(rs_, rp_), sub(integer(1), div(rb1_, rp_)))),
1044 mul(div(rw_, rb2_), neg(Fwx)));
1045 RCP<const Basic> lhs = add(add(Ms, div(mul(Kts_, Is), Gs_)), lhms);
1046 RCP<const Basic> rhs = add(Jsm_, div(div(Js_, Gs_), Gs_));
1047 return simplify(div(lhs, rhs));
1048 }
1049
justinT21446e4f62024-06-16 22:36:10 -07001050 Module ModulePhysics(const int m, DenseMatrix mounting_location) {
1051 VLOG(1) << "Solving module " << m;
1052
1053 Module result;
Austin Schuhb8b34be2024-07-14 16:06:19 -07001054 result.mounting_location = mounting_location;
justinT21446e4f62024-06-16 22:36:10 -07001055
1056 result.Is = symbol(absl::StrFormat("Is%u", m));
1057 result.Id = symbol(absl::StrFormat("Id%u", m));
1058
1059 RCP<const Symbol> thetamd = symbol(absl::StrFormat("theta_md%u", m));
1060 RCP<const Symbol> omegamd = symbol(absl::StrFormat("omega_md%u", m));
1061 RCP<const Symbol> alphamd = symbol(absl::StrFormat("alpha_md%u", m));
1062
1063 result.thetas = symbol(absl::StrFormat("thetas%u", m));
1064 result.omegas = symbol(absl::StrFormat("omegas%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -07001065 RCP<const Symbol> alphas = symbol(absl::StrFormat("alphas%u", m));
justinT21446e4f62024-06-16 22:36:10 -07001066
justinT21446e4f62024-06-16 22:36:10 -07001067 result.omegad = symbol(absl::StrFormat("omegad%u", m));
Austin Schuh6ea789e2024-07-27 13:45:53 -07001068 RCP<const Symbol> alphad = symbol(absl::StrFormat("alphad%u", m));
justinT21446e4f62024-06-16 22:36:10 -07001069
1070 // Velocity of the module in field coordinates
Austin Schuh2a1abec2024-07-10 20:31:16 -07001071 DenseMatrix robot_velocity = DenseMatrix(2, 1, {vx_, vy_});
justinT21446e4f62024-06-16 22:36:10 -07001072 VLOG(1) << "robot velocity: " << robot_velocity.__str__();
1073
1074 // Velocity of the contact patch in field coordinates
1075 DenseMatrix temp_matrix = DenseMatrix(2, 1);
1076 DenseMatrix temp_matrix2 = DenseMatrix(2, 1);
Austin Schuhbd75c482024-08-18 00:03:51 -07001077 DenseMatrix temp_matrix3 = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -07001078 result.contact_patch_velocity = DenseMatrix(2, 1);
justinT21446e4f62024-06-16 22:36:10 -07001079
Austin Schuhb8b34be2024-07-14 16:06:19 -07001080 mul_dense_dense(R(theta_), result.mounting_location, temp_matrix);
justinT21446e4f62024-06-16 22:36:10 -07001081 add_dense_dense(angle_cross(temp_matrix, omega_), robot_velocity,
1082 temp_matrix2);
1083 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuhbd75c482024-08-18 00:03:51 -07001084 DenseMatrix(2, 1, {neg(caster_), integer(0)}),
1085 temp_matrix3);
justinT21446e4f62024-06-16 22:36:10 -07001086 add_dense_dense(temp_matrix2,
Austin Schuhbd75c482024-08-18 00:03:51 -07001087 angle_cross(temp_matrix3, add(omega_, result.omegas)),
Austin Schuh2a1abec2024-07-10 20:31:16 -07001088 result.contact_patch_velocity);
justinT21446e4f62024-06-16 22:36:10 -07001089
1090 VLOG(1);
Austin Schuh2a1abec2024-07-10 20:31:16 -07001091 VLOG(1) << "contact patch velocity: "
1092 << result.contact_patch_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -07001093
1094 // Relative velocity of the surface of the wheel to the ground.
Austin Schuhb67a38f2024-07-04 13:48:38 -07001095 result.wheel_ground_velocity = DenseMatrix(2, 1);
Austin Schuh2a1abec2024-07-10 20:31:16 -07001096 mul_dense_dense(R(neg(add(result.thetas, theta_))),
1097 result.contact_patch_velocity,
Austin Schuhb67a38f2024-07-04 13:48:38 -07001098 result.wheel_ground_velocity);
justinT21446e4f62024-06-16 22:36:10 -07001099
Austin Schuhb8b34be2024-07-14 16:06:19 -07001100 // Compute the relative velocity between the wheel surface and the ground in
1101 // the wheel coordinate system.
1102 result.wheel_slip_velocity = DenseMatrix(2, 1);
1103 DenseMatrix wheel_velocity =
1104 DenseMatrix(2, 1, {mul(rw_, result.omegad), integer(0)});
1105 DenseMatrix negative_wheel_ground_velocity =
1106 DenseMatrix(2, 1,
1107 {neg(result.wheel_ground_velocity.get(0, 0)),
1108 neg(result.wheel_ground_velocity.get(1, 0))});
1109 add_dense_dense(negative_wheel_ground_velocity, wheel_velocity,
1110 result.wheel_slip_velocity);
1111
justinT21446e4f62024-06-16 22:36:10 -07001112 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001113 VLOG(1) << "wheel ground velocity: "
1114 << result.wheel_ground_velocity.__str__();
justinT21446e4f62024-06-16 22:36:10 -07001115
Austin Schuh5ddcb472024-07-21 17:55:34 -07001116 result.slip_angle = sin(neg(atan2(result.wheel_ground_velocity.get(1, 0),
1117 result.wheel_ground_velocity.get(0, 0))));
justinT21446e4f62024-06-16 22:36:10 -07001118
1119 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001120 VLOG(1) << "slip angle: " << result.slip_angle->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001121
Austin Schuh2a1abec2024-07-10 20:31:16 -07001122 // TODO(austin): Does this handle decel properly?
Austin Schuhb67a38f2024-07-04 13:48:38 -07001123 result.slip_ratio = div(
Austin Schuh2a1abec2024-07-10 20:31:16 -07001124 sub(mul(rw_, result.omegad), result.wheel_ground_velocity.get(0, 0)),
1125 SymEngine::max(
1126 {real_double(0.02), abs(result.wheel_ground_velocity.get(0, 0))}));
justinT21446e4f62024-06-16 22:36:10 -07001127 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001128 VLOG(1) << "Slip ratio " << result.slip_ratio->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001129
Austin Schuh6ea789e2024-07-27 13:45:53 -07001130 result.full.Fwx = simplify(mul(Cx_, result.slip_ratio));
Austin Schuhb67a38f2024-07-04 13:48:38 -07001131 result.Fwy = simplify(mul(Cy_, result.slip_angle));
justinT21446e4f62024-06-16 22:36:10 -07001132
Austin Schuh27694fa2024-07-20 16:29:49 -07001133 // The self-aligning moment needs to flip when the module flips direction.
Austin Schuh78a1b312024-08-18 17:21:34 -07001134 RCP<const Basic> softsign_velocity = add(
1135 div(integer(-2),
1136 add(integer(1), exp(mul(integer(100),
1137 result.wheel_ground_velocity.get(0, 0))))),
1138 integer(1));
1139 result.Ms =
1140 mul(neg(result.Fwy),
1141 add(div(mul(softsign_velocity, contact_patch_length_), integer(3)),
1142 caster_));
justinT21446e4f62024-06-16 22:36:10 -07001143 VLOG(1);
Austin Schuhb8b34be2024-07-14 16:06:19 -07001144 VLOG(1) << "Ms " << result.Ms->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001145 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001146 VLOG(1) << "full.Fwx " << result.full.Fwx->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001147 VLOG(1);
Austin Schuhb67a38f2024-07-04 13:48:38 -07001148 VLOG(1) << "Fwy " << result.Fwy->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001149
Austin Schuh6ea789e2024-07-27 13:45:53 -07001150 // -K_td * Id / Gd + Fwx * rw = 0
1151 // Fwx = K_td * Id / Gd / rw
1152 result.direct.Fwx = mul(Ktd_, div(result.Id, mul(Gd_, rw_)));
1153
1154 result.direct.alphas_eqn =
1155 SteerAccel(result.direct.Fwx, result.Ms, result.Is);
1156
1157 // d/dt omegas = ...
1158 result.full.alphas_eqn = SteerAccel(result.full.Fwx, result.Ms, result.Is);
justinT21446e4f62024-06-16 22:36:10 -07001159
1160 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001161 VLOG(1) << alphas->__str__() << " = " << result.full.alphas_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001162
Austin Schuh6ea789e2024-07-27 13:45:53 -07001163 RCP<const Basic> lhs =
1164 sub(mul(sub(div(add(rp_, rs_), rp_), integer(1)), alphas),
1165 mul(Gd1_, mul(Gd2_, alphamd)));
1166 RCP<const Basic> ddplanitary_eqn = sub(mul(Gd3_, lhs), alphad);
justinT21446e4f62024-06-16 22:36:10 -07001167
Austin Schuh6ea789e2024-07-27 13:45:53 -07001168 RCP<const Basic> full_drive_eqn =
1169 sub(add(mul(neg(Jdm_), div(alphamd, Gd_)),
1170 mul(Ktd_, div(neg(result.Id), Gd_))),
1171 mul(neg(result.full.Fwx), rw_));
justinT21446e4f62024-06-16 22:36:10 -07001172
Austin Schuh6ea789e2024-07-27 13:45:53 -07001173 VLOG(1) << "full_drive_eqn: " << full_drive_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001174
1175 // Substitute in ddplanitary_eqn so we get rid of alphamd
1176 map_basic_basic map;
1177 RCP<const Set> reals = interval(NegInf, Inf, true, true);
1178 RCP<const Set> solve_solution = solve(ddplanitary_eqn, alphamd, reals);
1179 map[alphamd] = solve_solution->get_args()[1]->get_args()[0];
1180 VLOG(1) << "temp: " << solve_solution->__str__();
Austin Schuh6ea789e2024-07-27 13:45:53 -07001181 RCP<const Basic> drive_eqn_subs = full_drive_eqn->subs(map);
justinT21446e4f62024-06-16 22:36:10 -07001182
1183 map.clear();
Austin Schuh6ea789e2024-07-27 13:45:53 -07001184 map[alphas] = result.full.alphas_eqn;
justinT21446e4f62024-06-16 22:36:10 -07001185 RCP<const Basic> drive_eqn_subs2 = drive_eqn_subs->subs(map);
1186 RCP<const Basic> drive_eqn_subs3 = simplify(drive_eqn_subs2);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001187 VLOG(1) << "full_drive_eqn simplified: " << drive_eqn_subs3->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001188
Austin Schuh6ea789e2024-07-27 13:45:53 -07001189 solve_solution = solve(drive_eqn_subs3, alphad, reals);
justinT21446e4f62024-06-16 22:36:10 -07001190
Austin Schuh6ea789e2024-07-27 13:45:53 -07001191 result.full.alphad_eqn =
justinT21446e4f62024-06-16 22:36:10 -07001192 simplify(solve_solution->get_args()[1]->get_args()[0]);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001193 VLOG(1) << "drive_accel: " << result.full.alphad_eqn->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001194
Austin Schuh2a1abec2024-07-10 20:31:16 -07001195 // Compute the resulting force from the module.
Austin Schuh6ea789e2024-07-27 13:45:53 -07001196 result.full.F = DenseMatrix(2, 1);
Austin Schuhb8b34be2024-07-14 16:06:19 -07001197 mul_dense_dense(R(add(theta_, result.thetas)),
Austin Schuh6ea789e2024-07-27 13:45:53 -07001198 DenseMatrix(2, 1, {result.full.Fwx, result.Fwy}),
1199 result.full.F);
justinT21d18f79f2024-09-22 19:43:05 -07001200
Austin Schuh5dac2292024-10-19 13:56:58 -07001201 result.rotated_mounting_location = DenseMatrix(2, 1);
justinT21d18f79f2024-09-22 19:43:05 -07001202 mul_dense_dense(R(theta_), result.mounting_location,
Austin Schuh5dac2292024-10-19 13:56:58 -07001203 result.rotated_mounting_location);
1204 result.full.torque =
1205 force_cross(result.rotated_mounting_location, result.full.F);
justinT21446e4f62024-06-16 22:36:10 -07001206
Austin Schuh6ea789e2024-07-27 13:45:53 -07001207 result.direct.F = DenseMatrix(2, 1);
1208 mul_dense_dense(R(add(theta_, result.thetas)),
1209 DenseMatrix(2, 1, {result.direct.Fwx, result.Fwy}),
1210 result.direct.F);
1211 result.direct.torque =
Austin Schuh5dac2292024-10-19 13:56:58 -07001212 force_cross(result.rotated_mounting_location, result.direct.F);
justinT21446e4f62024-06-16 22:36:10 -07001213
1214 VLOG(1);
Austin Schuh6ea789e2024-07-27 13:45:53 -07001215 VLOG(1) << "full torque = " << result.full.torque->__str__();
1216 VLOG(1) << "direct torque = " << result.full.torque->__str__();
justinT21446e4f62024-06-16 22:36:10 -07001217
justinT21446e4f62024-06-16 22:36:10 -07001218 return result;
1219 }
1220
1221 DenseMatrix R(const RCP<const Basic> theta) {
1222 return DenseMatrix(2, 2,
1223 {cos(theta), neg(sin(theta)), sin(theta), cos(theta)});
1224 }
1225
1226 DenseMatrix angle_cross(DenseMatrix a, RCP<const Basic> b) {
Austin Schuh2a1abec2024-07-10 20:31:16 -07001227 return DenseMatrix(2, 1, {mul(neg(a.get(1, 0)), b), mul(a.get(0, 0), b)});
justinT21446e4f62024-06-16 22:36:10 -07001228 }
1229
1230 RCP<const Basic> force_cross(DenseMatrix r, DenseMatrix f) {
1231 return sub(mul(r.get(0, 0), f.get(1, 0)), mul(r.get(1, 0), f.get(0, 0)));
1232 }
1233
1234 // z represents the number of teeth per gear, theta is the angle between
1235 // shafts(in degrees), D_02 is the pitch diameter of gear 2 and b_2 is the
1236 // length of the tooth of gear 2
1237 // returns std::pair(r_01, r_02)
1238 std::pair<double, double> GetBevelPitchRadius(double z1, double z2,
1239 double theta, double D_02,
1240 double b_2) {
1241 double gamma_1 = std::atan2(z1, z2);
1242 double gamma_2 = theta / 180.0 * std::numbers::pi - gamma_1;
1243 double R_m = D_02 / 2 / std::sin(gamma_2) - b_2 / 2;
1244 return std::pair(R_m * std::cos(gamma_2), R_m * std::sin(gamma_2));
1245 }
1246
1247 Motor drive_motor_;
1248 Motor steer_motor_;
1249
1250 RCP<const Basic> Cx_;
1251 RCP<const Basic> Cy_;
Austin Schuh2a1abec2024-07-10 20:31:16 -07001252 RCP<const Basic> rw_;
justinT21446e4f62024-06-16 22:36:10 -07001253 RCP<const Basic> m_;
1254 RCP<const Basic> J_;
1255 RCP<const Basic> Gd1_;
1256 RCP<const Basic> rs_;
1257 RCP<const Basic> rp_;
1258 RCP<const Basic> Gd2_;
1259 RCP<const Basic> rb1_;
1260 RCP<const Basic> rb2_;
1261 RCP<const Basic> Gd3_;
1262 RCP<const Basic> Gd_;
1263 RCP<const Basic> Js_;
1264 RCP<const Basic> Gs_;
1265 RCP<const Basic> wb_;
1266 RCP<const Basic> Jdm_;
1267 RCP<const Basic> Jsm_;
1268 RCP<const Basic> Kts_;
1269 RCP<const Basic> Ktd_;
1270 RCP<const Basic> robot_width_;
1271 RCP<const Basic> caster_;
1272 RCP<const Basic> contact_patch_length_;
1273 RCP<const Basic> x_;
1274 RCP<const Basic> y_;
1275 RCP<const Basic> theta_;
1276 RCP<const Basic> vx_;
1277 RCP<const Basic> vy_;
1278 RCP<const Basic> omega_;
1279 RCP<const Basic> ax_;
1280 RCP<const Basic> ay_;
1281 RCP<const Basic> atheta_;
1282
1283 std::array<Module, kNumModules> modules_;
1284
Austin Schuh6ea789e2024-07-27 13:45:53 -07001285 DenseMatrix full_accel_;
1286 RCP<const Basic> full_angular_accel_;
1287 DenseMatrix direct_accel_;
1288 RCP<const Basic> direct_angular_accel_;
justinT21446e4f62024-06-16 22:36:10 -07001289};
1290
1291} // namespace frc971::control_loops::swerve
1292
1293int main(int argc, char **argv) {
1294 aos::InitGoogle(&argc, &argv);
1295
1296 frc971::control_loops::swerve::SwerveSimulation sim;
1297
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001298 if (!absl::GetFlag(FLAGS_cc_output_path).empty() &&
1299 !absl::GetFlag(FLAGS_h_output_path).empty()) {
1300 sim.Write(absl::GetFlag(FLAGS_cc_output_path),
1301 absl::GetFlag(FLAGS_h_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001302 }
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001303 if (!absl::GetFlag(FLAGS_casadi_py_output_path).empty()) {
1304 sim.WriteCasadi(absl::GetFlag(FLAGS_casadi_py_output_path));
Austin Schuh0f881092024-06-28 15:36:48 -07001305 }
justinT21446e4f62024-06-16 22:36:10 -07001306
Austin Schuha9550c02024-10-19 13:48:10 -07001307 if (!absl::GetFlag(FLAGS_constants_output_path).empty()) {
1308 sim.WriteConstantsFile(absl::GetFlag(FLAGS_constants_output_path));
1309 }
1310
justinT21446e4f62024-06-16 22:36:10 -07001311 return 0;
1312}