blob: 3c3e690ec0febb9cc220d9c4416a2cff44501b2a [file] [log] [blame]
Austin Schuh10c2d112016-02-14 13:42:28 -08001#include "y2016/control_loops/superstructure/superstructure_controls.h"
2
3#include "aos/common/controls/control_loops.q.h"
4#include "aos/common/logging/logging.h"
5
6#include "y2016/control_loops/superstructure/integral_intake_plant.h"
7#include "y2016/control_loops/superstructure/integral_arm_plant.h"
8
9#include "y2016/constants.h"
10
11namespace y2016 {
12namespace control_loops {
13namespace superstructure {
14
15using ::frc971::PotAndIndexPosition;
16using ::frc971::EstimatorState;
17
18namespace {
19double UseUnlessZero(double target_value, double default_value) {
20 if (target_value != 0.0) {
21 return target_value;
22 } else {
23 return default_value;
24 }
25}
26} // namespace
27
Austin Schuh10c2d112016-02-14 13:42:28 -080028// Intake
29Intake::Intake()
Brian Silverman2b1957a2016-02-14 20:29:57 -050030 : loop_(new ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>(
31 ::y2016::control_loops::superstructure::MakeIntegralIntakeLoop())),
Austin Schuh10c2d112016-02-14 13:42:28 -080032 estimator_(constants::GetValues().intake.zeroing),
33 profile_(::aos::controls::kLoopFrequency) {
34 Y_.setZero();
35 unprofiled_goal_.setZero();
36 offset_.setZero();
37 AdjustProfile(0.0, 0.0);
38}
39
40void Intake::UpdateIntakeOffset(double offset) {
41 const double doffset = offset - offset_(0, 0);
42 LOG(INFO, "Adjusting Intake offset from %f to %f\n", offset_(0, 0), offset);
43
44 loop_->mutable_X_hat()(0, 0) += doffset;
45 Y_(0, 0) += doffset;
46 loop_->mutable_R(0, 0) += doffset;
47
48 profile_.MoveGoal(doffset);
49 offset_(0, 0) = offset;
50
51 CapGoal("R", &loop_->mutable_R());
52}
53
54void Intake::Correct(PotAndIndexPosition position) {
55 estimator_.UpdateEstimate(position);
56
57 if (!initialized_) {
58 if (estimator_.offset_ready()) {
59 UpdateIntakeOffset(estimator_.offset());
60 initialized_ = true;
61 }
62 }
63
64 if (!zeroed_ && estimator_.zeroed()) {
65 UpdateIntakeOffset(estimator_.offset());
66 zeroed_ = true;
67 }
68
69 Y_ << position.encoder;
70 Y_ += offset_;
71 loop_->Correct(Y_);
72}
73
74void Intake::CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal) {
75 const auto &values = constants::GetValues();
76
77 // Limit the goal to min/max allowable angles.
78 if ((*goal)(0, 0) >= values.intake.limits.upper) {
79 LOG(WARNING, "Intake goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
80 values.intake.limits.upper);
81 (*goal)(0, 0) = values.intake.limits.upper;
82 }
83 if ((*goal)(0, 0) <= values.intake.limits.lower) {
84 LOG(WARNING, "Intake goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
85 values.intake.limits.lower);
86 (*goal)(0, 0) = values.intake.limits.lower;
87 }
88}
89
90void Intake::ForceGoal(double goal) {
91 set_unprofiled_goal(goal);
92 loop_->mutable_R() = unprofiled_goal_;
93 loop_->mutable_next_R() = loop_->R();
94
95 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
96}
97
98void Intake::set_unprofiled_goal(double unprofiled_goal) {
99 unprofiled_goal_(0, 0) = unprofiled_goal;
100 unprofiled_goal_(1, 0) = 0.0;
101 unprofiled_goal_(2, 0) = 0.0;
102 CapGoal("unprofiled R", &unprofiled_goal_);
103}
104
105void Intake::Update(bool disable) {
106 if (!disable) {
107 ::Eigen::Matrix<double, 2, 1> goal_state =
108 profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
109
110 loop_->mutable_next_R(0, 0) = goal_state(0, 0);
111 loop_->mutable_next_R(1, 0) = goal_state(1, 0);
112 loop_->mutable_next_R(2, 0) = 0.0;
113 CapGoal("next R", &loop_->mutable_next_R());
114 }
115
116 loop_->Update(disable);
117
118 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
119 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
120 }
121}
122
123bool Intake::CheckHardLimits() {
124 const auto &values = constants::GetValues();
125 // Returns whether hard limits have been exceeded.
126
127 if (angle() >= values.intake.limits.upper_hard ||
128 angle() <= values.intake.limits.lower_hard) {
129 LOG(ERROR, "Intake at %f out of bounds [%f, %f], ESTOPing\n", angle(),
130 values.intake.limits.lower_hard, values.intake.limits.upper_hard);
131 return true;
132 }
133
134 return false;
135}
136
137void Intake::set_max_voltage(double voltage) {
Brian Silverman2b1957a2016-02-14 20:29:57 -0500138 loop_->set_max_voltages(voltage);
Austin Schuh10c2d112016-02-14 13:42:28 -0800139}
140
141void Intake::AdjustProfile(double max_angular_velocity,
142 double max_angular_acceleration) {
143 profile_.set_maximum_velocity(UseUnlessZero(max_angular_velocity, 10.0));
144 profile_.set_maximum_acceleration(
145 UseUnlessZero(max_angular_acceleration, 10.0));
146}
147
148void Intake::Reset() {
149 estimator_.Reset();
150 initialized_ = false;
151 zeroed_ = false;
152}
153
154EstimatorState Intake::IntakeEstimatorState() {
155 EstimatorState estimator_state;
156 ::frc971::zeroing::PopulateEstimatorState(estimator_, &estimator_state);
157
158 return estimator_state;
159}
160
161Arm::Arm()
Brian Silverman2b1957a2016-02-14 20:29:57 -0500162 : loop_(new ::frc971::control_loops::SimpleCappedStateFeedbackLoop<6, 2, 2>(
Austin Schuh10c2d112016-02-14 13:42:28 -0800163 ::y2016::control_loops::superstructure::MakeIntegralArmLoop())),
164 shoulder_profile_(::aos::controls::kLoopFrequency),
165 wrist_profile_(::aos::controls::kLoopFrequency),
166 shoulder_estimator_(constants::GetValues().shoulder.zeroing),
167 wrist_estimator_(constants::GetValues().wrist.zeroing) {
168 Y_.setZero();
169 offset_.setZero();
170 unprofiled_goal_.setZero();
171 AdjustProfile(0.0, 0.0, 0.0, 0.0);
172}
173
174void Arm::UpdateWristOffset(double offset) {
175 const double doffset = offset - offset_(1, 0);
176 LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), offset);
177
178 loop_->mutable_X_hat()(2, 0) += doffset;
179 Y_(1, 0) += doffset;
180 loop_->mutable_R(2, 0) += doffset;
181 loop_->mutable_next_R(2, 0) += doffset;
182 unprofiled_goal_(2, 0) += doffset;
183
184 wrist_profile_.MoveGoal(doffset);
185 offset_(1, 0) = offset;
186
187 CapGoal("R", &loop_->mutable_R());
188 CapGoal("unprofiled R", &loop_->mutable_next_R());
189}
190
191void Arm::UpdateShoulderOffset(double offset) {
192 const double doffset = offset - offset_(0, 0);
193 LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), offset);
194
195 loop_->mutable_X_hat()(0, 0) += doffset;
196 loop_->mutable_X_hat()(2, 0) += doffset;
197 Y_(0, 0) += doffset;
198 loop_->mutable_R(0, 0) += doffset;
199 loop_->mutable_R(2, 0) += doffset;
200 loop_->mutable_next_R(0, 0) += doffset;
201 loop_->mutable_next_R(2, 0) += doffset;
202 unprofiled_goal_(0, 0) += doffset;
203 unprofiled_goal_(2, 0) += doffset;
204
205 shoulder_profile_.MoveGoal(doffset);
206 wrist_profile_.MoveGoal(doffset);
207 offset_(0, 0) = offset;
208
209 CapGoal("R", &loop_->mutable_R());
210 CapGoal("unprofiled R", &loop_->mutable_next_R());
211}
212
213// TODO(austin): Handle zeroing errors.
214
215void Arm::Correct(PotAndIndexPosition position_shoulder,
216 PotAndIndexPosition position_wrist) {
217 shoulder_estimator_.UpdateEstimate(position_shoulder);
218 wrist_estimator_.UpdateEstimate(position_wrist);
219
220 if (!initialized_) {
221 if (shoulder_estimator_.offset_ready() && wrist_estimator_.offset_ready()) {
222 UpdateShoulderOffset(shoulder_estimator_.offset());
223 UpdateWristOffset(wrist_estimator_.offset());
224 initialized_ = true;
225 }
226 }
227
228 if (!shoulder_zeroed_ && shoulder_estimator_.zeroed()) {
229 UpdateShoulderOffset(shoulder_estimator_.offset());
230 shoulder_zeroed_ = true;
231 }
232 if (!wrist_zeroed_ && wrist_estimator_.zeroed()) {
233 UpdateWristOffset(wrist_estimator_.offset());
234 wrist_zeroed_ = true;
235 }
236
237 {
238 Y_ << position_shoulder.encoder, position_wrist.encoder;
239 Y_ += offset_;
240 loop_->Correct(Y_);
241 }
242}
243
244void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) {
245 // Limit the goals to min/max allowable angles.
246 const auto &values = constants::GetValues();
247
248 if ((*goal)(0, 0) >= values.shoulder.limits.upper) {
249 LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
250 values.shoulder.limits.upper);
251 (*goal)(0, 0) = values.shoulder.limits.upper;
252 }
253 if ((*goal)(0, 0) <= values.shoulder.limits.lower) {
254 LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
255 values.shoulder.limits.lower);
256 (*goal)(0, 0) = values.shoulder.limits.lower;
257 }
258
259 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
260
261 if (wrist_goal_angle_ungrounded >= values.wrist.limits.upper) {
262 LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
263 wrist_goal_angle_ungrounded, values.wrist.limits.upper);
264 (*goal)(2, 0) = values.wrist.limits.upper + (*goal)(0, 0);
265 }
266 if (wrist_goal_angle_ungrounded <= values.wrist.limits.lower) {
267 LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
268 wrist_goal_angle_ungrounded, values.wrist.limits.lower);
269 (*goal)(2, 0) = values.wrist.limits.lower + (*goal)(0, 0);
270 }
271}
272
273void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
274 set_unprofiled_goal(goal_shoulder, goal_wrist);
275 loop_->mutable_R() = unprofiled_goal_;
276 loop_->mutable_next_R() = loop_->R();
277
278 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
279 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
280}
281
282void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
283 double unprofiled_goal_wrist) {
284 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
285 0.0, 0.0;
286 CapGoal("unprofiled R", &unprofiled_goal_);
287}
288
289void Arm::AdjustProfile(double max_angular_velocity_shoulder,
290 double max_angular_acceleration_shoulder,
291 double max_angular_velocity_wrist,
292 double max_angular_acceleration_wrist) {
293 shoulder_profile_.set_maximum_velocity(
294 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
295 shoulder_profile_.set_maximum_acceleration(
296 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
297 wrist_profile_.set_maximum_velocity(
298 UseUnlessZero(max_angular_velocity_wrist, 10.0));
299 wrist_profile_.set_maximum_acceleration(
300 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
301}
302
303bool Arm::CheckHardLimits() {
304 const auto &values = constants::GetValues();
305 if (shoulder_angle() >= values.shoulder.limits.upper_hard ||
306 shoulder_angle() <= values.shoulder.limits.lower_hard) {
307 LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
308 shoulder_angle(), values.shoulder.limits.lower_hard,
309 values.shoulder.limits.upper_hard);
310 return true;
311 }
312
313 if (wrist_angle() - shoulder_angle() >= values.wrist.limits.upper_hard ||
314 wrist_angle() - shoulder_angle() <= values.wrist.limits.lower_hard) {
315 LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
316 wrist_angle() - shoulder_angle(), values.wrist.limits.lower_hard,
317 values.wrist.limits.upper_hard);
318 return true;
319 }
320
321 return false;
322}
323
324void Arm::Update(bool disable) {
325 if (!disable) {
326 // Compute next goal.
327 ::Eigen::Matrix<double, 2, 1> goal_state_shoulder =
328 shoulder_profile_.Update(unprofiled_goal_(0, 0),
329 unprofiled_goal_(1, 0));
330 loop_->mutable_next_R(0, 0) = goal_state_shoulder(0, 0);
331 loop_->mutable_next_R(1, 0) = goal_state_shoulder(1, 0);
332
333 ::Eigen::Matrix<double, 2, 1> goal_state_wrist =
334 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
335 loop_->mutable_next_R(2, 0) = goal_state_wrist(0, 0);
336 loop_->mutable_next_R(3, 0) = goal_state_wrist(1, 0);
337
338 loop_->mutable_next_R(4, 0) = unprofiled_goal_(4, 0);
339 loop_->mutable_next_R(5, 0) = unprofiled_goal_(5, 0);
340 CapGoal("next R", &loop_->mutable_next_R());
341 }
342
343 // Move loop
344 loop_->Update(disable);
345
346 // Shoulder saturated
347 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
348 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
349 }
350
351 // Wrist saturated
352 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
353 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
354 }
355}
356
357void Arm::set_max_voltage(double shoulder_max_voltage,
358 double wrist_max_voltage) {
Brian Silverman2b1957a2016-02-14 20:29:57 -0500359 loop_->set_max_voltages(shoulder_max_voltage, wrist_max_voltage);
Austin Schuh10c2d112016-02-14 13:42:28 -0800360}
361
362void Arm::Reset() {
363 shoulder_estimator_.Reset();
364 wrist_estimator_.Reset();
365 initialized_ = false;
366 shoulder_zeroed_ = false;
367 wrist_zeroed_ = false;
368}
369
370EstimatorState Arm::ShoulderEstimatorState() {
371 EstimatorState estimator_state;
372 ::frc971::zeroing::PopulateEstimatorState(shoulder_estimator_,
373 &estimator_state);
374
375 return estimator_state;
376}
377
378EstimatorState Arm::WristEstimatorState() {
379 EstimatorState estimator_state;
380 ::frc971::zeroing::PopulateEstimatorState(wrist_estimator_, &estimator_state);
381
382 return estimator_state;
383}
384
385} // namespace superstructure
386} // namespace control_loops
387} // namespace y2016