blob: 795a7d48c38cf9ec00127d1c3553f69d5af1fda9 [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_]) /
70 (chrono::duration_cast<chrono::duration<double>>(
71 ::aos::controls::kLoopFrequency)
72 .count() *
73 static_cast<double>(kHistoryLength - 1));
74
75 // Ready if average angular velocity is close to the goal.
Austin Schuhc66b6fc2017-03-25 19:56:59 -070076 error_ = average_angular_velocity_ - loop_->next_R(2, 0);
Tyler Chatow2737d2a2017-02-08 21:20:51 -080077
Austin Schuhc66b6fc2017-03-25 19:56:59 -070078 ready_ = std::abs(error_) < kTolerance && loop_->next_R(2, 0) > 1.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080079
Austin Schuheb5c22e2017-04-09 18:30:28 -070080 // If we are no longer ready, but were, and are spinning, then we shot a ball.
81 // Reset the KF.
Austin Schuhc66b6fc2017-03-25 19:56:59 -070082 if (last_ready_ && !ready_ && loop_->next_R(2, 0) > 1.0 && error_ < 0.0) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080083 needs_reset_ = true;
Austin Schuhc66b6fc2017-03-25 19:56:59 -070084 min_velocity_ = velocity();
Tyler Chatow2737d2a2017-02-08 21:20:51 -080085 }
86 if (needs_reset_) {
Austin Schuhc66b6fc2017-03-25 19:56:59 -070087 min_velocity_ = ::std::min(min_velocity_, velocity());
88 if (velocity() > min_velocity_ + 5.0) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080089 reset_ = true;
90 needs_reset_ = false;
91 }
92 }
93 if (reset_) {
Austin Schuheb5c22e2017-04-09 18:30:28 -070094 // TODO(austin): I'd rather not be incrementing X_hat each time. Sort out
95 // something better.
Austin Schuh43603512017-04-16 19:11:37 -070096 loop_->mutable_X_hat(3, 0) += 1.0;
Tyler Chatow2737d2a2017-02-08 21:20:51 -080097 reset_ = false;
98 }
99 last_ready_ = ready_;
100
101 X_hat_current_ = loop_->X_hat();
102 position_error_ = X_hat_current_(0, 0) - Y_(0, 0);
Austin Schuh932a5ce2017-03-05 01:04:18 -0800103 dt_velocity_ = dt_position_ /
104 chrono::duration_cast<chrono::duration<double>>(dt).count();
Austin Schuhd6e9fb42017-04-09 17:56:06 -0700105 fixed_dt_velocity_ = dt_position_ / 0.00505;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800106
Austin Schuh932a5ce2017-03-05 01:04:18 -0800107 loop_->Update(disabled, dt);
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800108}
109
110void ShooterController::SetStatus(ShooterStatus *status) {
111 status->avg_angular_velocity = average_angular_velocity_;
112
Austin Schuheb5c22e2017-04-09 18:30:28 -0700113 status->filtered_velocity = X_hat_current_(1, 0);
Austin Schuhc66b6fc2017-03-25 19:56:59 -0700114 status->angular_velocity = X_hat_current_(2, 0);
Austin Schuh169f5f22017-02-18 16:31:13 -0800115 status->ready = ready_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800116
Austin Schuhc66b6fc2017-03-25 19:56:59 -0700117 status->voltage_error = X_hat_current_(3, 0);
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800118 status->position_error = position_error_;
119 status->instantaneous_velocity = dt_velocity_;
Austin Schuh932a5ce2017-03-05 01:04:18 -0800120 status->fixed_instantaneous_velocity = fixed_dt_velocity_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800121}
122
123void Shooter::Reset() { wheel_.Reset(); }
124
125void Shooter::Iterate(const control_loops::ShooterGoal *goal,
Austin Schuh932a5ce2017-03-05 01:04:18 -0800126 const double *position,
127 ::aos::monotonic_clock::time_point position_time,
128 double *output, control_loops::ShooterStatus *status) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800129 if (goal) {
130 // Update position/goal for our wheel.
131 wheel_.set_goal(goal->angular_velocity);
132 }
133
134 wheel_.set_position(*position);
135
Austin Schuh932a5ce2017-03-05 01:04:18 -0800136 chrono::nanoseconds dt = ::aos::controls::kLoopFrequency;
137 if (last_time_ != ::aos::monotonic_clock::min_time) {
138 dt = position_time - last_time_;
139 }
140 last_time_ = position_time;
141
142 wheel_.Update(output == nullptr, dt);
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800143
144 wheel_.SetStatus(status);
145
146 if (last_ready_ && !status->ready) {
147 min_ = wheel_.dt_velocity();
148 } else if (!status->ready) {
149 min_ = ::std::min(min_, wheel_.dt_velocity());
150 } else if (!last_ready_ && status->ready) {
151 LOG(INFO, "Shot min was [%f]\n", min_);
152 }
153
154 if (output) {
155 *output = wheel_.voltage();
156 }
157 last_ready_ = status->ready;
158}
159
160} // namespace shooter
161} // namespace superstructure
162} // namespace control_loops
163} // namespace y2017