blob: 58c3cc1a965535e6fa077fba81f54899234604ea [file] [log] [blame]
Brian Silvermanb691f5e2015-08-02 11:37:55 -07001#ifndef Y2015_CONTROL_LOOPS_CLAW_H_
2#define Y2015_CONTROL_LOOPS_CLAW_H_
Ben Fredrickson6b5ba792015-01-25 17:14:40 -08003
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"
Austin Schuhbd5308a2015-02-28 21:48:44 -08008#include "aos/common/util/trapezoid_profile.h"
Ben Fredrickson6b5ba792015-01-25 17:14:40 -08009#include "frc971/control_loops/state_feedback_loop.h"
Brian Silvermanb691f5e2015-08-02 11:37:55 -070010#include "y2015/control_loops/claw/claw.q.h"
11#include "y2015/control_loops/claw/claw_motor_plant.h"
Daniel Petti9cf68c82015-02-14 14:57:17 -080012#include "frc971/zeroing/zeroing.h"
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080013
Austin Schuh88af0852016-12-04 20:31:32 -080014namespace y2015 {
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080015namespace control_loops {
Daniel Petti9cf68c82015-02-14 14:57:17 -080016namespace testing {
17class ClawTest_DisabledGoal_Test;
18class ClawTest_GoalPositiveWindup_Test;
19class ClawTest_GoalNegativeWindup_Test;
20} // namespace testing
21
22class ClawCappedStateFeedbackLoop : public StateFeedbackLoop<2, 1, 1> {
23 public:
24 ClawCappedStateFeedbackLoop(StateFeedbackLoop<2, 1, 1> &&loop)
25 : StateFeedbackLoop<2, 1, 1>(::std::move(loop)), max_voltage_(12.0) {}
26
27 void set_max_voltage(double max_voltage) {
28 max_voltage_ = ::std::max(0.0, ::std::min(12.0, max_voltage));
29 }
30
31 void CapU() override;
32
33 // Returns the amount to change the position goal in order to no longer
34 // saturate the controller.
35 double UnsaturateOutputGoalChange();
36
37 private:
38 double max_voltage_;
39};
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080040
Adam Snaider3cd11c52015-02-16 02:16:09 +000041class Claw : public aos::controls::ControlLoop<control_loops::ClawQueue> {
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080042 public:
Daniel Petti9cf68c82015-02-14 14:57:17 -080043 enum State {
44 // Waiting to receive data before doing anything.
45 UNINITIALIZED = 0,
46 // Estimating the starting location.
47 INITIALIZING = 1,
48 // Moving to find an index pulse.
49 ZEROING = 2,
50 // Normal operation.
51 RUNNING = 3,
52 // Internal error caused the claw to abort.
53 ESTOP = 4,
54 };
55
56 int state() { return state_; }
57
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080058 explicit Claw(
59 control_loops::ClawQueue *claw_queue = &control_loops::claw_queue);
60
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080061 protected:
Adam Snaider3cd11c52015-02-16 02:16:09 +000062 virtual void RunIteration(const control_loops::ClawQueue::Goal *goal,
63 const control_loops::ClawQueue::Position *position,
64 control_loops::ClawQueue::Output *output,
65 control_loops::ClawQueue::Status *status);
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080066
67 private:
Daniel Petti9cf68c82015-02-14 14:57:17 -080068 friend class testing::ClawTest_DisabledGoal_Test;
69 friend class testing::ClawTest_GoalPositiveWindup_Test;
70 friend class testing::ClawTest_GoalNegativeWindup_Test;
71
72 // Sets state_ to the correct state given the current state of the zeroing
73 // estimator.
74 void UpdateZeroingState();
75 void SetClawOffset(double offset);
76 // Corrects the Observer with the current position.
77 void Correct();
78
79 // Getter for the current claw position.
80 double claw_position() const;
81 // Our best guess at the current position of the claw.
82 double estimated_claw_position() const;
83 // Returns the current zeroing velocity for the claw. If the subsystem is too
84 // far away from the center, it will switch directions.
85 double claw_zeroing_velocity();
86
87 State state_ = UNINITIALIZED;
88
89 // The time when we last changed the claw piston state.
90 ::aos::time::Time last_piston_edge_;
91
Ben Fredrickson6b5ba792015-01-25 17:14:40 -080092 // The state feedback control loop to talk to.
Daniel Petti9cf68c82015-02-14 14:57:17 -080093 ::std::unique_ptr<ClawCappedStateFeedbackLoop> claw_loop_;
94
95 // Latest position from queue.
96 control_loops::ClawQueue::Position current_position_;
97 // Zeroing estimator for claw.
Austin Schuh88af0852016-12-04 20:31:32 -080098 ::frc971::zeroing::ZeroingEstimator claw_estimator_;
Daniel Petti9cf68c82015-02-14 14:57:17 -080099
100 // The goal for the claw.
101 double claw_goal_ = 0.0;
102 // Current velocity to move at while zeroing.
103 double claw_zeroing_velocity_ = 0.0;
104 // Offsets from the encoder position to the absolute position.
105 double claw_offset_ = 0.0;
106
107 // Whether claw was closed last cycle.
108 bool last_rollers_closed_ = false;
Austin Schuhbd5308a2015-02-28 21:48:44 -0800109
110 ::aos::util::TrapezoidProfile profile_;
Ben Fredrickson6b5ba792015-01-25 17:14:40 -0800111};
112
113} // namespace control_loops
Austin Schuh88af0852016-12-04 20:31:32 -0800114} // namespace y2015
Ben Fredrickson6b5ba792015-01-25 17:14:40 -0800115
Brian Silvermanb691f5e2015-08-02 11:37:55 -0700116#endif // Y2015_CONTROL_LOOPS_CLAW_H_