blob: 5cd7fffb10abc7563934bf21af0d21d42727286a [file] [log] [blame]
Austin Schuh96ce8ae2015-11-26 12:46:02 -08001#ifndef Y2014_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_
2#define Y2014_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_
3
4#include "aos/common/controls/polytope.h"
5
6#include "y2014/constants.h"
7#include "y2014/control_loops/drivetrain/drivetrain.q.h"
8#include "frc971/control_loops/state_feedback_loop.h"
9#include "y2014/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
10
Austin Schuh6197a182015-11-28 16:04:40 -080011namespace y2014 {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080012namespace control_loops {
Austin Schuh6197a182015-11-28 16:04:40 -080013namespace drivetrain {
Austin Schuh96ce8ae2015-11-26 12:46:02 -080014
15class PolyDrivetrain {
16 public:
17 enum Gear { HIGH, LOW, SHIFTING_UP, SHIFTING_DOWN };
18 // Stall Torque in N m
Austin Schuh250eb4c2015-11-28 16:35:39 -080019 static constexpr double kStallTorque = drivetrain::kStallTorque;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080020 // Stall Current in Amps
Austin Schuh250eb4c2015-11-28 16:35:39 -080021 static constexpr double kStallCurrent = drivetrain::kStallCurrent;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080022 // Free Speed in RPM. Used number from last year.
Austin Schuh250eb4c2015-11-28 16:35:39 -080023 static constexpr double kFreeSpeed = drivetrain::kFreeSpeedRPM;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080024 // Free Current in Amps
Austin Schuh250eb4c2015-11-28 16:35:39 -080025 static constexpr double kFreeCurrent = drivetrain::kFreeCurrent;
26 static constexpr double kWheelRadius = drivetrain::kWheelRadius;
Austin Schuh6197a182015-11-28 16:04:40 -080027 // Resistance of the motor, divided by the number of motors per side.
Austin Schuh250eb4c2015-11-28 16:35:39 -080028 static constexpr double kR = drivetrain::kR;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080029 // Motor velocity constant
Austin Schuh250eb4c2015-11-28 16:35:39 -080030 static constexpr double Kv = drivetrain::kV;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080031
32 // Torque constant
Austin Schuh250eb4c2015-11-28 16:35:39 -080033 static constexpr double Kt = drivetrain::kT;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080034
Austin Schuh6613a072016-01-06 19:54:48 -080035 PolyDrivetrain(StateFeedbackLoop<7, 2, 3> *kf);
Austin Schuh96ce8ae2015-11-26 12:46:02 -080036
Austin Schuh6197a182015-11-28 16:04:40 -080037 int controller_index() const { return loop_->controller_index(); }
38
Austin Schuh96ce8ae2015-11-26 12:46:02 -080039 static bool IsInGear(Gear gear) { return gear == LOW || gear == HIGH; }
40
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080041 // Computes the speed of the motor given the hall effect position and the
42 // speed of the robot.
Austin Schuh96ce8ae2015-11-26 12:46:02 -080043 static double MotorSpeed(const constants::ShifterHallEffect &hall_effect,
44 double shifter_position, double velocity);
45
Austin Schuhb9d5e8e2015-12-27 14:08:47 -080046 // Computes the states of the shifters for the left and right drivetrain sides
47 // given a requested state.
48 void UpdateGears(Gear requested_gear);
49
50 // Computes the next state of a shifter given the current state and the
51 // requested state.
52 static Gear UpdateSingleGear(Gear requested_gear, Gear current_gear);
Austin Schuh96ce8ae2015-11-26 12:46:02 -080053
54 void SetGoal(double wheel, double throttle, bool quickturn, bool highgear);
55
Austin Schuh6197a182015-11-28 16:04:40 -080056 void SetPosition(
Brian Silvermanb601d892015-12-20 18:24:38 -050057 const ::y2014::control_loops::DrivetrainQueue::Position *position);
Austin Schuh96ce8ae2015-11-26 12:46:02 -080058
59 double FilterVelocity(double throttle);
60
61 double MaxVelocity();
62
63 void Update();
64
Brian Silvermanb601d892015-12-20 18:24:38 -050065 void SendMotors(::y2014::control_loops::DrivetrainQueue::Output *output);
Austin Schuh96ce8ae2015-11-26 12:46:02 -080066
67 private:
Austin Schuh6613a072016-01-06 19:54:48 -080068 StateFeedbackLoop<7, 2, 3> *kf_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080069 const ::aos::controls::HPolytope<2> U_Poly_;
70
71 ::std::unique_ptr<StateFeedbackLoop<2, 2, 2>> loop_;
72
73 const double ttrust_;
74 double wheel_;
75 double throttle_;
76 bool quickturn_;
77 int stale_count_;
78 double position_time_delta_;
79 Gear left_gear_;
80 Gear right_gear_;
Brian Silvermanb601d892015-12-20 18:24:38 -050081 ::y2014::control_loops::DrivetrainQueue::Position last_position_;
82 ::y2014::control_loops::DrivetrainQueue::Position position_;
Austin Schuh96ce8ae2015-11-26 12:46:02 -080083 int counter_;
84};
85
Austin Schuh6197a182015-11-28 16:04:40 -080086} // namespace drivetrain
Austin Schuh96ce8ae2015-11-26 12:46:02 -080087} // namespace control_loops
Austin Schuh6197a182015-11-28 16:04:40 -080088} // namespace y2014
Austin Schuh96ce8ae2015-11-26 12:46:02 -080089
90#endif // Y2014_CONTROL_LOOPS_DRIVETRAIN_POLYDRIVETRAIN_H_