blob: fe09105b63d986e3db499c85ad30456a33a8172f [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;
17};
18
Austin Schuhdc1c84a2013-02-23 16:33:10 -080019class 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 Schuhfa033692013-02-24 01:00:55 -080033 // 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 Schuhdc1c84a2013-02-23 16:33:10 -080048 ::std::unique_ptr<StateFeedbackLoop<2, 1, 1>> loop_;
Austin Schuhfa033692013-02-24 01:00:55 -080049
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 Schuhdc1c84a2013-02-23 16:33:10 -080063 int error_count_;
Austin Schuhfa033692013-02-24 01:00:55 -080064 // Offset from the raw encoder value to the absolute angle.
Austin Schuhdc1c84a2013-02-23 16:33:10 -080065 double zero_offset_;
Austin Schuhfa033692013-02-24 01:00:55 -080066 // 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 Schuhdc1c84a2013-02-23 16:33:10 -080079};
80
81} // namespace control_loops
82} // namespace frc971
83
84#endif // FRC971_CONTROL_LOOPS_WRIST_H_