Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 1 | #include "y2017/control_loops/superstructure/hood/hood.h" |
| 2 | |
| 3 | #include "y2017/constants.h" |
| 4 | #include "y2017/control_loops/superstructure/hood/hood_integral_plant.h" |
| 5 | |
| 6 | namespace y2017 { |
| 7 | namespace control_loops { |
| 8 | namespace superstructure { |
| 9 | namespace hood { |
| 10 | |
| 11 | constexpr double Hood::kZeroingVoltage; |
| 12 | constexpr double Hood::kOperatingVoltage; |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 13 | // The tracking error to allow before declaring that we are stuck and reversing |
| 14 | // direction while zeroing. |
| 15 | constexpr double kStuckZeroingTrackingError = 0.02; |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 16 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 17 | IndexPulseProfiledSubsystem::IndexPulseProfiledSubsystem() |
| 18 | : ::frc971::control_loops::SingleDOFProfiledSubsystem< |
| 19 | ::frc971::zeroing::PulseIndexZeroingEstimator>( |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 20 | ::std::unique_ptr< |
| 21 | ::frc971::control_loops::SimpleCappedStateFeedbackLoop<3, 1, 1>>( |
| 22 | new ::frc971::control_loops::SimpleCappedStateFeedbackLoop< |
| 23 | 3, 1, 1>(MakeIntegralHoodLoop())), |
| 24 | constants::GetValues().hood.zeroing, constants::Values::kHoodRange, |
| 25 | 0.5, 10.0) {} |
| 26 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 27 | void IndexPulseProfiledSubsystem::CapGoal(const char *name, |
| 28 | Eigen::Matrix<double, 3, 1> *goal) { |
| 29 | if (zeroed()) { |
| 30 | ::frc971::control_loops::SingleDOFProfiledSubsystem< |
| 31 | ::frc971::zeroing::PulseIndexZeroingEstimator>::CapGoal(name, goal); |
| 32 | } else { |
| 33 | const double kMaxRange = range().upper_hard - range().lower_hard; |
| 34 | // Limit the goal to min/max allowable positions much less agressively. |
| 35 | // We don't know where the limits are, so we have to let the user move far |
| 36 | // enough to find them (and the index pulse which might be right next to |
| 37 | // one). |
| 38 | if ((*goal)(0, 0) > kMaxRange) { |
| 39 | LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0), |
| 40 | kMaxRange); |
| 41 | (*goal)(0, 0) = kMaxRange; |
| 42 | } |
| 43 | if ((*goal)(0, 0) < -kMaxRange) { |
| 44 | LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0), |
| 45 | kMaxRange); |
| 46 | (*goal)(0, 0) = -kMaxRange; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | Hood::Hood() {} |
| 52 | |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 53 | void Hood::Reset() { |
| 54 | state_ = State::UNINITIALIZED; |
| 55 | profiled_subsystem_.Reset(); |
| 56 | } |
| 57 | |
| 58 | void Hood::Iterate(const control_loops::HoodGoal *unsafe_goal, |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 59 | const ::frc971::IndexPosition *position, double *output, |
| 60 | ::frc971::control_loops::IndexProfiledJointStatus *status) { |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 61 | bool disable = output == nullptr; |
| 62 | profiled_subsystem_.Correct(*position); |
| 63 | |
| 64 | switch (state_) { |
| 65 | case State::UNINITIALIZED: |
| 66 | // Wait in the uninitialized state until the hood is initialized. |
| 67 | LOG(DEBUG, "Uninitialized, waiting for hood\n"); |
| 68 | if (profiled_subsystem_.initialized()) { |
| 69 | state_ = State::DISABLED_INITIALIZED; |
| 70 | } |
| 71 | disable = true; |
| 72 | break; |
| 73 | |
| 74 | case State::DISABLED_INITIALIZED: |
| 75 | // Wait here until we are either fully zeroed while disabled, or we become |
| 76 | // enabled. |
| 77 | if (disable) { |
| 78 | if (profiled_subsystem_.zeroed()) { |
| 79 | state_ = State::RUNNING; |
| 80 | } |
| 81 | } else { |
| 82 | state_ = State::ZEROING; |
| 83 | } |
| 84 | |
| 85 | // Set the goals to where we are now so when we start back up, we don't |
| 86 | // jump. |
| 87 | profiled_subsystem_.ForceGoal(profiled_subsystem_.position()); |
| 88 | // Set up the profile to be the zeroing profile. |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 89 | profiled_subsystem_.AdjustProfile(0.30, 1.0); |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 90 | |
| 91 | // We are not ready to start doing anything yet. |
| 92 | disable = true; |
| 93 | break; |
| 94 | |
| 95 | case State::ZEROING: |
| 96 | if (profiled_subsystem_.zeroed()) { |
| 97 | // Move the goal to the current goal so we stop moving. |
| 98 | profiled_subsystem_.set_unprofiled_goal(profiled_subsystem_.goal(0, 0)); |
| 99 | state_ = State::RUNNING; |
| 100 | } else if (disable) { |
| 101 | state_ = State::DISABLED_INITIALIZED; |
| 102 | } else { |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 103 | const double kRange = profiled_subsystem_.range().upper_hard - |
| 104 | profiled_subsystem_.range().lower_hard; |
| 105 | // Seek +- the range of motion. |
| 106 | if (profiled_subsystem_.position() > 0) { |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 107 | // We are above the middle. |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 108 | if (profiled_subsystem_.goal(1, 0) > 0 && |
| 109 | ::std::abs(profiled_subsystem_.position() - |
| 110 | profiled_subsystem_.goal(0, 0)) < |
| 111 | kStuckZeroingTrackingError) { |
| 112 | // And moving up and not stuck. Keep going until we've gone the |
| 113 | // full range of motion or get stuck. |
| 114 | profiled_subsystem_.set_unprofiled_goal(kRange); |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 115 | } else { |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 116 | // And no longer moving. Go down to the opposite of the range of |
| 117 | // motion. |
| 118 | profiled_subsystem_.set_unprofiled_goal(-kRange); |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 119 | } |
| 120 | } else { |
| 121 | // We are below the middle. |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 122 | if (profiled_subsystem_.goal(1, 0) < 0 && |
| 123 | ::std::abs(profiled_subsystem_.position() - |
| 124 | profiled_subsystem_.goal(0, 0)) < |
| 125 | kStuckZeroingTrackingError) { |
| 126 | // And moving down and not stuck. Keep going until we've gone the |
| 127 | // full range of motion or get stuck. |
| 128 | profiled_subsystem_.set_unprofiled_goal(-kRange); |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 129 | } else { |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 130 | // And no longer moving. Go up to the opposite of the range of |
| 131 | // motion. |
| 132 | profiled_subsystem_.set_unprofiled_goal(kRange); |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | break; |
| 137 | |
| 138 | case State::RUNNING: { |
| 139 | if (disable) { |
| 140 | // Reset the profile to the current position so it starts from here when |
| 141 | // we get re-enabled. |
| 142 | profiled_subsystem_.ForceGoal(profiled_subsystem_.position()); |
| 143 | } |
| 144 | |
| 145 | // If we have a goal, go to it. Otherwise stay where we are. |
| 146 | if (unsafe_goal) { |
| 147 | profiled_subsystem_.AdjustProfile(unsafe_goal->profile_params); |
| 148 | profiled_subsystem_.set_unprofiled_goal(unsafe_goal->angle); |
| 149 | } |
| 150 | |
| 151 | // ESTOP if we hit the hard limits. |
Austin Schuh | d93160a | 2017-03-05 01:00:54 -0800 | [diff] [blame] | 152 | if (profiled_subsystem_.CheckHardLimits() || |
| 153 | profiled_subsystem_.error()) { |
Austin Schuh | 87c1063 | 2017-02-05 19:02:17 -0800 | [diff] [blame] | 154 | state_ = State::ESTOP; |
| 155 | } |
| 156 | } break; |
| 157 | |
| 158 | case State::ESTOP: |
| 159 | LOG(ERROR, "Estop\n"); |
| 160 | disable = true; |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | // Set the voltage limits. |
| 165 | const double max_voltage = |
| 166 | (state_ == State::RUNNING) ? kOperatingVoltage : kZeroingVoltage; |
| 167 | |
| 168 | profiled_subsystem_.set_max_voltage({{max_voltage}}); |
| 169 | |
| 170 | // Calculate the loops for a cycle. |
| 171 | profiled_subsystem_.Update(disable); |
| 172 | |
| 173 | // Write out all the voltages. |
| 174 | if (output) { |
| 175 | *output = profiled_subsystem_.voltage(); |
| 176 | } |
| 177 | |
| 178 | // Save debug/internal state. |
| 179 | // TODO(austin): Save more. |
| 180 | status->zeroed = profiled_subsystem_.zeroed(); |
| 181 | |
| 182 | profiled_subsystem_.PopulateStatus(status); |
| 183 | status->estopped = (state_ == State::ESTOP); |
| 184 | status->state = static_cast<int32_t>(state_); |
| 185 | } |
| 186 | |
| 187 | } // namespace hood |
| 188 | } // namespace superstructure |
| 189 | } // namespace control_loops |
| 190 | } // namespace y2017 |