Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 1 | #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 Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 10 | #include "aos/commonmath.h" |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 11 | #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 | |
| 17 | namespace y2017 { |
| 18 | namespace control_loops { |
| 19 | namespace superstructure { |
| 20 | namespace column { |
| 21 | |
| 22 | namespace chrono = ::std::chrono; |
| 23 | using ::aos::monotonic_clock; |
| 24 | using ::frc971::zeroing::PulseIndexZeroingEstimator; |
| 25 | |
| 26 | namespace { |
| 27 | constexpr double kTolerance = 10.0; |
Austin Schuh | 4af3ac1 | 2017-04-09 18:34:01 -0700 | [diff] [blame] | 28 | constexpr double kIndexerAcceleration = 50.0; |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 29 | constexpr chrono::milliseconds kForwardTimeout{500}; |
Austin Schuh | 4af3ac1 | 2017-04-09 18:34:01 -0700 | [diff] [blame] | 30 | constexpr chrono::milliseconds kReverseTimeout{1000}; |
| 31 | constexpr chrono::milliseconds kReverseMinTimeout{500}; |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 32 | } // namespace |
| 33 | |
| 34 | constexpr double Column::kZeroingVoltage; |
| 35 | constexpr double Column::kOperatingVoltage; |
| 36 | constexpr double Column::kIntakeZeroingMinDistance; |
| 37 | constexpr double Column::kIntakeTolerance; |
| 38 | constexpr double Column::kStuckZeroingTrackingError; |
| 39 | |
| 40 | ColumnProfiledSubsystem::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 | |
| 63 | void 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 | |
| 69 | void 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 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 74 | AOS_LOG(INFO, "Adjusting indexer offset from %f to %f\n", offset_(0, 0), |
| 75 | indexer_offset); |
| 76 | AOS_LOG(INFO, "Adjusting turret offset from %f to %f\n", offset_(1, 0), |
| 77 | turret_offset); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 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 | |
| 98 | void ColumnProfiledSubsystem::Correct(const ColumnPosition &new_position) { |
| 99 | estimators_[0].UpdateEstimate(new_position); |
| 100 | |
| 101 | if (estimators_[0].error()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 102 | AOS_LOG(ERROR, "zeroing error\n"); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 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(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 121 | Y_ << new_position.indexer()->encoder(), new_position.turret()->encoder(); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 122 | Y_ += offset_; |
| 123 | loop_->Correct(Y_); |
| 124 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 125 | indexer_history_[indexer_history_position_] = |
| 126 | new_position.indexer()->encoder(); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 127 | indexer_history_position_ = (indexer_history_position_ + 1) % kHistoryLength; |
| 128 | |
| 129 | indexer_dt_velocity_ = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 130 | (new_position.indexer()->encoder() - indexer_last_position_) / |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 131 | ::aos::time::DurationInSeconds(::aos::controls::kLoopFrequency); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 132 | indexer_last_position_ = new_position.indexer()->encoder(); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 133 | |
| 134 | stuck_indexer_detector_->Correct(Y_); |
| 135 | |
| 136 | // Compute the oldest point in the history. |
| 137 | const int indexer_oldest_history_position = |
| 138 | ((indexer_history_position_ == 0) ? kHistoryLength |
| 139 | : indexer_history_position_) - |
| 140 | 1; |
| 141 | |
| 142 | // Compute the distance moved over that time period. |
| 143 | indexer_average_angular_velocity_ = |
| 144 | (indexer_history_[indexer_oldest_history_position] - |
| 145 | indexer_history_[indexer_history_position_]) / |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 146 | (::aos::time::DurationInSeconds(::aos::controls::kLoopFrequency) * |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 147 | static_cast<double>(kHistoryLength - 1)); |
| 148 | |
| 149 | // Ready if average angular velocity is close to the goal. |
| 150 | indexer_error_ = indexer_average_angular_velocity_ - unprofiled_goal_(1, 0); |
| 151 | |
| 152 | indexer_ready_ = |
| 153 | std::abs(indexer_error_) < kTolerance && unprofiled_goal_(1, 0) > 0.1; |
| 154 | |
| 155 | // Pull state from the profiled subsystem. |
| 156 | X_hat_current_ = controller().X_hat(); |
| 157 | stuck_indexer_X_hat_current_ = stuck_indexer_detector_->X_hat(); |
| 158 | indexer_position_error_ = X_hat_current_(0, 0) - Y(0, 0); |
| 159 | } |
| 160 | |
| 161 | void ColumnProfiledSubsystem::CapGoal(const char *name, |
| 162 | Eigen::Matrix<double, 6, 1> *goal) { |
| 163 | // Limit the goal to min/max allowable positions. |
| 164 | if (zeroed()) { |
| 165 | if ((*goal)(2, 0) > range_.upper) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 166 | AOS_LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(2, 0), |
| 167 | range_.upper); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 168 | (*goal)(2, 0) = range_.upper; |
| 169 | } |
| 170 | if ((*goal)(2, 0) < range_.lower) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 171 | AOS_LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(2, 0), |
| 172 | range_.lower); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 173 | (*goal)(2, 0) = range_.lower; |
| 174 | } |
| 175 | } else { |
| 176 | const double kMaxRange = range().upper_hard - range().lower_hard; |
| 177 | |
| 178 | // Limit the goal to min/max allowable positions much less agressively. |
| 179 | // We don't know where the limits are, so we have to let the user move far |
| 180 | // enough to find them (and the index pulse which might be right next to |
| 181 | // one). |
| 182 | // Upper - lower hard may be a bit generous, but we are moving slow. |
| 183 | |
| 184 | if ((*goal)(2, 0) > kMaxRange) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 185 | AOS_LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(2, 0), |
| 186 | kMaxRange); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 187 | (*goal)(2, 0) = kMaxRange; |
| 188 | } |
| 189 | if ((*goal)(2, 0) < -kMaxRange) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 190 | AOS_LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(2, 0), |
| 191 | -kMaxRange); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 192 | (*goal)(2, 0) = -kMaxRange; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void ColumnProfiledSubsystem::ForceGoal(double goal_velocity, double goal) { |
| 198 | set_unprofiled_goal(goal_velocity, goal); |
| 199 | loop_->mutable_R() = unprofiled_goal_; |
| 200 | loop_->mutable_next_R() = loop_->R(); |
| 201 | |
| 202 | const ::Eigen::Matrix<double, 6, 1> &R = loop_->R(); |
| 203 | profile_.MoveCurrentState(R.block<2, 1>(2, 0)); |
| 204 | } |
| 205 | |
| 206 | void ColumnProfiledSubsystem::set_unprofiled_goal(double goal_velocity, |
| 207 | double unprofiled_goal) { |
| 208 | unprofiled_goal_(0, 0) = 0.0; |
| 209 | unprofiled_goal_(1, 0) = goal_velocity; |
| 210 | unprofiled_goal_(2, 0) = unprofiled_goal; |
| 211 | unprofiled_goal_(3, 0) = 0.0; |
| 212 | unprofiled_goal_(4, 0) = 0.0; |
| 213 | unprofiled_goal_(5, 0) = 0.0; |
| 214 | CapGoal("unprofiled R", &unprofiled_goal_); |
| 215 | } |
| 216 | |
| 217 | void ColumnProfiledSubsystem::set_indexer_unprofiled_goal( |
| 218 | double goal_velocity) { |
| 219 | unprofiled_goal_(0, 0) = 0.0; |
| 220 | unprofiled_goal_(1, 0) = goal_velocity; |
| 221 | unprofiled_goal_(4, 0) = 0.0; |
| 222 | CapGoal("unprofiled R", &unprofiled_goal_); |
| 223 | } |
| 224 | |
| 225 | void ColumnProfiledSubsystem::set_turret_unprofiled_goal( |
| 226 | double unprofiled_goal) { |
| 227 | unprofiled_goal_(2, 0) = unprofiled_goal; |
| 228 | unprofiled_goal_(3, 0) = 0.0; |
| 229 | unprofiled_goal_(5, 0) = 0.0; |
| 230 | CapGoal("unprofiled R", &unprofiled_goal_); |
| 231 | } |
| 232 | |
| 233 | void ColumnProfiledSubsystem::Update(bool disable) { |
| 234 | // TODO(austin): If we really need to reset, reset the profiles, etc. That'll |
| 235 | // be covered by the layer above when disabled though, so we can get away with |
| 236 | // not doing it yet. |
| 237 | if (should_reset_) { |
| 238 | loop_->mutable_X_hat(0, 0) = Y_(0, 0); |
| 239 | loop_->mutable_X_hat(1, 0) = 0.0; |
| 240 | loop_->mutable_X_hat(2, 0) = Y_(0, 0) + Y_(1, 0); |
| 241 | loop_->mutable_X_hat(3, 0) = 0.0; |
| 242 | loop_->mutable_X_hat(4, 0) = 0.0; |
| 243 | loop_->mutable_X_hat(5, 0) = 0.0; |
| 244 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 245 | AOS_LOG(INFO, "Resetting\n"); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 246 | stuck_indexer_detector_->mutable_X_hat() = loop_->X_hat(); |
| 247 | should_reset_ = false; |
| 248 | saturated_ = false; |
| 249 | } |
| 250 | |
| 251 | if (!disable) { |
| 252 | ::Eigen::Matrix<double, 2, 1> goal_state = |
| 253 | profile_.Update(unprofiled_goal_(2, 0), unprofiled_goal_(3, 0)); |
| 254 | |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 255 | constexpr double kDt = |
| 256 | ::aos::time::DurationInSeconds(::aos::controls::kLoopFrequency); |
Austin Schuh | 471d3b1 | 2017-03-25 20:49:14 -0700 | [diff] [blame] | 257 | |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 258 | loop_->mutable_next_R(0, 0) = 0.0; |
Austin Schuh | 471d3b1 | 2017-03-25 20:49:14 -0700 | [diff] [blame] | 259 | // TODO(austin): This might not handle saturation right, but I'm not sure I |
| 260 | // really care. |
| 261 | loop_->mutable_next_R(1, 0) = ::aos::Clip( |
| 262 | unprofiled_goal_(1, 0), loop_->R(1, 0) - kIndexerAcceleration * kDt, |
| 263 | loop_->R(1, 0) + kIndexerAcceleration * kDt); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 264 | loop_->mutable_next_R(2, 0) = goal_state(0, 0); |
| 265 | loop_->mutable_next_R(3, 0) = goal_state(1, 0); |
| 266 | loop_->mutable_next_R(4, 0) = 0.0; |
| 267 | loop_->mutable_next_R(5, 0) = 0.0; |
| 268 | CapGoal("next R", &loop_->mutable_next_R()); |
| 269 | } |
| 270 | |
| 271 | // If the indexer goal velocity is low, switch to the indexer controller which |
| 272 | // won't fight to keep it moving at 0. |
| 273 | if (::std::abs(unprofiled_goal_(1, 0)) < 0.1) { |
| 274 | loop_->set_index(1); |
| 275 | } else { |
| 276 | loop_->set_index(0); |
| 277 | } |
| 278 | loop_->Update(disable); |
| 279 | |
| 280 | if (!disable && loop_->U(1, 0) != loop_->U_uncapped(1, 0)) { |
| 281 | const ::Eigen::Matrix<double, 6, 1> &R = loop_->R(); |
| 282 | profile_.MoveCurrentState(R.block<2, 1>(2, 0)); |
| 283 | saturated_ = true; |
| 284 | } else { |
| 285 | saturated_ = false; |
| 286 | } |
| 287 | |
| 288 | // Run the KF predict step. |
| 289 | stuck_indexer_detector_->UpdateObserver(loop_->U(), |
| 290 | ::aos::controls::kLoopFrequency); |
| 291 | } |
| 292 | |
| 293 | bool ColumnProfiledSubsystem::CheckHardLimits() { |
| 294 | // Returns whether hard limits have been exceeded. |
| 295 | |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 296 | if (turret_position() > range_.upper_hard || |
| 297 | turret_position() < range_.lower_hard) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 298 | AOS_LOG(ERROR, |
| 299 | "ColumnProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n", |
| 300 | turret_position(), range_.lower_hard, range_.upper_hard); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | void ColumnProfiledSubsystem::AdjustProfile( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 308 | const ::frc971::ProfileParameters *profile_parameters) { |
| 309 | AdjustProfile( |
| 310 | profile_parameters == nullptr ? 0.0 : profile_parameters->max_velocity(), |
| 311 | profile_parameters == nullptr ? 0.0 |
| 312 | : profile_parameters->max_acceleration()); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void ColumnProfiledSubsystem::AdjustProfile(double max_angular_velocity, |
| 316 | double max_angular_acceleration) { |
| 317 | profile_.set_maximum_velocity( |
| 318 | ::frc971::control_loops::internal::UseUnlessZero(max_angular_velocity, |
| 319 | default_velocity_)); |
| 320 | profile_.set_maximum_acceleration( |
| 321 | ::frc971::control_loops::internal::UseUnlessZero(max_angular_acceleration, |
| 322 | default_acceleration_)); |
| 323 | } |
| 324 | |
| 325 | double ColumnProfiledSubsystem::IndexerStuckVoltage() const { |
| 326 | // Compute the voltage from the control loop, excluding the voltage error |
| 327 | // term. |
| 328 | const double uncapped_applied_voltage = |
| 329 | uncapped_indexer_voltage() + X_hat(4, 0); |
| 330 | if (uncapped_applied_voltage < 0) { |
| 331 | return +stuck_indexer_X_hat_current_(4, 0); |
| 332 | } else { |
| 333 | return -stuck_indexer_X_hat_current_(4, 0); |
| 334 | } |
| 335 | } |
| 336 | bool ColumnProfiledSubsystem::IsIndexerStuck() const { |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 337 | return IndexerStuckVoltage() > 4.0; |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void ColumnProfiledSubsystem::PartialIndexerReset() { |
| 341 | mutable_X_hat(4, 0) = 0.0; |
| 342 | stuck_indexer_detector_->mutable_X_hat(4, 0) = 0.0; |
Austin Schuh | 471d3b1 | 2017-03-25 20:49:14 -0700 | [diff] [blame] | 343 | // Screw it, we are stuck. Reset the current goal to the current velocity so |
| 344 | // we start slewing faster to reverse if we have stopped. |
| 345 | loop_->mutable_R(1, 0) = X_hat(1, 0); |
| 346 | loop_->mutable_next_R(1, 0) = X_hat(1, 0); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void ColumnProfiledSubsystem::PartialTurretReset() { |
| 350 | mutable_X_hat(5, 0) = 0.0; |
| 351 | stuck_indexer_detector_->mutable_X_hat(5, 0) = 0.0; |
| 352 | } |
| 353 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 354 | void ColumnProfiledSubsystem::PopulateIndexerStatus( |
| 355 | IndexerStatus::Builder *status_builder) { |
| 356 | status_builder->add_avg_angular_velocity(indexer_average_angular_velocity_); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 357 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 358 | status_builder->add_angular_velocity(X_hat_current_(1, 0)); |
| 359 | status_builder->add_ready(indexer_ready_); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 360 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 361 | status_builder->add_voltage_error(X_hat_current_(4, 0)); |
| 362 | status_builder->add_stuck_voltage_error(stuck_indexer_X_hat_current_(4, 0)); |
| 363 | status_builder->add_position_error(indexer_position_error_); |
| 364 | status_builder->add_instantaneous_velocity(indexer_dt_velocity_); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 365 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 366 | status_builder->add_stuck(IsIndexerStuck()); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 367 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 368 | status_builder->add_stuck_voltage(IndexerStuckVoltage()); |
| 369 | } |
| 370 | |
| 371 | void ColumnProfiledSubsystem::PopulateTurretStatus( |
| 372 | TurretProfiledSubsystemStatus::Builder *status_builder, |
| 373 | flatbuffers::Offset<ColumnZeroingEstimator::State> estimator_state_offset) { |
| 374 | status_builder->add_zeroed(zeroed()); |
| 375 | |
| 376 | status_builder->add_position(X_hat(2, 0)); |
| 377 | status_builder->add_velocity(X_hat(3, 0)); |
| 378 | status_builder->add_goal_position(goal(2, 0)); |
| 379 | status_builder->add_goal_velocity(goal(3, 0)); |
| 380 | status_builder->add_unprofiled_goal_position(unprofiled_goal(2, 0)); |
| 381 | status_builder->add_unprofiled_goal_velocity(unprofiled_goal(3, 0)); |
| 382 | status_builder->add_voltage_error(X_hat(5, 0)); |
| 383 | status_builder->add_calculated_velocity( |
| 384 | (turret_position() - turret_last_position_) / |
| 385 | ::aos::time::DurationInSeconds(::aos::controls::kLoopFrequency)); |
| 386 | |
| 387 | status_builder->add_estimator_state(estimator_state_offset); |
| 388 | |
| 389 | Eigen::Matrix<double, 6, 1> error = controller().error(); |
| 390 | status_builder->add_position_power(controller().controller().K(0, 0) * |
| 391 | error(0, 0)); |
| 392 | status_builder->add_velocity_power(controller().controller().K(0, 1) * |
| 393 | error(1, 0)); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Austin Schuh | b6c5c85 | 2019-05-19 20:13:31 -0700 | [diff] [blame] | 396 | Column::Column(::aos::EventLoop *event_loop) |
| 397 | : vision_time_adjuster_(event_loop), |
| 398 | profiled_subsystem_( |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 399 | ::std::unique_ptr< |
| 400 | ::frc971::control_loops::SimpleCappedStateFeedbackLoop<6, 2, 2>>( |
| 401 | new ::frc971::control_loops::SimpleCappedStateFeedbackLoop< |
| 402 | 6, 2, 2>(MakeIntegralColumnLoop())), |
| 403 | constants::GetValues().column, constants::Values::kTurretRange, 7.0, |
Austin Schuh | 25db126 | 2017-04-05 19:39:55 -0700 | [diff] [blame] | 404 | 50.0), |
| 405 | vision_error_(constants::GetValues().vision_error) {} |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 406 | |
| 407 | void Column::Reset() { |
| 408 | state_ = State::UNINITIALIZED; |
| 409 | indexer_state_ = IndexerState::RUNNING; |
| 410 | profiled_subsystem_.Reset(); |
| 411 | // intake will automatically clear the minimum position on reset, so we don't |
| 412 | // need to do it here. |
| 413 | freeze_ = false; |
| 414 | } |
| 415 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 416 | std::pair<flatbuffers::Offset<IndexerStatus>, |
| 417 | flatbuffers::Offset<TurretProfiledSubsystemStatus>> |
| 418 | Column::Iterate(const ::aos::monotonic_clock::time_point monotonic_now, |
| 419 | const IndexerGoalT *unsafe_indexer_goal, |
| 420 | const TurretGoal *unsafe_turret_goal, |
| 421 | const ColumnPosition *position, |
| 422 | const vision::VisionStatus *vision_status, |
| 423 | double *indexer_output, double *turret_output, |
| 424 | flatbuffers::FlatBufferBuilder *fbb, intake::Intake *intake) { |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 425 | bool disable = turret_output == nullptr || indexer_output == nullptr; |
| 426 | profiled_subsystem_.Correct(*position); |
| 427 | |
Austin Schuh | 9271536 | 2019-07-07 20:47:45 -0700 | [diff] [blame] | 428 | vision_time_adjuster_.Tick(monotonic_now, profiled_subsystem_.X_hat(2, 0), |
| 429 | vision_status); |
Austin Schuh | ac76bb3 | 2017-03-22 22:34:26 -0700 | [diff] [blame] | 430 | |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 431 | switch (state_) { |
| 432 | case State::UNINITIALIZED: |
| 433 | // Wait in the uninitialized state until the turret is initialized. |
| 434 | // Set the goals to where we are now so when we start back up, we don't |
| 435 | // jump. |
| 436 | profiled_subsystem_.ForceGoal(0.0, profiled_subsystem_.turret_position()); |
| 437 | state_ = State::ZEROING_UNINITIALIZED; |
| 438 | |
| 439 | // Fall through so we can start the zeroing process immediately. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame^] | 440 | [[fallthrough]]; |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 441 | |
| 442 | case State::ZEROING_UNINITIALIZED: |
| 443 | // Set up the profile to be the zeroing profile. |
| 444 | profiled_subsystem_.AdjustProfile(0.50, 3); |
| 445 | |
| 446 | // Force the intake out. |
| 447 | intake->set_min_position(kIntakeZeroingMinDistance); |
| 448 | |
| 449 | if (disable) { |
| 450 | // If we are disabled, we want to reset the turret to stay where it |
| 451 | // currently is. |
| 452 | profiled_subsystem_.ForceGoal(0.0, |
| 453 | profiled_subsystem_.turret_position()); |
| 454 | } else if (profiled_subsystem_.initialized()) { |
| 455 | // If we are initialized, there's no value in continuing to move so stop |
| 456 | // and wait on the intake. |
| 457 | profiled_subsystem_.set_indexer_unprofiled_goal(0.0); |
| 458 | } else { |
| 459 | // Spin slowly backwards. |
| 460 | profiled_subsystem_.set_indexer_unprofiled_goal(2.0); |
| 461 | } |
| 462 | |
| 463 | // See if we are zeroed or initialized and far enough out and execute the |
| 464 | // proper state transition. |
| 465 | if (profiled_subsystem_.zeroed()) { |
| 466 | intake->clear_min_position(); |
| 467 | state_ = State::RUNNING; |
| 468 | } else if (profiled_subsystem_.initialized() && |
| 469 | intake->position() > |
| 470 | kIntakeZeroingMinDistance - kIntakeTolerance) { |
| 471 | if (profiled_subsystem_.turret_position() > 0) { |
| 472 | // We need to move in the negative direction. |
| 473 | state_ = State::ZEROING_NEGATIVE; |
| 474 | } else { |
| 475 | // We need to move in the positive direction. |
| 476 | state_ = State::ZEROING_POSITIVE; |
| 477 | } |
| 478 | } |
| 479 | break; |
| 480 | |
| 481 | case State::ZEROING_POSITIVE: |
| 482 | // We are now going to be moving in the positive direction towards 0. If |
| 483 | // we get close enough, we'll zero. |
| 484 | profiled_subsystem_.set_unprofiled_goal(0.0, 0.0); |
| 485 | intake->set_min_position(kIntakeZeroingMinDistance); |
| 486 | |
| 487 | if (profiled_subsystem_.zeroed()) { |
| 488 | intake->clear_min_position(); |
| 489 | state_ = State::RUNNING; |
| 490 | } else if (disable) { |
| 491 | // We are disabled, so pick back up from the current position. |
| 492 | profiled_subsystem_.ForceGoal(0.0, |
| 493 | profiled_subsystem_.turret_position()); |
| 494 | } else if (profiled_subsystem_.turret_position() < |
| 495 | profiled_subsystem_.goal(2, 0) - |
| 496 | kStuckZeroingTrackingError || |
| 497 | profiled_subsystem_.saturated()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 498 | AOS_LOG( |
| 499 | INFO, |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 500 | "Turret stuck going positive, switching directions. At %f, goal " |
| 501 | "%f\n", |
| 502 | profiled_subsystem_.turret_position(), |
| 503 | profiled_subsystem_.goal(2, 0)); |
| 504 | // The turret got too far behind. Declare it stuck and reverse. |
| 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_NEGATIVE; |
| 512 | } |
| 513 | break; |
| 514 | |
| 515 | case State::ZEROING_NEGATIVE: |
| 516 | // We are now going to be moving in the negative direction towards 0. If |
| 517 | // we get close enough, we'll zero. |
| 518 | profiled_subsystem_.set_unprofiled_goal(0.0, 0.0); |
| 519 | intake->set_min_position(kIntakeZeroingMinDistance); |
| 520 | |
| 521 | if (profiled_subsystem_.zeroed()) { |
| 522 | intake->clear_min_position(); |
| 523 | state_ = State::RUNNING; |
| 524 | } else if (disable) { |
| 525 | // We are disabled, so pick back up from the current position. |
| 526 | profiled_subsystem_.ForceGoal(0.0, |
| 527 | profiled_subsystem_.turret_position()); |
| 528 | } else if (profiled_subsystem_.turret_position() > |
| 529 | profiled_subsystem_.goal(2, 0) + |
| 530 | kStuckZeroingTrackingError || |
| 531 | profiled_subsystem_.saturated()) { |
| 532 | // The turret got too far behind. Declare it stuck and reverse. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 533 | AOS_LOG( |
| 534 | INFO, |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 535 | "Turret stuck going negative, switching directions. At %f, goal " |
| 536 | "%f\n", |
| 537 | profiled_subsystem_.turret_position(), |
| 538 | profiled_subsystem_.goal(2, 0)); |
| 539 | profiled_subsystem_.AddOffset(0.0, -2.0 * M_PI); |
| 540 | profiled_subsystem_.set_unprofiled_goal(0.0, 0.0); |
| 541 | profiled_subsystem_.ForceGoal(0.0, |
| 542 | profiled_subsystem_.turret_position()); |
| 543 | profiled_subsystem_.PartialTurretReset(); |
| 544 | profiled_subsystem_.PartialIndexerReset(); |
| 545 | state_ = State::ZEROING_POSITIVE; |
| 546 | } |
| 547 | break; |
| 548 | |
| 549 | case State::RUNNING: { |
| 550 | double starting_goal_angle = profiled_subsystem_.goal(2, 0); |
| 551 | if (disable) { |
| 552 | // Reset the profile to the current position so it starts from here when |
| 553 | // we get re-enabled. |
| 554 | profiled_subsystem_.ForceGoal(0.0, |
| 555 | profiled_subsystem_.turret_position()); |
| 556 | } |
| 557 | |
| 558 | if (unsafe_turret_goal && unsafe_indexer_goal) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 559 | profiled_subsystem_.AdjustProfile(unsafe_turret_goal->profile_params()); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 560 | profiled_subsystem_.set_unprofiled_goal( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 561 | unsafe_indexer_goal->angular_velocity, |
| 562 | unsafe_turret_goal->angle()); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 563 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 564 | if (unsafe_turret_goal->track()) { |
Austin Schuh | ac76bb3 | 2017-03-22 22:34:26 -0700 | [diff] [blame] | 565 | if (vision_time_adjuster_.valid()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 566 | AOS_LOG(INFO, "Vision aligning to %f\n", |
| 567 | vision_time_adjuster_.goal()); |
Austin Schuh | ac76bb3 | 2017-03-22 22:34:26 -0700 | [diff] [blame] | 568 | profiled_subsystem_.set_turret_unprofiled_goal( |
Austin Schuh | 25db126 | 2017-04-05 19:39:55 -0700 | [diff] [blame] | 569 | vision_time_adjuster_.goal() + vision_error_); |
Austin Schuh | ac76bb3 | 2017-03-22 22:34:26 -0700 | [diff] [blame] | 570 | } |
Austin Schuh | d85c66e | 2017-04-16 10:50:33 -0700 | [diff] [blame] | 571 | } else { |
| 572 | vision_time_adjuster_.ResetTime(); |
Austin Schuh | ac76bb3 | 2017-03-22 22:34:26 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 575 | if (freeze_) { |
| 576 | profiled_subsystem_.ForceGoal(unsafe_indexer_goal->angular_velocity, |
| 577 | starting_goal_angle); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | // ESTOP if we hit the hard limits. |
| 582 | if (profiled_subsystem_.CheckHardLimits() || |
| 583 | profiled_subsystem_.error()) { |
| 584 | state_ = State::ESTOP; |
| 585 | } |
| 586 | } break; |
| 587 | |
| 588 | case State::ESTOP: |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 589 | AOS_LOG(ERROR, "Estop\n"); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 590 | disable = true; |
| 591 | break; |
| 592 | } |
| 593 | |
| 594 | // Start indexing at the suggested velocity. |
| 595 | // If a "stuck" event is detected, reverse. Stay reversed until either |
| 596 | // unstuck, or 0.5 seconds have elapsed. |
| 597 | // Then, start going forwards. Don't detect stuck for 0.5 seconds. |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 598 | switch (indexer_state_) { |
| 599 | case IndexerState::RUNNING: |
| 600 | // The velocity goal is already set above in this case, so leave it |
| 601 | // alone. |
| 602 | |
| 603 | // If we are stuck and weren't just reversing, try reversing to unstick |
| 604 | // us. We don't want to chatter back and forth too fast if reversing |
| 605 | // isn't working. |
| 606 | if (profiled_subsystem_.IsIndexerStuck() && |
| 607 | monotonic_now > kForwardTimeout + last_transition_time_) { |
| 608 | indexer_state_ = IndexerState::REVERSING; |
| 609 | last_transition_time_ = monotonic_now; |
| 610 | profiled_subsystem_.PartialIndexerReset(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 611 | AOS_LOG(INFO, "Partial indexer reset while going forwards\n"); |
| 612 | AOS_LOG(INFO, "Indexer RUNNING -> REVERSING\n"); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 613 | } |
| 614 | break; |
| 615 | case IndexerState::REVERSING: |
| 616 | // "Reverse" "slowly". |
| 617 | profiled_subsystem_.set_indexer_unprofiled_goal( |
| 618 | -5.0 * ::aos::sign(profiled_subsystem_.unprofiled_goal(1, 0))); |
| 619 | |
| 620 | // If we've timed out or are no longer stuck, try running again. |
| 621 | if ((!profiled_subsystem_.IsIndexerStuck() && |
| 622 | monotonic_now > last_transition_time_ + kReverseMinTimeout) || |
| 623 | monotonic_now > kReverseTimeout + last_transition_time_) { |
| 624 | indexer_state_ = IndexerState::RUNNING; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 625 | AOS_LOG(INFO, "Indexer REVERSING -> RUNNING, stuck %d\n", |
| 626 | profiled_subsystem_.IsIndexerStuck()); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 627 | |
| 628 | // Only reset if we got stuck going this way too. |
| 629 | if (monotonic_now > kReverseTimeout + last_transition_time_) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 630 | AOS_LOG(INFO, "Partial indexer reset while reversing\n"); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 631 | profiled_subsystem_.PartialIndexerReset(); |
| 632 | } |
| 633 | last_transition_time_ = monotonic_now; |
| 634 | } |
| 635 | break; |
| 636 | } |
| 637 | |
| 638 | // Set the voltage limits. |
| 639 | const double max_voltage = |
| 640 | (state_ == State::RUNNING) ? kOperatingVoltage : kZeroingVoltage; |
| 641 | |
| 642 | profiled_subsystem_.set_max_voltage({{max_voltage, max_voltage}}); |
| 643 | |
| 644 | // Calculate the loops for a cycle. |
| 645 | profiled_subsystem_.Update(disable); |
| 646 | |
| 647 | // Write out all the voltages. |
| 648 | if (indexer_output) { |
| 649 | *indexer_output = profiled_subsystem_.indexer_voltage(); |
| 650 | } |
| 651 | if (turret_output) { |
| 652 | *turret_output = profiled_subsystem_.turret_voltage(); |
| 653 | } |
| 654 | |
| 655 | // Save debug/internal state. |
| 656 | // TODO(austin): Save more. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 657 | flatbuffers::Offset<ColumnZeroingEstimator::State> |
| 658 | column_estimator_state_offset = |
| 659 | profiled_subsystem_.EstimatorState(fbb, 0); |
| 660 | |
| 661 | TurretProfiledSubsystemStatus::Builder turret_status_builder(*fbb); |
| 662 | profiled_subsystem_.PopulateTurretStatus(&turret_status_builder, |
| 663 | column_estimator_state_offset); |
| 664 | turret_status_builder.add_estopped((state_ == State::ESTOP)); |
| 665 | turret_status_builder.add_state(static_cast<int32_t>(state_)); |
| 666 | turret_status_builder.add_turret_encoder_angle( |
| 667 | profiled_subsystem_.turret_position()); |
Austin Schuh | ac76bb3 | 2017-03-22 22:34:26 -0700 | [diff] [blame] | 668 | if (vision_time_adjuster_.valid()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 669 | turret_status_builder.add_vision_angle(vision_time_adjuster_.goal()); |
| 670 | turret_status_builder.add_raw_vision_angle( |
| 671 | vision_time_adjuster_.most_recent_vision_reading()); |
| 672 | turret_status_builder.add_vision_tracking(true); |
Austin Schuh | ac76bb3 | 2017-03-22 22:34:26 -0700 | [diff] [blame] | 673 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 674 | turret_status_builder.add_vision_angle( |
| 675 | ::std::numeric_limits<double>::quiet_NaN()); |
| 676 | turret_status_builder.add_vision_tracking(false); |
Austin Schuh | ac76bb3 | 2017-03-22 22:34:26 -0700 | [diff] [blame] | 677 | } |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 678 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 679 | flatbuffers::Offset<TurretProfiledSubsystemStatus> turret_status_offset = |
| 680 | turret_status_builder.Finish(); |
| 681 | |
| 682 | IndexerStatus::Builder indexer_status_builder(*fbb); |
| 683 | profiled_subsystem_.PopulateIndexerStatus(&indexer_status_builder); |
| 684 | |
| 685 | indexer_status_builder.add_state(static_cast<int32_t>(indexer_state_)); |
| 686 | |
| 687 | flatbuffers::Offset<IndexerStatus> indexer_status_offset = |
| 688 | indexer_status_builder.Finish(); |
| 689 | |
| 690 | return std::make_pair(indexer_status_offset, turret_status_offset); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | } // namespace column |
| 694 | } // namespace superstructure |
| 695 | } // namespace control_loops |
| 696 | } // namespace y2017 |