blob: f8bfecf7fa8adf9bb0013988928865151234c2b2 [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 Schuh20388b62017-11-23 22:40:46 -080026 : loop_(new StateFeedbackLoop<4, 1, 1, double,
27 StateFeedbackHybridPlant<4, 1, 1>,
Austin Schuhc66b6fc2017-03-25 19:56:59 -070028 HybridKalman<4, 1, 1>>(
Tyler Chatow2737d2a2017-02-08 21:20:51 -080029 superstructure::shooter::MakeIntegralShooterLoop())) {
30 history_.fill(0);
31 Y_.setZero();
32}
33
34void ShooterController::set_goal(double angular_velocity_goal) {
Austin Schuhc66b6fc2017-03-25 19:56:59 -070035 loop_->mutable_next_R() << 0.0, angular_velocity_goal, angular_velocity_goal,
36 0.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080037}
38
39void ShooterController::set_position(double current_position) {
40 // Update position in the model.
41 Y_ << current_position;
42
43 // Add the position to the history.
44 history_[history_position_] = current_position;
45 history_position_ = (history_position_ + 1) % kHistoryLength;
46
Austin Schuh932a5ce2017-03-05 01:04:18 -080047 dt_position_ = current_position - last_position_;
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
Austin Schuh932a5ce2017-03-05 01:04:18 -080055void ShooterController::Update(bool disabled, chrono::nanoseconds dt) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080056 loop_->mutable_R() = loop_->next_R();
Austin Schuhc66b6fc2017-03-25 19:56:59 -070057 if (::std::abs(loop_->R(2, 0)) < 1.0) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080058 // 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_]) /
Austin Schuh85387042017-09-13 23:59:21 -070071 (0.00505 * static_cast<double>(kHistoryLength - 1));
Tyler Chatow2737d2a2017-02-08 21:20:51 -080072
73 // Ready if average angular velocity is close to the goal.
Austin Schuhc66b6fc2017-03-25 19:56:59 -070074 error_ = average_angular_velocity_ - loop_->next_R(2, 0);
Tyler Chatow2737d2a2017-02-08 21:20:51 -080075
Austin Schuhc66b6fc2017-03-25 19:56:59 -070076 ready_ = std::abs(error_) < kTolerance && loop_->next_R(2, 0) > 1.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080077
Austin Schuheb5c22e2017-04-09 18:30:28 -070078 // If we are no longer ready, but were, and are spinning, then we shot a ball.
79 // Reset the KF.
Austin Schuhc66b6fc2017-03-25 19:56:59 -070080 if (last_ready_ && !ready_ && loop_->next_R(2, 0) > 1.0 && error_ < 0.0) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080081 needs_reset_ = true;
Austin Schuhc66b6fc2017-03-25 19:56:59 -070082 min_velocity_ = velocity();
Tyler Chatow2737d2a2017-02-08 21:20:51 -080083 }
84 if (needs_reset_) {
Austin Schuhc66b6fc2017-03-25 19:56:59 -070085 min_velocity_ = ::std::min(min_velocity_, velocity());
86 if (velocity() > min_velocity_ + 5.0) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080087 reset_ = true;
88 needs_reset_ = false;
89 }
90 }
91 if (reset_) {
Austin Schuheb5c22e2017-04-09 18:30:28 -070092 // TODO(austin): I'd rather not be incrementing X_hat each time. Sort out
93 // something better.
Austin Schuh43603512017-04-16 19:11:37 -070094 loop_->mutable_X_hat(3, 0) += 1.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080095 reset_ = false;
96 }
97 last_ready_ = ready_;
98
99 X_hat_current_ = loop_->X_hat();
100 position_error_ = X_hat_current_(0, 0) - Y_(0, 0);
Austin Schuh932a5ce2017-03-05 01:04:18 -0800101 dt_velocity_ = dt_position_ /
102 chrono::duration_cast<chrono::duration<double>>(dt).count();
Austin Schuhd6e9fb42017-04-09 17:56:06 -0700103 fixed_dt_velocity_ = dt_position_ / 0.00505;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800104
Austin Schuh932a5ce2017-03-05 01:04:18 -0800105 loop_->Update(disabled, dt);
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800106}
107
108void ShooterController::SetStatus(ShooterStatus *status) {
109 status->avg_angular_velocity = average_angular_velocity_;
110
Austin Schuheb5c22e2017-04-09 18:30:28 -0700111 status->filtered_velocity = X_hat_current_(1, 0);
Austin Schuhc66b6fc2017-03-25 19:56:59 -0700112 status->angular_velocity = X_hat_current_(2, 0);
Austin Schuh169f5f22017-02-18 16:31:13 -0800113 status->ready = ready_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800114
Austin Schuhc66b6fc2017-03-25 19:56:59 -0700115 status->voltage_error = X_hat_current_(3, 0);
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800116 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