blob: 6de98836c36c6b23d2ca66c71d2017a12f9de9a7 [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.
30};
31
Comran Morshed5323ecb2015-12-26 20:50:55 +000032struct DrivetrainConfig {
33 // Shifting method we are using.
34 ShifterType shifter_type;
35
Comran Morshed76ca8f52016-02-21 17:26:28 +000036 // Type of loop to use.
37 LoopType loop_type;
38
Campbell Crowley2527ed22017-02-17 21:10:02 -080039 // Type of gyro to use.
40 GyroType gyro_type;
41
Comran Morshed5323ecb2015-12-26 20:50:55 +000042 // Polydrivetrain functions returning various controller loops with plants.
43 ::std::function<StateFeedbackLoop<4, 2, 2>()> make_drivetrain_loop;
44 ::std::function<StateFeedbackLoop<2, 2, 2>()> make_v_drivetrain_loop;
45 ::std::function<StateFeedbackLoop<7, 2, 3>()> make_kf_drivetrain_loop;
46
Adam Snaider18f44172016-10-22 15:30:21 -070047 double dt; // Control loop time step.
Comran Morshed5323ecb2015-12-26 20:50:55 +000048 double robot_radius; // Robot radius, in meters.
49 double wheel_radius; // Wheel radius, in meters.
Adam Snaider18f44172016-10-22 15:30:21 -070050 double v; // Motor velocity constant.
Comran Morshed5323ecb2015-12-26 20:50:55 +000051
Austin Schuh09fa9bb2016-02-16 11:47:40 -080052 // Gear ratios, from wheel to motor shaft.
Comran Morshed5323ecb2015-12-26 20:50:55 +000053 double high_gear_ratio;
54 double low_gear_ratio;
55
56 // Hall effect constants. Unused if not applicable to shifter type.
57 constants::ShifterHallEffect left_drive;
58 constants::ShifterHallEffect right_drive;
Adam Snaiderbc918b62016-02-27 21:03:39 -080059
60 // Variable that holds the default gear ratio. We use this in ZeroOutputs().
61 // (ie. true means high gear is default).
62 bool default_high_gear;
Austin Schuh889fee82016-04-13 22:16:36 -070063
64 double down_offset;
Adam Snaider94a52372016-10-19 20:06:01 -070065
66 double wheel_non_linearity;
67
68 double quickturn_wheel_multiplier;
Austin Schuhd91c0d22016-10-15 21:24:28 -070069
Austin Schuhe8a54c02018-03-05 00:25:58 -080070 double wheel_multiplier;
71
Austin Schuhd91c0d22016-10-15 21:24:28 -070072 // Converts the robot state to a linear distance position, velocity.
73 static Eigen::Matrix<double, 2, 1> LeftRightToLinear(
74 const Eigen::Matrix<double, 7, 1> &left_right) {
75 Eigen::Matrix<double, 2, 1> linear;
76 linear(0, 0) = (left_right(0, 0) + left_right(2, 0)) / 2.0;
77 linear(1, 0) = (left_right(1, 0) + left_right(3, 0)) / 2.0;
78 return linear;
79 }
80 // Converts the robot state to an anglular distance, velocity.
81 Eigen::Matrix<double, 2, 1> LeftRightToAngular(
82 const Eigen::Matrix<double, 7, 1> &left_right) const {
83 Eigen::Matrix<double, 2, 1> angular;
84 angular(0, 0) =
85 (left_right(2, 0) - left_right(0, 0)) / (this->robot_radius * 2.0);
86 angular(1, 0) =
87 (left_right(3, 0) - left_right(1, 0)) / (this->robot_radius * 2.0);
88 return angular;
89 }
90
91 // Converts the linear and angular position, velocity to the top 4 states of
92 // the robot state.
93 Eigen::Matrix<double, 4, 1> AngularLinearToLeftRight(
94 const Eigen::Matrix<double, 2, 1> &linear,
95 const Eigen::Matrix<double, 2, 1> &angular) const {
96 Eigen::Matrix<double, 2, 1> scaled_angle =
97 angular * this->robot_radius;
98 Eigen::Matrix<double, 4, 1> state;
99 state(0, 0) = linear(0, 0) - scaled_angle(0, 0);
100 state(1, 0) = linear(1, 0) - scaled_angle(1, 0);
101 state(2, 0) = linear(0, 0) + scaled_angle(0, 0);
102 state(3, 0) = linear(1, 0) + scaled_angle(1, 0);
103 return state;
104 }
Comran Morshed5323ecb2015-12-26 20:50:55 +0000105};
106
107} // namespace drivetrain
108} // namespace control_loops
109} // namespace frc971
110
111#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_CONSTANTS_H_