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