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 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 8 | namespace y2016::control_loops::superstructure { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 9 | |
| 10 | using ::frc971::PotAndIndexPosition; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 11 | |
| 12 | namespace { |
| 13 | double UseUnlessZero(double target_value, double default_value) { |
| 14 | if (target_value != 0.0) { |
| 15 | return target_value; |
| 16 | } else { |
| 17 | return default_value; |
| 18 | } |
| 19 | } |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame] | 20 | |
| 21 | enum ArmIndices { kShoulderIndex = 0, kWristIndex = 1 }; |
| 22 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 23 | } // namespace |
| 24 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 25 | // Intake |
| 26 | Intake::Intake() |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 27 | : ::frc971::control_loops::SingleDOFProfiledSubsystem<>( |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame] | 28 | ::std::unique_ptr< |
| 29 | ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>>( |
| 30 | new ::frc971::control_loops::SimpleCappedStateFeedbackLoop< |
| 31 | 3, 1, 1>(::y2016::control_loops::superstructure:: |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 32 | MakeIntegralIntakeLoop())), |
| 33 | constants::GetValues().intake.zeroing, |
| 34 | constants::Values::kIntakeRange, 10.0, 10.0) {} |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 35 | |
| 36 | Arm::Arm() |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 37 | : ProfiledSubsystem( |
| 38 | ::std::unique_ptr<ArmControlLoop>(new ArmControlLoop( |
| 39 | ::y2016::control_loops::superstructure::MakeIntegralArmLoop())), |
Campbell Crowley | 36e93e9 | 2017-12-23 14:21:43 -0800 | [diff] [blame] | 40 | {{::frc971::zeroing::PotAndIndexPulseZeroingEstimator( |
| 41 | constants::GetValues().shoulder.zeroing), |
| 42 | ::frc971::zeroing::PotAndIndexPulseZeroingEstimator( |
| 43 | constants::GetValues().wrist.zeroing)}}), |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 44 | shoulder_profile_(::frc971::controls::kLoopFrequency), |
| 45 | wrist_profile_(::frc971::controls::kLoopFrequency) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 46 | Y_.setZero(); |
| 47 | offset_.setZero(); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 48 | AdjustProfile(0.0, 0.0, 0.0, 0.0); |
| 49 | } |
| 50 | |
| 51 | void Arm::UpdateWristOffset(double offset) { |
| 52 | const double doffset = offset - offset_(1, 0); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 53 | AOS_LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), |
| 54 | offset); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 55 | |
| 56 | loop_->mutable_X_hat()(2, 0) += doffset; |
| 57 | Y_(1, 0) += doffset; |
| 58 | loop_->mutable_R(2, 0) += doffset; |
| 59 | loop_->mutable_next_R(2, 0) += doffset; |
| 60 | unprofiled_goal_(2, 0) += doffset; |
| 61 | |
| 62 | wrist_profile_.MoveGoal(doffset); |
| 63 | offset_(1, 0) = offset; |
| 64 | |
| 65 | CapGoal("R", &loop_->mutable_R()); |
| 66 | CapGoal("unprofiled R", &loop_->mutable_next_R()); |
| 67 | } |
| 68 | |
| 69 | void Arm::UpdateShoulderOffset(double offset) { |
| 70 | const double doffset = offset - offset_(0, 0); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 71 | AOS_LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), |
| 72 | offset); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 73 | |
| 74 | loop_->mutable_X_hat()(0, 0) += doffset; |
| 75 | loop_->mutable_X_hat()(2, 0) += doffset; |
| 76 | Y_(0, 0) += doffset; |
| 77 | loop_->mutable_R(0, 0) += doffset; |
| 78 | loop_->mutable_R(2, 0) += doffset; |
| 79 | loop_->mutable_next_R(0, 0) += doffset; |
| 80 | loop_->mutable_next_R(2, 0) += doffset; |
| 81 | unprofiled_goal_(0, 0) += doffset; |
| 82 | unprofiled_goal_(2, 0) += doffset; |
| 83 | |
| 84 | shoulder_profile_.MoveGoal(doffset); |
| 85 | wrist_profile_.MoveGoal(doffset); |
| 86 | offset_(0, 0) = offset; |
| 87 | |
| 88 | CapGoal("R", &loop_->mutable_R()); |
| 89 | CapGoal("unprofiled R", &loop_->mutable_next_R()); |
| 90 | } |
| 91 | |
| 92 | // TODO(austin): Handle zeroing errors. |
| 93 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 94 | void Arm::Correct(const PotAndIndexPosition *position_shoulder, |
| 95 | const PotAndIndexPosition *position_wrist) { |
| 96 | estimators_[kShoulderIndex].UpdateEstimate(*position_shoulder); |
| 97 | estimators_[kWristIndex].UpdateEstimate(*position_wrist); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 98 | |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 99 | // Handle zeroing errors |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 100 | if (estimators_[kShoulderIndex].error()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 101 | AOS_LOG(ERROR, "zeroing error with shoulder_estimator\n"); |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 102 | X_hat_ = loop_->X_hat(); |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 103 | return; |
| 104 | } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 105 | if (estimators_[kWristIndex].error()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 106 | AOS_LOG(ERROR, "zeroing error with wrist_estimator\n"); |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 107 | X_hat_ = loop_->X_hat(); |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 111 | if (!initialized_) { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 112 | if (estimators_[kShoulderIndex].offset_ready() && |
| 113 | estimators_[kWristIndex].offset_ready()) { |
| 114 | UpdateShoulderOffset(estimators_[kShoulderIndex].offset()); |
| 115 | UpdateWristOffset(estimators_[kWristIndex].offset()); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 116 | initialized_ = true; |
| 117 | } |
| 118 | } |
| 119 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 120 | if (!zeroed(kShoulderIndex) && estimators_[kShoulderIndex].zeroed()) { |
| 121 | UpdateShoulderOffset(estimators_[kShoulderIndex].offset()); |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame] | 122 | set_zeroed(kShoulderIndex, true); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 123 | } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 124 | if (!zeroed(kWristIndex) && estimators_[kWristIndex].zeroed()) { |
| 125 | UpdateWristOffset(estimators_[kWristIndex].offset()); |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame] | 126 | set_zeroed(kWristIndex, true); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 130 | Y_ << position_shoulder->encoder(), position_wrist->encoder(); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 131 | Y_ += offset_; |
| 132 | loop_->Correct(Y_); |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 133 | X_hat_ = loop_->X_hat(); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) { |
| 138 | // Limit the goals to min/max allowable angles. |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 139 | |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 140 | if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 141 | AOS_LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, |
| 142 | (*goal)(0, 0), constants::Values::kShoulderRange.upper); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 143 | (*goal)(0, 0) = constants::Values::kShoulderRange.upper; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 144 | } |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 145 | if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 146 | AOS_LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, |
| 147 | (*goal)(0, 0), constants::Values::kShoulderRange.lower); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 148 | (*goal)(0, 0) = constants::Values::kShoulderRange.lower; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0); |
| 152 | |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 153 | if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 154 | AOS_LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name, |
| 155 | wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 156 | (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 157 | } |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 158 | if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 159 | AOS_LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name, |
| 160 | wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower); |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 161 | (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | void Arm::ForceGoal(double goal_shoulder, double goal_wrist) { |
| 166 | set_unprofiled_goal(goal_shoulder, goal_wrist); |
| 167 | loop_->mutable_R() = unprofiled_goal_; |
| 168 | loop_->mutable_next_R() = loop_->R(); |
| 169 | |
| 170 | shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0)); |
| 171 | wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0)); |
| 172 | } |
| 173 | |
| 174 | void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder, |
| 175 | double unprofiled_goal_wrist) { |
| 176 | unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0, |
| 177 | 0.0, 0.0; |
| 178 | CapGoal("unprofiled R", &unprofiled_goal_); |
| 179 | } |
| 180 | |
| 181 | void Arm::AdjustProfile(double max_angular_velocity_shoulder, |
| 182 | double max_angular_acceleration_shoulder, |
| 183 | double max_angular_velocity_wrist, |
| 184 | double max_angular_acceleration_wrist) { |
| 185 | shoulder_profile_.set_maximum_velocity( |
| 186 | UseUnlessZero(max_angular_velocity_shoulder, 10.0)); |
| 187 | shoulder_profile_.set_maximum_acceleration( |
| 188 | UseUnlessZero(max_angular_acceleration_shoulder, 10.0)); |
| 189 | wrist_profile_.set_maximum_velocity( |
| 190 | UseUnlessZero(max_angular_velocity_wrist, 10.0)); |
| 191 | wrist_profile_.set_maximum_acceleration( |
| 192 | UseUnlessZero(max_angular_acceleration_wrist, 10.0)); |
| 193 | } |
| 194 | |
| 195 | bool Arm::CheckHardLimits() { |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 196 | if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard || |
| 197 | shoulder_angle() < constants::Values::kShoulderRange.lower_hard) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 198 | AOS_LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n", |
| 199 | shoulder_angle(), constants::Values::kShoulderRange.lower_hard, |
| 200 | constants::Values::kShoulderRange.upper_hard); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | |
Austin Schuh | b1d682b | 2016-02-16 13:07:44 -0800 | [diff] [blame] | 204 | if (wrist_angle() - shoulder_angle() > |
| 205 | constants::Values::kWristRange.upper_hard || |
| 206 | wrist_angle() - shoulder_angle() < |
| 207 | constants::Values::kWristRange.lower_hard) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 208 | AOS_LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n", |
| 209 | wrist_angle() - shoulder_angle(), |
| 210 | constants::Values::kWristRange.lower_hard, |
| 211 | constants::Values::kWristRange.upper_hard); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 212 | return true; |
| 213 | } |
| 214 | |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | void Arm::Update(bool disable) { |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 219 | // TODO(austin): Reset the state vectors and internal state here. |
| 220 | if (should_reset_) { |
| 221 | should_reset_ = false; |
| 222 | } |
| 223 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 224 | if (!disable) { |
| 225 | // Compute next goal. |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 226 | loop_->mutable_next_R().block<2, 1>(0, 0) = shoulder_profile_.Update( |
| 227 | unprofiled_goal_(0, 0), unprofiled_goal_(1, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 228 | |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 229 | loop_->mutable_next_R().block<2, 1>(2, 0) = |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 230 | wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 231 | |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 232 | loop_->mutable_next_R().block<2, 1>(4, 0) = |
| 233 | unprofiled_goal_.block<2, 1>(4, 0); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 234 | CapGoal("next R", &loop_->mutable_next_R()); |
| 235 | } |
| 236 | |
| 237 | // Move loop |
| 238 | loop_->Update(disable); |
| 239 | |
| 240 | // Shoulder saturated |
| 241 | if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 242 | AOS_LOG(DEBUG, "Moving shoulder state. U: %f, %f\n", loop_->U(0, 0), |
| 243 | loop_->U_uncapped(0, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 244 | shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0)); |
| 245 | } |
| 246 | |
| 247 | // Wrist saturated |
| 248 | if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 249 | AOS_LOG(DEBUG, "Moving shooter state. U: %f, %f\n", loop_->U(1, 0), |
| 250 | loop_->U_uncapped(1, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 251 | wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0)); |
| 252 | } |
| 253 | } |
| 254 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 255 | } // namespace y2016::control_loops::superstructure |