Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 1 | #include "y2017/control_loops/superstructure/indexer/indexer.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | |
| 5 | #include "aos/common/commonmath.h" |
| 6 | #include "aos/common/controls/control_loops.q.h" |
| 7 | #include "aos/common/logging/logging.h" |
| 8 | #include "aos/common/logging/queue_logging.h" |
| 9 | #include "aos/common/time.h" |
| 10 | #include "y2017/control_loops/superstructure/indexer/indexer_integral_plant.h" |
| 11 | #include "y2017/control_loops/superstructure/indexer/stuck_indexer_integral_plant.h" |
| 12 | #include "y2017/control_loops/superstructure/superstructure.q.h" |
| 13 | |
| 14 | namespace y2017 { |
| 15 | namespace control_loops { |
| 16 | namespace superstructure { |
| 17 | namespace indexer { |
| 18 | |
| 19 | namespace chrono = ::std::chrono; |
| 20 | using ::aos::monotonic_clock; |
| 21 | |
| 22 | namespace { |
| 23 | constexpr double kTolerance = 10.0; |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 24 | constexpr chrono::milliseconds kForwardTimeout{500}; |
| 25 | constexpr chrono::milliseconds kReverseTimeout{500}; |
| 26 | constexpr chrono::milliseconds kReverseMinTimeout{100}; |
| 27 | } // namespace |
| 28 | |
| 29 | // TODO(austin): Pseudo current limit? |
| 30 | |
| 31 | IndexerController::IndexerController() |
| 32 | : loop_(new StateFeedbackLoop<3, 1, 1>( |
| 33 | superstructure::indexer::MakeIntegralIndexerLoop())), |
| 34 | stuck_indexer_detector_(new StateFeedbackLoop<3, 1, 1>( |
| 35 | superstructure::indexer::MakeStuckIntegralIndexerLoop())) { |
| 36 | history_.fill(0); |
| 37 | Y_.setZero(); |
| 38 | X_hat_current_.setZero(); |
| 39 | stuck_indexer_X_hat_current_.setZero(); |
| 40 | } |
| 41 | |
| 42 | void IndexerController::set_goal(double angular_velocity_goal) { |
| 43 | loop_->mutable_next_R() << 0.0, angular_velocity_goal, 0.0; |
| 44 | } |
| 45 | |
| 46 | void IndexerController::set_position(double current_position) { |
| 47 | // Update position in the model. |
| 48 | Y_ << current_position; |
| 49 | |
| 50 | // Add the position to the history. |
| 51 | history_[history_position_] = current_position; |
| 52 | history_position_ = (history_position_ + 1) % kHistoryLength; |
| 53 | |
| 54 | dt_velocity_ = (current_position - last_position_) / |
| 55 | chrono::duration_cast<chrono::duration<double>>( |
| 56 | ::aos::controls::kLoopFrequency) |
| 57 | .count(); |
| 58 | last_position_ = current_position; |
| 59 | } |
| 60 | |
| 61 | double IndexerController::voltage() const { return loop_->U(0, 0); } |
| 62 | |
Austin Schuh | a4dd26d | 2017-02-24 19:14:39 -0800 | [diff] [blame] | 63 | double IndexerController::StuckVoltage() const { |
| 64 | const double applied_voltage = voltage() + loop_->X_hat(2, 0); |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 65 | if (applied_voltage < 0) { |
Austin Schuh | a4dd26d | 2017-02-24 19:14:39 -0800 | [diff] [blame] | 66 | return +stuck_indexer_X_hat_current_(2, 0) + applied_voltage; |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 67 | } else { |
Austin Schuh | a4dd26d | 2017-02-24 19:14:39 -0800 | [diff] [blame] | 68 | return -stuck_indexer_X_hat_current_(2, 0) - applied_voltage; |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 69 | } |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 70 | } |
Austin Schuh | a4dd26d | 2017-02-24 19:14:39 -0800 | [diff] [blame] | 71 | bool IndexerController::IsStuck() const { return StuckVoltage() > 1.5; } |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 72 | |
| 73 | void IndexerController::Reset() { reset_ = true; } |
| 74 | |
| 75 | void IndexerController::PartialReset() { loop_->mutable_X_hat(2, 0) = 0.0; } |
| 76 | |
| 77 | void IndexerController::Update(bool disabled) { |
| 78 | loop_->mutable_R() = loop_->next_R(); |
| 79 | if (::std::abs(loop_->R(1, 0)) < 0.1) { |
| 80 | // Kill power at low angular velocities. |
| 81 | disabled = true; |
| 82 | } |
| 83 | |
| 84 | if (reset_) { |
| 85 | loop_->mutable_X_hat(0, 0) = Y_(0, 0); |
| 86 | loop_->mutable_X_hat(1, 0) = 0.0; |
| 87 | loop_->mutable_X_hat(2, 0) = 0.0; |
| 88 | stuck_indexer_detector_->mutable_X_hat(0, 0) = Y_(0, 0); |
| 89 | stuck_indexer_detector_->mutable_X_hat(1, 0) = 0.0; |
| 90 | stuck_indexer_detector_->mutable_X_hat(2, 0) = 0.0; |
| 91 | reset_ = false; |
| 92 | } |
| 93 | |
| 94 | loop_->Correct(Y_); |
| 95 | stuck_indexer_detector_->Correct(Y_); |
| 96 | |
| 97 | // Compute the oldest point in the history. |
| 98 | const int oldest_history_position = |
| 99 | ((history_position_ == 0) ? kHistoryLength : history_position_) - 1; |
| 100 | |
| 101 | // Compute the distance moved over that time period. |
| 102 | average_angular_velocity_ = |
| 103 | (history_[oldest_history_position] - history_[history_position_]) / |
| 104 | (chrono::duration_cast<chrono::duration<double>>( |
| 105 | ::aos::controls::kLoopFrequency) |
| 106 | .count() * |
| 107 | static_cast<double>(kHistoryLength - 1)); |
| 108 | |
| 109 | // Ready if average angular velocity is close to the goal. |
| 110 | error_ = average_angular_velocity_ - loop_->next_R(1, 0); |
| 111 | |
| 112 | ready_ = std::abs(error_) < kTolerance && loop_->next_R(1, 0) > 1.0; |
| 113 | |
| 114 | X_hat_current_ = loop_->X_hat(); |
| 115 | stuck_indexer_X_hat_current_ = stuck_indexer_detector_->X_hat(); |
| 116 | position_error_ = X_hat_current_(0, 0) - Y_(0, 0); |
| 117 | |
| 118 | loop_->Update(disabled); |
| 119 | stuck_indexer_detector_->UpdateObserver(loop_->U()); |
| 120 | } |
| 121 | |
| 122 | void IndexerController::SetStatus(IndexerStatus *status) { |
| 123 | status->avg_angular_velocity = average_angular_velocity_; |
| 124 | |
| 125 | status->angular_velocity = X_hat_current_(1, 0); |
| 126 | status->ready = ready_; |
| 127 | |
| 128 | status->voltage_error = X_hat_current_(2, 0); |
| 129 | status->stuck_voltage_error = stuck_indexer_X_hat_current_(2, 0); |
| 130 | status->position_error = position_error_; |
| 131 | status->instantaneous_velocity = dt_velocity_; |
| 132 | |
| 133 | status->stuck = IsStuck(); |
| 134 | |
Austin Schuh | a4dd26d | 2017-02-24 19:14:39 -0800 | [diff] [blame] | 135 | status->stuck_voltage = StuckVoltage(); |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void Indexer::Reset() { indexer_.Reset(); } |
| 139 | |
| 140 | void Indexer::Iterate(const control_loops::IndexerGoal *goal, |
| 141 | const double *position, double *output, |
| 142 | control_loops::IndexerStatus *status) { |
| 143 | if (goal) { |
| 144 | // Start indexing at the suggested velocity. |
| 145 | // If a "stuck" event is detected, reverse. Stay reversed until either |
| 146 | // unstuck, or 0.5 seconds have elapsed. |
| 147 | // Then, start going forwards. Don't detect stuck for 0.5 seconds. |
| 148 | |
| 149 | monotonic_clock::time_point monotonic_now = monotonic_clock::now(); |
| 150 | switch (state_) { |
| 151 | case State::RUNNING: |
| 152 | // Pass the velocity goal through. |
| 153 | indexer_.set_goal(goal->angular_velocity); |
| 154 | // If we are stuck and weren't just reversing, try reversing to unstick |
| 155 | // us. We don't want to chatter back and forth too fast if reversing |
| 156 | // isn't working. |
| 157 | if (indexer_.IsStuck() && |
| 158 | monotonic_now > kForwardTimeout + last_transition_time_) { |
| 159 | state_ = State::REVERSING; |
| 160 | last_transition_time_ = monotonic_now; |
| 161 | indexer_.Reset(); |
| 162 | } |
| 163 | break; |
| 164 | case State::REVERSING: |
| 165 | // "Reverse" "slowly". |
| 166 | indexer_.set_goal(-5.0 * aos::sign(goal->angular_velocity)); |
| 167 | |
| 168 | // If we've timed out or are no longer stuck, try running again. |
| 169 | if ((!indexer_.IsStuck() && |
| 170 | monotonic_now > last_transition_time_ + kReverseMinTimeout) || |
| 171 | monotonic_now > kReverseTimeout + last_transition_time_) { |
| 172 | state_ = State::RUNNING; |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 173 | |
| 174 | // Only reset if we got stuck going this way too. |
| 175 | if (monotonic_now > kReverseTimeout + last_transition_time_) { |
| 176 | indexer_.Reset(); |
| 177 | } |
Neil Balch | f6ba0ee | 2017-02-19 15:57:22 -0800 | [diff] [blame] | 178 | last_transition_time_ = monotonic_now; |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 179 | } |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | indexer_.set_position(*position); |
| 185 | |
| 186 | indexer_.Update(output == nullptr); |
| 187 | |
| 188 | indexer_.SetStatus(status); |
| 189 | status->state = static_cast<int32_t>(state_); |
| 190 | |
| 191 | if (output) { |
| 192 | *output = indexer_.voltage(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | } // namespace indexer |
| 197 | } // namespace superstructure |
| 198 | } // namespace control_loops |
| 199 | } // namespace y2017 |