blob: 85af86bbcaaa00b7f84ac21a3dc04568732b5b92 [file] [log] [blame]
Austin Schuhd5ccb862017-03-11 22:06:36 -08001#include "y2017/control_loops/superstructure/column/column.h"
2
3#include <array>
4#include <chrono>
5#include <memory>
6#include <utility>
7
8#include "Eigen/Dense"
9
John Park33858a32018-09-28 23:05:48 -070010#include "aos/commonmath.h"
Austin Schuhd5ccb862017-03-11 22:06:36 -080011#include "frc971/constants.h"
12#include "frc971/control_loops/profiled_subsystem.h"
13#include "frc971/control_loops/state_feedback_loop.h"
14#include "y2017/control_loops/superstructure/column/column_integral_plant.h"
15#include "y2017/control_loops/superstructure/column/stuck_column_integral_plant.h"
16
17namespace y2017 {
18namespace control_loops {
19namespace superstructure {
20namespace column {
21
22namespace chrono = ::std::chrono;
23using ::aos::monotonic_clock;
24using ::frc971::zeroing::PulseIndexZeroingEstimator;
25
26namespace {
27constexpr double kTolerance = 10.0;
Austin Schuh4af3ac12017-04-09 18:34:01 -070028constexpr double kIndexerAcceleration = 50.0;
Austin Schuhd5ccb862017-03-11 22:06:36 -080029constexpr chrono::milliseconds kForwardTimeout{500};
Austin Schuh4af3ac12017-04-09 18:34:01 -070030constexpr chrono::milliseconds kReverseTimeout{1000};
31constexpr chrono::milliseconds kReverseMinTimeout{500};
Austin Schuhd5ccb862017-03-11 22:06:36 -080032} // namespace
33
34constexpr double Column::kZeroingVoltage;
35constexpr double Column::kOperatingVoltage;
36constexpr double Column::kIntakeZeroingMinDistance;
37constexpr double Column::kIntakeTolerance;
38constexpr double Column::kStuckZeroingTrackingError;
39
40ColumnProfiledSubsystem::ColumnProfiledSubsystem(
41 ::std::unique_ptr<
42 ::frc971::control_loops::SimpleCappedStateFeedbackLoop<6, 2, 2>>
43 loop,
44 const ::y2017::constants::Values::Column &zeroing_constants,
45 const ::frc971::constants::Range &range, double default_velocity,
46 double default_acceleration)
47 : ProfiledSubsystem<6, 1, ColumnZeroingEstimator, 2, 2>(
48 ::std::move(loop), {{zeroing_constants}}),
49 stuck_indexer_detector_(new StateFeedbackLoop<6, 2, 2>(
50 column::MakeStuckIntegralColumnLoop())),
51 profile_(::aos::controls::kLoopFrequency),
52 range_(range),
53 default_velocity_(default_velocity),
54 default_acceleration_(default_acceleration) {
55 Y_.setZero();
56 offset_.setZero();
57 X_hat_current_.setZero();
58 stuck_indexer_X_hat_current_.setZero();
59 indexer_history_.fill(0);
60 AdjustProfile(0.0, 0.0);
61}
62
63void ColumnProfiledSubsystem::AddOffset(double indexer_offset_delta,
64 double turret_offset_delta) {
65 UpdateOffset(offset_(0, 0) + indexer_offset_delta,
66 offset_(1, 0) + turret_offset_delta);
67}
68
69void ColumnProfiledSubsystem::UpdateOffset(double indexer_offset,
70 double turret_offset) {
71 const double indexer_doffset = indexer_offset - offset_(0, 0);
72 const double turret_doffset = turret_offset - offset_(1, 0);
73
74 LOG(INFO, "Adjusting indexer offset from %f to %f\n", offset_(0, 0),
75 indexer_offset);
76 LOG(INFO, "Adjusting turret offset from %f to %f\n", offset_(1, 0),
77 turret_offset);
78
79 loop_->mutable_X_hat()(0, 0) += indexer_doffset;
80 loop_->mutable_X_hat()(2, 0) += turret_doffset + indexer_doffset;
81
82 stuck_indexer_detector_->mutable_X_hat()(0, 0) += indexer_doffset;
83 stuck_indexer_detector_->mutable_X_hat()(2, 0) +=
84 turret_doffset + indexer_doffset;
85 Y_(0, 0) += indexer_doffset;
86 Y_(1, 0) += turret_doffset;
87 turret_last_position_ += turret_doffset + indexer_doffset;
88 loop_->mutable_R(0, 0) += indexer_doffset;
89 loop_->mutable_R(2, 0) += turret_doffset + indexer_doffset;
90
91 profile_.MoveGoal(turret_doffset + indexer_doffset);
92 offset_(0, 0) = indexer_offset;
93 offset_(1, 0) = turret_offset;
94
95 CapGoal("R", &loop_->mutable_R());
96}
97
98void ColumnProfiledSubsystem::Correct(const ColumnPosition &new_position) {
99 estimators_[0].UpdateEstimate(new_position);
100
101 if (estimators_[0].error()) {
102 LOG(ERROR, "zeroing error\n");
103 return;
104 }
105
106 if (!initialized_) {
107 if (estimators_[0].offset_ready()) {
108 UpdateOffset(estimators_[0].indexer_offset(),
109 estimators_[0].turret_offset());
110 initialized_ = true;
111 }
112 }
113
114 if (!zeroed(0) && estimators_[0].zeroed()) {
115 UpdateOffset(estimators_[0].indexer_offset(),
116 estimators_[0].turret_offset());
117 set_zeroed(0, true);
118 }
119
120 turret_last_position_ = turret_position();
Lee Mracek598a2452019-01-07 00:50:44 -0800121 Y_ << new_position.indexer.encoder, new_position.turret.encoder;
Austin Schuhd5ccb862017-03-11 22:06:36 -0800122 Y_ += offset_;
123 loop_->Correct(Y_);
124
Lee Mracek598a2452019-01-07 00:50:44 -0800125 indexer_history_[indexer_history_position_] = new_position.indexer.encoder;
Austin Schuhd5ccb862017-03-11 22:06:36 -0800126 indexer_history_position_ = (indexer_history_position_ + 1) % kHistoryLength;
127
128 indexer_dt_velocity_ =
Lee Mracek598a2452019-01-07 00:50:44 -0800129 (new_position.indexer.encoder - indexer_last_position_) /
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700130 ::aos::time::DurationInSeconds(::aos::controls::kLoopFrequency);
Lee Mracek598a2452019-01-07 00:50:44 -0800131 indexer_last_position_ = new_position.indexer.encoder;
Austin Schuhd5ccb862017-03-11 22:06:36 -0800132
133 stuck_indexer_detector_->Correct(Y_);
134
135 // Compute the oldest point in the history.
136 const int indexer_oldest_history_position =
137 ((indexer_history_position_ == 0) ? kHistoryLength
138 : indexer_history_position_) -
139 1;
140
141 // Compute the distance moved over that time period.
142 indexer_average_angular_velocity_ =
143 (indexer_history_[indexer_oldest_history_position] -
144 indexer_history_[indexer_history_position_]) /
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700145 (::aos::time::DurationInSeconds(::aos::controls::kLoopFrequency) *
Austin Schuhd5ccb862017-03-11 22:06:36 -0800146 static_cast<double>(kHistoryLength - 1));
147
148 // Ready if average angular velocity is close to the goal.
149 indexer_error_ = indexer_average_angular_velocity_ - unprofiled_goal_(1, 0);
150
151 indexer_ready_ =
152 std::abs(indexer_error_) < kTolerance && unprofiled_goal_(1, 0) > 0.1;
153
154 // Pull state from the profiled subsystem.
155 X_hat_current_ = controller().X_hat();
156 stuck_indexer_X_hat_current_ = stuck_indexer_detector_->X_hat();
157 indexer_position_error_ = X_hat_current_(0, 0) - Y(0, 0);
158}
159
160void ColumnProfiledSubsystem::CapGoal(const char *name,
161 Eigen::Matrix<double, 6, 1> *goal) {
162 // Limit the goal to min/max allowable positions.
163 if (zeroed()) {
164 if ((*goal)(2, 0) > range_.upper) {
165 LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(2, 0),
166 range_.upper);
167 (*goal)(2, 0) = range_.upper;
168 }
169 if ((*goal)(2, 0) < range_.lower) {
170 LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(2, 0),
171 range_.lower);
172 (*goal)(2, 0) = range_.lower;
173 }
174 } else {
175 const double kMaxRange = range().upper_hard - range().lower_hard;
176
177 // Limit the goal to min/max allowable positions much less agressively.
178 // We don't know where the limits are, so we have to let the user move far
179 // enough to find them (and the index pulse which might be right next to
180 // one).
181 // Upper - lower hard may be a bit generous, but we are moving slow.
182
183 if ((*goal)(2, 0) > kMaxRange) {
184 LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(2, 0),
185 kMaxRange);
186 (*goal)(2, 0) = kMaxRange;
187 }
188 if ((*goal)(2, 0) < -kMaxRange) {
189 LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(2, 0),
190 -kMaxRange);
191 (*goal)(2, 0) = -kMaxRange;
192 }
193 }
194}
195
196void ColumnProfiledSubsystem::ForceGoal(double goal_velocity, double goal) {
197 set_unprofiled_goal(goal_velocity, goal);
198 loop_->mutable_R() = unprofiled_goal_;
199 loop_->mutable_next_R() = loop_->R();
200
201 const ::Eigen::Matrix<double, 6, 1> &R = loop_->R();
202 profile_.MoveCurrentState(R.block<2, 1>(2, 0));
203}
204
205void ColumnProfiledSubsystem::set_unprofiled_goal(double goal_velocity,
206 double unprofiled_goal) {
207 unprofiled_goal_(0, 0) = 0.0;
208 unprofiled_goal_(1, 0) = goal_velocity;
209 unprofiled_goal_(2, 0) = unprofiled_goal;
210 unprofiled_goal_(3, 0) = 0.0;
211 unprofiled_goal_(4, 0) = 0.0;
212 unprofiled_goal_(5, 0) = 0.0;
213 CapGoal("unprofiled R", &unprofiled_goal_);
214}
215
216void ColumnProfiledSubsystem::set_indexer_unprofiled_goal(
217 double goal_velocity) {
218 unprofiled_goal_(0, 0) = 0.0;
219 unprofiled_goal_(1, 0) = goal_velocity;
220 unprofiled_goal_(4, 0) = 0.0;
221 CapGoal("unprofiled R", &unprofiled_goal_);
222}
223
224void ColumnProfiledSubsystem::set_turret_unprofiled_goal(
225 double unprofiled_goal) {
226 unprofiled_goal_(2, 0) = unprofiled_goal;
227 unprofiled_goal_(3, 0) = 0.0;
228 unprofiled_goal_(5, 0) = 0.0;
229 CapGoal("unprofiled R", &unprofiled_goal_);
230}
231
232void ColumnProfiledSubsystem::Update(bool disable) {
233 // TODO(austin): If we really need to reset, reset the profiles, etc. That'll
234 // be covered by the layer above when disabled though, so we can get away with
235 // not doing it yet.
236 if (should_reset_) {
237 loop_->mutable_X_hat(0, 0) = Y_(0, 0);
238 loop_->mutable_X_hat(1, 0) = 0.0;
239 loop_->mutable_X_hat(2, 0) = Y_(0, 0) + Y_(1, 0);
240 loop_->mutable_X_hat(3, 0) = 0.0;
241 loop_->mutable_X_hat(4, 0) = 0.0;
242 loop_->mutable_X_hat(5, 0) = 0.0;
243
244 LOG(INFO, "Resetting\n");
245 stuck_indexer_detector_->mutable_X_hat() = loop_->X_hat();
246 should_reset_ = false;
247 saturated_ = false;
248 }
249
250 if (!disable) {
251 ::Eigen::Matrix<double, 2, 1> goal_state =
252 profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0));
253
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700254 constexpr double kDt =
255 ::aos::time::DurationInSeconds(::aos::controls::kLoopFrequency);
Austin Schuh471d3b12017-03-25 20:49:14 -0700256
Austin Schuhd5ccb862017-03-11 22:06:36 -0800257 loop_->mutable_next_R(0, 0) = 0.0;
Austin Schuh471d3b12017-03-25 20:49:14 -0700258 // TODO(austin): This might not handle saturation right, but I'm not sure I
259 // really care.
260 loop_->mutable_next_R(1, 0) = ::aos::Clip(
261 unprofiled_goal_(1, 0), loop_->R(1, 0) - kIndexerAcceleration * kDt,
262 loop_->R(1, 0) + kIndexerAcceleration * kDt);
Austin Schuhd5ccb862017-03-11 22:06:36 -0800263 loop_->mutable_next_R(2, 0) = goal_state(0, 0);
264 loop_->mutable_next_R(3, 0) = goal_state(1, 0);
265 loop_->mutable_next_R(4, 0) = 0.0;
266 loop_->mutable_next_R(5, 0) = 0.0;
267 CapGoal("next R", &loop_->mutable_next_R());
268 }
269
270 // If the indexer goal velocity is low, switch to the indexer controller which
271 // won't fight to keep it moving at 0.
272 if (::std::abs(unprofiled_goal_(1, 0)) < 0.1) {
273 loop_->set_index(1);
274 } else {
275 loop_->set_index(0);
276 }
277 loop_->Update(disable);
278
279 if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) {
280 const ::Eigen::Matrix<double, 6, 1> &R = loop_->R();
281 profile_.MoveCurrentState(R.block<2, 1>(2, 0));
282 saturated_ = true;
283 } else {
284 saturated_ = false;
285 }
286
287 // Run the KF predict step.
288 stuck_indexer_detector_->UpdateObserver(loop_->U(),
289 ::aos::controls::kLoopFrequency);
290}
291
292bool ColumnProfiledSubsystem::CheckHardLimits() {
293 // Returns whether hard limits have been exceeded.
294
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700295 if (turret_position() > range_.upper_hard ||
296 turret_position() < range_.lower_hard) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800297 LOG(ERROR,
298 "ColumnProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n",
299 turret_position(), range_.lower_hard, range_.upper_hard);
300 return true;
301 }
302
303 return false;
304}
305
306void ColumnProfiledSubsystem::AdjustProfile(
307 const ::frc971::ProfileParameters &profile_parameters) {
308 AdjustProfile(profile_parameters.max_velocity,
309 profile_parameters.max_acceleration);
310}
311
312void ColumnProfiledSubsystem::AdjustProfile(double max_angular_velocity,
313 double max_angular_acceleration) {
314 profile_.set_maximum_velocity(
315 ::frc971::control_loops::internal::UseUnlessZero(max_angular_velocity,
316 default_velocity_));
317 profile_.set_maximum_acceleration(
318 ::frc971::control_loops::internal::UseUnlessZero(max_angular_acceleration,
319 default_acceleration_));
320}
321
322double ColumnProfiledSubsystem::IndexerStuckVoltage() const {
323 // Compute the voltage from the control loop, excluding the voltage error
324 // term.
325 const double uncapped_applied_voltage =
326 uncapped_indexer_voltage() + X_hat(4, 0);
327 if (uncapped_applied_voltage < 0) {
328 return +stuck_indexer_X_hat_current_(4, 0);
329 } else {
330 return -stuck_indexer_X_hat_current_(4, 0);
331 }
332}
333bool ColumnProfiledSubsystem::IsIndexerStuck() const {
Austin Schuh546a0382017-04-16 19:10:18 -0700334 return IndexerStuckVoltage() > 4.0;
Austin Schuhd5ccb862017-03-11 22:06:36 -0800335}
336
337void ColumnProfiledSubsystem::PartialIndexerReset() {
338 mutable_X_hat(4, 0) = 0.0;
339 stuck_indexer_detector_->mutable_X_hat(4, 0) = 0.0;
Austin Schuh471d3b12017-03-25 20:49:14 -0700340 // Screw it, we are stuck. Reset the current goal to the current velocity so
341 // we start slewing faster to reverse if we have stopped.
342 loop_->mutable_R(1, 0) = X_hat(1, 0);
343 loop_->mutable_next_R(1, 0) = X_hat(1, 0);
Austin Schuhd5ccb862017-03-11 22:06:36 -0800344}
345
346void ColumnProfiledSubsystem::PartialTurretReset() {
347 mutable_X_hat(5, 0) = 0.0;
348 stuck_indexer_detector_->mutable_X_hat(5, 0) = 0.0;
349}
350
351void ColumnProfiledSubsystem::PopulateIndexerStatus(IndexerStatus *status) {
352 status->avg_angular_velocity = indexer_average_angular_velocity_;
353
354 status->angular_velocity = X_hat_current_(1, 0);
355 status->ready = indexer_ready_;
356
357 status->voltage_error = X_hat_current_(4, 0);
358 status->stuck_voltage_error = stuck_indexer_X_hat_current_(4, 0);
359 status->position_error = indexer_position_error_;
360 status->instantaneous_velocity = indexer_dt_velocity_;
361
362 status->stuck = IsIndexerStuck();
363
364 status->stuck_voltage = IndexerStuckVoltage();
365}
366
367Column::Column()
368 : profiled_subsystem_(
369 ::std::unique_ptr<
370 ::frc971::control_loops::SimpleCappedStateFeedbackLoop<6, 2, 2>>(
371 new ::frc971::control_loops::SimpleCappedStateFeedbackLoop<
372 6, 2, 2>(MakeIntegralColumnLoop())),
373 constants::GetValues().column, constants::Values::kTurretRange, 7.0,
Austin Schuh25db1262017-04-05 19:39:55 -0700374 50.0),
375 vision_error_(constants::GetValues().vision_error) {}
Austin Schuhd5ccb862017-03-11 22:06:36 -0800376
377void Column::Reset() {
378 state_ = State::UNINITIALIZED;
379 indexer_state_ = IndexerState::RUNNING;
380 profiled_subsystem_.Reset();
381 // intake will automatically clear the minimum position on reset, so we don't
382 // need to do it here.
383 freeze_ = false;
384}
385
386void Column::Iterate(const control_loops::IndexerGoal *unsafe_indexer_goal,
387 const control_loops::TurretGoal *unsafe_turret_goal,
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +0000388 const ColumnPosition *position,
389 const vision::VisionStatus *vision_status,
390 double *indexer_output, double *turret_output,
391 IndexerStatus *indexer_status,
Austin Schuhd5ccb862017-03-11 22:06:36 -0800392 TurretProfiledSubsystemStatus *turret_status,
393 intake::Intake *intake) {
394 bool disable = turret_output == nullptr || indexer_output == nullptr;
395 profiled_subsystem_.Correct(*position);
396
Austin Schuhac76bb32017-03-22 22:34:26 -0700397 vision_time_adjuster_.Tick(::aos::monotonic_clock::now(),
Philipp Schrader8e3ac0f2017-04-09 23:36:17 +0000398 profiled_subsystem_.X_hat(2, 0), vision_status);
Austin Schuhac76bb32017-03-22 22:34:26 -0700399
Austin Schuhd5ccb862017-03-11 22:06:36 -0800400 switch (state_) {
401 case State::UNINITIALIZED:
402 // Wait in the uninitialized state until the turret is initialized.
403 // Set the goals to where we are now so when we start back up, we don't
404 // jump.
405 profiled_subsystem_.ForceGoal(0.0, profiled_subsystem_.turret_position());
406 state_ = State::ZEROING_UNINITIALIZED;
407
408 // Fall through so we can start the zeroing process immediately.
409
410 case State::ZEROING_UNINITIALIZED:
411 // Set up the profile to be the zeroing profile.
412 profiled_subsystem_.AdjustProfile(0.50, 3);
413
414 // Force the intake out.
415 intake->set_min_position(kIntakeZeroingMinDistance);
416
417 if (disable) {
418 // If we are disabled, we want to reset the turret to stay where it
419 // currently is.
420 profiled_subsystem_.ForceGoal(0.0,
421 profiled_subsystem_.turret_position());
422 } else if (profiled_subsystem_.initialized()) {
423 // If we are initialized, there's no value in continuing to move so stop
424 // and wait on the intake.
425 profiled_subsystem_.set_indexer_unprofiled_goal(0.0);
426 } else {
427 // Spin slowly backwards.
428 profiled_subsystem_.set_indexer_unprofiled_goal(2.0);
429 }
430
431 // See if we are zeroed or initialized and far enough out and execute the
432 // proper state transition.
433 if (profiled_subsystem_.zeroed()) {
434 intake->clear_min_position();
435 state_ = State::RUNNING;
436 } else if (profiled_subsystem_.initialized() &&
437 intake->position() >
438 kIntakeZeroingMinDistance - kIntakeTolerance) {
439 if (profiled_subsystem_.turret_position() > 0) {
440 // We need to move in the negative direction.
441 state_ = State::ZEROING_NEGATIVE;
442 } else {
443 // We need to move in the positive direction.
444 state_ = State::ZEROING_POSITIVE;
445 }
446 }
447 break;
448
449 case State::ZEROING_POSITIVE:
450 // We are now going to be moving in the positive direction towards 0. If
451 // we get close enough, we'll zero.
452 profiled_subsystem_.set_unprofiled_goal(0.0, 0.0);
453 intake->set_min_position(kIntakeZeroingMinDistance);
454
455 if (profiled_subsystem_.zeroed()) {
456 intake->clear_min_position();
457 state_ = State::RUNNING;
458 } else if (disable) {
459 // We are disabled, so pick back up from the current position.
460 profiled_subsystem_.ForceGoal(0.0,
461 profiled_subsystem_.turret_position());
462 } else if (profiled_subsystem_.turret_position() <
463 profiled_subsystem_.goal(2, 0) -
464 kStuckZeroingTrackingError ||
465 profiled_subsystem_.saturated()) {
466 LOG(INFO,
467 "Turret stuck going positive, switching directions. At %f, goal "
468 "%f\n",
469 profiled_subsystem_.turret_position(),
470 profiled_subsystem_.goal(2, 0));
471 // The turret got too far behind. Declare it stuck and reverse.
472 profiled_subsystem_.AddOffset(0.0, 2.0 * M_PI);
473 profiled_subsystem_.set_unprofiled_goal(0.0, 0.0);
474 profiled_subsystem_.ForceGoal(0.0,
475 profiled_subsystem_.turret_position());
476 profiled_subsystem_.PartialTurretReset();
477 profiled_subsystem_.PartialIndexerReset();
478 state_ = State::ZEROING_NEGATIVE;
479 }
480 break;
481
482 case State::ZEROING_NEGATIVE:
483 // We are now going to be moving in the negative direction towards 0. If
484 // we get close enough, we'll zero.
485 profiled_subsystem_.set_unprofiled_goal(0.0, 0.0);
486 intake->set_min_position(kIntakeZeroingMinDistance);
487
488 if (profiled_subsystem_.zeroed()) {
489 intake->clear_min_position();
490 state_ = State::RUNNING;
491 } else if (disable) {
492 // We are disabled, so pick back up from the current position.
493 profiled_subsystem_.ForceGoal(0.0,
494 profiled_subsystem_.turret_position());
495 } else if (profiled_subsystem_.turret_position() >
496 profiled_subsystem_.goal(2, 0) +
497 kStuckZeroingTrackingError ||
498 profiled_subsystem_.saturated()) {
499 // The turret got too far behind. Declare it stuck and reverse.
500 LOG(INFO,
501 "Turret stuck going negative, switching directions. At %f, goal "
502 "%f\n",
503 profiled_subsystem_.turret_position(),
504 profiled_subsystem_.goal(2, 0));
505 profiled_subsystem_.AddOffset(0.0, -2.0 * M_PI);
506 profiled_subsystem_.set_unprofiled_goal(0.0, 0.0);
507 profiled_subsystem_.ForceGoal(0.0,
508 profiled_subsystem_.turret_position());
509 profiled_subsystem_.PartialTurretReset();
510 profiled_subsystem_.PartialIndexerReset();
511 state_ = State::ZEROING_POSITIVE;
512 }
513 break;
514
515 case State::RUNNING: {
516 double starting_goal_angle = profiled_subsystem_.goal(2, 0);
517 if (disable) {
518 // Reset the profile to the current position so it starts from here when
519 // we get re-enabled.
520 profiled_subsystem_.ForceGoal(0.0,
521 profiled_subsystem_.turret_position());
522 }
523
524 if (unsafe_turret_goal && unsafe_indexer_goal) {
525 profiled_subsystem_.AdjustProfile(unsafe_turret_goal->profile_params);
526 profiled_subsystem_.set_unprofiled_goal(
527 unsafe_indexer_goal->angular_velocity, unsafe_turret_goal->angle);
528
Austin Schuhac76bb32017-03-22 22:34:26 -0700529 if (unsafe_turret_goal->track) {
530 if (vision_time_adjuster_.valid()) {
531 LOG(INFO, "Vision aligning to %f\n", vision_time_adjuster_.goal());
532 profiled_subsystem_.set_turret_unprofiled_goal(
Austin Schuh25db1262017-04-05 19:39:55 -0700533 vision_time_adjuster_.goal() + vision_error_);
Austin Schuhac76bb32017-03-22 22:34:26 -0700534 }
Austin Schuhd85c66e2017-04-16 10:50:33 -0700535 } else {
536 vision_time_adjuster_.ResetTime();
Austin Schuhac76bb32017-03-22 22:34:26 -0700537 }
538
Austin Schuhd5ccb862017-03-11 22:06:36 -0800539 if (freeze_) {
540 profiled_subsystem_.ForceGoal(unsafe_indexer_goal->angular_velocity,
541 starting_goal_angle);
542 }
543 }
544
545 // ESTOP if we hit the hard limits.
546 if (profiled_subsystem_.CheckHardLimits() ||
547 profiled_subsystem_.error()) {
548 state_ = State::ESTOP;
549 }
550 } break;
551
552 case State::ESTOP:
553 LOG(ERROR, "Estop\n");
554 disable = true;
555 break;
556 }
557
558 // Start indexing at the suggested velocity.
559 // If a "stuck" event is detected, reverse. Stay reversed until either
560 // unstuck, or 0.5 seconds have elapsed.
561 // Then, start going forwards. Don't detect stuck for 0.5 seconds.
562
563 monotonic_clock::time_point monotonic_now = monotonic_clock::now();
564 switch (indexer_state_) {
565 case IndexerState::RUNNING:
566 // The velocity goal is already set above in this case, so leave it
567 // alone.
568
569 // If we are stuck and weren't just reversing, try reversing to unstick
570 // us. We don't want to chatter back and forth too fast if reversing
571 // isn't working.
572 if (profiled_subsystem_.IsIndexerStuck() &&
573 monotonic_now > kForwardTimeout + last_transition_time_) {
574 indexer_state_ = IndexerState::REVERSING;
575 last_transition_time_ = monotonic_now;
576 profiled_subsystem_.PartialIndexerReset();
577 LOG(INFO, "Partial indexer reset while going forwards\n");
578 LOG(INFO, "Indexer RUNNING -> REVERSING\n");
579 }
580 break;
581 case IndexerState::REVERSING:
582 // "Reverse" "slowly".
583 profiled_subsystem_.set_indexer_unprofiled_goal(
584 -5.0 * ::aos::sign(profiled_subsystem_.unprofiled_goal(1, 0)));
585
586 // If we've timed out or are no longer stuck, try running again.
587 if ((!profiled_subsystem_.IsIndexerStuck() &&
588 monotonic_now > last_transition_time_ + kReverseMinTimeout) ||
589 monotonic_now > kReverseTimeout + last_transition_time_) {
590 indexer_state_ = IndexerState::RUNNING;
591 LOG(INFO, "Indexer REVERSING -> RUNNING, stuck %d\n",
592 profiled_subsystem_.IsIndexerStuck());
593
594 // Only reset if we got stuck going this way too.
595 if (monotonic_now > kReverseTimeout + last_transition_time_) {
596 LOG(INFO, "Partial indexer reset while reversing\n");
597 profiled_subsystem_.PartialIndexerReset();
598 }
599 last_transition_time_ = monotonic_now;
600 }
601 break;
602 }
603
604 // Set the voltage limits.
605 const double max_voltage =
606 (state_ == State::RUNNING) ? kOperatingVoltage : kZeroingVoltage;
607
608 profiled_subsystem_.set_max_voltage({{max_voltage, max_voltage}});
609
610 // Calculate the loops for a cycle.
611 profiled_subsystem_.Update(disable);
612
613 // Write out all the voltages.
614 if (indexer_output) {
615 *indexer_output = profiled_subsystem_.indexer_voltage();
616 }
617 if (turret_output) {
618 *turret_output = profiled_subsystem_.turret_voltage();
619 }
620
621 // Save debug/internal state.
622 // TODO(austin): Save more.
623 turret_status->zeroed = profiled_subsystem_.zeroed();
624 profiled_subsystem_.PopulateTurretStatus(turret_status);
625 turret_status->estopped = (state_ == State::ESTOP);
626 turret_status->state = static_cast<int32_t>(state_);
Austin Schuheb5c22e2017-04-09 18:30:28 -0700627 turret_status->turret_encoder_angle = profiled_subsystem_.turret_position();
Austin Schuhac76bb32017-03-22 22:34:26 -0700628 if (vision_time_adjuster_.valid()) {
629 turret_status->vision_angle = vision_time_adjuster_.goal();
630 turret_status->raw_vision_angle =
631 vision_time_adjuster_.most_recent_vision_reading();
632 turret_status->vision_tracking = true;
633 } else {
634 turret_status->vision_angle = ::std::numeric_limits<double>::quiet_NaN();
635 turret_status->vision_tracking = false;
636 }
Austin Schuhd5ccb862017-03-11 22:06:36 -0800637
638 profiled_subsystem_.PopulateIndexerStatus(indexer_status);
639 indexer_status->state = static_cast<int32_t>(indexer_state_);
640}
641
642} // namespace column
643} // namespace superstructure
644} // namespace control_loops
645} // namespace y2017