blob: 0560ec4d812754e3acec01081ea1021a45fceb2e [file] [log] [blame]
Austin Schuhcd3237a2017-02-18 14:19:26 -08001#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
14namespace y2017 {
15namespace control_loops {
16namespace superstructure {
17namespace indexer {
18
19namespace chrono = ::std::chrono;
20using ::aos::monotonic_clock;
21
22namespace {
23constexpr double kTolerance = 10.0;
Austin Schuhcd3237a2017-02-18 14:19:26 -080024constexpr chrono::milliseconds kForwardTimeout{500};
25constexpr chrono::milliseconds kReverseTimeout{500};
26constexpr chrono::milliseconds kReverseMinTimeout{100};
27} // namespace
28
29// TODO(austin): Pseudo current limit?
30
31IndexerController::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
42void IndexerController::set_goal(double angular_velocity_goal) {
43 loop_->mutable_next_R() << 0.0, angular_velocity_goal, 0.0;
44}
45
46void 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
61double IndexerController::voltage() const { return loop_->U(0, 0); }
62
Austin Schuha4dd26d2017-02-24 19:14:39 -080063double IndexerController::StuckVoltage() const {
64 const double applied_voltage = voltage() + loop_->X_hat(2, 0);
Austin Schuhcd3237a2017-02-18 14:19:26 -080065 if (applied_voltage < 0) {
Austin Schuha4dd26d2017-02-24 19:14:39 -080066 return +stuck_indexer_X_hat_current_(2, 0) + applied_voltage;
Austin Schuhcd3237a2017-02-18 14:19:26 -080067 } else {
Austin Schuha4dd26d2017-02-24 19:14:39 -080068 return -stuck_indexer_X_hat_current_(2, 0) - applied_voltage;
Austin Schuhcd3237a2017-02-18 14:19:26 -080069 }
Austin Schuhcd3237a2017-02-18 14:19:26 -080070}
Austin Schuha4dd26d2017-02-24 19:14:39 -080071bool IndexerController::IsStuck() const { return StuckVoltage() > 1.5; }
Austin Schuhcd3237a2017-02-18 14:19:26 -080072
73void IndexerController::Reset() { reset_ = true; }
74
75void IndexerController::PartialReset() { loop_->mutable_X_hat(2, 0) = 0.0; }
76
77void 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);
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800119 stuck_indexer_detector_->UpdateObserver(loop_->U(),
120 ::aos::controls::kLoopFrequency);
Austin Schuhcd3237a2017-02-18 14:19:26 -0800121}
122
123void IndexerController::SetStatus(IndexerStatus *status) {
124 status->avg_angular_velocity = average_angular_velocity_;
125
126 status->angular_velocity = X_hat_current_(1, 0);
127 status->ready = ready_;
128
129 status->voltage_error = X_hat_current_(2, 0);
130 status->stuck_voltage_error = stuck_indexer_X_hat_current_(2, 0);
131 status->position_error = position_error_;
132 status->instantaneous_velocity = dt_velocity_;
133
134 status->stuck = IsStuck();
135
Austin Schuha4dd26d2017-02-24 19:14:39 -0800136 status->stuck_voltage = StuckVoltage();
Austin Schuhcd3237a2017-02-18 14:19:26 -0800137}
138
139void Indexer::Reset() { indexer_.Reset(); }
140
141void Indexer::Iterate(const control_loops::IndexerGoal *goal,
142 const double *position, double *output,
143 control_loops::IndexerStatus *status) {
144 if (goal) {
145 // Start indexing at the suggested velocity.
146 // If a "stuck" event is detected, reverse. Stay reversed until either
147 // unstuck, or 0.5 seconds have elapsed.
148 // Then, start going forwards. Don't detect stuck for 0.5 seconds.
149
150 monotonic_clock::time_point monotonic_now = monotonic_clock::now();
151 switch (state_) {
152 case State::RUNNING:
153 // Pass the velocity goal through.
154 indexer_.set_goal(goal->angular_velocity);
155 // If we are stuck and weren't just reversing, try reversing to unstick
156 // us. We don't want to chatter back and forth too fast if reversing
157 // isn't working.
158 if (indexer_.IsStuck() &&
159 monotonic_now > kForwardTimeout + last_transition_time_) {
160 state_ = State::REVERSING;
161 last_transition_time_ = monotonic_now;
162 indexer_.Reset();
163 }
164 break;
165 case State::REVERSING:
166 // "Reverse" "slowly".
167 indexer_.set_goal(-5.0 * aos::sign(goal->angular_velocity));
168
169 // If we've timed out or are no longer stuck, try running again.
170 if ((!indexer_.IsStuck() &&
171 monotonic_now > last_transition_time_ + kReverseMinTimeout) ||
172 monotonic_now > kReverseTimeout + last_transition_time_) {
173 state_ = State::RUNNING;
Austin Schuhcd3237a2017-02-18 14:19:26 -0800174
175 // Only reset if we got stuck going this way too.
176 if (monotonic_now > kReverseTimeout + last_transition_time_) {
177 indexer_.Reset();
178 }
Neil Balchf6ba0ee2017-02-19 15:57:22 -0800179 last_transition_time_ = monotonic_now;
Austin Schuhcd3237a2017-02-18 14:19:26 -0800180 }
181 break;
182 }
183 }
184
185 indexer_.set_position(*position);
186
187 indexer_.Update(output == nullptr);
188
189 indexer_.SetStatus(status);
190 status->state = static_cast<int32_t>(state_);
191
192 if (output) {
193 *output = indexer_.voltage();
194 }
195}
196
197} // namespace indexer
198} // namespace superstructure
199} // namespace control_loops
200} // namespace y2017