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