Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 1 | #include "y2017/control_loops/superstructure/shooter/shooter.h" |
| 2 | |
| 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" |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 8 | |
| 9 | #include "y2017/control_loops/superstructure/shooter/shooter.h" |
| 10 | |
| 11 | namespace y2017 { |
| 12 | namespace control_loops { |
| 13 | namespace superstructure { |
| 14 | namespace shooter { |
| 15 | |
| 16 | namespace chrono = ::std::chrono; |
| 17 | using ::aos::monotonic_clock; |
| 18 | |
| 19 | namespace { |
| 20 | constexpr double kTolerance = 10.0; |
| 21 | } // namespace |
| 22 | |
| 23 | // TODO(austin): Pseudo current limit? |
| 24 | |
| 25 | ShooterController::ShooterController() |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 26 | : loop_(new StateFeedbackLoop<4, 1, 1, double, |
| 27 | StateFeedbackHybridPlant<4, 1, 1>, |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 28 | HybridKalman<4, 1, 1>>( |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 29 | superstructure::shooter::MakeIntegralShooterLoop())) { |
| 30 | history_.fill(0); |
| 31 | Y_.setZero(); |
| 32 | } |
| 33 | |
| 34 | void ShooterController::set_goal(double angular_velocity_goal) { |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 35 | loop_->mutable_next_R() << 0.0, angular_velocity_goal, angular_velocity_goal, |
| 36 | 0.0; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void ShooterController::set_position(double current_position) { |
| 40 | // Update position in the model. |
| 41 | Y_ << current_position; |
| 42 | |
| 43 | // Add the position to the history. |
| 44 | history_[history_position_] = current_position; |
| 45 | history_position_ = (history_position_ + 1) % kHistoryLength; |
| 46 | |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 47 | dt_position_ = current_position - last_position_; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 48 | last_position_ = current_position; |
| 49 | } |
| 50 | |
| 51 | double ShooterController::voltage() const { return loop_->U(0, 0); } |
| 52 | |
| 53 | void ShooterController::Reset() { reset_ = true; } |
| 54 | |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 55 | void ShooterController::Update(bool disabled, chrono::nanoseconds dt) { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 56 | loop_->mutable_R() = loop_->next_R(); |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 57 | if (::std::abs(loop_->R(2, 0)) < 1.0) { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 58 | // Kill power at low angular velocities. |
| 59 | disabled = true; |
| 60 | } |
| 61 | |
| 62 | loop_->Correct(Y_); |
| 63 | |
| 64 | // Compute the oldest point in the history. |
| 65 | const int oldest_history_position = |
| 66 | ((history_position_ == 0) ? kHistoryLength : history_position_) - 1; |
| 67 | |
| 68 | // Compute the distance moved over that time period. |
| 69 | average_angular_velocity_ = |
| 70 | (history_[oldest_history_position] - history_[history_position_]) / |
Austin Schuh | 8538704 | 2017-09-13 23:59:21 -0700 | [diff] [blame] | 71 | (0.00505 * static_cast<double>(kHistoryLength - 1)); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 72 | |
| 73 | // Ready if average angular velocity is close to the goal. |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 74 | error_ = average_angular_velocity_ - loop_->next_R(2, 0); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 75 | |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 76 | ready_ = std::abs(error_) < kTolerance && loop_->next_R(2, 0) > 1.0; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 77 | |
Austin Schuh | eb5c22e | 2017-04-09 18:30:28 -0700 | [diff] [blame] | 78 | // If we are no longer ready, but were, and are spinning, then we shot a ball. |
| 79 | // Reset the KF. |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 80 | if (last_ready_ && !ready_ && loop_->next_R(2, 0) > 1.0 && error_ < 0.0) { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 81 | needs_reset_ = true; |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 82 | min_velocity_ = velocity(); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 83 | } |
| 84 | if (needs_reset_) { |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 85 | min_velocity_ = ::std::min(min_velocity_, velocity()); |
| 86 | if (velocity() > min_velocity_ + 5.0) { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 87 | reset_ = true; |
| 88 | needs_reset_ = false; |
| 89 | } |
| 90 | } |
| 91 | if (reset_) { |
Austin Schuh | eb5c22e | 2017-04-09 18:30:28 -0700 | [diff] [blame] | 92 | // TODO(austin): I'd rather not be incrementing X_hat each time. Sort out |
| 93 | // something better. |
Austin Schuh | 4360351 | 2017-04-16 19:11:37 -0700 | [diff] [blame] | 94 | loop_->mutable_X_hat(3, 0) += 1.0; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 95 | reset_ = false; |
| 96 | } |
| 97 | last_ready_ = ready_; |
| 98 | |
| 99 | X_hat_current_ = loop_->X_hat(); |
| 100 | position_error_ = X_hat_current_(0, 0) - Y_(0, 0); |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame^] | 101 | dt_velocity_ = dt_position_ / ::aos::time::DurationInSeconds(dt); |
Austin Schuh | d6e9fb4 | 2017-04-09 17:56:06 -0700 | [diff] [blame] | 102 | fixed_dt_velocity_ = dt_position_ / 0.00505; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 103 | |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 104 | loop_->Update(disabled, dt); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void ShooterController::SetStatus(ShooterStatus *status) { |
| 108 | status->avg_angular_velocity = average_angular_velocity_; |
| 109 | |
Austin Schuh | eb5c22e | 2017-04-09 18:30:28 -0700 | [diff] [blame] | 110 | status->filtered_velocity = X_hat_current_(1, 0); |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 111 | status->angular_velocity = X_hat_current_(2, 0); |
Austin Schuh | 169f5f2 | 2017-02-18 16:31:13 -0800 | [diff] [blame] | 112 | status->ready = ready_; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 113 | |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 114 | status->voltage_error = X_hat_current_(3, 0); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 115 | status->position_error = position_error_; |
| 116 | status->instantaneous_velocity = dt_velocity_; |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 117 | status->fixed_instantaneous_velocity = fixed_dt_velocity_; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void Shooter::Reset() { wheel_.Reset(); } |
| 121 | |
| 122 | void Shooter::Iterate(const control_loops::ShooterGoal *goal, |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 123 | const double *position, |
| 124 | ::aos::monotonic_clock::time_point position_time, |
| 125 | double *output, control_loops::ShooterStatus *status) { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 126 | if (goal) { |
| 127 | // Update position/goal for our wheel. |
| 128 | wheel_.set_goal(goal->angular_velocity); |
| 129 | } |
| 130 | |
| 131 | wheel_.set_position(*position); |
| 132 | |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 133 | chrono::nanoseconds dt = ::aos::controls::kLoopFrequency; |
| 134 | if (last_time_ != ::aos::monotonic_clock::min_time) { |
| 135 | dt = position_time - last_time_; |
| 136 | } |
| 137 | last_time_ = position_time; |
| 138 | |
| 139 | wheel_.Update(output == nullptr, dt); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 140 | |
| 141 | wheel_.SetStatus(status); |
| 142 | |
| 143 | if (last_ready_ && !status->ready) { |
| 144 | min_ = wheel_.dt_velocity(); |
| 145 | } else if (!status->ready) { |
| 146 | min_ = ::std::min(min_, wheel_.dt_velocity()); |
| 147 | } else if (!last_ready_ && status->ready) { |
| 148 | LOG(INFO, "Shot min was [%f]\n", min_); |
| 149 | } |
| 150 | |
| 151 | if (output) { |
| 152 | *output = wheel_.voltage(); |
| 153 | } |
| 154 | last_ready_ = status->ready; |
| 155 | } |
| 156 | |
| 157 | } // namespace shooter |
| 158 | } // namespace superstructure |
| 159 | } // namespace control_loops |
| 160 | } // namespace y2017 |