blob: b8f3a48aa5455381f81694e56414b768150d0cda [file] [log] [blame]
Austin Schuh10c2d112016-02-14 13:42:28 -08001#include "y2016/control_loops/superstructure/superstructure_controls.h"
2
John Park33858a32018-09-28 23:05:48 -07003#include "aos/controls/control_loops.q.h"
4#include "aos/logging/logging.h"
Austin Schuh10c2d112016-02-14 13:42:28 -08005
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;
Austin Schuh10c2d112016-02-14 13:42:28 -080016
17namespace {
18double UseUnlessZero(double target_value, double default_value) {
19 if (target_value != 0.0) {
20 return target_value;
21 } else {
22 return default_value;
23 }
24}
Austin Schuhcb60e292017-01-01 16:07:31 -080025
26enum ArmIndices { kShoulderIndex = 0, kWristIndex = 1 };
27
Austin Schuh10c2d112016-02-14 13:42:28 -080028} // namespace
29
Austin Schuh10c2d112016-02-14 13:42:28 -080030// Intake
31Intake::Intake()
Brian Silvermanab0b6772017-02-05 16:16:21 -080032 : ::frc971::control_loops::SingleDOFProfiledSubsystem<>(
Austin Schuhcb60e292017-01-01 16:07:31 -080033 ::std::unique_ptr<
34 ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>>(
35 new ::frc971::control_loops::SimpleCappedStateFeedbackLoop<
36 3, 1, 1>(::y2016::control_loops::superstructure::
Austin Schuh473a5652017-02-05 01:30:42 -080037 MakeIntegralIntakeLoop())),
38 constants::GetValues().intake.zeroing,
39 constants::Values::kIntakeRange, 10.0, 10.0) {}
Austin Schuh10c2d112016-02-14 13:42:28 -080040
41Arm::Arm()
Austin Schuh473a5652017-02-05 01:30:42 -080042 : ProfiledSubsystem(
43 ::std::unique_ptr<ArmControlLoop>(new ArmControlLoop(
44 ::y2016::control_loops::superstructure::MakeIntegralArmLoop())),
Campbell Crowley36e93e92017-12-23 14:21:43 -080045 {{::frc971::zeroing::PotAndIndexPulseZeroingEstimator(
46 constants::GetValues().shoulder.zeroing),
47 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator(
48 constants::GetValues().wrist.zeroing)}}),
Austin Schuh10c2d112016-02-14 13:42:28 -080049 shoulder_profile_(::aos::controls::kLoopFrequency),
Austin Schuh473a5652017-02-05 01:30:42 -080050 wrist_profile_(::aos::controls::kLoopFrequency) {
Austin Schuh10c2d112016-02-14 13:42:28 -080051 Y_.setZero();
52 offset_.setZero();
Austin Schuh10c2d112016-02-14 13:42:28 -080053 AdjustProfile(0.0, 0.0, 0.0, 0.0);
54}
55
56void Arm::UpdateWristOffset(double offset) {
57 const double doffset = offset - offset_(1, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -070058 AOS_LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0),
59 offset);
Austin Schuh10c2d112016-02-14 13:42:28 -080060
61 loop_->mutable_X_hat()(2, 0) += doffset;
62 Y_(1, 0) += doffset;
63 loop_->mutable_R(2, 0) += doffset;
64 loop_->mutable_next_R(2, 0) += doffset;
65 unprofiled_goal_(2, 0) += doffset;
66
67 wrist_profile_.MoveGoal(doffset);
68 offset_(1, 0) = offset;
69
70 CapGoal("R", &loop_->mutable_R());
71 CapGoal("unprofiled R", &loop_->mutable_next_R());
72}
73
74void Arm::UpdateShoulderOffset(double offset) {
75 const double doffset = offset - offset_(0, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -070076 AOS_LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0),
77 offset);
Austin Schuh10c2d112016-02-14 13:42:28 -080078
79 loop_->mutable_X_hat()(0, 0) += doffset;
80 loop_->mutable_X_hat()(2, 0) += doffset;
81 Y_(0, 0) += doffset;
82 loop_->mutable_R(0, 0) += doffset;
83 loop_->mutable_R(2, 0) += doffset;
84 loop_->mutable_next_R(0, 0) += doffset;
85 loop_->mutable_next_R(2, 0) += doffset;
86 unprofiled_goal_(0, 0) += doffset;
87 unprofiled_goal_(2, 0) += doffset;
88
89 shoulder_profile_.MoveGoal(doffset);
90 wrist_profile_.MoveGoal(doffset);
91 offset_(0, 0) = offset;
92
93 CapGoal("R", &loop_->mutable_R());
94 CapGoal("unprofiled R", &loop_->mutable_next_R());
95}
96
97// TODO(austin): Handle zeroing errors.
98
99void Arm::Correct(PotAndIndexPosition position_shoulder,
100 PotAndIndexPosition position_wrist) {
Austin Schuh473a5652017-02-05 01:30:42 -0800101 estimators_[kShoulderIndex].UpdateEstimate(position_shoulder);
102 estimators_[kWristIndex].UpdateEstimate(position_wrist);
Austin Schuh10c2d112016-02-14 13:42:28 -0800103
Diana Vandenberge2843c62016-02-13 17:44:20 -0800104 // Handle zeroing errors
Austin Schuh473a5652017-02-05 01:30:42 -0800105 if (estimators_[kShoulderIndex].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700106 AOS_LOG(ERROR, "zeroing error with shoulder_estimator\n");
Diana Vandenberge2843c62016-02-13 17:44:20 -0800107 return;
108 }
Austin Schuh473a5652017-02-05 01:30:42 -0800109 if (estimators_[kWristIndex].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700110 AOS_LOG(ERROR, "zeroing error with wrist_estimator\n");
Diana Vandenberge2843c62016-02-13 17:44:20 -0800111 return;
112 }
113
Austin Schuh10c2d112016-02-14 13:42:28 -0800114 if (!initialized_) {
Austin Schuh473a5652017-02-05 01:30:42 -0800115 if (estimators_[kShoulderIndex].offset_ready() &&
116 estimators_[kWristIndex].offset_ready()) {
117 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
118 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuh10c2d112016-02-14 13:42:28 -0800119 initialized_ = true;
120 }
121 }
122
Austin Schuh473a5652017-02-05 01:30:42 -0800123 if (!zeroed(kShoulderIndex) && estimators_[kShoulderIndex].zeroed()) {
124 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800125 set_zeroed(kShoulderIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800126 }
Austin Schuh473a5652017-02-05 01:30:42 -0800127 if (!zeroed(kWristIndex) && estimators_[kWristIndex].zeroed()) {
128 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800129 set_zeroed(kWristIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800130 }
131
132 {
133 Y_ << position_shoulder.encoder, position_wrist.encoder;
134 Y_ += offset_;
135 loop_->Correct(Y_);
136 }
137}
138
139void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) {
140 // Limit the goals to min/max allowable angles.
Austin Schuh10c2d112016-02-14 13:42:28 -0800141
Austin Schuh3b0f3642016-02-14 21:04:26 -0800142 if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700143 AOS_LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name,
144 (*goal)(0, 0), constants::Values::kShoulderRange.upper);
Comran Morshed225f0b92016-02-10 20:34:27 +0000145 (*goal)(0, 0) = constants::Values::kShoulderRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -0800146 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800147 if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700148 AOS_LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name,
149 (*goal)(0, 0), constants::Values::kShoulderRange.lower);
Comran Morshed225f0b92016-02-10 20:34:27 +0000150 (*goal)(0, 0) = constants::Values::kShoulderRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -0800151 }
152
153 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
154
Austin Schuh3b0f3642016-02-14 21:04:26 -0800155 if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700156 AOS_LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
157 wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper);
Comran Morshed225f0b92016-02-10 20:34:27 +0000158 (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800159 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800160 if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700161 AOS_LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
162 wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower);
Comran Morshed225f0b92016-02-10 20:34:27 +0000163 (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800164 }
165}
166
167void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
168 set_unprofiled_goal(goal_shoulder, goal_wrist);
169 loop_->mutable_R() = unprofiled_goal_;
170 loop_->mutable_next_R() = loop_->R();
171
172 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
173 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
174}
175
176void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
177 double unprofiled_goal_wrist) {
178 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
179 0.0, 0.0;
180 CapGoal("unprofiled R", &unprofiled_goal_);
181}
182
183void Arm::AdjustProfile(double max_angular_velocity_shoulder,
184 double max_angular_acceleration_shoulder,
185 double max_angular_velocity_wrist,
186 double max_angular_acceleration_wrist) {
187 shoulder_profile_.set_maximum_velocity(
188 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
189 shoulder_profile_.set_maximum_acceleration(
190 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
191 wrist_profile_.set_maximum_velocity(
192 UseUnlessZero(max_angular_velocity_wrist, 10.0));
193 wrist_profile_.set_maximum_acceleration(
194 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
195}
196
197bool Arm::CheckHardLimits() {
Austin Schuh3b0f3642016-02-14 21:04:26 -0800198 if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard ||
199 shoulder_angle() < constants::Values::kShoulderRange.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700200 AOS_LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
201 shoulder_angle(), constants::Values::kShoulderRange.lower_hard,
202 constants::Values::kShoulderRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800203 return true;
204 }
205
Austin Schuhb1d682b2016-02-16 13:07:44 -0800206 if (wrist_angle() - shoulder_angle() >
207 constants::Values::kWristRange.upper_hard ||
208 wrist_angle() - shoulder_angle() <
209 constants::Values::kWristRange.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700210 AOS_LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
211 wrist_angle() - shoulder_angle(),
212 constants::Values::kWristRange.lower_hard,
213 constants::Values::kWristRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800214 return true;
215 }
216
217 return false;
218}
219
220void Arm::Update(bool disable) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800221 // TODO(austin): Reset the state vectors and internal state here.
222 if (should_reset_) {
223 should_reset_ = false;
224 }
225
Austin Schuh10c2d112016-02-14 13:42:28 -0800226 if (!disable) {
227 // Compute next goal.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800228 loop_->mutable_next_R().block<2, 1>(0, 0) = shoulder_profile_.Update(
229 unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800230
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800231 loop_->mutable_next_R().block<2, 1>(2, 0) =
Austin Schuh10c2d112016-02-14 13:42:28 -0800232 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800233
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800234 loop_->mutable_next_R().block<2, 1>(4, 0) =
235 unprofiled_goal_.block<2, 1>(4, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800236 CapGoal("next R", &loop_->mutable_next_R());
237 }
238
239 // Move loop
240 loop_->Update(disable);
241
242 // Shoulder saturated
243 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700244 AOS_LOG(DEBUG, "Moving shoulder state. U: %f, %f\n", loop_->U(0, 0),
245 loop_->U_uncapped(0, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800246 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
247 }
248
249 // Wrist saturated
250 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700251 AOS_LOG(DEBUG, "Moving shooter state. U: %f, %f\n", loop_->U(1, 0),
252 loop_->U_uncapped(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800253 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
254 }
255}
256
Austin Schuh10c2d112016-02-14 13:42:28 -0800257} // namespace superstructure
258} // namespace control_loops
259} // namespace y2016