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