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 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 12 | namespace y2020::control_loops::superstructure::shooter { |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 13 | |
| 14 | Shooter::Shooter() |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 15 | : finisher_( |
| 16 | finisher::MakeIntegralFinisherLoop(), finisher::kBemf, |
| 17 | // There are 2 motors. So the current limit per motor is going to be |
| 18 | // using resistance * 2 to un-parallel the motor resistances. |
| 19 | finisher::kResistance * 2.0), |
Austin Schuh | e8ca06a | 2020-03-07 22:27:39 -0800 | [diff] [blame] | 20 | accelerator_left_(accelerator::MakeIntegralAcceleratorLoop(), |
| 21 | accelerator::kBemf, accelerator::kResistance), |
| 22 | accelerator_right_(accelerator::MakeIntegralAcceleratorLoop(), |
| 23 | accelerator::kBemf, accelerator::kResistance) {} |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 24 | |
Austin Schuh | 01d81c3 | 2021-11-06 22:59:56 -0700 | [diff] [blame] | 25 | void Shooter::UpToSpeed(const ShooterGoal *goal) { |
milind-u | 0beb7dc | 2021-10-16 19:31:33 -0700 | [diff] [blame] | 26 | finisher_ready_ = |
| 27 | (std::abs(goal->velocity_finisher() - finisher_.avg_angular_velocity()) < |
| 28 | kVelocityToleranceFinisher && |
| 29 | std::abs(goal->velocity_finisher() - finisher_.velocity()) < |
milind-u | 2450f1a | 2021-11-06 19:01:49 -0700 | [diff] [blame] | 30 | kVelocityToleranceFinisher && |
| 31 | goal->velocity_finisher() > kVelocityToleranceFinisher); |
milind-u | 0beb7dc | 2021-10-16 19:31:33 -0700 | [diff] [blame] | 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()) < |
milind-u | 2450f1a | 2021-11-06 19:01:49 -0700 | [diff] [blame] | 42 | kVelocityToleranceAccelerator && |
| 43 | goal->velocity_accelerator() > kVelocityToleranceAccelerator); |
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) { |
Austin Schuh | 01d81c3 | 2021-11-06 22:59:56 -0700 | [diff] [blame] | 77 | UpToSpeed(goal); |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Maxwell Henderson | 3424299 | 2024-01-07 12:39:11 -0800 | [diff] [blame] | 80 | flatbuffers::Offset<frc971::control_loops::flywheel::FlywheelControllerStatus> |
| 81 | finisher_status_offset = finisher_.SetStatus(fbb); |
| 82 | flatbuffers::Offset<frc971::control_loops::flywheel::FlywheelControllerStatus> |
| 83 | accelerator_left_status_offset = accelerator_left_.SetStatus(fbb); |
| 84 | flatbuffers::Offset<frc971::control_loops::flywheel::FlywheelControllerStatus> |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 85 | accelerator_right_status_offset = accelerator_right_.SetStatus(fbb); |
| 86 | |
| 87 | ShooterStatusBuilder status_builder(*fbb); |
| 88 | |
| 89 | status_builder.add_finisher(finisher_status_offset); |
| 90 | status_builder.add_accelerator_left(accelerator_left_status_offset); |
| 91 | status_builder.add_accelerator_right(accelerator_right_status_offset); |
Austin Schuh | 5c40ea4 | 2021-09-26 13:28:03 -0700 | [diff] [blame] | 92 | status_builder.add_ready(ready()); |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 93 | |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 94 | if (finisher_goal_changed_) { |
| 95 | // If we have caught up to the new goal, we can start detecting if a ball |
| 96 | // was shot. |
milind-u | 78f21b7 | 2021-10-10 13:47:28 -0700 | [diff] [blame] | 97 | finisher_goal_changed_ = (std::abs(finisher_.velocity() - finisher_goal()) > |
| 98 | kVelocityToleranceFinisher); |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | if (!finisher_goal_changed_) { |
| 102 | const bool finisher_was_accelerating = finisher_accelerating_; |
| 103 | finisher_accelerating_ = (finisher_.velocity() > last_finisher_velocity); |
| 104 | if (finisher_was_accelerating && !finisher_accelerating_) { |
| 105 | last_finisher_velocity_max_ = std::min( |
| 106 | last_finisher_velocity, static_cast<double>(finisher_goal())); |
| 107 | } |
| 108 | |
| 109 | const double finisher_velocity_dip = |
| 110 | last_finisher_velocity_max_ - finisher_.velocity(); |
| 111 | |
milind-u | 78f21b7 | 2021-10-10 13:47:28 -0700 | [diff] [blame] | 112 | if (finisher_velocity_dip < kVelocityToleranceFinisher && |
| 113 | ball_in_finisher_) { |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 114 | // If we detected a ball in the flywheel and now the angular velocity has |
| 115 | // come back up close to the last local maximum or is greater than it, the |
| 116 | // ball has been shot. |
| 117 | balls_shot_++; |
Austin Schuh | eb240f6 | 2021-11-07 19:57:06 -0800 | [diff] [blame] | 118 | VLOG(1) << "Shot ball at " << position_timestamp; |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 119 | ball_in_finisher_ = false; |
milind-u | 78f21b7 | 2021-10-10 13:47:28 -0700 | [diff] [blame] | 120 | } else if (!ball_in_finisher_ && |
| 121 | (finisher_goal() > kVelocityToleranceFinisher)) { |
milind-u | 7baf734 | 2021-08-25 18:31:26 -0700 | [diff] [blame] | 122 | // There is probably a ball in the flywheel if the angular |
| 123 | // velocity is atleast kMinVelocityErrorWithBall less than the last local |
| 124 | // maximum. |
| 125 | ball_in_finisher_ = |
| 126 | (finisher_velocity_dip >= kMinFinisherVelocityDipWithBall); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | status_builder.add_balls_shot(balls_shot_); |
| 131 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 132 | if (output) { |
| 133 | output->finisher_voltage = finisher_.voltage(); |
| 134 | output->accelerator_left_voltage = accelerator_left_.voltage(); |
| 135 | output->accelerator_right_voltage = accelerator_right_.voltage(); |
| 136 | } |
| 137 | |
| 138 | return status_builder.Finish(); |
| 139 | } |
| 140 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 141 | } // namespace y2020::control_loops::superstructure::shooter |