blob: a8953de9b19fdc599297c84709d376b8d3e29b17 [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) {
Austin Schuh10c2d112016-02-14 13:42:28 -080075 // Limit the goal to min/max allowable angles.
Austin Schuh3b0f3642016-02-14 21:04:26 -080076 if ((*goal)(0, 0) > constants::Values::kIntakeRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -080077 LOG(WARNING, "Intake goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +000078 constants::Values::kIntakeRange.upper);
79 (*goal)(0, 0) = constants::Values::kIntakeRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -080080 }
Austin Schuh3b0f3642016-02-14 21:04:26 -080081 if ((*goal)(0, 0) < constants::Values::kIntakeRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -080082 LOG(WARNING, "Intake goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +000083 constants::Values::kIntakeRange.lower);
84 (*goal)(0, 0) = constants::Values::kIntakeRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -080085 }
86}
87
88void Intake::ForceGoal(double goal) {
89 set_unprofiled_goal(goal);
90 loop_->mutable_R() = unprofiled_goal_;
91 loop_->mutable_next_R() = loop_->R();
92
93 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
94}
95
96void Intake::set_unprofiled_goal(double unprofiled_goal) {
97 unprofiled_goal_(0, 0) = unprofiled_goal;
98 unprofiled_goal_(1, 0) = 0.0;
99 unprofiled_goal_(2, 0) = 0.0;
100 CapGoal("unprofiled R", &unprofiled_goal_);
101}
102
103void Intake::Update(bool disable) {
104 if (!disable) {
105 ::Eigen::Matrix<double, 2, 1> goal_state =
106 profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
107
108 loop_->mutable_next_R(0, 0) = goal_state(0, 0);
109 loop_->mutable_next_R(1, 0) = goal_state(1, 0);
110 loop_->mutable_next_R(2, 0) = 0.0;
111 CapGoal("next R", &loop_->mutable_next_R());
112 }
113
114 loop_->Update(disable);
115
116 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
117 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
118 }
119}
120
121bool Intake::CheckHardLimits() {
Austin Schuh10c2d112016-02-14 13:42:28 -0800122 // Returns whether hard limits have been exceeded.
123
Austin Schuh3b0f3642016-02-14 21:04:26 -0800124 if (angle() > constants::Values::kIntakeRange.upper_hard ||
125 angle() < constants::Values::kIntakeRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800126 LOG(ERROR, "Intake at %f out of bounds [%f, %f], ESTOPing\n", angle(),
Comran Morshed225f0b92016-02-10 20:34:27 +0000127 constants::Values::kIntakeRange.lower_hard, constants::Values::kIntakeRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800128 return true;
129 }
130
131 return false;
132}
133
134void Intake::set_max_voltage(double voltage) {
Brian Silverman2b1957a2016-02-14 20:29:57 -0500135 loop_->set_max_voltages(voltage);
Austin Schuh10c2d112016-02-14 13:42:28 -0800136}
137
138void Intake::AdjustProfile(double max_angular_velocity,
139 double max_angular_acceleration) {
140 profile_.set_maximum_velocity(UseUnlessZero(max_angular_velocity, 10.0));
141 profile_.set_maximum_acceleration(
142 UseUnlessZero(max_angular_acceleration, 10.0));
143}
144
145void Intake::Reset() {
146 estimator_.Reset();
147 initialized_ = false;
148 zeroed_ = false;
149}
150
151EstimatorState Intake::IntakeEstimatorState() {
152 EstimatorState estimator_state;
153 ::frc971::zeroing::PopulateEstimatorState(estimator_, &estimator_state);
154
155 return estimator_state;
156}
157
158Arm::Arm()
Brian Silverman2b1957a2016-02-14 20:29:57 -0500159 : loop_(new ::frc971::control_loops::SimpleCappedStateFeedbackLoop<6, 2, 2>(
Austin Schuh10c2d112016-02-14 13:42:28 -0800160 ::y2016::control_loops::superstructure::MakeIntegralArmLoop())),
161 shoulder_profile_(::aos::controls::kLoopFrequency),
162 wrist_profile_(::aos::controls::kLoopFrequency),
163 shoulder_estimator_(constants::GetValues().shoulder.zeroing),
164 wrist_estimator_(constants::GetValues().wrist.zeroing) {
165 Y_.setZero();
166 offset_.setZero();
167 unprofiled_goal_.setZero();
168 AdjustProfile(0.0, 0.0, 0.0, 0.0);
169}
170
171void Arm::UpdateWristOffset(double offset) {
172 const double doffset = offset - offset_(1, 0);
173 LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), offset);
174
175 loop_->mutable_X_hat()(2, 0) += doffset;
176 Y_(1, 0) += doffset;
177 loop_->mutable_R(2, 0) += doffset;
178 loop_->mutable_next_R(2, 0) += doffset;
179 unprofiled_goal_(2, 0) += doffset;
180
181 wrist_profile_.MoveGoal(doffset);
182 offset_(1, 0) = offset;
183
184 CapGoal("R", &loop_->mutable_R());
185 CapGoal("unprofiled R", &loop_->mutable_next_R());
186}
187
188void Arm::UpdateShoulderOffset(double offset) {
189 const double doffset = offset - offset_(0, 0);
190 LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), offset);
191
192 loop_->mutable_X_hat()(0, 0) += doffset;
193 loop_->mutable_X_hat()(2, 0) += doffset;
194 Y_(0, 0) += doffset;
195 loop_->mutable_R(0, 0) += doffset;
196 loop_->mutable_R(2, 0) += doffset;
197 loop_->mutable_next_R(0, 0) += doffset;
198 loop_->mutable_next_R(2, 0) += doffset;
199 unprofiled_goal_(0, 0) += doffset;
200 unprofiled_goal_(2, 0) += doffset;
201
202 shoulder_profile_.MoveGoal(doffset);
203 wrist_profile_.MoveGoal(doffset);
204 offset_(0, 0) = offset;
205
206 CapGoal("R", &loop_->mutable_R());
207 CapGoal("unprofiled R", &loop_->mutable_next_R());
208}
209
210// TODO(austin): Handle zeroing errors.
211
212void Arm::Correct(PotAndIndexPosition position_shoulder,
213 PotAndIndexPosition position_wrist) {
214 shoulder_estimator_.UpdateEstimate(position_shoulder);
215 wrist_estimator_.UpdateEstimate(position_wrist);
216
217 if (!initialized_) {
218 if (shoulder_estimator_.offset_ready() && wrist_estimator_.offset_ready()) {
219 UpdateShoulderOffset(shoulder_estimator_.offset());
220 UpdateWristOffset(wrist_estimator_.offset());
221 initialized_ = true;
222 }
223 }
224
225 if (!shoulder_zeroed_ && shoulder_estimator_.zeroed()) {
226 UpdateShoulderOffset(shoulder_estimator_.offset());
227 shoulder_zeroed_ = true;
228 }
229 if (!wrist_zeroed_ && wrist_estimator_.zeroed()) {
230 UpdateWristOffset(wrist_estimator_.offset());
231 wrist_zeroed_ = true;
232 }
233
234 {
235 Y_ << position_shoulder.encoder, position_wrist.encoder;
236 Y_ += offset_;
237 loop_->Correct(Y_);
238 }
239}
240
241void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) {
242 // Limit the goals to min/max allowable angles.
Austin Schuh10c2d112016-02-14 13:42:28 -0800243
Austin Schuh3b0f3642016-02-14 21:04:26 -0800244 if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800245 LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000246 constants::Values::kShoulderRange.upper);
247 (*goal)(0, 0) = constants::Values::kShoulderRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -0800248 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800249 if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800250 LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000251 constants::Values::kShoulderRange.lower);
252 (*goal)(0, 0) = constants::Values::kShoulderRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -0800253 }
254
255 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
256
Austin Schuh3b0f3642016-02-14 21:04:26 -0800257 if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800258 LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000259 wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper);
260 (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800261 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800262 if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800263 LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000264 wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower);
265 (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800266 }
267}
268
269void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
270 set_unprofiled_goal(goal_shoulder, goal_wrist);
271 loop_->mutable_R() = unprofiled_goal_;
272 loop_->mutable_next_R() = loop_->R();
273
274 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
275 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
276}
277
278void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
279 double unprofiled_goal_wrist) {
280 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
281 0.0, 0.0;
282 CapGoal("unprofiled R", &unprofiled_goal_);
283}
284
285void Arm::AdjustProfile(double max_angular_velocity_shoulder,
286 double max_angular_acceleration_shoulder,
287 double max_angular_velocity_wrist,
288 double max_angular_acceleration_wrist) {
289 shoulder_profile_.set_maximum_velocity(
290 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
291 shoulder_profile_.set_maximum_acceleration(
292 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
293 wrist_profile_.set_maximum_velocity(
294 UseUnlessZero(max_angular_velocity_wrist, 10.0));
295 wrist_profile_.set_maximum_acceleration(
296 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
297}
298
299bool Arm::CheckHardLimits() {
Austin Schuh3b0f3642016-02-14 21:04:26 -0800300 if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard ||
301 shoulder_angle() < constants::Values::kShoulderRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800302 LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
Comran Morshed225f0b92016-02-10 20:34:27 +0000303 shoulder_angle(), constants::Values::kShoulderRange.lower_hard,
304 constants::Values::kShoulderRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800305 return true;
306 }
307
Austin Schuh3b0f3642016-02-14 21:04:26 -0800308 if (wrist_angle() - shoulder_angle() > constants::Values::kWristRange.upper_hard ||
309 wrist_angle() - shoulder_angle() < constants::Values::kWristRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800310 LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
Comran Morshed225f0b92016-02-10 20:34:27 +0000311 wrist_angle() - shoulder_angle(), constants::Values::kWristRange.lower_hard,
312 constants::Values::kWristRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800313 return true;
314 }
315
316 return false;
317}
318
319void Arm::Update(bool disable) {
320 if (!disable) {
321 // Compute next goal.
322 ::Eigen::Matrix<double, 2, 1> goal_state_shoulder =
323 shoulder_profile_.Update(unprofiled_goal_(0, 0),
324 unprofiled_goal_(1, 0));
325 loop_->mutable_next_R(0, 0) = goal_state_shoulder(0, 0);
326 loop_->mutable_next_R(1, 0) = goal_state_shoulder(1, 0);
327
328 ::Eigen::Matrix<double, 2, 1> goal_state_wrist =
329 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
330 loop_->mutable_next_R(2, 0) = goal_state_wrist(0, 0);
331 loop_->mutable_next_R(3, 0) = goal_state_wrist(1, 0);
332
333 loop_->mutable_next_R(4, 0) = unprofiled_goal_(4, 0);
334 loop_->mutable_next_R(5, 0) = unprofiled_goal_(5, 0);
335 CapGoal("next R", &loop_->mutable_next_R());
336 }
337
338 // Move loop
339 loop_->Update(disable);
340
341 // Shoulder saturated
342 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
343 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
344 }
345
346 // Wrist saturated
347 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
348 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
349 }
350}
351
352void Arm::set_max_voltage(double shoulder_max_voltage,
353 double wrist_max_voltage) {
Brian Silverman2b1957a2016-02-14 20:29:57 -0500354 loop_->set_max_voltages(shoulder_max_voltage, wrist_max_voltage);
Austin Schuh10c2d112016-02-14 13:42:28 -0800355}
356
357void Arm::Reset() {
358 shoulder_estimator_.Reset();
359 wrist_estimator_.Reset();
360 initialized_ = false;
361 shoulder_zeroed_ = false;
362 wrist_zeroed_ = false;
363}
364
365EstimatorState Arm::ShoulderEstimatorState() {
366 EstimatorState estimator_state;
367 ::frc971::zeroing::PopulateEstimatorState(shoulder_estimator_,
368 &estimator_state);
369
370 return estimator_state;
371}
372
373EstimatorState Arm::WristEstimatorState() {
374 EstimatorState estimator_state;
375 ::frc971::zeroing::PopulateEstimatorState(wrist_estimator_, &estimator_state);
376
377 return estimator_state;
378}
379
380} // namespace superstructure
381} // namespace control_loops
382} // namespace y2016