blob: ba7576ad005650f2fe4e75925e57afd01a6c473f [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;
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()
Austin Schuh473a5652017-02-05 01:30:42 -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())),
45 {{constants::GetValues().shoulder.zeroing,
46 constants::GetValues().wrist.zeroing}}),
Austin Schuh10c2d112016-02-14 13:42:28 -080047 shoulder_profile_(::aos::controls::kLoopFrequency),
Austin Schuh473a5652017-02-05 01:30:42 -080048 wrist_profile_(::aos::controls::kLoopFrequency) {
Austin Schuh10c2d112016-02-14 13:42:28 -080049 Y_.setZero();
50 offset_.setZero();
Austin Schuh10c2d112016-02-14 13:42:28 -080051 AdjustProfile(0.0, 0.0, 0.0, 0.0);
52}
53
54void Arm::UpdateWristOffset(double offset) {
55 const double doffset = offset - offset_(1, 0);
56 LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), offset);
57
58 loop_->mutable_X_hat()(2, 0) += doffset;
59 Y_(1, 0) += doffset;
60 loop_->mutable_R(2, 0) += doffset;
61 loop_->mutable_next_R(2, 0) += doffset;
62 unprofiled_goal_(2, 0) += doffset;
63
64 wrist_profile_.MoveGoal(doffset);
65 offset_(1, 0) = offset;
66
67 CapGoal("R", &loop_->mutable_R());
68 CapGoal("unprofiled R", &loop_->mutable_next_R());
69}
70
71void Arm::UpdateShoulderOffset(double offset) {
72 const double doffset = offset - offset_(0, 0);
73 LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), offset);
74
75 loop_->mutable_X_hat()(0, 0) += doffset;
76 loop_->mutable_X_hat()(2, 0) += doffset;
77 Y_(0, 0) += doffset;
78 loop_->mutable_R(0, 0) += doffset;
79 loop_->mutable_R(2, 0) += doffset;
80 loop_->mutable_next_R(0, 0) += doffset;
81 loop_->mutable_next_R(2, 0) += doffset;
82 unprofiled_goal_(0, 0) += doffset;
83 unprofiled_goal_(2, 0) += doffset;
84
85 shoulder_profile_.MoveGoal(doffset);
86 wrist_profile_.MoveGoal(doffset);
87 offset_(0, 0) = offset;
88
89 CapGoal("R", &loop_->mutable_R());
90 CapGoal("unprofiled R", &loop_->mutable_next_R());
91}
92
93// TODO(austin): Handle zeroing errors.
94
95void Arm::Correct(PotAndIndexPosition position_shoulder,
96 PotAndIndexPosition position_wrist) {
Austin Schuh473a5652017-02-05 01:30:42 -080097 estimators_[kShoulderIndex].UpdateEstimate(position_shoulder);
98 estimators_[kWristIndex].UpdateEstimate(position_wrist);
Austin Schuh10c2d112016-02-14 13:42:28 -080099
Diana Vandenberge2843c62016-02-13 17:44:20 -0800100 // Handle zeroing errors
Austin Schuh473a5652017-02-05 01:30:42 -0800101 if (estimators_[kShoulderIndex].error()) {
Diana Vandenberge2843c62016-02-13 17:44:20 -0800102 LOG(ERROR, "zeroing error with shoulder_estimator\n");
103 return;
104 }
Austin Schuh473a5652017-02-05 01:30:42 -0800105 if (estimators_[kWristIndex].error()) {
Diana Vandenberge2843c62016-02-13 17:44:20 -0800106 LOG(ERROR, "zeroing error with wrist_estimator\n");
107 return;
108 }
109
Austin Schuh10c2d112016-02-14 13:42:28 -0800110 if (!initialized_) {
Austin Schuh473a5652017-02-05 01:30:42 -0800111 if (estimators_[kShoulderIndex].offset_ready() &&
112 estimators_[kWristIndex].offset_ready()) {
113 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
114 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuh10c2d112016-02-14 13:42:28 -0800115 initialized_ = true;
116 }
117 }
118
Austin Schuh473a5652017-02-05 01:30:42 -0800119 if (!zeroed(kShoulderIndex) && estimators_[kShoulderIndex].zeroed()) {
120 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800121 set_zeroed(kShoulderIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800122 }
Austin Schuh473a5652017-02-05 01:30:42 -0800123 if (!zeroed(kWristIndex) && estimators_[kWristIndex].zeroed()) {
124 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800125 set_zeroed(kWristIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800126 }
127
128 {
129 Y_ << position_shoulder.encoder, position_wrist.encoder;
130 Y_ += offset_;
131 loop_->Correct(Y_);
132 }
133}
134
135void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) {
136 // Limit the goals to min/max allowable angles.
Austin Schuh10c2d112016-02-14 13:42:28 -0800137
Austin Schuh3b0f3642016-02-14 21:04:26 -0800138 if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800139 LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000140 constants::Values::kShoulderRange.upper);
141 (*goal)(0, 0) = constants::Values::kShoulderRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -0800142 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800143 if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800144 LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Comran Morshed225f0b92016-02-10 20:34:27 +0000145 constants::Values::kShoulderRange.lower);
146 (*goal)(0, 0) = constants::Values::kShoulderRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -0800147 }
148
149 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
150
Austin Schuh3b0f3642016-02-14 21:04:26 -0800151 if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800152 LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000153 wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper);
154 (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800155 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800156 if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800157 LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
Comran Morshed225f0b92016-02-10 20:34:27 +0000158 wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower);
159 (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800160 }
161}
162
163void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
164 set_unprofiled_goal(goal_shoulder, goal_wrist);
165 loop_->mutable_R() = unprofiled_goal_;
166 loop_->mutable_next_R() = loop_->R();
167
168 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
169 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
170}
171
172void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
173 double unprofiled_goal_wrist) {
174 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
175 0.0, 0.0;
176 CapGoal("unprofiled R", &unprofiled_goal_);
177}
178
179void Arm::AdjustProfile(double max_angular_velocity_shoulder,
180 double max_angular_acceleration_shoulder,
181 double max_angular_velocity_wrist,
182 double max_angular_acceleration_wrist) {
183 shoulder_profile_.set_maximum_velocity(
184 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
185 shoulder_profile_.set_maximum_acceleration(
186 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
187 wrist_profile_.set_maximum_velocity(
188 UseUnlessZero(max_angular_velocity_wrist, 10.0));
189 wrist_profile_.set_maximum_acceleration(
190 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
191}
192
193bool Arm::CheckHardLimits() {
Austin Schuh3b0f3642016-02-14 21:04:26 -0800194 if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard ||
195 shoulder_angle() < constants::Values::kShoulderRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800196 LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
Comran Morshed225f0b92016-02-10 20:34:27 +0000197 shoulder_angle(), constants::Values::kShoulderRange.lower_hard,
198 constants::Values::kShoulderRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800199 return true;
200 }
201
Austin Schuhb1d682b2016-02-16 13:07:44 -0800202 if (wrist_angle() - shoulder_angle() >
203 constants::Values::kWristRange.upper_hard ||
204 wrist_angle() - shoulder_angle() <
205 constants::Values::kWristRange.lower_hard) {
Austin Schuh10c2d112016-02-14 13:42:28 -0800206 LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuhb1d682b2016-02-16 13:07:44 -0800207 wrist_angle() - shoulder_angle(),
208 constants::Values::kWristRange.lower_hard,
Comran Morshed225f0b92016-02-10 20:34:27 +0000209 constants::Values::kWristRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800210 return true;
211 }
212
213 return false;
214}
215
216void Arm::Update(bool disable) {
217 if (!disable) {
218 // Compute next goal.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800219 loop_->mutable_next_R().block<2, 1>(0, 0) = shoulder_profile_.Update(
220 unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800221
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800222 loop_->mutable_next_R().block<2, 1>(2, 0) =
Austin Schuh10c2d112016-02-14 13:42:28 -0800223 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800224
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800225 loop_->mutable_next_R().block<2, 1>(4, 0) =
226 unprofiled_goal_.block<2, 1>(4, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800227 CapGoal("next R", &loop_->mutable_next_R());
228 }
229
230 // Move loop
231 loop_->Update(disable);
232
233 // Shoulder saturated
234 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
Austin Schuhd7e29942016-03-12 21:35:11 -0800235 LOG(DEBUG, "Moving shoulder state. U: %f, %f\n", loop_->U(0, 0),
236 loop_->U_uncapped(0, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800237 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
238 }
239
240 // Wrist saturated
241 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
Austin Schuhd7e29942016-03-12 21:35:11 -0800242 LOG(DEBUG, "Moving shooter state. U: %f, %f\n", loop_->U(1, 0),
243 loop_->U_uncapped(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800244 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
245 }
246}
247
Austin Schuh10c2d112016-02-14 13:42:28 -0800248} // namespace superstructure
249} // namespace control_loops
250} // namespace y2016