Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 1 | #include "y2016/control_loops/shooter/shooter.h" |
| 2 | |
| 3 | #include "aos/common/controls/control_loops.q.h" |
| 4 | #include "aos/common/logging/logging.h" |
| 5 | #include "aos/common/logging/queue_logging.h" |
| 6 | |
| 7 | #include "y2016/control_loops/shooter/shooter_plant.h" |
| 8 | |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 9 | |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 10 | namespace y2016 { |
| 11 | namespace control_loops { |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 12 | namespace shooter { |
| 13 | |
| 14 | // TODO(austin): Pseudo current limit? |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 15 | |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 16 | ShooterSide::ShooterSide() |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 17 | : loop_(new StateFeedbackLoop<3, 1, 1>(MakeIntegralShooterLoop())) { |
| 18 | history_.fill(0); |
| 19 | Y_.setZero(); |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 22 | void ShooterSide::set_goal(double angular_velocity_goal) { |
| 23 | loop_->mutable_next_R() << 0.0, angular_velocity_goal, 0.0; |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 24 | } |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 25 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 26 | void ShooterSide::set_position(double current_position) { |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 27 | // Update position in the model. |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 28 | Y_ << current_position; |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 29 | |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 30 | // Add the position to the history. |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 31 | history_[history_position_] = current_position; |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 32 | history_position_ = (history_position_ + 1) % kHistoryLength; |
| 33 | } |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 34 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 35 | double ShooterSide::voltage() const { |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 36 | return loop_->U(0, 0); |
| 37 | } |
| 38 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 39 | void ShooterSide::Update(bool disabled) { |
| 40 | loop_->mutable_R() = loop_->next_R(); |
| 41 | if (loop_->R(1, 0) < 1.0) { |
| 42 | // Kill power at low angular velocities. |
| 43 | disabled = true; |
| 44 | } |
| 45 | |
| 46 | loop_->Correct(Y_); |
| 47 | loop_->Update(disabled); |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 50 | void ShooterSide::SetStatus(ShooterSideStatus *status) { |
| 51 | // Compute the oldest point in the history. |
| 52 | const int oldest_history_position = |
| 53 | ((history_position_ == 0) ? kHistoryLength : history_position_) - 1; |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 54 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 55 | // Compute the distance moved over that time period. |
| 56 | status->avg_angular_velocity = |
| 57 | (history_[oldest_history_position] - history_[history_position_]) / |
| 58 | (::aos::controls::kLoopFrequency.ToSeconds() * |
| 59 | static_cast<double>(kHistoryLength - 1)); |
| 60 | |
| 61 | status->angular_velocity = loop_->X_hat(1, 0); |
| 62 | |
| 63 | // Ready if average angular velocity is close to the goal. |
| 64 | status->ready = (std::abs(loop_->next_R(1, 0) - |
| 65 | status->avg_angular_velocity) < kTolerance && |
| 66 | loop_->next_R(1, 0) > 1.0); |
| 67 | } |
| 68 | |
| 69 | Shooter::Shooter(ShooterQueue *my_shooter) |
| 70 | : aos::controls::ControlLoop<ShooterQueue>(my_shooter) {} |
| 71 | |
| 72 | void Shooter::RunIteration(const ShooterQueue::Goal *goal, |
| 73 | const ShooterQueue::Position *position, |
| 74 | ShooterQueue::Output *output, |
| 75 | ShooterQueue::Status *status) { |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 76 | if (goal) { |
| 77 | // Update position/goal for our two shooter sides. |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 78 | left_.set_goal(goal->angular_velocity); |
| 79 | right_.set_goal(goal->angular_velocity); |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 82 | left_.set_position(position->theta_left); |
| 83 | right_.set_position(position->theta_right); |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 84 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 85 | left_.Update(output == nullptr); |
| 86 | right_.Update(output == nullptr); |
Comran Morshed | cde5032 | 2016-01-18 15:10:36 +0000 | [diff] [blame] | 87 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 88 | left_.SetStatus(&status->left); |
| 89 | right_.SetStatus(&status->right); |
| 90 | status->ready = (status->left.ready && status->right.ready); |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 91 | |
| 92 | if (output) { |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 93 | output->voltage_left = left_.voltage(); |
| 94 | output->voltage_right = right_.voltage(); |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Austin Schuh | 09c2b0b | 2016-02-13 15:53:16 -0800 | [diff] [blame^] | 98 | } // namespace shooter |
Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame] | 99 | } // namespace control_loops |
| 100 | } // namespace y2016 |