blob: 8991658a3f312a8c7d2201ca3282553ca8269177 [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()
Austin Schuh3ad5ed82017-02-25 21:36:19 -080026 : loop_(new StateFeedbackLoop<3, 1, 1, StateFeedbackHybridPlant<3, 1, 1>,
27 HybridKalman<3, 1, 1>>(
Tyler Chatow2737d2a2017-02-08 21:20:51 -080028 superstructure::shooter::MakeIntegralShooterLoop())) {
29 history_.fill(0);
30 Y_.setZero();
31}
32
33void ShooterController::set_goal(double angular_velocity_goal) {
34 loop_->mutable_next_R() << 0.0, angular_velocity_goal, 0.0;
35}
36
37void ShooterController::set_position(double current_position) {
38 // Update position in the model.
39 Y_ << current_position;
40
41 // Add the position to the history.
42 history_[history_position_] = current_position;
43 history_position_ = (history_position_ + 1) % kHistoryLength;
44
Austin Schuh169f5f22017-02-18 16:31:13 -080045 dt_velocity_ = (current_position - last_position_) /
46 chrono::duration_cast<chrono::duration<double>>(
47 ::aos::controls::kLoopFrequency)
48 .count();
Tyler Chatow2737d2a2017-02-08 21:20:51 -080049 last_position_ = current_position;
50}
51
52double ShooterController::voltage() const { return loop_->U(0, 0); }
53
54void ShooterController::Reset() { reset_ = true; }
55
56void ShooterController::Update(bool disabled) {
57 loop_->mutable_R() = loop_->next_R();
58 if (::std::abs(loop_->R(1, 0)) < 1.0) {
59 // Kill power at low angular velocities.
60 disabled = true;
61 }
62
63 loop_->Correct(Y_);
64
65 // Compute the oldest point in the history.
66 const int oldest_history_position =
67 ((history_position_ == 0) ? kHistoryLength : history_position_) - 1;
68
69 // Compute the distance moved over that time period.
70 average_angular_velocity_ =
71 (history_[oldest_history_position] - history_[history_position_]) /
72 (chrono::duration_cast<chrono::duration<double>>(
73 ::aos::controls::kLoopFrequency)
74 .count() *
75 static_cast<double>(kHistoryLength - 1));
76
77 // Ready if average angular velocity is close to the goal.
78 error_ = average_angular_velocity_ - loop_->next_R(1, 0);
79
80 ready_ = std::abs(error_) < kTolerance && loop_->next_R(1, 0) > 1.0;
81
82 if (last_ready_ && !ready_ && loop_->next_R(1, 0) > 1.0 && error_ < 0.0) {
83 needs_reset_ = true;
84 min_velocity_ = loop_->X_hat(1, 0);
85 }
86 if (needs_reset_) {
87 min_velocity_ = ::std::min(min_velocity_, loop_->X_hat(1, 0));
88 if (loop_->X_hat(1, 0) > min_velocity_ + 5.0) {
89 reset_ = true;
90 needs_reset_ = false;
91 }
92 }
93 if (reset_) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080094 loop_->mutable_X_hat(0, 0) = Y_(0, 0);
Austin Schuhcd3237a2017-02-18 14:19:26 -080095 // TODO(austin): This should be 0 for the WPILib reset since dt_velocity_
96 // will be rubbish.
97 loop_->mutable_X_hat(1, 0) = dt_velocity_;
98 loop_->mutable_X_hat(2, 0) = 0.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080099 reset_ = false;
100 }
101 last_ready_ = ready_;
102
103 X_hat_current_ = loop_->X_hat();
104 position_error_ = X_hat_current_(0, 0) - Y_(0, 0);
105
106 loop_->Update(disabled);
107}
108
109void ShooterController::SetStatus(ShooterStatus *status) {
110 status->avg_angular_velocity = average_angular_velocity_;
111
112 status->angular_velocity = X_hat_current_(1, 0);
Austin Schuh169f5f22017-02-18 16:31:13 -0800113 status->ready = ready_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800114
115 status->voltage_error = X_hat_current_(2, 0);
116 status->position_error = position_error_;
117 status->instantaneous_velocity = dt_velocity_;
118}
119
120void Shooter::Reset() { wheel_.Reset(); }
121
122void Shooter::Iterate(const control_loops::ShooterGoal *goal,
123 const double *position, double *output,
124 control_loops::ShooterStatus *status) {
125 if (goal) {
126 // Update position/goal for our wheel.
127 wheel_.set_goal(goal->angular_velocity);
128 }
129
130 wheel_.set_position(*position);
131
132 wheel_.Update(output == nullptr);
133
134 wheel_.SetStatus(status);
135
136 if (last_ready_ && !status->ready) {
137 min_ = wheel_.dt_velocity();
138 } else if (!status->ready) {
139 min_ = ::std::min(min_, wheel_.dt_velocity());
140 } else if (!last_ready_ && status->ready) {
141 LOG(INFO, "Shot min was [%f]\n", min_);
142 }
143
144 if (output) {
145 *output = wheel_.voltage();
146 }
147 last_ready_ = status->ready;
148}
149
150} // namespace shooter
151} // namespace superstructure
152} // namespace control_loops
153} // namespace y2017