blob: 08ab1be2f62a3edb6ea29716201a83256f52cbdd [file] [log] [blame]
Comran Morshed2a97bc82016-01-16 17:27:01 +00001#include "y2016/control_loops/shooter/shooter.h"
2
Austin Schuh214e9c12016-11-25 17:26:20 -08003#include <chrono>
4
John Park33858a32018-09-28 23:05:48 -07005#include "aos/controls/control_loops.q.h"
6#include "aos/logging/logging.h"
7#include "aos/logging/queue_logging.h"
Comran Morshed2a97bc82016-01-16 17:27:01 +00008
9#include "y2016/control_loops/shooter/shooter_plant.h"
10
11namespace y2016 {
12namespace control_loops {
Austin Schuh09c2b0b2016-02-13 15:53:16 -080013namespace shooter {
14
Austin Schuh214e9c12016-11-25 17:26:20 -080015namespace chrono = ::std::chrono;
Austin Schuh94a54102016-11-26 15:14:35 -080016using ::aos::monotonic_clock;
Austin Schuh7eecd7c2016-02-28 21:59:05 -080017
Austin Schuh09c2b0b2016-02-13 15:53:16 -080018// TODO(austin): Pseudo current limit?
Comran Morshed2a97bc82016-01-16 17:27:01 +000019
Comran Morshedcde50322016-01-18 15:10:36 +000020ShooterSide::ShooterSide()
Austin Schuh09c2b0b2016-02-13 15:53:16 -080021 : loop_(new StateFeedbackLoop<3, 1, 1>(MakeIntegralShooterLoop())) {
22 history_.fill(0);
23 Y_.setZero();
Comran Morshed2a97bc82016-01-16 17:27:01 +000024}
25
Austin Schuh09c2b0b2016-02-13 15:53:16 -080026void ShooterSide::set_goal(double angular_velocity_goal) {
27 loop_->mutable_next_R() << 0.0, angular_velocity_goal, 0.0;
Comran Morshedcde50322016-01-18 15:10:36 +000028}
Comran Morshed2a97bc82016-01-16 17:27:01 +000029
Austin Schuh09c2b0b2016-02-13 15:53:16 -080030void ShooterSide::set_position(double current_position) {
Comran Morshedcde50322016-01-18 15:10:36 +000031 // Update position in the model.
Austin Schuh09c2b0b2016-02-13 15:53:16 -080032 Y_ << current_position;
Comran Morshed2a97bc82016-01-16 17:27:01 +000033
Comran Morshedcde50322016-01-18 15:10:36 +000034 // Add the position to the history.
Austin Schuh09c2b0b2016-02-13 15:53:16 -080035 history_[history_position_] = current_position;
Comran Morshedcde50322016-01-18 15:10:36 +000036 history_position_ = (history_position_ + 1) % kHistoryLength;
37}
Comran Morshed2a97bc82016-01-16 17:27:01 +000038
James Kuszmaul651fc3f2019-05-15 21:14:25 -070039double ShooterSide::voltage() const { return loop_->U(0, 0); }
Comran Morshedcde50322016-01-18 15:10:36 +000040
Austin Schuh09c2b0b2016-02-13 15:53:16 -080041void ShooterSide::Update(bool disabled) {
42 loop_->mutable_R() = loop_->next_R();
43 if (loop_->R(1, 0) < 1.0) {
44 // Kill power at low angular velocities.
45 disabled = true;
46 }
47
48 loop_->Correct(Y_);
49 loop_->Update(disabled);
Comran Morshedcde50322016-01-18 15:10:36 +000050}
51
Austin Schuh09c2b0b2016-02-13 15:53:16 -080052void ShooterSide::SetStatus(ShooterSideStatus *status) {
53 // Compute the oldest point in the history.
54 const int oldest_history_position =
55 ((history_position_ == 0) ? kHistoryLength : history_position_) - 1;
Comran Morshedcde50322016-01-18 15:10:36 +000056
Austin Schuh09c2b0b2016-02-13 15:53:16 -080057 // Compute the distance moved over that time period.
58 status->avg_angular_velocity =
59 (history_[oldest_history_position] - history_[history_position_]) /
James Kuszmaul651fc3f2019-05-15 21:14:25 -070060 (::aos::time::DurationInSeconds(::aos::controls::kLoopFrequency) *
Austin Schuh09c2b0b2016-02-13 15:53:16 -080061 static_cast<double>(kHistoryLength - 1));
62
63 status->angular_velocity = loop_->X_hat(1, 0);
64
65 // Ready if average angular velocity is close to the goal.
66 status->ready = (std::abs(loop_->next_R(1, 0) -
67 status->avg_angular_velocity) < kTolerance &&
68 loop_->next_R(1, 0) > 1.0);
69}
70
Austin Schuh55a13dc2019-01-27 22:39:03 -080071Shooter::Shooter(::aos::EventLoop *event_loop, const ::std::string &name)
72 : aos::controls::ControlLoop<ShooterQueue>(event_loop, name),
Austin Schuhf59b8ee2016-03-19 21:31:36 -070073 shots_(0),
Austin Schuh94a54102016-11-26 15:14:35 -080074 last_pre_shot_timeout_(::aos::monotonic_clock::min_time) {}
Austin Schuh09c2b0b2016-02-13 15:53:16 -080075
76void Shooter::RunIteration(const ShooterQueue::Goal *goal,
77 const ShooterQueue::Position *position,
78 ShooterQueue::Output *output,
79 ShooterQueue::Status *status) {
Comran Morshedcde50322016-01-18 15:10:36 +000080 if (goal) {
81 // Update position/goal for our two shooter sides.
Austin Schuh09c2b0b2016-02-13 15:53:16 -080082 left_.set_goal(goal->angular_velocity);
83 right_.set_goal(goal->angular_velocity);
Austin Schuhe0729a62016-03-12 21:54:17 -080084
85 // Turn the lights on if we are supposed to spin.
86 if (output) {
Austin Schuhb2c33382016-04-03 16:09:17 -070087 if (::std::abs(goal->angular_velocity) > 0.0) {
88 output->lights_on = true;
89 if (goal->shooting_forwards) {
90 output->forwards_flashlight = true;
91 output->backwards_flashlight = false;
92 } else {
93 output->forwards_flashlight = false;
94 output->backwards_flashlight = true;
95 }
96 }
97 if (goal->force_lights_on) {
Austin Schuhe0729a62016-03-12 21:54:17 -080098 output->lights_on = true;
99 }
100 }
Comran Morshedcde50322016-01-18 15:10:36 +0000101 }
102
Austin Schuh09c2b0b2016-02-13 15:53:16 -0800103 left_.set_position(position->theta_left);
104 right_.set_position(position->theta_right);
Comran Morshedcde50322016-01-18 15:10:36 +0000105
Austin Schuh09c2b0b2016-02-13 15:53:16 -0800106 left_.Update(output == nullptr);
107 right_.Update(output == nullptr);
Comran Morshedcde50322016-01-18 15:10:36 +0000108
Austin Schuh09c2b0b2016-02-13 15:53:16 -0800109 left_.SetStatus(&status->left);
110 right_.SetStatus(&status->right);
111 status->ready = (status->left.ready && status->right.ready);
Comran Morshed2a97bc82016-01-16 17:27:01 +0000112
113 if (output) {
Austin Schuh09c2b0b2016-02-13 15:53:16 -0800114 output->voltage_left = left_.voltage();
115 output->voltage_right = right_.voltage();
Comran Morshedb79c4242016-02-06 18:27:26 +0000116
117 if (goal) {
Austin Schuh7eecd7c2016-02-28 21:59:05 -0800118 bool shoot = false;
119 switch (state_) {
120 case ShooterLatchState::PASS_THROUGH:
121 if (goal->push_to_shooter) {
122 if (::std::abs(goal->angular_velocity) > 10) {
123 if (status->ready) {
124 state_ = ShooterLatchState::WAITING_FOR_SPINDOWN;
125 shoot = true;
126 }
127 } else {
128 shoot = true;
129 }
130 }
Austin Schuh94a54102016-11-26 15:14:35 -0800131 last_pre_shot_timeout_ = monotonic_clock::now() + chrono::seconds(1);
Austin Schuh7eecd7c2016-02-28 21:59:05 -0800132 break;
133 case ShooterLatchState::WAITING_FOR_SPINDOWN:
134 shoot = true;
135 if (left_.velocity() < goal->angular_velocity * 0.9 ||
136 right_.velocity() < goal->angular_velocity * 0.9) {
137 state_ = ShooterLatchState::WAITING_FOR_SPINUP;
138 }
139 if (::std::abs(goal->angular_velocity) < 10 ||
Austin Schuh94a54102016-11-26 15:14:35 -0800140 last_pre_shot_timeout_ < monotonic_clock::now()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700141 state_ = ShooterLatchState::INCREMENT_SHOT_COUNT;
Austin Schuh7eecd7c2016-02-28 21:59:05 -0800142 }
143 break;
144 case ShooterLatchState::WAITING_FOR_SPINUP:
145 shoot = true;
146 if (left_.velocity() > goal->angular_velocity * 0.95 &&
147 right_.velocity() > goal->angular_velocity * 0.95) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700148 state_ = ShooterLatchState::INCREMENT_SHOT_COUNT;
Austin Schuh7eecd7c2016-02-28 21:59:05 -0800149 }
150 if (::std::abs(goal->angular_velocity) < 10 ||
Austin Schuh94a54102016-11-26 15:14:35 -0800151 last_pre_shot_timeout_ < monotonic_clock::now()) {
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700152 state_ = ShooterLatchState::INCREMENT_SHOT_COUNT;
Austin Schuh7eecd7c2016-02-28 21:59:05 -0800153 }
154 break;
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700155 case ShooterLatchState::INCREMENT_SHOT_COUNT:
156 ++shots_;
157 state_ = ShooterLatchState::WAITING_FOR_SHOT_NEGEDGE;
158 break;
Austin Schuh7eecd7c2016-02-28 21:59:05 -0800159 case ShooterLatchState::WAITING_FOR_SHOT_NEGEDGE:
160 shoot = true;
161 if (!goal->push_to_shooter) {
162 state_ = ShooterLatchState::PASS_THROUGH;
163 }
164 break;
165 }
166
Comran Morshedb79c4242016-02-06 18:27:26 +0000167 output->clamp_open = goal->clamp_open;
Austin Schuh7eecd7c2016-02-28 21:59:05 -0800168 output->push_to_shooter = shoot;
Comran Morshedb79c4242016-02-06 18:27:26 +0000169 }
Comran Morshed2a97bc82016-01-16 17:27:01 +0000170 }
Austin Schuhf59b8ee2016-03-19 21:31:36 -0700171
172 status->shots = shots_;
Comran Morshed2a97bc82016-01-16 17:27:01 +0000173}
174
Austin Schuh09c2b0b2016-02-13 15:53:16 -0800175} // namespace shooter
Comran Morshed2a97bc82016-01-16 17:27:01 +0000176} // namespace control_loops
177} // namespace y2016