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 | |
| 6 | #include "frc971/shifter_hall_effect.h" |
| 7 | #include "frc971/control_loops/state_feedback_loop.h" |
| 8 | |
| 9 | namespace frc971 { |
| 10 | namespace control_loops { |
| 11 | namespace drivetrain { |
| 12 | |
| 13 | enum class ShifterType : int32_t { |
| 14 | HALL_EFFECT_SHIFTER = 0, // Detect when inbetween gears. |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 15 | SIMPLE_SHIFTER = 1, // Switch gears without speedmatch logic. |
| 16 | NO_SHIFTER = 2, // Only one gear ratio. |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 17 | }; |
| 18 | |
Comran Morshed | 76ca8f5 | 2016-02-21 17:26:28 +0000 | [diff] [blame] | 19 | enum class LoopType : int32_t { |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 20 | OPEN_LOOP = 0, // Only use open loop logic. |
Comran Morshed | 76ca8f5 | 2016-02-21 17:26:28 +0000 | [diff] [blame] | 21 | CLOSED_LOOP = 1, // Add in closed loop calculation. |
| 22 | }; |
| 23 | |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 24 | enum class GyroType : int32_t { |
| 25 | SPARTAN_GYRO = 0, // Use the gyro on the spartan board. |
| 26 | IMU_X_GYRO = 1, // Use the x-axis of the gyro on the IMU. |
| 27 | IMU_Y_GYRO = 2, // Use the y-axis of the gyro on the IMU. |
| 28 | IMU_Z_GYRO = 3, // Use the z-axis of the gyro on the IMU. |
| 29 | FLIPPED_SPARTAN_GYRO = 4, // Use the gyro on the spartan board. |
| 30 | }; |
| 31 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 32 | enum class IMUType : int32_t { |
| 33 | IMU_X = 0, // Use the x-axis of the IMU. |
| 34 | IMU_Y = 1, // Use the y-axis of the IMU. |
| 35 | }; |
| 36 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 37 | template <typename Scalar = double> |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 38 | struct DrivetrainConfig { |
| 39 | // Shifting method we are using. |
| 40 | ShifterType shifter_type; |
| 41 | |
Comran Morshed | 76ca8f5 | 2016-02-21 17:26:28 +0000 | [diff] [blame] | 42 | // Type of loop to use. |
| 43 | LoopType loop_type; |
| 44 | |
Campbell Crowley | 2527ed2 | 2017-02-17 21:10:02 -0800 | [diff] [blame] | 45 | // Type of gyro to use. |
| 46 | GyroType gyro_type; |
| 47 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 48 | // Type of IMU to use. |
| 49 | IMUType imu_type; |
| 50 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 51 | // Polydrivetrain functions returning various controller loops with plants. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 52 | ::std::function<StateFeedbackLoop<4, 2, 2, Scalar>()> make_drivetrain_loop; |
| 53 | ::std::function<StateFeedbackLoop<2, 2, 2, Scalar>()> make_v_drivetrain_loop; |
| 54 | ::std::function<StateFeedbackLoop<7, 2, 4, Scalar>()> make_kf_drivetrain_loop; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 55 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 56 | Scalar dt; // Control loop time step. |
| 57 | Scalar robot_radius; // Robot radius, in meters. |
| 58 | Scalar wheel_radius; // Wheel radius, in meters. |
| 59 | Scalar v; // Motor velocity constant. |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 60 | |
Austin Schuh | 09fa9bb | 2016-02-16 11:47:40 -0800 | [diff] [blame] | 61 | // Gear ratios, from wheel to motor shaft. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 62 | Scalar high_gear_ratio; |
| 63 | Scalar low_gear_ratio; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 64 | |
| 65 | // Hall effect constants. Unused if not applicable to shifter type. |
| 66 | constants::ShifterHallEffect left_drive; |
| 67 | constants::ShifterHallEffect right_drive; |
Adam Snaider | bc918b6 | 2016-02-27 21:03:39 -0800 | [diff] [blame] | 68 | |
| 69 | // Variable that holds the default gear ratio. We use this in ZeroOutputs(). |
| 70 | // (ie. true means high gear is default). |
| 71 | bool default_high_gear; |
Austin Schuh | 889fee8 | 2016-04-13 22:16:36 -0700 | [diff] [blame] | 72 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 73 | Scalar down_offset; |
Adam Snaider | 94a5237 | 2016-10-19 20:06:01 -0700 | [diff] [blame] | 74 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 75 | Scalar wheel_non_linearity; |
Adam Snaider | 94a5237 | 2016-10-19 20:06:01 -0700 | [diff] [blame] | 76 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 77 | Scalar quickturn_wheel_multiplier; |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 78 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 79 | Scalar wheel_multiplier; |
Austin Schuh | e8a54c0 | 2018-03-05 00:25:58 -0800 | [diff] [blame] | 80 | |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 81 | // Converts the robot state to a linear distance position, velocity. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 82 | static Eigen::Matrix<Scalar, 2, 1> LeftRightToLinear( |
| 83 | const Eigen::Matrix<Scalar, 7, 1> &left_right) { |
| 84 | Eigen::Matrix<Scalar, 2, 1> linear; |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 85 | linear(0, 0) = (left_right(0, 0) + left_right(2, 0)) / 2.0; |
| 86 | linear(1, 0) = (left_right(1, 0) + left_right(3, 0)) / 2.0; |
| 87 | return linear; |
| 88 | } |
| 89 | // Converts the robot state to an anglular distance, velocity. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 90 | Eigen::Matrix<Scalar, 2, 1> LeftRightToAngular( |
| 91 | const Eigen::Matrix<Scalar, 7, 1> &left_right) const { |
| 92 | Eigen::Matrix<Scalar, 2, 1> angular; |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 93 | angular(0, 0) = |
| 94 | (left_right(2, 0) - left_right(0, 0)) / (this->robot_radius * 2.0); |
| 95 | angular(1, 0) = |
| 96 | (left_right(3, 0) - left_right(1, 0)) / (this->robot_radius * 2.0); |
| 97 | return angular; |
| 98 | } |
| 99 | |
| 100 | // Converts the linear and angular position, velocity to the top 4 states of |
| 101 | // the robot state. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 102 | Eigen::Matrix<Scalar, 4, 1> AngularLinearToLeftRight( |
| 103 | const Eigen::Matrix<Scalar, 2, 1> &linear, |
| 104 | const Eigen::Matrix<Scalar, 2, 1> &angular) const { |
| 105 | Eigen::Matrix<Scalar, 2, 1> scaled_angle = |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 106 | angular * this->robot_radius; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame^] | 107 | Eigen::Matrix<Scalar, 4, 1> state; |
Austin Schuh | d91c0d2 | 2016-10-15 21:24:28 -0700 | [diff] [blame] | 108 | state(0, 0) = linear(0, 0) - scaled_angle(0, 0); |
| 109 | state(1, 0) = linear(1, 0) - scaled_angle(1, 0); |
| 110 | state(2, 0) = linear(0, 0) + scaled_angle(0, 0); |
| 111 | state(3, 0) = linear(1, 0) + scaled_angle(1, 0); |
| 112 | return state; |
| 113 | } |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 114 | }; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 115 | } // namespace drivetrain |
| 116 | } // namespace control_loops |
| 117 | } // namespace frc971 |
| 118 | |
| 119 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_CONSTANTS_H_ |