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> |
| 4 | |
| 5 | #include "aos/logging/logging.h" |
| 6 | #include "y2020/control_loops/superstructure/accelerator/accelerator_plant.h" |
| 7 | #include "y2020/control_loops/superstructure/finisher/finisher_plant.h" |
| 8 | |
| 9 | namespace y2020 { |
| 10 | namespace control_loops { |
| 11 | namespace superstructure { |
| 12 | namespace shooter { |
| 13 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 14 | namespace { |
Austin Schuh | 5c40ea4 | 2021-09-26 13:28:03 -0700 | [diff] [blame^] | 15 | const double kVelocityTolerance = 2.0; |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 16 | } // namespace |
| 17 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 18 | Shooter::Shooter() |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 19 | : finisher_( |
| 20 | finisher::MakeIntegralFinisherLoop(), finisher::kBemf, |
| 21 | // There are 2 motors. So the current limit per motor is going to be |
| 22 | // using resistance * 2 to un-parallel the motor resistances. |
| 23 | finisher::kResistance * 2.0), |
Austin Schuh | e8ca06a | 2020-03-07 22:27:39 -0800 | [diff] [blame] | 24 | accelerator_left_(accelerator::MakeIntegralAcceleratorLoop(), |
| 25 | accelerator::kBemf, accelerator::kResistance), |
| 26 | accelerator_right_(accelerator::MakeIntegralAcceleratorLoop(), |
| 27 | accelerator::kBemf, accelerator::kResistance) {} |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 28 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 29 | bool Shooter::UpToSpeed(const ShooterGoal *goal) { |
| 30 | return ( |
| 31 | std::abs(goal->velocity_finisher() - finisher_.avg_angular_velocity()) < |
| 32 | kVelocityTolerance && |
| 33 | std::abs(goal->velocity_accelerator() - |
| 34 | accelerator_left_.avg_angular_velocity()) < kVelocityTolerance && |
| 35 | std::abs(goal->velocity_accelerator() - |
Austin Schuh | 2efe168 | 2021-03-06 22:47:15 -0800 | [diff] [blame] | 36 | accelerator_right_.avg_angular_velocity()) < |
| 37 | kVelocityTolerance && |
| 38 | std::abs(goal->velocity_finisher() - finisher_.velocity()) < |
| 39 | kVelocityTolerance && |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 40 | std::abs(goal->velocity_accelerator() - accelerator_left_.velocity()) < |
| 41 | kVelocityTolerance && |
| 42 | std::abs(goal->velocity_accelerator() - accelerator_right_.velocity()) < |
| 43 | kVelocityTolerance); |
| 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) { |
| 50 | // Update position, output, and status for our two shooter sides. |
| 51 | finisher_.set_position(position->theta_finisher(), position_timestamp); |
| 52 | accelerator_left_.set_position(position->theta_accelerator_left(), |
| 53 | position_timestamp); |
| 54 | accelerator_right_.set_position(position->theta_accelerator_right(), |
| 55 | position_timestamp); |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 56 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 57 | // Update goal. |
| 58 | if (goal) { |
| 59 | finisher_.set_goal(goal->velocity_finisher()); |
| 60 | accelerator_left_.set_goal(goal->velocity_accelerator()); |
| 61 | accelerator_right_.set_goal(goal->velocity_accelerator()); |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 62 | } |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 63 | |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 64 | finisher_.Update(output == nullptr); |
| 65 | accelerator_left_.Update(output == nullptr); |
| 66 | accelerator_right_.Update(output == nullptr); |
| 67 | |
| 68 | if (goal) { |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 69 | if (UpToSpeed(goal) && goal->velocity_finisher() > kVelocityTolerance && |
| 70 | goal->velocity_accelerator() > kVelocityTolerance) { |
| 71 | ready_ = true; |
| 72 | } else { |
| 73 | ready_ = false; |
| 74 | } |
| 75 | } |
| 76 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 77 | flatbuffers::Offset<FlywheelControllerStatus> finisher_status_offset = |
| 78 | finisher_.SetStatus(fbb); |
| 79 | flatbuffers::Offset<FlywheelControllerStatus> accelerator_left_status_offset = |
| 80 | accelerator_left_.SetStatus(fbb); |
| 81 | flatbuffers::Offset<FlywheelControllerStatus> |
| 82 | accelerator_right_status_offset = accelerator_right_.SetStatus(fbb); |
| 83 | |
| 84 | ShooterStatusBuilder status_builder(*fbb); |
| 85 | |
| 86 | status_builder.add_finisher(finisher_status_offset); |
| 87 | status_builder.add_accelerator_left(accelerator_left_status_offset); |
| 88 | status_builder.add_accelerator_right(accelerator_right_status_offset); |
Austin Schuh | 5c40ea4 | 2021-09-26 13:28:03 -0700 | [diff] [blame^] | 89 | status_builder.add_ready(ready()); |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 90 | |
| 91 | if (output) { |
| 92 | output->finisher_voltage = finisher_.voltage(); |
| 93 | output->accelerator_left_voltage = accelerator_left_.voltage(); |
| 94 | output->accelerator_right_voltage = accelerator_right_.voltage(); |
| 95 | } |
| 96 | |
| 97 | return status_builder.Finish(); |
| 98 | } |
| 99 | |
| 100 | } // namespace shooter |
| 101 | } // namespace superstructure |
| 102 | } // namespace control_loops |
| 103 | } // namespace y2020 |