Comran Morshed | 25f81a0 | 2016-01-23 13:40:10 +0000 | [diff] [blame] | 1 | #include "y2016/control_loops/superstructure/superstructure.h" |
Austin Schuh | 10c2d11 | 2016-02-14 13:42:28 -0800 | [diff] [blame] | 2 | #include "y2016/control_loops/superstructure/superstructure_controls.h" |
Comran Morshed | 25f81a0 | 2016-01-23 13:40:10 +0000 | [diff] [blame] | 3 | |
| 4 | #include "aos/common/controls/control_loops.q.h" |
| 5 | #include "aos/common/logging/logging.h" |
| 6 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 7 | #include "y2016/control_loops/superstructure/integral_intake_plant.h" |
| 8 | #include "y2016/control_loops/superstructure/integral_arm_plant.h" |
| 9 | |
| 10 | #include "y2016/constants.h" |
| 11 | |
Comran Morshed | 25f81a0 | 2016-01-23 13:40:10 +0000 | [diff] [blame] | 12 | namespace y2016 { |
| 13 | namespace control_loops { |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 14 | namespace superstructure { |
Comran Morshed | 25f81a0 | 2016-01-23 13:40:10 +0000 | [diff] [blame] | 15 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 16 | namespace { |
Comran Morshed | f4cd764 | 2016-02-15 20:40:49 +0000 | [diff] [blame] | 17 | constexpr double kZeroingVoltage = 4.0; |
| 18 | |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 19 | constexpr double kIntakeEncoderIndexDifference = |
| 20 | constants::Values::kIntakeEncoderIndexDifference; |
| 21 | constexpr double kWristEncoderIndexDifference = |
| 22 | constants::Values::kWristEncoderIndexDifference; |
| 23 | constexpr double kShoulderEncoderIndexDifference = |
| 24 | constants::Values::kShoulderEncoderIndexDifference; |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 25 | } // namespace |
| 26 | |
Philipp Schrader | 0119eb1 | 2016-02-15 18:16:21 +0000 | [diff] [blame] | 27 | // ///// CollisionAvoidance ///// |
| 28 | |
| 29 | void CollisionAvoidance::UpdateGoal(double shoulder_angle_goal, |
| 30 | double wrist_angle_goal, |
| 31 | double intake_angle_goal) { |
| 32 | double shoulder_angle = arm_->shoulder_angle(); |
| 33 | double wrist_angle = arm_->wrist_angle(); |
| 34 | double intake_angle = intake_->angle(); |
| 35 | |
| 36 | // TODO(phil): This may need tuning to account for bounciness in the limbs or |
| 37 | // some other thing that I haven't thought of. At the very least, |
| 38 | // incorporating a small safety margin makes writing test cases much easier |
| 39 | // since you can directly compare statuses against the constants in the |
| 40 | // CollisionAvoidance class. |
| 41 | constexpr double kSafetyMargin = 0.01; // radians |
| 42 | |
| 43 | // Avoid colliding the shooter with the frame. |
| 44 | // If the shoulder is below a certain angle or we want to move it below |
| 45 | // that angle, then the shooter has to stay level to the ground. Otherwise, |
| 46 | // it will crash into the frame. |
| 47 | if (shoulder_angle < kMinShoulderAngleForHorizontalShooter || |
| 48 | shoulder_angle_goal < kMinShoulderAngleForHorizontalShooter) { |
| 49 | wrist_angle_goal = 0.0; |
| 50 | |
| 51 | // Make sure that we don't move the shoulder below a certain angle until |
| 52 | // the wrist is level with the ground. |
| 53 | if (::std::abs(wrist_angle) > kMaxWristAngleForSafeArmStowing) { |
| 54 | shoulder_angle_goal = |
| 55 | ::std::max(shoulder_angle_goal, |
| 56 | kMinShoulderAngleForHorizontalShooter + kSafetyMargin); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Is the arm where it could interfere with the intake right now? |
| 61 | bool shoulder_is_in_danger = |
| 62 | (shoulder_angle < kMinShoulderAngleForIntakeInterference && |
| 63 | shoulder_angle > kMaxShoulderAngleUntilSafeIntakeStowing); |
| 64 | |
| 65 | // Is the arm moving into collision zone from above? |
| 66 | bool shoulder_moving_into_danger_from_above = |
| 67 | (shoulder_angle >= kMinShoulderAngleForIntakeInterference && |
| 68 | shoulder_angle_goal <= kMinShoulderAngleForIntakeInterference); |
| 69 | |
| 70 | // Is the arm moving into collision zone from below? |
| 71 | bool shoulder_moving_into_danger_from_below = |
| 72 | (shoulder_angle <= kMaxShoulderAngleUntilSafeIntakeStowing && |
| 73 | shoulder_angle_goal >= kMaxShoulderAngleUntilSafeIntakeStowing); |
| 74 | |
| 75 | // Avoid colliding the arm with the intake. |
| 76 | if (shoulder_is_in_danger || shoulder_moving_into_danger_from_above || |
| 77 | shoulder_moving_into_danger_from_below) { |
| 78 | // If the arm could collide with the intake, we make sure to move the |
| 79 | // intake out of the way. The arm has priority. |
| 80 | intake_angle_goal = |
| 81 | ::std::min(intake_angle_goal, |
| 82 | kMaxIntakeAngleBeforeArmInterference - kSafetyMargin); |
| 83 | |
| 84 | // Don't let the shoulder move into the collision area until the intake is |
| 85 | // out of the way. |
| 86 | if (intake_angle > kMaxIntakeAngleBeforeArmInterference) { |
| 87 | const double kHalfwayPointBetweenSafeZones = |
| 88 | (kMinShoulderAngleForIntakeInterference + |
| 89 | kMaxShoulderAngleUntilSafeIntakeStowing) / |
| 90 | 2.0; |
| 91 | |
| 92 | if (shoulder_angle >= kHalfwayPointBetweenSafeZones) { |
| 93 | // The shoulder is closer to being above the collision area. Move it up |
| 94 | // there. |
| 95 | shoulder_angle_goal = |
| 96 | ::std::max(shoulder_angle_goal, |
| 97 | kMinShoulderAngleForIntakeInterference + kSafetyMargin); |
| 98 | } else { |
| 99 | // The shoulder is closer to being below the collision zone (i.e. in |
| 100 | // stowing/intake position), keep it there for now. |
| 101 | shoulder_angle_goal = |
| 102 | ::std::min(shoulder_angle_goal, |
| 103 | kMaxShoulderAngleUntilSafeIntakeStowing - kSafetyMargin); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Send the possibly adjusted goals to the components. |
| 109 | arm_->set_unprofiled_goal(shoulder_angle_goal, wrist_angle_goal); |
| 110 | intake_->set_unprofiled_goal(intake_angle_goal); |
| 111 | } |
| 112 | |
Philipp Schrader | 0714753 | 2016-02-16 01:23:07 +0000 | [diff] [blame] | 113 | bool CollisionAvoidance::collided() const { |
| 114 | return collided_with_given_angles(arm_->shoulder_angle(), arm_->wrist_angle(), |
| 115 | intake_->angle()); |
| 116 | } |
| 117 | |
| 118 | bool CollisionAvoidance::collided_with_given_angles(double shoulder_angle, |
| 119 | double wrist_angle, |
| 120 | double intake_angle) { |
| 121 | // The arm and the intake must not hit. |
| 122 | if (shoulder_angle >= |
| 123 | CollisionAvoidance::kMaxShoulderAngleUntilSafeIntakeStowing && |
| 124 | shoulder_angle <= |
| 125 | CollisionAvoidance::kMinShoulderAngleForIntakeInterference && |
| 126 | intake_angle > CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference) { |
Austin Schuh | c85cdd5 | 2016-02-16 13:05:35 -0800 | [diff] [blame^] | 127 | LOG(DEBUG, "Collided: Intake %f > %f, and shoulder %f < %f < %f.\n", intake_angle, |
| 128 | CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference, |
| 129 | CollisionAvoidance::kMinShoulderAngleForIntakeInterference, |
| 130 | shoulder_angle, |
| 131 | CollisionAvoidance::kMaxShoulderAngleUntilSafeIntakeStowing); |
Philipp Schrader | 0714753 | 2016-02-16 01:23:07 +0000 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | |
| 135 | // The wrist must go back to zero when the shoulder is moving the arm into |
| 136 | // a stowed/intaking position. |
| 137 | if (shoulder_angle < |
| 138 | CollisionAvoidance::kMinShoulderAngleForHorizontalShooter && |
| 139 | ::std::abs(wrist_angle) > kMaxWristAngleForSafeArmStowing) { |
Austin Schuh | c85cdd5 | 2016-02-16 13:05:35 -0800 | [diff] [blame^] | 140 | LOG(DEBUG, "Collided: Shoulder %f < %f and wrist |%f| < %f.\n", |
| 141 | shoulder_angle, |
| 142 | CollisionAvoidance::kMinShoulderAngleForHorizontalShooter, wrist_angle, |
| 143 | kMaxWristAngleForSafeArmStowing); |
Philipp Schrader | 0714753 | 2016-02-16 01:23:07 +0000 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | |
| 147 | return false; |
| 148 | } |
| 149 | |
Philipp Schrader | 0119eb1 | 2016-02-15 18:16:21 +0000 | [diff] [blame] | 150 | constexpr double CollisionAvoidance::kMinShoulderAngleForHorizontalShooter; |
| 151 | constexpr double CollisionAvoidance::kMinShoulderAngleForIntakeInterference; |
| 152 | constexpr double CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference; |
| 153 | constexpr double CollisionAvoidance::kMaxWristAngleForSafeArmStowing; |
| 154 | constexpr double CollisionAvoidance::kMaxShoulderAngleUntilSafeIntakeStowing; |
| 155 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 156 | Superstructure::Superstructure( |
| 157 | control_loops::SuperstructureQueue *superstructure_queue) |
| 158 | : aos::controls::ControlLoop<control_loops::SuperstructureQueue>( |
Philipp Schrader | 0119eb1 | 2016-02-15 18:16:21 +0000 | [diff] [blame] | 159 | superstructure_queue), |
| 160 | collision_avoidance_(&intake_, &arm_) {} |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 161 | |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 162 | bool Superstructure::IsArmNear(double shoulder_tolerance, |
| 163 | double wrist_tolerance) { |
| 164 | return ((arm_.unprofiled_goal() - arm_.X_hat()) |
| 165 | .block<2, 1>(0, 0) |
| 166 | .lpNorm<Eigen::Infinity>() < shoulder_tolerance) && |
| 167 | ((arm_.unprofiled_goal() - arm_.X_hat()) |
| 168 | .block<4, 1>(0, 0) |
| 169 | .lpNorm<Eigen::Infinity>() < wrist_tolerance) && |
| 170 | ((arm_.unprofiled_goal() - arm_.goal()) |
| 171 | .block<4, 1>(0, 0) |
| 172 | .lpNorm<Eigen::Infinity>() < 1e-6); |
| 173 | } |
| 174 | |
| 175 | bool Superstructure::IsArmNear(double tolerance) { |
| 176 | return ((arm_.unprofiled_goal() - arm_.X_hat()) |
| 177 | .block<4, 1>(0, 0) |
| 178 | .lpNorm<Eigen::Infinity>() < tolerance) && |
| 179 | ((arm_.unprofiled_goal() - arm_.goal()) |
| 180 | .block<4, 1>(0, 0) |
| 181 | .lpNorm<Eigen::Infinity>() < 1e-6); |
| 182 | } |
| 183 | |
| 184 | bool Superstructure::IsIntakeNear(double tolerance) { |
| 185 | return ((intake_.unprofiled_goal() - intake_.X_hat()) |
| 186 | .block<2, 1>(0, 0) |
| 187 | .lpNorm<Eigen::Infinity>() < tolerance); |
| 188 | } |
| 189 | |
| 190 | double Superstructure::MoveButKeepAbove(double reference_angle, |
| 191 | double current_angle, |
| 192 | double move_distance) { |
| 193 | return -MoveButKeepBelow(-reference_angle, -current_angle, -move_distance); |
| 194 | } |
| 195 | |
| 196 | double Superstructure::MoveButKeepBelow(double reference_angle, |
| 197 | double current_angle, |
| 198 | double move_distance) { |
| 199 | // There are 3 interesting places to move to. |
| 200 | const double small_negative_move = current_angle - move_distance; |
| 201 | const double small_positive_move = current_angle + move_distance; |
| 202 | // And the reference angle. |
| 203 | |
| 204 | // Move the the highest one that is below reference_angle. |
| 205 | if (small_negative_move > reference_angle) { |
| 206 | return reference_angle; |
| 207 | } else if (small_positive_move > reference_angle) { |
| 208 | return small_negative_move; |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 209 | } else { |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 210 | return small_positive_move; |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | void Superstructure::RunIteration( |
| 215 | const control_loops::SuperstructureQueue::Goal *unsafe_goal, |
| 216 | const control_loops::SuperstructureQueue::Position *position, |
| 217 | control_loops::SuperstructureQueue::Output *output, |
| 218 | control_loops::SuperstructureQueue::Status *status) { |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 219 | const State state_before_switch = state_; |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 220 | if (WasReset()) { |
| 221 | LOG(ERROR, "WPILib reset, restarting\n"); |
| 222 | arm_.Reset(); |
| 223 | intake_.Reset(); |
| 224 | state_ = UNINITIALIZED; |
| 225 | } |
| 226 | |
| 227 | // Bool to track if we should turn the motors on or not. |
| 228 | bool disable = output == nullptr; |
| 229 | |
| 230 | arm_.Correct(position->shoulder, position->wrist); |
| 231 | intake_.Correct(position->intake); |
| 232 | |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 233 | // There are 2 main zeroing paths, HIGH_ARM_ZERO and LOW_ARM_ZERO. |
| 234 | // |
| 235 | // HIGH_ARM_ZERO works by lifting the arm all the way up so it is clear, |
| 236 | // moving the shooter to be horizontal, moving the intake out, and then moving |
| 237 | // the arm back down. |
| 238 | // |
| 239 | // LOW_ARM_ZERO works by moving the intake out of the way, lifting the arm up, |
| 240 | // leveling the shooter, and then moving back down. |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 241 | |
Diana Vandenberg | e2843c6 | 2016-02-13 17:44:20 -0800 | [diff] [blame] | 242 | if (arm_.error() || intake_.error()) { |
| 243 | state_ = ESTOP; |
| 244 | } |
| 245 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 246 | switch (state_) { |
| 247 | case UNINITIALIZED: |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 248 | // Wait in the uninitialized state until both the arm and intake are |
| 249 | // initialized. |
| 250 | LOG(DEBUG, "Uninitialized, waiting for intake and arm\n"); |
| 251 | if (arm_.initialized() && intake_.initialized()) { |
| 252 | state_ = DISABLED_INITIALIZED; |
| 253 | } |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 254 | disable = true; |
| 255 | break; |
| 256 | |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 257 | case DISABLED_INITIALIZED: |
| 258 | // Wait here until we are either fully zeroed while disabled, or we become |
| 259 | // enabled. At that point, figure out if we should HIGH_ARM_ZERO or |
| 260 | // LOW_ARM_ZERO. |
| 261 | if (disable) { |
| 262 | if (arm_.zeroed() && intake_.zeroed()) { |
| 263 | state_ = SLOW_RUNNING; |
| 264 | } |
| 265 | } else { |
| 266 | if (arm_.shoulder_angle() >= kShoulderMiddleAngle) { |
| 267 | state_ = HIGH_ARM_ZERO_LIFT_ARM; |
| 268 | } else { |
| 269 | state_ = LOW_ARM_ZERO_LOWER_INTAKE; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // Set the goals to where we are now so when we start back up, we don't |
| 274 | // jump. |
| 275 | intake_.ForceGoal(intake_.angle()); |
| 276 | arm_.ForceGoal(arm_.shoulder_angle(), arm_.wrist_angle()); |
| 277 | // Set up the profile to be the zeroing profile. |
| 278 | intake_.AdjustProfile(0.5, 10); |
| 279 | arm_.AdjustProfile(0.5, 10, 0.5, 10); |
| 280 | |
| 281 | // We are not ready to start doing anything yet. |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 282 | disable = true; |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 283 | break; |
| 284 | |
| 285 | case HIGH_ARM_ZERO_LIFT_ARM: |
| 286 | if (disable) { |
| 287 | state_ = DISABLED_INITIALIZED; |
| 288 | } else { |
| 289 | // Raise the shoulder up out of the way. |
| 290 | arm_.set_unprofiled_goal(kShoulderUpAngle, arm_.wrist_angle()); |
| 291 | if (IsArmNear(kLooseTolerance)) { |
| 292 | // Close enough, start the next move. |
| 293 | state_ = HIGH_ARM_ZERO_LEVEL_SHOOTER; |
| 294 | } |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 295 | } |
| 296 | break; |
| 297 | |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 298 | case HIGH_ARM_ZERO_LEVEL_SHOOTER: |
| 299 | if (disable) { |
| 300 | state_ = DISABLED_INITIALIZED; |
| 301 | } else { |
| 302 | // Move the shooter to be level. |
| 303 | arm_.set_unprofiled_goal(kShoulderUpAngle, 0.0); |
| 304 | |
| 305 | if (IsArmNear(kLooseTolerance)) { |
| 306 | // Close enough, start the next move. |
| 307 | state_ = HIGH_ARM_ZERO_MOVE_INTAKE_OUT; |
| 308 | } |
| 309 | } |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 310 | break; |
| 311 | |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 312 | case HIGH_ARM_ZERO_MOVE_INTAKE_OUT: |
| 313 | if (disable) { |
| 314 | state_ = DISABLED_INITIALIZED; |
| 315 | } else { |
| 316 | // If we were just asked to move the intake, make sure it moves far |
| 317 | // enough. |
| 318 | if (last_state_ != HIGH_ARM_ZERO_MOVE_INTAKE_OUT) { |
| 319 | intake_.set_unprofiled_goal( |
| 320 | MoveButKeepBelow(kIntakeUpperClear, intake_.angle(), |
| 321 | kIntakeEncoderIndexDifference * 2.5)); |
| 322 | } |
| 323 | |
| 324 | if (IsIntakeNear(kLooseTolerance)) { |
| 325 | // Close enough, start the next move. |
| 326 | state_ = HIGH_ARM_ZERO_LOWER_ARM; |
| 327 | } |
| 328 | } |
| 329 | break; |
| 330 | |
| 331 | case HIGH_ARM_ZERO_LOWER_ARM: |
| 332 | if (disable) { |
| 333 | state_ = DISABLED_INITIALIZED; |
| 334 | } else { |
| 335 | // Land the shooter in the belly-pan. It should be zeroed by the time |
| 336 | // it gets there. If not, just estop. |
| 337 | arm_.set_unprofiled_goal(kShoulderLanded, 0.0); |
| 338 | if (arm_.zeroed() && intake_.zeroed()) { |
| 339 | state_ = RUNNING; |
| 340 | } else if (IsArmNear(kLooseTolerance)) { |
| 341 | LOG(ERROR, |
| 342 | "Failed to zero while executing the HIGH_ARM_ZERO sequence. " |
| 343 | "Arm: %d Intake %d\n", |
| 344 | arm_.zeroed(), intake_.zeroed()); |
| 345 | state_ = ESTOP; |
| 346 | } |
| 347 | } |
| 348 | break; |
| 349 | |
| 350 | case LOW_ARM_ZERO_LOWER_INTAKE: |
| 351 | if (disable) { |
| 352 | state_ = DISABLED_INITIALIZED; |
| 353 | } else { |
| 354 | // Move the intake down out of the way of the arm. Make sure to move it |
| 355 | // far enough to zero. |
| 356 | if (last_state_ != LOW_ARM_ZERO_LOWER_INTAKE) { |
| 357 | intake_.set_unprofiled_goal( |
| 358 | MoveButKeepBelow(kIntakeLowerClear, intake_.angle(), |
| 359 | kIntakeEncoderIndexDifference * 2.5)); |
| 360 | } |
| 361 | if (IsIntakeNear(kLooseTolerance)) { |
| 362 | if (::std::abs(arm_.wrist_angle()) < kWristAlmostLevel) { |
| 363 | state_ = LOW_ARM_ZERO_MAYBE_LEVEL_SHOOTER; |
| 364 | } else { |
| 365 | state_ = LOW_ARM_ZERO_LIFT_SHOULDER; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | break; |
| 370 | |
| 371 | case LOW_ARM_ZERO_MAYBE_LEVEL_SHOOTER: |
| 372 | if (disable) { |
| 373 | state_ = DISABLED_INITIALIZED; |
| 374 | } else { |
| 375 | // If we are supposed to level the shooter, set it to level, and wait |
| 376 | // until it is very close to level. |
| 377 | arm_.set_unprofiled_goal(arm_.unprofiled_goal(0, 0), 0.0); |
| 378 | if (IsArmNear(kLooseTolerance, kTightTolerance)) { |
| 379 | state_ = LOW_ARM_ZERO_LIFT_SHOULDER; |
| 380 | } |
| 381 | } |
| 382 | break; |
| 383 | |
| 384 | case LOW_ARM_ZERO_LIFT_SHOULDER: |
| 385 | if (disable) { |
| 386 | state_ = DISABLED_INITIALIZED; |
| 387 | } else { |
| 388 | // Decide where to move to. We need to move far enough to see an index |
| 389 | // pulse, but must also get high enough that we can safely level the |
| 390 | // shooter. |
| 391 | if (last_state_ != LOW_ARM_ZERO_LIFT_SHOULDER) { |
| 392 | arm_.set_unprofiled_goal( |
| 393 | MoveButKeepAbove(kShoulderWristClearAngle, arm_.shoulder_angle(), |
| 394 | ::std::max(kWristEncoderIndexDifference, |
| 395 | kShoulderEncoderIndexDifference) * |
| 396 | 2.5), |
| 397 | arm_.unprofiled_goal(2, 0)); |
| 398 | } |
| 399 | |
| 400 | // Wait until we are level and then go for it. |
| 401 | if (IsArmNear(kLooseTolerance)) { |
| 402 | state_ = LOW_ARM_ZERO_LEVEL_SHOOTER; |
| 403 | } |
| 404 | } |
| 405 | break; |
| 406 | |
| 407 | case LOW_ARM_ZERO_LEVEL_SHOOTER: |
| 408 | if (disable) { |
| 409 | state_ = DISABLED_INITIALIZED; |
| 410 | } else { |
| 411 | // Move the shooter level (and keep the same height). We don't want to |
| 412 | // got to RUNNING until we are completely level so that we don't |
| 413 | // give control back in a weird case where we might crash. |
| 414 | arm_.set_unprofiled_goal(arm_.unprofiled_goal(0, 0), 0.0); |
| 415 | if (IsArmNear(kLooseTolerance)) { |
| 416 | if (arm_.zeroed() && intake_.zeroed()) { |
| 417 | state_ = RUNNING; |
| 418 | } else { |
| 419 | LOG(ERROR, |
| 420 | "Failed to zero while executing the LOW_ARM_ZERO sequence. " |
| 421 | "Arm: %d Intake %d\n", |
| 422 | arm_.zeroed(), intake_.zeroed()); |
| 423 | state_ = ESTOP; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | break; |
| 428 | |
| 429 | case SLOW_RUNNING: |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 430 | case RUNNING: |
Austin Schuh | 829fe57 | 2016-02-14 21:41:16 -0800 | [diff] [blame] | 431 | if (disable) { |
| 432 | // TODO(austin): Enter SLOW_RUNNING if we are collided. |
| 433 | |
| 434 | // If we are disabled, reset the profile to the current position. |
| 435 | intake_.ForceGoal(intake_.angle()); |
| 436 | arm_.ForceGoal(arm_.shoulder_angle(), arm_.wrist_angle()); |
| 437 | } else { |
| 438 | if (state_ == SLOW_RUNNING) { |
| 439 | // TODO(austin): Exit SLOW_RUNNING if we are not collided. |
| 440 | LOG(ERROR, "Need to transition on non-collided, not all the time.\n"); |
| 441 | state_ = RUNNING; |
| 442 | } |
| 443 | } |
| 444 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 445 | if (unsafe_goal) { |
| 446 | arm_.AdjustProfile(unsafe_goal->max_angular_velocity_shoulder, |
Austin Schuh | 829fe57 | 2016-02-14 21:41:16 -0800 | [diff] [blame] | 447 | unsafe_goal->max_angular_acceleration_shoulder, |
| 448 | unsafe_goal->max_angular_velocity_wrist, |
| 449 | unsafe_goal->max_angular_acceleration_wrist); |
Austin Schuh | e7e4089 | 2016-02-15 18:21:04 -0800 | [diff] [blame] | 450 | intake_.AdjustProfile(unsafe_goal->max_angular_velocity_intake, |
Austin Schuh | 829fe57 | 2016-02-14 21:41:16 -0800 | [diff] [blame] | 451 | unsafe_goal->max_angular_acceleration_intake); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 452 | |
Philipp Schrader | 0119eb1 | 2016-02-15 18:16:21 +0000 | [diff] [blame] | 453 | if (collision_avoidance_enabled()) { |
| 454 | collision_avoidance_.UpdateGoal(unsafe_goal->angle_shoulder, |
| 455 | unsafe_goal->angle_wrist, |
| 456 | unsafe_goal->angle_intake); |
| 457 | } else { |
| 458 | arm_.set_unprofiled_goal(unsafe_goal->angle_shoulder, |
| 459 | unsafe_goal->angle_wrist); |
| 460 | intake_.set_unprofiled_goal(unsafe_goal->angle_intake); |
| 461 | } |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 464 | // ESTOP if we hit any of the limits. It is safe(ish) to hit the limits |
| 465 | // while zeroing since we use such low power. |
| 466 | if (state_ == RUNNING || state_ == SLOW_RUNNING) { |
| 467 | // ESTOP if we hit the hard limits. |
| 468 | if ((arm_.CheckHardLimits() || intake_.CheckHardLimits()) && output) { |
| 469 | state_ = ESTOP; |
| 470 | } |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 471 | } |
| 472 | break; |
| 473 | |
| 474 | case ESTOP: |
| 475 | LOG(ERROR, "Estop\n"); |
| 476 | disable = true; |
| 477 | break; |
| 478 | } |
| 479 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 480 | // Set the voltage limits. |
| 481 | const double max_voltage = state_ == RUNNING ? 12.0 : kZeroingVoltage; |
| 482 | arm_.set_max_voltage(max_voltage, max_voltage); |
| 483 | intake_.set_max_voltage(max_voltage); |
| 484 | |
| 485 | // Calculate the loops for a cycle. |
| 486 | arm_.Update(disable); |
| 487 | intake_.Update(disable); |
| 488 | |
| 489 | // Write out all the voltages. |
| 490 | if (output) { |
| 491 | output->voltage_intake = intake_.intake_voltage(); |
| 492 | output->voltage_shoulder = arm_.shoulder_voltage(); |
| 493 | output->voltage_wrist = arm_.wrist_voltage(); |
Comran Morshed | f4cd764 | 2016-02-15 20:40:49 +0000 | [diff] [blame] | 494 | |
| 495 | // Logic to run our rollers on the intake. |
| 496 | output->voltage_rollers = 0.0; |
| 497 | if (unsafe_goal) { |
| 498 | output->voltage_rollers = |
| 499 | ::std::max(-8.0, ::std::min(8.0, unsafe_goal->voltage_rollers)); |
| 500 | } |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | // Save debug/internal state. |
| 504 | // TODO(austin): Save the voltage errors. |
| 505 | status->zeroed = state_ == RUNNING; |
| 506 | |
| 507 | status->shoulder.angle = arm_.X_hat(0, 0); |
| 508 | status->shoulder.angular_velocity = arm_.X_hat(1, 0); |
| 509 | status->shoulder.goal_angle = arm_.goal(0, 0); |
| 510 | status->shoulder.goal_angular_velocity = arm_.goal(1, 0); |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 511 | status->shoulder.unprofiled_goal_angle = arm_.unprofiled_goal(0, 0); |
| 512 | status->shoulder.unprofiled_goal_angular_velocity = |
| 513 | arm_.unprofiled_goal(1, 0); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 514 | status->shoulder.estimator_state = arm_.ShoulderEstimatorState(); |
| 515 | |
| 516 | status->wrist.angle = arm_.X_hat(2, 0); |
| 517 | status->wrist.angular_velocity = arm_.X_hat(3, 0); |
| 518 | status->wrist.goal_angle = arm_.goal(2, 0); |
| 519 | status->wrist.goal_angular_velocity = arm_.goal(3, 0); |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 520 | status->wrist.unprofiled_goal_angle = arm_.unprofiled_goal(2, 0); |
| 521 | status->wrist.unprofiled_goal_angular_velocity = arm_.unprofiled_goal(3, 0); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 522 | status->wrist.estimator_state = arm_.WristEstimatorState(); |
| 523 | |
| 524 | status->intake.angle = intake_.X_hat(0, 0); |
| 525 | status->intake.angular_velocity = intake_.X_hat(1, 0); |
| 526 | status->intake.goal_angle = intake_.goal(0, 0); |
| 527 | status->intake.goal_angular_velocity = intake_.goal(1, 0); |
Austin Schuh | 39fd610 | 2016-02-13 22:59:48 -0800 | [diff] [blame] | 528 | status->intake.unprofiled_goal_angle = intake_.unprofiled_goal(0, 0); |
| 529 | status->intake.unprofiled_goal_angular_velocity = |
| 530 | intake_.unprofiled_goal(1, 0); |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 531 | status->intake.estimator_state = intake_.IntakeEstimatorState(); |
| 532 | |
| 533 | status->estopped = (state_ == ESTOP); |
| 534 | |
| 535 | status->state = state_; |
| 536 | |
Adam Snaider | 0677972 | 2016-02-14 15:26:22 -0800 | [diff] [blame] | 537 | last_state_ = state_before_switch; |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Philipp Schrader | 0119eb1 | 2016-02-15 18:16:21 +0000 | [diff] [blame] | 540 | constexpr double Superstructure::kShoulderMiddleAngle; |
| 541 | constexpr double Superstructure::kLooseTolerance; |
| 542 | constexpr double Superstructure::kIntakeUpperClear; |
| 543 | constexpr double Superstructure::kIntakeLowerClear; |
| 544 | constexpr double Superstructure::kShoulderUpAngle; |
| 545 | constexpr double Superstructure::kShoulderLanded; |
| 546 | constexpr double Superstructure::kTightTolerance; |
| 547 | constexpr double Superstructure::kWristAlmostLevel; |
| 548 | constexpr double Superstructure::kShoulderWristClearAngle; |
| 549 | |
Austin Schuh | 2fc10fa | 2016-02-08 00:44:34 -0800 | [diff] [blame] | 550 | } // namespace superstructure |
Comran Morshed | 25f81a0 | 2016-01-23 13:40:10 +0000 | [diff] [blame] | 551 | } // namespace control_loops |
| 552 | } // namespace y2016 |