blob: 55d14c8e068700c5f14b018e74779d580bef0f67 [file] [log] [blame]
Austin Schuhdc1c84a2013-02-23 16:33:10 -08001#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 Schuha40624b2013-03-03 14:02:40 -08008#include "frc971/control_loops/wrist/wrist_motor.q.h"
9#include "frc971/control_loops/wrist/wrist_motor_plant.h"
Austin Schuhdc1c84a2013-02-23 16:33:10 -080010
11namespace frc971 {
12namespace control_loops {
13
Austin Schuhfa033692013-02-24 01:00:55 -080014namespace testing {
15class WristTest_RezeroWithMissingPos_Test;
16class WristTest_DisableGoesUninitialized_Test;
Austin Schuh06ee48e2013-03-02 01:47:54 -080017class WristTest_NoWindup_Test;
18class WristTest_NoWindupPositive_Test;
19class WristTest_NoWindupNegative_Test;
Austin Schuhfa033692013-02-24 01:00:55 -080020};
21
Austin Schuh06ee48e2013-03-02 01:47:54 -080022class WristMotor;
23
Austin Schuhdc1c84a2013-02-23 16:33:10 -080024class 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 Schuhd11c3372013-03-09 14:33:31 -080030 // True if the goal was moved to avoid goal windup.
31 bool capped_goal() const { return capped_goal_; }
32
Austin Schuhdc1c84a2013-02-23 16:33:10 -080033 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 Schuhfa033692013-02-24 01:00:55 -080041 // 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 Schuh06ee48e2013-03-02 01:47:54 -080044 friend class testing::WristTest_NoWindupPositive_Test;
45 friend class testing::WristTest_NoWindupNegative_Test;
46 friend class WristStateFeedbackLoop;
Austin Schuhfa033692013-02-24 01:00:55 -080047
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 Schuh06ee48e2013-03-02 01:47:54 -080054
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 Schuhfa033692013-02-24 01:00:55 -080070
71 // The state feedback control loop to talk to.
Austin Schuh06ee48e2013-03-02 01:47:54 -080072 ::std::unique_ptr<WristStateFeedbackLoop> loop_;
Austin Schuhfa033692013-02-24 01:00:55 -080073
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 Schuhdc1c84a2013-02-23 16:33:10 -080087 int error_count_;
Austin Schuhfa033692013-02-24 01:00:55 -080088 // Offset from the raw encoder value to the absolute angle.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080089 double zero_offset_;
Austin Schuhfa033692013-02-24 01:00:55 -080090 // 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 Kuszmaule06e2512013-03-02 15:04:53 -080097 double wrist_lower_limit_;
98 double wrist_upper_limit_;
99 double wrist_hall_effect_start_angle_;
100 double wrist_zeroing_speed_;
Austin Schuhfa033692013-02-24 01:00:55 -0800101
Austin Schuhd11c3372013-03-09 14:33:31 -0800102 // True if the zeroing goal was capped during this cycle.
103 bool capped_goal_;
104
Austin Schuhfa033692013-02-24 01:00:55 -0800105 DISALLOW_COPY_AND_ASSIGN(WristMotor);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800106};
107
108} // namespace control_loops
109} // namespace frc971
110
111#endif // FRC971_CONTROL_LOOPS_WRIST_H_