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