Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_CLAW_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_CLAW_H_ |
| 3 | |
| 4 | #include <memory> |
| 5 | |
| 6 | #include "aos/common/controls/control_loop.h" |
Daniel Petti | 9cf68c8 | 2015-02-14 14:57:17 -0800 | [diff] [blame] | 7 | #include "aos/common/time.h" |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 8 | #include "frc971/control_loops/state_feedback_loop.h" |
| 9 | #include "frc971/control_loops/claw/claw.q.h" |
| 10 | #include "frc971/control_loops/claw/claw_motor_plant.h" |
Daniel Petti | 9cf68c8 | 2015-02-14 14:57:17 -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 { |
Daniel Petti | 9cf68c8 | 2015-02-14 14:57:17 -0800 | [diff] [blame] | 15 | namespace testing { |
| 16 | class ClawTest_DisabledGoal_Test; |
| 17 | class ClawTest_GoalPositiveWindup_Test; |
| 18 | class ClawTest_GoalNegativeWindup_Test; |
| 19 | } // namespace testing |
| 20 | |
| 21 | class ClawCappedStateFeedbackLoop : public StateFeedbackLoop<2, 1, 1> { |
| 22 | public: |
| 23 | ClawCappedStateFeedbackLoop(StateFeedbackLoop<2, 1, 1> &&loop) |
| 24 | : StateFeedbackLoop<2, 1, 1>(::std::move(loop)), max_voltage_(12.0) {} |
| 25 | |
| 26 | void set_max_voltage(double max_voltage) { |
| 27 | max_voltage_ = ::std::max(0.0, ::std::min(12.0, max_voltage)); |
| 28 | } |
| 29 | |
| 30 | void CapU() override; |
| 31 | |
| 32 | // Returns the amount to change the position goal in order to no longer |
| 33 | // saturate the controller. |
| 34 | double UnsaturateOutputGoalChange(); |
| 35 | |
| 36 | private: |
| 37 | double max_voltage_; |
| 38 | }; |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 39 | |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame^] | 40 | class Claw : public aos::controls::ControlLoop<control_loops::ClawQueue> { |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 41 | public: |
Daniel Petti | 9cf68c8 | 2015-02-14 14:57:17 -0800 | [diff] [blame] | 42 | enum State { |
| 43 | // Waiting to receive data before doing anything. |
| 44 | UNINITIALIZED = 0, |
| 45 | // Estimating the starting location. |
| 46 | INITIALIZING = 1, |
| 47 | // Moving to find an index pulse. |
| 48 | ZEROING = 2, |
| 49 | // Normal operation. |
| 50 | RUNNING = 3, |
| 51 | // Internal error caused the claw to abort. |
| 52 | ESTOP = 4, |
| 53 | }; |
| 54 | |
| 55 | int state() { return state_; } |
| 56 | |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 57 | explicit Claw( |
| 58 | control_loops::ClawQueue *claw_queue = &control_loops::claw_queue); |
| 59 | |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 60 | protected: |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame^] | 61 | virtual void RunIteration(const control_loops::ClawQueue::Goal *goal, |
| 62 | const control_loops::ClawQueue::Position *position, |
| 63 | control_loops::ClawQueue::Output *output, |
| 64 | control_loops::ClawQueue::Status *status); |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 65 | |
| 66 | private: |
Daniel Petti | 9cf68c8 | 2015-02-14 14:57:17 -0800 | [diff] [blame] | 67 | friend class testing::ClawTest_DisabledGoal_Test; |
| 68 | friend class testing::ClawTest_GoalPositiveWindup_Test; |
| 69 | friend class testing::ClawTest_GoalNegativeWindup_Test; |
| 70 | |
| 71 | // Sets state_ to the correct state given the current state of the zeroing |
| 72 | // estimator. |
| 73 | void UpdateZeroingState(); |
| 74 | void SetClawOffset(double offset); |
| 75 | // Corrects the Observer with the current position. |
| 76 | void Correct(); |
| 77 | |
| 78 | // Getter for the current claw position. |
| 79 | double claw_position() const; |
| 80 | // Our best guess at the current position of the claw. |
| 81 | double estimated_claw_position() const; |
| 82 | // Returns the current zeroing velocity for the claw. If the subsystem is too |
| 83 | // far away from the center, it will switch directions. |
| 84 | double claw_zeroing_velocity(); |
| 85 | |
| 86 | State state_ = UNINITIALIZED; |
| 87 | |
| 88 | // The time when we last changed the claw piston state. |
| 89 | ::aos::time::Time last_piston_edge_; |
| 90 | |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 91 | // The state feedback control loop to talk to. |
Daniel Petti | 9cf68c8 | 2015-02-14 14:57:17 -0800 | [diff] [blame] | 92 | ::std::unique_ptr<ClawCappedStateFeedbackLoop> claw_loop_; |
| 93 | |
| 94 | // Latest position from queue. |
| 95 | control_loops::ClawQueue::Position current_position_; |
| 96 | // Zeroing estimator for claw. |
| 97 | zeroing::ZeroingEstimator claw_estimator_; |
| 98 | |
| 99 | // The goal for the claw. |
| 100 | double claw_goal_ = 0.0; |
| 101 | // Current velocity to move at while zeroing. |
| 102 | double claw_zeroing_velocity_ = 0.0; |
| 103 | // Offsets from the encoder position to the absolute position. |
| 104 | double claw_offset_ = 0.0; |
| 105 | |
| 106 | // Whether claw was closed last cycle. |
| 107 | bool last_rollers_closed_ = false; |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace control_loops |
| 111 | } // namespace frc971 |
| 112 | |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame^] | 113 | #endif // FRC971_CONTROL_LOOPS_CLAW_H_ |