Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_WRIST_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_WRIST_H_ |
| 3 | |
| 4 | #include <memory> |
| 5 | |
| 6 | #include "aos/common/control_loop/ControlLoop.h" |
| 7 | #include "frc971/control_loops/state_feedback_loop.h" |
Austin Schuh | a40624b | 2013-03-03 14:02:40 -0800 | [diff] [blame] | 8 | #include "frc971/control_loops/wrist/wrist_motor.q.h" |
| 9 | #include "frc971/control_loops/wrist/wrist_motor_plant.h" |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 10 | |
| 11 | namespace frc971 { |
| 12 | namespace control_loops { |
| 13 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 14 | namespace testing { |
| 15 | class WristTest_RezeroWithMissingPos_Test; |
| 16 | class WristTest_DisableGoesUninitialized_Test; |
Austin Schuh | 06ee48e | 2013-03-02 01:47:54 -0800 | [diff] [blame] | 17 | class WristTest_NoWindup_Test; |
| 18 | class WristTest_NoWindupPositive_Test; |
| 19 | class WristTest_NoWindupNegative_Test; |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 20 | }; |
| 21 | |
Austin Schuh | 06ee48e | 2013-03-02 01:47:54 -0800 | [diff] [blame] | 22 | class WristMotor; |
| 23 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 24 | class WristMotor |
| 25 | : public aos::control_loops::ControlLoop<control_loops::WristLoop> { |
| 26 | public: |
| 27 | explicit WristMotor( |
| 28 | control_loops::WristLoop *my_wrist = &control_loops::wrist); |
| 29 | |
Austin Schuh | d11c337 | 2013-03-09 14:33:31 -0800 | [diff] [blame] | 30 | // True if the goal was moved to avoid goal windup. |
| 31 | bool capped_goal() const { return capped_goal_; } |
| 32 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 33 | protected: |
| 34 | virtual void RunIteration( |
| 35 | const ::aos::control_loops::Goal *goal, |
| 36 | const control_loops::WristLoop::Position *position, |
| 37 | ::aos::control_loops::Output *output, |
| 38 | ::aos::control_loops::Status *status); |
| 39 | |
| 40 | private: |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 41 | // Friend the test classes for acces to the internal state. |
| 42 | friend class testing::WristTest_RezeroWithMissingPos_Test; |
| 43 | friend class testing::WristTest_DisableGoesUninitialized_Test; |
Austin Schuh | 06ee48e | 2013-03-02 01:47:54 -0800 | [diff] [blame] | 44 | friend class testing::WristTest_NoWindupPositive_Test; |
| 45 | friend class testing::WristTest_NoWindupNegative_Test; |
| 46 | friend class WristStateFeedbackLoop; |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 47 | |
| 48 | // Fetches and locally caches the latest set of constants. |
| 49 | bool FetchConstants(); |
| 50 | |
| 51 | // Clips the goal to be inside the limits and returns the clipped goal. |
| 52 | // Requires the constants to have already been fetched. |
| 53 | double ClipGoal(double goal) const; |
Austin Schuh | 06ee48e | 2013-03-02 01:47:54 -0800 | [diff] [blame] | 54 | |
| 55 | // This class implements the CapU function correctly given all the extra |
| 56 | // information that we know about from the wrist motor. |
| 57 | class WristStateFeedbackLoop : public StateFeedbackLoop<2, 1, 1> { |
| 58 | public: |
| 59 | WristStateFeedbackLoop(StateFeedbackLoop<2, 1, 1> loop, |
| 60 | WristMotor *wrist_motor) |
| 61 | : StateFeedbackLoop<2, 1, 1>(loop), |
| 62 | wrist_motor_(wrist_motor) { |
| 63 | } |
| 64 | |
| 65 | // Caps U, but this time respects the state of the wrist as well. |
| 66 | virtual void CapU(); |
| 67 | private: |
| 68 | WristMotor *wrist_motor_; |
| 69 | }; |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 70 | |
| 71 | // The state feedback control loop to talk to. |
Austin Schuh | 06ee48e | 2013-03-02 01:47:54 -0800 | [diff] [blame] | 72 | ::std::unique_ptr<WristStateFeedbackLoop> loop_; |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 73 | |
| 74 | // Enum to store the state of the internal zeroing state machine. |
| 75 | enum State { |
| 76 | UNINITIALIZED, |
| 77 | MOVING_OFF, |
| 78 | ZEROING, |
| 79 | READY, |
| 80 | ESTOP |
| 81 | }; |
| 82 | |
| 83 | // Internal state for zeroing. |
| 84 | State state_; |
| 85 | |
| 86 | // Missed position packet count. |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 87 | int error_count_; |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 88 | // Offset from the raw encoder value to the absolute angle. |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 89 | double zero_offset_; |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 90 | // Position that gets incremented when zeroing the wrist to slowly move it to |
| 91 | // the hall effect sensor. |
| 92 | double zeroing_position_; |
| 93 | // Last position at which the hall effect sensor was off. |
| 94 | double last_off_position_; |
| 95 | |
| 96 | // Local cache of the wrist geometry constants. |
James Kuszmaul | e06e251 | 2013-03-02 15:04:53 -0800 | [diff] [blame] | 97 | double wrist_lower_limit_; |
| 98 | double wrist_upper_limit_; |
| 99 | double wrist_hall_effect_start_angle_; |
| 100 | double wrist_zeroing_speed_; |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 101 | |
Austin Schuh | d11c337 | 2013-03-09 14:33:31 -0800 | [diff] [blame] | 102 | // True if the zeroing goal was capped during this cycle. |
| 103 | bool capped_goal_; |
| 104 | |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame] | 105 | DISALLOW_COPY_AND_ASSIGN(WristMotor); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | } // namespace control_loops |
| 109 | } // namespace frc971 |
| 110 | |
| 111 | #endif // FRC971_CONTROL_LOOPS_WRIST_H_ |