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