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