Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 1 | #include "motors/seems_reasonable/spring.h" |
| 2 | |
| 3 | #include "frc971/zeroing/wrap.h" |
| 4 | |
| 5 | #include <cmath> |
| 6 | |
| 7 | namespace motors { |
| 8 | namespace seems_reasonable { |
| 9 | namespace { |
| 10 | |
| 11 | constexpr float kTwoPi = 2.0 * M_PI; |
| 12 | |
| 13 | } // namespace |
| 14 | |
| 15 | float NextGoal(float current_goal, float goal) { |
| 16 | float remainder = remainderf(current_goal - goal, kTwoPi); |
| 17 | if (remainder >= 0.0f) { |
| 18 | remainder -= kTwoPi; |
| 19 | } |
| 20 | return -remainder + current_goal; |
| 21 | } |
| 22 | |
| 23 | float PreviousGoal(float current_goal, float goal) { |
| 24 | float remainder = remainderf(current_goal - goal, kTwoPi); |
| 25 | if (remainder <= 0.0f) { |
| 26 | remainder += kTwoPi; |
| 27 | } |
| 28 | return -remainder + current_goal; |
| 29 | } |
| 30 | |
| 31 | void Spring::Iterate(bool unload, bool prime, bool fire, bool force_reset, |
Austin Schuh | e666dc6 | 2018-08-08 21:09:12 -0700 | [diff] [blame] | 32 | bool force_move, bool encoder_valid, float angle) { |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 33 | // Angle is +- M_PI. So, we need to find the nearest angle to the previous |
| 34 | // one, and that's our new angle. |
| 35 | angle_ = ::frc971::zeroing::Wrap(angle_, angle, kTwoPi); |
| 36 | |
| 37 | switch (state_) { |
| 38 | case State::UNINITIALIZED: |
| 39 | // Go to the previous unload from where we are. |
| 40 | goal_ = angle_; |
| 41 | goal_ = PreviousGoal(kUnloadGoal); |
Austin Schuh | e666dc6 | 2018-08-08 21:09:12 -0700 | [diff] [blame] | 42 | if (force_move) { |
| 43 | ForceMove(); |
| 44 | } else if (prime && fire) { |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 45 | Unload(); |
| 46 | } |
| 47 | break; |
Austin Schuh | e666dc6 | 2018-08-08 21:09:12 -0700 | [diff] [blame] | 48 | case State::FORCE_MOVE: |
| 49 | if (!force_move) { |
| 50 | state_ = State::UNINITIALIZED; |
| 51 | } |
| 52 | break; |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 53 | case State::UNLOAD: |
Austin Schuh | e666dc6 | 2018-08-08 21:09:12 -0700 | [diff] [blame] | 54 | if (force_move) { |
| 55 | ForceMove(); |
| 56 | } else if (!encoder_valid) { |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 57 | state_ = State::STUCK_UNLOAD; |
| 58 | } else if (!unload && prime && fire) { |
| 59 | // Go to the next goal from the current location. This handles if we |
| 60 | // fired or didn't on the previous cycle. |
| 61 | goal_ = angle_; |
| 62 | goal_ = NextGoal(kLoadGoal); |
| 63 | Load(); |
| 64 | } |
Brian Silverman | 1eea113 | 2018-09-02 16:29:53 -0700 | [diff] [blame] | 65 | // TODO(Austin): This should be a break, right? |
| 66 | // fallthrough |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 67 | case State::STUCK_UNLOAD: |
Austin Schuh | e666dc6 | 2018-08-08 21:09:12 -0700 | [diff] [blame] | 68 | if (force_move) { |
| 69 | ForceMove(); |
| 70 | } else if (force_reset && encoder_valid && |
| 71 | state_ == State::STUCK_UNLOAD) { |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 72 | state_ = State::UNINITIALIZED; |
| 73 | } else if (timeout_ > 0) { |
| 74 | --timeout_; |
| 75 | } |
| 76 | break; |
| 77 | case State::LOAD: |
Austin Schuh | e666dc6 | 2018-08-08 21:09:12 -0700 | [diff] [blame] | 78 | if (force_move) { |
| 79 | ForceMove(); |
| 80 | } else if (!encoder_valid) { |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 81 | goal_ = PreviousGoal(kUnloadGoal); |
| 82 | StuckUnload(); |
| 83 | } else if (unload) { |
| 84 | goal_ = PreviousGoal(kUnloadGoal); |
| 85 | Unload(); |
| 86 | } else if (!Near()) { |
| 87 | if (timeout_ > 0) { |
| 88 | --timeout_; |
| 89 | } else { |
| 90 | StuckUnload(); |
| 91 | } |
| 92 | } else if (prime) { |
| 93 | goal_ = NextGoal(kPrimeGoal); |
| 94 | Prime(); |
| 95 | } |
| 96 | break; |
| 97 | case State::PRIME: |
Austin Schuh | e666dc6 | 2018-08-08 21:09:12 -0700 | [diff] [blame] | 98 | if (force_move) { |
| 99 | ForceMove(); |
| 100 | } else if (!encoder_valid) { |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 101 | goal_ = PreviousGoal(kUnloadGoal); |
| 102 | StuckUnload(); |
| 103 | } else if (unload) { |
| 104 | goal_ = PreviousGoal(kUnloadGoal); |
| 105 | Unload(); |
| 106 | } else if (!prime) { |
| 107 | goal_ = PreviousGoal(kLoadGoal); |
| 108 | Load(); |
| 109 | } else if (!Near()) { |
| 110 | if (timeout_ > 0) { |
| 111 | --timeout_; |
| 112 | } else { |
| 113 | StuckUnload(); |
| 114 | } |
| 115 | } else if (fire) { |
| 116 | goal_ = NextGoal(kFireGoal); |
| 117 | Fire(); |
| 118 | } |
| 119 | break; |
| 120 | |
| 121 | case State::FIRE: |
Austin Schuh | e666dc6 | 2018-08-08 21:09:12 -0700 | [diff] [blame] | 122 | if (force_move) { |
| 123 | ForceMove(); |
| 124 | } else if (!encoder_valid) { |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 125 | goal_ = PreviousGoal(kUnloadGoal); |
| 126 | StuckUnload(); |
| 127 | } else if (!Near()) { |
| 128 | if (timeout_ > 0) { |
| 129 | --timeout_; |
| 130 | } else { |
| 131 | StuckUnload(); |
| 132 | } |
| 133 | } else { |
| 134 | // TODO(austin): Maybe have a different timeout for success. |
| 135 | if (timeout_ > 0) { |
| 136 | timeout_--; |
Austin Schuh | 72656c4 | 2018-04-01 16:37:52 -0700 | [diff] [blame] | 137 | } else if (!prime) { |
| 138 | state_ = State::WAIT_FOR_LOAD; |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | break; |
Austin Schuh | 72656c4 | 2018-04-01 16:37:52 -0700 | [diff] [blame] | 142 | case State::WAIT_FOR_LOAD: |
Austin Schuh | e666dc6 | 2018-08-08 21:09:12 -0700 | [diff] [blame] | 143 | if (force_move) { |
| 144 | ForceMove(); |
| 145 | } else if (!encoder_valid) { |
Austin Schuh | 72656c4 | 2018-04-01 16:37:52 -0700 | [diff] [blame] | 146 | StuckUnload(); |
| 147 | } else if (unload) { |
| 148 | // Goal is as good as it is going to get since unload is the fire |
| 149 | // position. |
| 150 | Unload(); |
| 151 | } else if (prime) { |
| 152 | state_ = State::WAIT_FOR_LOAD_RELEASE; |
| 153 | } |
| 154 | break; |
| 155 | case State::WAIT_FOR_LOAD_RELEASE: |
Austin Schuh | e666dc6 | 2018-08-08 21:09:12 -0700 | [diff] [blame] | 156 | if (force_move) { |
| 157 | ForceMove(); |
| 158 | } else if (!encoder_valid) { |
Austin Schuh | 72656c4 | 2018-04-01 16:37:52 -0700 | [diff] [blame] | 159 | StuckUnload(); |
| 160 | } else if (unload) { |
| 161 | // Goal is as good as it is going to get since unload is the fire |
| 162 | // position. |
| 163 | Unload(); |
| 164 | } else if (!prime) { |
| 165 | Load(); |
| 166 | goal_ = NextGoal(kLoadGoal); |
| 167 | } |
| 168 | break; |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 169 | } |
| 170 | const float error = goal_ - angle_; |
| 171 | const float derror = (error - last_error_) * 200.0f; |
| 172 | |
| 173 | switch (state_) { |
| 174 | case State::UNINITIALIZED: |
| 175 | output_ = 0.0f; |
| 176 | break; |
| 177 | case State::STUCK_UNLOAD: |
| 178 | case State::UNLOAD: |
| 179 | if (timeout_ > 0) { |
| 180 | output_ = -0.1f; |
| 181 | } else { |
| 182 | output_ = 0.0f; |
| 183 | } |
| 184 | break; |
| 185 | |
Austin Schuh | e666dc6 | 2018-08-08 21:09:12 -0700 | [diff] [blame] | 186 | case State::FORCE_MOVE: |
| 187 | output_ = 1.0f; |
| 188 | break; |
| 189 | |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 190 | case State::LOAD: |
| 191 | case State::PRIME: |
Austin Schuh | 72656c4 | 2018-04-01 16:37:52 -0700 | [diff] [blame] | 192 | case State::FIRE: |
| 193 | case State::WAIT_FOR_LOAD: |
| 194 | case State::WAIT_FOR_LOAD_RELEASE: { |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 195 | constexpr float kP = 3.00f; |
| 196 | constexpr float kD = 0.00f; |
| 197 | output_ = kP * error + kD * derror; |
| 198 | } break; |
| 199 | } |
| 200 | last_error_ = error; |
| 201 | } |
| 202 | |
| 203 | } // namespace seems_reasonable |
| 204 | } // namespace motors |