Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 1 | #include "y2016/control_loops/superstructure/superstructure_controls.h" |
| 2 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 3 | #include "aos/logging/logging.h" |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 4 | #include "y2016/constants.h" |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 5 | #include "y2016/control_loops/superstructure/integral_arm_plant.h" |
| 6 | #include "y2016/control_loops/superstructure/integral_intake_plant.h" |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 7 | |
| 8 | namespace y2016 { |
| 9 | namespace control_loops { |
| 10 | namespace superstructure { |
| 11 | |
| 12 | using ::frc971::PotAndIndexPosition; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 13 | |
| 14 | namespace { |
| 15 | double 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 Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame] | 22 | |
| 23 | enum ArmIndices { kShoulderIndex = 0, kWristIndex = 1 }; |
| 24 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 25 | } // namespace |
| 26 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 27 | // Intake |
| 28 | Intake::Intake() |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 29 | : ::frc971::control_loops::SingleDOFProfiledSubsystem<>( |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame] | 30 | ::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 Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 34 | MakeIntegralIntakeLoop())), |
| 35 | constants::GetValues().intake.zeroing, |
| 36 | constants::Values::kIntakeRange, 10.0, 10.0) {} |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 37 | |
| 38 | Arm::Arm() |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 39 | : ProfiledSubsystem( |
| 40 | ::std::unique_ptr<ArmControlLoop>(new ArmControlLoop( |
| 41 | ::y2016::control_loops::superstructure::MakeIntegralArmLoop())), |
Campbell Crowley | 36e93e9 | 2017-12-23 14:21:43 -0800 | [diff] [blame] | 42 | {{::frc971::zeroing::PotAndIndexPulseZeroingEstimator( |
| 43 | constants::GetValues().shoulder.zeroing), |
| 44 | ::frc971::zeroing::PotAndIndexPulseZeroingEstimator( |
| 45 | constants::GetValues().wrist.zeroing)}}), |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 46 | shoulder_profile_(::frc971::controls::kLoopFrequency), |
| 47 | wrist_profile_(::frc971::controls::kLoopFrequency) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 48 | Y_.setZero(); |
| 49 | offset_.setZero(); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 50 | AdjustProfile(0.0, 0.0, 0.0, 0.0); |
| 51 | } |
| 52 | |
| 53 | void Arm::UpdateWristOffset(double offset) { |
| 54 | const double doffset = offset - offset_(1, 0); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 55 | AOS_LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), |
| 56 | offset); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 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 | |
| 71 | void Arm::UpdateShoulderOffset(double offset) { |
| 72 | const double doffset = offset - offset_(0, 0); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 73 | AOS_LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), |
| 74 | offset); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 75 | |
| 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 96 | void Arm::Correct(const PotAndIndexPosition *position_shoulder, |
| 97 | const PotAndIndexPosition *position_wrist) { |
| 98 | estimators_[kShoulderIndex].UpdateEstimate(*position_shoulder); |
| 99 | estimators_[kWristIndex].UpdateEstimate(*position_wrist); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 100 | |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 101 | // Handle zeroing errors |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 102 | if (estimators_[kShoulderIndex].error()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 103 | AOS_LOG(ERROR, "zeroing error with shoulder_estimator\n"); |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 104 | X_hat_ = loop_->X_hat(); |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 105 | return; |
| 106 | } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 107 | if (estimators_[kWristIndex].error()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 108 | AOS_LOG(ERROR, "zeroing error with wrist_estimator\n"); |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 109 | X_hat_ = loop_->X_hat(); |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 113 | if (!initialized_) { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 114 | if (estimators_[kShoulderIndex].offset_ready() && |
| 115 | estimators_[kWristIndex].offset_ready()) { |
| 116 | UpdateShoulderOffset(estimators_[kShoulderIndex].offset()); |
| 117 | UpdateWristOffset(estimators_[kWristIndex].offset()); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 118 | initialized_ = true; |
| 119 | } |
| 120 | } |
| 121 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 122 | if (!zeroed(kShoulderIndex) && estimators_[kShoulderIndex].zeroed()) { |
| 123 | UpdateShoulderOffset(estimators_[kShoulderIndex].offset()); |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame] | 124 | set_zeroed(kShoulderIndex, true); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 125 | } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 126 | if (!zeroed(kWristIndex) && estimators_[kWristIndex].zeroed()) { |
| 127 | UpdateWristOffset(estimators_[kWristIndex].offset()); |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame] | 128 | set_zeroed(kWristIndex, true); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 132 | Y_ << position_shoulder->encoder(), position_wrist->encoder(); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 133 | Y_ += offset_; |
| 134 | loop_->Correct(Y_); |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 135 | X_hat_ = loop_->X_hat(); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) { |
| 140 | // Limit the goals to min/max allowable angles. |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 141 | |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 142 | if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 143 | AOS_LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, |
| 144 | (*goal)(0, 0), constants::Values::kShoulderRange.upper); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 145 | (*goal)(0, 0) = constants::Values::kShoulderRange.upper; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 146 | } |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 147 | if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 148 | AOS_LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, |
| 149 | (*goal)(0, 0), constants::Values::kShoulderRange.lower); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 150 | (*goal)(0, 0) = constants::Values::kShoulderRange.lower; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0); |
| 154 | |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 155 | if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 156 | AOS_LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name, |
| 157 | wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 158 | (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 159 | } |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 160 | if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 161 | AOS_LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name, |
| 162 | wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 163 | (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | void 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 | |
| 176 | void 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 | |
| 183 | void 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 | |
| 197 | bool Arm::CheckHardLimits() { |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 198 | if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard || |
| 199 | shoulder_angle() < constants::Values::kShoulderRange.lower_hard) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 200 | 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 Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 203 | return true; |
| 204 | } |
| 205 | |
Austin Schuh | b1d682b | 2016-02-16 13:07:44 -0800 | [diff] [blame] | 206 | if (wrist_angle() - shoulder_angle() > |
| 207 | constants::Values::kWristRange.upper_hard || |
| 208 | wrist_angle() - shoulder_angle() < |
| 209 | constants::Values::kWristRange.lower_hard) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 210 | 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 Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 214 | return true; |
| 215 | } |
| 216 | |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | void Arm::Update(bool disable) { |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 221 | // TODO(austin): Reset the state vectors and internal state here. |
| 222 | if (should_reset_) { |
| 223 | should_reset_ = false; |
| 224 | } |
| 225 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 226 | if (!disable) { |
| 227 | // Compute next goal. |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 228 | loop_->mutable_next_R().block<2, 1>(0, 0) = shoulder_profile_.Update( |
| 229 | unprofiled_goal_(0, 0), unprofiled_goal_(1, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 230 | |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 231 | loop_->mutable_next_R().block<2, 1>(2, 0) = |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 232 | wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 233 | |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 234 | loop_->mutable_next_R().block<2, 1>(4, 0) = |
| 235 | unprofiled_goal_.block<2, 1>(4, 0); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 236 | 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 244 | AOS_LOG(DEBUG, "Moving shoulder state. U: %f, %f\n", loop_->U(0, 0), |
| 245 | loop_->U_uncapped(0, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 246 | 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 251 | AOS_LOG(DEBUG, "Moving shooter state. U: %f, %f\n", loop_->U(1, 0), |
| 252 | loop_->U_uncapped(1, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 253 | wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0)); |
| 254 | } |
| 255 | } |
| 256 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 257 | } // namespace superstructure |
| 258 | } // namespace control_loops |
| 259 | } // namespace y2016 |