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 | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 15 | const double kVelocityTolerance = 20.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 | e8ca06a | 2020-03-07 22:27:39 -0800 | [diff] [blame] | 19 | : finisher_(finisher::MakeIntegralFinisherLoop(), finisher::kBemf, |
| 20 | finisher::kResistance), |
| 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) { |
| 27 | return ( |
| 28 | std::abs(goal->velocity_finisher() - finisher_.avg_angular_velocity()) < |
| 29 | kVelocityTolerance && |
| 30 | std::abs(goal->velocity_accelerator() - |
| 31 | accelerator_left_.avg_angular_velocity()) < kVelocityTolerance && |
| 32 | std::abs(goal->velocity_accelerator() - |
Austin Schuh | 2efe168 | 2021-03-06 22:47:15 -0800 | [diff] [blame^] | 33 | accelerator_right_.avg_angular_velocity()) < |
| 34 | kVelocityTolerance && |
| 35 | std::abs(goal->velocity_finisher() - finisher_.velocity()) < |
| 36 | kVelocityTolerance && |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 37 | std::abs(goal->velocity_accelerator() - accelerator_left_.velocity()) < |
| 38 | kVelocityTolerance && |
| 39 | std::abs(goal->velocity_accelerator() - accelerator_right_.velocity()) < |
| 40 | kVelocityTolerance); |
| 41 | } |
| 42 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 43 | flatbuffers::Offset<ShooterStatus> Shooter::RunIteration( |
| 44 | const ShooterGoal *goal, const ShooterPosition *position, |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 45 | flatbuffers::FlatBufferBuilder *fbb, OutputT *output, |
| 46 | const aos::monotonic_clock::time_point position_timestamp) { |
| 47 | // Update position, output, and status for our two shooter sides. |
| 48 | finisher_.set_position(position->theta_finisher(), position_timestamp); |
| 49 | accelerator_left_.set_position(position->theta_accelerator_left(), |
| 50 | position_timestamp); |
| 51 | accelerator_right_.set_position(position->theta_accelerator_right(), |
| 52 | position_timestamp); |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 53 | |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 54 | // Update goal. |
| 55 | if (goal) { |
| 56 | finisher_.set_goal(goal->velocity_finisher()); |
| 57 | accelerator_left_.set_goal(goal->velocity_accelerator()); |
| 58 | accelerator_right_.set_goal(goal->velocity_accelerator()); |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 59 | } |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 60 | |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 61 | finisher_.Update(output == nullptr); |
| 62 | accelerator_left_.Update(output == nullptr); |
| 63 | accelerator_right_.Update(output == nullptr); |
| 64 | |
| 65 | if (goal) { |
Sabina Davis | 0f31d3f | 2020-02-20 20:41:00 -0800 | [diff] [blame] | 66 | if (UpToSpeed(goal) && goal->velocity_finisher() > kVelocityTolerance && |
| 67 | goal->velocity_accelerator() > kVelocityTolerance) { |
| 68 | ready_ = true; |
| 69 | } else { |
| 70 | ready_ = false; |
| 71 | } |
| 72 | } |
| 73 | |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 74 | flatbuffers::Offset<FlywheelControllerStatus> finisher_status_offset = |
| 75 | finisher_.SetStatus(fbb); |
| 76 | flatbuffers::Offset<FlywheelControllerStatus> accelerator_left_status_offset = |
| 77 | accelerator_left_.SetStatus(fbb); |
| 78 | flatbuffers::Offset<FlywheelControllerStatus> |
| 79 | accelerator_right_status_offset = accelerator_right_.SetStatus(fbb); |
| 80 | |
| 81 | ShooterStatusBuilder status_builder(*fbb); |
| 82 | |
| 83 | status_builder.add_finisher(finisher_status_offset); |
| 84 | status_builder.add_accelerator_left(accelerator_left_status_offset); |
| 85 | status_builder.add_accelerator_right(accelerator_right_status_offset); |
| 86 | |
| 87 | if (output) { |
| 88 | output->finisher_voltage = finisher_.voltage(); |
| 89 | output->accelerator_left_voltage = accelerator_left_.voltage(); |
| 90 | output->accelerator_right_voltage = accelerator_right_.voltage(); |
| 91 | } |
| 92 | |
| 93 | return status_builder.Finish(); |
| 94 | } |
| 95 | |
| 96 | } // namespace shooter |
| 97 | } // namespace superstructure |
| 98 | } // namespace control_loops |
| 99 | } // namespace y2020 |