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