blob: d30a45c7fab40e1be85d170e79d657364c4c02ff [file] [log] [blame]
Tyler Chatow2737d2a2017-02-08 21:20:51 -08001#include "y2017/control_loops/superstructure/shooter/shooter.h"
2
3#include <chrono>
4
5#include "aos/common/controls/control_loops.q.h"
6#include "aos/common/logging/logging.h"
7#include "aos/common/logging/queue_logging.h"
8
9#include "y2017/control_loops/superstructure/shooter/shooter.h"
10
11namespace y2017 {
12namespace control_loops {
13namespace superstructure {
14namespace shooter {
15
16namespace chrono = ::std::chrono;
17using ::aos::monotonic_clock;
18
19namespace {
20constexpr double kTolerance = 10.0;
21} // namespace
22
23// TODO(austin): Pseudo current limit?
24
25ShooterController::ShooterController()
26 : loop_(new StateFeedbackLoop<3, 1, 1>(
27 superstructure::shooter::MakeIntegralShooterLoop())) {
28 history_.fill(0);
29 Y_.setZero();
30}
31
32void ShooterController::set_goal(double angular_velocity_goal) {
33 loop_->mutable_next_R() << 0.0, angular_velocity_goal, 0.0;
34}
35
36void ShooterController::set_position(double current_position) {
37 // Update position in the model.
38 Y_ << current_position;
39
40 // Add the position to the history.
41 history_[history_position_] = current_position;
42 history_position_ = (history_position_ + 1) % kHistoryLength;
43
Austin Schuh169f5f22017-02-18 16:31:13 -080044 dt_velocity_ = (current_position - last_position_) /
45 chrono::duration_cast<chrono::duration<double>>(
46 ::aos::controls::kLoopFrequency)
47 .count();
Tyler Chatow2737d2a2017-02-08 21:20:51 -080048 last_position_ = current_position;
49}
50
51double ShooterController::voltage() const { return loop_->U(0, 0); }
52
53void ShooterController::Reset() { reset_ = true; }
54
55void ShooterController::Update(bool disabled) {
56 loop_->mutable_R() = loop_->next_R();
57 if (::std::abs(loop_->R(1, 0)) < 1.0) {
58 // Kill power at low angular velocities.
59 disabled = true;
60 }
61
62 loop_->Correct(Y_);
63
64 // Compute the oldest point in the history.
65 const int oldest_history_position =
66 ((history_position_ == 0) ? kHistoryLength : history_position_) - 1;
67
68 // Compute the distance moved over that time period.
69 average_angular_velocity_ =
70 (history_[oldest_history_position] - history_[history_position_]) /
71 (chrono::duration_cast<chrono::duration<double>>(
72 ::aos::controls::kLoopFrequency)
73 .count() *
74 static_cast<double>(kHistoryLength - 1));
75
76 // Ready if average angular velocity is close to the goal.
77 error_ = average_angular_velocity_ - loop_->next_R(1, 0);
78
79 ready_ = std::abs(error_) < kTolerance && loop_->next_R(1, 0) > 1.0;
80
81 if (last_ready_ && !ready_ && loop_->next_R(1, 0) > 1.0 && error_ < 0.0) {
82 needs_reset_ = true;
83 min_velocity_ = loop_->X_hat(1, 0);
84 }
85 if (needs_reset_) {
86 min_velocity_ = ::std::min(min_velocity_, loop_->X_hat(1, 0));
87 if (loop_->X_hat(1, 0) > min_velocity_ + 5.0) {
88 reset_ = true;
89 needs_reset_ = false;
90 }
91 }
92 if (reset_) {
93 loop_->mutable_X_hat(2, 0) = 0.0;
94 loop_->mutable_X_hat(1, 0) = dt_velocity_;
95 loop_->mutable_X_hat(0, 0) = Y_(0, 0);
96 reset_ = false;
97 }
98 last_ready_ = ready_;
99
100 X_hat_current_ = loop_->X_hat();
101 position_error_ = X_hat_current_(0, 0) - Y_(0, 0);
102
103 loop_->Update(disabled);
104}
105
106void ShooterController::SetStatus(ShooterStatus *status) {
107 status->avg_angular_velocity = average_angular_velocity_;
108
109 status->angular_velocity = X_hat_current_(1, 0);
Austin Schuh169f5f22017-02-18 16:31:13 -0800110 status->ready = ready_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800111
112 status->voltage_error = X_hat_current_(2, 0);
113 status->position_error = position_error_;
114 status->instantaneous_velocity = dt_velocity_;
115}
116
117void Shooter::Reset() { wheel_.Reset(); }
118
119void Shooter::Iterate(const control_loops::ShooterGoal *goal,
120 const double *position, double *output,
121 control_loops::ShooterStatus *status) {
122 if (goal) {
123 // Update position/goal for our wheel.
124 wheel_.set_goal(goal->angular_velocity);
125 }
126
127 wheel_.set_position(*position);
128
129 wheel_.Update(output == nullptr);
130
131 wheel_.SetStatus(status);
132
133 if (last_ready_ && !status->ready) {
134 min_ = wheel_.dt_velocity();
135 } else if (!status->ready) {
136 min_ = ::std::min(min_, wheel_.dt_velocity());
137 } else if (!last_ready_ && status->ready) {
138 LOG(INFO, "Shot min was [%f]\n", min_);
139 }
140
141 if (output) {
142 *output = wheel_.voltage();
143 }
144 last_ready_ = status->ready;
145}
146
147} // namespace shooter
148} // namespace superstructure
149} // namespace control_loops
150} // namespace y2017