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