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" |
| 8 | #include "frc971/control_loops/wrist_motor.q.h" |
| 9 | #include "frc971/control_loops/wrist_motor_plant.h" |
| 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; |
| 17 | }; |
| 18 | |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 19 | class WristMotor |
| 20 | : public aos::control_loops::ControlLoop<control_loops::WristLoop> { |
| 21 | public: |
| 22 | explicit WristMotor( |
| 23 | control_loops::WristLoop *my_wrist = &control_loops::wrist); |
| 24 | |
| 25 | protected: |
| 26 | virtual void RunIteration( |
| 27 | const ::aos::control_loops::Goal *goal, |
| 28 | const control_loops::WristLoop::Position *position, |
| 29 | ::aos::control_loops::Output *output, |
| 30 | ::aos::control_loops::Status *status); |
| 31 | |
| 32 | private: |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame^] | 33 | // Friend the test classes for acces to the internal state. |
| 34 | friend class testing::WristTest_RezeroWithMissingPos_Test; |
| 35 | friend class testing::WristTest_DisableGoesUninitialized_Test; |
| 36 | |
| 37 | // Fetches and locally caches the latest set of constants. |
| 38 | bool FetchConstants(); |
| 39 | |
| 40 | // Clips the goal to be inside the limits and returns the clipped goal. |
| 41 | // Requires the constants to have already been fetched. |
| 42 | double ClipGoal(double goal) const; |
| 43 | // Limits the voltage depending whether the wrist has been zeroed or is out of |
| 44 | // range to make it safer to use. |
| 45 | double LimitVoltage(double absolute_position, double voltage) const; |
| 46 | |
| 47 | // The state feedback control loop to talk to. |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 48 | ::std::unique_ptr<StateFeedbackLoop<2, 1, 1>> loop_; |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame^] | 49 | |
| 50 | // Enum to store the state of the internal zeroing state machine. |
| 51 | enum State { |
| 52 | UNINITIALIZED, |
| 53 | MOVING_OFF, |
| 54 | ZEROING, |
| 55 | READY, |
| 56 | ESTOP |
| 57 | }; |
| 58 | |
| 59 | // Internal state for zeroing. |
| 60 | State state_; |
| 61 | |
| 62 | // Missed position packet count. |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 63 | int error_count_; |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame^] | 64 | // Offset from the raw encoder value to the absolute angle. |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 65 | double zero_offset_; |
Austin Schuh | fa03369 | 2013-02-24 01:00:55 -0800 | [diff] [blame^] | 66 | // Position that gets incremented when zeroing the wrist to slowly move it to |
| 67 | // the hall effect sensor. |
| 68 | double zeroing_position_; |
| 69 | // Last position at which the hall effect sensor was off. |
| 70 | double last_off_position_; |
| 71 | |
| 72 | // Local cache of the wrist geometry constants. |
| 73 | double horizontal_lower_limit_; |
| 74 | double horizontal_upper_limit_; |
| 75 | double horizontal_hall_effect_start_angle_; |
| 76 | double horizontal_zeroing_speed_; |
| 77 | |
| 78 | DISALLOW_COPY_AND_ASSIGN(WristMotor); |
Austin Schuh | dc1c84a | 2013-02-23 16:33:10 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | } // namespace control_loops |
| 82 | } // namespace frc971 |
| 83 | |
| 84 | #endif // FRC971_CONTROL_LOOPS_WRIST_H_ |