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