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 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 4 | #include "aos/commonmath.h" |
| 5 | #include "aos/controls/control_loop.h" |
| 6 | #include "aos/controls/polytope.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 7 | #include "aos/util/trapezoid_profile.h" |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 8 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 9 | #include "frc971/control_loops/coerce_goal.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "frc971/control_loops/control_loops_generated.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | #include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h" |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain_output_generated.h" |
| 14 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 15 | #include "frc971/control_loops/drivetrain/localizer.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 16 | #include "frc971/control_loops/state_feedback_loop.h" |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 17 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 18 | namespace frc971 { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 19 | namespace control_loops { |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 20 | namespace drivetrain { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 21 | |
| 22 | class DrivetrainMotorsSS { |
| 23 | public: |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 24 | DrivetrainMotorsSS(const DrivetrainConfig<double> &dt_config, |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 25 | StateFeedbackLoop<7, 2, 4> *kf, |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 26 | LocalizerInterface *localizer); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 27 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 28 | void SetGoal(const ::frc971::control_loops::drivetrain::Goal *goal); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 29 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 30 | // Computes the power to send out as part of the controller. Should be called |
| 31 | // when disabled (with enable_control_loop false) so the profiles get computed |
| 32 | // correctly. |
| 33 | // enable_control_loop includes the actual enable bit and if the loop will go |
| 34 | // out to hw. |
| 35 | void Update(bool enable_control_loop); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 36 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 37 | bool output_was_capped() const { return output_was_capped_; } |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 38 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 39 | void SetOutput(::frc971::control_loops::drivetrain::OutputT *output) const; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 40 | void PopulateStatus( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 41 | ::frc971::control_loops::drivetrain::StatusBuilder *builder) const; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 42 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 43 | private: |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 44 | void PolyCapU(Eigen::Matrix<double, 2, 1> *U); |
| 45 | void ScaleCapU(Eigen::Matrix<double, 2, 1> *U); |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 46 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 47 | const DrivetrainConfig<double> dt_config_; |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 48 | StateFeedbackLoop<7, 2, 4> *kf_; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 49 | Eigen::Matrix<double, 7, 1> unprofiled_goal_; |
| 50 | |
Austin Schuh | ba93d9e | 2016-03-18 22:38:57 -0700 | [diff] [blame] | 51 | double last_gyro_to_wheel_offset_ = 0; |
| 52 | |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 53 | constexpr static double kMaxVoltage = 12.0; |
| 54 | double max_voltage_ = kMaxVoltage; |
| 55 | |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 56 | // Reprsents +/- full power on each motor in U-space, aka the square from |
| 57 | // (-12, -12) to (12, 12). |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 58 | const ::aos::controls::HVPolytope<2, 4, 4> U_poly_; |
Austin Schuh | 093535c | 2016-03-05 23:21:00 -0800 | [diff] [blame] | 59 | |
| 60 | // multiplying by T converts [left_error, right_error] to |
| 61 | // [left_right_error_difference, total_distance_error]. |
| 62 | Eigen::Matrix<double, 2, 2> T_, T_inverse_; |
| 63 | |
| 64 | aos::util::TrapezoidProfile linear_profile_, angular_profile_; |
| 65 | |
| 66 | bool output_was_capped_ = false; |
| 67 | |
| 68 | bool use_profile_ = false; |
| 69 | |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 70 | LocalizerInterface *localizer_; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 73 | } // namespace drivetrain |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 74 | } // namespace control_loops |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 75 | } // namespace frc971 |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 76 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 77 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_ |