blob: c0b9d6ed3f0eb2b9a5be0721203d153a9a1f5754 [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 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_) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -080092 loop_->mutable_X_hat(0, 0) = Y_(0, 0);
Austin Schuhcd3237a2017-02-18 14:19:26 -080093 // TODO(austin): This should be 0 for the WPILib reset since dt_velocity_
94 // will be rubbish.
95 loop_->mutable_X_hat(1, 0) = dt_velocity_;
96 loop_->mutable_X_hat(2, 0) = 0.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();
105 fixed_dt_velocity_ = dt_position_ / 0.005;
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 Schuhc66b6fc2017-03-25 19:56:59 -0700113 status->angular_velocity = X_hat_current_(2, 0);
Austin Schuh169f5f22017-02-18 16:31:13 -0800114 status->ready = ready_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800115
Austin Schuhc66b6fc2017-03-25 19:56:59 -0700116 status->voltage_error = X_hat_current_(3, 0);
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800117 status->position_error = position_error_;
118 status->instantaneous_velocity = dt_velocity_;
Austin Schuh932a5ce2017-03-05 01:04:18 -0800119 status->fixed_instantaneous_velocity = fixed_dt_velocity_;
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800120}
121
122void Shooter::Reset() { wheel_.Reset(); }
123
124void Shooter::Iterate(const control_loops::ShooterGoal *goal,
Austin Schuh932a5ce2017-03-05 01:04:18 -0800125 const double *position,
126 ::aos::monotonic_clock::time_point position_time,
127 double *output, control_loops::ShooterStatus *status) {
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800128 if (goal) {
129 // Update position/goal for our wheel.
130 wheel_.set_goal(goal->angular_velocity);
131 }
132
133 wheel_.set_position(*position);
134
Austin Schuh932a5ce2017-03-05 01:04:18 -0800135 chrono::nanoseconds dt = ::aos::controls::kLoopFrequency;
136 if (last_time_ != ::aos::monotonic_clock::min_time) {
137 dt = position_time - last_time_;
138 }
139 last_time_ = position_time;
140
141 wheel_.Update(output == nullptr, dt);
Tyler Chatow2737d2a2017-02-08 21:20:51 -0800142
143 wheel_.SetStatus(status);
144
145 if (last_ready_ && !status->ready) {
146 min_ = wheel_.dt_velocity();
147 } else if (!status->ready) {
148 min_ = ::std::min(min_, wheel_.dt_velocity());
149 } else if (!last_ready_ && status->ready) {
150 LOG(INFO, "Shot min was [%f]\n", min_);
151 }
152
153 if (output) {
154 *output = wheel_.voltage();
155 }
156 last_ready_ = status->ready;
157}
158
159} // namespace shooter
160} // namespace superstructure
161} // namespace control_loops
162} // namespace y2017