blob: 381bd150923063bf39ebf3d3efd37a9e6ca5d324 [file] [log] [blame]
Ben Fredrickson6b5ba792015-01-25 17:14:40 -08001#ifndef FRC971_CONTROL_LOOPS_CLAW_H_
2#define FRC971_CONTROL_LOOPS_CLAW_H_
3
4#include <memory>
5
6#include "aos/common/controls/control_loop.h"
Daniel Petti9cf68c82015-02-14 14:57:17 -08007#include "aos/common/time.h"
Ben Fredrickson6b5ba792015-01-25 17:14:40 -08008#include "frc971/control_loops/state_feedback_loop.h"
9#include "frc971/control_loops/claw/claw.q.h"
10#include "frc971/control_loops/claw/claw_motor_plant.h"
Daniel Petti9cf68c82015-02-14 14:57:17 -080011#include "frc971/zeroing/zeroing.h"
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080012
13namespace frc971 {
14namespace control_loops {
Daniel Petti9cf68c82015-02-14 14:57:17 -080015namespace testing {
16class ClawTest_DisabledGoal_Test;
17class ClawTest_GoalPositiveWindup_Test;
18class ClawTest_GoalNegativeWindup_Test;
19} // namespace testing
20
21class ClawCappedStateFeedbackLoop : public StateFeedbackLoop<2, 1, 1> {
22 public:
23 ClawCappedStateFeedbackLoop(StateFeedbackLoop<2, 1, 1> &&loop)
24 : StateFeedbackLoop<2, 1, 1>(::std::move(loop)), max_voltage_(12.0) {}
25
26 void set_max_voltage(double max_voltage) {
27 max_voltage_ = ::std::max(0.0, ::std::min(12.0, max_voltage));
28 }
29
30 void CapU() override;
31
32 // Returns the amount to change the position goal in order to no longer
33 // saturate the controller.
34 double UnsaturateOutputGoalChange();
35
36 private:
37 double max_voltage_;
38};
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080039
Adam Snaider3cd11c52015-02-16 02:16:09 +000040class Claw : public aos::controls::ControlLoop<control_loops::ClawQueue> {
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080041 public:
Daniel Petti9cf68c82015-02-14 14:57:17 -080042 enum State {
43 // Waiting to receive data before doing anything.
44 UNINITIALIZED = 0,
45 // Estimating the starting location.
46 INITIALIZING = 1,
47 // Moving to find an index pulse.
48 ZEROING = 2,
49 // Normal operation.
50 RUNNING = 3,
51 // Internal error caused the claw to abort.
52 ESTOP = 4,
53 };
54
55 int state() { return state_; }
56
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080057 explicit Claw(
58 control_loops::ClawQueue *claw_queue = &control_loops::claw_queue);
59
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080060 protected:
Adam Snaider3cd11c52015-02-16 02:16:09 +000061 virtual void RunIteration(const control_loops::ClawQueue::Goal *goal,
62 const control_loops::ClawQueue::Position *position,
63 control_loops::ClawQueue::Output *output,
64 control_loops::ClawQueue::Status *status);
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080065
66 private:
Daniel Petti9cf68c82015-02-14 14:57:17 -080067 friend class testing::ClawTest_DisabledGoal_Test;
68 friend class testing::ClawTest_GoalPositiveWindup_Test;
69 friend class testing::ClawTest_GoalNegativeWindup_Test;
70
71 // Sets state_ to the correct state given the current state of the zeroing
72 // estimator.
73 void UpdateZeroingState();
74 void SetClawOffset(double offset);
75 // Corrects the Observer with the current position.
76 void Correct();
77
78 // Getter for the current claw position.
79 double claw_position() const;
80 // Our best guess at the current position of the claw.
81 double estimated_claw_position() const;
82 // Returns the current zeroing velocity for the claw. If the subsystem is too
83 // far away from the center, it will switch directions.
84 double claw_zeroing_velocity();
85
86 State state_ = UNINITIALIZED;
87
88 // The time when we last changed the claw piston state.
89 ::aos::time::Time last_piston_edge_;
90
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080091 // The state feedback control loop to talk to.
Daniel Petti9cf68c82015-02-14 14:57:17 -080092 ::std::unique_ptr<ClawCappedStateFeedbackLoop> claw_loop_;
93
94 // Latest position from queue.
95 control_loops::ClawQueue::Position current_position_;
96 // Zeroing estimator for claw.
97 zeroing::ZeroingEstimator claw_estimator_;
98
99 // The goal for the claw.
100 double claw_goal_ = 0.0;
101 // Current velocity to move at while zeroing.
102 double claw_zeroing_velocity_ = 0.0;
103 // Offsets from the encoder position to the absolute position.
104 double claw_offset_ = 0.0;
105
106 // Whether claw was closed last cycle.
107 bool last_rollers_closed_ = false;
Ben Fredrickson6b5ba792015-01-25 17:14:40 -0800108};
109
110} // namespace control_loops
111} // namespace frc971
112
Adam Snaider3cd11c52015-02-16 02:16:09 +0000113#endif // FRC971_CONTROL_LOOPS_CLAW_H_