Austin Schuh | c682d61 | 2013-03-03 14:32:52 -0800 | [diff] [blame] | 1 | #include "frc971/control_loops/shooter/shooter.h" |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 2 | |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 3 | #include "aos/common/control_loop/control_loops.q.h" |
| 4 | #include "aos/common/logging/logging.h" |
| 5 | |
Austin Schuh | c682d61 | 2013-03-03 14:32:52 -0800 | [diff] [blame] | 6 | #include "frc971/control_loops/shooter/shooter_motor_plant.h" |
Brian Silverman | 7ffb138 | 2013-09-29 16:55:12 -0700 | [diff] [blame] | 7 | #include "frc971/control_loops/index/index_motor.q.h" |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 8 | |
| 9 | namespace frc971 { |
| 10 | namespace control_loops { |
| 11 | |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 12 | ShooterMotor::ShooterMotor(control_loops::ShooterLoop *my_shooter) |
| 13 | : aos::control_loops::ControlLoop<control_loops::ShooterLoop>(my_shooter), |
| 14 | loop_(new StateFeedbackLoop<2, 1, 1>(MakeShooterLoop())), |
| 15 | history_position_(0), |
| 16 | position_goal_(0.0), |
Brian Silverman | 7ffb138 | 2013-09-29 16:55:12 -0700 | [diff] [blame] | 17 | last_position_(0.0), |
| 18 | last_velocity_goal_(0) { |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 19 | memset(history_, 0, sizeof(history_)); |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 20 | } |
| 21 | |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 22 | /*static*/ const double ShooterMotor::dt = 0.01; |
| 23 | /*static*/ const double ShooterMotor::kMaxSpeed = |
| 24 | 10000.0 * (2.0 * M_PI) / 60.0 * 15.0 / 34.0; |
| 25 | |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 26 | void ShooterMotor::RunIteration( |
| 27 | const control_loops::ShooterLoop::Goal *goal, |
| 28 | const control_loops::ShooterLoop::Position *position, |
| 29 | ::aos::control_loops::Output *output, |
| 30 | control_loops::ShooterLoop::Status *status) { |
Brian Silverman | 7ffb138 | 2013-09-29 16:55:12 -0700 | [diff] [blame] | 31 | double velocity_goal = std::min(goal->velocity, kMaxSpeed); |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 32 | const double current_position = |
| 33 | (position == NULL ? loop_->X_hat(0, 0) : position->position); |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 34 | double output_voltage = 0.0; |
| 35 | |
Brian Silverman | 7ffb138 | 2013-09-29 16:55:12 -0700 | [diff] [blame] | 36 | if (index_loop.status.FetchLatest() || index_loop.status.get()) { |
| 37 | if (index_loop.status->is_shooting) { |
| 38 | if (velocity_goal != last_velocity_goal_ && |
| 39 | velocity_goal < 130) { |
| 40 | velocity_goal = last_velocity_goal_; |
| 41 | } |
| 42 | } |
| 43 | } else { |
| 44 | LOG(WARNING, "assuming index isn't shooting\n"); |
| 45 | } |
| 46 | last_velocity_goal_ = velocity_goal; |
| 47 | |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 48 | // Track the current position if the velocity goal is small. |
| 49 | if (velocity_goal <= 1.0) { |
| 50 | position_goal_ = current_position; |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 51 | } |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 52 | |
| 53 | loop_->Y << current_position; |
| 54 | |
| 55 | // Add the position to the history. |
| 56 | history_[history_position_] = current_position; |
| 57 | history_position_ = (history_position_ + 1) % kHistoryLength; |
| 58 | |
| 59 | // Prevents integral windup by limiting the position error such that the |
| 60 | // error can't produce much more than full power. |
| 61 | const double kVelocityWeightScalar = 0.35; |
| 62 | const double max_reference = |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 63 | (loop_->U_max(0, 0) - kVelocityWeightScalar * |
| 64 | (velocity_goal - loop_->X_hat(1, 0)) * loop_->K(0, 1)) |
| 65 | / loop_->K(0, 0) + loop_->X_hat(0, 0); |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 66 | const double min_reference = |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 67 | (loop_->U_min(0, 0) - kVelocityWeightScalar * |
| 68 | (velocity_goal - loop_->X_hat(1, 0)) * loop_->K(0, 1)) |
| 69 | / loop_->K(0, 0) + loop_->X_hat(0, 0); |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 70 | |
| 71 | position_goal_ = ::std::max(::std::min(position_goal_, max_reference), |
| 72 | min_reference); |
| 73 | loop_->R << position_goal_, velocity_goal; |
| 74 | position_goal_ += velocity_goal * dt; |
Austin Schuh | 9644e1c | 2013-03-12 00:40:36 -0700 | [diff] [blame] | 75 | |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 76 | loop_->Update(position, output == NULL); |
| 77 | |
| 78 | // Kill power at low velocity goals. |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 79 | if (velocity_goal < 1.0) { |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 80 | loop_->U[0] = 0.0; |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 81 | } else { |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 82 | output_voltage = loop_->U[0]; |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 83 | } |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 84 | |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 85 | LOG(DEBUG, |
| 86 | "PWM: %f, raw_pos: %f rotations: %f " |
| 87 | "junk velocity: %f, xhat[0]: %f xhat[1]: %f, R[0]: %f R[1]: %f\n", |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 88 | output_voltage, current_position, |
| 89 | current_position / (2 * M_PI), |
| 90 | (current_position - last_position_) / dt, |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 91 | loop_->X_hat[0], loop_->X_hat[1], loop_->R[0], loop_->R[1]); |
| 92 | |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 93 | // Calculates the velocity over the last kHistoryLength * .01 seconds |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 94 | // by taking the difference between the current and next history positions. |
| 95 | int old_history_position = ((history_position_ == 0) ? |
| 96 | kHistoryLength : history_position_) - 1; |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 97 | average_velocity_ = (history_[old_history_position] - |
| 98 | history_[history_position_]) * 100.0 / (double)(kHistoryLength - 1); |
| 99 | |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 100 | status->average_velocity = average_velocity_; |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 101 | |
| 102 | // Determine if the velocity is close enough to the goal to be ready. |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 103 | if (std::abs(velocity_goal - average_velocity_) < 10.0 && |
| 104 | velocity_goal != 0.0) { |
| 105 | LOG(DEBUG, "Steady: "); |
| 106 | status->ready = true; |
| 107 | } else { |
| 108 | LOG(DEBUG, "Not ready: "); |
| 109 | status->ready = false; |
| 110 | } |
| 111 | LOG(DEBUG, "avg = %f goal = %f\n", average_velocity_, velocity_goal); |
| 112 | |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 113 | last_position_ = current_position; |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 114 | |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 115 | if (output) { |
| 116 | output->voltage = output_voltage; |
| 117 | } |
Austin Schuh | 0e38a5d | 2013-03-03 03:53:35 -0800 | [diff] [blame] | 118 | } |
James Kuszmaul | cdd033e | 2013-03-02 15:10:43 -0800 | [diff] [blame] | 119 | |
| 120 | } // namespace control_loops |
| 121 | } // namespace frc971 |