blob: 51f92b177fcfdd7a1630f41640a9403cbde4991c [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
Austin Schuha062edb2019-01-03 13:17:13 -08006#if defined(__linux__)
7#include "frc971/control_loops/hybrid_state_feedback_loop.h"
8#endif
Comran Morshed5323ecb2015-12-26 20:50:55 +00009#include "frc971/control_loops/state_feedback_loop.h"
Austin Schuha062edb2019-01-03 13:17:13 -080010#include "frc971/shifter_hall_effect.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000011
12namespace frc971 {
13namespace control_loops {
14namespace drivetrain {
15
16enum class ShifterType : int32_t {
17 HALL_EFFECT_SHIFTER = 0, // Detect when inbetween gears.
Adam Snaider18f44172016-10-22 15:30:21 -070018 SIMPLE_SHIFTER = 1, // Switch gears without speedmatch logic.
19 NO_SHIFTER = 2, // Only one gear ratio.
Comran Morshed5323ecb2015-12-26 20:50:55 +000020};
21
Comran Morshed76ca8f52016-02-21 17:26:28 +000022enum class LoopType : int32_t {
Adam Snaider18f44172016-10-22 15:30:21 -070023 OPEN_LOOP = 0, // Only use open loop logic.
Comran Morshed76ca8f52016-02-21 17:26:28 +000024 CLOSED_LOOP = 1, // Add in closed loop calculation.
25};
26
Campbell Crowley2527ed22017-02-17 21:10:02 -080027enum class GyroType : int32_t {
Austin Schuhd749d932020-12-30 21:38:40 -080028 SPARTAN_GYRO = 0, // Use the gyro on the spartan board.
29 IMU_X_GYRO = 1, // Use the x-axis of the gyro on the IMU.
30 IMU_Y_GYRO = 2, // Use the y-axis of the gyro on the IMU.
31 IMU_Z_GYRO = 3, // Use the z-axis of the gyro on the IMU.
32 FLIPPED_SPARTAN_GYRO = 4, // Use the gyro on the spartan board.
33 FLIPPED_IMU_Z_GYRO = 5, // Use the flipped z-axis of the gyro on the IMU.
Campbell Crowley2527ed22017-02-17 21:10:02 -080034};
35
Diana Burgessd0180f12018-03-21 21:24:17 -070036enum class IMUType : int32_t {
Austin Schuhe7f6ddf2019-03-22 20:29:49 -070037 IMU_X = 0, // Use the x-axis of the IMU.
38 IMU_Y = 1, // Use the y-axis of the IMU.
39 IMU_FLIPPED_X = 2, // Use the flipped x-axis of the IMU.
James Kuszmaul7f55f072020-03-01 10:21:26 -080040 IMU_Z = 3, // Use the z-axis of the IMU.
Diana Burgessd0180f12018-03-21 21:24:17 -070041};
42
James Kuszmaul207ae322022-02-25 21:15:31 -080043struct DownEstimatorConfig {
44 // Threshold, in g's, to use for detecting whether we are stopped in the down
45 // estimator.
46 double gravity_threshold = 0.025;
47 // Number of cycles of being still to require before taking accelerometer
48 // corrections.
49 int do_accel_corrections = 50;
50};
51
James Kuszmaulc29f4572023-02-25 17:02:33 -080052// Configuration for line-following mode.
53struct LineFollowConfig {
54 // The line-following uses an LQR controller with states of [theta,
55 // linear_velocity, angular_velocity] and inputs of [left_voltage,
56 // right_voltage].
57 // These Q and R matrices are the costs for state and input respectively.
58 Eigen::Matrix3d Q =
59 Eigen::Matrix3d((::Eigen::DiagonalMatrix<double, 3>().diagonal()
60 << 1.0 / ::std::pow(0.1, 2),
61 1.0 / ::std::pow(1.0, 2), 1.0 / ::std::pow(1.0, 2))
62 .finished()
63 .asDiagonal());
64 Eigen::Matrix2d R =
65 Eigen::Matrix2d((::Eigen::DiagonalMatrix<double, 2>().diagonal()
66 << 1.0 / ::std::pow(12.0, 2),
67 1.0 / ::std::pow(12.0, 2))
68 .finished()
69 .asDiagonal());
70
71 // The driver can use their steering controller to adjust where we attempt to
72 // place things laterally. This specifies how much range on either side of
73 // zero we allow them, in meters.
74 double max_controllable_offset = 0.1;
75};
76
Austin Schuhbcce26a2018-03-26 23:41:24 -070077template <typename Scalar = double>
Comran Morshed5323ecb2015-12-26 20:50:55 +000078struct DrivetrainConfig {
79 // Shifting method we are using.
80 ShifterType shifter_type;
81
Comran Morshed76ca8f52016-02-21 17:26:28 +000082 // Type of loop to use.
83 LoopType loop_type;
84
Campbell Crowley2527ed22017-02-17 21:10:02 -080085 // Type of gyro to use.
86 GyroType gyro_type;
87
Diana Burgessd0180f12018-03-21 21:24:17 -070088 // Type of IMU to use.
89 IMUType imu_type;
90
Comran Morshed5323ecb2015-12-26 20:50:55 +000091 // Polydrivetrain functions returning various controller loops with plants.
Austin Schuhbcce26a2018-03-26 23:41:24 -070092 ::std::function<StateFeedbackLoop<4, 2, 2, Scalar>()> make_drivetrain_loop;
93 ::std::function<StateFeedbackLoop<2, 2, 2, Scalar>()> make_v_drivetrain_loop;
94 ::std::function<StateFeedbackLoop<7, 2, 4, Scalar>()> make_kf_drivetrain_loop;
Austin Schuha062edb2019-01-03 13:17:13 -080095#if defined(__linux__)
96 ::std::function<
97 StateFeedbackLoop<2, 2, 2, Scalar, StateFeedbackHybridPlant<2, 2, 2>,
98 HybridKalman<2, 2, 2>>()>
99 make_hybrid_drivetrain_velocity_loop;
100#endif
Comran Morshed5323ecb2015-12-26 20:50:55 +0000101
Austin Schuhbb735b72019-01-03 12:58:41 -0800102 ::std::chrono::nanoseconds dt; // Control loop time step.
103 Scalar robot_radius; // Robot radius, in meters.
104 Scalar wheel_radius; // Wheel radius, in meters.
105 Scalar v; // Motor velocity constant.
Comran Morshed5323ecb2015-12-26 20:50:55 +0000106
Austin Schuh09fa9bb2016-02-16 11:47:40 -0800107 // Gear ratios, from wheel to motor shaft.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700108 Scalar high_gear_ratio;
109 Scalar low_gear_ratio;
Comran Morshed5323ecb2015-12-26 20:50:55 +0000110
Austin Schuhe6a9fdf2019-01-12 16:05:43 -0800111 // Moment of inertia and mass.
112 Scalar J;
113 Scalar mass;
114
Comran Morshed5323ecb2015-12-26 20:50:55 +0000115 // Hall effect constants. Unused if not applicable to shifter type.
116 constants::ShifterHallEffect left_drive;
117 constants::ShifterHallEffect right_drive;
Adam Snaiderbc918b62016-02-27 21:03:39 -0800118
119 // Variable that holds the default gear ratio. We use this in ZeroOutputs().
120 // (ie. true means high gear is default).
121 bool default_high_gear;
Austin Schuh889fee82016-04-13 22:16:36 -0700122
Austin Schuhbcce26a2018-03-26 23:41:24 -0700123 Scalar down_offset;
Adam Snaider94a52372016-10-19 20:06:01 -0700124
Austin Schuhbcce26a2018-03-26 23:41:24 -0700125 Scalar wheel_non_linearity;
Adam Snaider94a52372016-10-19 20:06:01 -0700126
Austin Schuhbcce26a2018-03-26 23:41:24 -0700127 Scalar quickturn_wheel_multiplier;
Austin Schuhd91c0d22016-10-15 21:24:28 -0700128
Austin Schuhbcce26a2018-03-26 23:41:24 -0700129 Scalar wheel_multiplier;
Austin Schuhe8a54c02018-03-05 00:25:58 -0800130
James Kuszmaul8bad2412019-03-10 10:47:56 -0700131 // Whether the shift button on the pistol grip enables line following mode.
132 bool pistol_grip_shift_enables_line_follow = false;
133
James Kuszmaul2215d922020-02-11 17:17:26 -0800134 // Rotation matrix from the IMU's coordinate frame to the robot's coordinate
135 // frame.
136 // I.e., imu_transform * imu_readings will give the imu readings in the
137 // robot frame.
James Kuszmauld478f872020-03-16 20:54:27 -0700138 Eigen::Matrix<Scalar, 3, 3> imu_transform =
139 Eigen::Matrix<Scalar, 3, 3>::Identity();
James Kuszmaul2215d922020-02-11 17:17:26 -0800140
141 // True if we are running a simulated drivetrain.
142 bool is_simulated = false;
143
James Kuszmaul207ae322022-02-25 21:15:31 -0800144 DownEstimatorConfig down_estimator_config{};
145
James Kuszmaulc29f4572023-02-25 17:02:33 -0800146 LineFollowConfig line_follow_config{};
147
Austin Schuhd91c0d22016-10-15 21:24:28 -0700148 // Converts the robot state to a linear distance position, velocity.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700149 static Eigen::Matrix<Scalar, 2, 1> LeftRightToLinear(
150 const Eigen::Matrix<Scalar, 7, 1> &left_right) {
151 Eigen::Matrix<Scalar, 2, 1> linear;
Austin Schuhd91c0d22016-10-15 21:24:28 -0700152 linear(0, 0) = (left_right(0, 0) + left_right(2, 0)) / 2.0;
153 linear(1, 0) = (left_right(1, 0) + left_right(3, 0)) / 2.0;
154 return linear;
155 }
156 // Converts the robot state to an anglular distance, velocity.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700157 Eigen::Matrix<Scalar, 2, 1> LeftRightToAngular(
158 const Eigen::Matrix<Scalar, 7, 1> &left_right) const {
159 Eigen::Matrix<Scalar, 2, 1> angular;
Austin Schuhd91c0d22016-10-15 21:24:28 -0700160 angular(0, 0) =
161 (left_right(2, 0) - left_right(0, 0)) / (this->robot_radius * 2.0);
162 angular(1, 0) =
163 (left_right(3, 0) - left_right(1, 0)) / (this->robot_radius * 2.0);
164 return angular;
165 }
166
Alex Perrye32eabc2019-02-08 19:51:19 -0800167 Eigen::Matrix<Scalar, 2, 2> Tlr_to_la() const {
168 return (::Eigen::Matrix<Scalar, 2, 2>() << 0.5, 0.5,
Austin Schuhd749d932020-12-30 21:38:40 -0800169 -1.0 / (2 * robot_radius), 1.0 / (2 * robot_radius))
170 .finished();
Alex Perrye32eabc2019-02-08 19:51:19 -0800171 }
172
173 Eigen::Matrix<Scalar, 2, 2> Tla_to_lr() const {
174 return Tlr_to_la().inverse();
175 }
176
Austin Schuhd91c0d22016-10-15 21:24:28 -0700177 // Converts the linear and angular position, velocity to the top 4 states of
178 // the robot state.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700179 Eigen::Matrix<Scalar, 4, 1> AngularLinearToLeftRight(
180 const Eigen::Matrix<Scalar, 2, 1> &linear,
181 const Eigen::Matrix<Scalar, 2, 1> &angular) const {
Austin Schuhd749d932020-12-30 21:38:40 -0800182 Eigen::Matrix<Scalar, 2, 1> scaled_angle = angular * this->robot_radius;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700183 Eigen::Matrix<Scalar, 4, 1> state;
Austin Schuhd91c0d22016-10-15 21:24:28 -0700184 state(0, 0) = linear(0, 0) - scaled_angle(0, 0);
185 state(1, 0) = linear(1, 0) - scaled_angle(1, 0);
186 state(2, 0) = linear(0, 0) + scaled_angle(0, 0);
187 state(3, 0) = linear(1, 0) + scaled_angle(1, 0);
188 return state;
189 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000190};
Comran Morshed5323ecb2015-12-26 20:50:55 +0000191} // namespace drivetrain
192} // namespace control_loops
193} // namespace frc971
194
195#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_CONSTANTS_H_