blob: d96e24d350d04bbb68a591cd1f0a8346314ed697 [file] [log] [blame]
Austin Schuh10c2d112016-02-14 13:42:28 -08001#ifndef Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_CONTROLS_H_
2#define Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_CONTROLS_H_
3
4#include <memory>
5
6#include "aos/common/controls/control_loop.h"
7#include "frc971/control_loops/state_feedback_loop.h"
8#include "aos/common/util/trapezoid_profile.h"
9
10#include "frc971/zeroing/zeroing.h"
11#include "y2016/control_loops/superstructure/superstructure.q.h"
12
13namespace y2016 {
14namespace control_loops {
15namespace superstructure {
16namespace testing {
17class SuperstructureTest_DisabledGoalTest_Test;
18} // namespace testing
19
20class SimpleCappedStateFeedbackLoop : public StateFeedbackLoop<3, 1, 1> {
21 public:
22 SimpleCappedStateFeedbackLoop(StateFeedbackLoop<3, 1, 1> &&loop)
23 : StateFeedbackLoop<3, 1, 1>(::std::move(loop)), max_voltage_(12.0) {}
24
25 void set_max_voltage(double max_voltage) {
26 max_voltage_ = ::std::max(0.0, ::std::min(12.0, max_voltage));
27 }
28
29 void CapU() override;
30
31 private:
32 double max_voltage_;
33};
34
35class DoubleCappedStateFeedbackLoop : public StateFeedbackLoop<6, 2, 2> {
36 public:
37 DoubleCappedStateFeedbackLoop(StateFeedbackLoop<6, 2, 2> &&loop)
38 : StateFeedbackLoop<6, 2, 2>(::std::move(loop)),
39 shoulder_max_voltage_(12.0),
40 wrist_max_voltage_(12.0) {}
41
42 void set_max_voltage(double shoulder_max_voltage, double wrist_max_voltage) {
43 shoulder_max_voltage_ =
44 ::std::max(0.0, ::std::min(12.0, shoulder_max_voltage));
45 wrist_max_voltage_ = ::std::max(0.0, ::std::min(12.0, wrist_max_voltage));
46 }
47
48 void CapU() override;
49
50 private:
51 double shoulder_max_voltage_;
52 double wrist_max_voltage_;
53};
54
55class Intake {
56 public:
57 Intake();
58 // Returns whether the estimators have been initialized and zeroed.
59 bool initialized() const { return initialized_; }
60 bool zeroed() const { return zeroed_; }
61
62 // Updates our estimator with the latest position.
63 void Correct(::frc971::PotAndIndexPosition position);
64
65 // Forces the current goal to the provided goal, bypassing the profiler.
66 void ForceGoal(double goal);
67 // Sets the unprofiled goal. The profiler will generate a profile to go to
68 // this goal.
69 void set_unprofiled_goal(double unprofiled_goal);
70
71 // Runs the controller and profile generator for a cycle.
72 void Update(bool disabled);
73
74 // Limits our profiles to a max velocity and acceleration for proper motion.
75 void AdjustProfile(double max_angular_velocity,
76 double max_angular_acceleration);
77 // Sets the maximum voltage that will be commanded by the loop.
78 void set_max_voltage(double voltage);
79
80 // Returns true if we have exceeded any hard limits.
81 bool CheckHardLimits();
82 // Resets the internal state.
83 void Reset();
84
85 // Returns the current internal estimator state for logging.
86 ::frc971::EstimatorState IntakeEstimatorState();
87
88 // Returns the requested intake voltage.
89 double intake_voltage() const { return loop_->U(0, 0); }
90
91 // Returns the current position.
92 double angle() const { return Y_(0, 0); }
93
94 // Returns the filtered goal.
95 const Eigen::Matrix<double, 3, 1> &goal() const { return loop_->R(); }
96 double goal(int row, int col) const { return loop_->R(row, col); }
97
98 // Returns the unprofiled goal.
99 const Eigen::Matrix<double, 3, 1> &unprofiled_goal() const {
100 return unprofiled_goal_;
101 }
102 double unprofiled_goal(int row, int col) const {
103 return unprofiled_goal_(row, col);
104 }
105
106 // Returns the current state estimate.
107 const Eigen::Matrix<double, 3, 1> &X_hat() const { return loop_->X_hat(); }
108 double X_hat(int row, int col) const { return loop_->X_hat(row, col); }
109
110 private:
111 // Limits the provided goal to the soft limits. Prints "name" when it fails
112 // to aid debugging.
113 void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal);
114
115 void UpdateIntakeOffset(double offset);
116
117 ::std::unique_ptr<SimpleCappedStateFeedbackLoop> loop_;
118
119 ::frc971::zeroing::ZeroingEstimator estimator_;
120 aos::util::TrapezoidProfile profile_;
121
122 // Current measurement.
123 Eigen::Matrix<double, 1, 1> Y_;
124 // Current offset. Y_ = offset_ + raw_sensor;
125 Eigen::Matrix<double, 1, 1> offset_;
126
127 // The goal that the profile tries to reach.
128 Eigen::Matrix<double, 3, 1> unprofiled_goal_;
129
130 bool initialized_ = false;
131 bool zeroed_ = false;
132};
133
134class Arm {
135 public:
136 Arm();
137 // Returns whether the estimators have been initialized and zeroed.
138 bool initialized() const { return initialized_; }
139 bool zeroed() const { return shoulder_zeroed_ && wrist_zeroed_; }
140 bool shoulder_zeroed() const { return shoulder_zeroed_; }
141 bool wrist_zeroed() const { return wrist_zeroed_; }
142
143 // Updates our estimator with the latest position.
144 void Correct(::frc971::PotAndIndexPosition position_shoulder,
145 ::frc971::PotAndIndexPosition position_wrist);
146
147 // Forces the goal to be the provided goal.
148 void ForceGoal(double unprofiled_goal_shoulder, double unprofiled_goal_wrist);
149 // Sets the unprofiled goal. The profiler will generate a profile to go to
150 // this goal.
151 void set_unprofiled_goal(double unprofiled_goal_shoulder,
152 double unprofiled_goal_wrist);
153
154 // Runs the controller and profile generator for a cycle.
155 void Update(bool disabled);
156
157 // Limits our profiles to a max velocity and acceleration for proper motion.
158 void AdjustProfile(double max_angular_velocity_shoulder,
159 double max_angular_acceleration_shoulder,
160 double max_angular_velocity_wrist,
161 double max_angular_acceleration_wrist);
162 void set_max_voltage(double shoulder_max_voltage, double wrist_max_voltage);
163
164 // Returns true if we have exceeded any hard limits.
165 bool CheckHardLimits();
166 // Resets the internal state.
167 void Reset();
168
169 // Returns the current internal estimator state for logging.
170 ::frc971::EstimatorState ShoulderEstimatorState();
171 ::frc971::EstimatorState WristEstimatorState();
172
173 // Returns the requested shoulder and wrist voltages.
174 double shoulder_voltage() const { return loop_->U(0, 0); }
175 double wrist_voltage() const { return loop_->U(1, 0); }
176
177 // Returns the current positions.
178 double shoulder_angle() const { return Y_(0, 0); }
179 double wrist_angle() const { return Y_(1, 0) + Y_(0, 0); }
180
181 // Returns the unprofiled goal.
182 const Eigen::Matrix<double, 6, 1> &unprofiled_goal() const {
183 return unprofiled_goal_;
184 }
185 double unprofiled_goal(int row, int col) const {
186 return unprofiled_goal_(row, col);
187 }
188
189 // Returns the filtered goal.
190 const Eigen::Matrix<double, 6, 1> &goal() const { return loop_->R(); }
191 double goal(int row, int col) const { return loop_->R(row, col); }
192
193 // Returns the current state estimate.
194 const Eigen::Matrix<double, 6, 1> &X_hat() const { return loop_->X_hat(); }
195 double X_hat(int row, int col) const { return loop_->X_hat(row, col); }
196
197 private:
198 // Limits the provided goal to the soft limits. Prints "name" when it fails
199 // to aid debugging.
200 void CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal);
201
202 // Updates the offset
203 void UpdateWristOffset(double offset);
204 void UpdateShoulderOffset(double offset);
205
206 friend class testing::SuperstructureTest_DisabledGoalTest_Test;
207 ::std::unique_ptr<DoubleCappedStateFeedbackLoop> loop_;
208
209 aos::util::TrapezoidProfile shoulder_profile_, wrist_profile_;
210 ::frc971::zeroing::ZeroingEstimator shoulder_estimator_, wrist_estimator_;
211
212 // Current measurement.
213 Eigen::Matrix<double, 2, 1> Y_;
214 // Current offset. Y_ = offset_ + raw_sensor;
215 Eigen::Matrix<double, 2, 1> offset_;
216
217 // The goal that the profile tries to reach.
218 Eigen::Matrix<double, 6, 1> unprofiled_goal_;
219
220 bool initialized_ = false;
221 bool shoulder_zeroed_ = false;
222 bool wrist_zeroed_ = false;
223};
224
225} // namespace superstructure
226} // namespace control_loops
227} // namespace y2016
228
229#endif // Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_CONTROLS_H_