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