blob: b4fbead04078b73e6c685e1870dde8459df288f6 [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"
Brian Silverman2b1957a2016-02-14 20:29:57 -05008#include "frc971/control_loops/simple_capped_state_feedback_loop.h"
Austin Schuh10c2d112016-02-14 13:42:28 -08009#include "aos/common/util/trapezoid_profile.h"
10
11#include "frc971/zeroing/zeroing.h"
12#include "y2016/control_loops/superstructure/superstructure.q.h"
Austin Schuhf59b6bc2016-03-11 21:26:19 -080013#include "y2016/control_loops/superstructure/integral_arm_plant.h"
Austin Schuh10c2d112016-02-14 13:42:28 -080014
15namespace y2016 {
16namespace control_loops {
17namespace superstructure {
18namespace testing {
19class SuperstructureTest_DisabledGoalTest_Test;
20} // namespace testing
21
Austin Schuh10c2d112016-02-14 13:42:28 -080022class Intake {
23 public:
24 Intake();
25 // Returns whether the estimators have been initialized and zeroed.
26 bool initialized() const { return initialized_; }
27 bool zeroed() const { return zeroed_; }
Diana Vandenberge2843c62016-02-13 17:44:20 -080028 // Returns whether an error has occured
29 bool error() const { return estimator_.error(); }
Austin Schuh10c2d112016-02-14 13:42:28 -080030
31 // Updates our estimator with the latest position.
32 void Correct(::frc971::PotAndIndexPosition position);
Adam Snaider06779722016-02-14 15:26:22 -080033 // Runs the controller and profile generator for a cycle.
34 void Update(bool disabled);
35 // Sets the maximum voltage that will be commanded by the loop.
36 void set_max_voltage(double voltage);
Austin Schuh10c2d112016-02-14 13:42:28 -080037
38 // Forces the current goal to the provided goal, bypassing the profiler.
39 void ForceGoal(double goal);
40 // Sets the unprofiled goal. The profiler will generate a profile to go to
41 // this goal.
42 void set_unprofiled_goal(double unprofiled_goal);
Austin Schuh10c2d112016-02-14 13:42:28 -080043 // Limits our profiles to a max velocity and acceleration for proper motion.
44 void AdjustProfile(double max_angular_velocity,
45 double max_angular_acceleration);
Austin Schuh10c2d112016-02-14 13:42:28 -080046
47 // Returns true if we have exceeded any hard limits.
48 bool CheckHardLimits();
49 // Resets the internal state.
50 void Reset();
51
52 // Returns the current internal estimator state for logging.
53 ::frc971::EstimatorState IntakeEstimatorState();
54
55 // Returns the requested intake voltage.
56 double intake_voltage() const { return loop_->U(0, 0); }
57
58 // Returns the current position.
59 double angle() const { return Y_(0, 0); }
60
Austin Schuhbe041152016-02-28 22:01:52 -080061 // Returns the controller error.
62 const StateFeedbackLoop<3, 1, 1> &controller() const { return *loop_; }
63
Austin Schuh10c2d112016-02-14 13:42:28 -080064 // Returns the filtered goal.
65 const Eigen::Matrix<double, 3, 1> &goal() const { return loop_->R(); }
66 double goal(int row, int col) const { return loop_->R(row, col); }
67
68 // Returns the unprofiled goal.
69 const Eigen::Matrix<double, 3, 1> &unprofiled_goal() const {
70 return unprofiled_goal_;
71 }
72 double unprofiled_goal(int row, int col) const {
73 return unprofiled_goal_(row, col);
74 }
75
76 // Returns the current state estimate.
77 const Eigen::Matrix<double, 3, 1> &X_hat() const { return loop_->X_hat(); }
78 double X_hat(int row, int col) const { return loop_->X_hat(row, col); }
79
Diana Vandenberge2843c62016-02-13 17:44:20 -080080 // For testing:
81 // Triggers an estimator error.
82 void TriggerEstimatorError() { estimator_.TriggerError(); }
83
Austin Schuh10c2d112016-02-14 13:42:28 -080084 private:
85 // Limits the provided goal to the soft limits. Prints "name" when it fails
86 // to aid debugging.
87 void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal);
88
89 void UpdateIntakeOffset(double offset);
90
Brian Silverman2b1957a2016-02-14 20:29:57 -050091 ::std::unique_ptr<
92 ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>> loop_;
Austin Schuh10c2d112016-02-14 13:42:28 -080093
94 ::frc971::zeroing::ZeroingEstimator estimator_;
95 aos::util::TrapezoidProfile profile_;
96
97 // Current measurement.
98 Eigen::Matrix<double, 1, 1> Y_;
99 // Current offset. Y_ = offset_ + raw_sensor;
100 Eigen::Matrix<double, 1, 1> offset_;
101
102 // The goal that the profile tries to reach.
103 Eigen::Matrix<double, 3, 1> unprofiled_goal_;
104
105 bool initialized_ = false;
106 bool zeroed_ = false;
107};
108
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800109class ArmControlLoop
110 : public ::frc971::control_loops::SimpleCappedStateFeedbackLoop<6, 2, 2> {
111 public:
112 ArmControlLoop(SimpleCappedStateFeedbackLoop<6, 2, 2> &&loop)
113 : SimpleCappedStateFeedbackLoop<6, 2, 2>(::std::move(loop)) {}
114
115 const Eigen::Matrix<double, 2, 1> ControllerOutput() override {
116 const Eigen::Matrix<double, 2, 1> accelerating_ff =
117 controller(0).Kff * (next_R() - controller(0).plant.A() * R());
118 const Eigen::Matrix<double, 2, 1> accelerating_controller =
119 controller(0).K * error() + accelerating_ff;
120
121 const Eigen::Matrix<double, 2, 1> decelerating_ff =
122 controller(1).Kff * (next_R() - controller(1).plant.A() * R());
123 const Eigen::Matrix<double, 2, 1> decelerating_controller =
124 controller(1).K * error() + decelerating_ff;
125
126 const double bemf_voltage = X_hat(1, 0) / kV_shoulder;
127 bool use_accelerating_controller = true;
128 LOG(DEBUG, "Accelerating at %f, decel %f, bemf %f\n",
129 accelerating_controller(0, 0), accelerating_controller(1, 0),
130 bemf_voltage);
131 if (IsAccelerating(bemf_voltage, accelerating_controller(0, 0))) {
132 use_accelerating_controller = true;
133 } else {
134 use_accelerating_controller = false;
135 }
136 if (use_accelerating_controller) {
137 ff_U_ = accelerating_ff;
138 set_controller_index(0);
139 return accelerating_controller;
140 } else {
141 set_controller_index(1);
142 ff_U_ = decelerating_ff;
143 return decelerating_controller;
144 }
145 }
146
147 private:
148 bool IsAccelerating(double bemf_voltage, double voltage) {
149 if (bemf_voltage > 0) {
150 return voltage > bemf_voltage;
151 } else {
152 return voltage < bemf_voltage;
153 }
154 }
155};
Diana Vandenberge2843c62016-02-13 17:44:20 -0800156
Austin Schuh10c2d112016-02-14 13:42:28 -0800157class Arm {
158 public:
159 Arm();
160 // Returns whether the estimators have been initialized and zeroed.
161 bool initialized() const { return initialized_; }
162 bool zeroed() const { return shoulder_zeroed_ && wrist_zeroed_; }
163 bool shoulder_zeroed() const { return shoulder_zeroed_; }
164 bool wrist_zeroed() const { return wrist_zeroed_; }
Diana Vandenberge2843c62016-02-13 17:44:20 -0800165 // Returns whether an error has occured
166 bool error() const {
167 return shoulder_estimator_.error() || wrist_estimator_.error();
168 }
Austin Schuh10c2d112016-02-14 13:42:28 -0800169
170 // Updates our estimator with the latest position.
171 void Correct(::frc971::PotAndIndexPosition position_shoulder,
172 ::frc971::PotAndIndexPosition position_wrist);
173
174 // Forces the goal to be the provided goal.
175 void ForceGoal(double unprofiled_goal_shoulder, double unprofiled_goal_wrist);
176 // Sets the unprofiled goal. The profiler will generate a profile to go to
177 // this goal.
178 void set_unprofiled_goal(double unprofiled_goal_shoulder,
179 double unprofiled_goal_wrist);
180
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800181 int controller_index() const { return loop_->controller_index(); }
182
Austin Schuh10c2d112016-02-14 13:42:28 -0800183 // Runs the controller and profile generator for a cycle.
184 void Update(bool disabled);
185
186 // Limits our profiles to a max velocity and acceleration for proper motion.
187 void AdjustProfile(double max_angular_velocity_shoulder,
188 double max_angular_acceleration_shoulder,
189 double max_angular_velocity_wrist,
190 double max_angular_acceleration_wrist);
191 void set_max_voltage(double shoulder_max_voltage, double wrist_max_voltage);
192
Austin Schuh9f4e8a72016-02-16 15:28:47 -0800193 void set_shoulder_asymetric_limits(double shoulder_min_voltage,
194 double shoulder_max_voltage) {
195 loop_->set_asymetric_voltage(0, shoulder_min_voltage, shoulder_max_voltage);
196 }
197
Austin Schuh10c2d112016-02-14 13:42:28 -0800198 // Returns true if we have exceeded any hard limits.
199 bool CheckHardLimits();
200 // Resets the internal state.
201 void Reset();
202
203 // Returns the current internal estimator state for logging.
204 ::frc971::EstimatorState ShoulderEstimatorState();
205 ::frc971::EstimatorState WristEstimatorState();
206
207 // Returns the requested shoulder and wrist voltages.
208 double shoulder_voltage() const { return loop_->U(0, 0); }
209 double wrist_voltage() const { return loop_->U(1, 0); }
210
211 // Returns the current positions.
212 double shoulder_angle() const { return Y_(0, 0); }
213 double wrist_angle() const { return Y_(1, 0) + Y_(0, 0); }
214
Austin Schuhbe041152016-02-28 22:01:52 -0800215 // Returns the controller error.
216 const StateFeedbackLoop<6, 2, 2> &controller() const { return *loop_; }
217
Austin Schuh10c2d112016-02-14 13:42:28 -0800218 // Returns the unprofiled goal.
219 const Eigen::Matrix<double, 6, 1> &unprofiled_goal() const {
220 return unprofiled_goal_;
221 }
222 double unprofiled_goal(int row, int col) const {
223 return unprofiled_goal_(row, col);
224 }
225
226 // Returns the filtered goal.
227 const Eigen::Matrix<double, 6, 1> &goal() const { return loop_->R(); }
228 double goal(int row, int col) const { return loop_->R(row, col); }
229
230 // Returns the current state estimate.
231 const Eigen::Matrix<double, 6, 1> &X_hat() const { return loop_->X_hat(); }
232 double X_hat(int row, int col) const { return loop_->X_hat(row, col); }
233
Diana Vandenberge2843c62016-02-13 17:44:20 -0800234 // For testing:
235 // Triggers an estimator error.
236 void TriggerEstimatorError() { shoulder_estimator_.TriggerError(); }
237
Austin Schuh10c2d112016-02-14 13:42:28 -0800238 private:
239 // Limits the provided goal to the soft limits. Prints "name" when it fails
240 // to aid debugging.
241 void CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal);
242
243 // Updates the offset
244 void UpdateWristOffset(double offset);
245 void UpdateShoulderOffset(double offset);
246
247 friend class testing::SuperstructureTest_DisabledGoalTest_Test;
Brian Silverman2b1957a2016-02-14 20:29:57 -0500248 ::std::unique_ptr<
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800249 ArmControlLoop> loop_;
Austin Schuh10c2d112016-02-14 13:42:28 -0800250
251 aos::util::TrapezoidProfile shoulder_profile_, wrist_profile_;
252 ::frc971::zeroing::ZeroingEstimator shoulder_estimator_, wrist_estimator_;
253
254 // Current measurement.
255 Eigen::Matrix<double, 2, 1> Y_;
256 // Current offset. Y_ = offset_ + raw_sensor;
257 Eigen::Matrix<double, 2, 1> offset_;
258
259 // The goal that the profile tries to reach.
260 Eigen::Matrix<double, 6, 1> unprofiled_goal_;
261
262 bool initialized_ = false;
263 bool shoulder_zeroed_ = false;
264 bool wrist_zeroed_ = false;
265};
266
267} // namespace superstructure
268} // namespace control_loops
269} // namespace y2016
270
271#endif // Y2016_CONTROL_LOOPS_SUPERSTRUCTURE_SUPERSTRUCTURE_CONTROLS_H_