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