blob: c73a00892f2abab6b19954223e08ee8cf3894745 [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
44 dt_velocity_ = (current_position - last_position_) / 0.005;
45 last_position_ = current_position;
46}
47
48double ShooterController::voltage() const { return loop_->U(0, 0); }
49
50void ShooterController::Reset() { reset_ = true; }
51
52void ShooterController::Update(bool disabled) {
53 loop_->mutable_R() = loop_->next_R();
54 if (::std::abs(loop_->R(1, 0)) < 1.0) {
55 // Kill power at low angular velocities.
56 disabled = true;
57 }
58
59 loop_->Correct(Y_);
60
61 // Compute the oldest point in the history.
62 const int oldest_history_position =
63 ((history_position_ == 0) ? kHistoryLength : history_position_) - 1;
64
65 // Compute the distance moved over that time period.
66 average_angular_velocity_ =
67 (history_[oldest_history_position] - history_[history_position_]) /
68 (chrono::duration_cast<chrono::duration<double>>(
69 ::aos::controls::kLoopFrequency)
70 .count() *
71 static_cast<double>(kHistoryLength - 1));
72
73 // Ready if average angular velocity is close to the goal.
74 error_ = average_angular_velocity_ - loop_->next_R(1, 0);
75
76 ready_ = std::abs(error_) < kTolerance && loop_->next_R(1, 0) > 1.0;
77
78 if (last_ready_ && !ready_ && loop_->next_R(1, 0) > 1.0 && error_ < 0.0) {
79 needs_reset_ = true;
80 min_velocity_ = loop_->X_hat(1, 0);
81 }
82 if (needs_reset_) {
83 min_velocity_ = ::std::min(min_velocity_, loop_->X_hat(1, 0));
84 if (loop_->X_hat(1, 0) > min_velocity_ + 5.0) {
85 reset_ = true;
86 needs_reset_ = false;
87 }
88 }
89 if (reset_) {
90 loop_->mutable_X_hat(2, 0) = 0.0;
91 loop_->mutable_X_hat(1, 0) = dt_velocity_;
92 loop_->mutable_X_hat(0, 0) = Y_(0, 0);
93 reset_ = false;
94 }
95 last_ready_ = ready_;
96
97 X_hat_current_ = loop_->X_hat();
98 position_error_ = X_hat_current_(0, 0) - Y_(0, 0);
99
100 loop_->Update(disabled);
101}
102
103void ShooterController::SetStatus(ShooterStatus *status) {
104 status->avg_angular_velocity = average_angular_velocity_;
105
106 status->angular_velocity = X_hat_current_(1, 0);
107 status->ready = std::abs(error_) < kTolerance && loop_->next_R(1, 0) > 1.0;
108
109 status->voltage_error = X_hat_current_(2, 0);
110 status->position_error = position_error_;
111 status->instantaneous_velocity = dt_velocity_;
112}
113
114void Shooter::Reset() { wheel_.Reset(); }
115
116void Shooter::Iterate(const control_loops::ShooterGoal *goal,
117 const double *position, double *output,
118 control_loops::ShooterStatus *status) {
119 if (goal) {
120 // Update position/goal for our wheel.
121 wheel_.set_goal(goal->angular_velocity);
122 }
123
124 wheel_.set_position(*position);
125
126 wheel_.Update(output == nullptr);
127
128 wheel_.SetStatus(status);
129
130 if (last_ready_ && !status->ready) {
131 min_ = wheel_.dt_velocity();
132 } else if (!status->ready) {
133 min_ = ::std::min(min_, wheel_.dt_velocity());
134 } else if (!last_ready_ && status->ready) {
135 LOG(INFO, "Shot min was [%f]\n", min_);
136 }
137
138 if (output) {
139 *output = wheel_.voltage();
140 }
141 last_ready_ = status->ready;
142}
143
144} // namespace shooter
145} // namespace superstructure
146} // namespace control_loops
147} // namespace y2017