Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 1 | #include "y2020/control_loops/superstructure/shooter/shooter.h" |
| 2 | |
| 3 | #include <chrono> |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 4 | #include <cmath> |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 5 | |
| 6 | #include "aos/logging/logging.h" |
| 7 | #include "y2020/control_loops/superstructure/accelerator/accelerator_plant.h" |
| 8 | #include "y2020/control_loops/superstructure/finisher/finisher_plant.h" |
| 9 | |
| 10 | namespace y2020 { |
| 11 | namespace control_loops { |
| 12 | namespace superstructure { |
| 13 | namespace shooter { |
| 14 | |
| 15 | Shooter::Shooter() |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 16 | : finisher_( |
| 17 | finisher::MakeIntegralFinisherLoop(), finisher::kBemf, |
| 18 | // There are 2 motors. So the current limit per motor is going to be |
| 19 | // using resistance * 2 to un-parallel the motor resistances. |
| 20 | finisher::kResistance * 2.0), |
Austin Schuh | e8ca06a | 2020-03-07 22:27:39 -0800 | [diff] [blame] | 21 | accelerator_left_(accelerator::MakeIntegralAcceleratorLoop(), |
| 22 | accelerator::kBemf, accelerator::kResistance), |
| 23 | accelerator_right_(accelerator::MakeIntegralAcceleratorLoop(), |
| 24 | accelerator::kBemf, accelerator::kResistance) {} |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 25 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 26 | bool Shooter::UpToSpeed(const ShooterGoal *goal) { |
milind-u | 0beb7dc | 2021-10-16 19:31:33 -0700 | [diff] [blame^] | 27 | finisher_ready_ = |
| 28 | (std::abs(goal->velocity_finisher() - finisher_.avg_angular_velocity()) < |
| 29 | kVelocityToleranceFinisher && |
| 30 | std::abs(goal->velocity_finisher() - finisher_.velocity()) < |
| 31 | kVelocityToleranceFinisher); |
| 32 | accelerator_ready_ = |
| 33 | (std::abs(goal->velocity_accelerator() - |
| 34 | accelerator_left_.avg_angular_velocity()) < |
| 35 | kVelocityToleranceAccelerator && |
| 36 | std::abs(goal->velocity_accelerator() - |
| 37 | accelerator_right_.avg_angular_velocity()) < |
| 38 | kVelocityToleranceAccelerator && |
| 39 | std::abs(goal->velocity_accelerator() - accelerator_left_.velocity()) < |
| 40 | kVelocityToleranceAccelerator && |
| 41 | std::abs(goal->velocity_accelerator() - accelerator_right_.velocity()) < |
| 42 | kVelocityToleranceAccelerator); |
| 43 | return (finisher_ready_ && accelerator_ready_); |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 46 | flatbuffers::Offset<ShooterStatus> Shooter::RunIteration( |
| 47 | const ShooterGoal *goal, const ShooterPosition *position, |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 48 | flatbuffers::FlatBufferBuilder *fbb, OutputT *output, |
| 49 | const aos::monotonic_clock::time_point position_timestamp) { |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 50 | const double last_finisher_velocity = finisher_.velocity(); |
| 51 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 52 | // Update position, output, and status for our two shooter sides. |
| 53 | finisher_.set_position(position->theta_finisher(), position_timestamp); |
| 54 | accelerator_left_.set_position(position->theta_accelerator_left(), |
| 55 | position_timestamp); |
| 56 | accelerator_right_.set_position(position->theta_accelerator_right(), |
| 57 | position_timestamp); |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 58 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 59 | // Update goal. |
| 60 | if (goal) { |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 61 | if (std::abs(goal->velocity_finisher() - finisher_goal()) >= |
milind-u | 78f21b7 | 2021-10-10 13:47:28 -0700 | [diff] [blame] | 62 | kVelocityToleranceFinisher) { |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 63 | finisher_goal_changed_ = true; |
| 64 | last_finisher_velocity_max_ = 0.0; |
| 65 | } |
| 66 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 67 | finisher_.set_goal(goal->velocity_finisher()); |
| 68 | accelerator_left_.set_goal(goal->velocity_accelerator()); |
| 69 | accelerator_right_.set_goal(goal->velocity_accelerator()); |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 70 | } |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 71 | |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 72 | finisher_.Update(output == nullptr); |
| 73 | accelerator_left_.Update(output == nullptr); |
| 74 | accelerator_right_.Update(output == nullptr); |
| 75 | |
| 76 | if (goal) { |
milind-u | 78f21b7 | 2021-10-10 13:47:28 -0700 | [diff] [blame] | 77 | if (UpToSpeed(goal) && |
| 78 | goal->velocity_finisher() > kVelocityToleranceFinisher && |
| 79 | goal->velocity_accelerator() > kVelocityToleranceAccelerator) { |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 80 | ready_ = true; |
| 81 | } else { |
| 82 | ready_ = false; |
milind-u | 0beb7dc | 2021-10-16 19:31:33 -0700 | [diff] [blame^] | 83 | finisher_ready_ = false; |
| 84 | accelerator_ready_ = false; |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 88 | flatbuffers::Offset<FlywheelControllerStatus> finisher_status_offset = |
| 89 | finisher_.SetStatus(fbb); |
| 90 | flatbuffers::Offset<FlywheelControllerStatus> accelerator_left_status_offset = |
| 91 | accelerator_left_.SetStatus(fbb); |
| 92 | flatbuffers::Offset<FlywheelControllerStatus> |
| 93 | accelerator_right_status_offset = accelerator_right_.SetStatus(fbb); |
| 94 | |
| 95 | ShooterStatusBuilder status_builder(*fbb); |
| 96 | |
| 97 | status_builder.add_finisher(finisher_status_offset); |
| 98 | status_builder.add_accelerator_left(accelerator_left_status_offset); |
| 99 | status_builder.add_accelerator_right(accelerator_right_status_offset); |
Austin Schuh | 5c40ea4 | 2021-09-26 13:28:03 -0700 | [diff] [blame] | 100 | status_builder.add_ready(ready()); |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 101 | |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 102 | if (finisher_goal_changed_) { |
| 103 | // If we have caught up to the new goal, we can start detecting if a ball |
| 104 | // was shot. |
milind-u | 78f21b7 | 2021-10-10 13:47:28 -0700 | [diff] [blame] | 105 | finisher_goal_changed_ = (std::abs(finisher_.velocity() - finisher_goal()) > |
| 106 | kVelocityToleranceFinisher); |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (!finisher_goal_changed_) { |
| 110 | const bool finisher_was_accelerating = finisher_accelerating_; |
| 111 | finisher_accelerating_ = (finisher_.velocity() > last_finisher_velocity); |
| 112 | if (finisher_was_accelerating && !finisher_accelerating_) { |
| 113 | last_finisher_velocity_max_ = std::min( |
| 114 | last_finisher_velocity, static_cast<double>(finisher_goal())); |
| 115 | } |
| 116 | |
| 117 | const double finisher_velocity_dip = |
| 118 | last_finisher_velocity_max_ - finisher_.velocity(); |
| 119 | |
milind-u | 78f21b7 | 2021-10-10 13:47:28 -0700 | [diff] [blame] | 120 | if (finisher_velocity_dip < kVelocityToleranceFinisher && |
| 121 | ball_in_finisher_) { |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 122 | // If we detected a ball in the flywheel and now the angular velocity has |
| 123 | // come back up close to the last local maximum or is greater than it, the |
| 124 | // ball has been shot. |
| 125 | balls_shot_++; |
| 126 | ball_in_finisher_ = false; |
milind-u | 78f21b7 | 2021-10-10 13:47:28 -0700 | [diff] [blame] | 127 | } else if (!ball_in_finisher_ && |
| 128 | (finisher_goal() > kVelocityToleranceFinisher)) { |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 129 | // There is probably a ball in the flywheel if the angular |
| 130 | // velocity is atleast kMinVelocityErrorWithBall less than the last local |
| 131 | // maximum. |
| 132 | ball_in_finisher_ = |
| 133 | (finisher_velocity_dip >= kMinFinisherVelocityDipWithBall); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | status_builder.add_balls_shot(balls_shot_); |
| 138 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 139 | if (output) { |
| 140 | output->finisher_voltage = finisher_.voltage(); |
| 141 | output->accelerator_left_voltage = accelerator_left_.voltage(); |
| 142 | output->accelerator_right_voltage = accelerator_right_.voltage(); |
| 143 | } |
| 144 | |
| 145 | return status_builder.Finish(); |
| 146 | } |
| 147 | |
| 148 | } // namespace shooter |
| 149 | } // namespace superstructure |
| 150 | } // namespace control_loops |
| 151 | } // namespace y2020 |