blob: ec08ec85eb79737ce299810d693a7b3242d72556 [file] [log] [blame]
Austin Schuhc682d612013-03-03 14:32:52 -08001#include "frc971/control_loops/shooter/shooter.h"
James Kuszmaulcdd033e2013-03-02 15:10:43 -08002
3#include "aos/aos_core.h"
4
5#include "aos/common/control_loop/control_loops.q.h"
6#include "aos/common/logging/logging.h"
7
Austin Schuhc682d612013-03-03 14:32:52 -08008#include "frc971/control_loops/shooter/shooter_motor_plant.h"
James Kuszmaulcdd033e2013-03-02 15:10:43 -08009
10namespace frc971 {
11namespace control_loops {
12
James Kuszmaulcdd033e2013-03-02 15:10:43 -080013ShooterMotor::ShooterMotor(control_loops::ShooterLoop *my_shooter)
14 : aos::control_loops::ControlLoop<control_loops::ShooterLoop>(my_shooter),
15 loop_(new StateFeedbackLoop<2, 1, 1>(MakeShooterLoop())),
16 history_position_(0),
17 position_goal_(0.0),
Austin Schuh0e38a5d2013-03-03 03:53:35 -080018 last_position_(0.0) {
19 memset(history_, 0, sizeof(history_));
James Kuszmaulcdd033e2013-03-02 15:10:43 -080020}
21
Austin Schuh0e38a5d2013-03-03 03:53:35 -080022/*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 Kuszmaulcdd033e2013-03-02 15:10:43 -080026void 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) {
Austin Schuh0e38a5d2013-03-03 03:53:35 -080031 const double velocity_goal = std::min(goal->velocity, kMaxSpeed);
Austin Schuh9644e1c2013-03-12 00:40:36 -070032 const double current_position =
33 (position == NULL ? loop_->X_hat(0, 0) : position->position);
James Kuszmaulcdd033e2013-03-02 15:10:43 -080034 double output_voltage = 0.0;
35
Austin Schuh0e38a5d2013-03-03 03:53:35 -080036 // Track the current position if the velocity goal is small.
37 if (velocity_goal <= 1.0) {
38 position_goal_ = current_position;
James Kuszmaulcdd033e2013-03-02 15:10:43 -080039 }
Austin Schuh0e38a5d2013-03-03 03:53:35 -080040
41 loop_->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
47 // Prevents integral windup by limiting the position error such that the
48 // error can't produce much more than full power.
49 const double kVelocityWeightScalar = 0.35;
50 const double max_reference =
Austin Schuh9644e1c2013-03-12 00:40:36 -070051 (loop_->U_max(0, 0) - kVelocityWeightScalar *
52 (velocity_goal - loop_->X_hat(1, 0)) * loop_->K(0, 1))
53 / loop_->K(0, 0) + loop_->X_hat(0, 0);
Austin Schuh0e38a5d2013-03-03 03:53:35 -080054 const double min_reference =
Austin Schuh9644e1c2013-03-12 00:40:36 -070055 (loop_->U_min(0, 0) - kVelocityWeightScalar *
56 (velocity_goal - loop_->X_hat(1, 0)) * loop_->K(0, 1))
57 / loop_->K(0, 0) + loop_->X_hat(0, 0);
Austin Schuh0e38a5d2013-03-03 03:53:35 -080058
59 position_goal_ = ::std::max(::std::min(position_goal_, max_reference),
60 min_reference);
61 loop_->R << position_goal_, velocity_goal;
62 position_goal_ += velocity_goal * dt;
Austin Schuh9644e1c2013-03-12 00:40:36 -070063
Austin Schuh0e38a5d2013-03-03 03:53:35 -080064 loop_->Update(position, output == NULL);
65
66 // Kill power at low velocity goals.
James Kuszmaulcdd033e2013-03-02 15:10:43 -080067 if (velocity_goal < 1.0) {
Austin Schuh0e38a5d2013-03-03 03:53:35 -080068 loop_->U[0] = 0.0;
James Kuszmaulcdd033e2013-03-02 15:10:43 -080069 } else {
Austin Schuh0e38a5d2013-03-03 03:53:35 -080070 output_voltage = loop_->U[0];
James Kuszmaulcdd033e2013-03-02 15:10:43 -080071 }
Austin Schuh0e38a5d2013-03-03 03:53:35 -080072
James Kuszmaulcdd033e2013-03-02 15:10:43 -080073 LOG(DEBUG,
74 "PWM: %f, raw_pos: %f rotations: %f "
75 "junk velocity: %f, xhat[0]: %f xhat[1]: %f, R[0]: %f R[1]: %f\n",
Austin Schuh0e38a5d2013-03-03 03:53:35 -080076 output_voltage, current_position,
77 current_position / (2 * M_PI),
78 (current_position - last_position_) / dt,
James Kuszmaulcdd033e2013-03-02 15:10:43 -080079 loop_->X_hat[0], loop_->X_hat[1], loop_->R[0], loop_->R[1]);
80
Austin Schuh0e38a5d2013-03-03 03:53:35 -080081 // Calculates the velocity over the last kHistoryLength * .01 seconds
James Kuszmaulcdd033e2013-03-02 15:10:43 -080082 // by taking the difference between the current and next history positions.
83 int old_history_position = ((history_position_ == 0) ?
84 kHistoryLength : history_position_) - 1;
Austin Schuh0e38a5d2013-03-03 03:53:35 -080085 average_velocity_ = (history_[old_history_position] -
86 history_[history_position_]) * 100.0 / (double)(kHistoryLength - 1);
87
James Kuszmaulcdd033e2013-03-02 15:10:43 -080088 status->average_velocity = average_velocity_;
Austin Schuh0e38a5d2013-03-03 03:53:35 -080089
90 // Determine if the velocity is close enough to the goal to be ready.
James Kuszmaulcdd033e2013-03-02 15:10:43 -080091 if (std::abs(velocity_goal - average_velocity_) < 10.0 &&
92 velocity_goal != 0.0) {
93 LOG(DEBUG, "Steady: ");
94 status->ready = true;
95 } else {
96 LOG(DEBUG, "Not ready: ");
97 status->ready = false;
98 }
99 LOG(DEBUG, "avg = %f goal = %f\n", average_velocity_, velocity_goal);
100
Austin Schuh0e38a5d2013-03-03 03:53:35 -0800101 last_position_ = current_position;
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800102
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800103 if (output) {
104 output->voltage = output_voltage;
105 }
Austin Schuh0e38a5d2013-03-03 03:53:35 -0800106}
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800107
108} // namespace control_loops
109} // namespace frc971