Brian Silverman | b691f5e | 2015-08-02 11:37:55 -0700 | [diff] [blame] | 1 | #ifndef Y2015_CONTROL_LOOPS_CLAW_H_ |
| 2 | #define Y2015_CONTROL_LOOPS_CLAW_H_ |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 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" |
Austin Schuh | bd5308a | 2015-02-28 21:48:44 -0800 | [diff] [blame] | 8 | #include "aos/common/util/trapezoid_profile.h" |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 9 | #include "frc971/control_loops/state_feedback_loop.h" |
Brian Silverman | b691f5e | 2015-08-02 11:37:55 -0700 | [diff] [blame] | 10 | #include "y2015/control_loops/claw/claw.q.h" |
| 11 | #include "y2015/control_loops/claw/claw_motor_plant.h" |
Daniel Petti | 9cf68c8 | 2015-02-14 14:57:17 -0800 | [diff] [blame] | 12 | #include "frc971/zeroing/zeroing.h" |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 13 | |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame^] | 14 | namespace y2015 { |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 15 | namespace control_loops { |
Daniel Petti | 9cf68c8 | 2015-02-14 14:57:17 -0800 | [diff] [blame] | 16 | namespace testing { |
| 17 | class ClawTest_DisabledGoal_Test; |
| 18 | class ClawTest_GoalPositiveWindup_Test; |
| 19 | class ClawTest_GoalNegativeWindup_Test; |
| 20 | } // namespace testing |
| 21 | |
| 22 | class ClawCappedStateFeedbackLoop : public StateFeedbackLoop<2, 1, 1> { |
| 23 | public: |
| 24 | ClawCappedStateFeedbackLoop(StateFeedbackLoop<2, 1, 1> &&loop) |
| 25 | : StateFeedbackLoop<2, 1, 1>(::std::move(loop)), max_voltage_(12.0) {} |
| 26 | |
| 27 | void set_max_voltage(double max_voltage) { |
| 28 | max_voltage_ = ::std::max(0.0, ::std::min(12.0, max_voltage)); |
| 29 | } |
| 30 | |
| 31 | void CapU() override; |
| 32 | |
| 33 | // Returns the amount to change the position goal in order to no longer |
| 34 | // saturate the controller. |
| 35 | double UnsaturateOutputGoalChange(); |
| 36 | |
| 37 | private: |
| 38 | double max_voltage_; |
| 39 | }; |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 40 | |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 41 | class Claw : public aos::controls::ControlLoop<control_loops::ClawQueue> { |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 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: |
Adam Snaider | 3cd11c5 | 2015-02-16 02:16:09 +0000 | [diff] [blame] | 62 | virtual void RunIteration(const control_loops::ClawQueue::Goal *goal, |
| 63 | const control_loops::ClawQueue::Position *position, |
| 64 | control_loops::ClawQueue::Output *output, |
| 65 | control_loops::ClawQueue::Status *status); |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 66 | |
| 67 | private: |
Daniel Petti | 9cf68c8 | 2015-02-14 14:57:17 -0800 | [diff] [blame] | 68 | friend class testing::ClawTest_DisabledGoal_Test; |
| 69 | friend class testing::ClawTest_GoalPositiveWindup_Test; |
| 70 | friend class testing::ClawTest_GoalNegativeWindup_Test; |
| 71 | |
| 72 | // Sets state_ to the correct state given the current state of the zeroing |
| 73 | // estimator. |
| 74 | void UpdateZeroingState(); |
| 75 | void SetClawOffset(double offset); |
| 76 | // Corrects the Observer with the current position. |
| 77 | void Correct(); |
| 78 | |
| 79 | // Getter for the current claw position. |
| 80 | double claw_position() const; |
| 81 | // Our best guess at the current position of the claw. |
| 82 | double estimated_claw_position() const; |
| 83 | // Returns the current zeroing velocity for the claw. If the subsystem is too |
| 84 | // far away from the center, it will switch directions. |
| 85 | double claw_zeroing_velocity(); |
| 86 | |
| 87 | State state_ = UNINITIALIZED; |
| 88 | |
| 89 | // The time when we last changed the claw piston state. |
| 90 | ::aos::time::Time last_piston_edge_; |
| 91 | |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 92 | // The state feedback control loop to talk to. |
Daniel Petti | 9cf68c8 | 2015-02-14 14:57:17 -0800 | [diff] [blame] | 93 | ::std::unique_ptr<ClawCappedStateFeedbackLoop> claw_loop_; |
| 94 | |
| 95 | // Latest position from queue. |
| 96 | control_loops::ClawQueue::Position current_position_; |
| 97 | // Zeroing estimator for claw. |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame^] | 98 | ::frc971::zeroing::ZeroingEstimator claw_estimator_; |
Daniel Petti | 9cf68c8 | 2015-02-14 14:57:17 -0800 | [diff] [blame] | 99 | |
| 100 | // The goal for the claw. |
| 101 | double claw_goal_ = 0.0; |
| 102 | // Current velocity to move at while zeroing. |
| 103 | double claw_zeroing_velocity_ = 0.0; |
| 104 | // Offsets from the encoder position to the absolute position. |
| 105 | double claw_offset_ = 0.0; |
| 106 | |
| 107 | // Whether claw was closed last cycle. |
| 108 | bool last_rollers_closed_ = false; |
Austin Schuh | bd5308a | 2015-02-28 21:48:44 -0800 | [diff] [blame] | 109 | |
| 110 | ::aos::util::TrapezoidProfile profile_; |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } // namespace control_loops |
Austin Schuh | 88af085 | 2016-12-04 20:31:32 -0800 | [diff] [blame^] | 114 | } // namespace y2015 |
Ben Fredrickson | 6b5ba79 | 2015-01-25 17:14:40 -0800 | [diff] [blame] | 115 | |
Brian Silverman | b691f5e | 2015-08-02 11:37:55 -0700 | [diff] [blame] | 116 | #endif // Y2015_CONTROL_LOOPS_CLAW_H_ |