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; |
| 16 | using ::frc971::EstimatorState; |
| 17 | |
| 18 | namespace { |
| 19 | double UseUnlessZero(double target_value, double default_value) { |
| 20 | if (target_value != 0.0) { |
| 21 | return target_value; |
| 22 | } else { |
| 23 | return default_value; |
| 24 | } |
| 25 | } |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame^] | 26 | |
| 27 | enum ArmIndices { kShoulderIndex = 0, kWristIndex = 1 }; |
| 28 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 29 | } // namespace |
| 30 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 31 | // Intake |
| 32 | Intake::Intake() |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame^] | 33 | : ProfiledSubsystem( |
| 34 | ::std::unique_ptr< |
| 35 | ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>>( |
| 36 | new ::frc971::control_loops::SimpleCappedStateFeedbackLoop< |
| 37 | 3, 1, 1>(::y2016::control_loops::superstructure:: |
| 38 | MakeIntegralIntakeLoop()))), |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 39 | estimator_(constants::GetValues().intake.zeroing), |
| 40 | profile_(::aos::controls::kLoopFrequency) { |
| 41 | Y_.setZero(); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 42 | offset_.setZero(); |
| 43 | AdjustProfile(0.0, 0.0); |
| 44 | } |
| 45 | |
| 46 | void Intake::UpdateIntakeOffset(double offset) { |
| 47 | const double doffset = offset - offset_(0, 0); |
| 48 | LOG(INFO, "Adjusting Intake offset from %f to %f\n", offset_(0, 0), offset); |
| 49 | |
| 50 | loop_->mutable_X_hat()(0, 0) += doffset; |
| 51 | Y_(0, 0) += doffset; |
| 52 | loop_->mutable_R(0, 0) += doffset; |
| 53 | |
| 54 | profile_.MoveGoal(doffset); |
| 55 | offset_(0, 0) = offset; |
| 56 | |
| 57 | CapGoal("R", &loop_->mutable_R()); |
| 58 | } |
| 59 | |
| 60 | void Intake::Correct(PotAndIndexPosition position) { |
| 61 | estimator_.UpdateEstimate(position); |
| 62 | |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 63 | if (estimator_.error()) { |
| 64 | LOG(ERROR, "zeroing error with intake_estimator\n"); |
| 65 | return; |
| 66 | } |
| 67 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 68 | if (!initialized_) { |
| 69 | if (estimator_.offset_ready()) { |
| 70 | UpdateIntakeOffset(estimator_.offset()); |
| 71 | initialized_ = true; |
| 72 | } |
| 73 | } |
| 74 | |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame^] | 75 | if (!zeroed(0) && estimator_.zeroed()) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 76 | UpdateIntakeOffset(estimator_.offset()); |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame^] | 77 | set_zeroed(0, true); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | Y_ << position.encoder; |
| 81 | Y_ += offset_; |
| 82 | loop_->Correct(Y_); |
| 83 | } |
| 84 | |
| 85 | void Intake::CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 86 | // Limit the goal to min/max allowable angles. |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 87 | if ((*goal)(0, 0) > constants::Values::kIntakeRange.upper) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 88 | LOG(WARNING, "Intake goal %s above limit, %f > %f\n", name, (*goal)(0, 0), |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 89 | constants::Values::kIntakeRange.upper); |
| 90 | (*goal)(0, 0) = constants::Values::kIntakeRange.upper; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 91 | } |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 92 | if ((*goal)(0, 0) < constants::Values::kIntakeRange.lower) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 93 | LOG(WARNING, "Intake goal %s below limit, %f < %f\n", name, (*goal)(0, 0), |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 94 | constants::Values::kIntakeRange.lower); |
| 95 | (*goal)(0, 0) = constants::Values::kIntakeRange.lower; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | void Intake::ForceGoal(double goal) { |
| 100 | set_unprofiled_goal(goal); |
| 101 | loop_->mutable_R() = unprofiled_goal_; |
| 102 | loop_->mutable_next_R() = loop_->R(); |
| 103 | |
| 104 | profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0)); |
| 105 | } |
| 106 | |
| 107 | void Intake::set_unprofiled_goal(double unprofiled_goal) { |
| 108 | unprofiled_goal_(0, 0) = unprofiled_goal; |
| 109 | unprofiled_goal_(1, 0) = 0.0; |
| 110 | unprofiled_goal_(2, 0) = 0.0; |
| 111 | CapGoal("unprofiled R", &unprofiled_goal_); |
| 112 | } |
| 113 | |
| 114 | void Intake::Update(bool disable) { |
| 115 | if (!disable) { |
| 116 | ::Eigen::Matrix<double, 2, 1> goal_state = |
| 117 | profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0)); |
| 118 | |
| 119 | loop_->mutable_next_R(0, 0) = goal_state(0, 0); |
| 120 | loop_->mutable_next_R(1, 0) = goal_state(1, 0); |
| 121 | loop_->mutable_next_R(2, 0) = 0.0; |
| 122 | CapGoal("next R", &loop_->mutable_next_R()); |
| 123 | } |
| 124 | |
| 125 | loop_->Update(disable); |
| 126 | |
| 127 | if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) { |
| 128 | profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | bool Intake::CheckHardLimits() { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 133 | // Returns whether hard limits have been exceeded. |
| 134 | |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 135 | if (angle() > constants::Values::kIntakeRange.upper_hard || |
| 136 | angle() < constants::Values::kIntakeRange.lower_hard) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 137 | LOG(ERROR, "Intake at %f out of bounds [%f, %f], ESTOPing\n", angle(), |
Austin Schuh | b1d682b | 2016-02-16 13:07:44 -0800 | [diff] [blame] | 138 | constants::Values::kIntakeRange.lower_hard, |
| 139 | constants::Values::kIntakeRange.upper_hard); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 140 | return true; |
| 141 | } |
| 142 | |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | void Intake::set_max_voltage(double voltage) { |
Austin Schuh | dc710d2 | 2016-02-16 13:55:26 -0800 | [diff] [blame] | 147 | loop_->set_max_voltage(0, voltage); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void Intake::AdjustProfile(double max_angular_velocity, |
| 151 | double max_angular_acceleration) { |
| 152 | profile_.set_maximum_velocity(UseUnlessZero(max_angular_velocity, 10.0)); |
| 153 | profile_.set_maximum_acceleration( |
| 154 | UseUnlessZero(max_angular_acceleration, 10.0)); |
| 155 | } |
| 156 | |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame^] | 157 | void Intake::DoReset() { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 158 | estimator_.Reset(); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | EstimatorState Intake::IntakeEstimatorState() { |
| 162 | EstimatorState estimator_state; |
| 163 | ::frc971::zeroing::PopulateEstimatorState(estimator_, &estimator_state); |
| 164 | |
| 165 | return estimator_state; |
| 166 | } |
| 167 | |
| 168 | Arm::Arm() |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame^] | 169 | : ProfiledSubsystem(::std::unique_ptr<ArmControlLoop>(new ArmControlLoop( |
| 170 | ::y2016::control_loops::superstructure::MakeIntegralArmLoop()))), |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 171 | shoulder_profile_(::aos::controls::kLoopFrequency), |
| 172 | wrist_profile_(::aos::controls::kLoopFrequency), |
| 173 | shoulder_estimator_(constants::GetValues().shoulder.zeroing), |
| 174 | wrist_estimator_(constants::GetValues().wrist.zeroing) { |
| 175 | Y_.setZero(); |
| 176 | offset_.setZero(); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 177 | AdjustProfile(0.0, 0.0, 0.0, 0.0); |
| 178 | } |
| 179 | |
| 180 | void Arm::UpdateWristOffset(double offset) { |
| 181 | const double doffset = offset - offset_(1, 0); |
| 182 | LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), offset); |
| 183 | |
| 184 | loop_->mutable_X_hat()(2, 0) += doffset; |
| 185 | Y_(1, 0) += doffset; |
| 186 | loop_->mutable_R(2, 0) += doffset; |
| 187 | loop_->mutable_next_R(2, 0) += doffset; |
| 188 | unprofiled_goal_(2, 0) += doffset; |
| 189 | |
| 190 | wrist_profile_.MoveGoal(doffset); |
| 191 | offset_(1, 0) = offset; |
| 192 | |
| 193 | CapGoal("R", &loop_->mutable_R()); |
| 194 | CapGoal("unprofiled R", &loop_->mutable_next_R()); |
| 195 | } |
| 196 | |
| 197 | void Arm::UpdateShoulderOffset(double offset) { |
| 198 | const double doffset = offset - offset_(0, 0); |
| 199 | LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), offset); |
| 200 | |
| 201 | loop_->mutable_X_hat()(0, 0) += doffset; |
| 202 | loop_->mutable_X_hat()(2, 0) += doffset; |
| 203 | Y_(0, 0) += doffset; |
| 204 | loop_->mutable_R(0, 0) += doffset; |
| 205 | loop_->mutable_R(2, 0) += doffset; |
| 206 | loop_->mutable_next_R(0, 0) += doffset; |
| 207 | loop_->mutable_next_R(2, 0) += doffset; |
| 208 | unprofiled_goal_(0, 0) += doffset; |
| 209 | unprofiled_goal_(2, 0) += doffset; |
| 210 | |
| 211 | shoulder_profile_.MoveGoal(doffset); |
| 212 | wrist_profile_.MoveGoal(doffset); |
| 213 | offset_(0, 0) = offset; |
| 214 | |
| 215 | CapGoal("R", &loop_->mutable_R()); |
| 216 | CapGoal("unprofiled R", &loop_->mutable_next_R()); |
| 217 | } |
| 218 | |
| 219 | // TODO(austin): Handle zeroing errors. |
| 220 | |
| 221 | void Arm::Correct(PotAndIndexPosition position_shoulder, |
| 222 | PotAndIndexPosition position_wrist) { |
| 223 | shoulder_estimator_.UpdateEstimate(position_shoulder); |
| 224 | wrist_estimator_.UpdateEstimate(position_wrist); |
| 225 | |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 226 | // Handle zeroing errors |
| 227 | if (shoulder_estimator_.error()) { |
| 228 | LOG(ERROR, "zeroing error with shoulder_estimator\n"); |
| 229 | return; |
| 230 | } |
| 231 | if (wrist_estimator_.error()) { |
| 232 | LOG(ERROR, "zeroing error with wrist_estimator\n"); |
| 233 | return; |
| 234 | } |
| 235 | |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 236 | if (!initialized_) { |
| 237 | if (shoulder_estimator_.offset_ready() && wrist_estimator_.offset_ready()) { |
| 238 | UpdateShoulderOffset(shoulder_estimator_.offset()); |
| 239 | UpdateWristOffset(wrist_estimator_.offset()); |
| 240 | initialized_ = true; |
| 241 | } |
| 242 | } |
| 243 | |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame^] | 244 | if (!zeroed(kShoulderIndex) && shoulder_estimator_.zeroed()) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 245 | UpdateShoulderOffset(shoulder_estimator_.offset()); |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame^] | 246 | set_zeroed(kShoulderIndex, true); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 247 | } |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame^] | 248 | if (!zeroed(kWristIndex) && wrist_estimator_.zeroed()) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 249 | UpdateWristOffset(wrist_estimator_.offset()); |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame^] | 250 | set_zeroed(kWristIndex, true); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | { |
| 254 | Y_ << position_shoulder.encoder, position_wrist.encoder; |
| 255 | Y_ += offset_; |
| 256 | loop_->Correct(Y_); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) { |
| 261 | // Limit the goals to min/max allowable angles. |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 262 | |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 263 | if ((*goal)(0, 0) > constants::Values::kShoulderRange.upper) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 264 | 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] | 265 | constants::Values::kShoulderRange.upper); |
| 266 | (*goal)(0, 0) = constants::Values::kShoulderRange.upper; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 267 | } |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 268 | if ((*goal)(0, 0) < constants::Values::kShoulderRange.lower) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 269 | 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] | 270 | constants::Values::kShoulderRange.lower); |
| 271 | (*goal)(0, 0) = constants::Values::kShoulderRange.lower; |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0); |
| 275 | |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 276 | if (wrist_goal_angle_ungrounded > constants::Values::kWristRange.upper) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 277 | LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name, |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 278 | wrist_goal_angle_ungrounded, constants::Values::kWristRange.upper); |
| 279 | (*goal)(2, 0) = constants::Values::kWristRange.upper + (*goal)(0, 0); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 280 | } |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 281 | if (wrist_goal_angle_ungrounded < constants::Values::kWristRange.lower) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 282 | LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name, |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 283 | wrist_goal_angle_ungrounded, constants::Values::kWristRange.lower); |
| 284 | (*goal)(2, 0) = constants::Values::kWristRange.lower + (*goal)(0, 0); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | void Arm::ForceGoal(double goal_shoulder, double goal_wrist) { |
| 289 | set_unprofiled_goal(goal_shoulder, goal_wrist); |
| 290 | loop_->mutable_R() = unprofiled_goal_; |
| 291 | loop_->mutable_next_R() = loop_->R(); |
| 292 | |
| 293 | shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0)); |
| 294 | wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0)); |
| 295 | } |
| 296 | |
| 297 | void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder, |
| 298 | double unprofiled_goal_wrist) { |
| 299 | unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0, |
| 300 | 0.0, 0.0; |
| 301 | CapGoal("unprofiled R", &unprofiled_goal_); |
| 302 | } |
| 303 | |
| 304 | void Arm::AdjustProfile(double max_angular_velocity_shoulder, |
| 305 | double max_angular_acceleration_shoulder, |
| 306 | double max_angular_velocity_wrist, |
| 307 | double max_angular_acceleration_wrist) { |
| 308 | shoulder_profile_.set_maximum_velocity( |
| 309 | UseUnlessZero(max_angular_velocity_shoulder, 10.0)); |
| 310 | shoulder_profile_.set_maximum_acceleration( |
| 311 | UseUnlessZero(max_angular_acceleration_shoulder, 10.0)); |
| 312 | wrist_profile_.set_maximum_velocity( |
| 313 | UseUnlessZero(max_angular_velocity_wrist, 10.0)); |
| 314 | wrist_profile_.set_maximum_acceleration( |
| 315 | UseUnlessZero(max_angular_acceleration_wrist, 10.0)); |
| 316 | } |
| 317 | |
| 318 | bool Arm::CheckHardLimits() { |
Austin Schuh | 3b0f364 | 2016-02-14 21:04:26 -0800 | [diff] [blame] | 319 | if (shoulder_angle() > constants::Values::kShoulderRange.upper_hard || |
| 320 | shoulder_angle() < constants::Values::kShoulderRange.lower_hard) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 321 | LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n", |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 322 | shoulder_angle(), constants::Values::kShoulderRange.lower_hard, |
| 323 | constants::Values::kShoulderRange.upper_hard); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 324 | return true; |
| 325 | } |
| 326 | |
Austin Schuh | b1d682b | 2016-02-16 13:07:44 -0800 | [diff] [blame] | 327 | if (wrist_angle() - shoulder_angle() > |
| 328 | constants::Values::kWristRange.upper_hard || |
| 329 | wrist_angle() - shoulder_angle() < |
| 330 | constants::Values::kWristRange.lower_hard) { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 331 | LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n", |
Austin Schuh | b1d682b | 2016-02-16 13:07:44 -0800 | [diff] [blame] | 332 | wrist_angle() - shoulder_angle(), |
| 333 | constants::Values::kWristRange.lower_hard, |
Comran Morshed | 225f0b9 | 2016-02-10 20:34:27 +0000 | [diff] [blame] | 334 | constants::Values::kWristRange.upper_hard); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 335 | return true; |
| 336 | } |
| 337 | |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | void Arm::Update(bool disable) { |
| 342 | if (!disable) { |
| 343 | // Compute next goal. |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 344 | loop_->mutable_next_R().block<2, 1>(0, 0) = shoulder_profile_.Update( |
| 345 | unprofiled_goal_(0, 0), unprofiled_goal_(1, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 346 | |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 347 | loop_->mutable_next_R().block<2, 1>(2, 0) = |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 348 | wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 349 | |
Austin Schuh | f59b6bc | 2016-03-11 21:26:19 -0800 | [diff] [blame] | 350 | loop_->mutable_next_R().block<2, 1>(4, 0) = |
| 351 | unprofiled_goal_.block<2, 1>(4, 0); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 352 | CapGoal("next R", &loop_->mutable_next_R()); |
| 353 | } |
| 354 | |
| 355 | // Move loop |
| 356 | loop_->Update(disable); |
| 357 | |
| 358 | // Shoulder saturated |
| 359 | if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) { |
Austin Schuh | d7e2994 | 2016-03-12 21:35:11 -0800 | [diff] [blame] | 360 | LOG(DEBUG, "Moving shoulder state. U: %f, %f\n", loop_->U(0, 0), |
| 361 | loop_->U_uncapped(0, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 362 | shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0)); |
| 363 | } |
| 364 | |
| 365 | // Wrist saturated |
| 366 | if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) { |
Austin Schuh | d7e2994 | 2016-03-12 21:35:11 -0800 | [diff] [blame] | 367 | LOG(DEBUG, "Moving shooter state. U: %f, %f\n", loop_->U(1, 0), |
| 368 | loop_->U_uncapped(1, 0)); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 369 | wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0)); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | void Arm::set_max_voltage(double shoulder_max_voltage, |
| 374 | double wrist_max_voltage) { |
Austin Schuh | dc710d2 | 2016-02-16 13:55:26 -0800 | [diff] [blame] | 375 | loop_->set_max_voltage(0, shoulder_max_voltage); |
| 376 | loop_->set_max_voltage(1, wrist_max_voltage); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Austin Schuh | cb60e29 | 2017-01-01 16:07:31 -0800 | [diff] [blame^] | 379 | void Arm::DoReset() { |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 380 | shoulder_estimator_.Reset(); |
| 381 | wrist_estimator_.Reset(); |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | EstimatorState Arm::ShoulderEstimatorState() { |
| 385 | EstimatorState estimator_state; |
| 386 | ::frc971::zeroing::PopulateEstimatorState(shoulder_estimator_, |
| 387 | &estimator_state); |
| 388 | |
| 389 | return estimator_state; |
| 390 | } |
| 391 | |
| 392 | EstimatorState Arm::WristEstimatorState() { |
| 393 | EstimatorState estimator_state; |
| 394 | ::frc971::zeroing::PopulateEstimatorState(wrist_estimator_, &estimator_state); |
| 395 | |
| 396 | return estimator_state; |
| 397 | } |
| 398 | |
| 399 | } // namespace superstructure |
| 400 | } // namespace control_loops |
| 401 | } // namespace y2016 |