Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 1 | #include "y2016_bot3/control_loops/intake/intake.h" |
| 2 | #include "y2016_bot3/control_loops/intake/intake_controls.h" |
| 3 | |
| 4 | #include "aos/common/commonmath.h" |
| 5 | #include "aos/common/controls/control_loops.q.h" |
| 6 | #include "aos/common/logging/logging.h" |
| 7 | |
| 8 | #include "y2016_bot3/control_loops/intake/integral_intake_plant.h" |
| 9 | #include "y2016_bot3/queues/ball_detector.q.h" |
| 10 | |
| 11 | namespace y2016_bot3 { |
Adam Snaider | 5b7c162 | 2017-01-04 21:39:34 -0800 | [diff] [blame] | 12 | namespace constants { |
| 13 | constexpr double IntakeZero::pot_offset; |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame^] | 14 | constexpr ::frc971::constants::PotAndIndexPulseZeroingConstants |
| 15 | IntakeZero::zeroing; |
Adam Snaider | 5b7c162 | 2017-01-04 21:39:34 -0800 | [diff] [blame] | 16 | } // namespace constants |
| 17 | |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 18 | namespace control_loops { |
| 19 | namespace intake { |
| 20 | |
| 21 | namespace { |
| 22 | // The maximum voltage the intake roller will be allowed to use. |
| 23 | constexpr float kMaxIntakeTopVoltage = 12.0; |
| 24 | constexpr float kMaxIntakeBottomVoltage = 12.0; |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 25 | constexpr float kMaxIntakeRollersVoltage = 12.0; |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 26 | } |
| 27 | // namespace |
| 28 | |
| 29 | void LimitChecker::UpdateGoal(double intake_angle_goal) { |
| 30 | intake_->set_unprofiled_goal(intake_angle_goal); |
| 31 | } |
| 32 | |
| 33 | Intake::Intake(control_loops::IntakeQueue *intake_queue) |
| 34 | : aos::controls::ControlLoop<control_loops::IntakeQueue>(intake_queue), |
| 35 | limit_checker_(&intake_) {} |
| 36 | bool Intake::IsIntakeNear(double tolerance) { |
| 37 | return ((intake_.unprofiled_goal() - intake_.X_hat()) |
| 38 | .block<2, 1>(0, 0) |
| 39 | .lpNorm<Eigen::Infinity>() < tolerance); |
| 40 | } |
| 41 | |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 42 | void Intake::RunIteration(const control_loops::IntakeQueue::Goal *unsafe_goal, |
| 43 | const control_loops::IntakeQueue::Position *position, |
| 44 | control_loops::IntakeQueue::Output *output, |
| 45 | control_loops::IntakeQueue::Status *status) { |
| 46 | const State state_before_switch = state_; |
| 47 | if (WasReset()) { |
| 48 | LOG(ERROR, "WPILib reset, restarting\n"); |
| 49 | intake_.Reset(); |
| 50 | state_ = UNINITIALIZED; |
| 51 | } |
| 52 | |
| 53 | // Bool to track if we should turn the motors on or not. |
| 54 | bool disable = output == nullptr; |
| 55 | |
| 56 | intake_.Correct(position->intake); |
| 57 | |
| 58 | // There are 2 main zeroing paths, HIGH_ARM_ZERO and LOW_ARM_ZERO. |
| 59 | // |
| 60 | // HIGH_ARM_ZERO works by lifting the arm all the way up so it is clear, |
| 61 | // moving the shooter to be horizontal, moving the intake out, and then moving |
| 62 | // the arm back down. |
| 63 | // |
| 64 | // LOW_ARM_ZERO works by moving the intake out of the way, lifting the arm up, |
| 65 | // leveling the shooter, and then moving back down. |
| 66 | |
| 67 | if (intake_.error()) { |
| 68 | state_ = ESTOP; |
| 69 | } |
| 70 | |
| 71 | switch (state_) { |
| 72 | case UNINITIALIZED: |
| 73 | // Wait in the uninitialized state until intake is initialized. |
| 74 | LOG(DEBUG, "Uninitialized, waiting for intake\n"); |
| 75 | if (intake_.initialized()) { |
| 76 | state_ = DISABLED_INITIALIZED; |
| 77 | } |
| 78 | disable = true; |
| 79 | break; |
| 80 | |
| 81 | case DISABLED_INITIALIZED: |
| 82 | // Wait here until we are either fully zeroed while disabled, or we become |
| 83 | // enabled. |
| 84 | if (disable) { |
| 85 | if (intake_.zeroed()) { |
| 86 | state_ = SLOW_RUNNING; |
| 87 | } |
| 88 | } else { |
| 89 | if (intake_.angle() <= kIntakeMiddleAngle) { |
| 90 | state_ = ZERO_LIFT_INTAKE; |
| 91 | } else { |
| 92 | state_ = ZERO_LOWER_INTAKE; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Set the goals to where we are now so when we start back up, we don't |
| 97 | // jump. |
| 98 | intake_.ForceGoal(intake_.angle()); |
| 99 | // Set up the profile to be the zeroing profile. |
| 100 | intake_.AdjustProfile(0.5, 10); |
| 101 | |
| 102 | // We are not ready to start doing anything yet. |
| 103 | disable = true; |
| 104 | break; |
| 105 | |
| 106 | case ZERO_LOWER_INTAKE: |
| 107 | if (disable) { |
| 108 | state_ = DISABLED_INITIALIZED; |
| 109 | } else { |
| 110 | intake_.set_unprofiled_goal(kIntakeDownAngle); |
| 111 | |
| 112 | if (IsIntakeNear(kLooseTolerance)) { |
| 113 | // Close enough, start the next move. |
| 114 | state_ = RUNNING; |
| 115 | } |
| 116 | } |
| 117 | break; |
| 118 | |
| 119 | case ZERO_LIFT_INTAKE: |
| 120 | if (disable) { |
| 121 | state_ = DISABLED_INITIALIZED; |
| 122 | } else { |
| 123 | intake_.set_unprofiled_goal(kIntakeUpAngle); |
| 124 | |
| 125 | if (IsIntakeNear(kLooseTolerance)) { |
| 126 | // Close enough, start the next move. |
| 127 | state_ = RUNNING; |
| 128 | } |
| 129 | } |
| 130 | break; |
| 131 | |
| 132 | // These 4 cases are very similar. |
| 133 | case SLOW_RUNNING: |
| 134 | case RUNNING: { |
| 135 | if (disable) { |
| 136 | // If we are disabled, go to slow running if we are collided. |
| 137 | // Reset the profile to the current position so it moves well from here. |
| 138 | intake_.ForceGoal(intake_.angle()); |
| 139 | } |
| 140 | |
| 141 | double requested_intake = M_PI / 2.0; |
| 142 | |
| 143 | if (unsafe_goal) { |
| 144 | intake_.AdjustProfile(unsafe_goal->max_angular_velocity_intake, |
| 145 | unsafe_goal->max_angular_acceleration_intake); |
| 146 | |
| 147 | requested_intake = unsafe_goal->angle_intake; |
| 148 | } |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 149 | // Push the request out to the hardware. |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 150 | limit_checker_.UpdateGoal(requested_intake); |
| 151 | |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 152 | // ESTOP if we hit the hard limits. |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 153 | if (intake_.CheckHardLimits() && output) { |
| 154 | state_ = ESTOP; |
| 155 | } |
| 156 | } break; |
| 157 | |
| 158 | case ESTOP: |
| 159 | LOG(ERROR, "Estop\n"); |
| 160 | disable = true; |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | // Set the voltage limits. |
| 165 | const double max_voltage = |
| 166 | (state_ == RUNNING) ? kOperatingVoltage : kZeroingVoltage; |
| 167 | |
| 168 | intake_.set_max_voltage(max_voltage); |
| 169 | |
| 170 | // Calculate the loops for a cycle. |
| 171 | { |
| 172 | Eigen::Matrix<double, 3, 1> error = intake_.controller().error(); |
| 173 | status->intake.position_power = intake_.controller().K(0, 0) * error(0, 0); |
| 174 | status->intake.velocity_power = intake_.controller().K(0, 1) * error(1, 0); |
| 175 | } |
| 176 | |
| 177 | intake_.Update(disable); |
| 178 | |
| 179 | // Write out all the voltages. |
| 180 | if (output) { |
| 181 | output->voltage_intake = intake_.intake_voltage(); |
| 182 | |
| 183 | output->voltage_top_rollers = 0.0; |
| 184 | output->voltage_bottom_rollers = 0.0; |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 185 | output->voltage_intake_rollers = 0.0; |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 186 | |
| 187 | if (unsafe_goal) { |
| 188 | // Ball detector lights. |
| 189 | ::y2016_bot3::sensors::ball_detector.FetchLatest(); |
| 190 | bool ball_detected = false; |
| 191 | if (::y2016_bot3::sensors::ball_detector.get()) { |
| 192 | ball_detected = ::y2016_bot3::sensors::ball_detector->voltage > 2.5; |
| 193 | } |
| 194 | |
| 195 | // Intake. |
| 196 | if (unsafe_goal->force_intake || !ball_detected) { |
| 197 | output->voltage_top_rollers = ::std::max( |
| 198 | -kMaxIntakeTopVoltage, |
| 199 | ::std::min(unsafe_goal->voltage_top_rollers, kMaxIntakeTopVoltage)); |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 200 | output->voltage_intake_rollers = |
| 201 | ::std::max(-kMaxIntakeRollersVoltage, |
| 202 | ::std::min(unsafe_goal->voltage_intake_rollers, |
| 203 | kMaxIntakeRollersVoltage)); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 204 | output->voltage_bottom_rollers = |
| 205 | ::std::max(-kMaxIntakeBottomVoltage, |
| 206 | ::std::min(unsafe_goal->voltage_bottom_rollers, |
| 207 | kMaxIntakeBottomVoltage)); |
| 208 | } else { |
| 209 | output->voltage_top_rollers = 0.0; |
| 210 | output->voltage_bottom_rollers = 0.0; |
| 211 | } |
| 212 | |
| 213 | // Traverse. |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 214 | output->traverse_down = unsafe_goal->traverse_down; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Save debug/internal state. |
| 219 | status->zeroed = intake_.zeroed(); |
| 220 | |
| 221 | status->intake.angle = intake_.X_hat(0, 0); |
| 222 | status->intake.angular_velocity = intake_.X_hat(1, 0); |
| 223 | status->intake.goal_angle = intake_.goal(0, 0); |
| 224 | status->intake.goal_angular_velocity = intake_.goal(1, 0); |
| 225 | status->intake.unprofiled_goal_angle = intake_.unprofiled_goal(0, 0); |
| 226 | status->intake.unprofiled_goal_angular_velocity = |
| 227 | intake_.unprofiled_goal(1, 0); |
| 228 | status->intake.calculated_velocity = |
| 229 | (intake_.angle() - last_intake_angle_) / 0.005; |
| 230 | status->intake.voltage_error = intake_.X_hat(2, 0); |
| 231 | status->intake.estimator_state = intake_.IntakeEstimatorState(); |
| 232 | status->intake.feedforwards_power = intake_.controller().ff_U(0, 0); |
| 233 | |
| 234 | last_intake_angle_ = intake_.angle(); |
| 235 | |
| 236 | status->estopped = (state_ == ESTOP); |
| 237 | |
| 238 | status->state = state_; |
| 239 | |
| 240 | last_state_ = state_before_switch; |
| 241 | } |
| 242 | |
| 243 | constexpr double Intake::kZeroingVoltage; |
| 244 | constexpr double Intake::kOperatingVoltage; |
| 245 | constexpr double Intake::kLooseTolerance; |
| 246 | constexpr double Intake::kTightTolerance; |
| 247 | constexpr double Intake::kIntakeUpAngle; |
| 248 | constexpr double Intake::kIntakeMiddleAngle; |
| 249 | constexpr double Intake::kIntakeDownAngle; |
| 250 | |
| 251 | } // namespace intake |
| 252 | } // namespace control_loops |
| 253 | } // namespace y2016_bot3 |