blob: 8e0a58da299b150baf4060b37507802c26668451 [file] [log] [blame]
Comran Morshed5323ecb2015-12-26 20:50:55 +00001#ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_CONSTANTS_H_
2#define FRC971_CONTROL_LOOPS_DRIVETRAIN_CONSTANTS_H_
3
4#include <functional>
5
James Kuszmaul68025332024-01-20 17:06:02 -08006#include "aos/flatbuffer_merge.h"
Austin Schuha062edb2019-01-03 13:17:13 -08007#if defined(__linux__)
8#include "frc971/control_loops/hybrid_state_feedback_loop.h"
James Kuszmaul68025332024-01-20 17:06:02 -08009#include "frc971/control_loops/hybrid_state_feedback_loop_converters.h"
Austin Schuha062edb2019-01-03 13:17:13 -080010#endif
James Kuszmaul68025332024-01-20 17:06:02 -080011#include "frc971/control_loops/drivetrain/drivetrain_config_static.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000012#include "frc971/control_loops/state_feedback_loop.h"
James Kuszmaul68025332024-01-20 17:06:02 -080013#include "frc971/control_loops/state_feedback_loop_converters.h"
Austin Schuha062edb2019-01-03 13:17:13 -080014#include "frc971/shifter_hall_effect.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000015
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080016namespace frc971::control_loops::drivetrain {
Comran Morshed5323ecb2015-12-26 20:50:55 +000017
James Kuszmaulc29f4572023-02-25 17:02:33 -080018// Configuration for line-following mode.
19struct LineFollowConfig {
20 // The line-following uses an LQR controller with states of [theta,
21 // linear_velocity, angular_velocity] and inputs of [left_voltage,
22 // right_voltage].
23 // These Q and R matrices are the costs for state and input respectively.
24 Eigen::Matrix3d Q =
25 Eigen::Matrix3d((::Eigen::DiagonalMatrix<double, 3>().diagonal()
26 << 1.0 / ::std::pow(0.1, 2),
27 1.0 / ::std::pow(1.0, 2), 1.0 / ::std::pow(1.0, 2))
28 .finished()
29 .asDiagonal());
30 Eigen::Matrix2d R =
31 Eigen::Matrix2d((::Eigen::DiagonalMatrix<double, 2>().diagonal()
32 << 1.0 / ::std::pow(12.0, 2),
33 1.0 / ::std::pow(12.0, 2))
34 .finished()
35 .asDiagonal());
36
37 // The driver can use their steering controller to adjust where we attempt to
38 // place things laterally. This specifies how much range on either side of
39 // zero we allow them, in meters.
40 double max_controllable_offset = 0.1;
James Kuszmaul68025332024-01-20 17:06:02 -080041
42 static LineFollowConfig FromFlatbuffer(const fbs::LineFollowConfig *fbs) {
43 if (fbs == nullptr) {
44 return {};
45 }
46 return LineFollowConfig{
47 .Q = ToEigenOrDie<3, 3>(*CHECK_NOTNULL(fbs->q())),
48 .R = ToEigenOrDie<2, 2>(*CHECK_NOTNULL(fbs->r())),
49 .max_controllable_offset = fbs->max_controllable_offset()};
50 }
James Kuszmaulc29f4572023-02-25 17:02:33 -080051};
52
Austin Schuhbcce26a2018-03-26 23:41:24 -070053template <typename Scalar = double>
Comran Morshed5323ecb2015-12-26 20:50:55 +000054struct DrivetrainConfig {
55 // Shifting method we are using.
56 ShifterType shifter_type;
57
Comran Morshed76ca8f52016-02-21 17:26:28 +000058 // Type of loop to use.
59 LoopType loop_type;
60
Campbell Crowley2527ed22017-02-17 21:10:02 -080061 // Type of gyro to use.
62 GyroType gyro_type;
63
Diana Burgessd0180f12018-03-21 21:24:17 -070064 // Type of IMU to use.
James Kuszmaul68025332024-01-20 17:06:02 -080065 ImuType imu_type;
Diana Burgessd0180f12018-03-21 21:24:17 -070066
Comran Morshed5323ecb2015-12-26 20:50:55 +000067 // Polydrivetrain functions returning various controller loops with plants.
Austin Schuhbcce26a2018-03-26 23:41:24 -070068 ::std::function<StateFeedbackLoop<4, 2, 2, Scalar>()> make_drivetrain_loop;
69 ::std::function<StateFeedbackLoop<2, 2, 2, Scalar>()> make_v_drivetrain_loop;
70 ::std::function<StateFeedbackLoop<7, 2, 4, Scalar>()> make_kf_drivetrain_loop;
Austin Schuha062edb2019-01-03 13:17:13 -080071#if defined(__linux__)
72 ::std::function<
73 StateFeedbackLoop<2, 2, 2, Scalar, StateFeedbackHybridPlant<2, 2, 2>,
74 HybridKalman<2, 2, 2>>()>
75 make_hybrid_drivetrain_velocity_loop;
76#endif
Comran Morshed5323ecb2015-12-26 20:50:55 +000077
Austin Schuhbb735b72019-01-03 12:58:41 -080078 ::std::chrono::nanoseconds dt; // Control loop time step.
79 Scalar robot_radius; // Robot radius, in meters.
80 Scalar wheel_radius; // Wheel radius, in meters.
81 Scalar v; // Motor velocity constant.
Comran Morshed5323ecb2015-12-26 20:50:55 +000082
Austin Schuh09fa9bb2016-02-16 11:47:40 -080083 // Gear ratios, from wheel to motor shaft.
Austin Schuhbcce26a2018-03-26 23:41:24 -070084 Scalar high_gear_ratio;
85 Scalar low_gear_ratio;
Comran Morshed5323ecb2015-12-26 20:50:55 +000086
Austin Schuhe6a9fdf2019-01-12 16:05:43 -080087 // Moment of inertia and mass.
88 Scalar J;
89 Scalar mass;
90
Comran Morshed5323ecb2015-12-26 20:50:55 +000091 // Hall effect constants. Unused if not applicable to shifter type.
James Kuszmaul68025332024-01-20 17:06:02 -080092 ShifterHallEffect left_drive;
93 ShifterHallEffect right_drive;
Adam Snaiderbc918b62016-02-27 21:03:39 -080094
95 // Variable that holds the default gear ratio. We use this in ZeroOutputs().
96 // (ie. true means high gear is default).
97 bool default_high_gear;
Austin Schuh889fee82016-04-13 22:16:36 -070098
Austin Schuhbcce26a2018-03-26 23:41:24 -070099 Scalar down_offset;
Adam Snaider94a52372016-10-19 20:06:01 -0700100
Austin Schuhbcce26a2018-03-26 23:41:24 -0700101 Scalar wheel_non_linearity;
Adam Snaider94a52372016-10-19 20:06:01 -0700102
Austin Schuhbcce26a2018-03-26 23:41:24 -0700103 Scalar quickturn_wheel_multiplier;
Austin Schuhd91c0d22016-10-15 21:24:28 -0700104
Austin Schuhbcce26a2018-03-26 23:41:24 -0700105 Scalar wheel_multiplier;
Austin Schuhe8a54c02018-03-05 00:25:58 -0800106
James Kuszmaul8bad2412019-03-10 10:47:56 -0700107 // Whether the shift button on the pistol grip enables line following mode.
108 bool pistol_grip_shift_enables_line_follow = false;
109
James Kuszmaul2215d922020-02-11 17:17:26 -0800110 // Rotation matrix from the IMU's coordinate frame to the robot's coordinate
111 // frame.
112 // I.e., imu_transform * imu_readings will give the imu readings in the
113 // robot frame.
James Kuszmauld478f872020-03-16 20:54:27 -0700114 Eigen::Matrix<Scalar, 3, 3> imu_transform =
115 Eigen::Matrix<Scalar, 3, 3>::Identity();
James Kuszmaul2215d922020-02-11 17:17:26 -0800116
117 // True if we are running a simulated drivetrain.
118 bool is_simulated = false;
119
James Kuszmaul68025332024-01-20 17:06:02 -0800120 DownEstimatorConfigT down_estimator_config{};
James Kuszmaul207ae322022-02-25 21:15:31 -0800121
James Kuszmaulc29f4572023-02-25 17:02:33 -0800122 LineFollowConfig line_follow_config{};
123
Maxwell Henderson24f89f32023-03-25 15:55:46 -0700124 PistolTopButtonUse top_button_use = PistolTopButtonUse::kShift;
125 PistolSecondButtonUse second_button_use = PistolSecondButtonUse::kShiftLow;
126 PistolBottomButtonUse bottom_button_use = PistolBottomButtonUse::kSlowDown;
127
Austin Schuhd91c0d22016-10-15 21:24:28 -0700128 // Converts the robot state to a linear distance position, velocity.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700129 static Eigen::Matrix<Scalar, 2, 1> LeftRightToLinear(
130 const Eigen::Matrix<Scalar, 7, 1> &left_right) {
131 Eigen::Matrix<Scalar, 2, 1> linear;
Austin Schuhd91c0d22016-10-15 21:24:28 -0700132 linear(0, 0) = (left_right(0, 0) + left_right(2, 0)) / 2.0;
133 linear(1, 0) = (left_right(1, 0) + left_right(3, 0)) / 2.0;
134 return linear;
135 }
136 // Converts the robot state to an anglular distance, velocity.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700137 Eigen::Matrix<Scalar, 2, 1> LeftRightToAngular(
138 const Eigen::Matrix<Scalar, 7, 1> &left_right) const {
139 Eigen::Matrix<Scalar, 2, 1> angular;
Austin Schuhd91c0d22016-10-15 21:24:28 -0700140 angular(0, 0) =
141 (left_right(2, 0) - left_right(0, 0)) / (this->robot_radius * 2.0);
142 angular(1, 0) =
143 (left_right(3, 0) - left_right(1, 0)) / (this->robot_radius * 2.0);
144 return angular;
145 }
146
Alex Perrye32eabc2019-02-08 19:51:19 -0800147 Eigen::Matrix<Scalar, 2, 2> Tlr_to_la() const {
148 return (::Eigen::Matrix<Scalar, 2, 2>() << 0.5, 0.5,
Austin Schuhd749d932020-12-30 21:38:40 -0800149 -1.0 / (2 * robot_radius), 1.0 / (2 * robot_radius))
150 .finished();
Alex Perrye32eabc2019-02-08 19:51:19 -0800151 }
152
153 Eigen::Matrix<Scalar, 2, 2> Tla_to_lr() const {
154 return Tlr_to_la().inverse();
155 }
156
Austin Schuhd91c0d22016-10-15 21:24:28 -0700157 // Converts the linear and angular position, velocity to the top 4 states of
158 // the robot state.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700159 Eigen::Matrix<Scalar, 4, 1> AngularLinearToLeftRight(
160 const Eigen::Matrix<Scalar, 2, 1> &linear,
161 const Eigen::Matrix<Scalar, 2, 1> &angular) const {
Austin Schuhd749d932020-12-30 21:38:40 -0800162 Eigen::Matrix<Scalar, 2, 1> scaled_angle = angular * this->robot_radius;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700163 Eigen::Matrix<Scalar, 4, 1> state;
Austin Schuhd91c0d22016-10-15 21:24:28 -0700164 state(0, 0) = linear(0, 0) - scaled_angle(0, 0);
165 state(1, 0) = linear(1, 0) - scaled_angle(1, 0);
166 state(2, 0) = linear(0, 0) + scaled_angle(0, 0);
167 state(3, 0) = linear(1, 0) + scaled_angle(1, 0);
168 return state;
169 }
James Kuszmaul68025332024-01-20 17:06:02 -0800170
171 static DrivetrainConfig FromFlatbuffer(const fbs::DrivetrainConfig &fbs) {
172 std::shared_ptr<aos::FlatbufferDetachedBuffer<fbs::DrivetrainConfig>>
173 fbs_copy = std::make_shared<
174 aos::FlatbufferDetachedBuffer<fbs::DrivetrainConfig>>(
175 aos::RecursiveCopyFlatBuffer(&fbs));
176 return {
177#define ASSIGN(field) .field = fbs.field()
178 ASSIGN(shifter_type), ASSIGN(loop_type), ASSIGN(gyro_type),
179 ASSIGN(imu_type),
180 .make_drivetrain_loop =
181 [fbs_copy]() {
182 return MakeStateFeedbackLoop<4, 2, 2>(*CHECK_NOTNULL(
183 fbs_copy->message().loop_config()->drivetrain_loop()));
184 },
185 .make_v_drivetrain_loop =
186 [fbs_copy]() {
187 return MakeStateFeedbackLoop<2, 2, 2>(
188 *CHECK_NOTNULL(fbs_copy->message()
189 .loop_config()
190 ->velocity_drivetrain_loop()));
191 },
192 .make_kf_drivetrain_loop =
193 [fbs_copy]() {
194 return MakeStateFeedbackLoop<7, 2, 4>(
195 *CHECK_NOTNULL(fbs_copy->message()
196 .loop_config()
197 ->kalman_drivetrain_loop()));
198 },
199#if defined(__linux__)
200 .make_hybrid_drivetrain_velocity_loop =
201 [fbs_copy]() {
202 return MakeHybridStateFeedbackLoop<2, 2, 2>(
203 *CHECK_NOTNULL(fbs_copy->message()
204 .loop_config()
205 ->hybrid_velocity_drivetrain_loop()));
206 },
207#endif
208 .dt = std::chrono::nanoseconds(fbs.loop_config()->dt()),
209 .robot_radius = fbs.loop_config()->robot_radius(),
210 .wheel_radius = fbs.loop_config()->wheel_radius(),
211 .v = fbs.loop_config()->motor_kv(),
212 .high_gear_ratio = fbs.loop_config()->high_gear_ratio(),
213 .low_gear_ratio = fbs.loop_config()->low_gear_ratio(),
214 .J = fbs.loop_config()->moment_of_inertia(),
James Kuszmaul2549e752024-01-20 17:42:51 -0800215 .mass = fbs.loop_config()->mass(),
216 .left_drive =
217 fbs.has_left_drive() ? *fbs.left_drive() : ShifterHallEffect{},
218 .right_drive =
219 fbs.has_right_drive() ? *fbs.right_drive() : ShifterHallEffect{},
220 ASSIGN(default_high_gear), ASSIGN(down_offset),
221 ASSIGN(wheel_non_linearity), ASSIGN(quickturn_wheel_multiplier),
222 ASSIGN(wheel_multiplier),
James Kuszmaul68025332024-01-20 17:06:02 -0800223 ASSIGN(pistol_grip_shift_enables_line_follow),
224 .imu_transform =
225 ToEigenOrDie<3, 3>(*CHECK_NOTNULL(fbs.imu_transform())),
226 ASSIGN(is_simulated),
227 .down_estimator_config =
228 aos::UnpackFlatbuffer(fbs.down_estimator_config()),
229 .line_follow_config =
230 LineFollowConfig::FromFlatbuffer(fbs.line_follow_config()),
231 ASSIGN(top_button_use), ASSIGN(second_button_use),
232 ASSIGN(bottom_button_use)
233#undef ASSIGN
234 };
235 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000236};
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800237} // namespace frc971::control_loops::drivetrain
Comran Morshed5323ecb2015-12-26 20:50:55 +0000238
239#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_CONSTANTS_H_