Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_CONSTANTS_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_DRIVETRAIN_CONSTANTS_H_ |
| 3 | |
| 4 | #include <functional> |
| 5 | |
Austin Schuh | a062edb | 2019-01-03 13:17:13 -0800 | [diff] [blame] | 6 | #if defined(__linux__) |
| 7 | #include "frc971/control_loops/hybrid_state_feedback_loop.h" |
| 8 | #endif |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 9 | #include "frc971/control_loops/state_feedback_loop.h" |
Austin Schuh | a062edb | 2019-01-03 13:17:13 -0800 | [diff] [blame] | 10 | #include "frc971/shifter_hall_effect.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 11 | |
| 12 | namespace frc971 { |
| 13 | namespace control_loops { |
| 14 | namespace drivetrain { |
| 15 | |
| 16 | enum class ShifterType : int32_t { |
| 17 | HALL_EFFECT_SHIFTER = 0, // Detect when inbetween gears. |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 18 | SIMPLE_SHIFTER = 1, // Switch gears without speedmatch logic. |
| 19 | NO_SHIFTER = 2, // Only one gear ratio. |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 20 | }; |
| 21 | |
Comran Morshed | 76ca8f5 | 2016-02-21 17:26:28 +0000 | [diff] [blame] | 22 | enum class LoopType : int32_t { |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 23 | OPEN_LOOP = 0, // Only use open loop logic. |
Comran Morshed | 76ca8f5 | 2016-02-21 17:26:28 +0000 | [diff] [blame] | 24 | CLOSED_LOOP = 1, // Add in closed loop calculation. |
| 25 | }; |
| 26 | |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 27 | enum class GyroType : int32_t { |
| 28 | 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. |
Austin Schuh | 90b43b4 | 2019-01-04 07:45:05 +1100 | [diff] [blame] | 33 | FLIPPED_IMU_Z_GYRO = 5, // Use the flipped z-axis of the gyro on the IMU. |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 34 | }; |
| 35 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 36 | enum class IMUType : int32_t { |
Austin Schuh | e7f6ddf | 2019-03-22 20:29:49 -0700 | [diff] [blame] | 37 | 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 Kuszmaul | 7f55f07 | 2020-03-01 10:21:26 -0800 | [diff] [blame] | 40 | IMU_Z = 3, // Use the z-axis of the IMU. |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 43 | template <typename Scalar = double> |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 44 | struct DrivetrainConfig { |
| 45 | // Shifting method we are using. |
| 46 | ShifterType shifter_type; |
| 47 | |
Comran Morshed | 76ca8f5 | 2016-02-21 17:26:28 +0000 | [diff] [blame] | 48 | // Type of loop to use. |
| 49 | LoopType loop_type; |
| 50 | |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 51 | // Type of gyro to use. |
| 52 | GyroType gyro_type; |
| 53 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 54 | // Type of IMU to use. |
| 55 | IMUType imu_type; |
| 56 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 57 | // Polydrivetrain functions returning various controller loops with plants. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 58 | ::std::function<StateFeedbackLoop<4, 2, 2, Scalar>()> make_drivetrain_loop; |
| 59 | ::std::function<StateFeedbackLoop<2, 2, 2, Scalar>()> make_v_drivetrain_loop; |
| 60 | ::std::function<StateFeedbackLoop<7, 2, 4, Scalar>()> make_kf_drivetrain_loop; |
Austin Schuh | a062edb | 2019-01-03 13:17:13 -0800 | [diff] [blame] | 61 | #if defined(__linux__) |
| 62 | ::std::function< |
| 63 | StateFeedbackLoop<2, 2, 2, Scalar, StateFeedbackHybridPlant<2, 2, 2>, |
| 64 | HybridKalman<2, 2, 2>>()> |
| 65 | make_hybrid_drivetrain_velocity_loop; |
| 66 | #endif |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 67 | |
Austin Schuh | bb735b7 | 2019-01-03 12:58:41 -0800 | [diff] [blame] | 68 | ::std::chrono::nanoseconds dt; // Control loop time step. |
| 69 | Scalar robot_radius; // Robot radius, in meters. |
| 70 | Scalar wheel_radius; // Wheel radius, in meters. |
| 71 | Scalar v; // Motor velocity constant. |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 72 | |
Austin Schuh | 09fa9bb | 2016-02-16 11:47:40 -0800 | [diff] [blame] | 73 | // Gear ratios, from wheel to motor shaft. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 74 | Scalar high_gear_ratio; |
| 75 | Scalar low_gear_ratio; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 76 | |
Austin Schuh | e6a9fdf | 2019-01-12 16:05:43 -0800 | [diff] [blame] | 77 | // Moment of inertia and mass. |
| 78 | Scalar J; |
| 79 | Scalar mass; |
| 80 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 81 | // Hall effect constants. Unused if not applicable to shifter type. |
| 82 | constants::ShifterHallEffect left_drive; |
| 83 | constants::ShifterHallEffect right_drive; |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 84 | |
| 85 | // Variable that holds the default gear ratio. We use this in ZeroOutputs(). |
| 86 | // (ie. true means high gear is default). |
| 87 | bool default_high_gear; |
Austin Schuh | 889fee8 | 2016-04-13 22:16:36 -0700 | [diff] [blame] | 88 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 89 | Scalar down_offset; |
Adam Snaider | 94a5237 | 2016-10-19 20:06:01 -0700 | [diff] [blame] | 90 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 91 | Scalar wheel_non_linearity; |
Adam Snaider | 94a5237 | 2016-10-19 20:06:01 -0700 | [diff] [blame] | 92 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 93 | Scalar quickturn_wheel_multiplier; |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 94 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 95 | Scalar wheel_multiplier; |
Austin Schuh | e8a54c0 | 2018-03-05 00:25:58 -0800 | [diff] [blame] | 96 | |
James Kuszmaul | 8bad241 | 2019-03-10 10:47:56 -0700 | [diff] [blame] | 97 | // Whether the shift button on the pistol grip enables line following mode. |
| 98 | bool pistol_grip_shift_enables_line_follow = false; |
| 99 | |
James Kuszmaul | 2215d92 | 2020-02-11 17:17:26 -0800 | [diff] [blame] | 100 | // Rotation matrix from the IMU's coordinate frame to the robot's coordinate |
| 101 | // frame. |
| 102 | // I.e., imu_transform * imu_readings will give the imu readings in the |
| 103 | // robot frame. |
| 104 | Eigen::Matrix<double, 3, 3> imu_transform = |
| 105 | Eigen::Matrix<double, 3, 3>::Identity(); |
| 106 | |
| 107 | // True if we are running a simulated drivetrain. |
| 108 | bool is_simulated = false; |
| 109 | |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 110 | // Converts the robot state to a linear distance position, velocity. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 111 | static Eigen::Matrix<Scalar, 2, 1> LeftRightToLinear( |
| 112 | const Eigen::Matrix<Scalar, 7, 1> &left_right) { |
| 113 | Eigen::Matrix<Scalar, 2, 1> linear; |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 114 | linear(0, 0) = (left_right(0, 0) + left_right(2, 0)) / 2.0; |
| 115 | linear(1, 0) = (left_right(1, 0) + left_right(3, 0)) / 2.0; |
| 116 | return linear; |
| 117 | } |
| 118 | // Converts the robot state to an anglular distance, velocity. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 119 | Eigen::Matrix<Scalar, 2, 1> LeftRightToAngular( |
| 120 | const Eigen::Matrix<Scalar, 7, 1> &left_right) const { |
| 121 | Eigen::Matrix<Scalar, 2, 1> angular; |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 122 | angular(0, 0) = |
| 123 | (left_right(2, 0) - left_right(0, 0)) / (this->robot_radius * 2.0); |
| 124 | angular(1, 0) = |
| 125 | (left_right(3, 0) - left_right(1, 0)) / (this->robot_radius * 2.0); |
| 126 | return angular; |
| 127 | } |
| 128 | |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 129 | Eigen::Matrix<Scalar, 2, 2> Tlr_to_la() const { |
| 130 | return (::Eigen::Matrix<Scalar, 2, 2>() << 0.5, 0.5, |
| 131 | -1.0 / (2 * robot_radius), 1.0 / (2 * robot_radius)).finished(); |
| 132 | } |
| 133 | |
| 134 | Eigen::Matrix<Scalar, 2, 2> Tla_to_lr() const { |
| 135 | return Tlr_to_la().inverse(); |
| 136 | } |
| 137 | |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 138 | // Converts the linear and angular position, velocity to the top 4 states of |
| 139 | // the robot state. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 140 | Eigen::Matrix<Scalar, 4, 1> AngularLinearToLeftRight( |
| 141 | const Eigen::Matrix<Scalar, 2, 1> &linear, |
| 142 | const Eigen::Matrix<Scalar, 2, 1> &angular) const { |
| 143 | Eigen::Matrix<Scalar, 2, 1> scaled_angle = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 144 | angular * this->robot_radius; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 145 | Eigen::Matrix<Scalar, 4, 1> state; |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 146 | state(0, 0) = linear(0, 0) - scaled_angle(0, 0); |
| 147 | state(1, 0) = linear(1, 0) - scaled_angle(1, 0); |
| 148 | state(2, 0) = linear(0, 0) + scaled_angle(0, 0); |
| 149 | state(3, 0) = linear(1, 0) + scaled_angle(1, 0); |
| 150 | return state; |
| 151 | } |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 152 | }; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 153 | } // namespace drivetrain |
| 154 | } // namespace control_loops |
| 155 | } // namespace frc971 |
| 156 | |
| 157 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_CONSTANTS_H_ |