blob: d16c73ca5b313ebbb57bae295a979f8bb8f72ba6 [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/logging/logging.h"
Austin Schuh10c2d112016-02-14 13:42:28 -08004#include "y2016/constants.h"
James Kuszmaul61750662021-06-21 21:32:33 -07005#include "y2016/control_loops/superstructure/integral_arm_plant.h"
6#include "y2016/control_loops/superstructure/integral_intake_plant.h"
Austin Schuh10c2d112016-02-14 13:42:28 -08007
8namespace y2016 {
9namespace control_loops {
10namespace superstructure {
11
12using ::frc971::PotAndIndexPosition;
Austin Schuh10c2d112016-02-14 13:42:28 -080013
14namespace {
15double UseUnlessZero(double target_value, double default_value) {
16 if (target_value != 0.0) {
17 return target_value;
18 } else {
19 return default_value;
20 }
21}
Austin Schuhcb60e292017-01-01 16:07:31 -080022
23enum ArmIndices { kShoulderIndex = 0, kWristIndex = 1 };
24
Austin Schuh10c2d112016-02-14 13:42:28 -080025} // namespace
26
Austin Schuh10c2d112016-02-14 13:42:28 -080027// Intake
28Intake::Intake()
Brian Silvermanab0b6772017-02-05 16:16:21 -080029 : ::frc971::control_loops::SingleDOFProfiledSubsystem<>(
Austin Schuhcb60e292017-01-01 16:07:31 -080030 ::std::unique_ptr<
31 ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>>(
32 new ::frc971::control_loops::SimpleCappedStateFeedbackLoop<
33 3, 1, 1>(::y2016::control_loops::superstructure::
Austin Schuh473a5652017-02-05 01:30:42 -080034 MakeIntegralIntakeLoop())),
35 constants::GetValues().intake.zeroing,
36 constants::Values::kIntakeRange, 10.0, 10.0) {}
Austin Schuh10c2d112016-02-14 13:42:28 -080037
38Arm::Arm()
Austin Schuh473a5652017-02-05 01:30:42 -080039 : ProfiledSubsystem(
40 ::std::unique_ptr<ArmControlLoop>(new ArmControlLoop(
41 ::y2016::control_loops::superstructure::MakeIntegralArmLoop())),
Campbell Crowley36e93e92017-12-23 14:21:43 -080042 {{::frc971::zeroing::PotAndIndexPulseZeroingEstimator(
43 constants::GetValues().shoulder.zeroing),
44 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator(
45 constants::GetValues().wrist.zeroing)}}),
James Kuszmaul61750662021-06-21 21:32:33 -070046 shoulder_profile_(::frc971::controls::kLoopFrequency),
47 wrist_profile_(::frc971::controls::kLoopFrequency) {
Austin Schuh10c2d112016-02-14 13:42:28 -080048 Y_.setZero();
49 offset_.setZero();
Austin Schuh10c2d112016-02-14 13:42:28 -080050 AdjustProfile(0.0, 0.0, 0.0, 0.0);
51}
52
53void Arm::UpdateWristOffset(double offset) {
54 const double doffset = offset - offset_(1, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -070055 AOS_LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0),
56 offset);
Austin Schuh10c2d112016-02-14 13:42:28 -080057
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);
Austin Schuhf257f3c2019-10-27 21:00:43 -070073 AOS_LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0),
74 offset);
Austin Schuh10c2d112016-02-14 13:42:28 -080075
76 loop_->mutable_X_hat()(0, 0) += doffset;
77 loop_->mutable_X_hat()(2, 0) += doffset;
78 Y_(0, 0) += doffset;
79 loop_->mutable_R(0, 0) += doffset;
80 loop_->mutable_R(2, 0) += doffset;
81 loop_->mutable_next_R(0, 0) += doffset;
82 loop_->mutable_next_R(2, 0) += doffset;
83 unprofiled_goal_(0, 0) += doffset;
84 unprofiled_goal_(2, 0) += doffset;
85
86 shoulder_profile_.MoveGoal(doffset);
87 wrist_profile_.MoveGoal(doffset);
88 offset_(0, 0) = offset;
89
90 CapGoal("R", &loop_->mutable_R());
91 CapGoal("unprofiled R", &loop_->mutable_next_R());
92}
93
94// TODO(austin): Handle zeroing errors.
95
Alex Perrycb7da4b2019-08-28 19:35:56 -070096void Arm::Correct(const PotAndIndexPosition *position_shoulder,
97 const PotAndIndexPosition *position_wrist) {
98 estimators_[kShoulderIndex].UpdateEstimate(*position_shoulder);
99 estimators_[kWristIndex].UpdateEstimate(*position_wrist);
Austin Schuh10c2d112016-02-14 13:42:28 -0800100
Diana Vandenberge2843c62016-02-13 17:44:20 -0800101 // Handle zeroing errors
Austin Schuh473a5652017-02-05 01:30:42 -0800102 if (estimators_[kShoulderIndex].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700103 AOS_LOG(ERROR, "zeroing error with shoulder_estimator\n");
Diana Vandenberge2843c62016-02-13 17:44:20 -0800104 return;
105 }
Austin Schuh473a5652017-02-05 01:30:42 -0800106 if (estimators_[kWristIndex].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700107 AOS_LOG(ERROR, "zeroing error with wrist_estimator\n");
Diana Vandenberge2843c62016-02-13 17:44:20 -0800108 return;
109 }
110
Austin Schuh10c2d112016-02-14 13:42:28 -0800111 if (!initialized_) {
Austin Schuh473a5652017-02-05 01:30:42 -0800112 if (estimators_[kShoulderIndex].offset_ready() &&
113 estimators_[kWristIndex].offset_ready()) {
114 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
115 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuh10c2d112016-02-14 13:42:28 -0800116 initialized_ = true;
117 }
118 }
119
Austin Schuh473a5652017-02-05 01:30:42 -0800120 if (!zeroed(kShoulderIndex) && estimators_[kShoulderIndex].zeroed()) {
121 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800122 set_zeroed(kShoulderIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800123 }
Austin Schuh473a5652017-02-05 01:30:42 -0800124 if (!zeroed(kWristIndex) && estimators_[kWristIndex].zeroed()) {
125 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800126 set_zeroed(kWristIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800127 }
128
129 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130 Y_ << position_shoulder->encoder(), position_wrist->encoder();
Austin Schuh10c2d112016-02-14 13:42:28 -0800131 Y_ += offset_;
132 loop_->Correct(Y_);
133 }
134}
135
136void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) {
137 // Limit the goals to min/max allowable angles.
Austin Schuh10c2d112016-02-14 13:42:28 -0800138
Austin Schuh3b0f3642016-02-14 21:04:26 -0800139 if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700140 AOS_LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name,
141 (*goal)(0, 0), constants::Values::kShoulderRange.upper);
Comran Morshed225f0b92016-02-10 20:34:27 +0000142 (*goal)(0, 0) = constants::Values::kShoulderRange.upper;
Austin Schuh10c2d112016-02-14 13:42:28 -0800143 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800144 if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700145 AOS_LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name,
146 (*goal)(0, 0), constants::Values::kShoulderRange.lower);
Comran Morshed225f0b92016-02-10 20:34:27 +0000147 (*goal)(0, 0) = constants::Values::kShoulderRange.lower;
Austin Schuh10c2d112016-02-14 13:42:28 -0800148 }
149
150 const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0);
151
Austin Schuh3b0f3642016-02-14 21:04:26 -0800152 if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700153 AOS_LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name,
154 wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper);
Comran Morshed225f0b92016-02-10 20:34:27 +0000155 (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800156 }
Austin Schuh3b0f3642016-02-14 21:04:26 -0800157 if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700158 AOS_LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name,
159 wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower);
Comran Morshed225f0b92016-02-10 20:34:27 +0000160 (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800161 }
162}
163
164void Arm::ForceGoal(double goal_shoulder, double goal_wrist) {
165 set_unprofiled_goal(goal_shoulder, goal_wrist);
166 loop_->mutable_R() = unprofiled_goal_;
167 loop_->mutable_next_R() = loop_->R();
168
169 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
170 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
171}
172
173void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder,
174 double unprofiled_goal_wrist) {
175 unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0,
176 0.0, 0.0;
177 CapGoal("unprofiled R", &unprofiled_goal_);
178}
179
180void Arm::AdjustProfile(double max_angular_velocity_shoulder,
181 double max_angular_acceleration_shoulder,
182 double max_angular_velocity_wrist,
183 double max_angular_acceleration_wrist) {
184 shoulder_profile_.set_maximum_velocity(
185 UseUnlessZero(max_angular_velocity_shoulder, 10.0));
186 shoulder_profile_.set_maximum_acceleration(
187 UseUnlessZero(max_angular_acceleration_shoulder, 10.0));
188 wrist_profile_.set_maximum_velocity(
189 UseUnlessZero(max_angular_velocity_wrist, 10.0));
190 wrist_profile_.set_maximum_acceleration(
191 UseUnlessZero(max_angular_acceleration_wrist, 10.0));
192}
193
194bool Arm::CheckHardLimits() {
Austin Schuh3b0f3642016-02-14 21:04:26 -0800195 if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard ||
196 shoulder_angle() < constants::Values::kShoulderRange.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700197 AOS_LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n",
198 shoulder_angle(), constants::Values::kShoulderRange.lower_hard,
199 constants::Values::kShoulderRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800200 return true;
201 }
202
Austin Schuhb1d682b2016-02-16 13:07:44 -0800203 if (wrist_angle() - shoulder_angle() >
204 constants::Values::kWristRange.upper_hard ||
205 wrist_angle() - shoulder_angle() <
206 constants::Values::kWristRange.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700207 AOS_LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n",
208 wrist_angle() - shoulder_angle(),
209 constants::Values::kWristRange.lower_hard,
210 constants::Values::kWristRange.upper_hard);
Austin Schuh10c2d112016-02-14 13:42:28 -0800211 return true;
212 }
213
214 return false;
215}
216
217void Arm::Update(bool disable) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800218 // TODO(austin): Reset the state vectors and internal state here.
219 if (should_reset_) {
220 should_reset_ = false;
221 }
222
Austin Schuh10c2d112016-02-14 13:42:28 -0800223 if (!disable) {
224 // Compute next goal.
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800225 loop_->mutable_next_R().block<2, 1>(0, 0) = shoulder_profile_.Update(
226 unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800227
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800228 loop_->mutable_next_R().block<2, 1>(2, 0) =
Austin Schuh10c2d112016-02-14 13:42:28 -0800229 wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800230
Austin Schuhf59b6bc2016-03-11 21:26:19 -0800231 loop_->mutable_next_R().block<2, 1>(4, 0) =
232 unprofiled_goal_.block<2, 1>(4, 0);
Austin Schuh10c2d112016-02-14 13:42:28 -0800233 CapGoal("next R", &loop_->mutable_next_R());
234 }
235
236 // Move loop
237 loop_->Update(disable);
238
239 // Shoulder saturated
240 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700241 AOS_LOG(DEBUG, "Moving shoulder state. U: %f, %f\n", loop_->U(0, 0),
242 loop_->U_uncapped(0, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800243 shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
244 }
245
246 // Wrist saturated
247 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700248 AOS_LOG(DEBUG, "Moving shooter state. U: %f, %f\n", loop_->U(1, 0),
249 loop_->U_uncapped(1, 0));
Austin Schuh10c2d112016-02-14 13:42:28 -0800250 wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0));
251 }
252}
253
Austin Schuh10c2d112016-02-14 13:42:28 -0800254} // namespace superstructure
255} // namespace control_loops
256} // namespace y2016