blob: 1fd633a10ff187c0fe81072c91d5e53fe3fbf82c [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
Comran Morshed5323ecb2015-12-26 20:50:55 +000024struct DrivetrainConfig {
25 // Shifting method we are using.
26 ShifterType shifter_type;
27
Comran Morshed76ca8f52016-02-21 17:26:28 +000028 // Type of loop to use.
29 LoopType loop_type;
30
Comran Morshed5323ecb2015-12-26 20:50:55 +000031 // Polydrivetrain functions returning various controller loops with plants.
32 ::std::function<StateFeedbackLoop<4, 2, 2>()> make_drivetrain_loop;
33 ::std::function<StateFeedbackLoop<2, 2, 2>()> make_v_drivetrain_loop;
34 ::std::function<StateFeedbackLoop<7, 2, 3>()> make_kf_drivetrain_loop;
35
Adam Snaider18f44172016-10-22 15:30:21 -070036 double dt; // Control loop time step.
Comran Morshed5323ecb2015-12-26 20:50:55 +000037 double robot_radius; // Robot radius, in meters.
38 double wheel_radius; // Wheel radius, in meters.
Adam Snaider18f44172016-10-22 15:30:21 -070039 double v; // Motor velocity constant.
Comran Morshed5323ecb2015-12-26 20:50:55 +000040
Austin Schuh09fa9bb2016-02-16 11:47:40 -080041 // Gear ratios, from wheel to motor shaft.
Comran Morshed5323ecb2015-12-26 20:50:55 +000042 double high_gear_ratio;
43 double low_gear_ratio;
44
45 // Hall effect constants. Unused if not applicable to shifter type.
46 constants::ShifterHallEffect left_drive;
47 constants::ShifterHallEffect right_drive;
Adam Snaiderbc918b62016-02-27 21:03:39 -080048
49 // Variable that holds the default gear ratio. We use this in ZeroOutputs().
50 // (ie. true means high gear is default).
51 bool default_high_gear;
Austin Schuh889fee82016-04-13 22:16:36 -070052
53 double down_offset;
Adam Snaider94a52372016-10-19 20:06:01 -070054
55 double wheel_non_linearity;
56
57 double quickturn_wheel_multiplier;
Austin Schuhd91c0d22016-10-15 21:24:28 -070058
59 // Converts the robot state to a linear distance position, velocity.
60 static Eigen::Matrix<double, 2, 1> LeftRightToLinear(
61 const Eigen::Matrix<double, 7, 1> &left_right) {
62 Eigen::Matrix<double, 2, 1> linear;
63 linear(0, 0) = (left_right(0, 0) + left_right(2, 0)) / 2.0;
64 linear(1, 0) = (left_right(1, 0) + left_right(3, 0)) / 2.0;
65 return linear;
66 }
67 // Converts the robot state to an anglular distance, velocity.
68 Eigen::Matrix<double, 2, 1> LeftRightToAngular(
69 const Eigen::Matrix<double, 7, 1> &left_right) const {
70 Eigen::Matrix<double, 2, 1> angular;
71 angular(0, 0) =
72 (left_right(2, 0) - left_right(0, 0)) / (this->robot_radius * 2.0);
73 angular(1, 0) =
74 (left_right(3, 0) - left_right(1, 0)) / (this->robot_radius * 2.0);
75 return angular;
76 }
77
78 // Converts the linear and angular position, velocity to the top 4 states of
79 // the robot state.
80 Eigen::Matrix<double, 4, 1> AngularLinearToLeftRight(
81 const Eigen::Matrix<double, 2, 1> &linear,
82 const Eigen::Matrix<double, 2, 1> &angular) const {
83 Eigen::Matrix<double, 2, 1> scaled_angle =
84 angular * this->robot_radius;
85 Eigen::Matrix<double, 4, 1> state;
86 state(0, 0) = linear(0, 0) - scaled_angle(0, 0);
87 state(1, 0) = linear(1, 0) - scaled_angle(1, 0);
88 state(2, 0) = linear(0, 0) + scaled_angle(0, 0);
89 state(3, 0) = linear(1, 0) + scaled_angle(1, 0);
90 return state;
91 }
Comran Morshed5323ecb2015-12-26 20:50:55 +000092};
93
94} // namespace drivetrain
95} // namespace control_loops
96} // namespace frc971
97
98#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_CONSTANTS_H_