blob: 81c3bcb6e25f3fbb511194594cbdfd98e24a717d [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"
8#include "frc971/control_loops/wrist_motor.q.h"
9#include "frc971/control_loops/wrist_motor_plant.h"
10
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
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 Schuhfa033692013-02-24 01:00:55 -080038 // 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 Schuh06ee48e2013-03-02 01:47:54 -080041 friend class testing::WristTest_NoWindupPositive_Test;
42 friend class testing::WristTest_NoWindupNegative_Test;
43 friend class WristStateFeedbackLoop;
Austin Schuhfa033692013-02-24 01:00:55 -080044
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 Schuh06ee48e2013-03-02 01:47:54 -080051
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 Schuhfa033692013-02-24 01:00:55 -080067
68 // The state feedback control loop to talk to.
Austin Schuh06ee48e2013-03-02 01:47:54 -080069 ::std::unique_ptr<WristStateFeedbackLoop> loop_;
Austin Schuhfa033692013-02-24 01:00:55 -080070
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 Schuhdc1c84a2013-02-23 16:33:10 -080084 int error_count_;
Austin Schuhfa033692013-02-24 01:00:55 -080085 // Offset from the raw encoder value to the absolute angle.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080086 double zero_offset_;
Austin Schuhfa033692013-02-24 01:00:55 -080087 // 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.
94 double horizontal_lower_limit_;
95 double horizontal_upper_limit_;
96 double horizontal_hall_effect_start_angle_;
97 double horizontal_zeroing_speed_;
98
99 DISALLOW_COPY_AND_ASSIGN(WristMotor);
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800100};
101
102} // namespace control_loops
103} // namespace frc971
104
105#endif // FRC971_CONTROL_LOOPS_WRIST_H_