Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 1 | #include "y2018/control_loops/superstructure/arm/arm.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <iostream> |
| 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/logging/logging.h" |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame] | 7 | #include "frc971/control_loops/double_jointed_arm/demo_path.h" |
| 8 | #include "frc971/control_loops/double_jointed_arm/dynamics.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | #include "y2018/constants.h" |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame] | 10 | #include "y2018/control_loops/superstructure/arm/arm_constants.h" |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 11 | #include "y2018/control_loops/superstructure/arm/generated_graph.h" |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 12 | |
| 13 | namespace y2018 { |
| 14 | namespace control_loops { |
| 15 | namespace superstructure { |
| 16 | namespace arm { |
| 17 | |
Austin Schuh | 7afcc23 | 2018-09-16 16:33:47 -0700 | [diff] [blame] | 18 | namespace { |
| 19 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 20 | namespace chrono = ::std::chrono; |
| 21 | using ::aos::monotonic_clock; |
| 22 | |
Austin Schuh | 7afcc23 | 2018-09-16 16:33:47 -0700 | [diff] [blame] | 23 | constexpr int kMaxBrownoutCount = 4; |
| 24 | |
| 25 | } // namespace |
| 26 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 27 | Arm::Arm() |
| 28 | : proximal_zeroing_estimator_(constants::GetValues().arm_proximal.zeroing), |
| 29 | distal_zeroing_estimator_(constants::GetValues().arm_distal.zeroing), |
| 30 | alpha_unitizer_((::Eigen::Matrix<double, 2, 2>() << 1.0 / kAlpha0Max(), |
| 31 | 0.0, 0.0, 1.0 / kAlpha1Max()) |
| 32 | .finished()), |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame] | 33 | dynamics_(kArmConstants), |
| 34 | search_graph_(MakeSearchGraph(&dynamics_, &trajectories_, alpha_unitizer_, |
| 35 | kVMax())), |
| 36 | arm_ekf_(&dynamics_), |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 37 | // Go to the start of the first trajectory. |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame] | 38 | follower_(&dynamics_, ReadyAboveBoxPoint()), |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 39 | points_(PointList()) { |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 40 | int i = 0; |
| 41 | for (const auto &trajectory : trajectories_) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 42 | AOS_LOG(INFO, "trajectory length for edge node %d: %f\n", i, |
| 43 | trajectory.trajectory.path().length()); |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 44 | ++i; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | void Arm::Reset() { state_ = State::UNINITIALIZED; } |
| 49 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 50 | flatbuffers::Offset<superstructure::ArmStatus> Arm::Iterate( |
| 51 | const ::aos::monotonic_clock::time_point monotonic_now, |
| 52 | const uint32_t *unsafe_goal, bool grab_box, bool open_claw, bool close_claw, |
| 53 | const superstructure::ArmPosition *position, |
| 54 | const bool claw_beambreak_triggered, |
| 55 | const bool box_back_beambreak_triggered, const bool intake_clear_of_box, |
| 56 | bool suicide, bool trajectory_override, double *proximal_output, |
| 57 | double *distal_output, bool *release_arm_brake, bool *claw_closed, |
| 58 | flatbuffers::FlatBufferBuilder *fbb) { |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 59 | ::Eigen::Matrix<double, 2, 1> Y; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 60 | const bool outputs_disabled = |
| 61 | ((proximal_output == nullptr) || (distal_output == nullptr) || |
| 62 | (release_arm_brake == nullptr) || (claw_closed == nullptr)); |
Austin Schuh | 7afcc23 | 2018-09-16 16:33:47 -0700 | [diff] [blame] | 63 | if (outputs_disabled) { |
| 64 | ++brownout_count_; |
| 65 | } else { |
| 66 | brownout_count_ = 0; |
| 67 | } |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 68 | |
| 69 | uint32_t filtered_goal = 0; |
| 70 | if (unsafe_goal != nullptr) { |
| 71 | filtered_goal = *unsafe_goal; |
| 72 | } |
| 73 | |
| 74 | if (open_claw) { |
| 75 | claw_closed_ = false; |
| 76 | } |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 77 | if (close_claw) { |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 78 | claw_closed_ = true; |
| 79 | } |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 80 | if (outputs_disabled) { |
| 81 | if (claw_closed_count_ == 0) { |
| 82 | claw_closed_ = true; |
| 83 | } else { |
| 84 | --claw_closed_count_; |
| 85 | } |
| 86 | } else { |
| 87 | // Wait this many iterations before closing the claw. That prevents |
| 88 | // brownouts from closing the claw. |
| 89 | claw_closed_count_ = 50; |
| 90 | } |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 91 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | Y << position->proximal()->encoder() + proximal_offset_, |
| 93 | position->distal()->encoder() + distal_offset_; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 94 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 95 | proximal_zeroing_estimator_.UpdateEstimate(*position->proximal()); |
| 96 | distal_zeroing_estimator_.UpdateEstimate(*position->distal()); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 97 | |
| 98 | if (proximal_output != nullptr) { |
| 99 | *proximal_output = 0.0; |
| 100 | } |
| 101 | if (distal_output != nullptr) { |
| 102 | *distal_output = 0.0; |
| 103 | } |
| 104 | |
| 105 | arm_ekf_.Correct(Y, kDt()); |
| 106 | |
| 107 | if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= 0.05 && |
| 108 | ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= 0.05) { |
| 109 | close_enough_for_full_power_ = true; |
| 110 | } |
| 111 | if (::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) >= 1.10 || |
| 112 | ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) >= 1.10) { |
| 113 | close_enough_for_full_power_ = false; |
| 114 | } |
| 115 | |
| 116 | switch (state_) { |
| 117 | case State::UNINITIALIZED: |
| 118 | // Wait in the uninitialized state until the intake is initialized. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 119 | AOS_LOG(DEBUG, "Uninitialized, waiting for intake\n"); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 120 | state_ = State::ZEROING; |
| 121 | proximal_zeroing_estimator_.Reset(); |
| 122 | distal_zeroing_estimator_.Reset(); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 123 | break; |
| 124 | |
| 125 | case State::ZEROING: |
| 126 | // Zero by not moving. |
| 127 | if (proximal_zeroing_estimator_.zeroed() && |
| 128 | distal_zeroing_estimator_.zeroed()) { |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 129 | state_ = State::DISABLED; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 130 | |
| 131 | proximal_offset_ = proximal_zeroing_estimator_.offset(); |
| 132 | distal_offset_ = distal_zeroing_estimator_.offset(); |
| 133 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 134 | Y << position->proximal()->encoder() + proximal_offset_, |
| 135 | position->distal()->encoder() + distal_offset_; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 136 | |
| 137 | // TODO(austin): Offset ekf rather than reset it. Since we aren't |
| 138 | // moving at this point, it's pretty safe to do this. |
| 139 | ::Eigen::Matrix<double, 4, 1> X; |
| 140 | X << Y(0), 0.0, Y(1), 0.0; |
| 141 | arm_ekf_.Reset(X); |
| 142 | } else { |
| 143 | break; |
| 144 | } |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 145 | [[fallthrough]]; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 146 | |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 147 | case State::DISABLED: { |
| 148 | follower_.SwitchTrajectory(nullptr); |
| 149 | close_enough_for_full_power_ = false; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 150 | |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 151 | const ::Eigen::Matrix<double, 2, 1> current_theta = |
| 152 | (::Eigen::Matrix<double, 2, 1>() << arm_ekf_.X_hat(0), |
| 153 | arm_ekf_.X_hat(2)) |
| 154 | .finished(); |
| 155 | uint32_t best_index = 0; |
| 156 | double best_distance = (points_[0] - current_theta).norm(); |
| 157 | uint32_t current_index = 0; |
| 158 | for (const ::Eigen::Matrix<double, 2, 1> &point : points_) { |
| 159 | const double new_distance = (point - current_theta).norm(); |
| 160 | if (new_distance < best_distance) { |
| 161 | best_distance = new_distance; |
| 162 | best_index = current_index; |
| 163 | } |
| 164 | ++current_index; |
| 165 | } |
| 166 | follower_.set_theta(points_[best_index]); |
| 167 | current_node_ = best_index; |
| 168 | |
| 169 | if (!outputs_disabled) { |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 170 | state_ = State::GOTO_PATH; |
| 171 | } else { |
| 172 | break; |
| 173 | } |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 174 | } |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 175 | [[fallthrough]]; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 176 | |
| 177 | case State::GOTO_PATH: |
| 178 | if (outputs_disabled) { |
| 179 | state_ = State::DISABLED; |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 180 | } else if (trajectory_override) { |
| 181 | follower_.SwitchTrajectory(nullptr); |
| 182 | current_node_ = filtered_goal; |
| 183 | follower_.set_theta(points_[current_node_]); |
| 184 | state_ = State::GOTO_PATH; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 185 | } else if (close_enough_for_full_power_) { |
| 186 | state_ = State::RUNNING; |
| 187 | grab_state_ = GrabState::NORMAL; |
| 188 | } |
| 189 | break; |
| 190 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 191 | case State::RUNNING: |
| 192 | // ESTOP if we hit the hard limits. |
| 193 | // TODO(austin): Pick some sane limits. |
| 194 | if (proximal_zeroing_estimator_.error() || |
| 195 | distal_zeroing_estimator_.error()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 196 | AOS_LOG(ERROR, "Zeroing error ESTOP\n"); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 197 | state_ = State::ESTOP; |
Austin Schuh | 7afcc23 | 2018-09-16 16:33:47 -0700 | [diff] [blame] | 198 | } else if (outputs_disabled && brownout_count_ > kMaxBrownoutCount) { |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 199 | state_ = State::DISABLED; |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 200 | } else if (trajectory_override) { |
| 201 | follower_.SwitchTrajectory(nullptr); |
| 202 | current_node_ = filtered_goal; |
| 203 | follower_.set_theta(points_[current_node_]); |
| 204 | state_ = State::GOTO_PATH; |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 205 | } else if (suicide) { |
| 206 | state_ = State::PREP_CLIMB; |
| 207 | climb_count_ = 50; |
| 208 | } |
| 209 | break; |
| 210 | |
| 211 | case State::PREP_CLIMB: |
| 212 | --climb_count_; |
| 213 | if (climb_count_ <= 0) { |
| 214 | state_ = State::ESTOP; |
| 215 | } else if (!suicide) { |
| 216 | state_ = State::RUNNING; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 217 | } |
| 218 | break; |
| 219 | |
| 220 | case State::ESTOP: |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 221 | AOS_LOG(ERROR, "Estop\n"); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 222 | break; |
| 223 | } |
| 224 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 225 | const bool disable = outputs_disabled || (state_ != State::RUNNING && |
| 226 | state_ != State::GOTO_PATH && |
| 227 | state_ != State::PREP_CLIMB); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 228 | if (disable) { |
| 229 | close_enough_for_full_power_ = false; |
| 230 | } |
| 231 | |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 232 | // TODO(austin): Do we need to debounce box_back_beambreak_triggered ? |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 233 | if (claw_closed_) { |
| 234 | if ((filtered_goal == ReadyAboveBoxIndex()) || |
| 235 | (filtered_goal == TallBoxGrabIndex()) || |
| 236 | (filtered_goal == ShortBoxGrabIndex())) { |
| 237 | filtered_goal = NeutralIndex(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // TODO(austin): Do we need to debounce box_back_beambreak_triggered ? |
| 242 | switch (grab_state_) { |
| 243 | case GrabState::NORMAL: |
| 244 | if (grab_box && !claw_closed_) { |
| 245 | grab_state_ = GrabState::WAIT_FOR_BOX; |
| 246 | } else { |
| 247 | break; |
| 248 | } |
| 249 | case GrabState::WAIT_FOR_BOX: |
| 250 | if (!grab_box) { |
| 251 | grab_state_ = GrabState::NORMAL; |
| 252 | } else { |
| 253 | if (AtState(ReadyAboveBoxIndex()) && NearEnd()) { |
| 254 | // We are being asked to grab the box, and the claw is near the box. |
| 255 | if (box_back_beambreak_triggered) { |
| 256 | // And we now see the box! Try for a tall box. |
| 257 | grab_state_ = GrabState::TALL_BOX; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | break; |
| 262 | case GrabState::TALL_BOX: |
| 263 | if (!grab_box) { |
| 264 | grab_state_ = GrabState::NORMAL; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 265 | } else if (AtState(TallBoxGrabIndex()) && NearEnd()) { |
| 266 | // We are being asked to grab the box, and the claw is near the box. |
| 267 | if (claw_beambreak_triggered) { |
| 268 | grab_state_ = GrabState::CLAW_CLOSE; |
| 269 | // Snap time for the delay here. |
Austin Schuh | 20177c9 | 2019-07-07 20:48:24 -0700 | [diff] [blame] | 270 | claw_close_start_time_ = monotonic_now; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 271 | } else { |
| 272 | grab_state_ = GrabState::SHORT_BOX; |
| 273 | } |
| 274 | } |
| 275 | break; |
| 276 | case GrabState::SHORT_BOX: |
| 277 | if (!grab_box) { |
| 278 | grab_state_ = GrabState::NORMAL; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 279 | } else if (AtState(ShortBoxGrabIndex()) && NearEnd()) { |
| 280 | // We are being asked to grab the box, and the claw is near the box. |
| 281 | if (claw_beambreak_triggered) { |
| 282 | grab_state_ = GrabState::CLAW_CLOSE; |
| 283 | // Snap time for the delay here. |
Austin Schuh | 20177c9 | 2019-07-07 20:48:24 -0700 | [diff] [blame] | 284 | claw_close_start_time_ = monotonic_now; |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 285 | } else { |
| 286 | grab_state_ = GrabState::WAIT_FOR_BOX; |
| 287 | } |
| 288 | } |
| 289 | break; |
| 290 | case GrabState::CLAW_CLOSE: |
Austin Schuh | 20177c9 | 2019-07-07 20:48:24 -0700 | [diff] [blame] | 291 | if (monotonic_now > |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 292 | claw_close_start_time_ + ::std::chrono::milliseconds(300)) { |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 293 | grab_state_ = GrabState::OPEN_INTAKE; |
| 294 | } |
| 295 | break; |
| 296 | case GrabState::OPEN_INTAKE: |
| 297 | if (intake_clear_of_box) { |
| 298 | grab_state_ = GrabState::NORMAL; |
| 299 | } |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | // Now, based out our current state, go to the right state. |
| 304 | switch (grab_state_) { |
| 305 | case GrabState::NORMAL: |
| 306 | // Don't let the intake close fully with the claw closed. |
| 307 | // TODO(austin): If we want to transfer the box from the claw to the |
| 308 | // intake, we'll need to change this. |
| 309 | if (claw_closed_) { |
| 310 | max_intake_override_ = -0.5; |
| 311 | } else { |
| 312 | max_intake_override_ = 1000.0; |
| 313 | } |
| 314 | break; |
| 315 | case GrabState::WAIT_FOR_BOX: |
| 316 | filtered_goal = ReadyAboveBoxIndex(); |
| 317 | claw_closed_ = false; |
| 318 | max_intake_override_ = 1000.0; |
| 319 | break; |
| 320 | case GrabState::TALL_BOX: |
| 321 | filtered_goal = TallBoxGrabIndex(); |
| 322 | claw_closed_ = false; |
| 323 | max_intake_override_ = 1000.0; |
| 324 | break; |
| 325 | case GrabState::SHORT_BOX: |
| 326 | filtered_goal = ShortBoxGrabIndex(); |
| 327 | claw_closed_ = false; |
| 328 | max_intake_override_ = 1000.0; |
| 329 | break; |
| 330 | case GrabState::CLAW_CLOSE: |
| 331 | // Don't move. |
| 332 | filtered_goal = current_node_; |
| 333 | claw_closed_ = true; |
| 334 | max_intake_override_ = 1000.0; |
| 335 | break; |
| 336 | case GrabState::OPEN_INTAKE: |
| 337 | // Don't move. |
| 338 | filtered_goal = current_node_; |
| 339 | claw_closed_ = true; |
| 340 | max_intake_override_ = -0.5; |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | if (state_ == State::RUNNING && unsafe_goal != nullptr) { |
| 345 | if (current_node_ != filtered_goal) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 346 | AOS_LOG(INFO, "Goal is different\n"); |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 347 | if (filtered_goal >= search_graph_.num_vertexes()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 348 | AOS_LOG(ERROR, "goal node out of range ESTOP\n"); |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 349 | state_ = State::ESTOP; |
| 350 | } else if (follower_.path_distance_to_go() > 1e-3) { |
| 351 | // Still on the old path segment. Can't change yet. |
| 352 | } else { |
| 353 | search_graph_.SetGoal(filtered_goal); |
| 354 | |
| 355 | size_t min_edge = 0; |
| 356 | double min_cost = ::std::numeric_limits<double>::infinity(); |
| 357 | for (const SearchGraph::HalfEdge &edge : |
| 358 | search_graph_.Neighbors(current_node_)) { |
| 359 | const double cost = search_graph_.GetCostToGoal(edge.dest); |
| 360 | if (cost < min_cost) { |
| 361 | min_edge = edge.edge_id; |
| 362 | min_cost = cost; |
| 363 | } |
| 364 | } |
| 365 | // Ok, now we know which edge we are on. Figure out the path and |
| 366 | // trajectory. |
| 367 | const SearchGraph::Edge &next_edge = search_graph_.edges()[min_edge]; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 368 | AOS_LOG(INFO, "Switching from node %d to %d along edge %d\n", |
| 369 | static_cast<int>(current_node_), |
| 370 | static_cast<int>(next_edge.end), static_cast<int>(min_edge)); |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 371 | vmax_ = trajectories_[min_edge].vmax; |
| 372 | follower_.SwitchTrajectory(&trajectories_[min_edge].trajectory); |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 373 | current_node_ = next_edge.end; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
Austin Schuh | 345a373 | 2018-03-21 20:49:32 -0700 | [diff] [blame] | 378 | const double max_operating_voltage = |
| 379 | close_enough_for_full_power_ |
| 380 | ? kOperatingVoltage() |
| 381 | : (state_ == State::GOTO_PATH ? kGotoPathVMax() : kPathlessVMax()); |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 382 | follower_.Update(arm_ekf_.X_hat(), disable, kDt(), vmax_, |
Austin Schuh | 345a373 | 2018-03-21 20:49:32 -0700 | [diff] [blame] | 383 | max_operating_voltage); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 384 | AOS_LOG(INFO, "Max voltage: %f\n", max_operating_voltage); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 385 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 386 | flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState> |
| 387 | proximal_estimator_state_offset = |
| 388 | proximal_zeroing_estimator_.GetEstimatorState(fbb); |
| 389 | flatbuffers::Offset<frc971::PotAndAbsoluteEncoderEstimatorState> |
| 390 | distal_estimator_state_offset = |
| 391 | distal_zeroing_estimator_.GetEstimatorState(fbb); |
| 392 | |
| 393 | superstructure::ArmStatus::Builder status_builder(*fbb); |
| 394 | status_builder.add_proximal_estimator_state(proximal_estimator_state_offset); |
| 395 | status_builder.add_distal_estimator_state(distal_estimator_state_offset); |
| 396 | |
| 397 | status_builder.add_goal_theta0(follower_.theta(0)); |
| 398 | status_builder.add_goal_theta1(follower_.theta(1)); |
| 399 | status_builder.add_goal_omega0(follower_.omega(0)); |
| 400 | status_builder.add_goal_omega1(follower_.omega(1)); |
| 401 | |
| 402 | status_builder.add_theta0(arm_ekf_.X_hat(0)); |
| 403 | status_builder.add_theta1(arm_ekf_.X_hat(2)); |
| 404 | status_builder.add_omega0(arm_ekf_.X_hat(1)); |
| 405 | status_builder.add_omega1(arm_ekf_.X_hat(3)); |
| 406 | status_builder.add_voltage_error0(arm_ekf_.X_hat(4)); |
| 407 | status_builder.add_voltage_error1(arm_ekf_.X_hat(5)); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 408 | |
| 409 | if (!disable) { |
| 410 | *proximal_output = ::std::max( |
| 411 | -kOperatingVoltage(), ::std::min(kOperatingVoltage(), follower_.U(0))); |
| 412 | *distal_output = ::std::max( |
| 413 | -kOperatingVoltage(), ::std::min(kOperatingVoltage(), follower_.U(1))); |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 414 | if (state_ != State::PREP_CLIMB) { |
| 415 | *release_arm_brake = true; |
| 416 | } else { |
| 417 | *release_arm_brake = false; |
| 418 | } |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 419 | *claw_closed = claw_closed_; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 422 | status_builder.add_path_distance_to_go(follower_.path_distance_to_go()); |
| 423 | status_builder.add_current_node(current_node_); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 424 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 425 | status_builder.add_zeroed(zeroed()); |
| 426 | status_builder.add_estopped(estopped()); |
| 427 | status_builder.add_state(static_cast<int32_t>(state_)); |
| 428 | status_builder.add_grab_state(static_cast<int32_t>(grab_state_)); |
| 429 | status_builder.add_failed_solutions(follower_.failed_solutions()); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 430 | |
| 431 | arm_ekf_.Predict(follower_.U(), kDt()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 432 | return status_builder.Finish(); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | } // namespace arm |
| 436 | } // namespace superstructure |
| 437 | } // namespace control_loops |
| 438 | } // namespace y2018 |