Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 1 | #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 | |
| 11 | namespace y2016 { |
| 12 | namespace control_loops { |
| 13 | namespace superstructure { |
| 14 | |
| 15 | using ::frc971::PotAndIndexPosition; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 16 | |
| 17 | namespace { |
| 18 | double 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 Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame] | 25 | |
| 26 | enum ArmIndices { kShoulderIndex = 0, kWristIndex = 1 }; |
| 27 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 28 | } // namespace |
| 29 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 30 | // Intake |
| 31 | Intake::Intake() |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 32 | : ::frc971::control_loops::SingleDOFProfiledSubsystem( |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame] | 33 | ::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 Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 37 | MakeIntegralIntakeLoop())), |
| 38 | constants::GetValues().intake.zeroing, |
| 39 | constants::Values::kIntakeRange, 10.0, 10.0) {} |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 40 | |
| 41 | Arm::Arm() |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 42 | : 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 Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 47 | shoulder_profile_(::aos::controls::kLoopFrequency), |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 48 | wrist_profile_(::aos::controls::kLoopFrequency) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 49 | Y_.setZero(); |
| 50 | offset_.setZero(); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 51 | AdjustProfile(0.0, 0.0, 0.0, 0.0); |
| 52 | } |
| 53 | |
| 54 | void 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 | |
| 71 | void 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 | |
| 95 | void Arm::Correct(PotAndIndexPosition position_shoulder, |
| 96 | PotAndIndexPosition position_wrist) { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 97 | estimators_[kShoulderIndex].UpdateEstimate(position_shoulder); |
| 98 | estimators_[kWristIndex].UpdateEstimate(position_wrist); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 99 | |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 100 | // Handle zeroing errors |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 101 | if (estimators_[kShoulderIndex].error()) { |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 102 | LOG(ERROR, "zeroing error with shoulder_estimator\n"); |
| 103 | return; |
| 104 | } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 105 | if (estimators_[kWristIndex].error()) { |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 106 | LOG(ERROR, "zeroing error with wrist_estimator\n"); |
| 107 | return; |
| 108 | } |
| 109 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 110 | if (!initialized_) { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 111 | if (estimators_[kShoulderIndex].offset_ready() && |
| 112 | estimators_[kWristIndex].offset_ready()) { |
| 113 | UpdateShoulderOffset(estimators_[kShoulderIndex].offset()); |
| 114 | UpdateWristOffset(estimators_[kWristIndex].offset()); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 115 | initialized_ = true; |
| 116 | } |
| 117 | } |
| 118 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 119 | if (!zeroed(kShoulderIndex) && estimators_[kShoulderIndex].zeroed()) { |
| 120 | UpdateShoulderOffset(estimators_[kShoulderIndex].offset()); |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame] | 121 | set_zeroed(kShoulderIndex, true); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 122 | } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 123 | if (!zeroed(kWristIndex) && estimators_[kWristIndex].zeroed()) { |
| 124 | UpdateWristOffset(estimators_[kWristIndex].offset()); |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame] | 125 | set_zeroed(kWristIndex, true); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | { |
| 129 | Y_ << position_shoulder.encoder, position_wrist.encoder; |
| 130 | Y_ += offset_; |
| 131 | loop_->Correct(Y_); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) { |
| 136 | // Limit the goals to min/max allowable angles. |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 137 | |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 138 | if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 139 | LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, (*goal)(0, 0), |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 140 | constants::Values::kShoulderRange.upper); |
| 141 | (*goal)(0, 0) = constants::Values::kShoulderRange.upper; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 142 | } |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 143 | if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 144 | LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, (*goal)(0, 0), |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 145 | constants::Values::kShoulderRange.lower); |
| 146 | (*goal)(0, 0) = constants::Values::kShoulderRange.lower; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0); |
| 150 | |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 151 | if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 152 | LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name, |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 153 | wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper); |
| 154 | (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 155 | } |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 156 | if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 157 | LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name, |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 158 | wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower); |
| 159 | (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | void 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 | |
| 172 | void 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 | |
| 179 | void 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 | |
| 193 | bool Arm::CheckHardLimits() { |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 194 | if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard || |
| 195 | shoulder_angle() < constants::Values::kShoulderRange.lower_hard) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 196 | LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n", |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 197 | shoulder_angle(), constants::Values::kShoulderRange.lower_hard, |
| 198 | constants::Values::kShoulderRange.upper_hard); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 199 | return true; |
| 200 | } |
| 201 | |
Austin Schuh | b1d682b | 2016-02-16 13:07:44 -0800 | [diff] [blame] | 202 | if (wrist_angle() - shoulder_angle() > |
| 203 | constants::Values::kWristRange.upper_hard || |
| 204 | wrist_angle() - shoulder_angle() < |
| 205 | constants::Values::kWristRange.lower_hard) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 206 | LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n", |
Austin Schuh | b1d682b | 2016-02-16 13:07:44 -0800 | [diff] [blame] | 207 | wrist_angle() - shoulder_angle(), |
| 208 | constants::Values::kWristRange.lower_hard, |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 209 | constants::Values::kWristRange.upper_hard); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | void Arm::Update(bool disable) { |
| 217 | if (!disable) { |
| 218 | // Compute next goal. |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 219 | loop_->mutable_next_R().block<2, 1>(0, 0) = shoulder_profile_.Update( |
| 220 | unprofiled_goal_(0, 0), unprofiled_goal_(1, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 221 | |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 222 | loop_->mutable_next_R().block<2, 1>(2, 0) = |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 223 | wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 224 | |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 225 | loop_->mutable_next_R().block<2, 1>(4, 0) = |
| 226 | unprofiled_goal_.block<2, 1>(4, 0); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 227 | 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 Schuh | d7e2994 | 2016-03-12 21:35:11 -0800 | [diff] [blame] | 235 | LOG(DEBUG, "Moving shoulder state. U: %f, %f\n", loop_->U(0, 0), |
| 236 | loop_->U_uncapped(0, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 237 | 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 Schuh | d7e2994 | 2016-03-12 21:35:11 -0800 | [diff] [blame] | 242 | LOG(DEBUG, "Moving shooter state. U: %f, %f\n", loop_->U(1, 0), |
| 243 | loop_->U_uncapped(1, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 244 | wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0)); |
| 245 | } |
| 246 | } |
| 247 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 248 | } // namespace superstructure |
| 249 | } // namespace control_loops |
| 250 | } // namespace y2016 |