Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_ |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 3 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 4 | #include "aos/common/commonmath.h" |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 5 | #include "aos/common/controls/control_loop.h" |
| 6 | #include "aos/common/controls/polytope.h" |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 7 | #include "aos/common/logging/matrix_logging.h" |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 8 | #include "aos/common/util/trapezoid_profile.h" |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 9 | |
| 10 | #include "frc971/control_loops/state_feedback_loop.h" |
| 11 | #include "frc971/control_loops/coerce_goal.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 12 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 14 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 15 | namespace frc971 { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 16 | namespace control_loops { |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 17 | namespace drivetrain { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 18 | |
| 19 | class DrivetrainMotorsSS { |
| 20 | public: |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 21 | DrivetrainMotorsSS(const DrivetrainConfig &dt_config, |
| 22 | StateFeedbackLoop<7, 2, 3> *kf, |
| 23 | double *integrated_kf_heading); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 24 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 25 | void SetGoal(const ::frc971::control_loops::DrivetrainQueue::Goal &goal); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 26 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 27 | // Computes the power to send out as part of the controller. Should be called |
| 28 | // when disabled (with enable_control_loop false) so the profiles get computed |
| 29 | // correctly. |
| 30 | // enable_control_loop includes the actual enable bit and if the loop will go |
| 31 | // out to hw. |
| 32 | void Update(bool enable_control_loop); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 33 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 34 | bool output_was_capped() const { return output_was_capped_; } |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 35 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 36 | void SetOutput( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 37 | ::frc971::control_loops::DrivetrainQueue::Output *output) const; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 38 | void PopulateStatus( |
| 39 | ::frc971::control_loops::DrivetrainQueue::Status *status) const; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 40 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 41 | // Converts the robot state to a linear distance position, velocity. |
| 42 | Eigen::Matrix<double, 2, 1> LeftRightToLinear( |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 43 | const Eigen::Matrix<double, 7, 1> &left_right) const; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 44 | // Converts the robot state to an anglular distance, velocity. |
| 45 | Eigen::Matrix<double, 2, 1> LeftRightToAngular( |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 46 | const Eigen::Matrix<double, 7, 1> &left_right) const; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 47 | |
| 48 | // Converts the linear and angular position, velocity to the top 4 states of |
| 49 | // the robot state. |
| 50 | Eigen::Matrix<double, 4, 1> AngularLinearToLeftRight( |
| 51 | const Eigen::Matrix<double, 2, 1> &linear, |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 52 | const Eigen::Matrix<double, 2, 1> &angular) const; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 53 | |
| 54 | private: |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 55 | void PolyCapU(Eigen::Matrix<double, 2, 1> *U); |
| 56 | void ScaleCapU(Eigen::Matrix<double, 2, 1> *U); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 57 | |
| 58 | const DrivetrainConfig dt_config_; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 59 | StateFeedbackLoop<7, 2, 3> *kf_; |
| 60 | Eigen::Matrix<double, 7, 1> unprofiled_goal_; |
| 61 | |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 62 | double last_gyro_to_wheel_offset_ = 0; |
| 63 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 64 | // Reprsents +/- full power on each motor in U-space, aka the square from |
| 65 | // (-12, -12) to (12, 12). |
| 66 | const ::aos::controls::HPolytope<2> U_poly_; |
| 67 | |
| 68 | // multiplying by T converts [left_error, right_error] to |
| 69 | // [left_right_error_difference, total_distance_error]. |
| 70 | Eigen::Matrix<double, 2, 2> T_, T_inverse_; |
| 71 | |
| 72 | aos::util::TrapezoidProfile linear_profile_, angular_profile_; |
| 73 | |
| 74 | bool output_was_capped_ = false; |
| 75 | |
| 76 | bool use_profile_ = false; |
| 77 | |
| 78 | double *integrated_kf_heading_; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 81 | } // namespace drivetrain |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 82 | } // namespace control_loops |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 83 | } // namespace frc971 |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 84 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 85 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_ |