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