blob: 476128d0c0a59337fac9fedd083c76647e4c361d [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
6#include "frc971/shifter_hall_effect.h"
7#include "frc971/control_loops/state_feedback_loop.h"
8
9namespace frc971 {
10namespace control_loops {
11namespace drivetrain {
12
13enum class ShifterType : int32_t {
14 HALL_EFFECT_SHIFTER = 0, // Detect when inbetween gears.
Adam Snaider18f44172016-10-22 15:30:21 -070015 SIMPLE_SHIFTER = 1, // Switch gears without speedmatch logic.
16 NO_SHIFTER = 2, // Only one gear ratio.
Comran Morshed5323ecb2015-12-26 20:50:55 +000017};
18
Comran Morshed76ca8f52016-02-21 17:26:28 +000019enum class LoopType : int32_t {
Adam Snaider18f44172016-10-22 15:30:21 -070020 OPEN_LOOP = 0, // Only use open loop logic.
Comran Morshed76ca8f52016-02-21 17:26:28 +000021 CLOSED_LOOP = 1, // Add in closed loop calculation.
22};
23
Campbell Crowley2527ed22017-02-17 21:10:02 -080024enum 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.
Austin Schuh90b43b42019-01-04 07:45:05 +110030 FLIPPED_IMU_Z_GYRO = 5, // Use the flipped z-axis of the gyro on the IMU.
Campbell Crowley2527ed22017-02-17 21:10:02 -080031};
32
Diana Burgessd0180f12018-03-21 21:24:17 -070033enum class IMUType : int32_t {
34 IMU_X = 0, // Use the x-axis of the IMU.
35 IMU_Y = 1, // Use the y-axis of the IMU.
36};
37
Austin Schuhbcce26a2018-03-26 23:41:24 -070038template <typename Scalar = double>
Comran Morshed5323ecb2015-12-26 20:50:55 +000039struct DrivetrainConfig {
40 // Shifting method we are using.
41 ShifterType shifter_type;
42
Comran Morshed76ca8f52016-02-21 17:26:28 +000043 // Type of loop to use.
44 LoopType loop_type;
45
Campbell Crowley2527ed22017-02-17 21:10:02 -080046 // Type of gyro to use.
47 GyroType gyro_type;
48
Diana Burgessd0180f12018-03-21 21:24:17 -070049 // Type of IMU to use.
50 IMUType imu_type;
51
Comran Morshed5323ecb2015-12-26 20:50:55 +000052 // Polydrivetrain functions returning various controller loops with plants.
Austin Schuhbcce26a2018-03-26 23:41:24 -070053 ::std::function<StateFeedbackLoop<4, 2, 2, Scalar>()> make_drivetrain_loop;
54 ::std::function<StateFeedbackLoop<2, 2, 2, Scalar>()> make_v_drivetrain_loop;
55 ::std::function<StateFeedbackLoop<7, 2, 4, Scalar>()> make_kf_drivetrain_loop;
Comran Morshed5323ecb2015-12-26 20:50:55 +000056
Austin Schuhbb735b72019-01-03 12:58:41 -080057 ::std::chrono::nanoseconds dt; // Control loop time step.
58 Scalar robot_radius; // Robot radius, in meters.
59 Scalar wheel_radius; // Wheel radius, in meters.
60 Scalar v; // Motor velocity constant.
Comran Morshed5323ecb2015-12-26 20:50:55 +000061
Austin Schuh09fa9bb2016-02-16 11:47:40 -080062 // Gear ratios, from wheel to motor shaft.
Austin Schuhbcce26a2018-03-26 23:41:24 -070063 Scalar high_gear_ratio;
64 Scalar low_gear_ratio;
Comran Morshed5323ecb2015-12-26 20:50:55 +000065
66 // Hall effect constants. Unused if not applicable to shifter type.
67 constants::ShifterHallEffect left_drive;
68 constants::ShifterHallEffect right_drive;
Adam Snaiderbc918b62016-02-27 21:03:39 -080069
70 // Variable that holds the default gear ratio. We use this in ZeroOutputs().
71 // (ie. true means high gear is default).
72 bool default_high_gear;
Austin Schuh889fee82016-04-13 22:16:36 -070073
Austin Schuhbcce26a2018-03-26 23:41:24 -070074 Scalar down_offset;
Adam Snaider94a52372016-10-19 20:06:01 -070075
Austin Schuhbcce26a2018-03-26 23:41:24 -070076 Scalar wheel_non_linearity;
Adam Snaider94a52372016-10-19 20:06:01 -070077
Austin Schuhbcce26a2018-03-26 23:41:24 -070078 Scalar quickturn_wheel_multiplier;
Austin Schuhd91c0d22016-10-15 21:24:28 -070079
Austin Schuhbcce26a2018-03-26 23:41:24 -070080 Scalar wheel_multiplier;
Austin Schuhe8a54c02018-03-05 00:25:58 -080081
Austin Schuhd91c0d22016-10-15 21:24:28 -070082 // Converts the robot state to a linear distance position, velocity.
Austin Schuhbcce26a2018-03-26 23:41:24 -070083 static Eigen::Matrix<Scalar, 2, 1> LeftRightToLinear(
84 const Eigen::Matrix<Scalar, 7, 1> &left_right) {
85 Eigen::Matrix<Scalar, 2, 1> linear;
Austin Schuhd91c0d22016-10-15 21:24:28 -070086 linear(0, 0) = (left_right(0, 0) + left_right(2, 0)) / 2.0;
87 linear(1, 0) = (left_right(1, 0) + left_right(3, 0)) / 2.0;
88 return linear;
89 }
90 // Converts the robot state to an anglular distance, velocity.
Austin Schuhbcce26a2018-03-26 23:41:24 -070091 Eigen::Matrix<Scalar, 2, 1> LeftRightToAngular(
92 const Eigen::Matrix<Scalar, 7, 1> &left_right) const {
93 Eigen::Matrix<Scalar, 2, 1> angular;
Austin Schuhd91c0d22016-10-15 21:24:28 -070094 angular(0, 0) =
95 (left_right(2, 0) - left_right(0, 0)) / (this->robot_radius * 2.0);
96 angular(1, 0) =
97 (left_right(3, 0) - left_right(1, 0)) / (this->robot_radius * 2.0);
98 return angular;
99 }
100
101 // Converts the linear and angular position, velocity to the top 4 states of
102 // the robot state.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700103 Eigen::Matrix<Scalar, 4, 1> AngularLinearToLeftRight(
104 const Eigen::Matrix<Scalar, 2, 1> &linear,
105 const Eigen::Matrix<Scalar, 2, 1> &angular) const {
106 Eigen::Matrix<Scalar, 2, 1> scaled_angle =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700107 angular * this->robot_radius;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700108 Eigen::Matrix<Scalar, 4, 1> state;
Austin Schuhd91c0d22016-10-15 21:24:28 -0700109 state(0, 0) = linear(0, 0) - scaled_angle(0, 0);
110 state(1, 0) = linear(1, 0) - scaled_angle(1, 0);
111 state(2, 0) = linear(0, 0) + scaled_angle(0, 0);
112 state(3, 0) = linear(1, 0) + scaled_angle(1, 0);
113 return state;
114 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000115};
Comran Morshed5323ecb2015-12-26 20:50:55 +0000116} // namespace drivetrain
117} // namespace control_loops
118} // namespace frc971
119
120#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_CONSTANTS_H_