blob: 88547d558c3754d2c72ef9ea9f7bfe4cfd629a1d [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 {
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 Schuh90b43b42019-01-04 07:45:05 +110033 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 {
37 IMU_X = 0, // Use the x-axis of the IMU.
38 IMU_Y = 1, // Use the y-axis of the IMU.
39};
40
Austin Schuhbcce26a2018-03-26 23:41:24 -070041template <typename Scalar = double>
Comran Morshed5323ecb2015-12-26 20:50:55 +000042struct DrivetrainConfig {
43 // Shifting method we are using.
44 ShifterType shifter_type;
45
Comran Morshed76ca8f52016-02-21 17:26:28 +000046 // Type of loop to use.
47 LoopType loop_type;
48
Campbell Crowley2527ed22017-02-17 21:10:02 -080049 // Type of gyro to use.
50 GyroType gyro_type;
51
Diana Burgessd0180f12018-03-21 21:24:17 -070052 // Type of IMU to use.
53 IMUType imu_type;
54
Comran Morshed5323ecb2015-12-26 20:50:55 +000055 // Polydrivetrain functions returning various controller loops with plants.
Austin Schuhbcce26a2018-03-26 23:41:24 -070056 ::std::function<StateFeedbackLoop<4, 2, 2, Scalar>()> make_drivetrain_loop;
57 ::std::function<StateFeedbackLoop<2, 2, 2, Scalar>()> make_v_drivetrain_loop;
58 ::std::function<StateFeedbackLoop<7, 2, 4, Scalar>()> make_kf_drivetrain_loop;
Austin Schuha062edb2019-01-03 13:17:13 -080059#if defined(__linux__)
60 ::std::function<
61 StateFeedbackLoop<2, 2, 2, Scalar, StateFeedbackHybridPlant<2, 2, 2>,
62 HybridKalman<2, 2, 2>>()>
63 make_hybrid_drivetrain_velocity_loop;
64#endif
Comran Morshed5323ecb2015-12-26 20:50:55 +000065
Austin Schuhbb735b72019-01-03 12:58:41 -080066 ::std::chrono::nanoseconds dt; // Control loop time step.
67 Scalar robot_radius; // Robot radius, in meters.
68 Scalar wheel_radius; // Wheel radius, in meters.
69 Scalar v; // Motor velocity constant.
Comran Morshed5323ecb2015-12-26 20:50:55 +000070
Austin Schuh09fa9bb2016-02-16 11:47:40 -080071 // Gear ratios, from wheel to motor shaft.
Austin Schuhbcce26a2018-03-26 23:41:24 -070072 Scalar high_gear_ratio;
73 Scalar low_gear_ratio;
Comran Morshed5323ecb2015-12-26 20:50:55 +000074
Austin Schuhe6a9fdf2019-01-12 16:05:43 -080075 // Moment of inertia and mass.
76 Scalar J;
77 Scalar mass;
78
Comran Morshed5323ecb2015-12-26 20:50:55 +000079 // Hall effect constants. Unused if not applicable to shifter type.
80 constants::ShifterHallEffect left_drive;
81 constants::ShifterHallEffect right_drive;
Adam Snaiderbc918b62016-02-27 21:03:39 -080082
83 // Variable that holds the default gear ratio. We use this in ZeroOutputs().
84 // (ie. true means high gear is default).
85 bool default_high_gear;
Austin Schuh889fee82016-04-13 22:16:36 -070086
Austin Schuhbcce26a2018-03-26 23:41:24 -070087 Scalar down_offset;
Adam Snaider94a52372016-10-19 20:06:01 -070088
Austin Schuhbcce26a2018-03-26 23:41:24 -070089 Scalar wheel_non_linearity;
Adam Snaider94a52372016-10-19 20:06:01 -070090
Austin Schuhbcce26a2018-03-26 23:41:24 -070091 Scalar quickturn_wheel_multiplier;
Austin Schuhd91c0d22016-10-15 21:24:28 -070092
Austin Schuhbcce26a2018-03-26 23:41:24 -070093 Scalar wheel_multiplier;
Austin Schuhe8a54c02018-03-05 00:25:58 -080094
Austin Schuhd91c0d22016-10-15 21:24:28 -070095 // Converts the robot state to a linear distance position, velocity.
Austin Schuhbcce26a2018-03-26 23:41:24 -070096 static Eigen::Matrix<Scalar, 2, 1> LeftRightToLinear(
97 const Eigen::Matrix<Scalar, 7, 1> &left_right) {
98 Eigen::Matrix<Scalar, 2, 1> linear;
Austin Schuhd91c0d22016-10-15 21:24:28 -070099 linear(0, 0) = (left_right(0, 0) + left_right(2, 0)) / 2.0;
100 linear(1, 0) = (left_right(1, 0) + left_right(3, 0)) / 2.0;
101 return linear;
102 }
103 // Converts the robot state to an anglular distance, velocity.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700104 Eigen::Matrix<Scalar, 2, 1> LeftRightToAngular(
105 const Eigen::Matrix<Scalar, 7, 1> &left_right) const {
106 Eigen::Matrix<Scalar, 2, 1> angular;
Austin Schuhd91c0d22016-10-15 21:24:28 -0700107 angular(0, 0) =
108 (left_right(2, 0) - left_right(0, 0)) / (this->robot_radius * 2.0);
109 angular(1, 0) =
110 (left_right(3, 0) - left_right(1, 0)) / (this->robot_radius * 2.0);
111 return angular;
112 }
113
114 // Converts the linear and angular position, velocity to the top 4 states of
115 // the robot state.
Austin Schuhbcce26a2018-03-26 23:41:24 -0700116 Eigen::Matrix<Scalar, 4, 1> AngularLinearToLeftRight(
117 const Eigen::Matrix<Scalar, 2, 1> &linear,
118 const Eigen::Matrix<Scalar, 2, 1> &angular) const {
119 Eigen::Matrix<Scalar, 2, 1> scaled_angle =
Austin Schuhd91c0d22016-10-15 21:24:28 -0700120 angular * this->robot_radius;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700121 Eigen::Matrix<Scalar, 4, 1> state;
Austin Schuhd91c0d22016-10-15 21:24:28 -0700122 state(0, 0) = linear(0, 0) - scaled_angle(0, 0);
123 state(1, 0) = linear(1, 0) - scaled_angle(1, 0);
124 state(2, 0) = linear(0, 0) + scaled_angle(0, 0);
125 state(3, 0) = linear(1, 0) + scaled_angle(1, 0);
126 return state;
127 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000128};
Comran Morshed5323ecb2015-12-26 20:50:55 +0000129} // namespace drivetrain
130} // namespace control_loops
131} // namespace frc971
132
133#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_CONSTANTS_H_