blob: c400e347d9b306606e80d95054ea3ae29a94541c [file] [log] [blame]
Sabina Davisedf89472020-02-17 15:27:37 -08001#include "y2020/control_loops/superstructure/shooter/shooter.h"
2
3#include <chrono>
milind-u7baf7342021-08-25 18:31:26 -07004#include <cmath>
Sabina Davisedf89472020-02-17 15:27:37 -08005
6#include "aos/logging/logging.h"
7#include "y2020/control_loops/superstructure/accelerator/accelerator_plant.h"
Maxwell Henderson34242992024-01-07 12:39:11 -08008#include "y2020/control_loops/superstructure/accelerator/integral_accelerator_plant.h"
Sabina Davisedf89472020-02-17 15:27:37 -08009#include "y2020/control_loops/superstructure/finisher/finisher_plant.h"
Maxwell Henderson34242992024-01-07 12:39:11 -080010#include "y2020/control_loops/superstructure/finisher/integral_finisher_plant.h"
Sabina Davisedf89472020-02-17 15:27:37 -080011
12namespace y2020 {
13namespace control_loops {
14namespace superstructure {
15namespace shooter {
16
17Shooter::Shooter()
Austin Schuh80476772021-03-06 20:17:36 -080018 : 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 Schuhe8ca06a2020-03-07 22:27:39 -080023 accelerator_left_(accelerator::MakeIntegralAcceleratorLoop(),
24 accelerator::kBemf, accelerator::kResistance),
25 accelerator_right_(accelerator::MakeIntegralAcceleratorLoop(),
26 accelerator::kBemf, accelerator::kResistance) {}
Sabina Davisedf89472020-02-17 15:27:37 -080027
Austin Schuh01d81c32021-11-06 22:59:56 -070028void Shooter::UpToSpeed(const ShooterGoal *goal) {
milind-u0beb7dc2021-10-16 19:31:33 -070029 finisher_ready_ =
30 (std::abs(goal->velocity_finisher() - finisher_.avg_angular_velocity()) <
31 kVelocityToleranceFinisher &&
32 std::abs(goal->velocity_finisher() - finisher_.velocity()) <
milind-u2450f1a2021-11-06 19:01:49 -070033 kVelocityToleranceFinisher &&
34 goal->velocity_finisher() > kVelocityToleranceFinisher);
milind-u0beb7dc2021-10-16 19:31:33 -070035 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-u2450f1a2021-11-06 19:01:49 -070045 kVelocityToleranceAccelerator &&
46 goal->velocity_accelerator() > kVelocityToleranceAccelerator);
Sabina Davis0f31d3f2020-02-20 20:41:00 -080047}
48
Sabina Davisedf89472020-02-17 15:27:37 -080049flatbuffers::Offset<ShooterStatus> Shooter::RunIteration(
50 const ShooterGoal *goal, const ShooterPosition *position,
Sabina Davis0f31d3f2020-02-20 20:41:00 -080051 flatbuffers::FlatBufferBuilder *fbb, OutputT *output,
52 const aos::monotonic_clock::time_point position_timestamp) {
milind-u7baf7342021-08-25 18:31:26 -070053 const double last_finisher_velocity = finisher_.velocity();
54
Sabina Davis0f31d3f2020-02-20 20:41:00 -080055 // 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 Davisedf89472020-02-17 15:27:37 -080061
Sabina Davis0f31d3f2020-02-20 20:41:00 -080062 // Update goal.
63 if (goal) {
milind-u7baf7342021-08-25 18:31:26 -070064 if (std::abs(goal->velocity_finisher() - finisher_goal()) >=
milind-u78f21b72021-10-10 13:47:28 -070065 kVelocityToleranceFinisher) {
milind-u7baf7342021-08-25 18:31:26 -070066 finisher_goal_changed_ = true;
67 last_finisher_velocity_max_ = 0.0;
68 }
69
Sabina Davis0f31d3f2020-02-20 20:41:00 -080070 finisher_.set_goal(goal->velocity_finisher());
71 accelerator_left_.set_goal(goal->velocity_accelerator());
72 accelerator_right_.set_goal(goal->velocity_accelerator());
Austin Schuh43b9ae92020-02-29 23:08:38 -080073 }
Sabina Davis0f31d3f2020-02-20 20:41:00 -080074
Austin Schuh43b9ae92020-02-29 23:08:38 -080075 finisher_.Update(output == nullptr);
76 accelerator_left_.Update(output == nullptr);
77 accelerator_right_.Update(output == nullptr);
78
79 if (goal) {
Austin Schuh01d81c32021-11-06 22:59:56 -070080 UpToSpeed(goal);
Sabina Davis0f31d3f2020-02-20 20:41:00 -080081 }
82
Maxwell Henderson34242992024-01-07 12:39:11 -080083 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 Davisedf89472020-02-17 15:27:37 -080088 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 Schuh5c40ea42021-09-26 13:28:03 -070095 status_builder.add_ready(ready());
Sabina Davisedf89472020-02-17 15:27:37 -080096
milind-u7baf7342021-08-25 18:31:26 -070097 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-u78f21b72021-10-10 13:47:28 -0700100 finisher_goal_changed_ = (std::abs(finisher_.velocity() - finisher_goal()) >
101 kVelocityToleranceFinisher);
milind-u7baf7342021-08-25 18:31:26 -0700102 }
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-u78f21b72021-10-10 13:47:28 -0700115 if (finisher_velocity_dip < kVelocityToleranceFinisher &&
116 ball_in_finisher_) {
milind-u7baf7342021-08-25 18:31:26 -0700117 // 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 Schuheb240f62021-11-07 19:57:06 -0800121 VLOG(1) << "Shot ball at " << position_timestamp;
milind-u7baf7342021-08-25 18:31:26 -0700122 ball_in_finisher_ = false;
milind-u78f21b72021-10-10 13:47:28 -0700123 } else if (!ball_in_finisher_ &&
124 (finisher_goal() > kVelocityToleranceFinisher)) {
milind-u7baf7342021-08-25 18:31:26 -0700125 // 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 Davisedf89472020-02-17 15:27:37 -0800135 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