blob: c4d16304a6e168f9a397639983598eac15938db0 [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_) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080093 loop_->mutable_X_hat(0, 0) = Y_(0, 0);
Austin Schuhcd3237a2017-02-18 14:19:26 -080094 // TODO(austin): This should be 0 for the WPILib reset since dt_velocity_
95 // will be rubbish.
96 loop_->mutable_X_hat(1, 0) = dt_velocity_;
97 loop_->mutable_X_hat(2, 0) = 0.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080098 reset_ = false;
99 }
100 last_ready_ = ready_;
101
102 X_hat_current_ = loop_->X_hat();
103 position_error_ = X_hat_current_(0, 0) - Y_(0, 0);
104
105 loop_->Update(disabled);
106}
107
108void ShooterController::SetStatus(ShooterStatus *status) {
109 status->avg_angular_velocity = average_angular_velocity_;
110
111 status->angular_velocity = X_hat_current_(1, 0);
Austin Schuh169f5f22017-02-18 16:31:13 -0800112 status->ready = ready_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800113
114 status->voltage_error = X_hat_current_(2, 0);
115 status->position_error = position_error_;
116 status->instantaneous_velocity = dt_velocity_;
117}
118
119void Shooter::Reset() { wheel_.Reset(); }
120
121void Shooter::Iterate(const control_loops::ShooterGoal *goal,
122 const double *position, double *output,
123 control_loops::ShooterStatus *status) {
124 if (goal) {
125 // Update position/goal for our wheel.
126 wheel_.set_goal(goal->angular_velocity);
127 }
128
129 wheel_.set_position(*position);
130
131 wheel_.Update(output == nullptr);
132
133 wheel_.SetStatus(status);
134
135 if (last_ready_ && !status->ready) {
136 min_ = wheel_.dt_velocity();
137 } else if (!status->ready) {
138 min_ = ::std::min(min_, wheel_.dt_velocity());
139 } else if (!last_ready_ && status->ready) {
140 LOG(INFO, "Shot min was [%f]\n", min_);
141 }
142
143 if (output) {
144 *output = wheel_.voltage();
145 }
146 last_ready_ = status->ready;
147}
148
149} // namespace shooter
150} // namespace superstructure
151} // namespace control_loops
152} // namespace y2017