Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 1 | #include "y2017/control_loops/superstructure/shooter/shooter.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 5 | #include "aos/logging/logging.h" |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 6 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 7 | namespace y2017::control_loops::superstructure::shooter { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 8 | |
| 9 | namespace chrono = ::std::chrono; |
| 10 | using ::aos::monotonic_clock; |
| 11 | |
| 12 | namespace { |
| 13 | constexpr double kTolerance = 10.0; |
| 14 | } // namespace |
| 15 | |
| 16 | // TODO(austin): Pseudo current limit? |
| 17 | |
| 18 | ShooterController::ShooterController() |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 19 | : loop_(new StateFeedbackLoop<4, 1, 1, double, |
| 20 | StateFeedbackHybridPlant<4, 1, 1>, |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 21 | HybridKalman<4, 1, 1>>( |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 22 | superstructure::shooter::MakeIntegralShooterLoop())) { |
| 23 | history_.fill(0); |
| 24 | Y_.setZero(); |
| 25 | } |
| 26 | |
| 27 | void ShooterController::set_goal(double angular_velocity_goal) { |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 28 | loop_->mutable_next_R() << 0.0, angular_velocity_goal, angular_velocity_goal, |
| 29 | 0.0; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void ShooterController::set_position(double current_position) { |
| 33 | // Update position in the model. |
| 34 | Y_ << current_position; |
| 35 | |
| 36 | // Add the position to the history. |
| 37 | history_[history_position_] = current_position; |
| 38 | history_position_ = (history_position_ + 1) % kHistoryLength; |
| 39 | |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 40 | dt_position_ = current_position - last_position_; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 41 | last_position_ = current_position; |
| 42 | } |
| 43 | |
| 44 | double ShooterController::voltage() const { return loop_->U(0, 0); } |
| 45 | |
| 46 | void ShooterController::Reset() { reset_ = true; } |
| 47 | |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 48 | void ShooterController::Update(bool disabled, chrono::nanoseconds dt) { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 49 | loop_->mutable_R() = loop_->next_R(); |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 50 | if (::std::abs(loop_->R(2, 0)) < 1.0) { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 51 | // Kill power at low angular velocities. |
| 52 | disabled = true; |
| 53 | } |
| 54 | |
| 55 | loop_->Correct(Y_); |
| 56 | |
| 57 | // Compute the oldest point in the history. |
| 58 | const int oldest_history_position = |
| 59 | ((history_position_ == 0) ? kHistoryLength : history_position_) - 1; |
| 60 | |
| 61 | // Compute the distance moved over that time period. |
| 62 | average_angular_velocity_ = |
| 63 | (history_[oldest_history_position] - history_[history_position_]) / |
Austin Schuh | 8538704 | 2017-09-13 23:59:21 -0700 | [diff] [blame] | 64 | (0.00505 * static_cast<double>(kHistoryLength - 1)); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 65 | |
| 66 | // Ready if average angular velocity is close to the goal. |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 67 | error_ = average_angular_velocity_ - loop_->next_R(2, 0); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 68 | |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 69 | ready_ = std::abs(error_) < kTolerance && loop_->next_R(2, 0) > 1.0; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 70 | |
Austin Schuh | eb5c22e | 2017-04-09 18:30:28 -0700 | [diff] [blame] | 71 | // If we are no longer ready, but were, and are spinning, then we shot a ball. |
| 72 | // Reset the KF. |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 73 | if (last_ready_ && !ready_ && loop_->next_R(2, 0) > 1.0 && error_ < 0.0) { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 74 | needs_reset_ = true; |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 75 | min_velocity_ = velocity(); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 76 | } |
| 77 | if (needs_reset_) { |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 78 | min_velocity_ = ::std::min(min_velocity_, velocity()); |
| 79 | if (velocity() > min_velocity_ + 5.0) { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 80 | reset_ = true; |
| 81 | needs_reset_ = false; |
| 82 | } |
| 83 | } |
| 84 | if (reset_) { |
Austin Schuh | eb5c22e | 2017-04-09 18:30:28 -0700 | [diff] [blame] | 85 | // TODO(austin): I'd rather not be incrementing X_hat each time. Sort out |
| 86 | // something better. |
Austin Schuh | 4360351 | 2017-04-16 19:11:37 -0700 | [diff] [blame] | 87 | loop_->mutable_X_hat(3, 0) += 1.0; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 88 | reset_ = false; |
| 89 | } |
| 90 | last_ready_ = ready_; |
| 91 | |
| 92 | X_hat_current_ = loop_->X_hat(); |
| 93 | position_error_ = X_hat_current_(0, 0) - Y_(0, 0); |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 94 | dt_velocity_ = dt_position_ / ::aos::time::DurationInSeconds(dt); |
Austin Schuh | d6e9fb4 | 2017-04-09 17:56:06 -0700 | [diff] [blame] | 95 | fixed_dt_velocity_ = dt_position_ / 0.00505; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 96 | |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 97 | loop_->Update(disabled, dt); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 100 | flatbuffers::Offset<ShooterStatus> ShooterController::BuildStatus( |
| 101 | flatbuffers::FlatBufferBuilder *fbb) { |
| 102 | ShooterStatus::Builder status_builder(*fbb); |
| 103 | status_builder.add_avg_angular_velocity(average_angular_velocity_); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 104 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 105 | status_builder.add_filtered_velocity(X_hat_current_(1, 0)); |
| 106 | status_builder.add_angular_velocity(X_hat_current_(2, 0)); |
| 107 | status_builder.add_ready(ready_); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 108 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 109 | status_builder.add_voltage_error(X_hat_current_(3, 0)); |
| 110 | status_builder.add_position_error(position_error_); |
| 111 | status_builder.add_instantaneous_velocity(dt_velocity_); |
| 112 | status_builder.add_fixed_instantaneous_velocity(fixed_dt_velocity_); |
| 113 | |
| 114 | return status_builder.Finish(); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void Shooter::Reset() { wheel_.Reset(); } |
| 118 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 119 | flatbuffers::Offset<ShooterStatus> Shooter::Iterate( |
| 120 | const ShooterGoalT *goal, const double position, |
| 121 | ::aos::monotonic_clock::time_point position_time, double *output, |
| 122 | flatbuffers::FlatBufferBuilder *fbb) { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 123 | if (goal) { |
| 124 | // Update position/goal for our wheel. |
| 125 | wheel_.set_goal(goal->angular_velocity); |
| 126 | } |
| 127 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 128 | wheel_.set_position(position); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 129 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 130 | chrono::nanoseconds dt = ::frc971::controls::kLoopFrequency; |
Austin Schuh | 932a5ce | 2017-03-05 01:04:18 -0800 | [diff] [blame] | 131 | if (last_time_ != ::aos::monotonic_clock::min_time) { |
| 132 | dt = position_time - last_time_; |
| 133 | } |
| 134 | last_time_ = position_time; |
| 135 | |
| 136 | wheel_.Update(output == nullptr, dt); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 137 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 138 | flatbuffers::Offset<ShooterStatus> status_offset = wheel_.BuildStatus(fbb); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 139 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 140 | if (last_ready_ && !wheel_.ready()) { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 141 | min_ = wheel_.dt_velocity(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 142 | } else if (!wheel_.ready()) { |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 143 | min_ = ::std::min(min_, wheel_.dt_velocity()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 144 | } else if (!last_ready_ && wheel_.ready()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 145 | AOS_LOG(INFO, "Shot min was [%f]\n", min_); |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | if (output) { |
| 149 | *output = wheel_.voltage(); |
| 150 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 151 | last_ready_ = wheel_.ready(); |
| 152 | |
| 153 | return status_offset; |
Tyler Chatow | 2737d2a | 2017-02-08 21:20:51 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 156 | } // namespace y2017::control_loops::superstructure::shooter |