blob: 419b4013a56231ebcad66cd09bca22f559dbaa1c [file] [log] [blame]
Comran Morshed5323ecb2015-12-26 20:50:55 +00001#ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_
2#define FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_
Austin Schuh64ebab22015-11-26 13:28:30 -08003
Austin Schuh64ebab22015-11-26 13:28:30 -08004#include "aos/common/commonmath.h"
Austin Schuh093535c2016-03-05 23:21:00 -08005#include "aos/common/controls/control_loop.h"
6#include "aos/common/controls/polytope.h"
Austin Schuh64ebab22015-11-26 13:28:30 -08007#include "aos/common/logging/matrix_logging.h"
Austin Schuh093535c2016-03-05 23:21:00 -08008#include "aos/common/util/trapezoid_profile.h"
Austin Schuh64ebab22015-11-26 13:28:30 -08009
10#include "frc971/control_loops/state_feedback_loop.h"
11#include "frc971/control_loops/coerce_goal.h"
Comran Morshed5323ecb2015-12-26 20:50:55 +000012#include "frc971/control_loops/drivetrain/drivetrain.q.h"
13#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Austin Schuh64ebab22015-11-26 13:28:30 -080014
Comran Morshed5323ecb2015-12-26 20:50:55 +000015namespace frc971 {
Austin Schuh64ebab22015-11-26 13:28:30 -080016namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080017namespace drivetrain {
Austin Schuh64ebab22015-11-26 13:28:30 -080018
19class DrivetrainMotorsSS {
20 public:
Austin Schuh093535c2016-03-05 23:21:00 -080021 DrivetrainMotorsSS(const DrivetrainConfig &dt_config,
22 StateFeedbackLoop<7, 2, 3> *kf,
23 double *integrated_kf_heading);
Austin Schuh64ebab22015-11-26 13:28:30 -080024
Austin Schuh093535c2016-03-05 23:21:00 -080025 void SetGoal(const ::frc971::control_loops::DrivetrainQueue::Goal &goal);
Austin Schuh64ebab22015-11-26 13:28:30 -080026
Austin Schuh093535c2016-03-05 23:21:00 -080027 // 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 Schuh64ebab22015-11-26 13:28:30 -080033
Austin Schuh093535c2016-03-05 23:21:00 -080034 bool output_was_capped() const { return output_was_capped_; }
Brian Silverman4e1b0662016-01-31 18:03:19 -050035
Austin Schuh093535c2016-03-05 23:21:00 -080036 void SetOutput(
Comran Morshed5323ecb2015-12-26 20:50:55 +000037 ::frc971::control_loops::DrivetrainQueue::Output *output) const;
Austin Schuh093535c2016-03-05 23:21:00 -080038 void PopulateStatus(
39 ::frc971::control_loops::DrivetrainQueue::Status *status) const;
Austin Schuh64ebab22015-11-26 13:28:30 -080040
Austin Schuh093535c2016-03-05 23:21:00 -080041 // Converts the robot state to a linear distance position, velocity.
42 Eigen::Matrix<double, 2, 1> LeftRightToLinear(
43 const Eigen::Matrix<double, 7, 1> &left_right);
44 // Converts the robot state to an anglular distance, velocity.
45 Eigen::Matrix<double, 2, 1> LeftRightToAngular(
46 const Eigen::Matrix<double, 7, 1> &left_right);
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,
52 const Eigen::Matrix<double, 2, 1> &angular);
Austin Schuh64ebab22015-11-26 13:28:30 -080053
54 private:
Austin Schuh093535c2016-03-05 23:21:00 -080055 void PolyCapU(Eigen::Matrix<double, 2, 1> *U);
56 void ScaleCapU(Eigen::Matrix<double, 2, 1> *U);
Comran Morshed5323ecb2015-12-26 20:50:55 +000057
58 const DrivetrainConfig dt_config_;
Austin Schuh093535c2016-03-05 23:21:00 -080059 StateFeedbackLoop<7, 2, 3> *kf_;
60 Eigen::Matrix<double, 7, 1> unprofiled_goal_;
61
62 // Reprsents +/- full power on each motor in U-space, aka the square from
63 // (-12, -12) to (12, 12).
64 const ::aos::controls::HPolytope<2> U_poly_;
65
66 // multiplying by T converts [left_error, right_error] to
67 // [left_right_error_difference, total_distance_error].
68 Eigen::Matrix<double, 2, 2> T_, T_inverse_;
69
70 aos::util::TrapezoidProfile linear_profile_, angular_profile_;
71
72 bool output_was_capped_ = false;
73
74 bool use_profile_ = false;
75
76 double *integrated_kf_heading_;
Austin Schuh64ebab22015-11-26 13:28:30 -080077};
78
Austin Schuh6197a182015-11-28 16:04:40 -080079} // namespace drivetrain
Austin Schuh64ebab22015-11-26 13:28:30 -080080} // namespace control_loops
Comran Morshed5323ecb2015-12-26 20:50:55 +000081} // namespace frc971
Austin Schuh64ebab22015-11-26 13:28:30 -080082
Comran Morshed5323ecb2015-12-26 20:50:55 +000083#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_SSDRIVETRAIN_H_