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