Comran Morshed | 25f81a0 | 2016-01-23 13:40:10 +0000 | [diff] [blame] | 1 | #include "y2016/control_loops/superstructure/superstructure.h" |
| 2 | |
| 3 | #include "aos/common/controls/control_loops.q.h" |
| 4 | #include "aos/common/logging/logging.h" |
| 5 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 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 | |
Comran Morshed | 25f81a0 | 2016-01-23 13:40:10 +0000 | [diff] [blame] | 11 | namespace y2016 { |
| 12 | namespace control_loops { |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 13 | namespace superstructure { |
Comran Morshed | 25f81a0 | 2016-01-23 13:40:10 +0000 | [diff] [blame] | 14 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 15 | namespace { |
| 16 | constexpr double kZeroingVoltage = 4.0; |
Comran Morshed | 25f81a0 | 2016-01-23 13:40:10 +0000 | [diff] [blame] | 17 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 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 | } |
Comran Morshed | 25f81a0 | 2016-01-23 13:40:10 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 26 | } // namespace |
| 27 | |
| 28 | void SimpleCappedStateFeedbackLoop::CapU() { |
| 29 | mutable_U(0, 0) = ::std::min(U(0, 0), max_voltage_); |
| 30 | mutable_U(0, 0) = ::std::max(U(0, 0), -max_voltage_); |
| 31 | } |
| 32 | |
| 33 | void DoubleCappedStateFeedbackLoop::CapU() { |
| 34 | mutable_U(0, 0) = ::std::min(U(0, 0), shoulder_max_voltage_); |
| 35 | mutable_U(0, 0) = ::std::max(U(0, 0), -shoulder_max_voltage_); |
| 36 | mutable_U(1, 0) = ::std::min(U(1, 0), wrist_max_voltage_); |
| 37 | mutable_U(1, 0) = ::std::max(U(1, 0), -wrist_max_voltage_); |
| 38 | } |
| 39 | |
| 40 | // Intake |
| 41 | Intake::Intake() |
| 42 | : loop_(new SimpleCappedStateFeedbackLoop(StateFeedbackLoop<3, 1, 1>( |
| 43 | ::y2016::control_loops::superstructure::MakeIntegralIntakeLoop()))), |
| 44 | estimator_(constants::GetValues().intake.zeroing), |
| 45 | profile_(::aos::controls::kLoopFrequency) { |
| 46 | Y_.setZero(); |
| 47 | unprofiled_goal_.setZero(); |
| 48 | offset_.setZero(); |
| 49 | AdjustProfile(0.0, 0.0); |
| 50 | } |
| 51 | |
| 52 | void Intake::UpdateIntakeOffset(double offset) { |
| 53 | const double doffset = offset - offset_(0, 0); |
| 54 | LOG(INFO, "Adjusting Intake offset from %f to %f\n", offset_(0, 0), offset); |
| 55 | |
| 56 | loop_->mutable_X_hat()(0, 0) += doffset; |
| 57 | Y_(0, 0) += doffset; |
| 58 | loop_->mutable_R(0, 0) += doffset; |
| 59 | |
| 60 | profile_.MoveGoal(doffset); |
| 61 | offset_(0, 0) = offset; |
| 62 | |
| 63 | CapGoal("R", &loop_->mutable_R()); |
| 64 | } |
| 65 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 66 | void Intake::Correct(PotAndIndexPosition position) { |
| 67 | estimator_.UpdateEstimate(position); |
| 68 | |
| 69 | if (!initialized_) { |
| 70 | if (estimator_.offset_ready()) { |
| 71 | UpdateIntakeOffset(estimator_.offset()); |
| 72 | initialized_ = true; |
| 73 | } |
| 74 | } |
| 75 | |
Austin Schuh | 444bccd | 2016-02-14 14:00:53 -0800 | [diff] [blame^] | 76 | if (!zeroed_ && estimator_.zeroed()) { |
| 77 | UpdateIntakeOffset(estimator_.offset()); |
| 78 | zeroed_ = true; |
| 79 | } |
| 80 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 81 | Y_ << position.encoder; |
| 82 | Y_ += offset_; |
| 83 | loop_->Correct(Y_); |
| 84 | } |
| 85 | |
| 86 | void Intake::CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal) { |
| 87 | const auto &values = constants::GetValues(); |
| 88 | |
| 89 | // Limit the goal to min/max allowable angles. |
| 90 | if ((*goal)(0, 0) >= values.intake.limits.upper) { |
| 91 | LOG(WARNING, "Intake goal %s above limit, %f > %f\n", name, (*goal)(0, 0), |
| 92 | values.intake.limits.upper); |
| 93 | (*goal)(0, 0) = values.intake.limits.upper; |
| 94 | } |
| 95 | if ((*goal)(0, 0) <= values.intake.limits.lower) { |
| 96 | LOG(WARNING, "Intake goal %s below limit, %f < %f\n", name, (*goal)(0, 0), |
| 97 | values.intake.limits.lower); |
| 98 | (*goal)(0, 0) = values.intake.limits.lower; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void Intake::ForceGoal(double goal) { |
| 103 | set_unprofiled_goal(goal); |
| 104 | loop_->mutable_R() = unprofiled_goal_; |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 105 | loop_->mutable_next_R() = loop_->R(); |
| 106 | |
| 107 | profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0)); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void Intake::set_unprofiled_goal(double unprofiled_goal) { |
| 111 | unprofiled_goal_(0, 0) = unprofiled_goal; |
| 112 | unprofiled_goal_(1, 0) = 0.0; |
| 113 | unprofiled_goal_(2, 0) = 0.0; |
| 114 | CapGoal("unprofiled R", &unprofiled_goal_); |
| 115 | } |
| 116 | |
| 117 | void Intake::Update(bool disable) { |
| 118 | if (!disable) { |
| 119 | ::Eigen::Matrix<double, 2, 1> goal_state = |
| 120 | profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0)); |
| 121 | |
| 122 | loop_->mutable_next_R(0, 0) = goal_state(0, 0); |
| 123 | loop_->mutable_next_R(1, 0) = goal_state(1, 0); |
| 124 | loop_->mutable_next_R(2, 0) = 0.0; |
| 125 | CapGoal("next R", &loop_->mutable_next_R()); |
| 126 | } |
| 127 | |
| 128 | loop_->Update(disable); |
| 129 | |
| 130 | if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) { |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 131 | profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0)); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | bool Intake::CheckHardLimits() { |
| 136 | const auto &values = constants::GetValues(); |
| 137 | // Returns whether hard limits have been exceeded. |
| 138 | |
| 139 | if (angle() >= values.intake.limits.upper_hard || |
| 140 | angle() <= values.intake.limits.lower_hard) { |
| 141 | LOG(ERROR, "Intake at %f out of bounds [%f, %f], ESTOPing\n", angle(), |
| 142 | values.intake.limits.lower_hard, values.intake.limits.upper_hard); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | return false; |
| 147 | } |
| 148 | |
Austin Schuh | 444bccd | 2016-02-14 14:00:53 -0800 | [diff] [blame^] | 149 | void Intake::set_max_voltage(double voltage) { |
| 150 | loop_->set_max_voltage(voltage); |
| 151 | } |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 152 | |
| 153 | void Intake::AdjustProfile(double max_angular_velocity, |
| 154 | double max_angular_acceleration) { |
| 155 | profile_.set_maximum_velocity(UseUnlessZero(max_angular_velocity, 10.0)); |
Austin Schuh | 444bccd | 2016-02-14 14:00:53 -0800 | [diff] [blame^] | 156 | profile_.set_maximum_acceleration( |
| 157 | UseUnlessZero(max_angular_acceleration, 10.0)); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void Intake::Reset() { |
| 161 | estimator_.Reset(); |
| 162 | initialized_ = false; |
| 163 | zeroed_ = false; |
| 164 | } |
| 165 | |
| 166 | EstimatorState Intake::IntakeEstimatorState() { |
| 167 | EstimatorState estimator_state; |
| 168 | ::frc971::zeroing::PopulateEstimatorState(estimator_, &estimator_state); |
| 169 | |
| 170 | return estimator_state; |
| 171 | } |
| 172 | |
| 173 | Arm::Arm() |
| 174 | : loop_(new DoubleCappedStateFeedbackLoop( |
| 175 | ::y2016::control_loops::superstructure::MakeIntegralArmLoop())), |
| 176 | shoulder_profile_(::aos::controls::kLoopFrequency), |
| 177 | wrist_profile_(::aos::controls::kLoopFrequency), |
| 178 | shoulder_estimator_(constants::GetValues().shoulder.zeroing), |
| 179 | wrist_estimator_(constants::GetValues().wrist.zeroing) { |
| 180 | Y_.setZero(); |
| 181 | offset_.setZero(); |
| 182 | unprofiled_goal_.setZero(); |
| 183 | AdjustProfile(0.0, 0.0, 0.0, 0.0); |
| 184 | } |
| 185 | |
| 186 | void Arm::UpdateWristOffset(double offset) { |
| 187 | const double doffset = offset - offset_(1, 0); |
| 188 | LOG(INFO, "Adjusting Wrist offset from %f to %f\n", offset_(1, 0), offset); |
| 189 | |
| 190 | loop_->mutable_X_hat()(2, 0) += doffset; |
| 191 | Y_(1, 0) += doffset; |
| 192 | loop_->mutable_R(2, 0) += doffset; |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 193 | loop_->mutable_next_R(2, 0) += doffset; |
| 194 | unprofiled_goal_(2, 0) += doffset; |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 195 | |
| 196 | wrist_profile_.MoveGoal(doffset); |
| 197 | offset_(1, 0) = offset; |
| 198 | |
| 199 | CapGoal("R", &loop_->mutable_R()); |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 200 | CapGoal("unprofiled R", &loop_->mutable_next_R()); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void Arm::UpdateShoulderOffset(double offset) { |
| 204 | const double doffset = offset - offset_(0, 0); |
Austin Schuh | 444bccd | 2016-02-14 14:00:53 -0800 | [diff] [blame^] | 205 | LOG(INFO, "Adjusting Shoulder offset from %f to %f\n", offset_(0, 0), offset); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 206 | |
| 207 | loop_->mutable_X_hat()(0, 0) += doffset; |
| 208 | loop_->mutable_X_hat()(2, 0) += doffset; |
| 209 | Y_(0, 0) += doffset; |
| 210 | loop_->mutable_R(0, 0) += doffset; |
| 211 | loop_->mutable_R(2, 0) += doffset; |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 212 | loop_->mutable_next_R(0, 0) += doffset; |
| 213 | loop_->mutable_next_R(2, 0) += doffset; |
| 214 | unprofiled_goal_(0, 0) += doffset; |
| 215 | unprofiled_goal_(2, 0) += doffset; |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 216 | |
| 217 | shoulder_profile_.MoveGoal(doffset); |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 218 | wrist_profile_.MoveGoal(doffset); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 219 | offset_(0, 0) = offset; |
| 220 | |
| 221 | CapGoal("R", &loop_->mutable_R()); |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 222 | CapGoal("unprofiled R", &loop_->mutable_next_R()); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // TODO(austin): Handle zeroing errors. |
| 226 | |
| 227 | void Arm::Correct(PotAndIndexPosition position_shoulder, |
| 228 | PotAndIndexPosition position_wrist) { |
| 229 | shoulder_estimator_.UpdateEstimate(position_shoulder); |
| 230 | wrist_estimator_.UpdateEstimate(position_wrist); |
| 231 | |
| 232 | if (!initialized_) { |
| 233 | if (shoulder_estimator_.offset_ready() && wrist_estimator_.offset_ready()) { |
| 234 | UpdateShoulderOffset(shoulder_estimator_.offset()); |
| 235 | UpdateWristOffset(wrist_estimator_.offset()); |
| 236 | initialized_ = true; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (!shoulder_zeroed_ && shoulder_estimator_.zeroed()) { |
| 241 | UpdateShoulderOffset(shoulder_estimator_.offset()); |
| 242 | shoulder_zeroed_ = true; |
| 243 | } |
| 244 | if (!wrist_zeroed_ && wrist_estimator_.zeroed()) { |
| 245 | UpdateWristOffset(wrist_estimator_.offset()); |
| 246 | wrist_zeroed_ = true; |
| 247 | } |
| 248 | |
| 249 | { |
| 250 | Y_ << position_shoulder.encoder, position_wrist.encoder; |
| 251 | Y_ += offset_; |
| 252 | loop_->Correct(Y_); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void Arm::CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal) { |
| 257 | // Limit the goals to min/max allowable angles. |
| 258 | const auto &values = constants::GetValues(); |
| 259 | |
| 260 | if ((*goal)(0, 0) >= values.shoulder.limits.upper) { |
| 261 | LOG(WARNING, "Shoulder goal %s above limit, %f > %f\n", name, (*goal)(0, 0), |
| 262 | values.shoulder.limits.upper); |
| 263 | (*goal)(0, 0) = values.shoulder.limits.upper; |
| 264 | } |
| 265 | if ((*goal)(0, 0) <= values.shoulder.limits.lower) { |
| 266 | LOG(WARNING, "Shoulder goal %s below limit, %f < %f\n", name, (*goal)(0, 0), |
| 267 | values.shoulder.limits.lower); |
| 268 | (*goal)(0, 0) = values.shoulder.limits.lower; |
| 269 | } |
| 270 | |
| 271 | const double wrist_goal_angle_ungrounded = (*goal)(2, 0) - (*goal)(0, 0); |
| 272 | |
| 273 | if (wrist_goal_angle_ungrounded >= values.wrist.limits.upper) { |
| 274 | LOG(WARNING, "Wrist goal %s above limit, %f > %f\n", name, |
| 275 | wrist_goal_angle_ungrounded, values.wrist.limits.upper); |
| 276 | (*goal)(2, 0) = values.wrist.limits.upper + (*goal)(0, 0); |
| 277 | } |
| 278 | if (wrist_goal_angle_ungrounded <= values.wrist.limits.lower) { |
| 279 | LOG(WARNING, "Wrist goal %s below limit, %f < %f\n", name, |
| 280 | wrist_goal_angle_ungrounded, values.wrist.limits.lower); |
| 281 | (*goal)(2, 0) = values.wrist.limits.lower + (*goal)(0, 0); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void Arm::ForceGoal(double goal_shoulder, double goal_wrist) { |
| 286 | set_unprofiled_goal(goal_shoulder, goal_wrist); |
| 287 | loop_->mutable_R() = unprofiled_goal_; |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 288 | loop_->mutable_next_R() = loop_->R(); |
| 289 | |
| 290 | shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0)); |
| 291 | wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0)); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void Arm::set_unprofiled_goal(double unprofiled_goal_shoulder, |
| 295 | double unprofiled_goal_wrist) { |
| 296 | unprofiled_goal_ << unprofiled_goal_shoulder, 0.0, unprofiled_goal_wrist, 0.0, |
| 297 | 0.0, 0.0; |
| 298 | CapGoal("unprofiled R", &unprofiled_goal_); |
| 299 | } |
| 300 | |
| 301 | void Arm::AdjustProfile(double max_angular_velocity_shoulder, |
| 302 | double max_angular_acceleration_shoulder, |
| 303 | double max_angular_velocity_wrist, |
| 304 | double max_angular_acceleration_wrist) { |
| 305 | shoulder_profile_.set_maximum_velocity( |
| 306 | UseUnlessZero(max_angular_velocity_shoulder, 10.0)); |
| 307 | shoulder_profile_.set_maximum_acceleration( |
| 308 | UseUnlessZero(max_angular_acceleration_shoulder, 10.0)); |
| 309 | wrist_profile_.set_maximum_velocity( |
| 310 | UseUnlessZero(max_angular_velocity_wrist, 10.0)); |
| 311 | wrist_profile_.set_maximum_acceleration( |
| 312 | UseUnlessZero(max_angular_acceleration_wrist, 10.0)); |
| 313 | } |
| 314 | |
| 315 | bool Arm::CheckHardLimits() { |
| 316 | const auto &values = constants::GetValues(); |
| 317 | if (shoulder_angle() >= values.shoulder.limits.upper_hard || |
| 318 | shoulder_angle() <= values.shoulder.limits.lower_hard) { |
| 319 | LOG(ERROR, "Shoulder at %f out of bounds [%f, %f], ESTOPing\n", |
| 320 | shoulder_angle(), values.shoulder.limits.lower_hard, |
| 321 | values.shoulder.limits.upper_hard); |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | if (wrist_angle() - shoulder_angle() >= values.wrist.limits.upper_hard || |
| 326 | wrist_angle() - shoulder_angle() <= values.wrist.limits.lower_hard) { |
| 327 | LOG(ERROR, "Wrist at %f out of bounds [%f, %f], ESTOPing\n", |
| 328 | wrist_angle() - shoulder_angle(), values.wrist.limits.lower_hard, |
| 329 | values.wrist.limits.upper_hard); |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | void Arm::Update(bool disable) { |
| 337 | if (!disable) { |
| 338 | // Compute next goal. |
| 339 | ::Eigen::Matrix<double, 2, 1> goal_state_shoulder = |
| 340 | shoulder_profile_.Update(unprofiled_goal_(0, 0), |
| 341 | unprofiled_goal_(1, 0)); |
| 342 | loop_->mutable_next_R(0, 0) = goal_state_shoulder(0, 0); |
| 343 | loop_->mutable_next_R(1, 0) = goal_state_shoulder(1, 0); |
| 344 | |
| 345 | ::Eigen::Matrix<double, 2, 1> goal_state_wrist = |
| 346 | wrist_profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0)); |
| 347 | loop_->mutable_next_R(2, 0) = goal_state_wrist(0, 0); |
| 348 | loop_->mutable_next_R(3, 0) = goal_state_wrist(1, 0); |
| 349 | |
| 350 | loop_->mutable_next_R(4, 0) = unprofiled_goal_(4, 0); |
| 351 | loop_->mutable_next_R(5, 0) = unprofiled_goal_(5, 0); |
| 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 | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 360 | shoulder_profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0)); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Wrist saturated |
| 364 | if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) { |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 365 | wrist_profile_.MoveCurrentState(loop_->R().block<2, 1>(2, 0)); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
| 369 | void Arm::set_max_voltage(double shoulder_max_voltage, |
| 370 | double wrist_max_voltage) { |
| 371 | loop_->set_max_voltage(shoulder_max_voltage, wrist_max_voltage); |
| 372 | } |
| 373 | |
| 374 | void Arm::Reset() { |
| 375 | shoulder_estimator_.Reset(); |
| 376 | wrist_estimator_.Reset(); |
| 377 | initialized_ = false; |
| 378 | shoulder_zeroed_ = false; |
| 379 | wrist_zeroed_ = false; |
| 380 | } |
| 381 | |
| 382 | EstimatorState Arm::ShoulderEstimatorState() { |
| 383 | EstimatorState estimator_state; |
| 384 | ::frc971::zeroing::PopulateEstimatorState(shoulder_estimator_, |
| 385 | &estimator_state); |
| 386 | |
| 387 | return estimator_state; |
| 388 | } |
| 389 | |
| 390 | EstimatorState Arm::WristEstimatorState() { |
| 391 | EstimatorState estimator_state; |
| 392 | ::frc971::zeroing::PopulateEstimatorState(wrist_estimator_, &estimator_state); |
| 393 | |
| 394 | return estimator_state; |
| 395 | } |
| 396 | |
| 397 | // ///// Superstructure ///// |
| 398 | Superstructure::Superstructure( |
| 399 | control_loops::SuperstructureQueue *superstructure_queue) |
| 400 | : aos::controls::ControlLoop<control_loops::SuperstructureQueue>( |
| 401 | superstructure_queue) {} |
| 402 | |
| 403 | void Superstructure::UpdateZeroingState() { |
| 404 | // TODO(austin): Explicit state transitions instead of this. |
| 405 | // TODO(adam): Change this once we have zeroing written. |
| 406 | if (!arm_.initialized() || !intake_.initialized()) { |
| 407 | state_ = INITIALIZING; |
| 408 | } else if (!intake_.zeroed()) { |
| 409 | state_ = ZEROING_INTAKE; |
| 410 | } else if (!arm_.zeroed()) { |
| 411 | state_ = ZEROING_ARM; |
| 412 | } else { |
| 413 | state_ = RUNNING; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | void Superstructure::RunIteration( |
| 418 | const control_loops::SuperstructureQueue::Goal *unsafe_goal, |
| 419 | const control_loops::SuperstructureQueue::Position *position, |
| 420 | control_loops::SuperstructureQueue::Output *output, |
| 421 | control_loops::SuperstructureQueue::Status *status) { |
| 422 | if (WasReset()) { |
| 423 | LOG(ERROR, "WPILib reset, restarting\n"); |
| 424 | arm_.Reset(); |
| 425 | intake_.Reset(); |
| 426 | state_ = UNINITIALIZED; |
| 427 | } |
| 428 | |
| 429 | // Bool to track if we should turn the motors on or not. |
| 430 | bool disable = output == nullptr; |
| 431 | |
| 432 | arm_.Correct(position->shoulder, position->wrist); |
| 433 | intake_.Correct(position->intake); |
| 434 | |
| 435 | // Zeroing will work as follows: |
| 436 | // Start with the intake. Move it towards the center. Once zeroed, move it |
| 437 | // back to the bottom. Rotate the shoulder towards the center. Once zeroed, |
| 438 | // move it up enough to rotate the wrist towards the center. |
| 439 | |
| 440 | // We'll then need code to do sanity checking on values. |
| 441 | |
| 442 | switch (state_) { |
| 443 | case UNINITIALIZED: |
| 444 | LOG(DEBUG, "Uninitialized\n"); |
| 445 | state_ = INITIALIZING; |
| 446 | disable = true; |
| 447 | break; |
| 448 | |
| 449 | case INITIALIZING: |
| 450 | LOG(DEBUG, "Waiting for accurate initial position.\n"); |
| 451 | disable = true; |
| 452 | // Update state_ to accurately represent the state of the zeroing |
| 453 | // estimators. |
| 454 | UpdateZeroingState(); |
| 455 | if (state_ != INITIALIZING) { |
| 456 | // Set the goals to where we are now. |
| 457 | intake_.ForceGoal(intake_.angle()); |
| 458 | arm_.ForceGoal(arm_.shoulder_angle(), arm_.wrist_angle()); |
| 459 | } |
| 460 | break; |
| 461 | |
| 462 | case ZEROING_INTAKE: |
| 463 | case ZEROING_ARM: |
| 464 | // TODO(adam): Add your magic here. |
| 465 | state_ = RUNNING; |
| 466 | break; |
| 467 | |
| 468 | case RUNNING: |
| 469 | if (unsafe_goal) { |
| 470 | arm_.AdjustProfile(unsafe_goal->max_angular_velocity_shoulder, |
| 471 | unsafe_goal->max_angular_acceleration_shoulder, |
| 472 | unsafe_goal->max_angular_velocity_wrist, |
| 473 | unsafe_goal->max_angular_acceleration_wrist); |
| 474 | intake_.AdjustProfile(unsafe_goal->max_angular_velocity_wrist, |
| 475 | unsafe_goal->max_angular_acceleration_intake); |
| 476 | |
| 477 | arm_.set_unprofiled_goal(unsafe_goal->angle_shoulder, |
| 478 | unsafe_goal->angle_wrist); |
| 479 | intake_.set_unprofiled_goal(unsafe_goal->angle_intake); |
| 480 | } |
| 481 | |
| 482 | // Update state_ to accurately represent the state of the zeroing |
| 483 | // estimators. |
| 484 | |
| 485 | if (state_ != RUNNING && state_ != ESTOP) { |
| 486 | state_ = UNINITIALIZED; |
| 487 | } |
| 488 | break; |
| 489 | |
| 490 | case ESTOP: |
| 491 | LOG(ERROR, "Estop\n"); |
| 492 | disable = true; |
| 493 | break; |
| 494 | } |
| 495 | |
| 496 | // ESTOP if we hit any of the limits. It is safe(ish) to hit the limits while |
| 497 | // zeroing since we use such low power. |
| 498 | if (state_ == RUNNING) { |
| 499 | // ESTOP if we hit the hard limits. |
| 500 | if ((arm_.CheckHardLimits() || intake_.CheckHardLimits()) && output) { |
| 501 | state_ = ESTOP; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | // Set the voltage limits. |
| 506 | const double max_voltage = state_ == RUNNING ? 12.0 : kZeroingVoltage; |
| 507 | arm_.set_max_voltage(max_voltage, max_voltage); |
| 508 | intake_.set_max_voltage(max_voltage); |
| 509 | |
| 510 | // Calculate the loops for a cycle. |
| 511 | arm_.Update(disable); |
| 512 | intake_.Update(disable); |
| 513 | |
| 514 | // Write out all the voltages. |
| 515 | if (output) { |
| 516 | output->voltage_intake = intake_.intake_voltage(); |
| 517 | output->voltage_shoulder = arm_.shoulder_voltage(); |
| 518 | output->voltage_wrist = arm_.wrist_voltage(); |
| 519 | } |
| 520 | |
| 521 | // Save debug/internal state. |
| 522 | // TODO(austin): Save the voltage errors. |
| 523 | status->zeroed = state_ == RUNNING; |
| 524 | |
| 525 | status->shoulder.angle = arm_.X_hat(0, 0); |
| 526 | status->shoulder.angular_velocity = arm_.X_hat(1, 0); |
| 527 | status->shoulder.goal_angle = arm_.goal(0, 0); |
| 528 | status->shoulder.goal_angular_velocity = arm_.goal(1, 0); |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 529 | status->shoulder.unprofiled_goal_angle = arm_.unprofiled_goal(0, 0); |
| 530 | status->shoulder.unprofiled_goal_angular_velocity = |
| 531 | arm_.unprofiled_goal(1, 0); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 532 | status->shoulder.estimator_state = arm_.ShoulderEstimatorState(); |
| 533 | |
| 534 | status->wrist.angle = arm_.X_hat(2, 0); |
| 535 | status->wrist.angular_velocity = arm_.X_hat(3, 0); |
| 536 | status->wrist.goal_angle = arm_.goal(2, 0); |
| 537 | status->wrist.goal_angular_velocity = arm_.goal(3, 0); |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 538 | status->wrist.unprofiled_goal_angle = arm_.unprofiled_goal(2, 0); |
| 539 | status->wrist.unprofiled_goal_angular_velocity = arm_.unprofiled_goal(3, 0); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 540 | status->wrist.estimator_state = arm_.WristEstimatorState(); |
| 541 | |
| 542 | status->intake.angle = intake_.X_hat(0, 0); |
| 543 | status->intake.angular_velocity = intake_.X_hat(1, 0); |
| 544 | status->intake.goal_angle = intake_.goal(0, 0); |
| 545 | status->intake.goal_angular_velocity = intake_.goal(1, 0); |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 546 | status->intake.unprofiled_goal_angle = intake_.unprofiled_goal(0, 0); |
| 547 | status->intake.unprofiled_goal_angular_velocity = |
| 548 | intake_.unprofiled_goal(1, 0); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 549 | status->intake.estimator_state = intake_.IntakeEstimatorState(); |
| 550 | |
| 551 | status->estopped = (state_ == ESTOP); |
| 552 | |
| 553 | status->state = state_; |
| 554 | |
| 555 | last_state_ = state_; |
| 556 | } |
| 557 | |
| 558 | } // namespace superstructure |
Comran Morshed | 25f81a0 | 2016-01-23 13:40:10 +0000 | [diff] [blame] | 559 | } // namespace control_loops |
| 560 | } // namespace y2016 |