blob: ee2020ebc9503369486e9f12a1b8b83bc91b458e [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");
Austin Schuh2669ece2022-03-11 18:30:57 -0800104 X_hat_ = loop_->X_hat();
Diana Vandenberge2843c62016-02-13 17:44:20 -0800105 return;
106 }
Austin Schuh473a5652017-02-05 01:30:42 -0800107 if (estimators_[kWristIndex].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700108 AOS_LOG(ERROR, "zeroing error with wrist_estimator\n");
Austin Schuh2669ece2022-03-11 18:30:57 -0800109 X_hat_ = loop_->X_hat();
Diana Vandenberge2843c62016-02-13 17:44:20 -0800110 return;
111 }
112
Austin Schuh10c2d112016-02-14 13:42:28 -0800113 if (!initialized_) {
Austin Schuh473a5652017-02-05 01:30:42 -0800114 if (estimators_[kShoulderIndex].offset_ready() &&
115 estimators_[kWristIndex].offset_ready()) {
116 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
117 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuh10c2d112016-02-14 13:42:28 -0800118 initialized_ = true;
119 }
120 }
121
Austin Schuh473a5652017-02-05 01:30:42 -0800122 if (!zeroed(kShoulderIndex) && estimators_[kShoulderIndex].zeroed()) {
123 UpdateShoulderOffset(estimators_[kShoulderIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800124 set_zeroed(kShoulderIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800125 }
Austin Schuh473a5652017-02-05 01:30:42 -0800126 if (!zeroed(kWristIndex) && estimators_[kWristIndex].zeroed()) {
127 UpdateWristOffset(estimators_[kWristIndex].offset());
Austin Schuhcb60e292017-01-01 16:07:31 -0800128 set_zeroed(kWristIndex, true);
Austin Schuh10c2d112016-02-14 13:42:28 -0800129 }
130
131 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132 Y_ << position_shoulder->encoder(), position_wrist->encoder();
Austin Schuh10c2d112016-02-14 13:42:28 -0800133 Y_ += offset_;
134 loop_->Correct(Y_);
Austin Schuh2669ece2022-03-11 18:30:57 -0800135 X_hat_ = loop_->X_hat();
Austin Schuh10c2d112016-02-14 13:42:28 -0800136 }
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