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