blob: 18fc795a1f96461f808dbfe30144aba0b500c79e [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 Schuh932a5ce2017-03-05 01:04:18 -080045 dt_position_ = current_position - last_position_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080046 last_position_ = current_position;
47}
48
49double ShooterController::voltage() const { return loop_->U(0, 0); }
50
51void ShooterController::Reset() { reset_ = true; }
52
Austin Schuh932a5ce2017-03-05 01:04:18 -080053void ShooterController::Update(bool disabled, chrono::nanoseconds dt) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080054 loop_->mutable_R() = loop_->next_R();
55 if (::std::abs(loop_->R(1, 0)) < 1.0) {
56 // Kill power at low angular velocities.
57 disabled = true;
58 }
59
60 loop_->Correct(Y_);
61
62 // Compute the oldest point in the history.
63 const int oldest_history_position =
64 ((history_position_ == 0) ? kHistoryLength : history_position_) - 1;
65
66 // Compute the distance moved over that time period.
67 average_angular_velocity_ =
68 (history_[oldest_history_position] - history_[history_position_]) /
69 (chrono::duration_cast<chrono::duration<double>>(
70 ::aos::controls::kLoopFrequency)
71 .count() *
72 static_cast<double>(kHistoryLength - 1));
73
74 // Ready if average angular velocity is close to the goal.
75 error_ = average_angular_velocity_ - loop_->next_R(1, 0);
76
77 ready_ = std::abs(error_) < kTolerance && loop_->next_R(1, 0) > 1.0;
78
79 if (last_ready_ && !ready_ && loop_->next_R(1, 0) > 1.0 && error_ < 0.0) {
80 needs_reset_ = true;
81 min_velocity_ = loop_->X_hat(1, 0);
82 }
83 if (needs_reset_) {
84 min_velocity_ = ::std::min(min_velocity_, loop_->X_hat(1, 0));
85 if (loop_->X_hat(1, 0) > min_velocity_ + 5.0) {
86 reset_ = true;
87 needs_reset_ = false;
88 }
89 }
90 if (reset_) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080091 loop_->mutable_X_hat(0, 0) = Y_(0, 0);
Austin Schuhcd3237a2017-02-18 14:19:26 -080092 // TODO(austin): This should be 0 for the WPILib reset since dt_velocity_
93 // will be rubbish.
94 loop_->mutable_X_hat(1, 0) = dt_velocity_;
95 loop_->mutable_X_hat(2, 0) = 0.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080096 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);
Austin Schuh932a5ce2017-03-05 01:04:18 -0800102 dt_velocity_ = dt_position_ /
103 chrono::duration_cast<chrono::duration<double>>(dt).count();
104 fixed_dt_velocity_ = dt_position_ / 0.005;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800105
Austin Schuh932a5ce2017-03-05 01:04:18 -0800106 loop_->Update(disabled, dt);
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800107}
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_;
Austin Schuh932a5ce2017-03-05 01:04:18 -0800118 status->fixed_instantaneous_velocity = fixed_dt_velocity_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800119}
120
121void Shooter::Reset() { wheel_.Reset(); }
122
123void Shooter::Iterate(const control_loops::ShooterGoal *goal,
Austin Schuh932a5ce2017-03-05 01:04:18 -0800124 const double *position,
125 ::aos::monotonic_clock::time_point position_time,
126 double *output, control_loops::ShooterStatus *status) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800127 if (goal) {
128 // Update position/goal for our wheel.
129 wheel_.set_goal(goal->angular_velocity);
130 }
131
132 wheel_.set_position(*position);
133
Austin Schuh932a5ce2017-03-05 01:04:18 -0800134 chrono::nanoseconds dt = ::aos::controls::kLoopFrequency;
135 if (last_time_ != ::aos::monotonic_clock::min_time) {
136 dt = position_time - last_time_;
137 }
138 last_time_ = position_time;
139
140 wheel_.Update(output == nullptr, dt);
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800141
142 wheel_.SetStatus(status);
143
144 if (last_ready_ && !status->ready) {
145 min_ = wheel_.dt_velocity();
146 } else if (!status->ready) {
147 min_ = ::std::min(min_, wheel_.dt_velocity());
148 } else if (!last_ready_ && status->ready) {
149 LOG(INFO, "Shot min was [%f]\n", min_);
150 }
151
152 if (output) {
153 *output = wheel_.voltage();
154 }
155 last_ready_ = status->ready;
156}
157
158} // namespace shooter
159} // namespace superstructure
160} // namespace control_loops
161} // namespace y2017