blob: 0a0a825758cf3cc1346111ed9f2e60712b2dc886 [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
Diana Vandenberge2843c62016-02-13 17:44:20 -080057 if (estimator_.error()) {
58 LOG(ERROR, "zeroing error with intake_estimator\n");
59 return;
60 }
61
Austin Schuh10c2d112016-02-14 13:42:28 -080062 if (!initialized_) {
63 if (estimator_.offset_ready()) {
64 UpdateIntakeOffset(estimator_.offset());
65 initialized_ = true;
66 }
67 }
68
69 if (!zeroed_ && estimator_.zeroed()) {
70 UpdateIntakeOffset(estimator_.offset());
71 zeroed_ = true;
72 }
73
74 Y_ << position.encoder;
75 Y_ += offset_;
76 loop_->Correct(Y_);
77}
78
79void Intake::CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal) {
Austin Schuh10c2d112016-02-14 13:42:28 -080080 // Limit the goal to min/max allowable angles.
Austin Schuh3b0f3642016-02-14 21:04:26 -080081 if ((*goal)(0, 0) > constants::Values::kIntakeRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -080082 LOG(WARNING, "Intake goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +000083 constants::Values::kIntakeRange.upper);
84 (*goal)(0, 0) = constants::Values::kIntakeRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -080085 }
Austin Schuh3b0f3642016-02-14 21:04:26 -080086 if ((*goal)(0, 0) < constants::Values::kIntakeRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -080087 LOG(WARNING, "Intake goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +000088 constants::Values::kIntakeRange.lower);
89 (*goal)(0, 0) = constants::Values::kIntakeRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -080090 }
91}
92
93void Intake::ForceGoal(double goal) {
94 set_unprofiled_goal(goal);
95 loop_->mutable_R() = unprofiled_goal_;
96 loop_->mutable_next_R() = loop_->R();
97
98 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
99}
100
101void Intake::set_unprofiled_goal(double unprofiled_goal) {
102 unprofiled_goal_(0, 0) = unprofiled_goal;
103 unprofiled_goal_(1, 0) = 0.0;
104 unprofiled_goal_(2, 0) = 0.0;
105 CapGoal("unprofiled R", &unprofiled_goal_);
106}
107
108void Intake::Update(bool disable) {
109 if (!disable) {
110 ::Eigen::Matrix<double, 2, 1> goal_state =
111 profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
112
113 loop_->mutable_next_R(0, 0) = goal_state(0, 0);
114 loop_->mutable_next_R(1, 0) = goal_state(1, 0);
115 loop_->mutable_next_R(2, 0) = 0.0;
116 CapGoal("next R", &loop_->mutable_next_R());
117 }
118
119 loop_->Update(disable);
120
121 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
122 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
123 }
124}
125
126bool Intake::CheckHardLimits() {
Austin Schuh10c2d112016-02-14 13:42:28 -0800127 // Returns whether hard limits have been exceeded.
128
Austin Schuh3b0f3642016-02-14 21:04:26 -0800129 if (angle() > constants::Values::kIntakeRange.upper_hard ||
130 angle() < constants::Values::kIntakeRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800131 LOG(ERROR, "Intake at %f out of bounds [%f, %f], ESTOPing\n", angle(),
Austin Schuhb1d682b2016-02-16 13:07:44 -0800132 constants::Values::kIntakeRange.lower_hard,
133 constants::Values::kIntakeRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800134 return true;
135 }
136
137 return false;
138}
139
140void Intake::set_max_voltage(double voltage) {
Austin Schuhdc710d22016-02-16 13:55:26 -0800141 loop_->set_max_voltage(0, voltage);
Austin Schuh10c2d112016-02-14 13:42:28 -0800142}
143
144void Intake::AdjustProfile(double max_angular_velocity,
145 double max_angular_acceleration) {
146 profile_.set_maximum_velocity(UseUnlessZero(max_angular_velocity, 10.0));
147 profile_.set_maximum_acceleration(
148 UseUnlessZero(max_angular_acceleration, 10.0));
149}
150
151void Intake::Reset() {
152 estimator_.Reset();
153 initialized_ = false;
154 zeroed_ = false;
155}
156
157EstimatorState Intake::IntakeEstimatorState() {
158 EstimatorState estimator_state;
159 ::frc971::zeroing::PopulateEstimatorState(estimator_, &estimator_state);
160
161 return estimator_state;
162}
163
164Arm::Arm()
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800165 : loop_(new ArmControlLoop(
Austin Schuh10c2d112016-02-14 13:42:28 -0800166 ::y2016::control_loops::superstructure::MakeIntegralArmLoop())),
167 shoulder_profile_(::aos::controls::kLoopFrequency),
168 wrist_profile_(::aos::controls::kLoopFrequency),
169 shoulder_estimator_(constants::GetValues().shoulder.zeroing),
170 wrist_estimator_(constants::GetValues().wrist.zeroing) {
171 Y_.setZero();
172 offset_.setZero();
173 unprofiled_goal_.setZero();
174 AdjustProfile(0.0, 0.0, 0.0, 0.0);
175}
176
177void Arm::UpdateWristOffset(double offset) {
178 const double doffset = offset - offset_(1, 0);
179 LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), offset);
180
181 loop_->mutable_X_hat()(2, 0) += doffset;
182 Y_(1, 0) += doffset;
183 loop_->mutable_R(2, 0) += doffset;
184 loop_->mutable_next_R(2, 0) += doffset;
185 unprofiled_goal_(2, 0) += doffset;
186
187 wrist_profile_.MoveGoal(doffset);
188 offset_(1, 0) = offset;
189
190 CapGoal("R", &loop_->mutable_R());
191 CapGoal("unprofiled R", &loop_->mutable_next_R());
192}
193
194void Arm::UpdateShoulderOffset(double offset) {
195 const double doffset = offset - offset_(0, 0);
196 LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), offset);
197
198 loop_->mutable_X_hat()(0, 0) += doffset;
199 loop_->mutable_X_hat()(2, 0) += doffset;
200 Y_(0, 0) += doffset;
201 loop_->mutable_R(0, 0) += doffset;
202 loop_->mutable_R(2, 0) += doffset;
203 loop_->mutable_next_R(0, 0) += doffset;
204 loop_->mutable_next_R(2, 0) += doffset;
205 unprofiled_goal_(0, 0) += doffset;
206 unprofiled_goal_(2, 0) += doffset;
207
208 shoulder_profile_.MoveGoal(doffset);
209 wrist_profile_.MoveGoal(doffset);
210 offset_(0, 0) = offset;
211
212 CapGoal("R", &loop_->mutable_R());
213 CapGoal("unprofiled R", &loop_->mutable_next_R());
214}
215
216// TODO(austin): Handle zeroing errors.
217
218void Arm::Correct(PotAndIndexPosition position_shoulder,
219 PotAndIndexPosition position_wrist) {
220 shoulder_estimator_.UpdateEstimate(position_shoulder);
221 wrist_estimator_.UpdateEstimate(position_wrist);
222
Diana Vandenberge2843c62016-02-13 17:44:20 -0800223 // Handle zeroing errors
224 if (shoulder_estimator_.error()) {
225 LOG(ERROR, "zeroing error with shoulder_estimator\n");
226 return;
227 }
228 if (wrist_estimator_.error()) {
229 LOG(ERROR, "zeroing error with wrist_estimator\n");
230 return;
231 }
232
Austin Schuh10c2d112016-02-14 13:42:28 -0800233 if (!initialized_) {
234 if (shoulder_estimator_.offset_ready() && wrist_estimator_.offset_ready()) {
235 UpdateShoulderOffset(shoulder_estimator_.offset());
236 UpdateWristOffset(wrist_estimator_.offset());
237 initialized_ = true;
238 }
239 }
240
241 if (!shoulder_zeroed_ && shoulder_estimator_.zeroed()) {
242 UpdateShoulderOffset(shoulder_estimator_.offset());
243 shoulder_zeroed_ = true;
244 }
245 if (!wrist_zeroed_ && wrist_estimator_.zeroed()) {
246 UpdateWristOffset(wrist_estimator_.offset());
247 wrist_zeroed_ = true;
248 }
249
250 {
251 Y_ << position_shoulder.encoder, position_wrist.encoder;
252 Y_ += offset_;
253 loop_->Correct(Y_);
254 }
255}
256
257void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) {
258 // Limit the goals to min/max allowable angles.
Austin Schuh10c2d112016-02-14 13:42:28 -0800259
Austin Schuh3b0f3642016-02-14 21:04:26 -0800260 if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800261 LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000262 constants::Values::kShoulderRange.upper);
263 (*goal)(0, 0) = constants::Values::kShoulderRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -0800264 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800265 if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800266 LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000267 constants::Values::kShoulderRange.lower);
268 (*goal)(0, 0) = constants::Values::kShoulderRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -0800269 }
270
271 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
272
Austin Schuh3b0f3642016-02-14 21:04:26 -0800273 if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800274 LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000275 wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper);
276 (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800277 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800278 if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800279 LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000280 wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower);
281 (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800282 }
283}
284
285void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
286 set_unprofiled_goal(goal_shoulder, goal_wrist);
287 loop_->mutable_R() = unprofiled_goal_;
288 loop_->mutable_next_R() = loop_->R();
289
290 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
291 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
292}
293
294void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
295 double unprofiled_goal_wrist) {
296 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
297 0.0, 0.0;
298 CapGoal("unprofiled R", &unprofiled_goal_);
299}
300
301void Arm::AdjustProfile(double max_angular_velocity_shoulder,
302 double max_angular_acceleration_shoulder,
303 double max_angular_velocity_wrist,
304 double max_angular_acceleration_wrist) {
305 shoulder_profile_.set_maximum_velocity(
306 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
307 shoulder_profile_.set_maximum_acceleration(
308 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
309 wrist_profile_.set_maximum_velocity(
310 UseUnlessZero(max_angular_velocity_wrist, 10.0));
311 wrist_profile_.set_maximum_acceleration(
312 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
313}
314
315bool Arm::CheckHardLimits() {
Austin Schuh3b0f3642016-02-14 21:04:26 -0800316 if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard ||
317 shoulder_angle() < constants::Values::kShoulderRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800318 LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
Comran Morshed225f0b92016-02-10 20:34:27 +0000319 shoulder_angle(), constants::Values::kShoulderRange.lower_hard,
320 constants::Values::kShoulderRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800321 return true;
322 }
323
Austin Schuhb1d682b2016-02-16 13:07:44 -0800324 if (wrist_angle() - shoulder_angle() >
325 constants::Values::kWristRange.upper_hard ||
326 wrist_angle() - shoulder_angle() <
327 constants::Values::kWristRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800328 LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuhb1d682b2016-02-16 13:07:44 -0800329 wrist_angle() - shoulder_angle(),
330 constants::Values::kWristRange.lower_hard,
Comran Morshed225f0b92016-02-10 20:34:27 +0000331 constants::Values::kWristRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800332 return true;
333 }
334
335 return false;
336}
337
338void Arm::Update(bool disable) {
339 if (!disable) {
340 // Compute next goal.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800341 loop_->mutable_next_R().block<2, 1>(0, 0) = shoulder_profile_.Update(
342 unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800343
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800344 loop_->mutable_next_R().block<2, 1>(2, 0) =
Austin Schuh10c2d112016-02-14 13:42:28 -0800345 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800346
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800347 loop_->mutable_next_R().block<2, 1>(4, 0) =
348 unprofiled_goal_.block<2, 1>(4, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800349 CapGoal("next R", &loop_->mutable_next_R());
350 }
351
352 // Move loop
353 loop_->Update(disable);
354
355 // Shoulder saturated
356 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
357 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
358 }
359
360 // Wrist saturated
361 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
362 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
363 }
364}
365
366void Arm::set_max_voltage(double shoulder_max_voltage,
367 double wrist_max_voltage) {
Austin Schuhdc710d22016-02-16 13:55:26 -0800368 loop_->set_max_voltage(0, shoulder_max_voltage);
369 loop_->set_max_voltage(1, wrist_max_voltage);
Austin Schuh10c2d112016-02-14 13:42:28 -0800370}
371
372void Arm::Reset() {
373 shoulder_estimator_.Reset();
374 wrist_estimator_.Reset();
375 initialized_ = false;
376 shoulder_zeroed_ = false;
377 wrist_zeroed_ = false;
378}
379
380EstimatorState Arm::ShoulderEstimatorState() {
381 EstimatorState estimator_state;
382 ::frc971::zeroing::PopulateEstimatorState(shoulder_estimator_,
383 &estimator_state);
384
385 return estimator_state;
386}
387
388EstimatorState Arm::WristEstimatorState() {
389 EstimatorState estimator_state;
390 ::frc971::zeroing::PopulateEstimatorState(wrist_estimator_, &estimator_state);
391
392 return estimator_state;
393}
394
395} // namespace superstructure
396} // namespace control_loops
397} // namespace y2016