Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 1 | #include "y2016/control_loops/shooter/shooter.h" |
| 2 | |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 3 | #include <chrono> |
| 4 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 5 | #include "aos/logging/logging.h" |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 6 | #include "y2016/control_loops/shooter/shooter_plant.h" |
| 7 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 8 | namespace y2016::control_loops::shooter { |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 9 | |
Austin Schuh | 214e9c1 | 2016-11-25 17:26:20 -0800 | [diff] [blame] | 10 | namespace chrono = ::std::chrono; |
Austin Schuh | 94a5410 | 2016-11-26 15:14:35 -0800 | [diff] [blame] | 11 | using ::aos::monotonic_clock; |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 12 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 13 | // TODO(austin): Pseudo current limit? |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 14 | |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 15 | ShooterSide::ShooterSide() |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 16 | : loop_(new StateFeedbackLoop<3, 1, 1>(MakeIntegralShooterLoop())) { |
| 17 | history_.fill(0); |
| 18 | Y_.setZero(); |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 19 | } |
| 20 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 21 | void ShooterSide::set_goal(double angular_velocity_goal) { |
| 22 | loop_->mutable_next_R() << 0.0, angular_velocity_goal, 0.0; |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 23 | } |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 24 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 25 | void ShooterSide::set_position(double current_position) { |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 26 | // Update position in the model. |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 27 | Y_ << current_position; |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 28 | |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 29 | // Add the position to the history. |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 30 | history_[history_position_] = current_position; |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 31 | history_position_ = (history_position_ + 1) % kHistoryLength; |
| 32 | } |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 33 | |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 34 | double ShooterSide::voltage() const { return loop_->U(0, 0); } |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 35 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 36 | void ShooterSide::Update(bool disabled) { |
| 37 | loop_->mutable_R() = loop_->next_R(); |
| 38 | if (loop_->R(1, 0) < 1.0) { |
| 39 | // Kill power at low angular velocities. |
| 40 | disabled = true; |
| 41 | } |
| 42 | |
| 43 | loop_->Correct(Y_); |
| 44 | loop_->Update(disabled); |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 47 | flatbuffers::Offset<ShooterSideStatus> ShooterSide::SetStatus( |
| 48 | flatbuffers::FlatBufferBuilder *fbb) { |
| 49 | ShooterSideStatus::Builder shooter_side_status_builder(*fbb); |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 50 | // Compute the oldest point in the history. |
| 51 | const int oldest_history_position = |
| 52 | ((history_position_ == 0) ? kHistoryLength : history_position_) - 1; |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 53 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 54 | // Compute the distance moved over that time period. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 55 | const double avg_angular_velocity = |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 56 | (history_[oldest_history_position] - history_[history_position_]) / |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 57 | (::aos::time::DurationInSeconds(::frc971::controls::kLoopFrequency) * |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 58 | static_cast<double>(kHistoryLength - 1)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 59 | shooter_side_status_builder.add_avg_angular_velocity(avg_angular_velocity); |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 60 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 61 | shooter_side_status_builder.add_angular_velocity(loop_->X_hat(1, 0)); |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 62 | |
| 63 | // Ready if average angular velocity is close to the goal. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 64 | shooter_side_status_builder.add_ready( |
| 65 | (std::abs(loop_->next_R(1, 0) - avg_angular_velocity) < kTolerance && |
| 66 | loop_->next_R(1, 0) > 1.0)); |
| 67 | |
| 68 | return shooter_side_status_builder.Finish(); |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Austin Schuh | 55a13dc | 2019-01-27 22:39:03 -0800 | [diff] [blame] | 71 | Shooter::Shooter(::aos::EventLoop *event_loop, const ::std::string &name) |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 72 | : frc971::controls::ControlLoop<Goal, Position, Status, Output>(event_loop, |
| 73 | name), |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 74 | shots_(0), |
Austin Schuh | 94a5410 | 2016-11-26 15:14:35 -0800 | [diff] [blame] | 75 | last_pre_shot_timeout_(::aos::monotonic_clock::min_time) {} |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 76 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 77 | void Shooter::RunIteration(const Goal *goal, const Position *position, |
| 78 | aos::Sender<Output>::Builder *output, |
| 79 | aos::Sender<Status>::Builder *status) { |
Austin Schuh | 13d6816 | 2019-07-07 20:46:44 -0700 | [diff] [blame] | 80 | const ::aos::monotonic_clock::time_point monotonic_now = |
| 81 | event_loop()->monotonic_now(); |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 82 | if (goal) { |
| 83 | // Update position/goal for our two shooter sides. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 84 | left_.set_goal(goal->angular_velocity()); |
| 85 | right_.set_goal(goal->angular_velocity()); |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 88 | left_.set_position(position->theta_left()); |
| 89 | right_.set_position(position->theta_right()); |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 90 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame] | 91 | left_.Update(output == nullptr); |
| 92 | right_.Update(output == nullptr); |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 93 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 94 | flatbuffers::Offset<ShooterSideStatus> left_status_offset = |
| 95 | left_.SetStatus(status->fbb()); |
| 96 | flatbuffers::Offset<ShooterSideStatus> right_status_offset = |
| 97 | right_.SetStatus(status->fbb()); |
| 98 | |
| 99 | ShooterSideStatus *left_status = |
| 100 | GetMutableTemporaryPointer(*status->fbb(), left_status_offset); |
| 101 | ShooterSideStatus *right_status = |
| 102 | GetMutableTemporaryPointer(*status->fbb(), right_status_offset); |
| 103 | |
| 104 | const bool ready = (left_status->ready() && right_status->ready()); |
| 105 | |
| 106 | Status::Builder status_builder = status->MakeBuilder<Status>(); |
| 107 | status_builder.add_ready((left_status->ready() && right_status->ready())); |
| 108 | status_builder.add_left(left_status_offset); |
| 109 | status_builder.add_right(right_status_offset); |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 110 | |
| 111 | if (output) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 112 | Output::Builder output_builder = output->MakeBuilder<Output>(); |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 113 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 114 | output_builder.add_voltage_left(left_.voltage()); |
| 115 | output_builder.add_voltage_right(right_.voltage()); |
| 116 | // Turn the lights on if we are supposed to spin. |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 117 | if (goal) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 118 | bool lights_on = false; |
| 119 | if (::std::abs(goal->angular_velocity()) > 0.0) { |
| 120 | lights_on = true; |
| 121 | if (goal->shooting_forwards()) { |
| 122 | output_builder.add_forwards_flashlight(true); |
| 123 | output_builder.add_backwards_flashlight(false); |
| 124 | } else { |
| 125 | output_builder.add_forwards_flashlight(false); |
| 126 | output_builder.add_backwards_flashlight(true); |
| 127 | } |
| 128 | } |
| 129 | if (goal->force_lights_on()) { |
| 130 | lights_on = true; |
| 131 | } |
| 132 | output_builder.add_lights_on(lights_on); |
| 133 | |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 134 | bool shoot = false; |
| 135 | switch (state_) { |
| 136 | case ShooterLatchState::PASS_THROUGH: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 137 | if (goal->push_to_shooter()) { |
| 138 | if (::std::abs(goal->angular_velocity()) > 10) { |
| 139 | if (ready) { |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 140 | state_ = ShooterLatchState::WAITING_FOR_SPINDOWN; |
| 141 | shoot = true; |
| 142 | } |
| 143 | } else { |
| 144 | shoot = true; |
| 145 | } |
| 146 | } |
Austin Schuh | 13d6816 | 2019-07-07 20:46:44 -0700 | [diff] [blame] | 147 | last_pre_shot_timeout_ = monotonic_now + chrono::seconds(1); |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 148 | break; |
| 149 | case ShooterLatchState::WAITING_FOR_SPINDOWN: |
| 150 | shoot = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 151 | if (left_.velocity() < goal->angular_velocity() * 0.9 || |
| 152 | right_.velocity() < goal->angular_velocity() * 0.9) { |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 153 | state_ = ShooterLatchState::WAITING_FOR_SPINUP; |
| 154 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 155 | if (::std::abs(goal->angular_velocity()) < 10 || |
Austin Schuh | 13d6816 | 2019-07-07 20:46:44 -0700 | [diff] [blame] | 156 | last_pre_shot_timeout_ < monotonic_now) { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 157 | state_ = ShooterLatchState::INCREMENT_SHOT_COUNT; |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 158 | } |
| 159 | break; |
| 160 | case ShooterLatchState::WAITING_FOR_SPINUP: |
| 161 | shoot = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 162 | if (left_.velocity() > goal->angular_velocity() * 0.95 && |
| 163 | right_.velocity() > goal->angular_velocity() * 0.95) { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 164 | state_ = ShooterLatchState::INCREMENT_SHOT_COUNT; |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 165 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 166 | if (::std::abs(goal->angular_velocity()) < 10 || |
Austin Schuh | 13d6816 | 2019-07-07 20:46:44 -0700 | [diff] [blame] | 167 | last_pre_shot_timeout_ < monotonic_now) { |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 168 | state_ = ShooterLatchState::INCREMENT_SHOT_COUNT; |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 169 | } |
| 170 | break; |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 171 | case ShooterLatchState::INCREMENT_SHOT_COUNT: |
| 172 | ++shots_; |
| 173 | state_ = ShooterLatchState::WAITING_FOR_SHOT_NEGEDGE; |
| 174 | break; |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 175 | case ShooterLatchState::WAITING_FOR_SHOT_NEGEDGE: |
| 176 | shoot = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 177 | if (!goal->push_to_shooter()) { |
Austin Schuh | 7eecd7c | 2016-02-28 21:59:05 -0800 | [diff] [blame] | 178 | state_ = ShooterLatchState::PASS_THROUGH; |
| 179 | } |
| 180 | break; |
| 181 | } |
| 182 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 183 | output_builder.add_clamp_open(goal->clamp_open()); |
| 184 | output_builder.add_push_to_shooter(shoot); |
Comran Morshed | b79c424 | 2016-02-06 18:27:26 +0000 | [diff] [blame] | 185 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 186 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 187 | output->CheckOk(output->Send(output_builder.Finish())); |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 188 | } |
Austin Schuh | f59b8ee | 2016-03-19 21:31:36 -0700 | [diff] [blame] | 189 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 190 | status_builder.add_shots(shots_); |
| 191 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 192 | (void)status->Send(status_builder.Finish()); |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 195 | } // namespace y2016::control_loops::shooter |