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