Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_FRIDGE_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_FRIDGE_H_ |
| 3 | |
| 4 | #include <memory> |
| 5 | |
| 6 | #include "aos/common/controls/control_loop.h" |
| 7 | #include "frc971/control_loops/state_feedback_loop.h" |
| 8 | #include "frc971/control_loops/fridge/fridge.q.h" |
| 9 | #include "frc971/control_loops/fridge/arm_motor_plant.h" |
| 10 | #include "frc971/control_loops/fridge/elevator_motor_plant.h" |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 11 | #include "frc971/zeroing/zeroing.h" |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 12 | |
| 13 | namespace frc971 { |
| 14 | namespace control_loops { |
Austin Schuh | dbd6bfa | 2015-02-14 21:25:16 -0800 | [diff] [blame^] | 15 | namespace testing { |
| 16 | class FridgeTest_DisabledGoalTest_Test; |
| 17 | class FridgeTest_ArmGoalPositiveWindupTest_Test; |
| 18 | class FridgeTest_ElevatorGoalPositiveWindupTest_Test; |
| 19 | class FridgeTest_ArmGoalNegativeWindupTest_Test; |
| 20 | class FridgeTest_ElevatorGoalNegativeWindupTest_Test; |
| 21 | } |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 22 | |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 23 | class CappedStateFeedbackLoop : public StateFeedbackLoop<4, 2, 2> { |
| 24 | public: |
| 25 | CappedStateFeedbackLoop(StateFeedbackLoop<4, 2, 2> &&loop) |
| 26 | : StateFeedbackLoop<4, 2, 2>(::std::move(loop)), max_voltage_(12.0) {} |
| 27 | |
| 28 | void set_max_voltage(double max_voltage) { |
| 29 | max_voltage_ = ::std::max(-12.0, ::std::min(12.0, max_voltage)); |
| 30 | } |
| 31 | |
| 32 | void CapU() override; |
| 33 | |
| 34 | // Returns the amount to change the position goals (average and difference) in |
| 35 | // order to no longer saturate the controller. |
| 36 | Eigen::Matrix<double, 2, 1> UnsaturateOutputGoalChange(); |
| 37 | |
| 38 | private: |
| 39 | double max_voltage_; |
| 40 | }; |
| 41 | |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 42 | class Fridge |
| 43 | : public aos::controls::ControlLoop<control_loops::FridgeQueue> { |
| 44 | public: |
| 45 | explicit Fridge( |
| 46 | control_loops::FridgeQueue *fridge_queue = &control_loops::fridge_queue); |
| 47 | |
| 48 | // Control loop time step. |
| 49 | // Please figure out how to set dt from a common location |
| 50 | // Please decide the correct value |
| 51 | // Please use dt in your implementation so we can change looptimnig |
| 52 | // and be consistent with legacy |
| 53 | // And Brian please approve my code review as people are wait on |
| 54 | // these files to exist and they will be rewritten anyway |
| 55 | //static constexpr double dt; |
| 56 | |
Austin Schuh | dbd6bfa | 2015-02-14 21:25:16 -0800 | [diff] [blame^] | 57 | enum State { |
| 58 | // Waiting to receive data before doing anything. |
| 59 | UNINITIALIZED = 0, |
| 60 | // Estimating the starting location. |
| 61 | INITIALIZING = 1, |
| 62 | // Moving the elevator to find an index pulse. |
| 63 | ZEROING_ELEVATOR = 2, |
| 64 | // Moving the arm to find an index pulse. |
| 65 | ZEROING_ARM = 3, |
| 66 | // All good! |
| 67 | RUNNING = 4, |
| 68 | // Internal error caused the fridge to abort. |
| 69 | ESTOP = 5, |
| 70 | }; |
| 71 | |
| 72 | State state() const { return state_; } |
| 73 | |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 74 | protected: |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 75 | void RunIteration(const control_loops::FridgeQueue::Goal *goal, |
| 76 | const control_loops::FridgeQueue::Position *position, |
| 77 | control_loops::FridgeQueue::Output *output, |
| 78 | control_loops::FridgeQueue::Status *status) override; |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 79 | |
| 80 | private: |
Austin Schuh | dbd6bfa | 2015-02-14 21:25:16 -0800 | [diff] [blame^] | 81 | friend class testing::FridgeTest_DisabledGoalTest_Test; |
| 82 | friend class testing::FridgeTest_ElevatorGoalPositiveWindupTest_Test; |
| 83 | friend class testing::FridgeTest_ArmGoalPositiveWindupTest_Test; |
| 84 | friend class testing::FridgeTest_ElevatorGoalNegativeWindupTest_Test; |
| 85 | friend class testing::FridgeTest_ArmGoalNegativeWindupTest_Test; |
| 86 | |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 87 | // Sets state_ to the correct state given the current state of the zeroing |
| 88 | // estimators. |
| 89 | void UpdateZeroingState(); |
| 90 | |
| 91 | void SetElevatorOffset(double left_offset, double right_offset); |
| 92 | void SetArmOffset(double left_offset, double right_offset); |
| 93 | |
| 94 | // Getters for the current elevator positions. |
| 95 | double left_elevator(); |
| 96 | double right_elevator(); |
| 97 | double elevator(); |
| 98 | |
| 99 | // Getters for the current arm positions. |
| 100 | double left_arm(); |
| 101 | double right_arm(); |
| 102 | double arm(); |
| 103 | |
| 104 | // Our best guess at the current position of the elevator. |
| 105 | double estimated_left_elevator(); |
| 106 | double estimated_right_elevator(); |
| 107 | double estimated_elevator(); |
| 108 | |
| 109 | // Our best guess at the current position of the arm. |
| 110 | double estimated_left_arm(); |
| 111 | double estimated_right_arm(); |
| 112 | double estimated_arm(); |
| 113 | |
| 114 | // Returns the current zeroing velocity for either subsystem. |
| 115 | // If the subsystem is too far away from the center, these will switch |
| 116 | // directions. |
| 117 | double elevator_zeroing_velocity(); |
| 118 | double arm_zeroing_velocity(); |
| 119 | |
| 120 | // Corrects the Observer with the current position. |
| 121 | void Correct(); |
| 122 | |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 123 | // The state feedback control loop or loops to talk to. |
Austin Schuh | 703b8d4 | 2015-02-01 14:56:34 -0800 | [diff] [blame] | 124 | ::std::unique_ptr<CappedStateFeedbackLoop> arm_loop_; |
| 125 | ::std::unique_ptr<CappedStateFeedbackLoop> elevator_loop_; |
| 126 | |
| 127 | zeroing::ZeroingEstimator left_arm_estimator_; |
| 128 | zeroing::ZeroingEstimator right_arm_estimator_; |
| 129 | zeroing::ZeroingEstimator left_elevator_estimator_; |
| 130 | zeroing::ZeroingEstimator right_elevator_estimator_; |
| 131 | |
| 132 | // Offsets from the encoder position to the absolute position. Add these to |
| 133 | // the encoder position to get the absolute position. |
| 134 | double left_elevator_offset_ = 0.0; |
| 135 | double right_elevator_offset_ = 0.0; |
| 136 | double left_arm_offset_ = 0.0; |
| 137 | double right_arm_offset_ = 0.0; |
| 138 | |
| 139 | // Current velocity to move at while zeroing. |
| 140 | double elevator_zeroing_velocity_ = 0.0; |
| 141 | double arm_zeroing_velocity_ = 0.0; |
| 142 | |
| 143 | // The goals for the elevator and arm. |
| 144 | double elevator_goal_ = 0.0; |
| 145 | double arm_goal_ = 0.0; |
| 146 | |
| 147 | State state_ = UNINITIALIZED; |
| 148 | State last_state_ = UNINITIALIZED; |
| 149 | |
| 150 | control_loops::FridgeQueue::Position current_position_; |
| 151 | static constexpr double dt = 0.005; |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | } // namespace control_loops |
| 155 | } // namespace frc971 |
| 156 | |
| 157 | #endif // FRC971_CONTROL_LOOPS_FRIDGE_H_ |
| 158 | |