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 | |
| 4 | #include "aos/common/controls/polytope.h" |
| 5 | #include "aos/common/commonmath.h" |
| 6 | #include "aos/common/logging/matrix_logging.h" |
| 7 | |
| 8 | #include "frc971/control_loops/state_feedback_loop.h" |
| 9 | #include "frc971/control_loops/coerce_goal.h" |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 12 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 13 | namespace frc971 { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 14 | namespace control_loops { |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 15 | namespace drivetrain { |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 16 | |
| 17 | class DrivetrainMotorsSS { |
| 18 | public: |
| 19 | class LimitedDrivetrainLoop : public StateFeedbackLoop<4, 2, 2> { |
| 20 | public: |
| 21 | LimitedDrivetrainLoop(StateFeedbackLoop<4, 2, 2> &&loop); |
| 22 | |
| 23 | bool output_was_capped() const { |
| 24 | return output_was_capped_; |
| 25 | } |
| 26 | |
| 27 | private: |
| 28 | void CapU() override; |
| 29 | |
Brian Silverman | 4e1b066 | 2016-01-31 18:03:19 -0500 | [diff] [blame] | 30 | // Reprsents +/- full power on each motor in U-space, aka the square from |
| 31 | // (-12, -12) to (12, 12). |
| 32 | const ::aos::controls::HPolytope<2> U_poly_; |
| 33 | |
| 34 | // multiplying by T converts [left_error, right_error] to |
| 35 | // [left_right_error_difference, total_distance_error]. |
| 36 | Eigen::Matrix<double, 2, 2> T_, T_inverse_; |
| 37 | |
| 38 | bool output_was_capped_ = false; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 41 | DrivetrainMotorsSS(const DrivetrainConfig &dt_config); |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 42 | |
| 43 | void SetGoal(double left, double left_velocity, double right, |
| 44 | double right_velocity); |
| 45 | |
| 46 | void SetRawPosition(double left, double right); |
| 47 | |
| 48 | void SetPosition(double left, double right, double gyro); |
| 49 | |
| 50 | void SetExternalMotors(double left_voltage, double right_voltage); |
| 51 | |
| 52 | void Update(bool stop_motors, bool enable_control_loop); |
| 53 | |
| 54 | double GetEstimatedRobotSpeed() const; |
| 55 | |
| 56 | double GetEstimatedLeftEncoder() const { |
| 57 | return loop_->X_hat(0, 0); |
| 58 | } |
| 59 | |
Austin Schuh | 209f170 | 2015-11-29 17:03:00 -0800 | [diff] [blame] | 60 | double left_velocity() const { return loop_->X_hat(1, 0); } |
| 61 | double right_velocity() const { return loop_->X_hat(3, 0); } |
| 62 | |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 63 | double GetEstimatedRightEncoder() const { |
| 64 | return loop_->X_hat(2, 0); |
| 65 | } |
| 66 | |
| 67 | bool OutputWasCapped() const { |
| 68 | return loop_->output_was_capped(); |
| 69 | } |
| 70 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 71 | void SendMotors( |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 72 | ::frc971::control_loops::DrivetrainQueue::Output *output) const; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 73 | |
| 74 | const LimitedDrivetrainLoop &loop() const { return *loop_; } |
| 75 | |
| 76 | private: |
| 77 | ::std::unique_ptr<LimitedDrivetrainLoop> loop_; |
| 78 | |
| 79 | double filtered_offset_; |
| 80 | double gyro_; |
| 81 | double left_goal_; |
| 82 | double right_goal_; |
| 83 | double raw_left_; |
| 84 | double raw_right_; |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 85 | |
| 86 | const DrivetrainConfig dt_config_; |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
Austin Schuh | 6197a18 | 2015-11-28 16:04:40 -0800 | [diff] [blame] | 89 | } // namespace drivetrain |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 90 | } // namespace control_loops |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 91 | } // namespace frc971 |
Austin Schuh | 64ebab2 | 2015-11-26 13:28:30 -0800 | [diff] [blame] | 92 | |
Comran Morshed | 5323ecb | 2015-12-26 20:50:55 +0000 | [diff] [blame] | 93 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_ |