blob: 6186f7f6deec473396604ee324bc3a9559674501 [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);
119 stuck_indexer_detector_->UpdateObserver(loop_->U());
120}
121
122void 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 Schuha4dd26d2017-02-24 19:14:39 -0800135 status->stuck_voltage = StuckVoltage();
Austin Schuhcd3237a2017-02-18 14:19:26 -0800136}
137
138void Indexer::Reset() { indexer_.Reset(); }
139
140void 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 Schuhcd3237a2017-02-18 14:19:26 -0800173
174 // Only reset if we got stuck going this way too.
175 if (monotonic_now > kReverseTimeout + last_transition_time_) {
176 indexer_.Reset();
177 }
Neil Balchf6ba0ee2017-02-19 15:57:22 -0800178 last_transition_time_ = monotonic_now;
Austin Schuhcd3237a2017-02-18 14:19:26 -0800179 }
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