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 | |
| 9 | namespace y2016 { |
| 10 | namespace control_loops { |
| 11 | |
| 12 | Shooter::Shooter(control_loops::ShooterQueue *my_shooter) |
| 13 | : aos::controls::ControlLoop<control_loops::ShooterQueue>(my_shooter), |
| 14 | loop_(new StateFeedbackLoop<2, 1, 1>( |
| 15 | ::y2016::control_loops::shooter::MakeShooterLoop())), |
| 16 | history_position_(0), |
| 17 | position_goal_(0.0), |
| 18 | last_position_(0.0), |
| 19 | last_velocity_goal_(0) { |
| 20 | memset(history_, 0, sizeof(history_)); |
| 21 | } |
| 22 | |
| 23 | /*static*/ const double Shooter::dt = 0.005; |
| 24 | /*static*/ const double Shooter::kMaxSpeed = |
| 25 | 10000.0 * (2.0 * M_PI) / 60.0 * 15.0 / 34.0; |
| 26 | |
| 27 | void Shooter::RunIteration( |
| 28 | const control_loops::ShooterQueue::Goal *goal, |
| 29 | const control_loops::ShooterQueue::Position *position, |
| 30 | ::aos::control_loops::Output *output, |
| 31 | control_loops::ShooterQueue::Status *status) { |
| 32 | double velocity_goal = std::min(goal->velocity, kMaxSpeed); |
| 33 | const double current_position = |
| 34 | (position == NULL ? loop_->X_hat(0, 0) : position->position); |
| 35 | double output_voltage = 0.0; |
| 36 | |
| 37 | // TODO(phil): Set a queue to trigger a shot. For now, disable the fetch from |
| 38 | // the queue. |
| 39 | if (false) { |
| 40 | // if (index_loop.status.FetchLatest() || index_loop.status.get()) { |
| 41 | // if (index_loop.status->is_shooting) { |
| 42 | if (velocity_goal != last_velocity_goal_ && velocity_goal < 130) { |
| 43 | velocity_goal = last_velocity_goal_; |
| 44 | } |
| 45 | } else { |
| 46 | LOG(WARNING, "assuming index isn't shooting\n"); |
| 47 | } |
| 48 | last_velocity_goal_ = velocity_goal; |
| 49 | |
| 50 | // Track the current position if the velocity goal is small. |
| 51 | if (velocity_goal <= 1.0) { |
| 52 | position_goal_ = current_position; |
| 53 | } |
| 54 | |
| 55 | // TODO(phil): Does this change make sense? |
| 56 | // loop_->Y << current_position; |
| 57 | Eigen::Matrix<double, 1, 1> Y; |
| 58 | Y << current_position; |
| 59 | loop_->Correct(Y); |
| 60 | |
| 61 | // Add the position to the history. |
| 62 | history_[history_position_] = current_position; |
| 63 | history_position_ = (history_position_ + 1) % kHistoryLength; |
| 64 | |
| 65 | // Prevents integral windup by limiting the position error such that the |
| 66 | // error can't produce much more than full power. |
| 67 | const double kVelocityWeightScalar = 0.35; |
| 68 | const double max_reference = |
| 69 | (loop_->U_max(0, 0) - |
| 70 | kVelocityWeightScalar * (velocity_goal - loop_->X_hat(1, 0)) * |
| 71 | loop_->K(0, 1)) / |
| 72 | loop_->K(0, 0) + |
| 73 | loop_->X_hat(0, 0); |
| 74 | const double min_reference = |
| 75 | (loop_->U_min(0, 0) - |
| 76 | kVelocityWeightScalar * (velocity_goal - loop_->X_hat(1, 0)) * |
| 77 | loop_->K(0, 1)) / |
| 78 | loop_->K(0, 0) + |
| 79 | loop_->X_hat(0, 0); |
| 80 | |
| 81 | position_goal_ = |
| 82 | ::std::max(::std::min(position_goal_, max_reference), min_reference); |
| 83 | // TODO(phil): Does this change make sense? |
| 84 | // loop_->R << position_goal_, velocity_goal; |
| 85 | loop_->mutable_R(0, 0) = position_goal_; |
| 86 | loop_->mutable_R(1, 0) = velocity_goal; |
| 87 | position_goal_ += velocity_goal * dt; |
| 88 | |
| 89 | // TODO(phil): Does this change make sense? |
| 90 | // loop_->Update(position, output == NULL); |
| 91 | loop_->Update(output == NULL); |
| 92 | |
| 93 | // Kill power at low velocity goals. |
| 94 | if (velocity_goal < 1.0) { |
| 95 | loop_->mutable_U(0, 0) = 0.0; |
| 96 | } else { |
| 97 | output_voltage = loop_->U(0, 0); |
| 98 | } |
| 99 | |
| 100 | LOG(DEBUG, |
| 101 | "PWM: %f, raw_pos: %f rotations: %f " |
| 102 | "junk velocity: %f, xhat[0]: %f xhat[1]: %f, R[0]: %f R[1]: %f\n", |
| 103 | output_voltage, current_position, current_position / (2 * M_PI), |
| 104 | (current_position - last_position_) / dt, |
| 105 | // TODO(phil): Does this change make sense? |
| 106 | // loop_->X_hat[0], loop_->X_hat[1], loop_->R[0], loop_->R[1]); |
| 107 | loop_->X_hat(0, 0), loop_->X_hat(1, 0), loop_->R(0, 0), loop_->R(1, 0)); |
| 108 | |
| 109 | // Calculates the velocity over the last kHistoryLength * .01 seconds |
| 110 | // by taking the difference between the current and next history positions. |
| 111 | int old_history_position = |
| 112 | ((history_position_ == 0) ? kHistoryLength : history_position_) - 1; |
| 113 | average_velocity_ = |
| 114 | (history_[old_history_position] - history_[history_position_]) / dt / |
| 115 | (double)(kHistoryLength - 1); |
| 116 | |
| 117 | status->average_velocity = average_velocity_; |
| 118 | |
| 119 | // Determine if the velocity is close enough to the goal to be ready. |
| 120 | if (std::abs(velocity_goal - average_velocity_) < 10.0 && |
| 121 | velocity_goal != 0.0) { |
| 122 | LOG(DEBUG, "Steady: "); |
| 123 | status->ready = true; |
| 124 | } else { |
| 125 | LOG(DEBUG, "Not ready: "); |
| 126 | status->ready = false; |
| 127 | } |
| 128 | LOG(DEBUG, "avg = %f goal = %f\n", average_velocity_, velocity_goal); |
| 129 | |
| 130 | last_position_ = current_position; |
| 131 | |
| 132 | if (output) { |
| 133 | output->voltage = output_voltage; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | } // namespace control_loops |
| 138 | } // namespace y2016 |