blob: 03abd999c3b3b62e0fafa7b57b1233fff04afe39 [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 Schuhc66b6fc2017-03-25 19:56:59 -070026 : loop_(new StateFeedbackLoop<4, 1, 1, StateFeedbackHybridPlant<4, 1, 1>,
27 HybridKalman<4, 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) {
Austin Schuhc66b6fc2017-03-25 19:56:59 -070034 loop_->mutable_next_R() << 0.0, angular_velocity_goal, angular_velocity_goal,
35 0.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080036}
37
38void ShooterController::set_position(double current_position) {
39 // Update position in the model.
40 Y_ << current_position;
41
42 // Add the position to the history.
43 history_[history_position_] = current_position;
44 history_position_ = (history_position_ + 1) % kHistoryLength;
45
Austin Schuh932a5ce2017-03-05 01:04:18 -080046 dt_position_ = current_position - last_position_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080047 last_position_ = current_position;
48}
49
50double ShooterController::voltage() const { return loop_->U(0, 0); }
51
52void ShooterController::Reset() { reset_ = true; }
53
Austin Schuh932a5ce2017-03-05 01:04:18 -080054void ShooterController::Update(bool disabled, chrono::nanoseconds dt) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080055 loop_->mutable_R() = loop_->next_R();
Austin Schuhc66b6fc2017-03-25 19:56:59 -070056 if (::std::abs(loop_->R(2, 0)) < 1.0) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080057 // Kill power at low angular velocities.
58 disabled = true;
59 }
60
61 loop_->Correct(Y_);
62
63 // Compute the oldest point in the history.
64 const int oldest_history_position =
65 ((history_position_ == 0) ? kHistoryLength : history_position_) - 1;
66
67 // Compute the distance moved over that time period.
68 average_angular_velocity_ =
69 (history_[oldest_history_position] - history_[history_position_]) /
Austin Schuh85387042017-09-13 23:59:21 -070070 (0.00505 * static_cast<double>(kHistoryLength - 1));
Tyler Chatow2737d2a2017-02-08 21:20:51 -080071
72 // Ready if average angular velocity is close to the goal.
Austin Schuhc66b6fc2017-03-25 19:56:59 -070073 error_ = average_angular_velocity_ - loop_->next_R(2, 0);
Tyler Chatow2737d2a2017-02-08 21:20:51 -080074
Austin Schuhc66b6fc2017-03-25 19:56:59 -070075 ready_ = std::abs(error_) < kTolerance && loop_->next_R(2, 0) > 1.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080076
Austin Schuheb5c22e2017-04-09 18:30:28 -070077 // If we are no longer ready, but were, and are spinning, then we shot a ball.
78 // Reset the KF.
Austin Schuhc66b6fc2017-03-25 19:56:59 -070079 if (last_ready_ && !ready_ && loop_->next_R(2, 0) > 1.0 && error_ < 0.0) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080080 needs_reset_ = true;
Austin Schuhc66b6fc2017-03-25 19:56:59 -070081 min_velocity_ = velocity();
Tyler Chatow2737d2a2017-02-08 21:20:51 -080082 }
83 if (needs_reset_) {
Austin Schuhc66b6fc2017-03-25 19:56:59 -070084 min_velocity_ = ::std::min(min_velocity_, velocity());
85 if (velocity() > min_velocity_ + 5.0) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080086 reset_ = true;
87 needs_reset_ = false;
88 }
89 }
90 if (reset_) {
Austin Schuheb5c22e2017-04-09 18:30:28 -070091 // TODO(austin): I'd rather not be incrementing X_hat each time. Sort out
92 // something better.
Austin Schuh43603512017-04-16 19:11:37 -070093 loop_->mutable_X_hat(3, 0) += 1.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080094 reset_ = false;
95 }
96 last_ready_ = ready_;
97
98 X_hat_current_ = loop_->X_hat();
99 position_error_ = X_hat_current_(0, 0) - Y_(0, 0);
Austin Schuh932a5ce2017-03-05 01:04:18 -0800100 dt_velocity_ = dt_position_ /
101 chrono::duration_cast<chrono::duration<double>>(dt).count();
Austin Schuhd6e9fb42017-04-09 17:56:06 -0700102 fixed_dt_velocity_ = dt_position_ / 0.00505;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800103
Austin Schuh932a5ce2017-03-05 01:04:18 -0800104 loop_->Update(disabled, dt);
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800105}
106
107void ShooterController::SetStatus(ShooterStatus *status) {
108 status->avg_angular_velocity = average_angular_velocity_;
109
Austin Schuheb5c22e2017-04-09 18:30:28 -0700110 status->filtered_velocity = X_hat_current_(1, 0);
Austin Schuhc66b6fc2017-03-25 19:56:59 -0700111 status->angular_velocity = X_hat_current_(2, 0);
Austin Schuh169f5f22017-02-18 16:31:13 -0800112 status->ready = ready_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800113
Austin Schuhc66b6fc2017-03-25 19:56:59 -0700114 status->voltage_error = X_hat_current_(3, 0);
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800115 status->position_error = position_error_;
116 status->instantaneous_velocity = dt_velocity_;
Austin Schuh932a5ce2017-03-05 01:04:18 -0800117 status->fixed_instantaneous_velocity = fixed_dt_velocity_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800118}
119
120void Shooter::Reset() { wheel_.Reset(); }
121
122void Shooter::Iterate(const control_loops::ShooterGoal *goal,
Austin Schuh932a5ce2017-03-05 01:04:18 -0800123 const double *position,
124 ::aos::monotonic_clock::time_point position_time,
125 double *output, control_loops::ShooterStatus *status) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800126 if (goal) {
127 // Update position/goal for our wheel.
128 wheel_.set_goal(goal->angular_velocity);
129 }
130
131 wheel_.set_position(*position);
132
Austin Schuh932a5ce2017-03-05 01:04:18 -0800133 chrono::nanoseconds dt = ::aos::controls::kLoopFrequency;
134 if (last_time_ != ::aos::monotonic_clock::min_time) {
135 dt = position_time - last_time_;
136 }
137 last_time_ = position_time;
138
139 wheel_.Update(output == nullptr, dt);
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800140
141 wheel_.SetStatus(status);
142
143 if (last_ready_ && !status->ready) {
144 min_ = wheel_.dt_velocity();
145 } else if (!status->ready) {
146 min_ = ::std::min(min_, wheel_.dt_velocity());
147 } else if (!last_ready_ && status->ready) {
148 LOG(INFO, "Shot min was [%f]\n", min_);
149 }
150
151 if (output) {
152 *output = wheel_.voltage();
153 }
154 last_ready_ = status->ready;
155}
156
157} // namespace shooter
158} // namespace superstructure
159} // namespace control_loops
160} // namespace y2017